1//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements semantic analysis for OpenMP directives and
10/// clauses.
11///
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/SemaOpenMP.h"
15#include "clang/AST/ASTConsumer.h"
16
17#include "TreeTransform.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTMutationListener.h"
20#include "clang/AST/CXXInheritance.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclOpenMP.h"
24#include "clang/AST/DynamicRecursiveASTVisitor.h"
25#include "clang/AST/OpenMPClause.h"
26#include "clang/AST/StmtCXX.h"
27#include "clang/AST/StmtOpenMP.h"
28#include "clang/AST/StmtVisitor.h"
29#include "clang/Basic/DiagnosticSema.h"
30#include "clang/Basic/OpenMPKinds.h"
31#include "clang/Basic/PartialDiagnostic.h"
32#include "clang/Basic/TargetInfo.h"
33#include "clang/Sema/EnterExpressionEvaluationContext.h"
34#include "clang/Sema/Initialization.h"
35#include "clang/Sema/Lookup.h"
36#include "clang/Sema/ParsedAttr.h"
37#include "clang/Sema/Scope.h"
38#include "clang/Sema/ScopeInfo.h"
39#include "clang/Sema/Sema.h"
40#include "llvm/ADT/IndexedMap.h"
41#include "llvm/ADT/PointerEmbeddedInt.h"
42#include "llvm/ADT/STLExtras.h"
43#include "llvm/ADT/Sequence.h"
44#include "llvm/ADT/SetVector.h"
45#include "llvm/ADT/SmallSet.h"
46#include "llvm/ADT/StringExtras.h"
47#include "llvm/Frontend/OpenMP/OMPAssume.h"
48#include "llvm/Frontend/OpenMP/OMPConstants.h"
49#include "llvm/IR/Assumptions.h"
50#include <optional>
51
52using namespace clang;
53using namespace llvm::omp;
54
55//===----------------------------------------------------------------------===//
56// Stack of data-sharing attributes for variables
57//===----------------------------------------------------------------------===//
58
59static const Expr *checkMapClauseExpressionBase(
60 Sema &SemaRef, Expr *E,
61 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
62 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose);
63
64static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C);
65
66namespace {
67/// Default data sharing attributes, which can be applied to directive.
68enum DefaultDataSharingAttributes {
69 DSA_unspecified = 0, /// Data sharing attribute not specified.
70 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
71 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
72 DSA_private = 1 << 2, /// Default data sharing attribute 'private'.
73 DSA_firstprivate = 1 << 3, /// Default data sharing attribute 'firstprivate'.
74};
75
76/// Variable Category attributes to restrict the modifier of the
77/// default clause (DefaultDataSharingAttributes)
78/// Not mentioning any Variable category attribute indicates
79/// the modifier (DefaultDataSharingAttributes) is for all variables.
80enum DefaultDataSharingVCAttributes {
81 DSA_VC_all = 0, /// for all variables.
82 DSA_VC_aggregate, /// for aggregate variables.
83 DSA_VC_pointer, /// for pointer variables.
84 DSA_VC_scalar, /// for scalar variables.
85};
86
87/// Stack for tracking declarations used in OpenMP directives and
88/// clauses and their data-sharing attributes.
89class DSAStackTy {
90public:
91 struct DSAVarData {
92 OpenMPDirectiveKind DKind = OMPD_unknown;
93 OpenMPClauseKind CKind = OMPC_unknown;
94 unsigned Modifier = 0;
95 const Expr *RefExpr = nullptr;
96 DeclRefExpr *PrivateCopy = nullptr;
97 SourceLocation ImplicitDSALoc;
98 bool AppliedToPointee = false;
99 DSAVarData() = default;
100 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
101 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
102 SourceLocation ImplicitDSALoc, unsigned Modifier,
103 bool AppliedToPointee)
104 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
105 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc),
106 AppliedToPointee(AppliedToPointee) {}
107 };
108 using OperatorOffsetTy =
109 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
110 using DoacrossClauseMapTy = llvm::DenseMap<OMPClause *, OperatorOffsetTy>;
111 /// Kind of the declaration used in the uses_allocators clauses.
112 enum class UsesAllocatorsDeclKind {
113 /// Predefined allocator
114 PredefinedAllocator,
115 /// User-defined allocator
116 UserDefinedAllocator,
117 /// The declaration that represent allocator trait
118 AllocatorTrait,
119 };
120
121private:
122 struct DSAInfo {
123 OpenMPClauseKind Attributes = OMPC_unknown;
124 unsigned Modifier = 0;
125 /// Pointer to a reference expression and a flag which shows that the
126 /// variable is marked as lastprivate(true) or not (false).
127 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
128 DeclRefExpr *PrivateCopy = nullptr;
129 /// true if the attribute is applied to the pointee, not the variable
130 /// itself.
131 bool AppliedToPointee = false;
132 };
133 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
134 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
135 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
136 using LoopControlVariablesMapTy =
137 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
138 /// Struct that associates a component with the clause kind where they are
139 /// found.
140 struct MappedExprComponentTy {
141 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
142 OpenMPClauseKind Kind = OMPC_unknown;
143 };
144 using MappedExprComponentsTy =
145 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
146 using CriticalsWithHintsTy =
147 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
148 struct ReductionData {
149 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
150 SourceRange ReductionRange;
151 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
152 ReductionData() = default;
153 void set(BinaryOperatorKind BO, SourceRange RR) {
154 ReductionRange = RR;
155 ReductionOp = BO;
156 }
157 void set(const Expr *RefExpr, SourceRange RR) {
158 ReductionRange = RR;
159 ReductionOp = RefExpr;
160 }
161 };
162 using DeclReductionMapTy =
163 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
164 struct DefaultmapInfo {
165 OpenMPDefaultmapClauseModifier ImplicitBehavior =
166 OMPC_DEFAULTMAP_MODIFIER_unknown;
167 SourceLocation SLoc;
168 DefaultmapInfo() = default;
169 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
170 : ImplicitBehavior(M), SLoc(Loc) {}
171 };
172
173 struct SharingMapTy {
174 DeclSAMapTy SharingMap;
175 DeclReductionMapTy ReductionMap;
176 UsedRefMapTy AlignedMap;
177 UsedRefMapTy NontemporalMap;
178 MappedExprComponentsTy MappedExprComponents;
179 LoopControlVariablesMapTy LCVMap;
180 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
181 SourceLocation DefaultAttrLoc;
182 DefaultDataSharingVCAttributes DefaultVCAttr = DSA_VC_all;
183 SourceLocation DefaultAttrVCLoc;
184 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown + 1];
185 OpenMPDirectiveKind Directive = OMPD_unknown;
186 DeclarationNameInfo DirectiveName;
187 Scope *CurScope = nullptr;
188 DeclContext *Context = nullptr;
189 SourceLocation ConstructLoc;
190 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
191 /// get the data (loop counters etc.) about enclosing loop-based construct.
192 /// This data is required during codegen.
193 DoacrossClauseMapTy DoacrossDepends;
194 /// First argument (Expr *) contains optional argument of the
195 /// 'ordered' clause, the second one is true if the regions has 'ordered'
196 /// clause, false otherwise.
197 std::optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
198 bool RegionHasOrderConcurrent = false;
199 unsigned AssociatedLoops = 1;
200 bool HasMutipleLoops = false;
201 const Decl *PossiblyLoopCounter = nullptr;
202 bool NowaitRegion = false;
203 bool UntiedRegion = false;
204 bool CancelRegion = false;
205 bool LoopStart = false;
206 bool BodyComplete = false;
207 SourceLocation PrevScanLocation;
208 SourceLocation PrevOrderedLocation;
209 SourceLocation InnerTeamsRegionLoc;
210 /// Reference to the taskgroup task_reduction reference expression.
211 Expr *TaskgroupReductionRef = nullptr;
212 llvm::DenseSet<QualType> MappedClassesQualTypes;
213 SmallVector<Expr *, 4> InnerUsedAllocators;
214 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
215 /// List of globals marked as declare target link in this target region
216 /// (isOpenMPTargetExecutionDirective(Directive) == true).
217 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
218 /// List of decls used in inclusive/exclusive clauses of the scan directive.
219 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
220 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
221 UsesAllocatorsDecls;
222 /// Data is required on creating capture fields for implicit
223 /// default first|private clause.
224 struct ImplicitDefaultFDInfoTy {
225 /// Field decl.
226 const FieldDecl *FD = nullptr;
227 /// Nesting stack level
228 size_t StackLevel = 0;
229 /// Capture variable decl.
230 VarDecl *VD = nullptr;
231 ImplicitDefaultFDInfoTy(const FieldDecl *FD, size_t StackLevel,
232 VarDecl *VD)
233 : FD(FD), StackLevel(StackLevel), VD(VD) {}
234 };
235 /// List of captured fields
236 llvm::SmallVector<ImplicitDefaultFDInfoTy, 8>
237 ImplicitDefaultFirstprivateFDs;
238 Expr *DeclareMapperVar = nullptr;
239 SmallVector<VarDecl *, 16> IteratorVarDecls;
240 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
241 Scope *CurScope, SourceLocation Loc)
242 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
243 ConstructLoc(Loc) {}
244 SharingMapTy() = default;
245 };
246
247 using StackTy = SmallVector<SharingMapTy, 4>;
248
249 /// Stack of used declaration and their data-sharing attributes.
250 DeclSAMapTy Threadprivates;
251 DeclSAMapTy Groupprivates;
252 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
253 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
254 /// true, if check for DSA must be from parent directive, false, if
255 /// from current directive.
256 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
257 Sema &SemaRef;
258 bool ForceCapturing = false;
259 /// true if all the variables in the target executable directives must be
260 /// captured by reference.
261 bool ForceCaptureByReferenceInTargetExecutable = false;
262 CriticalsWithHintsTy Criticals;
263 unsigned IgnoredStackElements = 0;
264
265 /// Iterators over the stack iterate in order from innermost to outermost
266 /// directive.
267 using const_iterator = StackTy::const_reverse_iterator;
268 const_iterator begin() const {
269 return Stack.empty() ? const_iterator()
270 : Stack.back().first.rbegin() + IgnoredStackElements;
271 }
272 const_iterator end() const {
273 return Stack.empty() ? const_iterator() : Stack.back().first.rend();
274 }
275 using iterator = StackTy::reverse_iterator;
276 iterator begin() {
277 return Stack.empty() ? iterator()
278 : Stack.back().first.rbegin() + IgnoredStackElements;
279 }
280 iterator end() {
281 return Stack.empty() ? iterator() : Stack.back().first.rend();
282 }
283
284 // Convenience operations to get at the elements of the stack.
285
286 bool isStackEmpty() const {
287 return Stack.empty() ||
288 Stack.back().second != CurrentNonCapturingFunctionScope ||
289 Stack.back().first.size() <= IgnoredStackElements;
290 }
291 size_t getStackSize() const {
292 return isStackEmpty() ? 0
293 : Stack.back().first.size() - IgnoredStackElements;
294 }
295
296 SharingMapTy *getTopOfStackOrNull() {
297 size_t Size = getStackSize();
298 if (Size == 0)
299 return nullptr;
300 return &Stack.back().first[Size - 1];
301 }
302 const SharingMapTy *getTopOfStackOrNull() const {
303 return const_cast<DSAStackTy &>(*this).getTopOfStackOrNull();
304 }
305 SharingMapTy &getTopOfStack() {
306 assert(!isStackEmpty() && "no current directive");
307 return *getTopOfStackOrNull();
308 }
309 const SharingMapTy &getTopOfStack() const {
310 return const_cast<DSAStackTy &>(*this).getTopOfStack();
311 }
312
313 SharingMapTy *getSecondOnStackOrNull() {
314 size_t Size = getStackSize();
315 if (Size <= 1)
316 return nullptr;
317 return &Stack.back().first[Size - 2];
318 }
319 const SharingMapTy *getSecondOnStackOrNull() const {
320 return const_cast<DSAStackTy &>(*this).getSecondOnStackOrNull();
321 }
322
323 /// Get the stack element at a certain level (previously returned by
324 /// \c getNestingLevel).
325 ///
326 /// Note that nesting levels count from outermost to innermost, and this is
327 /// the reverse of our iteration order where new inner levels are pushed at
328 /// the front of the stack.
329 SharingMapTy &getStackElemAtLevel(unsigned Level) {
330 assert(Level < getStackSize() && "no such stack element");
331 return Stack.back().first[Level];
332 }
333 const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
334 return const_cast<DSAStackTy &>(*this).getStackElemAtLevel(Level);
335 }
336
337 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
338
339 /// Checks if the variable is a local for OpenMP region.
340 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
341
342 /// Vector of previously declared requires directives
343 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
344 /// omp_allocator_handle_t type.
345 QualType OMPAllocatorHandleT;
346 /// omp_depend_t type.
347 QualType OMPDependT;
348 /// omp_event_handle_t type.
349 QualType OMPEventHandleT;
350 /// omp_alloctrait_t type.
351 QualType OMPAlloctraitT;
352 /// Expression for the predefined allocators.
353 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
354 nullptr};
355 /// Vector of previously encountered target directives
356 SmallVector<SourceLocation, 2> TargetLocations;
357 SourceLocation AtomicLocation;
358 /// Vector of declare variant construct traits.
359 SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits;
360
361public:
362 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
363
364 /// Sets omp_allocator_handle_t type.
365 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
366 /// Gets omp_allocator_handle_t type.
367 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
368 /// Sets omp_alloctrait_t type.
369 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
370 /// Gets omp_alloctrait_t type.
371 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
372 /// Sets the given default allocator.
373 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
374 Expr *Allocator) {
375 OMPPredefinedAllocators[AllocatorKind] = Allocator;
376 }
377 /// Returns the specified default allocator.
378 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
379 return OMPPredefinedAllocators[AllocatorKind];
380 }
381 /// Sets omp_depend_t type.
382 void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
383 /// Gets omp_depend_t type.
384 QualType getOMPDependT() const { return OMPDependT; }
385
386 /// Sets omp_event_handle_t type.
387 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
388 /// Gets omp_event_handle_t type.
389 QualType getOMPEventHandleT() const { return OMPEventHandleT; }
390
391 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
392 OpenMPClauseKind getClauseParsingMode() const {
393 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
394 return ClauseKindMode;
395 }
396 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
397
398 bool isBodyComplete() const {
399 const SharingMapTy *Top = getTopOfStackOrNull();
400 return Top && Top->BodyComplete;
401 }
402 void setBodyComplete() { getTopOfStack().BodyComplete = true; }
403
404 bool isForceVarCapturing() const { return ForceCapturing; }
405 void setForceVarCapturing(bool V) { ForceCapturing = V; }
406
407 void setForceCaptureByReferenceInTargetExecutable(bool V) {
408 ForceCaptureByReferenceInTargetExecutable = V;
409 }
410 bool isForceCaptureByReferenceInTargetExecutable() const {
411 return ForceCaptureByReferenceInTargetExecutable;
412 }
413
414 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
415 Scope *CurScope, SourceLocation Loc) {
416 assert(!IgnoredStackElements &&
417 "cannot change stack while ignoring elements");
418 if (Stack.empty() ||
419 Stack.back().second != CurrentNonCapturingFunctionScope)
420 Stack.emplace_back(Args: StackTy(), Args&: CurrentNonCapturingFunctionScope);
421 Stack.back().first.emplace_back(Args&: DKind, Args: DirName, Args&: CurScope, Args&: Loc);
422 Stack.back().first.back().DefaultAttrLoc = Loc;
423 }
424
425 void pop() {
426 assert(!IgnoredStackElements &&
427 "cannot change stack while ignoring elements");
428 assert(!Stack.back().first.empty() &&
429 "Data-sharing attributes stack is empty!");
430 Stack.back().first.pop_back();
431 }
432
433 /// RAII object to temporarily leave the scope of a directive when we want to
434 /// logically operate in its parent.
435 class ParentDirectiveScope {
436 DSAStackTy &Self;
437 bool Active;
438
439 public:
440 ParentDirectiveScope(DSAStackTy &Self, bool Activate)
441 : Self(Self), Active(false) {
442 if (Activate)
443 enable();
444 }
445 ~ParentDirectiveScope() { disable(); }
446 void disable() {
447 if (Active) {
448 --Self.IgnoredStackElements;
449 Active = false;
450 }
451 }
452 void enable() {
453 if (!Active) {
454 ++Self.IgnoredStackElements;
455 Active = true;
456 }
457 }
458 };
459
460 /// Marks that we're started loop parsing.
461 void loopInit() {
462 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
463 "Expected loop-based directive.");
464 getTopOfStack().LoopStart = true;
465 }
466 /// Start capturing of the variables in the loop context.
467 void loopStart() {
468 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
469 "Expected loop-based directive.");
470 getTopOfStack().LoopStart = false;
471 }
472 /// true, if variables are captured, false otherwise.
473 bool isLoopStarted() const {
474 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
475 "Expected loop-based directive.");
476 return !getTopOfStack().LoopStart;
477 }
478 /// Marks (or clears) declaration as possibly loop counter.
479 void resetPossibleLoopCounter(const Decl *D = nullptr) {
480 getTopOfStack().PossiblyLoopCounter = D ? D->getCanonicalDecl() : D;
481 }
482 /// Gets the possible loop counter decl.
483 const Decl *getPossiblyLoopCounter() const {
484 return getTopOfStack().PossiblyLoopCounter;
485 }
486 /// Start new OpenMP region stack in new non-capturing function.
487 void pushFunction() {
488 assert(!IgnoredStackElements &&
489 "cannot change stack while ignoring elements");
490 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
491 assert(!isa<CapturingScopeInfo>(CurFnScope));
492 CurrentNonCapturingFunctionScope = CurFnScope;
493 }
494 /// Pop region stack for non-capturing function.
495 void popFunction(const FunctionScopeInfo *OldFSI) {
496 assert(!IgnoredStackElements &&
497 "cannot change stack while ignoring elements");
498 if (!Stack.empty() && Stack.back().second == OldFSI) {
499 assert(Stack.back().first.empty());
500 Stack.pop_back();
501 }
502 CurrentNonCapturingFunctionScope = nullptr;
503 for (const FunctionScopeInfo *FSI : llvm::reverse(C&: SemaRef.FunctionScopes)) {
504 if (!isa<CapturingScopeInfo>(Val: FSI)) {
505 CurrentNonCapturingFunctionScope = FSI;
506 break;
507 }
508 }
509 }
510
511 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
512 Criticals.try_emplace(Key: D->getDirectiveName().getAsString(), Args&: D, Args&: Hint);
513 }
514 std::pair<const OMPCriticalDirective *, llvm::APSInt>
515 getCriticalWithHint(const DeclarationNameInfo &Name) const {
516 auto I = Criticals.find(Key: Name.getAsString());
517 if (I != Criticals.end())
518 return I->second;
519 return std::make_pair(x: nullptr, y: llvm::APSInt());
520 }
521 /// If 'aligned' declaration for given variable \a D was not seen yet,
522 /// add it and return NULL; otherwise return previous occurrence's expression
523 /// for diagnostics.
524 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
525 /// If 'nontemporal' declaration for given variable \a D was not seen yet,
526 /// add it and return NULL; otherwise return previous occurrence's expression
527 /// for diagnostics.
528 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
529
530 /// Register specified variable as loop control variable.
531 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
532 /// Check if the specified variable is a loop control variable for
533 /// current region.
534 /// \return The index of the loop control variable in the list of associated
535 /// for-loops (from outer to inner).
536 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
537 /// Check if the specified variable is a loop control variable for
538 /// parent region.
539 /// \return The index of the loop control variable in the list of associated
540 /// for-loops (from outer to inner).
541 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
542 /// Check if the specified variable is a loop control variable for
543 /// current region.
544 /// \return The index of the loop control variable in the list of associated
545 /// for-loops (from outer to inner).
546 const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
547 unsigned Level) const;
548 /// Get the loop control variable for the I-th loop (or nullptr) in
549 /// parent directive.
550 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
551
552 /// Marks the specified decl \p D as used in scan directive.
553 void markDeclAsUsedInScanDirective(ValueDecl *D) {
554 if (SharingMapTy *Stack = getSecondOnStackOrNull())
555 Stack->UsedInScanDirective.insert(V: D);
556 }
557
558 /// Checks if the specified declaration was used in the inner scan directive.
559 bool isUsedInScanDirective(ValueDecl *D) const {
560 if (const SharingMapTy *Stack = getTopOfStackOrNull())
561 return Stack->UsedInScanDirective.contains(V: D);
562 return false;
563 }
564
565 /// Adds explicit data sharing attribute to the specified declaration.
566 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
567 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
568 bool AppliedToPointee = false);
569
570 /// Adds additional information for the reduction items with the reduction id
571 /// represented as an operator.
572 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
573 BinaryOperatorKind BOK);
574 /// Adds additional information for the reduction items with the reduction id
575 /// represented as reduction identifier.
576 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
577 const Expr *ReductionRef);
578 /// Returns the location and reduction operation from the innermost parent
579 /// region for the given \p D.
580 const DSAVarData
581 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
582 BinaryOperatorKind &BOK,
583 Expr *&TaskgroupDescriptor) const;
584 /// Returns the location and reduction operation from the innermost parent
585 /// region for the given \p D.
586 const DSAVarData
587 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
588 const Expr *&ReductionRef,
589 Expr *&TaskgroupDescriptor) const;
590 /// Return reduction reference expression for the current taskgroup or
591 /// parallel/worksharing directives with task reductions.
592 Expr *getTaskgroupReductionRef() const {
593 assert((getTopOfStack().Directive == OMPD_taskgroup ||
594 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
595 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
596 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
597 "taskgroup reference expression requested for non taskgroup or "
598 "parallel/worksharing directive.");
599 return getTopOfStack().TaskgroupReductionRef;
600 }
601 /// Checks if the given \p VD declaration is actually a taskgroup reduction
602 /// descriptor variable at the \p Level of OpenMP regions.
603 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
604 return getStackElemAtLevel(Level).TaskgroupReductionRef &&
605 cast<DeclRefExpr>(Val: getStackElemAtLevel(Level).TaskgroupReductionRef)
606 ->getDecl() == VD;
607 }
608
609 /// Returns data sharing attributes from top of the stack for the
610 /// specified declaration.
611 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
612 /// Returns data-sharing attributes for the specified declaration.
613 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
614 /// Returns data-sharing attributes for the specified declaration.
615 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
616 /// Checks if the specified variables has data-sharing attributes which
617 /// match specified \a CPred predicate in any directive which matches \a DPred
618 /// predicate.
619 const DSAVarData
620 hasDSA(ValueDecl *D,
621 const llvm::function_ref<bool(OpenMPClauseKind, bool,
622 DefaultDataSharingAttributes)>
623 CPred,
624 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
625 bool FromParent) const;
626 /// Checks if the specified variables has data-sharing attributes which
627 /// match specified \a CPred predicate in any innermost directive which
628 /// matches \a DPred predicate.
629 const DSAVarData
630 hasInnermostDSA(ValueDecl *D,
631 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
632 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
633 bool FromParent) const;
634 /// Checks if the specified variables has explicit data-sharing
635 /// attributes which match specified \a CPred predicate at the specified
636 /// OpenMP region.
637 bool
638 hasExplicitDSA(const ValueDecl *D,
639 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
640 unsigned Level, bool NotLastprivate = false) const;
641
642 /// Returns true if the directive at level \Level matches in the
643 /// specified \a DPred predicate.
644 bool hasExplicitDirective(
645 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
646 unsigned Level) const;
647
648 /// Finds a directive which matches specified \a DPred predicate.
649 bool hasDirective(
650 const llvm::function_ref<bool(
651 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
652 DPred,
653 bool FromParent) const;
654
655 /// Returns currently analyzed directive.
656 OpenMPDirectiveKind getCurrentDirective() const {
657 const SharingMapTy *Top = getTopOfStackOrNull();
658 return Top ? Top->Directive : OMPD_unknown;
659 }
660 /// Returns directive kind at specified level.
661 OpenMPDirectiveKind getDirective(unsigned Level) const {
662 assert(!isStackEmpty() && "No directive at specified level.");
663 return getStackElemAtLevel(Level).Directive;
664 }
665 /// Returns the capture region at the specified level.
666 OpenMPDirectiveKind getCaptureRegion(unsigned Level,
667 unsigned OpenMPCaptureLevel) const {
668 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
669 getOpenMPCaptureRegions(CaptureRegions, DKind: getDirective(Level));
670 return CaptureRegions[OpenMPCaptureLevel];
671 }
672 /// Returns parent directive.
673 OpenMPDirectiveKind getParentDirective() const {
674 const SharingMapTy *Parent = getSecondOnStackOrNull();
675 return Parent ? Parent->Directive : OMPD_unknown;
676 }
677
678 /// Add requires decl to internal vector
679 void addRequiresDecl(OMPRequiresDecl *RD) { RequiresDecls.push_back(Elt: RD); }
680
681 /// Checks if the defined 'requires' directive has specified type of clause.
682 template <typename ClauseType> bool hasRequiresDeclWithClause() const {
683 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
684 return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
685 return isa<ClauseType>(C);
686 });
687 });
688 }
689
690 /// Checks for a duplicate clause amongst previously declared requires
691 /// directives
692 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
693 bool IsDuplicate = false;
694 for (OMPClause *CNew : ClauseList) {
695 for (const OMPRequiresDecl *D : RequiresDecls) {
696 for (const OMPClause *CPrev : D->clauselists()) {
697 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
698 SemaRef.Diag(Loc: CNew->getBeginLoc(),
699 DiagID: diag::err_omp_requires_clause_redeclaration)
700 << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
701 SemaRef.Diag(Loc: CPrev->getBeginLoc(),
702 DiagID: diag::note_omp_requires_previous_clause)
703 << getOpenMPClauseNameForDiag(C: CPrev->getClauseKind());
704 IsDuplicate = true;
705 }
706 }
707 }
708 }
709 return IsDuplicate;
710 }
711
712 /// Add location of previously encountered target to internal vector
713 void addTargetDirLocation(SourceLocation LocStart) {
714 TargetLocations.push_back(Elt: LocStart);
715 }
716
717 /// Add location for the first encountered atomic directive.
718 void addAtomicDirectiveLoc(SourceLocation Loc) {
719 if (AtomicLocation.isInvalid())
720 AtomicLocation = Loc;
721 }
722
723 /// Returns the location of the first encountered atomic directive in the
724 /// module.
725 SourceLocation getAtomicDirectiveLoc() const { return AtomicLocation; }
726
727 // Return previously encountered target region locations.
728 ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
729 return TargetLocations;
730 }
731
732 /// Set default data sharing attribute to none.
733 void setDefaultDSANone(SourceLocation Loc) {
734 getTopOfStack().DefaultAttr = DSA_none;
735 getTopOfStack().DefaultAttrLoc = Loc;
736 }
737 /// Set default data sharing attribute to shared.
738 void setDefaultDSAShared(SourceLocation Loc) {
739 getTopOfStack().DefaultAttr = DSA_shared;
740 getTopOfStack().DefaultAttrLoc = Loc;
741 }
742 /// Set default data sharing attribute to private.
743 void setDefaultDSAPrivate(SourceLocation Loc) {
744 getTopOfStack().DefaultAttr = DSA_private;
745 getTopOfStack().DefaultAttrLoc = Loc;
746 }
747 /// Set default data sharing attribute to firstprivate.
748 void setDefaultDSAFirstPrivate(SourceLocation Loc) {
749 getTopOfStack().DefaultAttr = DSA_firstprivate;
750 getTopOfStack().DefaultAttrLoc = Loc;
751 }
752 /// Set default data sharing variable category attribute to aggregate.
753 void setDefaultDSAVCAggregate(SourceLocation VCLoc) {
754 getTopOfStack().DefaultVCAttr = DSA_VC_aggregate;
755 getTopOfStack().DefaultAttrVCLoc = VCLoc;
756 }
757 /// Set default data sharing variable category attribute to all.
758 void setDefaultDSAVCAll(SourceLocation VCLoc) {
759 getTopOfStack().DefaultVCAttr = DSA_VC_all;
760 getTopOfStack().DefaultAttrVCLoc = VCLoc;
761 }
762 /// Set default data sharing variable category attribute to pointer.
763 void setDefaultDSAVCPointer(SourceLocation VCLoc) {
764 getTopOfStack().DefaultVCAttr = DSA_VC_pointer;
765 getTopOfStack().DefaultAttrVCLoc = VCLoc;
766 }
767 /// Set default data sharing variable category attribute to scalar.
768 void setDefaultDSAVCScalar(SourceLocation VCLoc) {
769 getTopOfStack().DefaultVCAttr = DSA_VC_scalar;
770 getTopOfStack().DefaultAttrVCLoc = VCLoc;
771 }
772 /// Set default data mapping attribute to Modifier:Kind
773 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
774 OpenMPDefaultmapClauseKind Kind, SourceLocation Loc) {
775 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
776 DMI.ImplicitBehavior = M;
777 DMI.SLoc = Loc;
778 }
779 /// Check whether the implicit-behavior has been set in defaultmap
780 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
781 if (VariableCategory == OMPC_DEFAULTMAP_unknown)
782 return getTopOfStack()
783 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
784 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
785 getTopOfStack()
786 .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
787 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
788 getTopOfStack()
789 .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
790 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
791 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
792 OMPC_DEFAULTMAP_MODIFIER_unknown;
793 }
794
795 ArrayRef<llvm::omp::TraitProperty> getConstructTraits() {
796 return ConstructTraits;
797 }
798 void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits,
799 bool ScopeEntry) {
800 if (ScopeEntry)
801 ConstructTraits.append(in_start: Traits.begin(), in_end: Traits.end());
802 else
803 for (llvm::omp::TraitProperty Trait : llvm::reverse(C&: Traits)) {
804 llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val();
805 assert(Top == Trait && "Something left a trait on the stack!");
806 (void)Trait;
807 (void)Top;
808 }
809 }
810
811 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
812 return getStackSize() <= Level ? DSA_unspecified
813 : getStackElemAtLevel(Level).DefaultAttr;
814 }
815 DefaultDataSharingAttributes getDefaultDSA() const {
816 return isStackEmpty() ? DSA_unspecified : getTopOfStack().DefaultAttr;
817 }
818 SourceLocation getDefaultDSALocation() const {
819 return isStackEmpty() ? SourceLocation() : getTopOfStack().DefaultAttrLoc;
820 }
821 OpenMPDefaultmapClauseModifier
822 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
823 return isStackEmpty()
824 ? OMPC_DEFAULTMAP_MODIFIER_unknown
825 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
826 }
827 OpenMPDefaultmapClauseModifier
828 getDefaultmapModifierAtLevel(unsigned Level,
829 OpenMPDefaultmapClauseKind Kind) const {
830 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
831 }
832 bool isDefaultmapCapturedByRef(unsigned Level,
833 OpenMPDefaultmapClauseKind Kind) const {
834 OpenMPDefaultmapClauseModifier M =
835 getDefaultmapModifierAtLevel(Level, Kind);
836 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
837 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
838 (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
839 (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
840 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom) ||
841 (M == OMPC_DEFAULTMAP_MODIFIER_present) ||
842 (M == OMPC_DEFAULTMAP_MODIFIER_storage);
843 }
844 return true;
845 }
846 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
847 OpenMPDefaultmapClauseKind Kind) {
848 switch (Kind) {
849 case OMPC_DEFAULTMAP_scalar:
850 case OMPC_DEFAULTMAP_pointer:
851 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
852 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
853 (M == OMPC_DEFAULTMAP_MODIFIER_default);
854 case OMPC_DEFAULTMAP_aggregate:
855 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
856 default:
857 break;
858 }
859 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
860 }
861 bool mustBeFirstprivateAtLevel(unsigned Level,
862 OpenMPDefaultmapClauseKind Kind) const {
863 OpenMPDefaultmapClauseModifier M =
864 getDefaultmapModifierAtLevel(Level, Kind);
865 return mustBeFirstprivateBase(M, Kind);
866 }
867 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
868 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
869 return mustBeFirstprivateBase(M, Kind);
870 }
871
872 /// Checks if the specified variable is a threadprivate.
873 bool isThreadPrivate(VarDecl *D) {
874 const DSAVarData DVar = getTopDSA(D, FromParent: false);
875 return isOpenMPThreadPrivate(Kind: DVar.CKind);
876 }
877
878 /// Marks current region as ordered (it has an 'ordered' clause).
879 void setOrderedRegion(bool IsOrdered, const Expr *Param,
880 OMPOrderedClause *Clause) {
881 if (IsOrdered)
882 getTopOfStack().OrderedRegion.emplace(args&: Param, args&: Clause);
883 else
884 getTopOfStack().OrderedRegion.reset();
885 }
886 /// Returns true, if region is ordered (has associated 'ordered' clause),
887 /// false - otherwise.
888 bool isOrderedRegion() const {
889 if (const SharingMapTy *Top = getTopOfStackOrNull())
890 return Top->OrderedRegion.has_value();
891 return false;
892 }
893 /// Returns optional parameter for the ordered region.
894 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
895 if (const SharingMapTy *Top = getTopOfStackOrNull())
896 if (Top->OrderedRegion)
897 return *Top->OrderedRegion;
898 return std::make_pair(x: nullptr, y: nullptr);
899 }
900 /// Returns true, if parent region is ordered (has associated
901 /// 'ordered' clause), false - otherwise.
902 bool isParentOrderedRegion() const {
903 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
904 return Parent->OrderedRegion.has_value();
905 return false;
906 }
907 /// Returns optional parameter for the ordered region.
908 std::pair<const Expr *, OMPOrderedClause *>
909 getParentOrderedRegionParam() const {
910 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
911 if (Parent->OrderedRegion)
912 return *Parent->OrderedRegion;
913 return std::make_pair(x: nullptr, y: nullptr);
914 }
915 /// Marks current region as having an 'order' clause.
916 void setRegionHasOrderConcurrent(bool HasOrderConcurrent) {
917 getTopOfStack().RegionHasOrderConcurrent = HasOrderConcurrent;
918 }
919 /// Returns true, if parent region is order (has associated
920 /// 'order' clause), false - otherwise.
921 bool isParentOrderConcurrent() const {
922 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
923 return Parent->RegionHasOrderConcurrent;
924 return false;
925 }
926 /// Marks current region as nowait (it has a 'nowait' clause).
927 void setNowaitRegion(bool IsNowait = true) {
928 getTopOfStack().NowaitRegion = IsNowait;
929 }
930 /// Returns true, if parent region is nowait (has associated
931 /// 'nowait' clause), false - otherwise.
932 bool isParentNowaitRegion() const {
933 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
934 return Parent->NowaitRegion;
935 return false;
936 }
937 /// Marks current region as untied (it has a 'untied' clause).
938 void setUntiedRegion(bool IsUntied = true) {
939 getTopOfStack().UntiedRegion = IsUntied;
940 }
941 /// Return true if current region is untied.
942 bool isUntiedRegion() const {
943 const SharingMapTy *Top = getTopOfStackOrNull();
944 return Top ? Top->UntiedRegion : false;
945 }
946 /// Marks parent region as cancel region.
947 void setParentCancelRegion(bool Cancel = true) {
948 if (SharingMapTy *Parent = getSecondOnStackOrNull())
949 Parent->CancelRegion |= Cancel;
950 }
951 /// Return true if current region has inner cancel construct.
952 bool isCancelRegion() const {
953 const SharingMapTy *Top = getTopOfStackOrNull();
954 return Top ? Top->CancelRegion : false;
955 }
956
957 /// Mark that parent region already has scan directive.
958 void setParentHasScanDirective(SourceLocation Loc) {
959 if (SharingMapTy *Parent = getSecondOnStackOrNull())
960 Parent->PrevScanLocation = Loc;
961 }
962 /// Return true if current region has inner cancel construct.
963 bool doesParentHasScanDirective() const {
964 const SharingMapTy *Top = getSecondOnStackOrNull();
965 return Top ? Top->PrevScanLocation.isValid() : false;
966 }
967 /// Return true if current region has inner cancel construct.
968 SourceLocation getParentScanDirectiveLoc() const {
969 const SharingMapTy *Top = getSecondOnStackOrNull();
970 return Top ? Top->PrevScanLocation : SourceLocation();
971 }
972 /// Mark that parent region already has ordered directive.
973 void setParentHasOrderedDirective(SourceLocation Loc) {
974 if (SharingMapTy *Parent = getSecondOnStackOrNull())
975 Parent->PrevOrderedLocation = Loc;
976 }
977 /// Return true if current region has inner ordered construct.
978 bool doesParentHasOrderedDirective() const {
979 const SharingMapTy *Top = getSecondOnStackOrNull();
980 return Top ? Top->PrevOrderedLocation.isValid() : false;
981 }
982 /// Returns the location of the previously specified ordered directive.
983 SourceLocation getParentOrderedDirectiveLoc() const {
984 const SharingMapTy *Top = getSecondOnStackOrNull();
985 return Top ? Top->PrevOrderedLocation : SourceLocation();
986 }
987
988 /// Set collapse value for the region.
989 void setAssociatedLoops(unsigned Val) {
990 getTopOfStack().AssociatedLoops = Val;
991 if (Val > 1)
992 getTopOfStack().HasMutipleLoops = true;
993 }
994 /// Return collapse value for region.
995 unsigned getAssociatedLoops() const {
996 const SharingMapTy *Top = getTopOfStackOrNull();
997 return Top ? Top->AssociatedLoops : 0;
998 }
999 /// Returns true if the construct is associated with multiple loops.
1000 bool hasMutipleLoops() const {
1001 const SharingMapTy *Top = getTopOfStackOrNull();
1002 return Top ? Top->HasMutipleLoops : false;
1003 }
1004
1005 /// Marks current target region as one with closely nested teams
1006 /// region.
1007 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
1008 if (SharingMapTy *Parent = getSecondOnStackOrNull())
1009 Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
1010 }
1011 /// Returns true, if current region has closely nested teams region.
1012 bool hasInnerTeamsRegion() const {
1013 return getInnerTeamsRegionLoc().isValid();
1014 }
1015 /// Returns location of the nested teams region (if any).
1016 SourceLocation getInnerTeamsRegionLoc() const {
1017 const SharingMapTy *Top = getTopOfStackOrNull();
1018 return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
1019 }
1020
1021 Scope *getCurScope() const {
1022 const SharingMapTy *Top = getTopOfStackOrNull();
1023 return Top ? Top->CurScope : nullptr;
1024 }
1025 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
1026 SourceLocation getConstructLoc() const {
1027 const SharingMapTy *Top = getTopOfStackOrNull();
1028 return Top ? Top->ConstructLoc : SourceLocation();
1029 }
1030
1031 /// Do the check specified in \a Check to all component lists and return true
1032 /// if any issue is found.
1033 bool checkMappableExprComponentListsForDecl(
1034 const ValueDecl *VD, bool CurrentRegionOnly,
1035 const llvm::function_ref<
1036 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1037 OpenMPClauseKind)>
1038 Check) const {
1039 if (isStackEmpty())
1040 return false;
1041 auto SI = begin();
1042 auto SE = end();
1043
1044 if (SI == SE)
1045 return false;
1046
1047 if (CurrentRegionOnly)
1048 SE = std::next(x: SI);
1049 else
1050 std::advance(i&: SI, n: 1);
1051
1052 for (; SI != SE; ++SI) {
1053 auto MI = SI->MappedExprComponents.find(Val: VD);
1054 if (MI != SI->MappedExprComponents.end())
1055 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1056 MI->second.Components)
1057 if (Check(L, MI->second.Kind))
1058 return true;
1059 }
1060 return false;
1061 }
1062
1063 /// Do the check specified in \a Check to all component lists at a given level
1064 /// and return true if any issue is found.
1065 bool checkMappableExprComponentListsForDeclAtLevel(
1066 const ValueDecl *VD, unsigned Level,
1067 const llvm::function_ref<
1068 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1069 OpenMPClauseKind)>
1070 Check) const {
1071 if (getStackSize() <= Level)
1072 return false;
1073
1074 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1075 auto MI = StackElem.MappedExprComponents.find(Val: VD);
1076 if (MI != StackElem.MappedExprComponents.end())
1077 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1078 MI->second.Components)
1079 if (Check(L, MI->second.Kind))
1080 return true;
1081 return false;
1082 }
1083
1084 /// Create a new mappable expression component list associated with a given
1085 /// declaration and initialize it with the provided list of components.
1086 void addMappableExpressionComponents(
1087 const ValueDecl *VD,
1088 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
1089 OpenMPClauseKind WhereFoundClauseKind) {
1090 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
1091 // Create new entry and append the new components there.
1092 MEC.Components.resize(N: MEC.Components.size() + 1);
1093 MEC.Components.back().append(in_start: Components.begin(), in_end: Components.end());
1094 MEC.Kind = WhereFoundClauseKind;
1095 }
1096
1097 unsigned getNestingLevel() const {
1098 assert(!isStackEmpty());
1099 return getStackSize() - 1;
1100 }
1101 void addDoacrossDependClause(OMPClause *C, const OperatorOffsetTy &OpsOffs) {
1102 SharingMapTy *Parent = getSecondOnStackOrNull();
1103 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1104 Parent->DoacrossDepends.try_emplace(Key: C, Args: OpsOffs);
1105 }
1106 llvm::iterator_range<DoacrossClauseMapTy::const_iterator>
1107 getDoacrossDependClauses() const {
1108 const SharingMapTy &StackElem = getTopOfStack();
1109 if (isOpenMPWorksharingDirective(DKind: StackElem.Directive)) {
1110 const DoacrossClauseMapTy &Ref = StackElem.DoacrossDepends;
1111 return llvm::make_range(x: Ref.begin(), y: Ref.end());
1112 }
1113 return llvm::make_range(x: StackElem.DoacrossDepends.end(),
1114 y: StackElem.DoacrossDepends.end());
1115 }
1116
1117 // Store types of classes which have been explicitly mapped
1118 void addMappedClassesQualTypes(QualType QT) {
1119 SharingMapTy &StackElem = getTopOfStack();
1120 StackElem.MappedClassesQualTypes.insert(V: QT);
1121 }
1122
1123 // Return set of mapped classes types
1124 bool isClassPreviouslyMapped(QualType QT) const {
1125 const SharingMapTy &StackElem = getTopOfStack();
1126 return StackElem.MappedClassesQualTypes.contains(V: QT);
1127 }
1128
1129 /// Adds global declare target to the parent target region.
1130 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1131 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1132 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1133 "Expected declare target link global.");
1134 for (auto &Elem : *this) {
1135 if (isOpenMPTargetExecutionDirective(DKind: Elem.Directive)) {
1136 Elem.DeclareTargetLinkVarDecls.push_back(Elt: E);
1137 return;
1138 }
1139 }
1140 }
1141
1142 /// Returns the list of globals with declare target link if current directive
1143 /// is target.
1144 ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1145 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1146 "Expected target executable directive.");
1147 return getTopOfStack().DeclareTargetLinkVarDecls;
1148 }
1149
1150 /// Adds list of allocators expressions.
1151 void addInnerAllocatorExpr(Expr *E) {
1152 getTopOfStack().InnerUsedAllocators.push_back(Elt: E);
1153 }
1154 /// Return list of used allocators.
1155 ArrayRef<Expr *> getInnerAllocators() const {
1156 return getTopOfStack().InnerUsedAllocators;
1157 }
1158 /// Marks the declaration as implicitly firstprivate nin the task-based
1159 /// regions.
1160 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1161 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(V: D);
1162 }
1163 /// Checks if the decl is implicitly firstprivate in the task-based region.
1164 bool isImplicitTaskFirstprivate(Decl *D) const {
1165 return getTopOfStack().ImplicitTaskFirstprivates.contains(V: D);
1166 }
1167
1168 /// Marks decl as used in uses_allocators clause as the allocator.
1169 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1170 getTopOfStack().UsesAllocatorsDecls.try_emplace(Key: D, Args&: Kind);
1171 }
1172 /// Checks if specified decl is used in uses allocator clause as the
1173 /// allocator.
1174 std::optional<UsesAllocatorsDeclKind>
1175 isUsesAllocatorsDecl(unsigned Level, const Decl *D) const {
1176 const SharingMapTy &StackElem = getTopOfStack();
1177 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1178 if (I == StackElem.UsesAllocatorsDecls.end())
1179 return std::nullopt;
1180 return I->getSecond();
1181 }
1182 std::optional<UsesAllocatorsDeclKind>
1183 isUsesAllocatorsDecl(const Decl *D) const {
1184 const SharingMapTy &StackElem = getTopOfStack();
1185 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1186 if (I == StackElem.UsesAllocatorsDecls.end())
1187 return std::nullopt;
1188 return I->getSecond();
1189 }
1190
1191 void addDeclareMapperVarRef(Expr *Ref) {
1192 SharingMapTy &StackElem = getTopOfStack();
1193 StackElem.DeclareMapperVar = Ref;
1194 }
1195 const Expr *getDeclareMapperVarRef() const {
1196 const SharingMapTy *Top = getTopOfStackOrNull();
1197 return Top ? Top->DeclareMapperVar : nullptr;
1198 }
1199
1200 /// Add a new iterator variable.
1201 void addIteratorVarDecl(VarDecl *VD) {
1202 SharingMapTy &StackElem = getTopOfStack();
1203 StackElem.IteratorVarDecls.push_back(Elt: VD->getCanonicalDecl());
1204 }
1205 /// Check if variable declaration is an iterator VarDecl.
1206 bool isIteratorVarDecl(const VarDecl *VD) const {
1207 const SharingMapTy *Top = getTopOfStackOrNull();
1208 if (!Top)
1209 return false;
1210
1211 return llvm::is_contained(Range: Top->IteratorVarDecls, Element: VD->getCanonicalDecl());
1212 }
1213 /// get captured field from ImplicitDefaultFirstprivateFDs
1214 VarDecl *getImplicitFDCapExprDecl(const FieldDecl *FD) const {
1215 const_iterator I = begin();
1216 const_iterator EndI = end();
1217 size_t StackLevel = getStackSize();
1218 for (; I != EndI; ++I) {
1219 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1220 break;
1221 StackLevel--;
1222 }
1223 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1224 if (I == EndI)
1225 return nullptr;
1226 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1227 if (IFD.FD == FD && IFD.StackLevel == StackLevel)
1228 return IFD.VD;
1229 return nullptr;
1230 }
1231 /// Check if capture decl is field captured in ImplicitDefaultFirstprivateFDs
1232 bool isImplicitDefaultFirstprivateFD(VarDecl *VD) const {
1233 const_iterator I = begin();
1234 const_iterator EndI = end();
1235 for (; I != EndI; ++I)
1236 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1237 break;
1238 if (I == EndI)
1239 return false;
1240 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1241 if (IFD.VD == VD)
1242 return true;
1243 return false;
1244 }
1245 /// Store capture FD info in ImplicitDefaultFirstprivateFDs
1246 void addImplicitDefaultFirstprivateFD(const FieldDecl *FD, VarDecl *VD) {
1247 iterator I = begin();
1248 const_iterator EndI = end();
1249 size_t StackLevel = getStackSize();
1250 for (; I != EndI; ++I) {
1251 if (I->DefaultAttr == DSA_private || I->DefaultAttr == DSA_firstprivate) {
1252 I->ImplicitDefaultFirstprivateFDs.emplace_back(Args&: FD, Args&: StackLevel, Args&: VD);
1253 break;
1254 }
1255 StackLevel--;
1256 }
1257 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1258 }
1259};
1260
1261bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1262 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
1263}
1264
1265bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1266 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(Kind: DKind) ||
1267 DKind == OMPD_unknown;
1268}
1269
1270} // namespace
1271
1272static const Expr *getExprAsWritten(const Expr *E) {
1273 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
1274 E = FE->getSubExpr();
1275
1276 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Val: E))
1277 E = MTE->getSubExpr();
1278
1279 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(Val: E))
1280 E = Binder->getSubExpr();
1281
1282 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E))
1283 E = ICE->getSubExprAsWritten();
1284 return E->IgnoreParens();
1285}
1286
1287static Expr *getExprAsWritten(Expr *E) {
1288 return const_cast<Expr *>(getExprAsWritten(E: const_cast<const Expr *>(E)));
1289}
1290
1291static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1292 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: D))
1293 if (const auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
1294 D = ME->getMemberDecl();
1295
1296 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
1297 return D;
1298}
1299
1300static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1301 return const_cast<ValueDecl *>(
1302 getCanonicalDecl(D: const_cast<const ValueDecl *>(D)));
1303}
1304
1305static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C) {
1306 if (C == OMPC_threadprivate)
1307 return getOpenMPClauseName(C).str() + " or thread local";
1308 return getOpenMPClauseName(C).str();
1309}
1310
1311DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1312 ValueDecl *D) const {
1313 D = getCanonicalDecl(D);
1314 auto *VD = dyn_cast<VarDecl>(Val: D);
1315 const auto *FD = dyn_cast<FieldDecl>(Val: D);
1316 DSAVarData DVar;
1317 if (Iter == end()) {
1318 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1319 // in a region but not in construct]
1320 // File-scope or namespace-scope variables referenced in called routines
1321 // in the region are shared unless they appear in a threadprivate
1322 // directive.
1323 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(Val: VD))
1324 DVar.CKind = OMPC_shared;
1325
1326 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1327 // in a region but not in construct]
1328 // Variables with static storage duration that are declared in called
1329 // routines in the region are shared.
1330 if (VD && VD->hasGlobalStorage())
1331 DVar.CKind = OMPC_shared;
1332
1333 // Non-static data members are shared by default.
1334 if (FD)
1335 DVar.CKind = OMPC_shared;
1336
1337 return DVar;
1338 }
1339
1340 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1341 // in a Construct, C/C++, predetermined, p.1]
1342 // Variables with automatic storage duration that are declared in a scope
1343 // inside the construct are private.
1344 if (VD && isOpenMPLocal(D: VD, Iter) && VD->isLocalVarDecl() &&
1345 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1346 DVar.CKind = OMPC_private;
1347 return DVar;
1348 }
1349
1350 DVar.DKind = Iter->Directive;
1351 // Explicitly specified attributes and local variables with predetermined
1352 // attributes.
1353 if (Iter->SharingMap.count(Val: D)) {
1354 const DSAInfo &Data = Iter->SharingMap.lookup(Val: D);
1355 DVar.RefExpr = Data.RefExpr.getPointer();
1356 DVar.PrivateCopy = Data.PrivateCopy;
1357 DVar.CKind = Data.Attributes;
1358 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1359 DVar.Modifier = Data.Modifier;
1360 DVar.AppliedToPointee = Data.AppliedToPointee;
1361 return DVar;
1362 }
1363
1364 DefaultDataSharingAttributes IterDA = Iter->DefaultAttr;
1365 switch (Iter->DefaultVCAttr) {
1366 case DSA_VC_aggregate:
1367 if (!D->getType()->isAggregateType())
1368 IterDA = DSA_none;
1369 break;
1370 case DSA_VC_pointer:
1371 if (!D->getType()->isPointerType())
1372 IterDA = DSA_none;
1373 break;
1374 case DSA_VC_scalar:
1375 if (!D->getType()->isScalarType())
1376 IterDA = DSA_none;
1377 break;
1378 case DSA_VC_all:
1379 break;
1380 }
1381
1382 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1383 // in a Construct, C/C++, implicitly determined, p.1]
1384 // In a parallel or task construct, the data-sharing attributes of these
1385 // variables are determined by the default clause, if present.
1386 switch (IterDA) {
1387 case DSA_shared:
1388 DVar.CKind = OMPC_shared;
1389 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1390 return DVar;
1391 case DSA_none:
1392 return DVar;
1393 case DSA_firstprivate:
1394 if (VD && VD->getStorageDuration() == SD_Static &&
1395 VD->getDeclContext()->isFileContext()) {
1396 DVar.CKind = OMPC_unknown;
1397 } else {
1398 DVar.CKind = OMPC_firstprivate;
1399 }
1400 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1401 return DVar;
1402 case DSA_private:
1403 // each variable with static storage duration that is declared
1404 // in a namespace or global scope and referenced in the construct,
1405 // and that does not have a predetermined data-sharing attribute
1406 if (VD && VD->getStorageDuration() == SD_Static &&
1407 VD->getDeclContext()->isFileContext()) {
1408 DVar.CKind = OMPC_unknown;
1409 } else {
1410 DVar.CKind = OMPC_private;
1411 }
1412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1413 return DVar;
1414 case DSA_unspecified:
1415 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1416 // in a Construct, implicitly determined, p.2]
1417 // In a parallel construct, if no default clause is present, these
1418 // variables are shared.
1419 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1420 if ((isOpenMPParallelDirective(DKind: DVar.DKind) &&
1421 !isOpenMPTaskLoopDirective(DKind: DVar.DKind)) ||
1422 isOpenMPTeamsDirective(DKind: DVar.DKind)) {
1423 DVar.CKind = OMPC_shared;
1424 return DVar;
1425 }
1426
1427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1428 // in a Construct, implicitly determined, p.4]
1429 // In a task construct, if no default clause is present, a variable that in
1430 // the enclosing context is determined to be shared by all implicit tasks
1431 // bound to the current team is shared.
1432 if (isOpenMPTaskingDirective(Kind: DVar.DKind)) {
1433 DSAVarData DVarTemp;
1434 const_iterator I = Iter, E = end();
1435 do {
1436 ++I;
1437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1438 // Referenced in a Construct, implicitly determined, p.6]
1439 // In a task construct, if no default clause is present, a variable
1440 // whose data-sharing attribute is not determined by the rules above is
1441 // firstprivate.
1442 DVarTemp = getDSA(Iter&: I, D);
1443 if (DVarTemp.CKind != OMPC_shared) {
1444 DVar.RefExpr = nullptr;
1445 DVar.CKind = OMPC_firstprivate;
1446 return DVar;
1447 }
1448 } while (I != E && !isImplicitTaskingRegion(DKind: I->Directive));
1449 DVar.CKind =
1450 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1451 return DVar;
1452 }
1453 }
1454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1455 // in a Construct, implicitly determined, p.3]
1456 // For constructs other than task, if no default clause is present, these
1457 // variables inherit their data-sharing attributes from the enclosing
1458 // context.
1459 return getDSA(Iter&: ++Iter, D);
1460}
1461
1462const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1463 const Expr *NewDE) {
1464 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1465 D = getCanonicalDecl(D);
1466 SharingMapTy &StackElem = getTopOfStack();
1467 auto [It, Inserted] = StackElem.AlignedMap.try_emplace(Key: D, Args&: NewDE);
1468 if (Inserted) {
1469 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1470 return nullptr;
1471 }
1472 assert(It->second && "Unexpected nullptr expr in the aligned map");
1473 return It->second;
1474}
1475
1476const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1477 const Expr *NewDE) {
1478 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1479 D = getCanonicalDecl(D);
1480 SharingMapTy &StackElem = getTopOfStack();
1481 auto [It, Inserted] = StackElem.NontemporalMap.try_emplace(Key: D, Args&: NewDE);
1482 if (Inserted) {
1483 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1484 return nullptr;
1485 }
1486 assert(It->second && "Unexpected nullptr expr in the aligned map");
1487 return It->second;
1488}
1489
1490void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1491 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1492 D = getCanonicalDecl(D);
1493 SharingMapTy &StackElem = getTopOfStack();
1494 StackElem.LCVMap.try_emplace(
1495 Key: D, Args: LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1496}
1497
1498const DSAStackTy::LCDeclInfo
1499DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1500 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1501 D = getCanonicalDecl(D);
1502 const SharingMapTy &StackElem = getTopOfStack();
1503 auto It = StackElem.LCVMap.find(Val: D);
1504 if (It != StackElem.LCVMap.end())
1505 return It->second;
1506 return {0, nullptr};
1507}
1508
1509const DSAStackTy::LCDeclInfo
1510DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1511 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1512 D = getCanonicalDecl(D);
1513 for (unsigned I = Level + 1; I > 0; --I) {
1514 const SharingMapTy &StackElem = getStackElemAtLevel(Level: I - 1);
1515 auto It = StackElem.LCVMap.find(Val: D);
1516 if (It != StackElem.LCVMap.end())
1517 return It->second;
1518 }
1519 return {0, nullptr};
1520}
1521
1522const DSAStackTy::LCDeclInfo
1523DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1524 const SharingMapTy *Parent = getSecondOnStackOrNull();
1525 assert(Parent && "Data-sharing attributes stack is empty");
1526 D = getCanonicalDecl(D);
1527 auto It = Parent->LCVMap.find(Val: D);
1528 if (It != Parent->LCVMap.end())
1529 return It->second;
1530 return {0, nullptr};
1531}
1532
1533const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1534 const SharingMapTy *Parent = getSecondOnStackOrNull();
1535 assert(Parent && "Data-sharing attributes stack is empty");
1536 if (Parent->LCVMap.size() < I)
1537 return nullptr;
1538 for (const auto &Pair : Parent->LCVMap)
1539 if (Pair.second.first == I)
1540 return Pair.first;
1541 return nullptr;
1542}
1543
1544void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1545 DeclRefExpr *PrivateCopy, unsigned Modifier,
1546 bool AppliedToPointee) {
1547 D = getCanonicalDecl(D);
1548 if (A == OMPC_threadprivate) {
1549 DSAInfo &Data = Threadprivates[D];
1550 Data.Attributes = A;
1551 Data.RefExpr.setPointer(E);
1552 Data.PrivateCopy = nullptr;
1553 Data.Modifier = Modifier;
1554 } else if (A == OMPC_groupprivate) {
1555 DSAInfo &Data = Groupprivates[D];
1556 Data.Attributes = A;
1557 Data.RefExpr.setPointer(E);
1558 Data.PrivateCopy = nullptr;
1559 Data.Modifier = Modifier;
1560 } else {
1561 DSAInfo &Data = getTopOfStack().SharingMap[D];
1562 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1563 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1564 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1565 (isLoopControlVariable(D).first && A == OMPC_private));
1566 Data.Modifier = Modifier;
1567 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1568 Data.RefExpr.setInt(/*IntVal=*/true);
1569 return;
1570 }
1571 const bool IsLastprivate =
1572 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1573 Data.Attributes = A;
1574 Data.RefExpr.setPointerAndInt(PtrVal: E, IntVal: IsLastprivate);
1575 Data.PrivateCopy = PrivateCopy;
1576 Data.AppliedToPointee = AppliedToPointee;
1577 if (PrivateCopy) {
1578 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1579 Data.Modifier = Modifier;
1580 Data.Attributes = A;
1581 Data.RefExpr.setPointerAndInt(PtrVal: PrivateCopy, IntVal: IsLastprivate);
1582 Data.PrivateCopy = nullptr;
1583 Data.AppliedToPointee = AppliedToPointee;
1584 }
1585 }
1586}
1587
1588/// Build a variable declaration for OpenMP loop iteration variable.
1589static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1590 StringRef Name, const AttrVec *Attrs = nullptr,
1591 DeclRefExpr *OrigRef = nullptr) {
1592 DeclContext *DC = SemaRef.CurContext;
1593 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1594 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(T: Type, Loc);
1595 auto *Decl =
1596 VarDecl::Create(C&: SemaRef.Context, DC, StartLoc: Loc, IdLoc: Loc, Id: II, T: Type, TInfo, S: SC_None);
1597 if (Attrs) {
1598 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1599 I != E; ++I)
1600 Decl->addAttr(A: *I);
1601 }
1602 Decl->setImplicit();
1603 if (OrigRef) {
1604 Decl->addAttr(
1605 A: OMPReferencedVarAttr::CreateImplicit(Ctx&: SemaRef.Context, Ref: OrigRef));
1606 }
1607 return Decl;
1608}
1609
1610static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1611 SourceLocation Loc,
1612 bool RefersToCapture = false) {
1613 D->setReferenced();
1614 D->markUsed(C&: S.Context);
1615 return DeclRefExpr::Create(Context: S.getASTContext(), QualifierLoc: NestedNameSpecifierLoc(),
1616 TemplateKWLoc: SourceLocation(), D, RefersToEnclosingVariableOrCapture: RefersToCapture, NameLoc: Loc, T: Ty,
1617 VK: VK_LValue);
1618}
1619
1620void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1621 BinaryOperatorKind BOK) {
1622 D = getCanonicalDecl(D);
1623 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1624 assert(
1625 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1626 "Additional reduction info may be specified only for reduction items.");
1627 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1628 assert(ReductionData.ReductionRange.isInvalid() &&
1629 (getTopOfStack().Directive == OMPD_taskgroup ||
1630 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1631 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1632 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1633 "Additional reduction info may be specified only once for reduction "
1634 "items.");
1635 ReductionData.set(BO: BOK, RR: SR);
1636 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1637 if (!TaskgroupReductionRef) {
1638 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1639 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1640 TaskgroupReductionRef =
1641 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1642 }
1643}
1644
1645void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1646 const Expr *ReductionRef) {
1647 D = getCanonicalDecl(D);
1648 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1649 assert(
1650 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1651 "Additional reduction info may be specified only for reduction items.");
1652 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1653 assert(ReductionData.ReductionRange.isInvalid() &&
1654 (getTopOfStack().Directive == OMPD_taskgroup ||
1655 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1656 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1657 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1658 "Additional reduction info may be specified only once for reduction "
1659 "items.");
1660 ReductionData.set(RefExpr: ReductionRef, RR: SR);
1661 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1662 if (!TaskgroupReductionRef) {
1663 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1664 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1665 TaskgroupReductionRef =
1666 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1667 }
1668}
1669
1670const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1671 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1672 Expr *&TaskgroupDescriptor) const {
1673 D = getCanonicalDecl(D);
1674 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1675 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1676 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1677 if (Data.Attributes != OMPC_reduction ||
1678 Data.Modifier != OMPC_REDUCTION_task)
1679 continue;
1680 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1681 if (!ReductionData.ReductionOp ||
1682 isa<const Expr *>(Val: ReductionData.ReductionOp))
1683 return DSAVarData();
1684 SR = ReductionData.ReductionRange;
1685 BOK = cast<ReductionData::BOKPtrType>(Val: ReductionData.ReductionOp);
1686 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1687 "expression for the descriptor is not "
1688 "set.");
1689 TaskgroupDescriptor = I->TaskgroupReductionRef;
1690 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1691 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1692 /*AppliedToPointee=*/false);
1693 }
1694 return DSAVarData();
1695}
1696
1697const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1698 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1699 Expr *&TaskgroupDescriptor) const {
1700 D = getCanonicalDecl(D);
1701 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1702 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1703 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1704 if (Data.Attributes != OMPC_reduction ||
1705 Data.Modifier != OMPC_REDUCTION_task)
1706 continue;
1707 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1708 if (!ReductionData.ReductionOp ||
1709 !isa<const Expr *>(Val: ReductionData.ReductionOp))
1710 return DSAVarData();
1711 SR = ReductionData.ReductionRange;
1712 ReductionRef = cast<const Expr *>(Val: ReductionData.ReductionOp);
1713 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1714 "expression for the descriptor is not "
1715 "set.");
1716 TaskgroupDescriptor = I->TaskgroupReductionRef;
1717 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1718 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1719 /*AppliedToPointee=*/false);
1720 }
1721 return DSAVarData();
1722}
1723
1724bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1725 D = D->getCanonicalDecl();
1726 for (const_iterator E = end(); I != E; ++I) {
1727 if (isImplicitOrExplicitTaskingRegion(DKind: I->Directive) ||
1728 isOpenMPTargetExecutionDirective(DKind: I->Directive)) {
1729 if (I->CurScope) {
1730 Scope *TopScope = I->CurScope->getParent();
1731 Scope *CurScope = getCurScope();
1732 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1733 CurScope = CurScope->getParent();
1734 return CurScope != TopScope;
1735 }
1736 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent())
1737 if (I->Context == DC)
1738 return true;
1739 return false;
1740 }
1741 }
1742 return false;
1743}
1744
1745static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1746 bool AcceptIfMutable = true,
1747 bool *IsClassType = nullptr) {
1748 ASTContext &Context = SemaRef.getASTContext();
1749 Type = Type.getNonReferenceType().getCanonicalType();
1750 bool IsConstant = Type.isConstant(Ctx: Context);
1751 Type = Context.getBaseElementType(QT: Type);
1752 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1753 ? Type->getAsCXXRecordDecl()
1754 : nullptr;
1755 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(Val: RD))
1756 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1757 RD = CTD->getTemplatedDecl();
1758 if (IsClassType)
1759 *IsClassType = RD;
1760 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1761 RD->hasDefinition() && RD->hasMutableFields());
1762}
1763
1764static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1765 QualType Type, OpenMPClauseKind CKind,
1766 SourceLocation ELoc,
1767 bool AcceptIfMutable = true,
1768 bool ListItemNotVar = false) {
1769 ASTContext &Context = SemaRef.getASTContext();
1770 bool IsClassType;
1771 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, IsClassType: &IsClassType)) {
1772 unsigned Diag = ListItemNotVar ? diag::err_omp_const_list_item
1773 : IsClassType ? diag::err_omp_const_not_mutable_variable
1774 : diag::err_omp_const_variable;
1775 SemaRef.Diag(Loc: ELoc, DiagID: Diag) << getOpenMPClauseNameForDiag(C: CKind);
1776 if (!ListItemNotVar && D) {
1777 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
1778 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1779 VarDecl::DeclarationOnly;
1780 SemaRef.Diag(Loc: D->getLocation(),
1781 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1782 << D;
1783 }
1784 return true;
1785 }
1786 return false;
1787}
1788
1789const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1790 bool FromParent) {
1791 D = getCanonicalDecl(D);
1792 DSAVarData DVar;
1793
1794 auto *VD = dyn_cast<VarDecl>(Val: D);
1795 auto TI = Threadprivates.find(Val: D);
1796 if (TI != Threadprivates.end()) {
1797 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1798 DVar.CKind = OMPC_threadprivate;
1799 DVar.Modifier = TI->getSecond().Modifier;
1800 return DVar;
1801 }
1802 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1803 DVar.RefExpr = buildDeclRefExpr(
1804 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1805 Loc: VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1806 DVar.CKind = OMPC_threadprivate;
1807 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1808 return DVar;
1809 }
1810 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1811 // in a Construct, C/C++, predetermined, p.1]
1812 // Variables appearing in threadprivate directives are threadprivate.
1813 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1814 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1815 SemaRef.getLangOpts().OpenMPUseTLS &&
1816 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1817 (VD && VD->getStorageClass() == SC_Register &&
1818 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1819 DVar.RefExpr = buildDeclRefExpr(
1820 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(), Loc: D->getLocation());
1821 DVar.CKind = OMPC_threadprivate;
1822 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1823 return DVar;
1824 }
1825 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1826 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1827 !isLoopControlVariable(D).first) {
1828 const_iterator IterTarget =
1829 std::find_if(first: begin(), last: end(), pred: [](const SharingMapTy &Data) {
1830 return isOpenMPTargetExecutionDirective(DKind: Data.Directive);
1831 });
1832 if (IterTarget != end()) {
1833 const_iterator ParentIterTarget = IterTarget + 1;
1834 for (const_iterator Iter = begin(); Iter != ParentIterTarget; ++Iter) {
1835 if (isOpenMPLocal(D: VD, I: Iter)) {
1836 DVar.RefExpr =
1837 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1838 Loc: D->getLocation());
1839 DVar.CKind = OMPC_threadprivate;
1840 return DVar;
1841 }
1842 }
1843 if (!isClauseParsingMode() || IterTarget != begin()) {
1844 auto DSAIter = IterTarget->SharingMap.find(Val: D);
1845 if (DSAIter != IterTarget->SharingMap.end() &&
1846 isOpenMPPrivate(Kind: DSAIter->getSecond().Attributes)) {
1847 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1848 DVar.CKind = OMPC_threadprivate;
1849 return DVar;
1850 }
1851 const_iterator End = end();
1852 if (!SemaRef.OpenMP().isOpenMPCapturedByRef(
1853 D, Level: std::distance(first: ParentIterTarget, last: End),
1854 /*OpenMPCaptureLevel=*/0)) {
1855 DVar.RefExpr =
1856 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1857 Loc: IterTarget->ConstructLoc);
1858 DVar.CKind = OMPC_threadprivate;
1859 return DVar;
1860 }
1861 }
1862 }
1863 }
1864
1865 if (isStackEmpty())
1866 // Not in OpenMP execution region and top scope was already checked.
1867 return DVar;
1868
1869 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1870 // in a Construct, C/C++, predetermined, p.4]
1871 // Static data members are shared.
1872 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1873 // in a Construct, C/C++, predetermined, p.7]
1874 // Variables with static storage duration that are declared in a scope
1875 // inside the construct are shared.
1876 if (VD && VD->isStaticDataMember()) {
1877 // Check for explicitly specified attributes.
1878 const_iterator I = begin();
1879 const_iterator EndI = end();
1880 if (FromParent && I != EndI)
1881 ++I;
1882 if (I != EndI) {
1883 auto It = I->SharingMap.find(Val: D);
1884 if (It != I->SharingMap.end()) {
1885 const DSAInfo &Data = It->getSecond();
1886 DVar.RefExpr = Data.RefExpr.getPointer();
1887 DVar.PrivateCopy = Data.PrivateCopy;
1888 DVar.CKind = Data.Attributes;
1889 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1890 DVar.DKind = I->Directive;
1891 DVar.Modifier = Data.Modifier;
1892 DVar.AppliedToPointee = Data.AppliedToPointee;
1893 return DVar;
1894 }
1895 }
1896
1897 DVar.CKind = OMPC_shared;
1898 return DVar;
1899 }
1900
1901 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1902 // The predetermined shared attribute for const-qualified types having no
1903 // mutable members was removed after OpenMP 3.1.
1904 if (SemaRef.LangOpts.OpenMP <= 31) {
1905 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1906 // in a Construct, C/C++, predetermined, p.6]
1907 // Variables with const qualified type having no mutable member are
1908 // shared.
1909 if (isConstNotMutableType(SemaRef, Type: D->getType())) {
1910 // Variables with const-qualified type having no mutable member may be
1911 // listed in a firstprivate clause, even if they are static data members.
1912 DSAVarData DVarTemp = hasInnermostDSA(
1913 D,
1914 CPred: [](OpenMPClauseKind C, bool) {
1915 return C == OMPC_firstprivate || C == OMPC_shared;
1916 },
1917 DPred: MatchesAlways, FromParent);
1918 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1919 return DVarTemp;
1920
1921 DVar.CKind = OMPC_shared;
1922 return DVar;
1923 }
1924 }
1925
1926 // Explicitly specified attributes and local variables with predetermined
1927 // attributes.
1928 const_iterator I = begin();
1929 const_iterator EndI = end();
1930 if (FromParent && I != EndI)
1931 ++I;
1932 if (I == EndI)
1933 return DVar;
1934 auto It = I->SharingMap.find(Val: D);
1935 if (It != I->SharingMap.end()) {
1936 const DSAInfo &Data = It->getSecond();
1937 DVar.RefExpr = Data.RefExpr.getPointer();
1938 DVar.PrivateCopy = Data.PrivateCopy;
1939 DVar.CKind = Data.Attributes;
1940 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1941 DVar.DKind = I->Directive;
1942 DVar.Modifier = Data.Modifier;
1943 DVar.AppliedToPointee = Data.AppliedToPointee;
1944 }
1945
1946 return DVar;
1947}
1948
1949const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1950 bool FromParent) const {
1951 if (isStackEmpty()) {
1952 const_iterator I;
1953 return getDSA(Iter&: I, D);
1954 }
1955 D = getCanonicalDecl(D);
1956 const_iterator StartI = begin();
1957 const_iterator EndI = end();
1958 if (FromParent && StartI != EndI)
1959 ++StartI;
1960 return getDSA(Iter&: StartI, D);
1961}
1962
1963const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1964 unsigned Level) const {
1965 if (getStackSize() <= Level)
1966 return DSAVarData();
1967 D = getCanonicalDecl(D);
1968 const_iterator StartI = std::next(x: begin(), n: getStackSize() - 1 - Level);
1969 return getDSA(Iter&: StartI, D);
1970}
1971
1972const DSAStackTy::DSAVarData
1973DSAStackTy::hasDSA(ValueDecl *D,
1974 const llvm::function_ref<bool(OpenMPClauseKind, bool,
1975 DefaultDataSharingAttributes)>
1976 CPred,
1977 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1978 bool FromParent) const {
1979 if (isStackEmpty())
1980 return {};
1981 D = getCanonicalDecl(D);
1982 const_iterator I = begin();
1983 const_iterator EndI = end();
1984 if (FromParent && I != EndI)
1985 ++I;
1986 for (; I != EndI; ++I) {
1987 if (!DPred(I->Directive) &&
1988 !isImplicitOrExplicitTaskingRegion(DKind: I->Directive))
1989 continue;
1990 const_iterator NewI = I;
1991 DSAVarData DVar = getDSA(Iter&: NewI, D);
1992 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee, I->DefaultAttr))
1993 return DVar;
1994 }
1995 return {};
1996}
1997
1998const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1999 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2000 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2001 bool FromParent) const {
2002 if (isStackEmpty())
2003 return {};
2004 D = getCanonicalDecl(D);
2005 const_iterator StartI = begin();
2006 const_iterator EndI = end();
2007 if (FromParent && StartI != EndI)
2008 ++StartI;
2009 if (StartI == EndI || !DPred(StartI->Directive))
2010 return {};
2011 const_iterator NewI = StartI;
2012 DSAVarData DVar = getDSA(Iter&: NewI, D);
2013 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee))
2014 ? DVar
2015 : DSAVarData();
2016}
2017
2018bool DSAStackTy::hasExplicitDSA(
2019 const ValueDecl *D,
2020 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2021 unsigned Level, bool NotLastprivate) const {
2022 if (getStackSize() <= Level)
2023 return false;
2024 D = getCanonicalDecl(D);
2025 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2026 auto I = StackElem.SharingMap.find(Val: D);
2027 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() &&
2028 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) &&
2029 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
2030 return true;
2031 // Check predetermined rules for the loop control variables.
2032 auto LI = StackElem.LCVMap.find(Val: D);
2033 if (LI != StackElem.LCVMap.end())
2034 return CPred(OMPC_private, /*AppliedToPointee=*/false);
2035 return false;
2036}
2037
2038bool DSAStackTy::hasExplicitDirective(
2039 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2040 unsigned Level) const {
2041 if (getStackSize() <= Level)
2042 return false;
2043 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2044 return DPred(StackElem.Directive);
2045}
2046
2047bool DSAStackTy::hasDirective(
2048 const llvm::function_ref<bool(OpenMPDirectiveKind,
2049 const DeclarationNameInfo &, SourceLocation)>
2050 DPred,
2051 bool FromParent) const {
2052 // We look only in the enclosing region.
2053 size_t Skip = FromParent ? 2 : 1;
2054 for (const_iterator I = begin() + std::min(a: Skip, b: getStackSize()), E = end();
2055 I != E; ++I) {
2056 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
2057 return true;
2058 }
2059 return false;
2060}
2061
2062void SemaOpenMP::InitDataSharingAttributesStack() {
2063 VarDataSharingAttributesStack = new DSAStackTy(SemaRef);
2064}
2065
2066#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
2067
2068void SemaOpenMP::pushOpenMPFunctionRegion() { DSAStack->pushFunction(); }
2069
2070void SemaOpenMP::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
2071 DSAStack->popFunction(OldFSI);
2072}
2073
2074static bool isOpenMPDeviceDelayedContext(Sema &S) {
2075 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsTargetDevice &&
2076 "Expected OpenMP device compilation.");
2077 return !S.OpenMP().isInOpenMPTargetExecutionDirective();
2078}
2079
2080namespace {
2081/// Status of the function emission on the host/device.
2082enum class FunctionEmissionStatus {
2083 Emitted,
2084 Discarded,
2085 Unknown,
2086};
2087} // anonymous namespace
2088
2089SemaBase::SemaDiagnosticBuilder
2090SemaOpenMP::diagIfOpenMPDeviceCode(SourceLocation Loc, unsigned DiagID,
2091 const FunctionDecl *FD) {
2092 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice &&
2093 "Expected OpenMP device compilation.");
2094
2095 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2096 if (FD) {
2097 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2098 switch (FES) {
2099 case Sema::FunctionEmissionStatus::Emitted:
2100 Kind = SemaDiagnosticBuilder::K_Immediate;
2101 break;
2102 case Sema::FunctionEmissionStatus::Unknown:
2103 // TODO: We should always delay diagnostics here in case a target
2104 // region is in a function we do not emit. However, as the
2105 // current diagnostics are associated with the function containing
2106 // the target region and we do not emit that one, we would miss out
2107 // on diagnostics for the target region itself. We need to anchor
2108 // the diagnostics with the new generated function *or* ensure we
2109 // emit diagnostics associated with the surrounding function.
2110 Kind = isOpenMPDeviceDelayedContext(S&: SemaRef)
2111 ? SemaDiagnosticBuilder::K_Deferred
2112 : SemaDiagnosticBuilder::K_Immediate;
2113 break;
2114 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2115 case Sema::FunctionEmissionStatus::OMPDiscarded:
2116 Kind = SemaDiagnosticBuilder::K_Nop;
2117 break;
2118 case Sema::FunctionEmissionStatus::CUDADiscarded:
2119 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
2120 break;
2121 }
2122 }
2123
2124 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2125}
2126
2127SemaBase::SemaDiagnosticBuilder
2128SemaOpenMP::diagIfOpenMPHostCode(SourceLocation Loc, unsigned DiagID,
2129 const FunctionDecl *FD) {
2130 assert(getLangOpts().OpenMP && !getLangOpts().OpenMPIsTargetDevice &&
2131 "Expected OpenMP host compilation.");
2132
2133 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2134 if (FD) {
2135 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2136 switch (FES) {
2137 case Sema::FunctionEmissionStatus::Emitted:
2138 Kind = SemaDiagnosticBuilder::K_Immediate;
2139 break;
2140 case Sema::FunctionEmissionStatus::Unknown:
2141 Kind = SemaDiagnosticBuilder::K_Deferred;
2142 break;
2143 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2144 case Sema::FunctionEmissionStatus::OMPDiscarded:
2145 case Sema::FunctionEmissionStatus::CUDADiscarded:
2146 Kind = SemaDiagnosticBuilder::K_Nop;
2147 break;
2148 }
2149 }
2150
2151 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2152}
2153
2154static OpenMPDefaultmapClauseKind
2155getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
2156 if (LO.OpenMP <= 45) {
2157 if (VD->getType().getNonReferenceType()->isScalarType())
2158 return OMPC_DEFAULTMAP_scalar;
2159 return OMPC_DEFAULTMAP_aggregate;
2160 }
2161 if (VD->getType().getNonReferenceType()->isAnyPointerType())
2162 return OMPC_DEFAULTMAP_pointer;
2163 if (VD->getType().getNonReferenceType()->isScalarType())
2164 return OMPC_DEFAULTMAP_scalar;
2165 return OMPC_DEFAULTMAP_aggregate;
2166}
2167
2168bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
2169 unsigned OpenMPCaptureLevel) const {
2170 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2171
2172 ASTContext &Ctx = getASTContext();
2173 bool IsByRef = true;
2174
2175 // Find the directive that is associated with the provided scope.
2176 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
2177 QualType Ty = D->getType();
2178
2179 bool IsVariableUsedInMapClause = false;
2180 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level)) {
2181 // This table summarizes how a given variable should be passed to the device
2182 // given its type and the clauses where it appears. This table is based on
2183 // the description in OpenMP 4.5 [2.10.4, target Construct] and
2184 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
2185 //
2186 // =========================================================================
2187 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
2188 // | |(tofrom:scalar)| | pvt | |has_dv_adr| |
2189 // =========================================================================
2190 // | scl | | | | - | | bycopy|
2191 // | scl | | - | x | - | - | bycopy|
2192 // | scl | | x | - | - | - | null |
2193 // | scl | x | | | - | | byref |
2194 // | scl | x | - | x | - | - | bycopy|
2195 // | scl | x | x | - | - | - | null |
2196 // | scl | | - | - | - | x | byref |
2197 // | scl | x | - | - | - | x | byref |
2198 //
2199 // | agg | n.a. | | | - | | byref |
2200 // | agg | n.a. | - | x | - | - | byref |
2201 // | agg | n.a. | x | - | - | - | null |
2202 // | agg | n.a. | - | - | - | x | byref |
2203 // | agg | n.a. | - | - | - | x[] | byref |
2204 //
2205 // | ptr | n.a. | | | - | | bycopy|
2206 // | ptr | n.a. | - | x | - | - | bycopy|
2207 // | ptr | n.a. | x | - | - | - | null |
2208 // | ptr | n.a. | - | - | - | x | byref |
2209 // | ptr | n.a. | - | - | - | x, x[] | bycopy|
2210 // | ptr | n.a. | - | - | - | x[] | bycopy|
2211 // | ptr | n.a. | - | - | x | | bycopy|
2212 // | ptr | n.a. | - | - | x | x | bycopy|
2213 // | ptr | n.a. | - | - | x | x[] | bycopy|
2214 // =========================================================================
2215 // Legend:
2216 // scl - scalar
2217 // ptr - pointer
2218 // agg - aggregate
2219 // x - applies
2220 // - - invalid in this combination
2221 // [] - mapped with an array section
2222 // byref - should be mapped by reference
2223 // byval - should be mapped by value
2224 // null - initialize a local variable to null on the device
2225 //
2226 // Observations:
2227 // - All scalar declarations that show up in a map clause have to be passed
2228 // by reference, because they may have been mapped in the enclosing data
2229 // environment.
2230 // - If the scalar value does not fit the size of uintptr, it has to be
2231 // passed by reference, regardless the result in the table above.
2232 // - For pointers mapped by value that have either an implicit map or an
2233 // array section, the runtime library may pass the NULL value to the
2234 // device instead of the value passed to it by the compiler.
2235 // - If both a pointer and a dereference of it are mapped, then the pointer
2236 // should be passed by reference.
2237
2238 if (Ty->isReferenceType())
2239 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2240
2241 // Locate map clauses and see if the variable being captured is mapped by
2242 // itself, or referred to, in any of those clauses. Here we only care about
2243 // variables, not fields, because fields are part of aggregates.
2244 bool IsVariableAssociatedWithSection = false;
2245 bool IsVariableItselfMapped = false;
2246
2247 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2248 VD: D, Level,
2249 Check: [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
2250 &IsVariableItselfMapped,
2251 D](OMPClauseMappableExprCommon::MappableExprComponentListRef
2252 MapExprComponents,
2253 OpenMPClauseKind WhereFoundClauseKind) {
2254 // Both map and has_device_addr clauses information influences how a
2255 // variable is captured. E.g. is_device_ptr does not require changing
2256 // the default behavior.
2257 if (WhereFoundClauseKind != OMPC_map &&
2258 WhereFoundClauseKind != OMPC_has_device_addr)
2259 return false;
2260
2261 auto EI = MapExprComponents.rbegin();
2262 auto EE = MapExprComponents.rend();
2263
2264 assert(EI != EE && "Invalid map expression!");
2265
2266 if (isa<DeclRefExpr>(Val: EI->getAssociatedExpression()) &&
2267 EI->getAssociatedDeclaration() == D) {
2268 IsVariableUsedInMapClause = true;
2269
2270 // If the component list has only one element, it's for mapping the
2271 // variable itself, like map(p). This takes precedence in
2272 // determining how it's captured, so we don't need to look further
2273 // for any other maps that use the variable (like map(p[0]) etc.)
2274 if (MapExprComponents.size() == 1) {
2275 IsVariableItselfMapped = true;
2276 return true;
2277 }
2278 }
2279
2280 ++EI;
2281 if (EI == EE)
2282 return false;
2283 auto Last = std::prev(x: EE);
2284 const auto *UO =
2285 dyn_cast<UnaryOperator>(Val: Last->getAssociatedExpression());
2286 if ((UO && UO->getOpcode() == UO_Deref) ||
2287 isa<ArraySubscriptExpr>(Val: Last->getAssociatedExpression()) ||
2288 isa<ArraySectionExpr>(Val: Last->getAssociatedExpression()) ||
2289 isa<MemberExpr>(Val: EI->getAssociatedExpression()) ||
2290 isa<OMPArrayShapingExpr>(Val: Last->getAssociatedExpression())) {
2291 IsVariableAssociatedWithSection = true;
2292 // We've found a case like map(p[0]) or map(p->a) or map(*p),
2293 // so we are done with this particular map, but we need to keep
2294 // looking in case we find a map(p).
2295 return false;
2296 }
2297
2298 // Keep looking for more map info.
2299 return false;
2300 });
2301
2302 if (IsVariableUsedInMapClause) {
2303 // If variable is identified in a map clause it is always captured by
2304 // reference except if it is a pointer that is dereferenced somehow, but
2305 // not itself mapped.
2306 //
2307 // OpenMP 6.0, 7.1.1: Data sharing attribute rules, variables referenced
2308 // in a construct::
2309 // If a list item in a has_device_addr clause or in a map clause on the
2310 // target construct has a base pointer, and the base pointer is a scalar
2311 // variable *that is not a list item in a map clause on the construct*,
2312 // the base pointer is firstprivate.
2313 //
2314 // OpenMP 4.5, 2.15.1.1: Data-sharing Attribute Rules for Variables
2315 // Referenced in a Construct:
2316 // If an array section is a list item in a map clause on the target
2317 // construct and the array section is derived from a variable for which
2318 // the type is pointer then that variable is firstprivate.
2319 IsByRef = IsVariableItselfMapped ||
2320 !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2321 } else {
2322 // By default, all the data that has a scalar type is mapped by copy
2323 // (except for reduction variables).
2324 // Defaultmap scalar is mutual exclusive to defaultmap pointer
2325 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2326 !Ty->isAnyPointerType()) ||
2327 !Ty->isScalarType() ||
2328 DSAStack->isDefaultmapCapturedByRef(
2329 Level, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)) ||
2330 DSAStack->hasExplicitDSA(
2331 D,
2332 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) {
2333 return K == OMPC_reduction && !AppliedToPointee;
2334 },
2335 Level);
2336 }
2337 }
2338
2339 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
2340 IsByRef =
2341 ((IsVariableUsedInMapClause &&
2342 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2343 OMPD_target) ||
2344 !(DSAStack->hasExplicitDSA(
2345 D,
2346 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2347 return K == OMPC_firstprivate ||
2348 (K == OMPC_reduction && AppliedToPointee);
2349 },
2350 Level, /*NotLastprivate=*/true) ||
2351 DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2352 // If the variable is artificial and must be captured by value - try to
2353 // capture by value.
2354 !(isa<OMPCapturedExprDecl>(Val: D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
2355 !cast<OMPCapturedExprDecl>(Val: D)->getInit()->isGLValue()) &&
2356 // If the variable is implicitly firstprivate and scalar - capture by
2357 // copy
2358 !((DSAStack->getDefaultDSA() == DSA_firstprivate ||
2359 DSAStack->getDefaultDSA() == DSA_private) &&
2360 !DSAStack->hasExplicitDSA(
2361 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; },
2362 Level) &&
2363 !DSAStack->isLoopControlVariable(D, Level).first);
2364 }
2365
2366 // When passing data by copy, we need to make sure it fits the uintptr size
2367 // and alignment, because the runtime library only deals with uintptr types.
2368 // If it does not fit the uintptr size, we need to pass the data by reference
2369 // instead.
2370 if (!IsByRef && (Ctx.getTypeSizeInChars(T: Ty) >
2371 Ctx.getTypeSizeInChars(T: Ctx.getUIntPtrType()) ||
2372 Ctx.getAlignOfGlobalVarInChars(T: Ty, VD: dyn_cast<VarDecl>(Val: D)) >
2373 Ctx.getTypeAlignInChars(T: Ctx.getUIntPtrType()))) {
2374 IsByRef = true;
2375 }
2376
2377 return IsByRef;
2378}
2379
2380unsigned SemaOpenMP::getOpenMPNestingLevel() const {
2381 assert(getLangOpts().OpenMP);
2382 return DSAStack->getNestingLevel();
2383}
2384
2385bool SemaOpenMP::isInOpenMPTaskUntiedContext() const {
2386 return isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2387 DSAStack->isUntiedRegion();
2388}
2389
2390bool SemaOpenMP::isInOpenMPTargetExecutionDirective() const {
2391 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2392 !DSAStack->isClauseParsingMode()) ||
2393 DSAStack->hasDirective(
2394 DPred: [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2395 SourceLocation) -> bool {
2396 return isOpenMPTargetExecutionDirective(DKind: K);
2397 },
2398 FromParent: false);
2399}
2400
2401bool SemaOpenMP::isOpenMPRebuildMemberExpr(ValueDecl *D) {
2402 // Only rebuild for Field.
2403 if (!isa<FieldDecl>(Val: D))
2404 return false;
2405 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2406 D,
2407 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2408 DefaultDataSharingAttributes DefaultAttr) {
2409 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2410 (DefaultAttr == DSA_firstprivate || DefaultAttr == DSA_private);
2411 },
2412 DPred: [](OpenMPDirectiveKind) { return true; },
2413 DSAStack->isClauseParsingMode());
2414 if (DVarPrivate.CKind != OMPC_unknown)
2415 return true;
2416 return false;
2417}
2418
2419static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
2420 Expr *CaptureExpr, bool WithInit,
2421 DeclContext *CurContext,
2422 bool AsExpression);
2423
2424VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2425 unsigned StopAt) {
2426 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2427 D = getCanonicalDecl(D);
2428
2429 auto *VD = dyn_cast<VarDecl>(Val: D);
2430 // Do not capture constexpr variables.
2431 if (VD && VD->isConstexpr())
2432 return nullptr;
2433
2434 // If we want to determine whether the variable should be captured from the
2435 // perspective of the current capturing scope, and we've already left all the
2436 // capturing scopes of the top directive on the stack, check from the
2437 // perspective of its parent directive (if any) instead.
2438 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2439 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
2440
2441 // If we are attempting to capture a global variable in a directive with
2442 // 'target' we return true so that this global is also mapped to the device.
2443 //
2444 if (VD && !VD->hasLocalStorage() &&
2445 (SemaRef.getCurCapturedRegion() || SemaRef.getCurBlock() ||
2446 SemaRef.getCurLambda())) {
2447 if (isInOpenMPTargetExecutionDirective()) {
2448 DSAStackTy::DSAVarData DVarTop =
2449 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2450 if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr)
2451 return VD;
2452 // If the declaration is enclosed in a 'declare target' directive,
2453 // then it should not be captured.
2454 //
2455 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2456 return nullptr;
2457 CapturedRegionScopeInfo *CSI = nullptr;
2458 for (FunctionScopeInfo *FSI : llvm::drop_begin(
2459 RangeOrContainer: llvm::reverse(C&: SemaRef.FunctionScopes),
2460 N: CheckScopeInfo ? (SemaRef.FunctionScopes.size() - (StopAt + 1))
2461 : 0)) {
2462 if (!isa<CapturingScopeInfo>(Val: FSI))
2463 return nullptr;
2464 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2465 if (RSI->CapRegionKind == CR_OpenMP) {
2466 CSI = RSI;
2467 break;
2468 }
2469 }
2470 assert(CSI && "Failed to find CapturedRegionScopeInfo");
2471 SmallVector<OpenMPDirectiveKind, 4> Regions;
2472 getOpenMPCaptureRegions(CaptureRegions&: Regions,
2473 DSAStack->getDirective(Level: CSI->OpenMPLevel));
2474 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2475 return VD;
2476 }
2477 if (isInOpenMPDeclareTargetContext()) {
2478 // Try to mark variable as declare target if it is used in capturing
2479 // regions.
2480 if (getLangOpts().OpenMP <= 45 &&
2481 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2482 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: VD);
2483 return nullptr;
2484 }
2485 }
2486
2487 if (CheckScopeInfo) {
2488 bool OpenMPFound = false;
2489 for (unsigned I = StopAt + 1; I > 0; --I) {
2490 FunctionScopeInfo *FSI = SemaRef.FunctionScopes[I - 1];
2491 if (!isa<CapturingScopeInfo>(Val: FSI))
2492 return nullptr;
2493 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2494 if (RSI->CapRegionKind == CR_OpenMP) {
2495 OpenMPFound = true;
2496 break;
2497 }
2498 }
2499 if (!OpenMPFound)
2500 return nullptr;
2501 }
2502
2503 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2504 (!DSAStack->isClauseParsingMode() ||
2505 DSAStack->getParentDirective() != OMPD_unknown)) {
2506 auto &&Info = DSAStack->isLoopControlVariable(D);
2507 if (Info.first ||
2508 (VD && VD->hasLocalStorage() &&
2509 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2510 (VD && DSAStack->isForceVarCapturing()))
2511 return VD ? VD : Info.second;
2512 DSAStackTy::DSAVarData DVarTop =
2513 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2514 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(Kind: DVarTop.CKind) &&
2515 (!VD || VD->hasLocalStorage() ||
2516 !(DVarTop.AppliedToPointee && DVarTop.CKind != OMPC_reduction)))
2517 return VD ? VD : cast<VarDecl>(Val: DVarTop.PrivateCopy->getDecl());
2518 // Threadprivate variables must not be captured.
2519 if (isOpenMPThreadPrivate(Kind: DVarTop.CKind))
2520 return nullptr;
2521 // The variable is not private or it is the variable in the directive with
2522 // default(none) clause and not used in any clause.
2523 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2524 D,
2525 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
2526 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
2527 },
2528 DPred: [](OpenMPDirectiveKind) { return true; },
2529 DSAStack->isClauseParsingMode());
2530 // Global shared must not be captured.
2531 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown &&
2532 ((DSAStack->getDefaultDSA() != DSA_none &&
2533 DSAStack->getDefaultDSA() != DSA_private &&
2534 DSAStack->getDefaultDSA() != DSA_firstprivate) ||
2535 DVarTop.CKind == OMPC_shared))
2536 return nullptr;
2537 auto *FD = dyn_cast<FieldDecl>(Val: D);
2538 if (DVarPrivate.CKind != OMPC_unknown && !VD && FD &&
2539 !DVarPrivate.PrivateCopy) {
2540 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2541 D,
2542 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2543 DefaultDataSharingAttributes DefaultAttr) {
2544 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2545 (DefaultAttr == DSA_firstprivate ||
2546 DefaultAttr == DSA_private);
2547 },
2548 DPred: [](OpenMPDirectiveKind) { return true; },
2549 DSAStack->isClauseParsingMode());
2550 if (DVarPrivate.CKind == OMPC_unknown)
2551 return nullptr;
2552
2553 VarDecl *VD = DSAStack->getImplicitFDCapExprDecl(FD);
2554 if (VD)
2555 return VD;
2556 if (SemaRef.getCurrentThisType().isNull())
2557 return nullptr;
2558 Expr *ThisExpr = SemaRef.BuildCXXThisExpr(Loc: SourceLocation(),
2559 Type: SemaRef.getCurrentThisType(),
2560 /*IsImplicit=*/true);
2561 const CXXScopeSpec CS = CXXScopeSpec();
2562 Expr *ME = SemaRef.BuildMemberExpr(
2563 Base: ThisExpr, /*IsArrow=*/true, OpLoc: SourceLocation(),
2564 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: FD,
2565 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
2566 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(), Ty: FD->getType(),
2567 VK: VK_LValue, OK: OK_Ordinary);
2568 OMPCapturedExprDecl *CD = buildCaptureDecl(
2569 S&: SemaRef, Id: FD->getIdentifier(), CaptureExpr: ME, WithInit: DVarPrivate.CKind != OMPC_private,
2570 CurContext: SemaRef.CurContext->getParent(), /*AsExpression=*/false);
2571 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
2572 S&: SemaRef, D: CD, Ty: CD->getType().getNonReferenceType(), Loc: SourceLocation());
2573 VD = cast<VarDecl>(Val: VDPrivateRefExpr->getDecl());
2574 DSAStack->addImplicitDefaultFirstprivateFD(FD, VD);
2575 return VD;
2576 }
2577 if (DVarPrivate.CKind != OMPC_unknown ||
2578 (VD && (DSAStack->getDefaultDSA() == DSA_none ||
2579 DSAStack->getDefaultDSA() == DSA_private ||
2580 DSAStack->getDefaultDSA() == DSA_firstprivate)))
2581 return VD ? VD : cast<VarDecl>(Val: DVarPrivate.PrivateCopy->getDecl());
2582 }
2583 return nullptr;
2584}
2585
2586void SemaOpenMP::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2587 unsigned Level) const {
2588 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2589}
2590
2591void SemaOpenMP::startOpenMPLoop() {
2592 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2593 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2594 DSAStack->loopInit();
2595}
2596
2597void SemaOpenMP::startOpenMPCXXRangeFor() {
2598 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2599 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2600 DSAStack->resetPossibleLoopCounter();
2601 DSAStack->loopStart();
2602 }
2603}
2604
2605OpenMPClauseKind SemaOpenMP::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2606 unsigned CapLevel) const {
2607 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2608 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2609 (!DSAStack->isClauseParsingMode() ||
2610 DSAStack->getParentDirective() != OMPD_unknown)) {
2611 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2612 D,
2613 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2614 DefaultDataSharingAttributes DefaultAttr) {
2615 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2616 DefaultAttr == DSA_private;
2617 },
2618 DPred: [](OpenMPDirectiveKind) { return true; },
2619 DSAStack->isClauseParsingMode());
2620 if (DVarPrivate.CKind == OMPC_private && isa<OMPCapturedExprDecl>(Val: D) &&
2621 DSAStack->isImplicitDefaultFirstprivateFD(VD: cast<VarDecl>(Val: D)) &&
2622 !DSAStack->isLoopControlVariable(D).first)
2623 return OMPC_private;
2624 }
2625 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTaskingDirective, Level)) {
2626 bool IsTriviallyCopyable =
2627 D->getType().getNonReferenceType().isTriviallyCopyableType(
2628 Context: getASTContext()) &&
2629 !D->getType()
2630 .getNonReferenceType()
2631 .getCanonicalType()
2632 ->getAsCXXRecordDecl();
2633 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2634 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2635 getOpenMPCaptureRegions(CaptureRegions, DKind);
2636 if (isOpenMPTaskingDirective(Kind: CaptureRegions[CapLevel]) &&
2637 (IsTriviallyCopyable ||
2638 !isOpenMPTaskLoopDirective(DKind: CaptureRegions[CapLevel]))) {
2639 if (DSAStack->hasExplicitDSA(
2640 D,
2641 CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; },
2642 Level, /*NotLastprivate=*/true))
2643 return OMPC_firstprivate;
2644 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2645 if (DVar.CKind != OMPC_shared &&
2646 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) {
2647 DSAStack->addImplicitTaskFirstprivate(Level, D);
2648 return OMPC_firstprivate;
2649 }
2650 }
2651 }
2652 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()) &&
2653 !isOpenMPLoopTransformationDirective(DSAStack->getCurrentDirective())) {
2654 if (DSAStack->getAssociatedLoops() > 0 && !DSAStack->isLoopStarted()) {
2655 DSAStack->resetPossibleLoopCounter(D);
2656 DSAStack->loopStart();
2657 return OMPC_private;
2658 }
2659 if ((DSAStack->getPossiblyLoopCounter() == D->getCanonicalDecl() ||
2660 DSAStack->isLoopControlVariable(D).first) &&
2661 !DSAStack->hasExplicitDSA(
2662 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_private; },
2663 Level) &&
2664 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2665 return OMPC_private;
2666 }
2667 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2668 if (DSAStack->isThreadPrivate(D: const_cast<VarDecl *>(VD)) &&
2669 DSAStack->isForceVarCapturing() &&
2670 !DSAStack->hasExplicitDSA(
2671 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; },
2672 Level))
2673 return OMPC_private;
2674 }
2675 // User-defined allocators are private since they must be defined in the
2676 // context of target region.
2677 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level) &&
2678 DSAStack->isUsesAllocatorsDecl(Level, D).value_or(
2679 u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2680 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2681 return OMPC_private;
2682 return (DSAStack->hasExplicitDSA(
2683 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_private; },
2684 Level) ||
2685 (DSAStack->isClauseParsingMode() &&
2686 DSAStack->getClauseParsingMode() == OMPC_private) ||
2687 // Consider taskgroup reduction descriptor variable a private
2688 // to avoid possible capture in the region.
2689 (DSAStack->hasExplicitDirective(
2690 DPred: [](OpenMPDirectiveKind K) {
2691 return K == OMPD_taskgroup ||
2692 ((isOpenMPParallelDirective(DKind: K) ||
2693 isOpenMPWorksharingDirective(DKind: K)) &&
2694 !isOpenMPSimdDirective(DKind: K));
2695 },
2696 Level) &&
2697 DSAStack->isTaskgroupReductionRef(VD: D, Level)))
2698 ? OMPC_private
2699 : OMPC_unknown;
2700}
2701
2702void SemaOpenMP::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2703 unsigned Level) {
2704 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2705 D = getCanonicalDecl(D);
2706 OpenMPClauseKind OMPC = OMPC_unknown;
2707 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2708 const unsigned NewLevel = I - 1;
2709 if (DSAStack->hasExplicitDSA(
2710 D,
2711 CPred: [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2712 if (isOpenMPPrivate(Kind: K) && !AppliedToPointee) {
2713 OMPC = K;
2714 return true;
2715 }
2716 return false;
2717 },
2718 Level: NewLevel))
2719 break;
2720 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2721 VD: D, Level: NewLevel,
2722 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2723 OpenMPClauseKind) { return true; })) {
2724 OMPC = OMPC_map;
2725 break;
2726 }
2727 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2728 Level: NewLevel)) {
2729 OMPC = OMPC_map;
2730 if (DSAStack->mustBeFirstprivateAtLevel(
2731 Level: NewLevel, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)))
2732 OMPC = OMPC_firstprivate;
2733 break;
2734 }
2735 }
2736 if (OMPC != OMPC_unknown)
2737 FD->addAttr(
2738 A: OMPCaptureKindAttr::CreateImplicit(Ctx&: getASTContext(), CaptureKindVal: unsigned(OMPC)));
2739}
2740
2741bool SemaOpenMP::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2742 unsigned CaptureLevel) const {
2743 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2744 // Return true if the current level is no longer enclosed in a target region.
2745
2746 SmallVector<OpenMPDirectiveKind, 4> Regions;
2747 getOpenMPCaptureRegions(CaptureRegions&: Regions, DSAStack->getDirective(Level));
2748 const auto *VD = dyn_cast<VarDecl>(Val: D);
2749 return VD && !VD->hasLocalStorage() &&
2750 DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2751 Level) &&
2752 Regions[CaptureLevel] != OMPD_task;
2753}
2754
2755bool SemaOpenMP::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2756 unsigned CaptureLevel) const {
2757 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2758 // Return true if the current level is no longer enclosed in a target region.
2759
2760 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2761 if (!VD->hasLocalStorage()) {
2762 if (isInOpenMPTargetExecutionDirective())
2763 return true;
2764 DSAStackTy::DSAVarData TopDVar =
2765 DSAStack->getTopDSA(D, /*FromParent=*/false);
2766 unsigned NumLevels =
2767 getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2768 if (Level == 0)
2769 // non-file scope static variable with default(firstprivate)
2770 // should be global captured.
2771 return (NumLevels == CaptureLevel + 1 &&
2772 (TopDVar.CKind != OMPC_shared ||
2773 DSAStack->getDefaultDSA() == DSA_firstprivate));
2774 do {
2775 --Level;
2776 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2777 if (DVar.CKind != OMPC_shared)
2778 return true;
2779 } while (Level > 0);
2780 }
2781 }
2782 return true;
2783}
2784
2785void SemaOpenMP::DestroyDataSharingAttributesStack() { delete DSAStack; }
2786
2787void SemaOpenMP::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2788 OMPTraitInfo &TI) {
2789 OMPDeclareVariantScopes.push_back(Elt: OMPDeclareVariantScope(TI));
2790}
2791
2792void SemaOpenMP::ActOnOpenMPEndDeclareVariant() {
2793 assert(isInOpenMPDeclareVariantScope() &&
2794 "Not in OpenMP declare variant scope!");
2795
2796 OMPDeclareVariantScopes.pop_back();
2797}
2798
2799void SemaOpenMP::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2800 const FunctionDecl *Callee,
2801 SourceLocation Loc) {
2802 assert(getLangOpts().OpenMP && "Expected OpenMP compilation mode.");
2803 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2804 OMPDeclareTargetDeclAttr::getDeviceType(VD: Caller->getMostRecentDecl());
2805 // Ignore host functions during device analysis.
2806 if (getLangOpts().OpenMPIsTargetDevice &&
2807 (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host))
2808 return;
2809 // Ignore nohost functions during host analysis.
2810 if (!getLangOpts().OpenMPIsTargetDevice && DevTy &&
2811 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2812 return;
2813 const FunctionDecl *FD = Callee->getMostRecentDecl();
2814 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD: FD);
2815 if (getLangOpts().OpenMPIsTargetDevice && DevTy &&
2816 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2817 // Diagnose host function called during device codegen.
2818 StringRef HostDevTy =
2819 getOpenMPSimpleClauseTypeName(Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_host);
2820 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2821 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2822 DiagID: diag::note_omp_marked_device_type_here)
2823 << HostDevTy;
2824 return;
2825 }
2826 if (!getLangOpts().OpenMPIsTargetDevice &&
2827 !getLangOpts().OpenMPOffloadMandatory && DevTy &&
2828 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2829 // In OpenMP 5.2 or later, if the function has a host variant then allow
2830 // that to be called instead
2831 auto &&HasHostAttr = [](const FunctionDecl *Callee) {
2832 for (OMPDeclareVariantAttr *A :
2833 Callee->specific_attrs<OMPDeclareVariantAttr>()) {
2834 auto *DeclRefVariant = cast<DeclRefExpr>(Val: A->getVariantFuncRef());
2835 auto *VariantFD = cast<FunctionDecl>(Val: DeclRefVariant->getDecl());
2836 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2837 OMPDeclareTargetDeclAttr::getDeviceType(
2838 VD: VariantFD->getMostRecentDecl());
2839 if (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2840 return true;
2841 }
2842 return false;
2843 };
2844 if (getLangOpts().OpenMP >= 52 &&
2845 Callee->hasAttr<OMPDeclareVariantAttr>() && HasHostAttr(Callee))
2846 return;
2847 // Diagnose nohost function called during host codegen.
2848 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2849 Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_nohost);
2850 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2851 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2852 DiagID: diag::note_omp_marked_device_type_here)
2853 << NoHostDevTy;
2854 }
2855}
2856
2857void SemaOpenMP::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2858 const DeclarationNameInfo &DirName,
2859 Scope *CurScope, SourceLocation Loc) {
2860 DSAStack->push(DKind, DirName, CurScope, Loc);
2861 SemaRef.PushExpressionEvaluationContext(
2862 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
2863}
2864
2865void SemaOpenMP::StartOpenMPClause(OpenMPClauseKind K) {
2866 DSAStack->setClauseParsingMode(K);
2867}
2868
2869void SemaOpenMP::EndOpenMPClause() {
2870 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2871 SemaRef.CleanupVarDeclMarking();
2872}
2873
2874static std::pair<ValueDecl *, bool>
2875getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2876 SourceRange &ERange, bool AllowArraySection = false,
2877 bool AllowAssumedSizeArray = false, StringRef DiagType = "");
2878
2879/// Check consistency of the reduction clauses.
2880static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2881 ArrayRef<OMPClause *> Clauses) {
2882 bool InscanFound = false;
2883 SourceLocation InscanLoc;
2884 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2885 // A reduction clause without the inscan reduction-modifier may not appear on
2886 // a construct on which a reduction clause with the inscan reduction-modifier
2887 // appears.
2888 for (OMPClause *C : Clauses) {
2889 if (C->getClauseKind() != OMPC_reduction)
2890 continue;
2891 auto *RC = cast<OMPReductionClause>(Val: C);
2892 if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2893 InscanFound = true;
2894 InscanLoc = RC->getModifierLoc();
2895 continue;
2896 }
2897 if (RC->getModifier() == OMPC_REDUCTION_task) {
2898 // OpenMP 5.0, 2.19.5.4 reduction Clause.
2899 // A reduction clause with the task reduction-modifier may only appear on
2900 // a parallel construct, a worksharing construct or a combined or
2901 // composite construct for which any of the aforementioned constructs is a
2902 // constituent construct and simd or loop are not constituent constructs.
2903 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2904 if (!(isOpenMPParallelDirective(DKind: CurDir) ||
2905 isOpenMPWorksharingDirective(DKind: CurDir)) ||
2906 isOpenMPSimdDirective(DKind: CurDir))
2907 S.Diag(Loc: RC->getModifierLoc(),
2908 DiagID: diag::err_omp_reduction_task_not_parallel_or_worksharing);
2909 continue;
2910 }
2911 }
2912 if (InscanFound) {
2913 for (OMPClause *C : Clauses) {
2914 if (C->getClauseKind() != OMPC_reduction)
2915 continue;
2916 auto *RC = cast<OMPReductionClause>(Val: C);
2917 if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2918 S.Diag(Loc: RC->getModifier() == OMPC_REDUCTION_unknown
2919 ? RC->getBeginLoc()
2920 : RC->getModifierLoc(),
2921 DiagID: diag::err_omp_inscan_reduction_expected);
2922 S.Diag(Loc: InscanLoc, DiagID: diag::note_omp_previous_inscan_reduction);
2923 continue;
2924 }
2925 for (Expr *Ref : RC->varlist()) {
2926 assert(Ref && "NULL expr in OpenMP reduction clause.");
2927 SourceLocation ELoc;
2928 SourceRange ERange;
2929 Expr *SimpleRefExpr = Ref;
2930 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
2931 /*AllowArraySection=*/true);
2932 ValueDecl *D = Res.first;
2933 if (!D)
2934 continue;
2935 if (!Stack->isUsedInScanDirective(D: getCanonicalDecl(D))) {
2936 S.Diag(Loc: Ref->getExprLoc(),
2937 DiagID: diag::err_omp_reduction_not_inclusive_exclusive)
2938 << Ref->getSourceRange();
2939 }
2940 }
2941 }
2942 }
2943}
2944
2945static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2946 ArrayRef<OMPClause *> Clauses);
2947static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2948 bool WithInit);
2949
2950static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2951 const ValueDecl *D,
2952 const DSAStackTy::DSAVarData &DVar,
2953 bool IsLoopIterVar = false);
2954
2955void SemaOpenMP::EndOpenMPDSABlock(Stmt *CurDirective) {
2956 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2957 // A variable of class type (or array thereof) that appears in a lastprivate
2958 // clause requires an accessible, unambiguous default constructor for the
2959 // class type, unless the list item is also specified in a firstprivate
2960 // clause.
2961
2962 auto FinalizeLastprivate = [&](OMPLastprivateClause *Clause) {
2963 SmallVector<Expr *, 8> PrivateCopies;
2964 for (Expr *DE : Clause->varlist()) {
2965 if (DE->isValueDependent() || DE->isTypeDependent()) {
2966 PrivateCopies.push_back(Elt: nullptr);
2967 continue;
2968 }
2969 auto *DRE = cast<DeclRefExpr>(Val: DE->IgnoreParens());
2970 auto *VD = cast<VarDecl>(Val: DRE->getDecl());
2971 QualType Type = VD->getType().getNonReferenceType();
2972 const DSAStackTy::DSAVarData DVar =
2973 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
2974 if (DVar.CKind != OMPC_lastprivate) {
2975 // The variable is also a firstprivate, so initialization sequence
2976 // for private copy is generated already.
2977 PrivateCopies.push_back(Elt: nullptr);
2978 continue;
2979 }
2980 // Generate helper private variable and initialize it with the
2981 // default value. The address of the original variable is replaced
2982 // by the address of the new private variable in CodeGen. This new
2983 // variable is not added to IdResolver, so the code in the OpenMP
2984 // region uses original variable for proper diagnostics.
2985 VarDecl *VDPrivate = buildVarDecl(
2986 SemaRef, Loc: DE->getExprLoc(), Type: Type.getUnqualifiedType(), Name: VD->getName(),
2987 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr, OrigRef: DRE);
2988 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
2989 if (VDPrivate->isInvalidDecl()) {
2990 PrivateCopies.push_back(Elt: nullptr);
2991 continue;
2992 }
2993 PrivateCopies.push_back(Elt: buildDeclRefExpr(
2994 S&: SemaRef, D: VDPrivate, Ty: DE->getType(), Loc: DE->getExprLoc()));
2995 }
2996 Clause->setPrivateCopies(PrivateCopies);
2997 };
2998
2999 auto FinalizeNontemporal = [&](OMPNontemporalClause *Clause) {
3000 // Finalize nontemporal clause by handling private copies, if any.
3001 SmallVector<Expr *, 8> PrivateRefs;
3002 for (Expr *RefExpr : Clause->varlist()) {
3003 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
3004 SourceLocation ELoc;
3005 SourceRange ERange;
3006 Expr *SimpleRefExpr = RefExpr;
3007 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
3008 if (Res.second)
3009 // It will be analyzed later.
3010 PrivateRefs.push_back(Elt: RefExpr);
3011 ValueDecl *D = Res.first;
3012 if (!D)
3013 continue;
3014
3015 const DSAStackTy::DSAVarData DVar =
3016 DSAStack->getTopDSA(D, /*FromParent=*/false);
3017 PrivateRefs.push_back(Elt: DVar.PrivateCopy ? DVar.PrivateCopy
3018 : SimpleRefExpr);
3019 }
3020 Clause->setPrivateRefs(PrivateRefs);
3021 };
3022
3023 auto FinalizeAllocators = [&](OMPUsesAllocatorsClause *Clause) {
3024 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) {
3025 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
3026 auto *DRE = dyn_cast<DeclRefExpr>(Val: D.Allocator->IgnoreParenImpCasts());
3027 if (!DRE)
3028 continue;
3029 ValueDecl *VD = DRE->getDecl();
3030 if (!VD || !isa<VarDecl>(Val: VD))
3031 continue;
3032 DSAStackTy::DSAVarData DVar =
3033 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
3034 // OpenMP [2.12.5, target Construct]
3035 // Memory allocators that appear in a uses_allocators clause cannot
3036 // appear in other data-sharing attribute clauses or data-mapping
3037 // attribute clauses in the same construct.
3038 Expr *MapExpr = nullptr;
3039 if (DVar.RefExpr ||
3040 DSAStack->checkMappableExprComponentListsForDecl(
3041 VD, /*CurrentRegionOnly=*/true,
3042 Check: [VD, &MapExpr](
3043 OMPClauseMappableExprCommon::MappableExprComponentListRef
3044 MapExprComponents,
3045 OpenMPClauseKind C) {
3046 auto MI = MapExprComponents.rbegin();
3047 auto ME = MapExprComponents.rend();
3048 if (MI != ME &&
3049 MI->getAssociatedDeclaration()->getCanonicalDecl() ==
3050 VD->getCanonicalDecl()) {
3051 MapExpr = MI->getAssociatedExpression();
3052 return true;
3053 }
3054 return false;
3055 })) {
3056 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_allocator_used_in_clauses)
3057 << D.Allocator->getSourceRange();
3058 if (DVar.RefExpr)
3059 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
3060 else
3061 Diag(Loc: MapExpr->getExprLoc(), DiagID: diag::note_used_here)
3062 << MapExpr->getSourceRange();
3063 }
3064 }
3065 };
3066
3067 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(Val: CurDirective)) {
3068 for (OMPClause *C : D->clauses()) {
3069 if (auto *Clause = dyn_cast<OMPLastprivateClause>(Val: C)) {
3070 FinalizeLastprivate(Clause);
3071 } else if (auto *Clause = dyn_cast<OMPNontemporalClause>(Val: C)) {
3072 FinalizeNontemporal(Clause);
3073 } else if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
3074 FinalizeAllocators(Clause);
3075 }
3076 }
3077 // Check allocate clauses.
3078 if (!SemaRef.CurContext->isDependentContext())
3079 checkAllocateClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3080 checkReductionClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3081 }
3082
3083 DSAStack->pop();
3084 SemaRef.DiscardCleanupsInEvaluationContext();
3085 SemaRef.PopExpressionEvaluationContext();
3086}
3087
3088static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
3089 Expr *NumIterations, Sema &SemaRef,
3090 Scope *S, DSAStackTy *Stack);
3091
3092static bool finishLinearClauses(Sema &SemaRef, ArrayRef<OMPClause *> Clauses,
3093 OMPLoopBasedDirective::HelperExprs &B,
3094 DSAStackTy *Stack) {
3095 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
3096 "loop exprs were not built");
3097
3098 if (SemaRef.CurContext->isDependentContext())
3099 return false;
3100
3101 // Finalize the clauses that need pre-built expressions for CodeGen.
3102 for (OMPClause *C : Clauses) {
3103 auto *LC = dyn_cast<OMPLinearClause>(Val: C);
3104 if (!LC)
3105 continue;
3106 if (FinishOpenMPLinearClause(Clause&: *LC, IV: cast<DeclRefExpr>(Val: B.IterationVarRef),
3107 NumIterations: B.NumIterations, SemaRef,
3108 S: SemaRef.getCurScope(), Stack))
3109 return true;
3110 }
3111
3112 return false;
3113}
3114
3115namespace {
3116
3117class VarDeclFilterCCC final : public CorrectionCandidateCallback {
3118private:
3119 Sema &SemaRef;
3120
3121public:
3122 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
3123 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3124 NamedDecl *ND = Candidate.getCorrectionDecl();
3125 if (const auto *VD = dyn_cast_or_null<VarDecl>(Val: ND)) {
3126 return VD->hasGlobalStorage() &&
3127 SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3128 S: SemaRef.getCurScope());
3129 }
3130 return false;
3131 }
3132
3133 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3134 return std::make_unique<VarDeclFilterCCC>(args&: *this);
3135 }
3136};
3137
3138class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
3139private:
3140 Sema &SemaRef;
3141
3142public:
3143 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
3144 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3145 NamedDecl *ND = Candidate.getCorrectionDecl();
3146 if (ND && ((isa<VarDecl>(Val: ND) && ND->getKind() == Decl::Var) ||
3147 isa<FunctionDecl>(Val: ND))) {
3148 return SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3149 S: SemaRef.getCurScope());
3150 }
3151 return false;
3152 }
3153
3154 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3155 return std::make_unique<VarOrFuncDeclFilterCCC>(args&: *this);
3156 }
3157};
3158
3159} // namespace
3160
3161ExprResult SemaOpenMP::ActOnOpenMPIdExpression(Scope *CurScope,
3162 CXXScopeSpec &ScopeSpec,
3163 const DeclarationNameInfo &Id,
3164 OpenMPDirectiveKind Kind) {
3165 ASTContext &Context = getASTContext();
3166 unsigned OMPVersion = getLangOpts().OpenMP;
3167 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
3168 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
3169 /*ObjectType=*/QualType(),
3170 /*AllowBuiltinCreation=*/true);
3171
3172 if (Lookup.isAmbiguous())
3173 return ExprError();
3174
3175 VarDecl *VD;
3176 if (!Lookup.isSingleResult()) {
3177 VarDeclFilterCCC CCC(SemaRef);
3178 if (TypoCorrection Corrected =
3179 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
3180 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
3181 SemaRef.diagnoseTypo(
3182 Correction: Corrected,
3183 TypoDiag: SemaRef.PDiag(DiagID: Lookup.empty() ? diag::err_undeclared_var_use_suggest
3184 : diag::err_omp_expected_var_arg_suggest)
3185 << Id.getName());
3186 VD = Corrected.getCorrectionDeclAs<VarDecl>();
3187 } else {
3188 Diag(Loc: Id.getLoc(), DiagID: Lookup.empty() ? diag::err_undeclared_var_use
3189 : diag::err_omp_expected_var_arg)
3190 << Id.getName();
3191 return ExprError();
3192 }
3193 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
3194 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_expected_var_arg) << Id.getName();
3195 Diag(Loc: Lookup.getFoundDecl()->getLocation(), DiagID: diag::note_declared_at);
3196 return ExprError();
3197 }
3198 Lookup.suppressDiagnostics();
3199
3200 // OpenMP [2.9.2, Syntax, C/C++]
3201 // Variables must be file-scope, namespace-scope, or static block-scope.
3202 if ((Kind == OMPD_threadprivate || Kind == OMPD_groupprivate) &&
3203 !VD->hasGlobalStorage()) {
3204 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_global_var_arg)
3205 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << !VD->isStaticLocal();
3206 bool IsDecl =
3207 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3208 Diag(Loc: VD->getLocation(),
3209 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3210 << VD;
3211 return ExprError();
3212 }
3213
3214 VarDecl *CanonicalVD = VD->getCanonicalDecl();
3215 NamedDecl *ND = CanonicalVD;
3216 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
3217 // A threadprivate or groupprivate directive for file-scope variables must
3218 // appear outside any definition or declaration.
3219 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
3220 !SemaRef.getCurLexicalContext()->isTranslationUnit()) {
3221 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3222 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3223 bool IsDecl =
3224 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3225 Diag(Loc: VD->getLocation(),
3226 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3227 << VD;
3228 return ExprError();
3229 }
3230 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
3231 // A threadprivate or groupprivate directive for static class member
3232 // variables must appear in the class definition, in the same scope in which
3233 // the member variables are declared.
3234 if (CanonicalVD->isStaticDataMember() &&
3235 !CanonicalVD->getDeclContext()->Equals(DC: SemaRef.getCurLexicalContext())) {
3236 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3237 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3238 bool IsDecl =
3239 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3240 Diag(Loc: VD->getLocation(),
3241 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3242 << VD;
3243 return ExprError();
3244 }
3245 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
3246 // A threadprivate or groupprivate directive for namespace-scope variables
3247 // must appear outside any definition or declaration other than the
3248 // namespace definition itself.
3249 if (CanonicalVD->getDeclContext()->isNamespace() &&
3250 (!SemaRef.getCurLexicalContext()->isFileContext() ||
3251 !SemaRef.getCurLexicalContext()->Encloses(
3252 DC: CanonicalVD->getDeclContext()))) {
3253 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3254 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3255 bool IsDecl =
3256 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3257 Diag(Loc: VD->getLocation(),
3258 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3259 << VD;
3260 return ExprError();
3261 }
3262 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
3263 // A threadprivate or groupprivate directive for static block-scope
3264 // variables must appear in the scope of the variable and not in a nested
3265 // scope.
3266 if (CanonicalVD->isLocalVarDecl() && CurScope &&
3267 !SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(), S: CurScope)) {
3268 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3269 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3270 bool IsDecl =
3271 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3272 Diag(Loc: VD->getLocation(),
3273 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3274 << VD;
3275 return ExprError();
3276 }
3277
3278 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
3279 // A threadprivate or groupprivate directive must lexically precede all
3280 // references to any of the variables in its list.
3281 if ((Kind == OMPD_threadprivate && VD->isUsed() &&
3282 !DSAStack->isThreadPrivate(D: VD)) ||
3283 (Kind == OMPD_groupprivate && VD->isUsed())) {
3284 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_used)
3285 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3286 return ExprError();
3287 }
3288
3289 QualType ExprType = VD->getType().getNonReferenceType();
3290 return DeclRefExpr::Create(Context, QualifierLoc: NestedNameSpecifierLoc(),
3291 TemplateKWLoc: SourceLocation(), D: VD,
3292 /*RefersToEnclosingVariableOrCapture=*/false,
3293 NameLoc: Id.getLoc(), T: ExprType, VK: VK_LValue);
3294}
3295
3296SemaOpenMP::DeclGroupPtrTy
3297SemaOpenMP::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
3298 ArrayRef<Expr *> VarList) {
3299 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
3300 SemaRef.CurContext->addDecl(D);
3301 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3302 }
3303 return nullptr;
3304}
3305
3306SemaOpenMP::DeclGroupPtrTy
3307SemaOpenMP::ActOnOpenMPGroupPrivateDirective(SourceLocation Loc,
3308 ArrayRef<Expr *> VarList) {
3309 if (!getLangOpts().OpenMP || getLangOpts().OpenMP < 60) {
3310 Diag(Loc, DiagID: diag::err_omp_unexpected_directive)
3311 << getOpenMPDirectiveName(D: OMPD_groupprivate, Ver: getLangOpts().OpenMP);
3312 return nullptr;
3313 }
3314 if (OMPGroupPrivateDecl *D = CheckOMPGroupPrivateDecl(Loc, VarList)) {
3315 SemaRef.CurContext->addDecl(D);
3316 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3317 }
3318 return nullptr;
3319}
3320
3321namespace {
3322class LocalVarRefChecker final
3323 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
3324 Sema &SemaRef;
3325
3326public:
3327 bool VisitDeclRefExpr(const DeclRefExpr *E) {
3328 if (const auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
3329 if (VD->hasLocalStorage()) {
3330 SemaRef.Diag(Loc: E->getBeginLoc(),
3331 DiagID: diag::err_omp_local_var_in_threadprivate_init)
3332 << E->getSourceRange();
3333 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_defined_here)
3334 << VD << VD->getSourceRange();
3335 return true;
3336 }
3337 }
3338 return false;
3339 }
3340 bool VisitStmt(const Stmt *S) {
3341 for (const Stmt *Child : S->children()) {
3342 if (Child && Visit(S: Child))
3343 return true;
3344 }
3345 return false;
3346 }
3347 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
3348};
3349} // namespace
3350
3351OMPThreadPrivateDecl *
3352SemaOpenMP::CheckOMPThreadPrivateDecl(SourceLocation Loc,
3353 ArrayRef<Expr *> VarList) {
3354 ASTContext &Context = getASTContext();
3355 SmallVector<Expr *, 8> Vars;
3356 for (Expr *RefExpr : VarList) {
3357 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3358 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3359 SourceLocation ILoc = DE->getExprLoc();
3360
3361 // Mark variable as used.
3362 VD->setReferenced();
3363 VD->markUsed(C&: Context);
3364
3365 QualType QType = VD->getType();
3366 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3367 // It will be analyzed later.
3368 Vars.push_back(Elt: DE);
3369 continue;
3370 }
3371
3372 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3373 // A threadprivate variable must not have an incomplete type.
3374 if (SemaRef.RequireCompleteType(
3375 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_threadprivate_incomplete_type)) {
3376 continue;
3377 }
3378
3379 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3380 // A threadprivate variable must not have a reference type.
3381 if (VD->getType()->isReferenceType()) {
3382 unsigned OMPVersion = getLangOpts().OpenMP;
3383 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3384 << getOpenMPDirectiveName(D: OMPD_threadprivate, Ver: OMPVersion)
3385 << VD->getType();
3386 bool IsDecl =
3387 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3388 Diag(Loc: VD->getLocation(),
3389 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3390 << VD;
3391 continue;
3392 }
3393
3394 // Check if this is a TLS variable. If TLS is not being supported, produce
3395 // the corresponding diagnostic.
3396 if ((VD->getTLSKind() != VarDecl::TLS_None &&
3397 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
3398 getLangOpts().OpenMPUseTLS &&
3399 getASTContext().getTargetInfo().isTLSSupported())) ||
3400 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3401 !VD->isLocalVarDecl())) {
3402 Diag(Loc: ILoc, DiagID: diag::err_omp_var_thread_local)
3403 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
3404 bool IsDecl =
3405 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3406 Diag(Loc: VD->getLocation(),
3407 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3408 << VD;
3409 continue;
3410 }
3411
3412 // Check if initial value of threadprivate variable reference variable with
3413 // local storage (it is not supported by runtime).
3414 if (const Expr *Init = VD->getAnyInitializer()) {
3415 LocalVarRefChecker Checker(SemaRef);
3416 if (Checker.Visit(S: Init))
3417 continue;
3418 }
3419
3420 Vars.push_back(Elt: RefExpr);
3421 DSAStack->addDSA(D: VD, E: DE, A: OMPC_threadprivate);
3422 VD->addAttr(A: OMPThreadPrivateDeclAttr::CreateImplicit(
3423 Ctx&: Context, Range: SourceRange(Loc, Loc)));
3424 if (ASTMutationListener *ML = Context.getASTMutationListener())
3425 ML->DeclarationMarkedOpenMPThreadPrivate(D: VD);
3426 }
3427 OMPThreadPrivateDecl *D = nullptr;
3428 if (!Vars.empty()) {
3429 D = OMPThreadPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3430 L: Loc, VL: Vars);
3431 D->setAccess(AS_public);
3432 }
3433 return D;
3434}
3435
3436OMPGroupPrivateDecl *
3437SemaOpenMP::CheckOMPGroupPrivateDecl(SourceLocation Loc,
3438 ArrayRef<Expr *> VarList) {
3439 ASTContext &Context = getASTContext();
3440 SmallVector<Expr *, 8> Vars;
3441 for (Expr *RefExpr : VarList) {
3442 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3443 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3444 SourceLocation ILoc = DE->getExprLoc();
3445
3446 // Mark variable as used.
3447 VD->setReferenced();
3448 VD->markUsed(C&: Context);
3449
3450 QualType QType = VD->getType();
3451 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3452 // It will be analyzed later.
3453 Vars.push_back(Elt: DE);
3454 continue;
3455 }
3456
3457 // OpenMP groupprivate restrictions:
3458 // A groupprivate variable must not have an incomplete type.
3459 if (SemaRef.RequireCompleteType(
3460 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_groupprivate_incomplete_type)) {
3461 continue;
3462 }
3463
3464 // A groupprivate variable must not have a reference type.
3465 if (VD->getType()->isReferenceType()) {
3466 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3467 << getOpenMPDirectiveName(D: OMPD_groupprivate) << VD->getType();
3468 bool IsDecl =
3469 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3470 Diag(Loc: VD->getLocation(),
3471 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3472 << VD;
3473 continue;
3474 }
3475
3476 // A variable that is declared with an initializer must not appear in a
3477 // groupprivate directive.
3478 if (VD->getAnyInitializer()) {
3479 Diag(Loc: ILoc, DiagID: diag::err_omp_groupprivate_with_initializer)
3480 << VD->getDeclName();
3481 bool IsDecl =
3482 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3483 Diag(Loc: VD->getLocation(),
3484 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3485 << VD;
3486 continue;
3487 }
3488
3489 Vars.push_back(Elt: RefExpr);
3490 DSAStack->addDSA(D: VD, E: DE, A: OMPC_groupprivate);
3491 VD->addAttr(A: OMPGroupPrivateDeclAttr::CreateImplicit(Ctx&: Context,
3492 Range: SourceRange(Loc, Loc)));
3493 if (ASTMutationListener *ML = Context.getASTMutationListener())
3494 ML->DeclarationMarkedOpenMPGroupPrivate(D: VD);
3495 }
3496 OMPGroupPrivateDecl *D = nullptr;
3497 if (!Vars.empty()) {
3498 D = OMPGroupPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3499 L: Loc, VL: Vars);
3500 D->setAccess(AS_public);
3501 }
3502 return D;
3503}
3504
3505static OMPAllocateDeclAttr::AllocatorTypeTy
3506getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3507 if (!Allocator)
3508 return OMPAllocateDeclAttr::OMPNullMemAlloc;
3509 if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3510 Allocator->isInstantiationDependent() ||
3511 Allocator->containsUnexpandedParameterPack())
3512 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3513 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3514 llvm::FoldingSetNodeID AEId;
3515 const Expr *AE = Allocator->IgnoreParenImpCasts();
3516 AE->IgnoreImpCasts()->Profile(ID&: AEId, Context: S.getASTContext(), /*Canonical=*/true);
3517 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
3518 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3519 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3520 llvm::FoldingSetNodeID DAEId;
3521 DefAllocator->IgnoreImpCasts()->Profile(ID&: DAEId, Context: S.getASTContext(),
3522 /*Canonical=*/true);
3523 if (AEId == DAEId) {
3524 AllocatorKindRes = AllocatorKind;
3525 break;
3526 }
3527 }
3528 return AllocatorKindRes;
3529}
3530
3531static bool checkPreviousOMPAllocateAttribute(
3532 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3533 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3534 if (!VD->hasAttr<OMPAllocateDeclAttr>())
3535 return false;
3536 const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3537 Expr *PrevAllocator = A->getAllocator();
3538 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3539 getAllocatorKind(S, Stack, Allocator: PrevAllocator);
3540 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3541 if (AllocatorsMatch &&
3542 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
3543 Allocator && PrevAllocator) {
3544 const Expr *AE = Allocator->IgnoreParenImpCasts();
3545 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3546 llvm::FoldingSetNodeID AEId, PAEId;
3547 AE->Profile(ID&: AEId, Context: S.Context, /*Canonical=*/true);
3548 PAE->Profile(ID&: PAEId, Context: S.Context, /*Canonical=*/true);
3549 AllocatorsMatch = AEId == PAEId;
3550 }
3551 if (!AllocatorsMatch) {
3552 SmallString<256> AllocatorBuffer;
3553 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3554 if (Allocator)
3555 Allocator->printPretty(OS&: AllocatorStream, Helper: nullptr, Policy: S.getPrintingPolicy());
3556 SmallString<256> PrevAllocatorBuffer;
3557 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3558 if (PrevAllocator)
3559 PrevAllocator->printPretty(OS&: PrevAllocatorStream, Helper: nullptr,
3560 Policy: S.getPrintingPolicy());
3561
3562 SourceLocation AllocatorLoc =
3563 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
3564 SourceRange AllocatorRange =
3565 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
3566 SourceLocation PrevAllocatorLoc =
3567 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
3568 SourceRange PrevAllocatorRange =
3569 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
3570 S.Diag(Loc: AllocatorLoc, DiagID: diag::warn_omp_used_different_allocator)
3571 << (Allocator ? 1 : 0) << AllocatorStream.str()
3572 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
3573 << AllocatorRange;
3574 S.Diag(Loc: PrevAllocatorLoc, DiagID: diag::note_omp_previous_allocator)
3575 << PrevAllocatorRange;
3576 return true;
3577 }
3578 return false;
3579}
3580
3581static void
3582applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3583 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3584 Expr *Allocator, Expr *Alignment, SourceRange SR) {
3585 if (VD->hasAttr<OMPAllocateDeclAttr>())
3586 return;
3587 if (Alignment &&
3588 (Alignment->isTypeDependent() || Alignment->isValueDependent() ||
3589 Alignment->isInstantiationDependent() ||
3590 Alignment->containsUnexpandedParameterPack()))
3591 // Apply later when we have a usable value.
3592 return;
3593 if (Allocator &&
3594 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3595 Allocator->isInstantiationDependent() ||
3596 Allocator->containsUnexpandedParameterPack()))
3597 return;
3598 auto *A = OMPAllocateDeclAttr::CreateImplicit(Ctx&: S.Context, AllocatorType: AllocatorKind,
3599 Allocator, Alignment, Range: SR);
3600 VD->addAttr(A);
3601 if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3602 ML->DeclarationMarkedOpenMPAllocate(D: VD, A);
3603}
3604
3605SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPAllocateDirective(
3606 SourceLocation Loc, ArrayRef<Expr *> VarList, ArrayRef<OMPClause *> Clauses,
3607 DeclContext *Owner) {
3608 assert(Clauses.size() <= 2 && "Expected at most two clauses.");
3609 Expr *Alignment = nullptr;
3610 Expr *Allocator = nullptr;
3611 if (Clauses.empty()) {
3612 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3613 // allocate directives that appear in a target region must specify an
3614 // allocator clause unless a requires directive with the dynamic_allocators
3615 // clause is present in the same compilation unit.
3616 if (getLangOpts().OpenMPIsTargetDevice &&
3617 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3618 SemaRef.targetDiag(Loc, DiagID: diag::err_expected_allocator_clause);
3619 } else {
3620 for (const OMPClause *C : Clauses)
3621 if (const auto *AC = dyn_cast<OMPAllocatorClause>(Val: C))
3622 Allocator = AC->getAllocator();
3623 else if (const auto *AC = dyn_cast<OMPAlignClause>(Val: C))
3624 Alignment = AC->getAlignment();
3625 else
3626 llvm_unreachable("Unexpected clause on allocate directive");
3627 }
3628 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3629 getAllocatorKind(S&: SemaRef, DSAStack, Allocator);
3630 SmallVector<Expr *, 8> Vars;
3631 for (Expr *RefExpr : VarList) {
3632 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3633 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3634
3635 // Check if this is a TLS variable or global register.
3636 if (VD->getTLSKind() != VarDecl::TLS_None ||
3637 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3638 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3639 !VD->isLocalVarDecl()))
3640 continue;
3641
3642 // If the used several times in the allocate directive, the same allocator
3643 // must be used.
3644 if (checkPreviousOMPAllocateAttribute(S&: SemaRef, DSAStack, RefExpr, VD,
3645 AllocatorKind, Allocator))
3646 continue;
3647
3648 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3649 // If a list item has a static storage type, the allocator expression in the
3650 // allocator clause must be a constant expression that evaluates to one of
3651 // the predefined memory allocator values.
3652 if (Allocator && VD->hasGlobalStorage()) {
3653 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3654 Diag(Loc: Allocator->getExprLoc(),
3655 DiagID: diag::err_omp_expected_predefined_allocator)
3656 << Allocator->getSourceRange();
3657 bool IsDecl = VD->isThisDeclarationADefinition(getASTContext()) ==
3658 VarDecl::DeclarationOnly;
3659 Diag(Loc: VD->getLocation(),
3660 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3661 << VD;
3662 continue;
3663 }
3664 }
3665
3666 Vars.push_back(Elt: RefExpr);
3667 applyOMPAllocateAttribute(S&: SemaRef, VD, AllocatorKind, Allocator, Alignment,
3668 SR: DE->getSourceRange());
3669 }
3670 if (Vars.empty())
3671 return nullptr;
3672 if (!Owner)
3673 Owner = SemaRef.getCurLexicalContext();
3674 auto *D = OMPAllocateDecl::Create(C&: getASTContext(), DC: Owner, L: Loc, VL: Vars, CL: Clauses);
3675 D->setAccess(AS_public);
3676 Owner->addDecl(D);
3677 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3678}
3679
3680SemaOpenMP::DeclGroupPtrTy
3681SemaOpenMP::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3682 ArrayRef<OMPClause *> ClauseList) {
3683 OMPRequiresDecl *D = nullptr;
3684 if (!SemaRef.CurContext->isFileContext()) {
3685 Diag(Loc, DiagID: diag::err_omp_invalid_scope) << "requires";
3686 } else {
3687 D = CheckOMPRequiresDecl(Loc, Clauses: ClauseList);
3688 if (D) {
3689 SemaRef.CurContext->addDecl(D);
3690 DSAStack->addRequiresDecl(RD: D);
3691 }
3692 }
3693 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3694}
3695
3696void SemaOpenMP::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3697 OpenMPDirectiveKind DKind,
3698 ArrayRef<std::string> Assumptions,
3699 bool SkippedClauses) {
3700 if (!SkippedClauses && Assumptions.empty()) {
3701 unsigned OMPVersion = getLangOpts().OpenMP;
3702 Diag(Loc, DiagID: diag::err_omp_no_clause_for_directive)
3703 << llvm::omp::getAllAssumeClauseOptions()
3704 << llvm::omp::getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
3705 }
3706
3707 auto *AA =
3708 OMPAssumeAttr::Create(Ctx&: getASTContext(), Assumption: llvm::join(R&: Assumptions, Separator: ","), Range: Loc);
3709 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3710 OMPAssumeScoped.push_back(Elt: AA);
3711 return;
3712 }
3713
3714 // Global assumes without assumption clauses are ignored.
3715 if (Assumptions.empty())
3716 return;
3717
3718 assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3719 "Unexpected omp assumption directive!");
3720 OMPAssumeGlobal.push_back(Elt: AA);
3721
3722 // The OMPAssumeGlobal scope above will take care of new declarations but
3723 // we also want to apply the assumption to existing ones, e.g., to
3724 // declarations in included headers. To this end, we traverse all existing
3725 // declaration contexts and annotate function declarations here.
3726 SmallVector<DeclContext *, 8> DeclContexts;
3727 auto *Ctx = SemaRef.CurContext;
3728 while (Ctx->getLexicalParent())
3729 Ctx = Ctx->getLexicalParent();
3730 DeclContexts.push_back(Elt: Ctx);
3731 while (!DeclContexts.empty()) {
3732 DeclContext *DC = DeclContexts.pop_back_val();
3733 for (auto *SubDC : DC->decls()) {
3734 if (SubDC->isInvalidDecl())
3735 continue;
3736 if (auto *CTD = dyn_cast<ClassTemplateDecl>(Val: SubDC)) {
3737 DeclContexts.push_back(Elt: CTD->getTemplatedDecl());
3738 llvm::append_range(C&: DeclContexts, R: CTD->specializations());
3739 continue;
3740 }
3741 if (auto *DC = dyn_cast<DeclContext>(Val: SubDC))
3742 DeclContexts.push_back(Elt: DC);
3743 if (auto *F = dyn_cast<FunctionDecl>(Val: SubDC)) {
3744 F->addAttr(A: AA);
3745 continue;
3746 }
3747 }
3748 }
3749}
3750
3751void SemaOpenMP::ActOnOpenMPEndAssumesDirective() {
3752 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3753 OMPAssumeScoped.pop_back();
3754}
3755
3756StmtResult SemaOpenMP::ActOnOpenMPAssumeDirective(ArrayRef<OMPClause *> Clauses,
3757 Stmt *AStmt,
3758 SourceLocation StartLoc,
3759 SourceLocation EndLoc) {
3760 if (!AStmt)
3761 return StmtError();
3762
3763 return OMPAssumeDirective::Create(Ctx: getASTContext(), StartLoc, EndLoc, Clauses,
3764 AStmt);
3765}
3766
3767OMPRequiresDecl *
3768SemaOpenMP::CheckOMPRequiresDecl(SourceLocation Loc,
3769 ArrayRef<OMPClause *> ClauseList) {
3770 /// For target specific clauses, the requires directive cannot be
3771 /// specified after the handling of any of the target regions in the
3772 /// current compilation unit.
3773 ArrayRef<SourceLocation> TargetLocations =
3774 DSAStack->getEncounteredTargetLocs();
3775 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3776 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) {
3777 for (const OMPClause *CNew : ClauseList) {
3778 // Check if any of the requires clauses affect target regions.
3779 if (isa<OMPUnifiedSharedMemoryClause>(Val: CNew) ||
3780 isa<OMPUnifiedAddressClause>(Val: CNew) ||
3781 isa<OMPReverseOffloadClause>(Val: CNew) ||
3782 isa<OMPDynamicAllocatorsClause>(Val: CNew)) {
3783 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3784 << "target" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3785 for (SourceLocation TargetLoc : TargetLocations) {
3786 Diag(Loc: TargetLoc, DiagID: diag::note_omp_requires_encountered_directive)
3787 << "target";
3788 }
3789 } else if (!AtomicLoc.isInvalid() &&
3790 isa<OMPAtomicDefaultMemOrderClause>(Val: CNew)) {
3791 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3792 << "atomic" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3793 Diag(Loc: AtomicLoc, DiagID: diag::note_omp_requires_encountered_directive)
3794 << "atomic";
3795 }
3796 }
3797 }
3798
3799 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3800 return OMPRequiresDecl::Create(
3801 C&: getASTContext(), DC: SemaRef.getCurLexicalContext(), L: Loc, CL: ClauseList);
3802 return nullptr;
3803}
3804
3805static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3806 const ValueDecl *D,
3807 const DSAStackTy::DSAVarData &DVar,
3808 bool IsLoopIterVar) {
3809 if (DVar.RefExpr) {
3810 SemaRef.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
3811 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3812 return;
3813 }
3814 enum {
3815 PDSA_StaticMemberShared,
3816 PDSA_StaticLocalVarShared,
3817 PDSA_LoopIterVarPrivate,
3818 PDSA_LoopIterVarLinear,
3819 PDSA_LoopIterVarLastprivate,
3820 PDSA_ConstVarShared,
3821 PDSA_GlobalVarShared,
3822 PDSA_TaskVarFirstprivate,
3823 PDSA_LocalVarPrivate,
3824 PDSA_Implicit
3825 } Reason = PDSA_Implicit;
3826 bool ReportHint = false;
3827 auto ReportLoc = D->getLocation();
3828 auto *VD = dyn_cast<VarDecl>(Val: D);
3829 if (IsLoopIterVar) {
3830 if (DVar.CKind == OMPC_private)
3831 Reason = PDSA_LoopIterVarPrivate;
3832 else if (DVar.CKind == OMPC_lastprivate)
3833 Reason = PDSA_LoopIterVarLastprivate;
3834 else
3835 Reason = PDSA_LoopIterVarLinear;
3836 } else if (isOpenMPTaskingDirective(Kind: DVar.DKind) &&
3837 DVar.CKind == OMPC_firstprivate) {
3838 Reason = PDSA_TaskVarFirstprivate;
3839 ReportLoc = DVar.ImplicitDSALoc;
3840 } else if (VD && VD->isStaticLocal())
3841 Reason = PDSA_StaticLocalVarShared;
3842 else if (VD && VD->isStaticDataMember())
3843 Reason = PDSA_StaticMemberShared;
3844 else if (VD && VD->isFileVarDecl())
3845 Reason = PDSA_GlobalVarShared;
3846 else if (D->getType().isConstant(Ctx: SemaRef.getASTContext()))
3847 Reason = PDSA_ConstVarShared;
3848 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3849 ReportHint = true;
3850 Reason = PDSA_LocalVarPrivate;
3851 }
3852 if (Reason != PDSA_Implicit) {
3853 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
3854 SemaRef.Diag(Loc: ReportLoc, DiagID: diag::note_omp_predetermined_dsa)
3855 << Reason << ReportHint
3856 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), Ver: OMPVersion);
3857 } else if (DVar.ImplicitDSALoc.isValid()) {
3858 SemaRef.Diag(Loc: DVar.ImplicitDSALoc, DiagID: diag::note_omp_implicit_dsa)
3859 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3860 }
3861}
3862
3863static OpenMPMapClauseKind
3864getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3865 bool IsAggregateOrDeclareTarget,
3866 bool HasConstQualifier) {
3867 OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3868 switch (M) {
3869 case OMPC_DEFAULTMAP_MODIFIER_alloc:
3870 case OMPC_DEFAULTMAP_MODIFIER_storage:
3871 Kind = OMPC_MAP_alloc;
3872 break;
3873 case OMPC_DEFAULTMAP_MODIFIER_to:
3874 Kind = OMPC_MAP_to;
3875 break;
3876 case OMPC_DEFAULTMAP_MODIFIER_from:
3877 Kind = OMPC_MAP_from;
3878 break;
3879 case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3880 Kind = OMPC_MAP_tofrom;
3881 break;
3882 case OMPC_DEFAULTMAP_MODIFIER_present:
3883 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
3884 // If implicit-behavior is present, each variable referenced in the
3885 // construct in the category specified by variable-category is treated as if
3886 // it had been listed in a map clause with the map-type of alloc and
3887 // map-type-modifier of present.
3888 Kind = OMPC_MAP_alloc;
3889 break;
3890 case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3891 case OMPC_DEFAULTMAP_MODIFIER_private:
3892 case OMPC_DEFAULTMAP_MODIFIER_last:
3893 llvm_unreachable("Unexpected defaultmap implicit behavior");
3894 case OMPC_DEFAULTMAP_MODIFIER_none:
3895 case OMPC_DEFAULTMAP_MODIFIER_default:
3896 case OMPC_DEFAULTMAP_MODIFIER_unknown:
3897 // IsAggregateOrDeclareTarget could be true if:
3898 // 1. the implicit behavior for aggregate is tofrom
3899 // 2. it's a declare target link
3900 if (IsAggregateOrDeclareTarget) {
3901 if (HasConstQualifier)
3902 Kind = OMPC_MAP_to;
3903 else
3904 Kind = OMPC_MAP_tofrom;
3905 break;
3906 }
3907 llvm_unreachable("Unexpected defaultmap implicit behavior");
3908 }
3909 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3910 return Kind;
3911}
3912
3913static bool hasNoMutableFields(const CXXRecordDecl *RD) {
3914 for (const auto *FD : RD->fields()) {
3915 if (FD->isMutable())
3916 return false;
3917 QualType FT = FD->getType();
3918 while (FT->isArrayType())
3919 FT = FT->getAsArrayTypeUnsafe()->getElementType();
3920 if (const auto *NestedRD = FT->getAsCXXRecordDecl())
3921 if (!hasNoMutableFields(RD: NestedRD))
3922 return false;
3923 }
3924 return true;
3925}
3926
3927static bool hasConstQualifiedMappingType(QualType T) {
3928 while (T->isArrayType())
3929 T = T->getAsArrayTypeUnsafe()->getElementType();
3930 if (!T.isConstQualified())
3931 return false;
3932 if (const auto *RD = T->getAsCXXRecordDecl())
3933 // TODO : Per OpenMP 6.0 p299 lines 3-4, non-mutable members of a
3934 // const-qualified struct should also be ignored for 'from'. This
3935 // requires per-member mapping granularity via compiler-generated
3936 // default mappers and a mechanism to ensure constness to the mapper.
3937 // For now we conservatively treat any struct with mutable members as
3938 // requiring full 'tofrom'.
3939 return hasNoMutableFields(RD);
3940 return true;
3941}
3942
3943namespace {
3944struct VariableImplicitInfo {
3945 static const unsigned MapKindNum = OMPC_MAP_unknown;
3946 static const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_unknown + 1;
3947
3948 llvm::SetVector<Expr *> Privates;
3949 llvm::SetVector<Expr *> Firstprivates;
3950 llvm::SetVector<Expr *> Mappings[DefaultmapKindNum][MapKindNum];
3951 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
3952 MapModifiers[DefaultmapKindNum];
3953};
3954
3955class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3956 DSAStackTy *Stack;
3957 Sema &SemaRef;
3958 OpenMPDirectiveKind DKind = OMPD_unknown;
3959 bool ErrorFound = false;
3960 bool TryCaptureCXXThisMembers = false;
3961 CapturedStmt *CS = nullptr;
3962
3963 VariableImplicitInfo ImpInfo;
3964 SemaOpenMP::VarsWithInheritedDSAType VarsWithInheritedDSA;
3965 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3966
3967 void VisitSubCaptures(OMPExecutableDirective *S) {
3968 // Check implicitly captured variables.
3969 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
3970 return;
3971 if (S->getDirectiveKind() == OMPD_atomic ||
3972 S->getDirectiveKind() == OMPD_critical ||
3973 S->getDirectiveKind() == OMPD_section ||
3974 S->getDirectiveKind() == OMPD_master ||
3975 S->getDirectiveKind() == OMPD_masked ||
3976 S->getDirectiveKind() == OMPD_scope ||
3977 S->getDirectiveKind() == OMPD_assume ||
3978 isOpenMPLoopTransformationDirective(DKind: S->getDirectiveKind())) {
3979 Visit(S: S->getAssociatedStmt());
3980 return;
3981 }
3982 visitSubCaptures(S: S->getInnermostCapturedStmt());
3983 // Try to capture inner this->member references to generate correct mappings
3984 // and diagnostics.
3985 if (TryCaptureCXXThisMembers ||
3986 (isOpenMPTargetExecutionDirective(DKind) &&
3987 llvm::any_of(Range: S->getInnermostCapturedStmt()->captures(),
3988 P: [](const CapturedStmt::Capture &C) {
3989 return C.capturesThis();
3990 }))) {
3991 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3992 TryCaptureCXXThisMembers = true;
3993 Visit(S: S->getInnermostCapturedStmt()->getCapturedStmt());
3994 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3995 }
3996 // In tasks firstprivates are not captured anymore, need to analyze them
3997 // explicitly.
3998 if (isOpenMPTaskingDirective(Kind: S->getDirectiveKind()) &&
3999 !isOpenMPTaskLoopDirective(DKind: S->getDirectiveKind())) {
4000 for (OMPClause *C : S->clauses())
4001 if (auto *FC = dyn_cast<OMPFirstprivateClause>(Val: C)) {
4002 for (Expr *Ref : FC->varlist())
4003 Visit(S: Ref);
4004 }
4005 }
4006 }
4007
4008public:
4009 void VisitDeclRefExpr(DeclRefExpr *E) {
4010 if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
4011 E->isValueDependent() || E->containsUnexpandedParameterPack() ||
4012 E->isInstantiationDependent() ||
4013 E->isNonOdrUse() == clang::NOUR_Unevaluated)
4014 return;
4015 if (auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
4016 // Check the datasharing rules for the expressions in the clauses.
4017 if (!CS || (isa<OMPCapturedExprDecl>(Val: VD) && !CS->capturesVariable(Var: VD) &&
4018 !Stack->getTopDSA(D: VD, /*FromParent=*/false).RefExpr &&
4019 !Stack->isImplicitDefaultFirstprivateFD(VD))) {
4020 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: VD))
4021 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
4022 Visit(S: CED->getInit());
4023 return;
4024 }
4025 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(Val: VD))
4026 // Do not analyze internal variables and do not enclose them into
4027 // implicit clauses.
4028 if (!Stack->isImplicitDefaultFirstprivateFD(VD))
4029 return;
4030 VD = VD->getCanonicalDecl();
4031 // Skip internally declared variables.
4032 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(Var: VD) &&
4033 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4034 !Stack->isImplicitTaskFirstprivate(D: VD))
4035 return;
4036 // Skip allocators in uses_allocators clauses.
4037 if (Stack->isUsesAllocatorsDecl(D: VD))
4038 return;
4039
4040 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
4041 // Check if the variable has explicit DSA set and stop analysis if it so.
4042 if (DVar.RefExpr || !ImplicitDeclarations.insert(V: VD).second)
4043 return;
4044
4045 // Skip internally declared static variables.
4046 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
4047 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
4048 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(Var: VD) &&
4049 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4050 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) &&
4051 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4052 !Stack->isImplicitTaskFirstprivate(D: VD))
4053 return;
4054
4055 SourceLocation ELoc = E->getExprLoc();
4056 // The default(none) clause requires that each variable that is referenced
4057 // in the construct, and does not have a predetermined data-sharing
4058 // attribute, must have its data-sharing attribute explicitly determined
4059 // by being listed in a data-sharing attribute clause.
4060 if (DVar.CKind == OMPC_unknown &&
4061 (Stack->getDefaultDSA() == DSA_none ||
4062 Stack->getDefaultDSA() == DSA_private ||
4063 Stack->getDefaultDSA() == DSA_firstprivate) &&
4064 isImplicitOrExplicitTaskingRegion(DKind) &&
4065 VarsWithInheritedDSA.count(Val: VD) == 0) {
4066 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
4067 if (!InheritedDSA && (Stack->getDefaultDSA() == DSA_firstprivate ||
4068 Stack->getDefaultDSA() == DSA_private)) {
4069 DSAStackTy::DSAVarData DVar =
4070 Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4071 InheritedDSA = DVar.CKind == OMPC_unknown;
4072 }
4073 if (InheritedDSA)
4074 VarsWithInheritedDSA[VD] = E;
4075 if (Stack->getDefaultDSA() == DSA_none)
4076 return;
4077 }
4078
4079 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
4080 // If implicit-behavior is none, each variable referenced in the
4081 // construct that does not have a predetermined data-sharing attribute
4082 // and does not appear in a to or link clause on a declare target
4083 // directive must be listed in a data-mapping attribute clause, a
4084 // data-sharing attribute clause (including a data-sharing attribute
4085 // clause on a combined construct where target. is one of the
4086 // constituent constructs), or an is_device_ptr clause.
4087 OpenMPDefaultmapClauseKind ClauseKind =
4088 getVariableCategoryFromDecl(LO: SemaRef.getLangOpts(), VD);
4089 if (SemaRef.getLangOpts().OpenMP >= 50) {
4090 bool IsModifierNone = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4091 OMPC_DEFAULTMAP_MODIFIER_none;
4092 if (DVar.CKind == OMPC_unknown && IsModifierNone &&
4093 VarsWithInheritedDSA.count(Val: VD) == 0 && !Res) {
4094 // Only check for data-mapping attribute and is_device_ptr here
4095 // since we have already make sure that the declaration does not
4096 // have a data-sharing attribute above
4097 if (!Stack->checkMappableExprComponentListsForDecl(
4098 VD, /*CurrentRegionOnly=*/true,
4099 Check: [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
4100 MapExprComponents,
4101 OpenMPClauseKind) {
4102 auto MI = MapExprComponents.rbegin();
4103 auto ME = MapExprComponents.rend();
4104 return MI != ME && MI->getAssociatedDeclaration() == VD;
4105 })) {
4106 VarsWithInheritedDSA[VD] = E;
4107 return;
4108 }
4109 }
4110 }
4111 if (SemaRef.getLangOpts().OpenMP > 50) {
4112 bool IsModifierPresent = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4113 OMPC_DEFAULTMAP_MODIFIER_present;
4114 if (IsModifierPresent) {
4115 if (!llvm::is_contained(Range&: ImpInfo.MapModifiers[ClauseKind],
4116 Element: OMPC_MAP_MODIFIER_present)) {
4117 ImpInfo.MapModifiers[ClauseKind].push_back(
4118 Elt: OMPC_MAP_MODIFIER_present);
4119 }
4120 }
4121 }
4122
4123 if (isOpenMPTargetExecutionDirective(DKind) &&
4124 !Stack->isLoopControlVariable(D: VD).first) {
4125 if (!Stack->checkMappableExprComponentListsForDecl(
4126 VD, /*CurrentRegionOnly=*/true,
4127 Check: [this](OMPClauseMappableExprCommon::MappableExprComponentListRef
4128 StackComponents,
4129 OpenMPClauseKind) {
4130 if (SemaRef.LangOpts.OpenMP >= 50)
4131 return !StackComponents.empty();
4132 // Variable is used if it has been marked as an array, array
4133 // section, array shaping or the variable itself.
4134 return StackComponents.size() == 1 ||
4135 llvm::all_of(
4136 Range: llvm::drop_begin(RangeOrContainer: llvm::reverse(C&: StackComponents)),
4137 P: [](const OMPClauseMappableExprCommon::
4138 MappableComponent &MC) {
4139 return MC.getAssociatedDeclaration() ==
4140 nullptr &&
4141 (isa<ArraySectionExpr>(
4142 Val: MC.getAssociatedExpression()) ||
4143 isa<OMPArrayShapingExpr>(
4144 Val: MC.getAssociatedExpression()) ||
4145 isa<ArraySubscriptExpr>(
4146 Val: MC.getAssociatedExpression()));
4147 });
4148 })) {
4149 bool IsFirstprivate = false;
4150 // By default lambdas are captured as firstprivates.
4151 if (const auto *RD =
4152 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
4153 IsFirstprivate = RD->isLambda();
4154 IsFirstprivate =
4155 IsFirstprivate || (Stack->mustBeFirstprivate(Kind: ClauseKind) && !Res);
4156 if (IsFirstprivate) {
4157 ImpInfo.Firstprivates.insert(X: E);
4158 } else {
4159 OpenMPDefaultmapClauseModifier M =
4160 Stack->getDefaultmapModifier(Kind: ClauseKind);
4161 if (M == OMPC_DEFAULTMAP_MODIFIER_private) {
4162 ImpInfo.Privates.insert(X: E);
4163 } else {
4164 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4165 M, IsAggregateOrDeclareTarget: ClauseKind == OMPC_DEFAULTMAP_aggregate || Res,
4166 HasConstQualifier: hasConstQualifiedMappingType(T: E->getType()));
4167 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4168 }
4169 }
4170 return;
4171 }
4172 }
4173
4174 // OpenMP [2.9.3.6, Restrictions, p.2]
4175 // A list item that appears in a reduction clause of the innermost
4176 // enclosing worksharing or parallel construct may not be accessed in an
4177 // explicit task.
4178 DVar = Stack->hasInnermostDSA(
4179 D: VD,
4180 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4181 return C == OMPC_reduction && !AppliedToPointee;
4182 },
4183 DPred: [](OpenMPDirectiveKind K) {
4184 return isOpenMPParallelDirective(DKind: K) ||
4185 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4186 },
4187 /*FromParent=*/true);
4188 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4189 ErrorFound = true;
4190 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4191 reportOriginalDsa(SemaRef, Stack, D: VD, DVar);
4192 return;
4193 }
4194
4195 // Define implicit data-sharing attributes for task.
4196 DVar = Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4197 if (((isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared) ||
4198 (((Stack->getDefaultDSA() == DSA_firstprivate &&
4199 DVar.CKind == OMPC_firstprivate) ||
4200 (Stack->getDefaultDSA() == DSA_private &&
4201 DVar.CKind == OMPC_private)) &&
4202 !DVar.RefExpr)) &&
4203 !Stack->isLoopControlVariable(D: VD).first) {
4204 if (Stack->getDefaultDSA() == DSA_private)
4205 ImpInfo.Privates.insert(X: E);
4206 else
4207 ImpInfo.Firstprivates.insert(X: E);
4208 return;
4209 }
4210
4211 // Store implicitly used globals with declare target link for parent
4212 // target.
4213 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
4214 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
4215 Stack->addToParentTargetRegionLinkGlobals(E);
4216 return;
4217 }
4218 }
4219 }
4220 void VisitMemberExpr(MemberExpr *E) {
4221 if (E->isTypeDependent() || E->isValueDependent() ||
4222 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
4223 return;
4224 auto *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
4225 if (auto *TE = dyn_cast<CXXThisExpr>(Val: E->getBase()->IgnoreParenCasts())) {
4226 if (!FD)
4227 return;
4228 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: FD, /*FromParent=*/false);
4229 // Check if the variable has explicit DSA set and stop analysis if it
4230 // so.
4231 if (DVar.RefExpr || !ImplicitDeclarations.insert(V: FD).second)
4232 return;
4233
4234 if (isOpenMPTargetExecutionDirective(DKind) &&
4235 !Stack->isLoopControlVariable(D: FD).first &&
4236 !Stack->checkMappableExprComponentListsForDecl(
4237 VD: FD, /*CurrentRegionOnly=*/true,
4238 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef
4239 StackComponents,
4240 OpenMPClauseKind) {
4241 return isa<CXXThisExpr>(
4242 Val: cast<MemberExpr>(
4243 Val: StackComponents.back().getAssociatedExpression())
4244 ->getBase()
4245 ->IgnoreParens());
4246 })) {
4247 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
4248 // A bit-field cannot appear in a map clause.
4249 //
4250 if (FD->isBitField())
4251 return;
4252
4253 // Check to see if the member expression is referencing a class that
4254 // has already been explicitly mapped
4255 if (Stack->isClassPreviouslyMapped(QT: TE->getType()))
4256 return;
4257
4258 OpenMPDefaultmapClauseModifier Modifier =
4259 Stack->getDefaultmapModifier(Kind: OMPC_DEFAULTMAP_aggregate);
4260 OpenMPDefaultmapClauseKind ClauseKind =
4261 getVariableCategoryFromDecl(LO: SemaRef.getLangOpts(), VD: FD);
4262 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4263 M: Modifier, /*IsAggregateOrDeclareTarget=*/true,
4264 /*HasConstQualifier=*/false);
4265 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4266 return;
4267 }
4268
4269 SourceLocation ELoc = E->getExprLoc();
4270 // OpenMP [2.9.3.6, Restrictions, p.2]
4271 // A list item that appears in a reduction clause of the innermost
4272 // enclosing worksharing or parallel construct may not be accessed in
4273 // an explicit task.
4274 DVar = Stack->hasInnermostDSA(
4275 D: FD,
4276 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4277 return C == OMPC_reduction && !AppliedToPointee;
4278 },
4279 DPred: [](OpenMPDirectiveKind K) {
4280 return isOpenMPParallelDirective(DKind: K) ||
4281 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4282 },
4283 /*FromParent=*/true);
4284 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4285 ErrorFound = true;
4286 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4287 reportOriginalDsa(SemaRef, Stack, D: FD, DVar);
4288 return;
4289 }
4290
4291 // Define implicit data-sharing attributes for task.
4292 DVar = Stack->getImplicitDSA(D: FD, /*FromParent=*/false);
4293 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared &&
4294 !Stack->isLoopControlVariable(D: FD).first) {
4295 // Check if there is a captured expression for the current field in the
4296 // region. Do not mark it as firstprivate unless there is no captured
4297 // expression.
4298 // TODO: try to make it firstprivate.
4299 if (DVar.CKind != OMPC_unknown)
4300 ImpInfo.Firstprivates.insert(X: E);
4301 }
4302 return;
4303 }
4304 if (isOpenMPTargetExecutionDirective(DKind)) {
4305 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
4306 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, CKind: OMPC_map,
4307 DKind, /*NoDiagnose=*/true))
4308 return;
4309 const auto *VD = cast<ValueDecl>(
4310 Val: CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
4311 if (!Stack->checkMappableExprComponentListsForDecl(
4312 VD, /*CurrentRegionOnly=*/true,
4313 Check: [&CurComponents](
4314 OMPClauseMappableExprCommon::MappableExprComponentListRef
4315 StackComponents,
4316 OpenMPClauseKind) {
4317 auto CCI = CurComponents.rbegin();
4318 auto CCE = CurComponents.rend();
4319 for (const auto &SC : llvm::reverse(C&: StackComponents)) {
4320 // Do both expressions have the same kind?
4321 if (CCI->getAssociatedExpression()->getStmtClass() !=
4322 SC.getAssociatedExpression()->getStmtClass())
4323 if (!((isa<ArraySectionExpr>(
4324 Val: SC.getAssociatedExpression()) ||
4325 isa<OMPArrayShapingExpr>(
4326 Val: SC.getAssociatedExpression())) &&
4327 isa<ArraySubscriptExpr>(
4328 Val: CCI->getAssociatedExpression())))
4329 return false;
4330
4331 const Decl *CCD = CCI->getAssociatedDeclaration();
4332 const Decl *SCD = SC.getAssociatedDeclaration();
4333 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
4334 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
4335 if (SCD != CCD)
4336 return false;
4337 std::advance(i&: CCI, n: 1);
4338 if (CCI == CCE)
4339 break;
4340 }
4341 return true;
4342 })) {
4343 Visit(S: E->getBase());
4344 }
4345 } else if (!TryCaptureCXXThisMembers) {
4346 Visit(S: E->getBase());
4347 }
4348 }
4349 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
4350 for (OMPClause *C : S->clauses()) {
4351 // Skip analysis of arguments of private clauses for task|target
4352 // directives.
4353 if (isa_and_nonnull<OMPPrivateClause>(Val: C))
4354 continue;
4355 // Skip analysis of arguments of implicitly defined firstprivate clause
4356 // for task|target directives.
4357 // Skip analysis of arguments of implicitly defined map clause for target
4358 // directives.
4359 if (C && !((isa<OMPFirstprivateClause>(Val: C) || isa<OMPMapClause>(Val: C)) &&
4360 C->isImplicit() && !isOpenMPTaskingDirective(Kind: DKind))) {
4361 for (Stmt *CC : C->children()) {
4362 if (CC)
4363 Visit(S: CC);
4364 }
4365 }
4366 }
4367 // Check implicitly captured variables.
4368 VisitSubCaptures(S);
4369 }
4370
4371 void VisitOMPCanonicalLoopNestTransformationDirective(
4372 OMPCanonicalLoopNestTransformationDirective *S) {
4373 // Loop transformation directives do not introduce data sharing
4374 VisitStmt(S);
4375 }
4376
4377 void VisitCallExpr(CallExpr *S) {
4378 for (Stmt *C : S->arguments()) {
4379 if (C) {
4380 // Check implicitly captured variables in the task-based directives to
4381 // check if they must be firstprivatized.
4382 Visit(S: C);
4383 }
4384 }
4385 if (Expr *Callee = S->getCallee()) {
4386 auto *CI = Callee->IgnoreParenImpCasts();
4387 if (auto *CE = dyn_cast<MemberExpr>(Val: CI))
4388 Visit(S: CE->getBase());
4389 else if (auto *CE = dyn_cast<DeclRefExpr>(Val: CI))
4390 Visit(S: CE);
4391 }
4392 }
4393 void VisitStmt(Stmt *S) {
4394 for (Stmt *C : S->children()) {
4395 if (C) {
4396 // Check implicitly captured variables in the task-based directives to
4397 // check if they must be firstprivatized.
4398 Visit(S: C);
4399 }
4400 }
4401 }
4402
4403 void visitSubCaptures(CapturedStmt *S) {
4404 for (const CapturedStmt::Capture &Cap : S->captures()) {
4405 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
4406 continue;
4407 VarDecl *VD = Cap.getCapturedVar();
4408 // Do not try to map the variable if it or its sub-component was mapped
4409 // already.
4410 if (isOpenMPTargetExecutionDirective(DKind) &&
4411 Stack->checkMappableExprComponentListsForDecl(
4412 VD, /*CurrentRegionOnly=*/true,
4413 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
4414 OpenMPClauseKind) { return true; }))
4415 continue;
4416 DeclRefExpr *DRE = buildDeclRefExpr(
4417 S&: SemaRef, D: VD, Ty: VD->getType().getNonLValueExprType(Context: SemaRef.Context),
4418 Loc: Cap.getLocation(), /*RefersToCapture=*/true);
4419 Visit(S: DRE);
4420 }
4421 }
4422 bool isErrorFound() const { return ErrorFound; }
4423 const VariableImplicitInfo &getImplicitInfo() const { return ImpInfo; }
4424 const SemaOpenMP::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
4425 return VarsWithInheritedDSA;
4426 }
4427
4428 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
4429 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
4430 DKind = S->getCurrentDirective();
4431 // Process declare target link variables for the target directives.
4432 if (isOpenMPTargetExecutionDirective(DKind)) {
4433 for (DeclRefExpr *E : Stack->getLinkGlobals())
4434 Visit(S: E);
4435 }
4436 }
4437};
4438} // namespace
4439
4440static void handleDeclareVariantConstructTrait(DSAStackTy *Stack,
4441 OpenMPDirectiveKind DKind,
4442 bool ScopeEntry) {
4443 SmallVector<llvm::omp::TraitProperty, 8> Traits;
4444 if (isOpenMPTargetExecutionDirective(DKind))
4445 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_target_target);
4446 if (isOpenMPTeamsDirective(DKind))
4447 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_teams_teams);
4448 if (isOpenMPParallelDirective(DKind))
4449 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_parallel_parallel);
4450 if (isOpenMPWorksharingDirective(DKind))
4451 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_for_for);
4452 if (isOpenMPSimdDirective(DKind))
4453 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_simd_simd);
4454 Stack->handleConstructTrait(Traits, ScopeEntry);
4455}
4456
4457static SmallVector<SemaOpenMP::CapturedParamNameType>
4458getParallelRegionParams(Sema &SemaRef, bool LoopBoundSharing) {
4459 ASTContext &Context = SemaRef.getASTContext();
4460 QualType KmpInt32Ty =
4461 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4462 QualType KmpInt32PtrTy =
4463 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4464 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4465 std::make_pair(x: ".global_tid.", y&: KmpInt32PtrTy),
4466 std::make_pair(x: ".bound_tid.", y&: KmpInt32PtrTy),
4467 };
4468 if (LoopBoundSharing) {
4469 QualType KmpSizeTy = Context.getSizeType().withConst();
4470 Params.push_back(Elt: std::make_pair(x: ".previous.lb.", y&: KmpSizeTy));
4471 Params.push_back(Elt: std::make_pair(x: ".previous.ub.", y&: KmpSizeTy));
4472 }
4473
4474 // __context with shared vars
4475 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4476 return Params;
4477}
4478
4479static SmallVector<SemaOpenMP::CapturedParamNameType>
4480getTeamsRegionParams(Sema &SemaRef) {
4481 return getParallelRegionParams(SemaRef, /*LoopBoundSharing=*/false);
4482}
4483
4484static SmallVector<SemaOpenMP::CapturedParamNameType>
4485getTaskRegionParams(Sema &SemaRef) {
4486 ASTContext &Context = SemaRef.getASTContext();
4487 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(DestWidth: 32, Signed: 1).withConst();
4488 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4489 QualType KmpInt32PtrTy =
4490 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4491 QualType Args[] = {VoidPtrTy};
4492 FunctionProtoType::ExtProtoInfo EPI;
4493 EPI.Variadic = true;
4494 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4495 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4496 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4497 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4498 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4499 std::make_pair(
4500 x: ".copy_fn.",
4501 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4502 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4503 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4504 };
4505 return Params;
4506}
4507
4508static SmallVector<SemaOpenMP::CapturedParamNameType>
4509getTargetRegionParams(Sema &SemaRef) {
4510 ASTContext &Context = SemaRef.getASTContext();
4511 SmallVector<SemaOpenMP::CapturedParamNameType> Params;
4512 // __context with shared vars
4513 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4514 // Implicit dyn_ptr argument, appended as the last parameter. Present on both
4515 // host and device so argument counts match without runtime manipulation.
4516 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4517 Params.push_back(Elt: std::make_pair(x: StringRef("dyn_ptr"), y&: VoidPtrTy));
4518 return Params;
4519}
4520
4521static SmallVector<SemaOpenMP::CapturedParamNameType>
4522getUnknownRegionParams(Sema &SemaRef) {
4523 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4524 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4525 };
4526 return Params;
4527}
4528
4529static SmallVector<SemaOpenMP::CapturedParamNameType>
4530getTaskloopRegionParams(Sema &SemaRef) {
4531 ASTContext &Context = SemaRef.getASTContext();
4532 QualType KmpInt32Ty =
4533 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4534 QualType KmpUInt64Ty =
4535 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0).withConst();
4536 QualType KmpInt64Ty =
4537 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1).withConst();
4538 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4539 QualType KmpInt32PtrTy =
4540 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4541 QualType Args[] = {VoidPtrTy};
4542 FunctionProtoType::ExtProtoInfo EPI;
4543 EPI.Variadic = true;
4544 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4545 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4546 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4547 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4548 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4549 std::make_pair(
4550 x: ".copy_fn.",
4551 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4552 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4553 std::make_pair(x: ".lb.", y&: KmpUInt64Ty),
4554 std::make_pair(x: ".ub.", y&: KmpUInt64Ty),
4555 std::make_pair(x: ".st.", y&: KmpInt64Ty),
4556 std::make_pair(x: ".liter.", y&: KmpInt32Ty),
4557 std::make_pair(x: ".reductions.", y&: VoidPtrTy),
4558 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4559 };
4560 return Params;
4561}
4562
4563static void processCapturedRegions(Sema &SemaRef, OpenMPDirectiveKind DKind,
4564 Scope *CurScope, SourceLocation Loc) {
4565 SmallVector<OpenMPDirectiveKind> Regions;
4566 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind);
4567
4568 bool LoopBoundSharing = isOpenMPLoopBoundSharingDirective(Kind: DKind);
4569
4570 auto MarkAsInlined = [&](CapturedRegionScopeInfo *CSI) {
4571 CSI->TheCapturedDecl->addAttr(A: AlwaysInlineAttr::CreateImplicit(
4572 Ctx&: SemaRef.getASTContext(), Range: {}, S: AlwaysInlineAttr::Keyword_forceinline));
4573 };
4574
4575 for (auto [Level, RKind] : llvm::enumerate(First&: Regions)) {
4576 switch (RKind) {
4577 // All region kinds that can be returned from `getOpenMPCaptureRegions`
4578 // are listed here.
4579 case OMPD_parallel:
4580 SemaRef.ActOnCapturedRegionStart(
4581 Loc, CurScope, Kind: CR_OpenMP,
4582 Params: getParallelRegionParams(SemaRef, LoopBoundSharing), OpenMPCaptureLevel: Level);
4583 break;
4584 case OMPD_teams:
4585 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4586 Params: getTeamsRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4587 break;
4588 case OMPD_task:
4589 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4590 Params: getTaskRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4591 // Mark this captured region as inlined, because we don't use outlined
4592 // function directly.
4593 MarkAsInlined(SemaRef.getCurCapturedRegion());
4594 break;
4595 case OMPD_taskloop:
4596 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4597 Params: getTaskloopRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4598 // Mark this captured region as inlined, because we don't use outlined
4599 // function directly.
4600 MarkAsInlined(SemaRef.getCurCapturedRegion());
4601 break;
4602 case OMPD_target:
4603 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4604 Params: getTargetRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4605 break;
4606 case OMPD_unknown:
4607 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4608 Params: getUnknownRegionParams(SemaRef));
4609 break;
4610 case OMPD_metadirective:
4611 case OMPD_nothing:
4612 default:
4613 llvm_unreachable("Unexpected capture region");
4614 }
4615 }
4616}
4617
4618void SemaOpenMP::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind,
4619 Scope *CurScope) {
4620 switch (DKind) {
4621 case OMPD_atomic:
4622 case OMPD_critical:
4623 case OMPD_masked:
4624 case OMPD_master:
4625 case OMPD_section:
4626 case OMPD_tile:
4627 case OMPD_stripe:
4628 case OMPD_unroll:
4629 case OMPD_reverse:
4630 case OMPD_interchange:
4631 case OMPD_fuse:
4632 case OMPD_assume:
4633 break;
4634 default:
4635 processCapturedRegions(SemaRef, DKind, CurScope,
4636 DSAStack->getConstructLoc());
4637 break;
4638 }
4639
4640 DSAStack->setContext(SemaRef.CurContext);
4641 handleDeclareVariantConstructTrait(DSAStack, DKind, /*ScopeEntry=*/true);
4642}
4643
4644int SemaOpenMP::getNumberOfConstructScopes(unsigned Level) const {
4645 return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4646}
4647
4648int SemaOpenMP::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4649 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4650 getOpenMPCaptureRegions(CaptureRegions, DKind);
4651 return CaptureRegions.size();
4652}
4653
4654static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4655 Expr *CaptureExpr, bool WithInit,
4656 DeclContext *CurContext,
4657 bool AsExpression) {
4658 assert(CaptureExpr);
4659 ASTContext &C = S.getASTContext();
4660 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
4661 QualType Ty = Init->getType();
4662 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
4663 if (S.getLangOpts().CPlusPlus) {
4664 Ty = C.getLValueReferenceType(T: Ty);
4665 } else {
4666 Ty = C.getPointerType(T: Ty);
4667 ExprResult Res =
4668 S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_AddrOf, InputExpr: Init);
4669 if (!Res.isUsable())
4670 return nullptr;
4671 Init = Res.get();
4672 }
4673 WithInit = true;
4674 }
4675 auto *CED = OMPCapturedExprDecl::Create(C, DC: CurContext, Id, T: Ty,
4676 StartLoc: CaptureExpr->getBeginLoc());
4677 if (!WithInit)
4678 CED->addAttr(A: OMPCaptureNoInitAttr::CreateImplicit(Ctx&: C));
4679 CurContext->addHiddenDecl(D: CED);
4680 Sema::TentativeAnalysisScope Trap(S);
4681 S.AddInitializerToDecl(dcl: CED, init: Init, /*DirectInit=*/false);
4682 return CED;
4683}
4684
4685static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4686 bool WithInit) {
4687 OMPCapturedExprDecl *CD;
4688 if (VarDecl *VD = S.OpenMP().isOpenMPCapturedDecl(D))
4689 CD = cast<OMPCapturedExprDecl>(Val: VD);
4690 else
4691 CD = buildCaptureDecl(S, Id: D->getIdentifier(), CaptureExpr, WithInit,
4692 CurContext: S.CurContext,
4693 /*AsExpression=*/false);
4694 return buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
4695 Loc: CaptureExpr->getExprLoc());
4696}
4697
4698static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref,
4699 StringRef Name) {
4700 CaptureExpr = S.DefaultLvalueConversion(E: CaptureExpr).get();
4701 if (!Ref) {
4702 OMPCapturedExprDecl *CD = buildCaptureDecl(
4703 S, Id: &S.getASTContext().Idents.get(Name), CaptureExpr,
4704 /*WithInit=*/true, CurContext: S.CurContext, /*AsExpression=*/true);
4705 Ref = buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
4706 Loc: CaptureExpr->getExprLoc());
4707 }
4708 ExprResult Res = Ref;
4709 if (!S.getLangOpts().CPlusPlus &&
4710 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
4711 Ref->getType()->isPointerType()) {
4712 Res = S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_Deref, InputExpr: Ref);
4713 if (!Res.isUsable())
4714 return ExprError();
4715 }
4716 return S.DefaultLvalueConversion(E: Res.get());
4717}
4718
4719namespace {
4720// OpenMP directives parsed in this section are represented as a
4721// CapturedStatement with an associated statement. If a syntax error
4722// is detected during the parsing of the associated statement, the
4723// compiler must abort processing and close the CapturedStatement.
4724//
4725// Combined directives such as 'target parallel' have more than one
4726// nested CapturedStatements. This RAII ensures that we unwind out
4727// of all the nested CapturedStatements when an error is found.
4728class CaptureRegionUnwinderRAII {
4729private:
4730 Sema &S;
4731 bool &ErrorFound;
4732 OpenMPDirectiveKind DKind = OMPD_unknown;
4733
4734public:
4735 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4736 OpenMPDirectiveKind DKind)
4737 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4738 ~CaptureRegionUnwinderRAII() {
4739 if (ErrorFound) {
4740 int ThisCaptureLevel = S.OpenMP().getOpenMPCaptureLevels(DKind);
4741 while (--ThisCaptureLevel >= 0)
4742 S.ActOnCapturedRegionError();
4743 }
4744 }
4745};
4746} // namespace
4747
4748void SemaOpenMP::tryCaptureOpenMPLambdas(ValueDecl *V) {
4749 // Capture variables captured by reference in lambdas for target-based
4750 // directives.
4751 if (!SemaRef.CurContext->isDependentContext() &&
4752 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4753 isOpenMPTargetDataManagementDirective(
4754 DSAStack->getCurrentDirective()))) {
4755 QualType Type = V->getType();
4756 if (const auto *RD = Type.getCanonicalType()
4757 .getNonReferenceType()
4758 ->getAsCXXRecordDecl()) {
4759 bool SavedForceCaptureByReferenceInTargetExecutable =
4760 DSAStack->isForceCaptureByReferenceInTargetExecutable();
4761 DSAStack->setForceCaptureByReferenceInTargetExecutable(
4762 /*V=*/true);
4763 if (RD->isLambda()) {
4764 llvm::DenseMap<const ValueDecl *, FieldDecl *> Captures;
4765 FieldDecl *ThisCapture;
4766 RD->getCaptureFields(Captures, ThisCapture);
4767 for (const LambdaCapture &LC : RD->captures()) {
4768 if (LC.getCaptureKind() == LCK_ByRef) {
4769 VarDecl *VD = cast<VarDecl>(Val: LC.getCapturedVar());
4770 DeclContext *VDC = VD->getDeclContext();
4771 if (!VDC->Encloses(DC: SemaRef.CurContext))
4772 continue;
4773 SemaRef.MarkVariableReferenced(Loc: LC.getLocation(), Var: VD);
4774 } else if (LC.getCaptureKind() == LCK_This) {
4775 QualType ThisTy = SemaRef.getCurrentThisType();
4776 if (!ThisTy.isNull() && getASTContext().typesAreCompatible(
4777 T1: ThisTy, T2: ThisCapture->getType()))
4778 SemaRef.CheckCXXThisCapture(Loc: LC.getLocation());
4779 }
4780 }
4781 }
4782 DSAStack->setForceCaptureByReferenceInTargetExecutable(
4783 SavedForceCaptureByReferenceInTargetExecutable);
4784 }
4785 }
4786}
4787
4788static bool checkOrderedOrderSpecified(Sema &S,
4789 const ArrayRef<OMPClause *> Clauses) {
4790 const OMPOrderedClause *Ordered = nullptr;
4791 const OMPOrderClause *Order = nullptr;
4792
4793 for (const OMPClause *Clause : Clauses) {
4794 if (Clause->getClauseKind() == OMPC_ordered)
4795 Ordered = cast<OMPOrderedClause>(Val: Clause);
4796 else if (Clause->getClauseKind() == OMPC_order) {
4797 Order = cast<OMPOrderClause>(Val: Clause);
4798 if (Order->getKind() != OMPC_ORDER_concurrent)
4799 Order = nullptr;
4800 }
4801 if (Ordered && Order)
4802 break;
4803 }
4804
4805 if (Ordered && Order) {
4806 S.Diag(Loc: Order->getKindKwLoc(),
4807 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
4808 << getOpenMPClauseNameForDiag(C: OMPC_order)
4809 << getOpenMPSimpleClauseTypeName(Kind: OMPC_order, Type: OMPC_ORDER_concurrent)
4810 << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4811 S.Diag(Loc: Ordered->getBeginLoc(), DiagID: diag::note_omp_ordered_param)
4812 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4813 return true;
4814 }
4815 return false;
4816}
4817
4818StmtResult SemaOpenMP::ActOnOpenMPRegionEnd(StmtResult S,
4819 ArrayRef<OMPClause *> Clauses) {
4820 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
4821 /*ScopeEntry=*/false);
4822 if (!isOpenMPCapturingDirective(DSAStack->getCurrentDirective()))
4823 return S;
4824
4825 bool ErrorFound = false;
4826 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4827 SemaRef, ErrorFound, DSAStack->getCurrentDirective());
4828 if (!S.isUsable()) {
4829 ErrorFound = true;
4830 return StmtError();
4831 }
4832
4833 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4834 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4835 OMPOrderedClause *OC = nullptr;
4836 OMPScheduleClause *SC = nullptr;
4837 SmallVector<const OMPLinearClause *, 4> LCs;
4838 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4839 // This is required for proper codegen.
4840 for (OMPClause *Clause : Clauses) {
4841 if (!getLangOpts().OpenMPSimd &&
4842 (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) ||
4843 DSAStack->getCurrentDirective() == OMPD_target) &&
4844 Clause->getClauseKind() == OMPC_in_reduction) {
4845 // Capture taskgroup task_reduction descriptors inside the tasking regions
4846 // with the corresponding in_reduction items.
4847 auto *IRC = cast<OMPInReductionClause>(Val: Clause);
4848 for (Expr *E : IRC->taskgroup_descriptors())
4849 if (E)
4850 SemaRef.MarkDeclarationsReferencedInExpr(E);
4851 }
4852 if (isOpenMPPrivate(Kind: Clause->getClauseKind()) ||
4853 Clause->getClauseKind() == OMPC_copyprivate ||
4854 (getLangOpts().OpenMPUseTLS &&
4855 getASTContext().getTargetInfo().isTLSSupported() &&
4856 Clause->getClauseKind() == OMPC_copyin)) {
4857 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4858 // Mark all variables in private list clauses as used in inner region.
4859 for (Stmt *VarRef : Clause->children()) {
4860 if (auto *E = cast_or_null<Expr>(Val: VarRef)) {
4861 SemaRef.MarkDeclarationsReferencedInExpr(E);
4862 }
4863 }
4864 DSAStack->setForceVarCapturing(/*V=*/false);
4865 } else if (CaptureRegions.size() > 1 ||
4866 CaptureRegions.back() != OMPD_unknown) {
4867 if (auto *C = OMPClauseWithPreInit::get(C: Clause))
4868 PICs.push_back(Elt: C);
4869 if (auto *C = OMPClauseWithPostUpdate::get(C: Clause)) {
4870 if (Expr *E = C->getPostUpdateExpr())
4871 SemaRef.MarkDeclarationsReferencedInExpr(E);
4872 }
4873 }
4874 if (Clause->getClauseKind() == OMPC_schedule)
4875 SC = cast<OMPScheduleClause>(Val: Clause);
4876 else if (Clause->getClauseKind() == OMPC_ordered)
4877 OC = cast<OMPOrderedClause>(Val: Clause);
4878 else if (Clause->getClauseKind() == OMPC_linear)
4879 LCs.push_back(Elt: cast<OMPLinearClause>(Val: Clause));
4880 }
4881 // Capture allocator expressions if used.
4882 for (Expr *E : DSAStack->getInnerAllocators())
4883 SemaRef.MarkDeclarationsReferencedInExpr(E);
4884 // OpenMP, 2.7.1 Loop Construct, Restrictions
4885 // The nonmonotonic modifier cannot be specified if an ordered clause is
4886 // specified.
4887 if (SC &&
4888 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
4889 SC->getSecondScheduleModifier() ==
4890 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4891 OC) {
4892 Diag(Loc: SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4893 ? SC->getFirstScheduleModifierLoc()
4894 : SC->getSecondScheduleModifierLoc(),
4895 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
4896 << getOpenMPClauseNameForDiag(C: OMPC_schedule)
4897 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule,
4898 Type: OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4899 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4900 ErrorFound = true;
4901 }
4902 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4903 // If an order(concurrent) clause is present, an ordered clause may not appear
4904 // on the same directive.
4905 if (checkOrderedOrderSpecified(S&: SemaRef, Clauses))
4906 ErrorFound = true;
4907 if (!LCs.empty() && OC && OC->getNumForLoops()) {
4908 for (const OMPLinearClause *C : LCs) {
4909 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_linear_ordered)
4910 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4911 }
4912 ErrorFound = true;
4913 }
4914 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4915 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
4916 OC->getNumForLoops()) {
4917 unsigned OMPVersion = getLangOpts().OpenMP;
4918 Diag(Loc: OC->getBeginLoc(), DiagID: diag::err_omp_ordered_simd)
4919 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(), Ver: OMPVersion);
4920 ErrorFound = true;
4921 }
4922 if (ErrorFound) {
4923 return StmtError();
4924 }
4925 StmtResult SR = S;
4926 unsigned CompletedRegions = 0;
4927 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(C&: CaptureRegions)) {
4928 // Mark all variables in private list clauses as used in inner region.
4929 // Required for proper codegen of combined directives.
4930 // TODO: add processing for other clauses.
4931 if (ThisCaptureRegion != OMPD_unknown) {
4932 for (const clang::OMPClauseWithPreInit *C : PICs) {
4933 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4934 // Find the particular capture region for the clause if the
4935 // directive is a combined one with multiple capture regions.
4936 // If the directive is not a combined one, the capture region
4937 // associated with the clause is OMPD_unknown and is generated
4938 // only once.
4939 if (CaptureRegion == ThisCaptureRegion ||
4940 CaptureRegion == OMPD_unknown) {
4941 if (auto *DS = cast_or_null<DeclStmt>(Val: C->getPreInitStmt())) {
4942 for (Decl *D : DS->decls())
4943 SemaRef.MarkVariableReferenced(Loc: D->getLocation(),
4944 Var: cast<VarDecl>(Val: D));
4945 }
4946 }
4947 }
4948 }
4949 if (ThisCaptureRegion == OMPD_target) {
4950 // Capture allocator traits in the target region. They are used implicitly
4951 // and, thus, are not captured by default.
4952 for (OMPClause *C : Clauses) {
4953 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
4954 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4955 ++I) {
4956 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4957 if (Expr *E = D.AllocatorTraits)
4958 SemaRef.MarkDeclarationsReferencedInExpr(E);
4959 }
4960 continue;
4961 }
4962 }
4963 }
4964 if (ThisCaptureRegion == OMPD_parallel) {
4965 // Capture temp arrays for inscan reductions and locals in aligned
4966 // clauses.
4967 for (OMPClause *C : Clauses) {
4968 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C)) {
4969 if (RC->getModifier() != OMPC_REDUCTION_inscan)
4970 continue;
4971 for (Expr *E : RC->copy_array_temps())
4972 if (E)
4973 SemaRef.MarkDeclarationsReferencedInExpr(E);
4974 }
4975 if (auto *AC = dyn_cast<OMPAlignedClause>(Val: C)) {
4976 for (Expr *E : AC->varlist())
4977 SemaRef.MarkDeclarationsReferencedInExpr(E);
4978 }
4979 }
4980 }
4981 if (++CompletedRegions == CaptureRegions.size())
4982 DSAStack->setBodyComplete();
4983 SR = SemaRef.ActOnCapturedRegionEnd(S: SR.get());
4984 }
4985 return SR;
4986}
4987
4988static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4989 OpenMPDirectiveKind CancelRegion,
4990 SourceLocation StartLoc) {
4991 // CancelRegion is only needed for cancel and cancellation_point.
4992 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
4993 return false;
4994
4995 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
4996 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
4997 return false;
4998
4999 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
5000 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_wrong_cancel_region)
5001 << getOpenMPDirectiveName(D: CancelRegion, Ver: OMPVersion);
5002 return true;
5003}
5004
5005static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
5006 OpenMPDirectiveKind CurrentRegion,
5007 const DeclarationNameInfo &CurrentName,
5008 OpenMPDirectiveKind CancelRegion,
5009 OpenMPBindClauseKind BindKind,
5010 SourceLocation StartLoc) {
5011 if (!Stack->getCurScope())
5012 return false;
5013
5014 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
5015 OpenMPDirectiveKind OffendingRegion = ParentRegion;
5016 bool NestingProhibited = false;
5017 bool CloseNesting = true;
5018 bool OrphanSeen = false;
5019 enum {
5020 NoRecommend,
5021 ShouldBeInParallelRegion,
5022 ShouldBeInOrderedRegion,
5023 ShouldBeInTargetRegion,
5024 ShouldBeInTeamsRegion,
5025 ShouldBeInLoopSimdRegion,
5026 } Recommend = NoRecommend;
5027
5028 SmallVector<OpenMPDirectiveKind, 4> LeafOrComposite;
5029 ArrayRef<OpenMPDirectiveKind> ParentLOC =
5030 getLeafOrCompositeConstructs(D: ParentRegion, Output&: LeafOrComposite);
5031 OpenMPDirectiveKind EnclosingConstruct = ParentLOC.back();
5032 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
5033
5034 if (OMPVersion >= 50 && Stack->isParentOrderConcurrent() &&
5035 !isOpenMPOrderConcurrentNestableDirective(DKind: CurrentRegion,
5036 LangOpts: SemaRef.LangOpts)) {
5037 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_order)
5038 << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5039 return true;
5040 }
5041 if (isOpenMPSimdDirective(DKind: ParentRegion) &&
5042 ((OMPVersion <= 45 && CurrentRegion != OMPD_ordered) ||
5043 (OMPVersion >= 50 && CurrentRegion != OMPD_ordered &&
5044 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic &&
5045 CurrentRegion != OMPD_scan))) {
5046 // OpenMP [2.16, Nesting of Regions]
5047 // OpenMP constructs may not be nested inside a simd region.
5048 // OpenMP [2.8.1,simd Construct, Restrictions]
5049 // An ordered construct with the simd clause is the only OpenMP
5050 // construct that can appear in the simd region.
5051 // Allowing a SIMD construct nested in another SIMD construct is an
5052 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
5053 // message.
5054 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
5055 // The only OpenMP constructs that can be encountered during execution of
5056 // a simd region are the atomic construct, the loop construct, the simd
5057 // construct and the ordered construct with the simd clause.
5058 SemaRef.Diag(Loc: StartLoc, DiagID: (CurrentRegion != OMPD_simd)
5059 ? diag::err_omp_prohibited_region_simd
5060 : diag::warn_omp_nesting_simd)
5061 << (OMPVersion >= 50 ? 1 : 0);
5062 return CurrentRegion != OMPD_simd;
5063 }
5064 if (EnclosingConstruct == OMPD_atomic) {
5065 // OpenMP [2.16, Nesting of Regions]
5066 // OpenMP constructs may not be nested inside an atomic region.
5067 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_atomic);
5068 return true;
5069 }
5070 if (CurrentRegion == OMPD_section) {
5071 // OpenMP [2.7.2, sections Construct, Restrictions]
5072 // Orphaned section directives are prohibited. That is, the section
5073 // directives must appear within the sections construct and must not be
5074 // encountered elsewhere in the sections region.
5075 if (EnclosingConstruct != OMPD_sections) {
5076 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_section_directive)
5077 << (ParentRegion != OMPD_unknown)
5078 << getOpenMPDirectiveName(D: ParentRegion, Ver: OMPVersion);
5079 return true;
5080 }
5081 return false;
5082 }
5083 // Allow some constructs (except teams and cancellation constructs) to be
5084 // orphaned (they could be used in functions, called from OpenMP regions
5085 // with the required preconditions).
5086 if (ParentRegion == OMPD_unknown &&
5087 !isOpenMPNestingTeamsDirective(DKind: CurrentRegion) &&
5088 CurrentRegion != OMPD_cancellation_point &&
5089 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
5090 return false;
5091 // Checks needed for mapping "loop" construct. Please check mapLoopConstruct
5092 // for a detailed explanation
5093 if (OMPVersion >= 50 && CurrentRegion == OMPD_loop &&
5094 (BindKind == OMPC_BIND_parallel || BindKind == OMPC_BIND_teams) &&
5095 (isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5096 EnclosingConstruct == OMPD_loop)) {
5097 int ErrorMsgNumber = (BindKind == OMPC_BIND_parallel) ? 1 : 4;
5098 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5099 << true << getOpenMPDirectiveName(D: ParentRegion, Ver: OMPVersion)
5100 << ErrorMsgNumber << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5101 return true;
5102 }
5103 if (CurrentRegion == OMPD_cancellation_point ||
5104 CurrentRegion == OMPD_cancel) {
5105 // OpenMP [2.16, Nesting of Regions]
5106 // A cancellation point construct for which construct-type-clause is
5107 // taskgroup must be nested inside a task construct. A cancellation
5108 // point construct for which construct-type-clause is not taskgroup must
5109 // be closely nested inside an OpenMP construct that matches the type
5110 // specified in construct-type-clause.
5111 // A cancel construct for which construct-type-clause is taskgroup must be
5112 // nested inside a task construct. A cancel construct for which
5113 // construct-type-clause is not taskgroup must be closely nested inside an
5114 // OpenMP construct that matches the type specified in
5115 // construct-type-clause.
5116 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: ParentRegion);
5117 if (CancelRegion == OMPD_taskgroup) {
5118 NestingProhibited =
5119 EnclosingConstruct != OMPD_task &&
5120 (OMPVersion < 50 || EnclosingConstruct != OMPD_taskloop);
5121 } else if (CancelRegion == OMPD_sections) {
5122 NestingProhibited = EnclosingConstruct != OMPD_section &&
5123 EnclosingConstruct != OMPD_sections;
5124 } else {
5125 NestingProhibited = CancelRegion != Leafs.back();
5126 }
5127 OrphanSeen = ParentRegion == OMPD_unknown;
5128 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) {
5129 // OpenMP 5.1 [2.22, Nesting of Regions]
5130 // A masked region may not be closely nested inside a worksharing, loop,
5131 // atomic, task, or taskloop region.
5132 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5133 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5134 isOpenMPTaskingDirective(Kind: ParentRegion);
5135 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
5136 // OpenMP [2.16, Nesting of Regions]
5137 // A critical region may not be nested (closely or otherwise) inside a
5138 // critical region with the same name. Note that this restriction is not
5139 // sufficient to prevent deadlock.
5140 SourceLocation PreviousCriticalLoc;
5141 bool DeadLock = Stack->hasDirective(
5142 DPred: [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
5143 const DeclarationNameInfo &DNI,
5144 SourceLocation Loc) {
5145 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
5146 PreviousCriticalLoc = Loc;
5147 return true;
5148 }
5149 return false;
5150 },
5151 FromParent: false /* skip top directive */);
5152 if (DeadLock) {
5153 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_critical_same_name)
5154 << CurrentName.getName();
5155 if (PreviousCriticalLoc.isValid())
5156 SemaRef.Diag(Loc: PreviousCriticalLoc,
5157 DiagID: diag::note_omp_previous_critical_region);
5158 return true;
5159 }
5160 } else if (CurrentRegion == OMPD_barrier || CurrentRegion == OMPD_scope) {
5161 // OpenMP 5.1 [2.22, Nesting of Regions]
5162 // A scope region may not be closely nested inside a worksharing, loop,
5163 // task, taskloop, critical, ordered, atomic, or masked region.
5164 // OpenMP 5.1 [2.22, Nesting of Regions]
5165 // A barrier region may not be closely nested inside a worksharing, loop,
5166 // task, taskloop, critical, ordered, atomic, or masked region.
5167 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5168 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5169 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5170 llvm::is_contained(Set: {OMPD_masked, OMPD_master,
5171 OMPD_critical, OMPD_ordered},
5172 Element: EnclosingConstruct);
5173 } else if (isOpenMPWorksharingDirective(DKind: CurrentRegion) &&
5174 !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5175 !isOpenMPTeamsDirective(DKind: CurrentRegion)) {
5176 // OpenMP 5.1 [2.22, Nesting of Regions]
5177 // A loop region that binds to a parallel region or a worksharing region
5178 // may not be closely nested inside a worksharing, loop, task, taskloop,
5179 // critical, ordered, atomic, or masked region.
5180 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5181 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5182 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5183 llvm::is_contained(Set: {OMPD_masked, OMPD_master,
5184 OMPD_critical, OMPD_ordered},
5185 Element: EnclosingConstruct);
5186 Recommend = ShouldBeInParallelRegion;
5187 } else if (CurrentRegion == OMPD_ordered) {
5188 // OpenMP [2.16, Nesting of Regions]
5189 // An ordered region may not be closely nested inside a critical,
5190 // atomic, or explicit task region.
5191 // An ordered region must be closely nested inside a loop region (or
5192 // parallel loop region) with an ordered clause.
5193 // OpenMP [2.8.1,simd Construct, Restrictions]
5194 // An ordered construct with the simd clause is the only OpenMP construct
5195 // that can appear in the simd region.
5196 NestingProhibited = EnclosingConstruct == OMPD_critical ||
5197 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5198 !(isOpenMPSimdDirective(DKind: ParentRegion) ||
5199 Stack->isParentOrderedRegion());
5200 Recommend = ShouldBeInOrderedRegion;
5201 } else if (isOpenMPNestingTeamsDirective(DKind: CurrentRegion)) {
5202 // OpenMP [2.16, Nesting of Regions]
5203 // If specified, a teams construct must be contained within a target
5204 // construct.
5205 NestingProhibited =
5206 (OMPVersion <= 45 && EnclosingConstruct != OMPD_target) ||
5207 (OMPVersion >= 50 && EnclosingConstruct != OMPD_unknown &&
5208 EnclosingConstruct != OMPD_target);
5209 OrphanSeen = ParentRegion == OMPD_unknown;
5210 Recommend = ShouldBeInTargetRegion;
5211 } else if (CurrentRegion == OMPD_scan) {
5212 if (OMPVersion >= 50) {
5213 // OpenMP spec 5.0 and 5.1 require scan to be directly enclosed by for,
5214 // simd, or for simd. This has to take into account combined directives.
5215 // In 5.2 this seems to be implied by the fact that the specified
5216 // separated constructs are do, for, and simd.
5217 NestingProhibited = !llvm::is_contained(
5218 Set: {OMPD_for, OMPD_simd, OMPD_for_simd}, Element: EnclosingConstruct);
5219 } else {
5220 NestingProhibited = true;
5221 }
5222 OrphanSeen = ParentRegion == OMPD_unknown;
5223 Recommend = ShouldBeInLoopSimdRegion;
5224 }
5225 if (!NestingProhibited && !isOpenMPTargetExecutionDirective(DKind: CurrentRegion) &&
5226 !isOpenMPTargetDataManagementDirective(DKind: CurrentRegion) &&
5227 EnclosingConstruct == OMPD_teams) {
5228 // OpenMP [5.1, 2.22, Nesting of Regions]
5229 // distribute, distribute simd, distribute parallel worksharing-loop,
5230 // distribute parallel worksharing-loop SIMD, loop, parallel regions,
5231 // including any parallel regions arising from combined constructs,
5232 // omp_get_num_teams() regions, and omp_get_team_num() regions are the
5233 // only OpenMP regions that may be strictly nested inside the teams
5234 // region.
5235 //
5236 // As an extension, we permit atomic within teams as well.
5237 NestingProhibited = !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5238 !isOpenMPDistributeDirective(DKind: CurrentRegion) &&
5239 CurrentRegion != OMPD_loop &&
5240 !(SemaRef.getLangOpts().OpenMPExtensions &&
5241 CurrentRegion == OMPD_atomic);
5242 Recommend = ShouldBeInParallelRegion;
5243 }
5244 if (!NestingProhibited && CurrentRegion == OMPD_loop) {
5245 // OpenMP [5.1, 2.11.7, loop Construct, Restrictions]
5246 // If the bind clause is present on the loop construct and binding is
5247 // teams then the corresponding loop region must be strictly nested inside
5248 // a teams region.
5249 NestingProhibited =
5250 BindKind == OMPC_BIND_teams && EnclosingConstruct != OMPD_teams;
5251 Recommend = ShouldBeInTeamsRegion;
5252 }
5253 if (!NestingProhibited && isOpenMPNestingDistributeDirective(DKind: CurrentRegion)) {
5254 // OpenMP 4.5 [2.17 Nesting of Regions]
5255 // The region associated with the distribute construct must be strictly
5256 // nested inside a teams region
5257 NestingProhibited = EnclosingConstruct != OMPD_teams;
5258 Recommend = ShouldBeInTeamsRegion;
5259 }
5260 if (!NestingProhibited &&
5261 (isOpenMPTargetExecutionDirective(DKind: CurrentRegion) ||
5262 isOpenMPTargetDataManagementDirective(DKind: CurrentRegion))) {
5263 // OpenMP 4.5 [2.17 Nesting of Regions]
5264 // If a target, target update, target data, target enter data, or
5265 // target exit data construct is encountered during execution of a
5266 // target region, the behavior is unspecified.
5267 NestingProhibited = Stack->hasDirective(
5268 DPred: [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
5269 SourceLocation) {
5270 if (isOpenMPTargetExecutionDirective(DKind: K)) {
5271 OffendingRegion = K;
5272 return true;
5273 }
5274 return false;
5275 },
5276 FromParent: false /* don't skip top directive */);
5277 CloseNesting = false;
5278 }
5279 if (NestingProhibited) {
5280 if (OrphanSeen) {
5281 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
5282 << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion) << Recommend;
5283 } else {
5284 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5285 << CloseNesting << getOpenMPDirectiveName(D: OffendingRegion, Ver: OMPVersion)
5286 << Recommend << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5287 }
5288 return true;
5289 }
5290 return false;
5291}
5292
5293struct Kind2Unsigned {
5294 using argument_type = OpenMPDirectiveKind;
5295 unsigned operator()(argument_type DK) { return unsigned(DK); }
5296};
5297static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
5298 ArrayRef<OMPClause *> Clauses,
5299 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
5300 bool ErrorFound = false;
5301 unsigned NamedModifiersNumber = 0;
5302 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
5303 FoundNameModifiers.resize(S: llvm::omp::Directive_enumSize + 1);
5304 SmallVector<SourceLocation, 4> NameModifierLoc;
5305 unsigned OMPVersion = S.getLangOpts().OpenMP;
5306 for (const OMPClause *C : Clauses) {
5307 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(Val: C)) {
5308 // At most one if clause without a directive-name-modifier can appear on
5309 // the directive.
5310 OpenMPDirectiveKind CurNM = IC->getNameModifier();
5311 auto &FNM = FoundNameModifiers[CurNM];
5312 if (FNM) {
5313 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
5314 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion)
5315 << getOpenMPClauseNameForDiag(C: OMPC_if) << (CurNM != OMPD_unknown)
5316 << getOpenMPDirectiveName(D: CurNM, Ver: OMPVersion);
5317 ErrorFound = true;
5318 } else if (CurNM != OMPD_unknown) {
5319 NameModifierLoc.push_back(Elt: IC->getNameModifierLoc());
5320 ++NamedModifiersNumber;
5321 }
5322 FNM = IC;
5323 if (CurNM == OMPD_unknown)
5324 continue;
5325 // Check if the specified name modifier is allowed for the current
5326 // directive.
5327 // At most one if clause with the particular directive-name-modifier can
5328 // appear on the directive.
5329 if (!llvm::is_contained(Range&: AllowedNameModifiers, Element: CurNM)) {
5330 S.Diag(Loc: IC->getNameModifierLoc(),
5331 DiagID: diag::err_omp_wrong_if_directive_name_modifier)
5332 << getOpenMPDirectiveName(D: CurNM, Ver: OMPVersion)
5333 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion);
5334 ErrorFound = true;
5335 }
5336 }
5337 }
5338 // If any if clause on the directive includes a directive-name-modifier then
5339 // all if clauses on the directive must include a directive-name-modifier.
5340 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
5341 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
5342 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
5343 DiagID: diag::err_omp_no_more_if_clause);
5344 } else {
5345 std::string Values;
5346 std::string Sep(", ");
5347 unsigned AllowedCnt = 0;
5348 unsigned TotalAllowedNum =
5349 AllowedNameModifiers.size() - NamedModifiersNumber;
5350 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
5351 ++Cnt) {
5352 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
5353 if (!FoundNameModifiers[NM]) {
5354 Values += "'";
5355 Values += getOpenMPDirectiveName(D: NM, Ver: OMPVersion);
5356 Values += "'";
5357 if (AllowedCnt + 2 == TotalAllowedNum)
5358 Values += " or ";
5359 else if (AllowedCnt + 1 != TotalAllowedNum)
5360 Values += Sep;
5361 ++AllowedCnt;
5362 }
5363 }
5364 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
5365 DiagID: diag::err_omp_unnamed_if_clause)
5366 << (TotalAllowedNum > 1) << Values;
5367 }
5368 for (SourceLocation Loc : NameModifierLoc) {
5369 S.Diag(Loc, DiagID: diag::note_omp_previous_named_if_clause);
5370 }
5371 ErrorFound = true;
5372 }
5373 return ErrorFound;
5374}
5375
5376static std::pair<ValueDecl *, bool>
5377getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
5378 SourceRange &ERange, bool AllowArraySection,
5379 bool AllowAssumedSizeArray, StringRef DiagType) {
5380 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5381 RefExpr->containsUnexpandedParameterPack())
5382 return std::make_pair(x: nullptr, y: true);
5383
5384 // OpenMP [3.1, C/C++]
5385 // A list item is a variable name.
5386 // OpenMP [2.9.3.3, Restrictions, p.1]
5387 // A variable that is part of another variable (as an array or
5388 // structure element) cannot appear in a private clause.
5389 //
5390 // OpenMP [6.0]
5391 // 5.2.5 Array Sections, p. 166, L28-29
5392 // When the length is absent and the size of the dimension is not known,
5393 // the array section is an assumed-size array.
5394 // 2 Glossary, p. 23, L4-6
5395 // assumed-size array
5396 // For C/C++, an array section for which the length is absent and the
5397 // size of the dimensions is not known.
5398 // 5.2.5 Array Sections, p. 168, L11
5399 // An assumed-size array can appear only in clauses for which it is
5400 // explicitly allowed.
5401 // 7.4 List Item Privatization, Restrictions, p. 222, L15
5402 // Assumed-size arrays must not be privatized.
5403 RefExpr = RefExpr->IgnoreParens();
5404 enum {
5405 NoArrayExpr = -1,
5406 ArraySubscript = 0,
5407 OMPArraySection = 1
5408 } IsArrayExpr = NoArrayExpr;
5409 if (AllowArraySection) {
5410 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(Val: RefExpr)) {
5411 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
5412 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5413 Base = TempASE->getBase()->IgnoreParenImpCasts();
5414 RefExpr = Base;
5415 IsArrayExpr = ArraySubscript;
5416 } else if (auto *OASE = dyn_cast_or_null<ArraySectionExpr>(Val: RefExpr)) {
5417 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
5418 if (S.getLangOpts().OpenMP >= 60 && !AllowAssumedSizeArray &&
5419 OASE->getColonLocFirst().isValid() && !OASE->getLength()) {
5420 QualType BaseType = ArraySectionExpr::getBaseOriginalType(Base);
5421 if (BaseType.isNull() || (!BaseType->isConstantArrayType() &&
5422 !BaseType->isVariableArrayType())) {
5423 S.Diag(Loc: OASE->getColonLocFirst(),
5424 DiagID: diag::err_omp_section_length_undefined)
5425 << (!BaseType.isNull() && BaseType->isArrayType());
5426 return std::make_pair(x: nullptr, y: false);
5427 }
5428 }
5429 while (auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base))
5430 Base = TempOASE->getBase()->IgnoreParenImpCasts();
5431 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5432 Base = TempASE->getBase()->IgnoreParenImpCasts();
5433 RefExpr = Base;
5434 IsArrayExpr = OMPArraySection;
5435 }
5436 }
5437 ELoc = RefExpr->getExprLoc();
5438 ERange = RefExpr->getSourceRange();
5439 RefExpr = RefExpr->IgnoreParenImpCasts();
5440 auto *DE = dyn_cast_or_null<DeclRefExpr>(Val: RefExpr);
5441 auto *ME = dyn_cast_or_null<MemberExpr>(Val: RefExpr);
5442 if ((!DE || !isa<VarDecl>(Val: DE->getDecl())) &&
5443 (S.getCurrentThisType().isNull() || !ME ||
5444 !isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()) ||
5445 !isa<FieldDecl>(Val: ME->getMemberDecl()))) {
5446 if (IsArrayExpr != NoArrayExpr) {
5447 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
5448 << IsArrayExpr << ERange;
5449 } else if (!DiagType.empty()) {
5450 unsigned DiagSelect = S.getLangOpts().CPlusPlus
5451 ? (S.getCurrentThisType().isNull() ? 1 : 2)
5452 : 0;
5453 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr_with_type)
5454 << DiagSelect << DiagType << ERange;
5455 } else {
5456 S.Diag(Loc: ELoc,
5457 DiagID: AllowArraySection
5458 ? diag::err_omp_expected_var_name_member_expr_or_array_item
5459 : diag::err_omp_expected_var_name_member_expr)
5460 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
5461 }
5462 return std::make_pair(x: nullptr, y: false);
5463 }
5464 return std::make_pair(
5465 x: getCanonicalDecl(D: DE ? DE->getDecl() : ME->getMemberDecl()), y: false);
5466}
5467
5468namespace {
5469/// Checks if the allocator is used in uses_allocators clause to be allowed in
5470/// target regions.
5471class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> {
5472 DSAStackTy *S = nullptr;
5473
5474public:
5475 bool VisitDeclRefExpr(const DeclRefExpr *E) {
5476 return S->isUsesAllocatorsDecl(D: E->getDecl())
5477 .value_or(u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
5478 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait;
5479 }
5480 bool VisitStmt(const Stmt *S) {
5481 for (const Stmt *Child : S->children()) {
5482 if (Child && Visit(S: Child))
5483 return true;
5484 }
5485 return false;
5486 }
5487 explicit AllocatorChecker(DSAStackTy *S) : S(S) {}
5488};
5489} // namespace
5490
5491static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
5492 ArrayRef<OMPClause *> Clauses) {
5493 assert(!S.CurContext->isDependentContext() &&
5494 "Expected non-dependent context.");
5495 auto AllocateRange =
5496 llvm::make_filter_range(Range&: Clauses, Pred: OMPAllocateClause::classof);
5497 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> DeclToCopy;
5498 auto PrivateRange = llvm::make_filter_range(Range&: Clauses, Pred: [](const OMPClause *C) {
5499 return isOpenMPPrivate(Kind: C->getClauseKind());
5500 });
5501 for (OMPClause *Cl : PrivateRange) {
5502 MutableArrayRef<Expr *>::iterator I, It, Et;
5503 if (Cl->getClauseKind() == OMPC_private) {
5504 auto *PC = cast<OMPPrivateClause>(Val: Cl);
5505 I = PC->private_copies().begin();
5506 It = PC->varlist_begin();
5507 Et = PC->varlist_end();
5508 } else if (Cl->getClauseKind() == OMPC_firstprivate) {
5509 auto *PC = cast<OMPFirstprivateClause>(Val: Cl);
5510 I = PC->private_copies().begin();
5511 It = PC->varlist_begin();
5512 Et = PC->varlist_end();
5513 } else if (Cl->getClauseKind() == OMPC_lastprivate) {
5514 auto *PC = cast<OMPLastprivateClause>(Val: Cl);
5515 I = PC->private_copies().begin();
5516 It = PC->varlist_begin();
5517 Et = PC->varlist_end();
5518 } else if (Cl->getClauseKind() == OMPC_linear) {
5519 auto *PC = cast<OMPLinearClause>(Val: Cl);
5520 I = PC->privates().begin();
5521 It = PC->varlist_begin();
5522 Et = PC->varlist_end();
5523 } else if (Cl->getClauseKind() == OMPC_reduction) {
5524 auto *PC = cast<OMPReductionClause>(Val: Cl);
5525 I = PC->privates().begin();
5526 It = PC->varlist_begin();
5527 Et = PC->varlist_end();
5528 } else if (Cl->getClauseKind() == OMPC_task_reduction) {
5529 auto *PC = cast<OMPTaskReductionClause>(Val: Cl);
5530 I = PC->privates().begin();
5531 It = PC->varlist_begin();
5532 Et = PC->varlist_end();
5533 } else if (Cl->getClauseKind() == OMPC_in_reduction) {
5534 auto *PC = cast<OMPInReductionClause>(Val: Cl);
5535 I = PC->privates().begin();
5536 It = PC->varlist_begin();
5537 Et = PC->varlist_end();
5538 } else {
5539 llvm_unreachable("Expected private clause.");
5540 }
5541 for (Expr *E : llvm::make_range(x: It, y: Et)) {
5542 if (!*I) {
5543 ++I;
5544 continue;
5545 }
5546 SourceLocation ELoc;
5547 SourceRange ERange;
5548 Expr *SimpleRefExpr = E;
5549 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
5550 /*AllowArraySection=*/true);
5551 DeclToCopy.try_emplace(Key: Res.first,
5552 Args: cast<VarDecl>(Val: cast<DeclRefExpr>(Val: *I)->getDecl()));
5553 ++I;
5554 }
5555 }
5556 for (OMPClause *C : AllocateRange) {
5557 auto *AC = cast<OMPAllocateClause>(Val: C);
5558 if (S.getLangOpts().OpenMP >= 50 &&
5559 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() &&
5560 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()) &&
5561 AC->getAllocator()) {
5562 Expr *Allocator = AC->getAllocator();
5563 // OpenMP, 2.12.5 target Construct
5564 // Memory allocators that do not appear in a uses_allocators clause cannot
5565 // appear as an allocator in an allocate clause or be used in the target
5566 // region unless a requires directive with the dynamic_allocators clause
5567 // is present in the same compilation unit.
5568 AllocatorChecker Checker(Stack);
5569 if (Checker.Visit(S: Allocator))
5570 S.Diag(Loc: Allocator->getExprLoc(),
5571 DiagID: diag::err_omp_allocator_not_in_uses_allocators)
5572 << Allocator->getSourceRange();
5573 }
5574 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
5575 getAllocatorKind(S, Stack, Allocator: AC->getAllocator());
5576 // OpenMP, 2.11.4 allocate Clause, Restrictions.
5577 // For task, taskloop or target directives, allocation requests to memory
5578 // allocators with the trait access set to thread result in unspecified
5579 // behavior.
5580 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
5581 (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
5582 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()))) {
5583 unsigned OMPVersion = S.getLangOpts().OpenMP;
5584 S.Diag(Loc: AC->getAllocator()->getExprLoc(),
5585 DiagID: diag::warn_omp_allocate_thread_on_task_target_directive)
5586 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), Ver: OMPVersion);
5587 }
5588 for (Expr *E : AC->varlist()) {
5589 SourceLocation ELoc;
5590 SourceRange ERange;
5591 Expr *SimpleRefExpr = E;
5592 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
5593 ValueDecl *VD = Res.first;
5594 if (!VD)
5595 continue;
5596 DSAStackTy::DSAVarData Data = Stack->getTopDSA(D: VD, /*FromParent=*/false);
5597 if (!isOpenMPPrivate(Kind: Data.CKind)) {
5598 S.Diag(Loc: E->getExprLoc(),
5599 DiagID: diag::err_omp_expected_private_copy_for_allocate);
5600 continue;
5601 }
5602 VarDecl *PrivateVD = DeclToCopy[VD];
5603 if (checkPreviousOMPAllocateAttribute(S, Stack, RefExpr: E, VD: PrivateVD,
5604 AllocatorKind, Allocator: AC->getAllocator()))
5605 continue;
5606 applyOMPAllocateAttribute(S, VD: PrivateVD, AllocatorKind, Allocator: AC->getAllocator(),
5607 Alignment: AC->getAlignment(), SR: E->getSourceRange());
5608 }
5609 }
5610}
5611
5612namespace {
5613/// Rewrite statements and expressions for Sema \p Actions CurContext.
5614///
5615/// Used to wrap already parsed statements/expressions into a new CapturedStmt
5616/// context. DeclRefExpr used inside the new context are changed to refer to the
5617/// captured variable instead.
5618class CaptureVars : public TreeTransform<CaptureVars> {
5619 using BaseTransform = TreeTransform<CaptureVars>;
5620
5621public:
5622 CaptureVars(Sema &Actions) : BaseTransform(Actions) {}
5623
5624 bool AlwaysRebuild() { return true; }
5625};
5626} // namespace
5627
5628static VarDecl *precomputeExpr(Sema &Actions,
5629 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E,
5630 StringRef Name) {
5631 Expr *NewE = AssertSuccess(R: CaptureVars(Actions).TransformExpr(E));
5632 VarDecl *NewVar = buildVarDecl(SemaRef&: Actions, Loc: {}, Type: NewE->getType(), Name, Attrs: nullptr,
5633 OrigRef: dyn_cast<DeclRefExpr>(Val: E->IgnoreImplicit()));
5634 auto *NewDeclStmt = cast<DeclStmt>(Val: AssertSuccess(
5635 R: Actions.ActOnDeclStmt(Decl: Actions.ConvertDeclToDeclGroup(Ptr: NewVar), StartLoc: {}, EndLoc: {})));
5636 Actions.AddInitializerToDecl(dcl: NewDeclStmt->getSingleDecl(), init: NewE, DirectInit: false);
5637 BodyStmts.push_back(Elt: NewDeclStmt);
5638 return NewVar;
5639}
5640
5641/// Create a closure that computes the number of iterations of a loop.
5642///
5643/// \param Actions The Sema object.
5644/// \param LogicalTy Type for the logical iteration number.
5645/// \param Rel Comparison operator of the loop condition.
5646/// \param StartExpr Value of the loop counter at the first iteration.
5647/// \param StopExpr Expression the loop counter is compared against in the loop
5648/// condition. \param StepExpr Amount of increment after each iteration.
5649///
5650/// \return Closure (CapturedStmt) of the distance calculation.
5651static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy,
5652 BinaryOperator::Opcode Rel,
5653 Expr *StartExpr, Expr *StopExpr,
5654 Expr *StepExpr) {
5655 ASTContext &Ctx = Actions.getASTContext();
5656 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(T: LogicalTy);
5657
5658 // Captured regions currently don't support return values, we use an
5659 // out-parameter instead. All inputs are implicit captures.
5660 // TODO: Instead of capturing each DeclRefExpr occurring in
5661 // StartExpr/StopExpr/Step, these could also be passed as a value capture.
5662 QualType ResultTy = Ctx.getLValueReferenceType(T: LogicalTy);
5663 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy},
5664 {StringRef(), QualType()}};
5665 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
5666
5667 Stmt *Body;
5668 {
5669 Sema::CompoundScopeRAII CompoundScope(Actions);
5670 CapturedDecl *CS = cast<CapturedDecl>(Val: Actions.CurContext);
5671
5672 // Get the LValue expression for the result.
5673 ImplicitParamDecl *DistParam = CS->getParam(i: 0);
5674 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr(
5675 D: DistParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5676
5677 SmallVector<Stmt *, 4> BodyStmts;
5678
5679 // Capture all referenced variable references.
5680 // TODO: Instead of computing NewStart/NewStop/NewStep inside the
5681 // CapturedStmt, we could compute them before and capture the result, to be
5682 // used jointly with the LoopVar function.
5683 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, E: StartExpr, Name: ".start");
5684 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, E: StopExpr, Name: ".stop");
5685 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, E: StepExpr, Name: ".step");
5686 auto BuildVarRef = [&](VarDecl *VD) {
5687 return buildDeclRefExpr(S&: Actions, D: VD, Ty: VD->getType(), Loc: {});
5688 };
5689
5690 IntegerLiteral *Zero = IntegerLiteral::Create(
5691 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 0), type: LogicalTy, l: {});
5692 IntegerLiteral *One = IntegerLiteral::Create(
5693 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
5694 Expr *Dist;
5695 if (Rel == BO_NE) {
5696 // When using a != comparison, the increment can be +1 or -1. This can be
5697 // dynamic at runtime, so we need to check for the direction.
5698 Expr *IsNegStep = AssertSuccess(
5699 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_LT, LHSExpr: BuildVarRef(NewStep), RHSExpr: Zero));
5700
5701 // Positive increment.
5702 Expr *ForwardRange = AssertSuccess(R: Actions.BuildBinOp(
5703 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
5704 ForwardRange = AssertSuccess(
5705 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: ForwardRange));
5706 Expr *ForwardDist = AssertSuccess(R: Actions.BuildBinOp(
5707 S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: ForwardRange, RHSExpr: BuildVarRef(NewStep)));
5708
5709 // Negative increment.
5710 Expr *BackwardRange = AssertSuccess(R: Actions.BuildBinOp(
5711 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5712 BackwardRange = AssertSuccess(
5713 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: BackwardRange));
5714 Expr *NegIncAmount = AssertSuccess(
5715 R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: BuildVarRef(NewStep)));
5716 Expr *BackwardDist = AssertSuccess(
5717 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: BackwardRange, RHSExpr: NegIncAmount));
5718
5719 // Use the appropriate case.
5720 Dist = AssertSuccess(R: Actions.ActOnConditionalOp(
5721 QuestionLoc: {}, ColonLoc: {}, CondExpr: IsNegStep, LHSExpr: BackwardDist, RHSExpr: ForwardDist));
5722 } else {
5723 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) &&
5724 "Expected one of these relational operators");
5725
5726 // We can derive the direction from any other comparison operator. It is
5727 // non well-formed OpenMP if Step increments/decrements in the other
5728 // directions. Whether at least the first iteration passes the loop
5729 // condition.
5730 Expr *HasAnyIteration = AssertSuccess(R: Actions.BuildBinOp(
5731 S: nullptr, OpLoc: {}, Opc: Rel, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5732
5733 // Compute the range between first and last counter value.
5734 Expr *Range;
5735 if (Rel == BO_GE || Rel == BO_GT)
5736 Range = AssertSuccess(R: Actions.BuildBinOp(
5737 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5738 else
5739 Range = AssertSuccess(R: Actions.BuildBinOp(
5740 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
5741
5742 // Ensure unsigned range space.
5743 Range =
5744 AssertSuccess(R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: Range));
5745
5746 if (Rel == BO_LE || Rel == BO_GE) {
5747 // Add one to the range if the relational operator is inclusive.
5748 Range =
5749 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: One));
5750 }
5751
5752 // Divide by the absolute step amount. If the range is not a multiple of
5753 // the step size, rounding-up the effective upper bound ensures that the
5754 // last iteration is included.
5755 // Note that the rounding-up may cause an overflow in a temporary that
5756 // could be avoided, but would have occurred in a C-style for-loop as
5757 // well.
5758 Expr *Divisor = BuildVarRef(NewStep);
5759 if (Rel == BO_GE || Rel == BO_GT)
5760 Divisor =
5761 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: Divisor));
5762 Expr *DivisorMinusOne =
5763 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: Divisor, RHSExpr: One));
5764 Expr *RangeRoundUp = AssertSuccess(
5765 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: DivisorMinusOne));
5766 Dist = AssertSuccess(
5767 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: RangeRoundUp, RHSExpr: Divisor));
5768
5769 // If there is not at least one iteration, the range contains garbage. Fix
5770 // to zero in this case.
5771 Dist = AssertSuccess(
5772 R: Actions.ActOnConditionalOp(QuestionLoc: {}, ColonLoc: {}, CondExpr: HasAnyIteration, LHSExpr: Dist, RHSExpr: Zero));
5773 }
5774
5775 // Assign the result to the out-parameter.
5776 Stmt *ResultAssign = AssertSuccess(R: Actions.BuildBinOp(
5777 S: Actions.getCurScope(), OpLoc: {}, Opc: BO_Assign, LHSExpr: DistRef, RHSExpr: Dist));
5778 BodyStmts.push_back(Elt: ResultAssign);
5779
5780 Body = AssertSuccess(R: Actions.ActOnCompoundStmt(L: {}, R: {}, Elts: BodyStmts, isStmtExpr: false));
5781 }
5782
5783 return cast<CapturedStmt>(
5784 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
5785}
5786
5787/// Create a closure that computes the loop variable from the logical iteration
5788/// number.
5789///
5790/// \param Actions The Sema object.
5791/// \param LoopVarTy Type for the loop variable used for result value.
5792/// \param LogicalTy Type for the logical iteration number.
5793/// \param StartExpr Value of the loop counter at the first iteration.
5794/// \param Step Amount of increment after each iteration.
5795/// \param Deref Whether the loop variable is a dereference of the loop
5796/// counter variable.
5797///
5798/// \return Closure (CapturedStmt) of the loop value calculation.
5799static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy,
5800 QualType LogicalTy,
5801 DeclRefExpr *StartExpr, Expr *Step,
5802 bool Deref) {
5803 ASTContext &Ctx = Actions.getASTContext();
5804
5805 // Pass the result as an out-parameter. Passing as return value would require
5806 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to
5807 // invoke a copy constructor.
5808 QualType TargetParamTy = Ctx.getLValueReferenceType(T: LoopVarTy);
5809 SemaOpenMP::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy},
5810 {"Logical", LogicalTy},
5811 {StringRef(), QualType()}};
5812 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
5813
5814 // Capture the initial iterator which represents the LoopVar value at the
5815 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update
5816 // it in every iteration, capture it by value before it is modified.
5817 VarDecl *StartVar = cast<VarDecl>(Val: StartExpr->getDecl());
5818 bool Invalid = Actions.tryCaptureVariable(Var: StartVar, Loc: {},
5819 Kind: TryCaptureKind::ExplicitByVal, EllipsisLoc: {});
5820 (void)Invalid;
5821 assert(!Invalid && "Expecting capture-by-value to work.");
5822
5823 Expr *Body;
5824 {
5825 Sema::CompoundScopeRAII CompoundScope(Actions);
5826 auto *CS = cast<CapturedDecl>(Val: Actions.CurContext);
5827
5828 ImplicitParamDecl *TargetParam = CS->getParam(i: 0);
5829 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr(
5830 D: TargetParam, Ty: LoopVarTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5831 ImplicitParamDecl *IndvarParam = CS->getParam(i: 1);
5832 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr(
5833 D: IndvarParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5834
5835 // Capture the Start expression.
5836 CaptureVars Recap(Actions);
5837 Expr *NewStart = AssertSuccess(R: Recap.TransformExpr(E: StartExpr));
5838 Expr *NewStep = AssertSuccess(R: Recap.TransformExpr(E: Step));
5839
5840 Expr *Skip = AssertSuccess(
5841 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Mul, LHSExpr: NewStep, RHSExpr: LogicalRef));
5842 // TODO: Explicitly cast to the iterator's difference_type instead of
5843 // relying on implicit conversion.
5844 Expr *Advanced =
5845 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: NewStart, RHSExpr: Skip));
5846
5847 if (Deref) {
5848 // For range-based for-loops convert the loop counter value to a concrete
5849 // loop variable value by dereferencing the iterator.
5850 Advanced =
5851 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Deref, Input: Advanced));
5852 }
5853
5854 // Assign the result to the output parameter.
5855 Body = AssertSuccess(R: Actions.BuildBinOp(S: Actions.getCurScope(), OpLoc: {},
5856 Opc: BO_Assign, LHSExpr: TargetRef, RHSExpr: Advanced));
5857 }
5858 return cast<CapturedStmt>(
5859 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
5860}
5861
5862StmtResult SemaOpenMP::ActOnOpenMPCanonicalLoop(Stmt *AStmt) {
5863 ASTContext &Ctx = getASTContext();
5864
5865 // Extract the common elements of ForStmt and CXXForRangeStmt:
5866 // Loop variable, repeat condition, increment
5867 Expr *Cond, *Inc;
5868 VarDecl *LIVDecl, *LUVDecl;
5869 if (auto *For = dyn_cast<ForStmt>(Val: AStmt)) {
5870 Stmt *Init = For->getInit();
5871 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Val: Init)) {
5872 // For statement declares loop variable.
5873 LIVDecl = cast<VarDecl>(Val: LCVarDeclStmt->getSingleDecl());
5874 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Val: Init)) {
5875 // For statement reuses variable.
5876 assert(LCAssign->getOpcode() == BO_Assign &&
5877 "init part must be a loop variable assignment");
5878 auto *CounterRef = cast<DeclRefExpr>(Val: LCAssign->getLHS());
5879 LIVDecl = cast<VarDecl>(Val: CounterRef->getDecl());
5880 } else
5881 llvm_unreachable("Cannot determine loop variable");
5882 LUVDecl = LIVDecl;
5883
5884 Cond = For->getCond();
5885 Inc = For->getInc();
5886 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(Val: AStmt)) {
5887 DeclStmt *BeginStmt = RangeFor->getBeginStmt();
5888 LIVDecl = cast<VarDecl>(Val: BeginStmt->getSingleDecl());
5889 LUVDecl = RangeFor->getLoopVariable();
5890
5891 Cond = RangeFor->getCond();
5892 Inc = RangeFor->getInc();
5893 } else
5894 llvm_unreachable("unhandled kind of loop");
5895
5896 QualType CounterTy = LIVDecl->getType();
5897 QualType LVTy = LUVDecl->getType();
5898
5899 // Analyze the loop condition.
5900 Expr *LHS, *RHS;
5901 BinaryOperator::Opcode CondRel;
5902 Cond = Cond->IgnoreImplicit();
5903 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Val: Cond)) {
5904 LHS = CondBinExpr->getLHS();
5905 RHS = CondBinExpr->getRHS();
5906 CondRel = CondBinExpr->getOpcode();
5907 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Cond)) {
5908 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands");
5909 LHS = CondCXXOp->getArg(Arg: 0);
5910 RHS = CondCXXOp->getArg(Arg: 1);
5911 switch (CondCXXOp->getOperator()) {
5912 case OO_ExclaimEqual:
5913 CondRel = BO_NE;
5914 break;
5915 case OO_Less:
5916 CondRel = BO_LT;
5917 break;
5918 case OO_LessEqual:
5919 CondRel = BO_LE;
5920 break;
5921 case OO_Greater:
5922 CondRel = BO_GT;
5923 break;
5924 case OO_GreaterEqual:
5925 CondRel = BO_GE;
5926 break;
5927 default:
5928 llvm_unreachable("unexpected iterator operator");
5929 }
5930 } else
5931 llvm_unreachable("unexpected loop condition");
5932
5933 // Normalize such that the loop counter is on the LHS.
5934 if (!isa<DeclRefExpr>(Val: LHS->IgnoreImplicit()) ||
5935 cast<DeclRefExpr>(Val: LHS->IgnoreImplicit())->getDecl() != LIVDecl) {
5936 std::swap(a&: LHS, b&: RHS);
5937 CondRel = BinaryOperator::reverseComparisonOp(Opc: CondRel);
5938 }
5939 auto *CounterRef = cast<DeclRefExpr>(Val: LHS->IgnoreImplicit());
5940
5941 // Decide the bit width for the logical iteration counter. By default use the
5942 // unsigned ptrdiff_t integer size (for iterators and pointers).
5943 // TODO: For iterators, use iterator::difference_type,
5944 // std::iterator_traits<>::difference_type or decltype(it - end).
5945 QualType LogicalTy = Ctx.getUnsignedPointerDiffType();
5946 if (CounterTy->isIntegerType()) {
5947 unsigned BitWidth = Ctx.getIntWidth(T: CounterTy);
5948 LogicalTy = Ctx.getIntTypeForBitwidth(DestWidth: BitWidth, Signed: false);
5949 }
5950
5951 // Analyze the loop increment.
5952 Expr *Step;
5953 if (auto *IncUn = dyn_cast<UnaryOperator>(Val: Inc)) {
5954 int Direction;
5955 switch (IncUn->getOpcode()) {
5956 case UO_PreInc:
5957 case UO_PostInc:
5958 Direction = 1;
5959 break;
5960 case UO_PreDec:
5961 case UO_PostDec:
5962 Direction = -1;
5963 break;
5964 default:
5965 llvm_unreachable("unhandled unary increment operator");
5966 }
5967 Step = IntegerLiteral::Create(
5968 C: Ctx,
5969 V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), Direction, /*isSigned=*/true),
5970 type: LogicalTy, l: {});
5971 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Val: Inc)) {
5972 if (IncBin->getOpcode() == BO_AddAssign) {
5973 Step = IncBin->getRHS();
5974 } else if (IncBin->getOpcode() == BO_SubAssign) {
5975 Step = AssertSuccess(
5976 R: SemaRef.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: IncBin->getRHS()));
5977 } else
5978 llvm_unreachable("unhandled binary increment operator");
5979 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Inc)) {
5980 switch (CondCXXOp->getOperator()) {
5981 case OO_PlusPlus:
5982 Step = IntegerLiteral::Create(
5983 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
5984 break;
5985 case OO_MinusMinus:
5986 Step = IntegerLiteral::Create(
5987 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), -1), type: LogicalTy, l: {});
5988 break;
5989 case OO_PlusEqual:
5990 Step = CondCXXOp->getArg(Arg: 1);
5991 break;
5992 case OO_MinusEqual:
5993 Step = AssertSuccess(
5994 R: SemaRef.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: CondCXXOp->getArg(Arg: 1)));
5995 break;
5996 default:
5997 llvm_unreachable("unhandled overloaded increment operator");
5998 }
5999 } else
6000 llvm_unreachable("unknown increment expression");
6001
6002 CapturedStmt *DistanceFunc =
6003 buildDistanceFunc(Actions&: SemaRef, LogicalTy, Rel: CondRel, StartExpr: LHS, StopExpr: RHS, StepExpr: Step);
6004 CapturedStmt *LoopVarFunc = buildLoopVarFunc(
6005 Actions&: SemaRef, LoopVarTy: LVTy, LogicalTy, StartExpr: CounterRef, Step, Deref: isa<CXXForRangeStmt>(Val: AStmt));
6006 DeclRefExpr *LVRef =
6007 SemaRef.BuildDeclRefExpr(D: LUVDecl, Ty: LUVDecl->getType(), VK: VK_LValue, NameInfo: {},
6008 SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
6009 return OMPCanonicalLoop::create(Ctx: getASTContext(), LoopStmt: AStmt, DistanceFunc,
6010 LoopVarFunc, LoopVarRef: LVRef);
6011}
6012
6013StmtResult SemaOpenMP::ActOnOpenMPLoopnest(Stmt *AStmt) {
6014 // Handle a literal loop.
6015 if (isa<ForStmt>(Val: AStmt) || isa<CXXForRangeStmt>(Val: AStmt))
6016 return ActOnOpenMPCanonicalLoop(AStmt);
6017
6018 // If not a literal loop, it must be the result of a loop transformation.
6019 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(Val: AStmt);
6020 assert(
6021 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) &&
6022 "Loop transformation directive expected");
6023 return LoopTransform;
6024}
6025
6026static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
6027 CXXScopeSpec &MapperIdScopeSpec,
6028 const DeclarationNameInfo &MapperId,
6029 QualType Type,
6030 Expr *UnresolvedMapper);
6031
6032/// Perform DFS through the structure/class data members trying to find
6033/// member(s) with user-defined 'default' mapper and generate implicit map
6034/// clauses for such members with the found 'default' mapper.
6035static void
6036processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack,
6037 SmallVectorImpl<OMPClause *> &Clauses) {
6038 // Check for the default mapper for data members.
6039 if (S.getLangOpts().OpenMP < 50)
6040 return;
6041 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) {
6042 auto *C = dyn_cast<OMPMapClause>(Val: Clauses[Cnt]);
6043 if (!C)
6044 continue;
6045 SmallVector<Expr *, 4> SubExprs;
6046 auto *MI = C->mapperlist_begin();
6047 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End;
6048 ++I, ++MI) {
6049 // Expression is mapped using mapper - skip it.
6050 if (*MI)
6051 continue;
6052 Expr *E = *I;
6053 // Expression is dependent - skip it, build the mapper when it gets
6054 // instantiated.
6055 if (E->isTypeDependent() || E->isValueDependent() ||
6056 E->containsUnexpandedParameterPack())
6057 continue;
6058 // Array section - need to check for the mapping of the array section
6059 // element.
6060 QualType CanonType = E->getType().getCanonicalType();
6061 if (CanonType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
6062 const auto *OASE = cast<ArraySectionExpr>(Val: E->IgnoreParenImpCasts());
6063 QualType BaseType =
6064 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
6065 QualType ElemType;
6066 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
6067 ElemType = ATy->getElementType();
6068 else
6069 ElemType = BaseType->getPointeeType();
6070 CanonType = ElemType;
6071 }
6072
6073 // DFS over data members in structures/classes.
6074 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(
6075 1, {CanonType, nullptr});
6076 llvm::DenseMap<const Type *, Expr *> Visited;
6077 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(
6078 1, {nullptr, 1});
6079 while (!Types.empty()) {
6080 QualType BaseType;
6081 FieldDecl *CurFD;
6082 std::tie(args&: BaseType, args&: CurFD) = Types.pop_back_val();
6083 while (ParentChain.back().second == 0)
6084 ParentChain.pop_back();
6085 --ParentChain.back().second;
6086 if (BaseType.isNull())
6087 continue;
6088 // Only structs/classes are allowed to have mappers.
6089 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
6090 if (!RD)
6091 continue;
6092 auto It = Visited.find(Val: BaseType.getTypePtr());
6093 if (It == Visited.end()) {
6094 // Try to find the associated user-defined mapper.
6095 CXXScopeSpec MapperIdScopeSpec;
6096 DeclarationNameInfo DefaultMapperId;
6097 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
6098 ID: &S.Context.Idents.get(Name: "default")));
6099 DefaultMapperId.setLoc(E->getExprLoc());
6100 ExprResult ER = buildUserDefinedMapperRef(
6101 SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec, MapperId: DefaultMapperId,
6102 Type: BaseType, /*UnresolvedMapper=*/nullptr);
6103 if (ER.isInvalid())
6104 continue;
6105 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args: ER.get()).first;
6106 }
6107 // Found default mapper.
6108 if (It->second) {
6109 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType,
6110 VK_LValue, OK_Ordinary, E);
6111 OE->setIsUnique(/*V=*/true);
6112 Expr *BaseExpr = OE;
6113 for (const auto &P : ParentChain) {
6114 if (P.first) {
6115 BaseExpr = S.BuildMemberExpr(
6116 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6117 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: P.first,
6118 FoundDecl: DeclAccessPair::make(D: P.first, AS: P.first->getAccess()),
6119 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6120 Ty: P.first->getType(), VK: VK_LValue, OK: OK_Ordinary);
6121 BaseExpr = S.DefaultLvalueConversion(E: BaseExpr).get();
6122 }
6123 }
6124 if (CurFD)
6125 BaseExpr = S.BuildMemberExpr(
6126 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6127 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: CurFD,
6128 FoundDecl: DeclAccessPair::make(D: CurFD, AS: CurFD->getAccess()),
6129 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6130 Ty: CurFD->getType(), VK: VK_LValue, OK: OK_Ordinary);
6131 SubExprs.push_back(Elt: BaseExpr);
6132 continue;
6133 }
6134 // Check for the "default" mapper for data members.
6135 bool FirstIter = true;
6136 for (FieldDecl *FD : RD->fields()) {
6137 if (!FD)
6138 continue;
6139 QualType FieldTy = FD->getType();
6140 if (FieldTy.isNull() ||
6141 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
6142 continue;
6143 if (FirstIter) {
6144 FirstIter = false;
6145 ParentChain.emplace_back(Args&: CurFD, Args: 1);
6146 } else {
6147 ++ParentChain.back().second;
6148 }
6149 Types.emplace_back(Args&: FieldTy, Args&: FD);
6150 }
6151 }
6152 }
6153 if (SubExprs.empty())
6154 continue;
6155 CXXScopeSpec MapperIdScopeSpec;
6156 DeclarationNameInfo MapperId;
6157 if (OMPClause *NewClause = S.OpenMP().ActOnOpenMPMapClause(
6158 IteratorModifier: nullptr, MapTypeModifiers: C->getMapTypeModifiers(), MapTypeModifiersLoc: C->getMapTypeModifiersLoc(),
6159 MapperIdScopeSpec, MapperId, MapType: C->getMapType(),
6160 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6161 VarList: SubExprs, Locs: OMPVarListLocTy()))
6162 Clauses.push_back(Elt: NewClause);
6163 }
6164}
6165
6166namespace {
6167/// A 'teams loop' with a nested 'loop bind(parallel)' or generic function
6168/// call in the associated loop-nest cannot be a 'parallel for'.
6169class TeamsLoopChecker final : public ConstStmtVisitor<TeamsLoopChecker> {
6170 Sema &SemaRef;
6171
6172public:
6173 bool teamsLoopCanBeParallelFor() const { return TeamsLoopCanBeParallelFor; }
6174
6175 // Is there a nested OpenMP loop bind(parallel)
6176 void VisitOMPExecutableDirective(const OMPExecutableDirective *D) {
6177 if (D->getDirectiveKind() == llvm::omp::Directive::OMPD_loop) {
6178 if (const auto *C = D->getSingleClause<OMPBindClause>())
6179 if (C->getBindKind() == OMPC_BIND_parallel) {
6180 TeamsLoopCanBeParallelFor = false;
6181 // No need to continue visiting any more
6182 return;
6183 }
6184 }
6185 for (const Stmt *Child : D->children())
6186 if (Child)
6187 Visit(S: Child);
6188 }
6189
6190 void VisitCallExpr(const CallExpr *C) {
6191 // Function calls inhibit parallel loop translation of 'target teams loop'
6192 // unless the assume-no-nested-parallelism flag has been specified.
6193 // OpenMP API runtime library calls do not inhibit parallel loop
6194 // translation, regardless of the assume-no-nested-parallelism.
6195 bool IsOpenMPAPI = false;
6196 auto *FD = dyn_cast_or_null<FunctionDecl>(Val: C->getCalleeDecl());
6197 if (FD) {
6198 std::string Name = FD->getNameInfo().getAsString();
6199 IsOpenMPAPI = Name.find(s: "omp_") == 0;
6200 }
6201 TeamsLoopCanBeParallelFor =
6202 IsOpenMPAPI || SemaRef.getLangOpts().OpenMPNoNestedParallelism;
6203 if (!TeamsLoopCanBeParallelFor)
6204 return;
6205
6206 for (const Stmt *Child : C->children())
6207 if (Child)
6208 Visit(S: Child);
6209 }
6210
6211 void VisitCapturedStmt(const CapturedStmt *S) {
6212 if (!S)
6213 return;
6214 Visit(S: S->getCapturedDecl()->getBody());
6215 }
6216
6217 void VisitStmt(const Stmt *S) {
6218 if (!S)
6219 return;
6220 for (const Stmt *Child : S->children())
6221 if (Child)
6222 Visit(S: Child);
6223 }
6224 explicit TeamsLoopChecker(Sema &SemaRef)
6225 : SemaRef(SemaRef), TeamsLoopCanBeParallelFor(true) {}
6226
6227private:
6228 bool TeamsLoopCanBeParallelFor;
6229};
6230} // namespace
6231
6232static bool teamsLoopCanBeParallelFor(Stmt *AStmt, Sema &SemaRef) {
6233 TeamsLoopChecker Checker(SemaRef);
6234 Checker.Visit(S: AStmt);
6235 return Checker.teamsLoopCanBeParallelFor();
6236}
6237
6238StmtResult SemaOpenMP::ActOnOpenMPExecutableDirective(
6239 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
6240 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
6241 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
6242 assert(isOpenMPExecutableDirective(Kind) && "Unexpected directive category");
6243
6244 StmtResult Res = StmtError();
6245 OpenMPBindClauseKind BindKind = OMPC_BIND_unknown;
6246 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
6247
6248 if (const OMPBindClause *BC =
6249 OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses))
6250 BindKind = BC->getBindKind();
6251
6252 if (Kind == OMPD_loop && BindKind == OMPC_BIND_unknown) {
6253 const OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
6254
6255 // Setting the enclosing teams or parallel construct for the loop
6256 // directive without bind clause.
6257 // [5.0:129:25-28] If the bind clause is not present on the construct and
6258 // the loop construct is closely nested inside a teams or parallel
6259 // construct, the binding region is the corresponding teams or parallel
6260 // region. If none of those conditions hold, the binding region is not
6261 // defined.
6262 BindKind = OMPC_BIND_thread; // Default bind(thread) if binding is unknown
6263 ArrayRef<OpenMPDirectiveKind> ParentLeafs =
6264 getLeafConstructsOrSelf(D: ParentDirective);
6265
6266 if (ParentDirective == OMPD_unknown) {
6267 Diag(DSAStack->getDefaultDSALocation(),
6268 DiagID: diag::err_omp_bind_required_on_loop);
6269 } else if (ParentLeafs.back() == OMPD_parallel) {
6270 BindKind = OMPC_BIND_parallel;
6271 } else if (ParentLeafs.back() == OMPD_teams) {
6272 BindKind = OMPC_BIND_teams;
6273 }
6274
6275 assert(BindKind != OMPC_BIND_unknown && "Expecting BindKind");
6276
6277 OMPClause *C =
6278 ActOnOpenMPBindClause(Kind: BindKind, KindLoc: SourceLocation(), StartLoc: SourceLocation(),
6279 LParenLoc: SourceLocation(), EndLoc: SourceLocation());
6280 ClausesWithImplicit.push_back(Elt: C);
6281 }
6282
6283 // Diagnose "loop bind(teams)" with "reduction".
6284 if (Kind == OMPD_loop && BindKind == OMPC_BIND_teams) {
6285 for (OMPClause *C : Clauses) {
6286 if (C->getClauseKind() == OMPC_reduction)
6287 Diag(DSAStack->getDefaultDSALocation(),
6288 DiagID: diag::err_omp_loop_reduction_clause);
6289 }
6290 }
6291
6292 // First check CancelRegion which is then used in checkNestingOfRegions.
6293 if (checkCancelRegion(SemaRef, CurrentRegion: Kind, CancelRegion, StartLoc) ||
6294 checkNestingOfRegions(SemaRef, DSAStack, CurrentRegion: Kind, CurrentName: DirName, CancelRegion,
6295 BindKind, StartLoc)) {
6296 return StmtError();
6297 }
6298
6299 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
6300 if (getLangOpts().HIP && (isOpenMPTargetExecutionDirective(DKind: Kind) ||
6301 isOpenMPTargetDataManagementDirective(DKind: Kind)))
6302 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
6303
6304 VarsWithInheritedDSAType VarsWithInheritedDSA;
6305 bool ErrorFound = false;
6306 ClausesWithImplicit.append(in_start: Clauses.begin(), in_end: Clauses.end());
6307
6308 if (AStmt && !SemaRef.CurContext->isDependentContext() &&
6309 isOpenMPCapturingDirective(DKind: Kind)) {
6310 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6311
6312 // Check default data sharing attributes for referenced variables.
6313 DSAAttrChecker DSAChecker(DSAStack, SemaRef, cast<CapturedStmt>(Val: AStmt));
6314 int ThisCaptureLevel = getOpenMPCaptureLevels(DKind: Kind);
6315 Stmt *S = AStmt;
6316 while (--ThisCaptureLevel >= 0)
6317 S = cast<CapturedStmt>(Val: S)->getCapturedStmt();
6318 DSAChecker.Visit(S);
6319 if (!isOpenMPTargetDataManagementDirective(DKind: Kind) &&
6320 !isOpenMPTaskingDirective(Kind)) {
6321 // Visit subcaptures to generate implicit clauses for captured vars.
6322 auto *CS = cast<CapturedStmt>(Val: AStmt);
6323 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
6324 getOpenMPCaptureRegions(CaptureRegions, DKind: Kind);
6325 // Ignore outer tasking regions for target directives.
6326 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
6327 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
6328 DSAChecker.visitSubCaptures(S: CS);
6329 }
6330 if (DSAChecker.isErrorFound())
6331 return StmtError();
6332 // Generate list of implicitly defined firstprivate variables.
6333 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
6334 VariableImplicitInfo ImpInfo = DSAChecker.getImplicitInfo();
6335
6336 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
6337 ImplicitMapModifiersLoc[VariableImplicitInfo::DefaultmapKindNum];
6338 // Get the original location of present modifier from Defaultmap clause.
6339 SourceLocation PresentModifierLocs[VariableImplicitInfo::DefaultmapKindNum];
6340 for (OMPClause *C : Clauses) {
6341 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(Val: C))
6342 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present)
6343 PresentModifierLocs[DMC->getDefaultmapKind()] =
6344 DMC->getDefaultmapModifierLoc();
6345 }
6346
6347 for (OpenMPDefaultmapClauseKind K :
6348 llvm::enum_seq_inclusive<OpenMPDefaultmapClauseKind>(
6349 Begin: OpenMPDefaultmapClauseKind(), End: OMPC_DEFAULTMAP_unknown)) {
6350 std::fill_n(first: std::back_inserter(x&: ImplicitMapModifiersLoc[K]),
6351 n: ImpInfo.MapModifiers[K].size(), value: PresentModifierLocs[K]);
6352 }
6353 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
6354 for (OMPClause *C : Clauses) {
6355 if (auto *IRC = dyn_cast<OMPInReductionClause>(Val: C)) {
6356 for (Expr *E : IRC->taskgroup_descriptors())
6357 if (E)
6358 ImpInfo.Firstprivates.insert(X: E);
6359 }
6360 // OpenMP 5.0, 2.10.1 task Construct
6361 // [detach clause]... The event-handle will be considered as if it was
6362 // specified on a firstprivate clause.
6363 if (auto *DC = dyn_cast<OMPDetachClause>(Val: C))
6364 ImpInfo.Firstprivates.insert(X: DC->getEventHandler());
6365 }
6366 if (!ImpInfo.Firstprivates.empty()) {
6367 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
6368 VarList: ImpInfo.Firstprivates.getArrayRef(), StartLoc: SourceLocation(),
6369 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6370 ClausesWithImplicit.push_back(Elt: Implicit);
6371 ErrorFound = cast<OMPFirstprivateClause>(Val: Implicit)->varlist_size() !=
6372 ImpInfo.Firstprivates.size();
6373 } else {
6374 ErrorFound = true;
6375 }
6376 }
6377 if (!ImpInfo.Privates.empty()) {
6378 if (OMPClause *Implicit = ActOnOpenMPPrivateClause(
6379 VarList: ImpInfo.Privates.getArrayRef(), StartLoc: SourceLocation(),
6380 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6381 ClausesWithImplicit.push_back(Elt: Implicit);
6382 ErrorFound = cast<OMPPrivateClause>(Val: Implicit)->varlist_size() !=
6383 ImpInfo.Privates.size();
6384 } else {
6385 ErrorFound = true;
6386 }
6387 }
6388 // OpenMP 5.0 [2.19.7]
6389 // If a list item appears in a reduction, lastprivate or linear
6390 // clause on a combined target construct then it is treated as
6391 // if it also appears in a map clause with a map-type of tofrom
6392 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target &&
6393 isOpenMPTargetExecutionDirective(DKind: Kind)) {
6394 SmallVector<Expr *, 4> ImplicitExprs;
6395 for (OMPClause *C : Clauses) {
6396 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C))
6397 for (Expr *E : RC->varlist())
6398 if (!isa<DeclRefExpr>(Val: E->IgnoreParenImpCasts()))
6399 ImplicitExprs.emplace_back(Args&: E);
6400 }
6401 if (!ImplicitExprs.empty()) {
6402 ArrayRef<Expr *> Exprs = ImplicitExprs;
6403 CXXScopeSpec MapperIdScopeSpec;
6404 DeclarationNameInfo MapperId;
6405 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6406 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(),
6407 MapperIdScopeSpec, MapperId, MapType: OMPC_MAP_tofrom,
6408 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6409 VarList: Exprs, Locs: OMPVarListLocTy(), /*NoDiagnose=*/true))
6410 ClausesWithImplicit.emplace_back(Args&: Implicit);
6411 }
6412 }
6413 for (unsigned I = 0; I < VariableImplicitInfo::DefaultmapKindNum; ++I) {
6414 int ClauseKindCnt = -1;
6415 for (unsigned J = 0; J < VariableImplicitInfo::MapKindNum; ++J) {
6416 ArrayRef<Expr *> ImplicitMap = ImpInfo.Mappings[I][J].getArrayRef();
6417 ++ClauseKindCnt;
6418 if (ImplicitMap.empty())
6419 continue;
6420 CXXScopeSpec MapperIdScopeSpec;
6421 DeclarationNameInfo MapperId;
6422 auto K = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
6423 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6424 IteratorModifier: nullptr, MapTypeModifiers: ImpInfo.MapModifiers[I], MapTypeModifiersLoc: ImplicitMapModifiersLoc[I],
6425 MapperIdScopeSpec, MapperId, MapType: K, /*IsMapTypeImplicit=*/true,
6426 MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: ImplicitMap,
6427 Locs: OMPVarListLocTy())) {
6428 ClausesWithImplicit.emplace_back(Args&: Implicit);
6429 ErrorFound |= cast<OMPMapClause>(Val: Implicit)->varlist_size() !=
6430 ImplicitMap.size();
6431 } else {
6432 ErrorFound = true;
6433 }
6434 }
6435 }
6436 // Build expressions for implicit maps of data members with 'default'
6437 // mappers.
6438 if (getLangOpts().OpenMP >= 50)
6439 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
6440 Clauses&: ClausesWithImplicit);
6441 }
6442
6443 switch (Kind) {
6444 case OMPD_parallel:
6445 Res = ActOnOpenMPParallelDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6446 EndLoc);
6447 break;
6448 case OMPD_simd:
6449 Res = ActOnOpenMPSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6450 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6451 break;
6452 case OMPD_tile:
6453 Res =
6454 ActOnOpenMPTileDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6455 break;
6456 case OMPD_stripe:
6457 Res = ActOnOpenMPStripeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6458 EndLoc);
6459 break;
6460 case OMPD_unroll:
6461 Res = ActOnOpenMPUnrollDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6462 EndLoc);
6463 break;
6464 case OMPD_reverse:
6465 assert(ClausesWithImplicit.empty() &&
6466 "reverse directive does not support any clauses");
6467 Res = ActOnOpenMPReverseDirective(AStmt, StartLoc, EndLoc);
6468 break;
6469 case OMPD_interchange:
6470 Res = ActOnOpenMPInterchangeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6471 EndLoc);
6472 break;
6473 case OMPD_fuse:
6474 Res =
6475 ActOnOpenMPFuseDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6476 break;
6477 case OMPD_for:
6478 Res = ActOnOpenMPForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6479 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6480 break;
6481 case OMPD_for_simd:
6482 Res = ActOnOpenMPForSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6483 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6484 break;
6485 case OMPD_sections:
6486 Res = ActOnOpenMPSectionsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6487 EndLoc);
6488 break;
6489 case OMPD_section:
6490 assert(ClausesWithImplicit.empty() &&
6491 "No clauses are allowed for 'omp section' directive");
6492 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
6493 break;
6494 case OMPD_single:
6495 Res = ActOnOpenMPSingleDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6496 EndLoc);
6497 break;
6498 case OMPD_master:
6499 assert(ClausesWithImplicit.empty() &&
6500 "No clauses are allowed for 'omp master' directive");
6501 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
6502 break;
6503 case OMPD_masked:
6504 Res = ActOnOpenMPMaskedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6505 EndLoc);
6506 break;
6507 case OMPD_critical:
6508 Res = ActOnOpenMPCriticalDirective(DirName, Clauses: ClausesWithImplicit, AStmt,
6509 StartLoc, EndLoc);
6510 break;
6511 case OMPD_parallel_for:
6512 Res = ActOnOpenMPParallelForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6513 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6514 break;
6515 case OMPD_parallel_for_simd:
6516 Res = ActOnOpenMPParallelForSimdDirective(
6517 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6518 break;
6519 case OMPD_scope:
6520 Res =
6521 ActOnOpenMPScopeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6522 break;
6523 case OMPD_parallel_master:
6524 Res = ActOnOpenMPParallelMasterDirective(Clauses: ClausesWithImplicit, AStmt,
6525 StartLoc, EndLoc);
6526 break;
6527 case OMPD_parallel_masked:
6528 Res = ActOnOpenMPParallelMaskedDirective(Clauses: ClausesWithImplicit, AStmt,
6529 StartLoc, EndLoc);
6530 break;
6531 case OMPD_parallel_sections:
6532 Res = ActOnOpenMPParallelSectionsDirective(Clauses: ClausesWithImplicit, AStmt,
6533 StartLoc, EndLoc);
6534 break;
6535 case OMPD_task:
6536 Res =
6537 ActOnOpenMPTaskDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6538 break;
6539 case OMPD_taskyield:
6540 assert(ClausesWithImplicit.empty() &&
6541 "No clauses are allowed for 'omp taskyield' directive");
6542 assert(AStmt == nullptr &&
6543 "No associated statement allowed for 'omp taskyield' directive");
6544 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
6545 break;
6546 case OMPD_error:
6547 assert(AStmt == nullptr &&
6548 "No associated statement allowed for 'omp error' directive");
6549 Res = ActOnOpenMPErrorDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6550 break;
6551 case OMPD_barrier:
6552 assert(ClausesWithImplicit.empty() &&
6553 "No clauses are allowed for 'omp barrier' directive");
6554 assert(AStmt == nullptr &&
6555 "No associated statement allowed for 'omp barrier' directive");
6556 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
6557 break;
6558 case OMPD_taskwait:
6559 assert(AStmt == nullptr &&
6560 "No associated statement allowed for 'omp taskwait' directive");
6561 Res = ActOnOpenMPTaskwaitDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6562 break;
6563 case OMPD_taskgroup:
6564 Res = ActOnOpenMPTaskgroupDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6565 EndLoc);
6566 break;
6567 case OMPD_flush:
6568 assert(AStmt == nullptr &&
6569 "No associated statement allowed for 'omp flush' directive");
6570 Res = ActOnOpenMPFlushDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6571 break;
6572 case OMPD_depobj:
6573 assert(AStmt == nullptr &&
6574 "No associated statement allowed for 'omp depobj' directive");
6575 Res = ActOnOpenMPDepobjDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6576 break;
6577 case OMPD_scan:
6578 assert(AStmt == nullptr &&
6579 "No associated statement allowed for 'omp scan' directive");
6580 Res = ActOnOpenMPScanDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6581 break;
6582 case OMPD_ordered:
6583 Res = ActOnOpenMPOrderedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6584 EndLoc);
6585 break;
6586 case OMPD_atomic:
6587 Res = ActOnOpenMPAtomicDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6588 EndLoc);
6589 break;
6590 case OMPD_teams:
6591 Res =
6592 ActOnOpenMPTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6593 break;
6594 case OMPD_target:
6595 Res = ActOnOpenMPTargetDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6596 EndLoc);
6597 break;
6598 case OMPD_target_parallel:
6599 Res = ActOnOpenMPTargetParallelDirective(Clauses: ClausesWithImplicit, AStmt,
6600 StartLoc, EndLoc);
6601 break;
6602 case OMPD_target_parallel_for:
6603 Res = ActOnOpenMPTargetParallelForDirective(
6604 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6605 break;
6606 case OMPD_cancellation_point:
6607 assert(ClausesWithImplicit.empty() &&
6608 "No clauses are allowed for 'omp cancellation point' directive");
6609 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
6610 "cancellation point' directive");
6611 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
6612 break;
6613 case OMPD_cancel:
6614 assert(AStmt == nullptr &&
6615 "No associated statement allowed for 'omp cancel' directive");
6616 Res = ActOnOpenMPCancelDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc,
6617 CancelRegion);
6618 break;
6619 case OMPD_target_data:
6620 Res = ActOnOpenMPTargetDataDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6621 EndLoc);
6622 break;
6623 case OMPD_target_enter_data:
6624 Res = ActOnOpenMPTargetEnterDataDirective(Clauses: ClausesWithImplicit, StartLoc,
6625 EndLoc, AStmt);
6626 break;
6627 case OMPD_target_exit_data:
6628 Res = ActOnOpenMPTargetExitDataDirective(Clauses: ClausesWithImplicit, StartLoc,
6629 EndLoc, AStmt);
6630 break;
6631 case OMPD_taskloop:
6632 Res = ActOnOpenMPTaskLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6633 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6634 break;
6635 case OMPD_taskloop_simd:
6636 Res = ActOnOpenMPTaskLoopSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6637 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6638 break;
6639 case OMPD_master_taskloop:
6640 Res = ActOnOpenMPMasterTaskLoopDirective(
6641 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6642 break;
6643 case OMPD_masked_taskloop:
6644 Res = ActOnOpenMPMaskedTaskLoopDirective(
6645 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6646 break;
6647 case OMPD_master_taskloop_simd:
6648 Res = ActOnOpenMPMasterTaskLoopSimdDirective(
6649 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6650 break;
6651 case OMPD_masked_taskloop_simd:
6652 Res = ActOnOpenMPMaskedTaskLoopSimdDirective(
6653 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6654 break;
6655 case OMPD_parallel_master_taskloop:
6656 Res = ActOnOpenMPParallelMasterTaskLoopDirective(
6657 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6658 break;
6659 case OMPD_parallel_masked_taskloop:
6660 Res = ActOnOpenMPParallelMaskedTaskLoopDirective(
6661 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6662 break;
6663 case OMPD_parallel_master_taskloop_simd:
6664 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
6665 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6666 break;
6667 case OMPD_parallel_masked_taskloop_simd:
6668 Res = ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
6669 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6670 break;
6671 case OMPD_distribute:
6672 Res = ActOnOpenMPDistributeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6673 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6674 break;
6675 case OMPD_target_update:
6676 Res = ActOnOpenMPTargetUpdateDirective(Clauses: ClausesWithImplicit, StartLoc,
6677 EndLoc, AStmt);
6678 break;
6679 case OMPD_distribute_parallel_for:
6680 Res = ActOnOpenMPDistributeParallelForDirective(
6681 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6682 break;
6683 case OMPD_distribute_parallel_for_simd:
6684 Res = ActOnOpenMPDistributeParallelForSimdDirective(
6685 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6686 break;
6687 case OMPD_distribute_simd:
6688 Res = ActOnOpenMPDistributeSimdDirective(
6689 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6690 break;
6691 case OMPD_target_parallel_for_simd:
6692 Res = ActOnOpenMPTargetParallelForSimdDirective(
6693 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6694 break;
6695 case OMPD_target_simd:
6696 Res = ActOnOpenMPTargetSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6697 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6698 break;
6699 case OMPD_teams_distribute:
6700 Res = ActOnOpenMPTeamsDistributeDirective(
6701 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6702 break;
6703 case OMPD_teams_distribute_simd:
6704 Res = ActOnOpenMPTeamsDistributeSimdDirective(
6705 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6706 break;
6707 case OMPD_teams_distribute_parallel_for_simd:
6708 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6709 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6710 break;
6711 case OMPD_teams_distribute_parallel_for:
6712 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
6713 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6714 break;
6715 case OMPD_target_teams:
6716 Res = ActOnOpenMPTargetTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6717 EndLoc);
6718 break;
6719 case OMPD_target_teams_distribute:
6720 Res = ActOnOpenMPTargetTeamsDistributeDirective(
6721 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6722 break;
6723 case OMPD_target_teams_distribute_parallel_for:
6724 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6725 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6726 break;
6727 case OMPD_target_teams_distribute_parallel_for_simd:
6728 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6729 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6730 break;
6731 case OMPD_target_teams_distribute_simd:
6732 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
6733 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6734 break;
6735 case OMPD_interop:
6736 assert(AStmt == nullptr &&
6737 "No associated statement allowed for 'omp interop' directive");
6738 Res = ActOnOpenMPInteropDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6739 break;
6740 case OMPD_dispatch:
6741 Res = ActOnOpenMPDispatchDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6742 EndLoc);
6743 break;
6744 case OMPD_loop:
6745 Res = ActOnOpenMPGenericLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6746 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6747 break;
6748 case OMPD_teams_loop:
6749 Res = ActOnOpenMPTeamsGenericLoopDirective(
6750 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6751 break;
6752 case OMPD_target_teams_loop:
6753 Res = ActOnOpenMPTargetTeamsGenericLoopDirective(
6754 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6755 break;
6756 case OMPD_parallel_loop:
6757 Res = ActOnOpenMPParallelGenericLoopDirective(
6758 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6759 break;
6760 case OMPD_target_parallel_loop:
6761 Res = ActOnOpenMPTargetParallelGenericLoopDirective(
6762 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6763 break;
6764 case OMPD_declare_target:
6765 case OMPD_end_declare_target:
6766 case OMPD_threadprivate:
6767 case OMPD_allocate:
6768 case OMPD_declare_reduction:
6769 case OMPD_declare_mapper:
6770 case OMPD_declare_simd:
6771 case OMPD_requires:
6772 case OMPD_declare_variant:
6773 case OMPD_begin_declare_variant:
6774 case OMPD_end_declare_variant:
6775 llvm_unreachable("OpenMP Directive is not allowed");
6776 case OMPD_unknown:
6777 default:
6778 llvm_unreachable("Unknown OpenMP directive");
6779 }
6780
6781 ErrorFound = Res.isInvalid() || ErrorFound;
6782
6783 // Check variables in the clauses if default(none) or
6784 // default(firstprivate) was specified.
6785 if (DSAStack->getDefaultDSA() == DSA_none ||
6786 DSAStack->getDefaultDSA() == DSA_private ||
6787 DSAStack->getDefaultDSA() == DSA_firstprivate) {
6788 DSAAttrChecker DSAChecker(DSAStack, SemaRef, nullptr);
6789 for (OMPClause *C : Clauses) {
6790 switch (C->getClauseKind()) {
6791 case OMPC_num_threads:
6792 case OMPC_dist_schedule:
6793 // Do not analyze if no parent teams directive.
6794 if (isOpenMPTeamsDirective(DKind: Kind))
6795 break;
6796 continue;
6797 case OMPC_if:
6798 if (isOpenMPTeamsDirective(DKind: Kind) &&
6799 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_target)
6800 break;
6801 if (isOpenMPParallelDirective(DKind: Kind) &&
6802 isOpenMPTaskLoopDirective(DKind: Kind) &&
6803 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_parallel)
6804 break;
6805 continue;
6806 case OMPC_schedule:
6807 case OMPC_detach:
6808 break;
6809 case OMPC_grainsize:
6810 case OMPC_num_tasks:
6811 case OMPC_final:
6812 case OMPC_priority:
6813 case OMPC_novariants:
6814 case OMPC_nocontext:
6815 // Do not analyze if no parent parallel directive.
6816 if (isOpenMPParallelDirective(DKind: Kind))
6817 break;
6818 continue;
6819 case OMPC_ordered:
6820 case OMPC_device:
6821 case OMPC_num_teams:
6822 case OMPC_thread_limit:
6823 case OMPC_hint:
6824 case OMPC_collapse:
6825 case OMPC_safelen:
6826 case OMPC_simdlen:
6827 case OMPC_sizes:
6828 case OMPC_default:
6829 case OMPC_proc_bind:
6830 case OMPC_private:
6831 case OMPC_firstprivate:
6832 case OMPC_lastprivate:
6833 case OMPC_shared:
6834 case OMPC_reduction:
6835 case OMPC_task_reduction:
6836 case OMPC_in_reduction:
6837 case OMPC_linear:
6838 case OMPC_aligned:
6839 case OMPC_copyin:
6840 case OMPC_copyprivate:
6841 case OMPC_nowait:
6842 case OMPC_untied:
6843 case OMPC_mergeable:
6844 case OMPC_allocate:
6845 case OMPC_read:
6846 case OMPC_write:
6847 case OMPC_update:
6848 case OMPC_capture:
6849 case OMPC_compare:
6850 case OMPC_seq_cst:
6851 case OMPC_acq_rel:
6852 case OMPC_acquire:
6853 case OMPC_release:
6854 case OMPC_relaxed:
6855 case OMPC_depend:
6856 case OMPC_threads:
6857 case OMPC_simd:
6858 case OMPC_map:
6859 case OMPC_nogroup:
6860 case OMPC_defaultmap:
6861 case OMPC_to:
6862 case OMPC_from:
6863 case OMPC_use_device_ptr:
6864 case OMPC_use_device_addr:
6865 case OMPC_is_device_ptr:
6866 case OMPC_has_device_addr:
6867 case OMPC_nontemporal:
6868 case OMPC_order:
6869 case OMPC_destroy:
6870 case OMPC_inclusive:
6871 case OMPC_exclusive:
6872 case OMPC_uses_allocators:
6873 case OMPC_affinity:
6874 case OMPC_bind:
6875 case OMPC_filter:
6876 case OMPC_severity:
6877 case OMPC_message:
6878 continue;
6879 case OMPC_allocator:
6880 case OMPC_flush:
6881 case OMPC_depobj:
6882 case OMPC_threadprivate:
6883 case OMPC_groupprivate:
6884 case OMPC_uniform:
6885 case OMPC_unknown:
6886 case OMPC_unified_address:
6887 case OMPC_unified_shared_memory:
6888 case OMPC_reverse_offload:
6889 case OMPC_dynamic_allocators:
6890 case OMPC_atomic_default_mem_order:
6891 case OMPC_self_maps:
6892 case OMPC_device_type:
6893 case OMPC_match:
6894 case OMPC_when:
6895 case OMPC_at:
6896 default:
6897 llvm_unreachable("Unexpected clause");
6898 }
6899 for (Stmt *CC : C->children()) {
6900 if (CC)
6901 DSAChecker.Visit(S: CC);
6902 }
6903 }
6904 for (const auto &P : DSAChecker.getVarsWithInheritedDSA())
6905 VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
6906 }
6907 for (const auto &P : VarsWithInheritedDSA) {
6908 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(Val: P.getFirst()))
6909 continue;
6910 ErrorFound = true;
6911 if (DSAStack->getDefaultDSA() == DSA_none ||
6912 DSAStack->getDefaultDSA() == DSA_private ||
6913 DSAStack->getDefaultDSA() == DSA_firstprivate) {
6914 Diag(Loc: P.second->getExprLoc(), DiagID: diag::err_omp_no_dsa_for_variable)
6915 << P.first << P.second->getSourceRange();
6916 Diag(DSAStack->getDefaultDSALocation(), DiagID: diag::note_omp_default_dsa_none);
6917 } else if (getLangOpts().OpenMP >= 50) {
6918 Diag(Loc: P.second->getExprLoc(),
6919 DiagID: diag::err_omp_defaultmap_no_attr_for_variable)
6920 << P.first << P.second->getSourceRange();
6921 Diag(DSAStack->getDefaultDSALocation(),
6922 DiagID: diag::note_omp_defaultmap_attr_none);
6923 }
6924 }
6925
6926 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
6927 for (OpenMPDirectiveKind D : getLeafConstructsOrSelf(D: Kind)) {
6928 if (isAllowedClauseForDirective(D, C: OMPC_if, Version: getLangOpts().OpenMP))
6929 AllowedNameModifiers.push_back(Elt: D);
6930 }
6931 if (!AllowedNameModifiers.empty())
6932 ErrorFound = checkIfClauses(S&: SemaRef, Kind, Clauses, AllowedNameModifiers) ||
6933 ErrorFound;
6934
6935 if (ErrorFound)
6936 return StmtError();
6937
6938 if (!SemaRef.CurContext->isDependentContext() &&
6939 isOpenMPTargetExecutionDirective(DKind: Kind) &&
6940 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
6941 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
6942 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
6943 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
6944 // Register target to DSA Stack.
6945 DSAStack->addTargetDirLocation(LocStart: StartLoc);
6946 }
6947
6948 return Res;
6949}
6950
6951SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareSimdDirective(
6952 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
6953 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
6954 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
6955 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
6956 assert(Aligneds.size() == Alignments.size());
6957 assert(Linears.size() == LinModifiers.size());
6958 assert(Linears.size() == Steps.size());
6959 if (!DG || DG.get().isNull())
6960 return DeclGroupPtrTy();
6961
6962 const int SimdId = 0;
6963 if (!DG.get().isSingleDecl()) {
6964 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
6965 << SimdId;
6966 return DG;
6967 }
6968 Decl *ADecl = DG.get().getSingleDecl();
6969 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
6970 ADecl = FTD->getTemplatedDecl();
6971
6972 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
6973 if (!FD) {
6974 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected) << SimdId;
6975 return DeclGroupPtrTy();
6976 }
6977
6978 // OpenMP [2.8.2, declare simd construct, Description]
6979 // The parameter of the simdlen clause must be a constant positive integer
6980 // expression.
6981 ExprResult SL;
6982 if (Simdlen)
6983 SL = VerifyPositiveIntegerConstantInClause(Op: Simdlen, CKind: OMPC_simdlen);
6984 // OpenMP [2.8.2, declare simd construct, Description]
6985 // The special this pointer can be used as if was one of the arguments to the
6986 // function in any of the linear, aligned, or uniform clauses.
6987 // The uniform clause declares one or more arguments to have an invariant
6988 // value for all concurrent invocations of the function in the execution of a
6989 // single SIMD loop.
6990 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
6991 const Expr *UniformedLinearThis = nullptr;
6992 for (const Expr *E : Uniforms) {
6993 E = E->IgnoreParenImpCasts();
6994 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
6995 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl()))
6996 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6997 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
6998 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
6999 UniformedArgs.try_emplace(Key: PVD->getCanonicalDecl(), Args&: E);
7000 continue;
7001 }
7002 if (isa<CXXThisExpr>(Val: E)) {
7003 UniformedLinearThis = E;
7004 continue;
7005 }
7006 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7007 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7008 }
7009 // OpenMP [2.8.2, declare simd construct, Description]
7010 // The aligned clause declares that the object to which each list item points
7011 // is aligned to the number of bytes expressed in the optional parameter of
7012 // the aligned clause.
7013 // The special this pointer can be used as if was one of the arguments to the
7014 // function in any of the linear, aligned, or uniform clauses.
7015 // The type of list items appearing in the aligned clause must be array,
7016 // pointer, reference to array, or reference to pointer.
7017 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
7018 const Expr *AlignedThis = nullptr;
7019 for (const Expr *E : Aligneds) {
7020 E = E->IgnoreParenImpCasts();
7021 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
7022 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7023 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7024 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7025 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7026 ->getCanonicalDecl() == CanonPVD) {
7027 // OpenMP [2.8.1, simd construct, Restrictions]
7028 // A list-item cannot appear in more than one aligned clause.
7029 auto [It, Inserted] = AlignedArgs.try_emplace(Key: CanonPVD, Args&: E);
7030 if (!Inserted) {
7031 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
7032 << 1 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
7033 << E->getSourceRange();
7034 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7035 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7036 continue;
7037 }
7038 QualType QTy = PVD->getType()
7039 .getNonReferenceType()
7040 .getUnqualifiedType()
7041 .getCanonicalType();
7042 const Type *Ty = QTy.getTypePtrOrNull();
7043 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
7044 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_aligned_expected_array_or_ptr)
7045 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
7046 Diag(Loc: PVD->getLocation(), DiagID: diag::note_previous_decl) << PVD;
7047 }
7048 continue;
7049 }
7050 }
7051 if (isa<CXXThisExpr>(Val: E)) {
7052 if (AlignedThis) {
7053 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
7054 << 2 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
7055 << E->getSourceRange();
7056 Diag(Loc: AlignedThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7057 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7058 }
7059 AlignedThis = E;
7060 continue;
7061 }
7062 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7063 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7064 }
7065 // The optional parameter of the aligned clause, alignment, must be a constant
7066 // positive integer expression. If no optional parameter is specified,
7067 // implementation-defined default alignments for SIMD instructions on the
7068 // target platforms are assumed.
7069 SmallVector<const Expr *, 4> NewAligns;
7070 for (Expr *E : Alignments) {
7071 ExprResult Align;
7072 if (E)
7073 Align = VerifyPositiveIntegerConstantInClause(Op: E, CKind: OMPC_aligned);
7074 NewAligns.push_back(Elt: Align.get());
7075 }
7076 // OpenMP [2.8.2, declare simd construct, Description]
7077 // The linear clause declares one or more list items to be private to a SIMD
7078 // lane and to have a linear relationship with respect to the iteration space
7079 // of a loop.
7080 // The special this pointer can be used as if was one of the arguments to the
7081 // function in any of the linear, aligned, or uniform clauses.
7082 // When a linear-step expression is specified in a linear clause it must be
7083 // either a constant integer expression or an integer-typed parameter that is
7084 // specified in a uniform clause on the directive.
7085 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
7086 const bool IsUniformedThis = UniformedLinearThis != nullptr;
7087 auto MI = LinModifiers.begin();
7088 for (const Expr *E : Linears) {
7089 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
7090 ++MI;
7091 E = E->IgnoreParenImpCasts();
7092 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
7093 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7094 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7095 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7096 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7097 ->getCanonicalDecl() == CanonPVD) {
7098 // OpenMP [2.15.3.7, linear Clause, Restrictions]
7099 // A list-item cannot appear in more than one linear clause.
7100 if (auto It = LinearArgs.find(Val: CanonPVD); It != LinearArgs.end()) {
7101 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7102 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7103 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7104 << E->getSourceRange();
7105 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7106 << getOpenMPClauseNameForDiag(C: OMPC_linear);
7107 continue;
7108 }
7109 // Each argument can appear in at most one uniform or linear clause.
7110 if (auto It = UniformedArgs.find(Val: CanonPVD);
7111 It != UniformedArgs.end()) {
7112 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7113 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7114 << getOpenMPClauseNameForDiag(C: OMPC_uniform)
7115 << E->getSourceRange();
7116 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7117 << getOpenMPClauseNameForDiag(C: OMPC_uniform);
7118 continue;
7119 }
7120 LinearArgs[CanonPVD] = E;
7121 if (E->isValueDependent() || E->isTypeDependent() ||
7122 E->isInstantiationDependent() ||
7123 E->containsUnexpandedParameterPack())
7124 continue;
7125 (void)CheckOpenMPLinearDecl(D: CanonPVD, ELoc: E->getExprLoc(), LinKind,
7126 Type: PVD->getOriginalType(),
7127 /*IsDeclareSimd=*/true);
7128 continue;
7129 }
7130 }
7131 if (isa<CXXThisExpr>(Val: E)) {
7132 if (UniformedLinearThis) {
7133 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7134 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7135 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7136 : OMPC_linear)
7137 << E->getSourceRange();
7138 Diag(Loc: UniformedLinearThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7139 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7140 : OMPC_linear);
7141 continue;
7142 }
7143 UniformedLinearThis = E;
7144 if (E->isValueDependent() || E->isTypeDependent() ||
7145 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7146 continue;
7147 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, ELoc: E->getExprLoc(), LinKind,
7148 Type: E->getType(), /*IsDeclareSimd=*/true);
7149 continue;
7150 }
7151 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7152 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7153 }
7154 Expr *Step = nullptr;
7155 Expr *NewStep = nullptr;
7156 SmallVector<Expr *, 4> NewSteps;
7157 for (Expr *E : Steps) {
7158 // Skip the same step expression, it was checked already.
7159 if (Step == E || !E) {
7160 NewSteps.push_back(Elt: E ? NewStep : nullptr);
7161 continue;
7162 }
7163 Step = E;
7164 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Step))
7165 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7166 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7167 if (UniformedArgs.count(Val: CanonPVD) == 0) {
7168 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_uniform_param)
7169 << Step->getSourceRange();
7170 } else if (E->isValueDependent() || E->isTypeDependent() ||
7171 E->isInstantiationDependent() ||
7172 E->containsUnexpandedParameterPack() ||
7173 CanonPVD->getType()->hasIntegerRepresentation()) {
7174 NewSteps.push_back(Elt: Step);
7175 } else {
7176 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_int_param)
7177 << Step->getSourceRange();
7178 }
7179 continue;
7180 }
7181 NewStep = Step;
7182 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7183 !Step->isInstantiationDependent() &&
7184 !Step->containsUnexpandedParameterPack()) {
7185 NewStep = PerformOpenMPImplicitIntegerConversion(OpLoc: Step->getExprLoc(), Op: Step)
7186 .get();
7187 if (NewStep)
7188 NewStep = SemaRef
7189 .VerifyIntegerConstantExpression(
7190 E: NewStep, /*FIXME*/ CanFold: AllowFoldKind::Allow)
7191 .get();
7192 }
7193 NewSteps.push_back(Elt: NewStep);
7194 }
7195 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
7196 Ctx&: getASTContext(), BranchState: BS, Simdlen: SL.get(), Uniforms: const_cast<Expr **>(Uniforms.data()),
7197 UniformsSize: Uniforms.size(), Aligneds: const_cast<Expr **>(Aligneds.data()), AlignedsSize: Aligneds.size(),
7198 Alignments: const_cast<Expr **>(NewAligns.data()), AlignmentsSize: NewAligns.size(),
7199 Linears: const_cast<Expr **>(Linears.data()), LinearsSize: Linears.size(),
7200 Modifiers: const_cast<unsigned *>(LinModifiers.data()), ModifiersSize: LinModifiers.size(),
7201 Steps: NewSteps.data(), StepsSize: NewSteps.size(), Range: SR);
7202 ADecl->addAttr(A: NewAttr);
7203 return DG;
7204}
7205
7206StmtResult SemaOpenMP::ActOnOpenMPInformationalDirective(
7207 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
7208 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7209 SourceLocation EndLoc) {
7210 assert(isOpenMPInformationalDirective(Kind) &&
7211 "Unexpected directive category");
7212
7213 StmtResult Res = StmtError();
7214
7215 switch (Kind) {
7216 case OMPD_assume:
7217 Res = ActOnOpenMPAssumeDirective(Clauses, AStmt, StartLoc, EndLoc);
7218 break;
7219 default:
7220 llvm_unreachable("Unknown OpenMP directive");
7221 }
7222
7223 return Res;
7224}
7225
7226static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto,
7227 QualType NewType) {
7228 assert(NewType->isFunctionProtoType() &&
7229 "Expected function type with prototype.");
7230 assert(FD->getType()->isFunctionNoProtoType() &&
7231 "Expected function with type with no prototype.");
7232 assert(FDWithProto->getType()->isFunctionProtoType() &&
7233 "Expected function with prototype.");
7234 // Synthesize parameters with the same types.
7235 FD->setType(NewType);
7236 SmallVector<ParmVarDecl *, 16> Params;
7237 for (const ParmVarDecl *P : FDWithProto->parameters()) {
7238 auto *Param = ParmVarDecl::Create(C&: S.getASTContext(), DC: FD, StartLoc: SourceLocation(),
7239 IdLoc: SourceLocation(), Id: nullptr, T: P->getType(),
7240 /*TInfo=*/nullptr, S: SC_None, DefArg: nullptr);
7241 Param->setScopeInfo(scopeDepth: 0, parameterIndex: Params.size());
7242 Param->setImplicit();
7243 Params.push_back(Elt: Param);
7244 }
7245
7246 FD->setParams(Params);
7247}
7248
7249void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) {
7250 if (D->isInvalidDecl())
7251 return;
7252 FunctionDecl *FD = nullptr;
7253 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7254 FD = UTemplDecl->getTemplatedDecl();
7255 else
7256 FD = cast<FunctionDecl>(Val: D);
7257 assert(FD && "Expected a function declaration!");
7258
7259 // If we are instantiating templates we do *not* apply scoped assumptions but
7260 // only global ones. We apply scoped assumption to the template definition
7261 // though.
7262 if (!SemaRef.inTemplateInstantiation()) {
7263 for (OMPAssumeAttr *AA : OMPAssumeScoped)
7264 FD->addAttr(A: AA);
7265 }
7266 for (OMPAssumeAttr *AA : OMPAssumeGlobal)
7267 FD->addAttr(A: AA);
7268}
7269
7270SemaOpenMP::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI)
7271 : TI(&TI), NameSuffix(TI.getMangledName()) {}
7272
7273void SemaOpenMP::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
7274 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists,
7275 SmallVectorImpl<FunctionDecl *> &Bases) {
7276 if (!D.getIdentifier())
7277 return;
7278
7279 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7280
7281 // Template specialization is an extension, check if we do it.
7282 bool IsTemplated = !TemplateParamLists.empty();
7283 if (IsTemplated &&
7284 !DVScope.TI->isExtensionActive(
7285 TP: llvm::omp::TraitProperty::implementation_extension_allow_templates))
7286 return;
7287
7288 const IdentifierInfo *BaseII = D.getIdentifier();
7289 LookupResult Lookup(SemaRef, DeclarationName(BaseII), D.getIdentifierLoc(),
7290 Sema::LookupOrdinaryName);
7291 SemaRef.LookupParsedName(R&: Lookup, S, SS: &D.getCXXScopeSpec(),
7292 /*ObjectType=*/QualType());
7293
7294 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
7295 QualType FType = TInfo->getType();
7296
7297 bool IsConstexpr =
7298 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr;
7299 bool IsConsteval =
7300 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval;
7301
7302 for (auto *Candidate : Lookup) {
7303 auto *CandidateDecl = Candidate->getUnderlyingDecl();
7304 FunctionDecl *UDecl = nullptr;
7305 if (IsTemplated && isa<FunctionTemplateDecl>(Val: CandidateDecl)) {
7306 auto *FTD = cast<FunctionTemplateDecl>(Val: CandidateDecl);
7307 // FIXME: Should this compare the template parameter lists on all levels?
7308 if (SemaRef.Context.isSameTemplateParameterList(
7309 X: FTD->getTemplateParameters(), Y: TemplateParamLists.back()))
7310 UDecl = FTD->getTemplatedDecl();
7311 } else if (!IsTemplated)
7312 UDecl = dyn_cast<FunctionDecl>(Val: CandidateDecl);
7313 if (!UDecl)
7314 continue;
7315
7316 // Don't specialize constexpr/consteval functions with
7317 // non-constexpr/consteval functions.
7318 if (UDecl->isConstexpr() && !IsConstexpr)
7319 continue;
7320 if (UDecl->isConsteval() && !IsConsteval)
7321 continue;
7322
7323 QualType UDeclTy = UDecl->getType();
7324 if (!UDeclTy->isDependentType()) {
7325 QualType NewType = getASTContext().mergeFunctionTypes(
7326 FType, UDeclTy, /*OfBlockPointer=*/false,
7327 /*Unqualified=*/false, /*AllowCXX=*/true);
7328 if (NewType.isNull())
7329 continue;
7330 }
7331
7332 // Found a base!
7333 Bases.push_back(Elt: UDecl);
7334 }
7335
7336 bool UseImplicitBase = !DVScope.TI->isExtensionActive(
7337 TP: llvm::omp::TraitProperty::implementation_extension_disable_implicit_base);
7338 // If no base was found we create a declaration that we use as base.
7339 if (Bases.empty() && UseImplicitBase) {
7340 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration);
7341 Decl *BaseD = SemaRef.HandleDeclarator(S, D, TemplateParameterLists: TemplateParamLists);
7342 BaseD->setImplicit(true);
7343 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(Val: BaseD))
7344 Bases.push_back(Elt: BaseTemplD->getTemplatedDecl());
7345 else
7346 Bases.push_back(Elt: cast<FunctionDecl>(Val: BaseD));
7347 }
7348
7349 std::string MangledName;
7350 MangledName += D.getIdentifier()->getName();
7351 MangledName += getOpenMPVariantManglingSeparatorStr();
7352 MangledName += DVScope.NameSuffix;
7353 IdentifierInfo &VariantII = getASTContext().Idents.get(Name: MangledName);
7354
7355 VariantII.setMangledOpenMPVariantName(true);
7356 D.SetIdentifier(Id: &VariantII, IdLoc: D.getBeginLoc());
7357}
7358
7359void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(
7360 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) {
7361 // Do not mark function as is used to prevent its emission if this is the
7362 // only place where it is used.
7363 EnterExpressionEvaluationContext Unevaluated(
7364 SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);
7365
7366 FunctionDecl *FD = nullptr;
7367 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7368 FD = UTemplDecl->getTemplatedDecl();
7369 else
7370 FD = cast<FunctionDecl>(Val: D);
7371 auto *VariantFuncRef = DeclRefExpr::Create(
7372 Context: getASTContext(), QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: FD,
7373 /*RefersToEnclosingVariableOrCapture=*/false,
7374 /*NameLoc=*/FD->getLocation(), T: FD->getType(), VK: ExprValueKind::VK_PRValue);
7375
7376 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7377 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit(
7378 Ctx&: getASTContext(), VariantFuncRef, TraitInfos: DVScope.TI,
7379 /*NothingArgs=*/AdjustArgsNothing: nullptr, /*NothingArgsSize=*/AdjustArgsNothingSize: 0,
7380 /*NeedDevicePtrArgs=*/AdjustArgsNeedDevicePtr: nullptr, /*NeedDevicePtrArgsSize=*/AdjustArgsNeedDevicePtrSize: 0,
7381 /*NeedDeviceAddrArgs=*/AdjustArgsNeedDeviceAddr: nullptr, /*NeedDeviceAddrArgsSize=*/AdjustArgsNeedDeviceAddrSize: 0,
7382 /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0);
7383 for (FunctionDecl *BaseFD : Bases)
7384 BaseFD->addAttr(A: OMPDeclareVariantA);
7385}
7386
7387ExprResult SemaOpenMP::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
7388 SourceLocation LParenLoc,
7389 MultiExprArg ArgExprs,
7390 SourceLocation RParenLoc,
7391 Expr *ExecConfig) {
7392 // The common case is a regular call we do not want to specialize at all. Try
7393 // to make that case fast by bailing early.
7394 CallExpr *CE = dyn_cast<CallExpr>(Val: Call.get());
7395 if (!CE)
7396 return Call;
7397
7398 FunctionDecl *CalleeFnDecl = CE->getDirectCallee();
7399
7400 // Mark indirect calls inside target regions, to allow for insertion of
7401 // __llvm_omp_indirect_call_lookup calls during codegen.
7402 if (!CalleeFnDecl) {
7403 if (isInOpenMPTargetExecutionDirective()) {
7404 Expr *E = CE->getCallee()->IgnoreParenImpCasts();
7405 DeclRefExpr *DRE = nullptr;
7406 while (E) {
7407 if ((DRE = dyn_cast<DeclRefExpr>(Val: E)))
7408 break;
7409 if (auto *ME = dyn_cast<MemberExpr>(Val: E))
7410 E = ME->getBase()->IgnoreParenImpCasts();
7411 else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E))
7412 E = ASE->getBase()->IgnoreParenImpCasts();
7413 else
7414 break;
7415 }
7416 VarDecl *VD = DRE ? dyn_cast<VarDecl>(Val: DRE->getDecl()) : nullptr;
7417 if (VD && !VD->hasAttr<OMPTargetIndirectCallAttr>()) {
7418 VD->addAttr(A: OMPTargetIndirectCallAttr::CreateImplicit(Ctx&: getASTContext()));
7419 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
7420 ML->DeclarationMarkedOpenMPIndirectCall(D: VD);
7421 }
7422 }
7423
7424 return Call;
7425 }
7426
7427 if (getLangOpts().OpenMP >= 50 && getLangOpts().OpenMP <= 60 &&
7428 CalleeFnDecl->getIdentifier() &&
7429 CalleeFnDecl->getName().starts_with_insensitive(Prefix: "omp_")) {
7430 // checking for any calls inside an Order region
7431 if (Scope && Scope->isOpenMPOrderClauseScope())
7432 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_call_to_omp_runtime_api);
7433 }
7434
7435 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>())
7436 return Call;
7437
7438 ASTContext &Context = getASTContext();
7439 std::function<void(StringRef)> DiagUnknownTrait = [this,
7440 CE](StringRef ISATrait) {
7441 // TODO Track the selector locations in a way that is accessible here to
7442 // improve the diagnostic location.
7443 Diag(Loc: CE->getBeginLoc(), DiagID: diag::warn_unknown_declare_variant_isa_trait)
7444 << ISATrait;
7445 };
7446 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait),
7447 SemaRef.getCurFunctionDecl(),
7448 DSAStack->getConstructTraits(), getOpenMPDeviceNum());
7449
7450 QualType CalleeFnType = CalleeFnDecl->getType();
7451
7452 SmallVector<Expr *, 4> Exprs;
7453 SmallVector<VariantMatchInfo, 4> VMIs;
7454 while (CalleeFnDecl) {
7455 for (OMPDeclareVariantAttr *A :
7456 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) {
7457 Expr *VariantRef = A->getVariantFuncRef();
7458
7459 VariantMatchInfo VMI;
7460 OMPTraitInfo &TI = A->getTraitInfo();
7461 TI.getAsVariantMatchInfo(ASTCtx&: Context, VMI);
7462 if (!isVariantApplicableInContext(VMI, Ctx: OMPCtx,
7463 /*DeviceSetOnly=*/DeviceOrImplementationSetOnly: false))
7464 continue;
7465
7466 VMIs.push_back(Elt: VMI);
7467 Exprs.push_back(Elt: VariantRef);
7468 }
7469
7470 CalleeFnDecl = CalleeFnDecl->getPreviousDecl();
7471 }
7472
7473 ExprResult NewCall;
7474 do {
7475 int BestIdx = getBestVariantMatchForContext(VMIs, Ctx: OMPCtx);
7476 if (BestIdx < 0)
7477 return Call;
7478 Expr *BestExpr = cast<DeclRefExpr>(Val: Exprs[BestIdx]);
7479 Decl *BestDecl = cast<DeclRefExpr>(Val: BestExpr)->getDecl();
7480
7481 {
7482 // Try to build a (member) call expression for the current best applicable
7483 // variant expression. We allow this to fail in which case we continue
7484 // with the next best variant expression. The fail case is part of the
7485 // implementation defined behavior in the OpenMP standard when it talks
7486 // about what differences in the function prototypes: "Any differences
7487 // that the specific OpenMP context requires in the prototype of the
7488 // variant from the base function prototype are implementation defined."
7489 // This wording is there to allow the specialized variant to have a
7490 // different type than the base function. This is intended and OK but if
7491 // we cannot create a call the difference is not in the "implementation
7492 // defined range" we allow.
7493 Sema::TentativeAnalysisScope Trap(SemaRef);
7494
7495 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(Val: BestDecl)) {
7496 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(Val: CE);
7497 BestExpr = MemberExpr::CreateImplicit(
7498 C: Context, Base: MemberCall->getImplicitObjectArgument(),
7499 /*IsArrow=*/false, MemberDecl: SpecializedMethod, T: Context.BoundMemberTy,
7500 VK: MemberCall->getValueKind(), OK: MemberCall->getObjectKind());
7501 }
7502 NewCall = SemaRef.BuildCallExpr(S: Scope, Fn: BestExpr, LParenLoc, ArgExprs,
7503 RParenLoc, ExecConfig);
7504 if (NewCall.isUsable()) {
7505 if (CallExpr *NCE = dyn_cast<CallExpr>(Val: NewCall.get())) {
7506 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee();
7507 QualType NewType = getASTContext().mergeFunctionTypes(
7508 CalleeFnType, NewCalleeFnDecl->getType(),
7509 /*OfBlockPointer=*/false,
7510 /*Unqualified=*/false, /*AllowCXX=*/true);
7511 if (!NewType.isNull())
7512 break;
7513 // Don't use the call if the function type was not compatible.
7514 NewCall = nullptr;
7515 }
7516 }
7517 }
7518
7519 VMIs.erase(CI: VMIs.begin() + BestIdx);
7520 Exprs.erase(CI: Exprs.begin() + BestIdx);
7521 } while (!VMIs.empty());
7522
7523 if (!NewCall.isUsable())
7524 return Call;
7525 return PseudoObjectExpr::Create(Context: getASTContext(), syntactic: CE, semantic: {NewCall.get()}, resultIndex: 0);
7526}
7527
7528std::optional<std::pair<FunctionDecl *, Expr *>>
7529SemaOpenMP::checkOpenMPDeclareVariantFunction(SemaOpenMP::DeclGroupPtrTy DG,
7530 Expr *VariantRef,
7531 OMPTraitInfo &TI,
7532 unsigned NumAppendArgs,
7533 SourceRange SR) {
7534 ASTContext &Context = getASTContext();
7535 if (!DG || DG.get().isNull())
7536 return std::nullopt;
7537
7538 const int VariantId = 1;
7539 // Must be applied only to single decl.
7540 if (!DG.get().isSingleDecl()) {
7541 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
7542 << VariantId << SR;
7543 return std::nullopt;
7544 }
7545 Decl *ADecl = DG.get().getSingleDecl();
7546 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
7547 ADecl = FTD->getTemplatedDecl();
7548
7549 // Decl must be a function.
7550 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
7551 if (!FD) {
7552 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected)
7553 << VariantId << SR;
7554 return std::nullopt;
7555 }
7556
7557 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
7558 // The 'target' attribute needs to be separately checked because it does
7559 // not always signify a multiversion function declaration.
7560 return FD->isMultiVersion() || FD->hasAttr<TargetAttr>();
7561 };
7562 // OpenMP is not compatible with multiversion function attributes.
7563 if (HasMultiVersionAttributes(FD)) {
7564 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_incompat_attributes)
7565 << SR;
7566 return std::nullopt;
7567 }
7568
7569 // Allow #pragma omp declare variant only if the function is not used.
7570 if (FD->isUsed(CheckUsedAttr: false))
7571 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_used)
7572 << FD->getLocation();
7573
7574 // Check if the function was emitted already.
7575 const FunctionDecl *Definition;
7576 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
7577 (getLangOpts().EmitAllDecls || Context.DeclMustBeEmitted(D: Definition)))
7578 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_emitted)
7579 << FD->getLocation();
7580
7581 // The VariantRef must point to function.
7582 if (!VariantRef) {
7583 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_function_expected) << VariantId;
7584 return std::nullopt;
7585 }
7586
7587 auto ShouldDelayChecks = [](Expr *&E, bool) {
7588 return E && (E->isTypeDependent() || E->isValueDependent() ||
7589 E->containsUnexpandedParameterPack() ||
7590 E->isInstantiationDependent());
7591 };
7592 // Do not check templates, wait until instantiation.
7593 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) ||
7594 TI.anyScoreOrCondition(Cond: ShouldDelayChecks))
7595 return std::make_pair(x&: FD, y&: VariantRef);
7596
7597 // Deal with non-constant score and user condition expressions.
7598 auto HandleNonConstantScoresAndConditions = [this](Expr *&E,
7599 bool IsScore) -> bool {
7600 if (!E || E->isIntegerConstantExpr(Ctx: getASTContext()))
7601 return false;
7602
7603 if (IsScore) {
7604 // We warn on non-constant scores and pretend they were not present.
7605 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_declare_variant_score_not_constant)
7606 << E;
7607 E = nullptr;
7608 } else {
7609 // We could replace a non-constant user condition with "false" but we
7610 // will soon need to handle these anyway for the dynamic version of
7611 // OpenMP context selectors.
7612 Diag(Loc: E->getExprLoc(),
7613 DiagID: diag::err_omp_declare_variant_user_condition_not_constant)
7614 << E;
7615 }
7616 return true;
7617 };
7618 if (TI.anyScoreOrCondition(Cond: HandleNonConstantScoresAndConditions))
7619 return std::nullopt;
7620
7621 QualType AdjustedFnType = FD->getType();
7622 if (NumAppendArgs) {
7623 const auto *PTy = AdjustedFnType->getAsAdjusted<FunctionProtoType>();
7624 if (!PTy) {
7625 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_prototype_required)
7626 << SR;
7627 return std::nullopt;
7628 }
7629 // Adjust the function type to account for an extra omp_interop_t for each
7630 // specified in the append_args clause.
7631 const TypeDecl *TD = nullptr;
7632 LookupResult Result(SemaRef, &Context.Idents.get(Name: "omp_interop_t"),
7633 SR.getBegin(), Sema::LookupOrdinaryName);
7634 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
7635 NamedDecl *ND = Result.getFoundDecl();
7636 TD = dyn_cast_or_null<TypeDecl>(Val: ND);
7637 }
7638 if (!TD) {
7639 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_interop_type_not_found) << SR;
7640 return std::nullopt;
7641 }
7642 QualType InteropType =
7643 Context.getTypeDeclType(Keyword: ElaboratedTypeKeyword::None,
7644 /*Qualifier=*/std::nullopt, Decl: TD);
7645 if (PTy->isVariadic()) {
7646 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_append_args_with_varargs) << SR;
7647 return std::nullopt;
7648 }
7649 llvm::SmallVector<QualType, 8> Params;
7650 Params.append(in_start: PTy->param_type_begin(), in_end: PTy->param_type_end());
7651 Params.insert(I: Params.end(), NumToInsert: NumAppendArgs, Elt: InteropType);
7652 AdjustedFnType = Context.getFunctionType(ResultTy: PTy->getReturnType(), Args: Params,
7653 EPI: PTy->getExtProtoInfo());
7654 }
7655
7656 // Convert VariantRef expression to the type of the original function to
7657 // resolve possible conflicts.
7658 ExprResult VariantRefCast = VariantRef;
7659 if (getLangOpts().CPlusPlus) {
7660 QualType FnPtrType;
7661 auto *Method = dyn_cast<CXXMethodDecl>(Val: FD);
7662 if (Method && !Method->isStatic()) {
7663 FnPtrType = Context.getMemberPointerType(
7664 T: AdjustedFnType, /*Qualifier=*/std::nullopt, Cls: Method->getParent());
7665 ExprResult ER;
7666 {
7667 // Build addr_of unary op to correctly handle type checks for member
7668 // functions.
7669 Sema::TentativeAnalysisScope Trap(SemaRef);
7670 ER = SemaRef.CreateBuiltinUnaryOp(OpLoc: VariantRef->getBeginLoc(), Opc: UO_AddrOf,
7671 InputExpr: VariantRef);
7672 }
7673 if (!ER.isUsable()) {
7674 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7675 << VariantId << VariantRef->getSourceRange();
7676 return std::nullopt;
7677 }
7678 VariantRef = ER.get();
7679 } else {
7680 FnPtrType = Context.getPointerType(T: AdjustedFnType);
7681 }
7682 QualType VarianPtrType = Context.getPointerType(T: VariantRef->getType());
7683 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) {
7684 ImplicitConversionSequence ICS = SemaRef.TryImplicitConversion(
7685 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
7686 /*SuppressUserConversions=*/false, AllowExplicit: Sema::AllowedExplicit::None,
7687 /*InOverloadResolution=*/false,
7688 /*CStyle=*/false,
7689 /*AllowObjCWritebackConversion=*/false);
7690 if (ICS.isFailure()) {
7691 Diag(Loc: VariantRef->getExprLoc(),
7692 DiagID: diag::err_omp_declare_variant_incompat_types)
7693 << VariantRef->getType()
7694 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType())
7695 << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange();
7696 return std::nullopt;
7697 }
7698 VariantRefCast = SemaRef.PerformImplicitConversion(
7699 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
7700 Action: AssignmentAction::Converting);
7701 if (!VariantRefCast.isUsable())
7702 return std::nullopt;
7703 }
7704 // Drop previously built artificial addr_of unary op for member functions.
7705 if (Method && !Method->isStatic()) {
7706 Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
7707 if (auto *UO = dyn_cast<UnaryOperator>(
7708 Val: PossibleAddrOfVariantRef->IgnoreImplicit()))
7709 VariantRefCast = UO->getSubExpr();
7710 }
7711 }
7712
7713 ExprResult ER = SemaRef.CheckPlaceholderExpr(E: VariantRefCast.get());
7714 if (!ER.isUsable() ||
7715 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
7716 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7717 << VariantId << VariantRef->getSourceRange();
7718 return std::nullopt;
7719 }
7720
7721 // The VariantRef must point to function.
7722 auto *DRE = dyn_cast<DeclRefExpr>(Val: ER.get()->IgnoreParenImpCasts());
7723 if (!DRE) {
7724 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7725 << VariantId << VariantRef->getSourceRange();
7726 return std::nullopt;
7727 }
7728 auto *NewFD = dyn_cast_or_null<FunctionDecl>(Val: DRE->getDecl());
7729 if (!NewFD) {
7730 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7731 << VariantId << VariantRef->getSourceRange();
7732 return std::nullopt;
7733 }
7734
7735 if (FD->getCanonicalDecl() == NewFD->getCanonicalDecl()) {
7736 Diag(Loc: VariantRef->getExprLoc(),
7737 DiagID: diag::err_omp_declare_variant_same_base_function)
7738 << VariantRef->getSourceRange();
7739 return std::nullopt;
7740 }
7741
7742 // Check if function types are compatible in C.
7743 if (!getLangOpts().CPlusPlus) {
7744 QualType NewType =
7745 Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType());
7746 if (NewType.isNull()) {
7747 Diag(Loc: VariantRef->getExprLoc(),
7748 DiagID: diag::err_omp_declare_variant_incompat_types)
7749 << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0)
7750 << VariantRef->getSourceRange();
7751 return std::nullopt;
7752 }
7753 if (NewType->isFunctionProtoType()) {
7754 if (FD->getType()->isFunctionNoProtoType())
7755 setPrototype(S&: SemaRef, FD, FDWithProto: NewFD, NewType);
7756 else if (NewFD->getType()->isFunctionNoProtoType())
7757 setPrototype(S&: SemaRef, FD: NewFD, FDWithProto: FD, NewType);
7758 }
7759 }
7760
7761 // Check if variant function is not marked with declare variant directive.
7762 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
7763 Diag(Loc: VariantRef->getExprLoc(),
7764 DiagID: diag::warn_omp_declare_variant_marked_as_declare_variant)
7765 << VariantRef->getSourceRange();
7766 SourceRange SR =
7767 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
7768 Diag(Loc: SR.getBegin(), DiagID: diag::note_omp_marked_declare_variant_here) << SR;
7769 return std::nullopt;
7770 }
7771
7772 enum DoesntSupport {
7773 VirtFuncs = 1,
7774 Constructors = 3,
7775 Destructors = 4,
7776 DeletedFuncs = 5,
7777 DefaultedFuncs = 6,
7778 ConstexprFuncs = 7,
7779 ConstevalFuncs = 8,
7780 };
7781 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(Val: FD)) {
7782 if (CXXFD->isVirtual()) {
7783 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7784 << VirtFuncs;
7785 return std::nullopt;
7786 }
7787
7788 if (isa<CXXConstructorDecl>(Val: FD)) {
7789 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7790 << Constructors;
7791 return std::nullopt;
7792 }
7793
7794 if (isa<CXXDestructorDecl>(Val: FD)) {
7795 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7796 << Destructors;
7797 return std::nullopt;
7798 }
7799 }
7800
7801 if (FD->isDeleted()) {
7802 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7803 << DeletedFuncs;
7804 return std::nullopt;
7805 }
7806
7807 if (FD->isDefaulted()) {
7808 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7809 << DefaultedFuncs;
7810 return std::nullopt;
7811 }
7812
7813 if (FD->isConstexpr()) {
7814 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7815 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
7816 return std::nullopt;
7817 }
7818
7819 // Check general compatibility.
7820 if (SemaRef.areMultiversionVariantFunctionsCompatible(
7821 OldFD: FD, NewFD, NoProtoDiagID: PartialDiagnostic::NullDiagnostic(),
7822 NoteCausedDiagIDAt: PartialDiagnosticAt(SourceLocation(),
7823 PartialDiagnostic::NullDiagnostic()),
7824 NoSupportDiagIDAt: PartialDiagnosticAt(
7825 VariantRef->getExprLoc(),
7826 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_doesnt_support)),
7827 DiffDiagIDAt: PartialDiagnosticAt(VariantRef->getExprLoc(),
7828 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_diff)
7829 << FD->getLocation()),
7830 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
7831 /*CLinkageMayDiffer=*/true))
7832 return std::nullopt;
7833 return std::make_pair(x&: FD, y: cast<Expr>(Val: DRE));
7834}
7835
7836void SemaOpenMP::ActOnOpenMPDeclareVariantDirective(
7837 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI,
7838 ArrayRef<Expr *> AdjustArgsNothing,
7839 ArrayRef<Expr *> AdjustArgsNeedDevicePtr,
7840 ArrayRef<Expr *> AdjustArgsNeedDeviceAddr,
7841 ArrayRef<OMPInteropInfo> AppendArgs, SourceLocation AdjustArgsLoc,
7842 SourceLocation AppendArgsLoc, SourceRange SR) {
7843
7844 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7845 // An adjust_args clause or append_args clause can only be specified if the
7846 // dispatch selector of the construct selector set appears in the match
7847 // clause.
7848
7849 SmallVector<Expr *, 8> AllAdjustArgs;
7850 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNothing);
7851 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNeedDevicePtr);
7852 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNeedDeviceAddr);
7853
7854 if (!AllAdjustArgs.empty() || !AppendArgs.empty()) {
7855 VariantMatchInfo VMI;
7856 TI.getAsVariantMatchInfo(ASTCtx&: getASTContext(), VMI);
7857 if (!llvm::is_contained(
7858 Range&: VMI.ConstructTraits,
7859 Element: llvm::omp::TraitProperty::construct_dispatch_dispatch)) {
7860 if (!AllAdjustArgs.empty())
7861 Diag(Loc: AdjustArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
7862 << getOpenMPClauseNameForDiag(C: OMPC_adjust_args);
7863 if (!AppendArgs.empty())
7864 Diag(Loc: AppendArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
7865 << getOpenMPClauseNameForDiag(C: OMPC_append_args);
7866 return;
7867 }
7868 }
7869
7870 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7871 // Each argument can only appear in a single adjust_args clause for each
7872 // declare variant directive.
7873 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars;
7874
7875 for (Expr *E : AllAdjustArgs) {
7876 E = E->IgnoreParenImpCasts();
7877 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
7878 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7879 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7880 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7881 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7882 ->getCanonicalDecl() == CanonPVD) {
7883 // It's a parameter of the function, check duplicates.
7884 if (!AdjustVars.insert(Ptr: CanonPVD).second) {
7885 Diag(Loc: DRE->getLocation(), DiagID: diag::err_omp_adjust_arg_multiple_clauses)
7886 << PVD;
7887 return;
7888 }
7889 continue;
7890 }
7891 }
7892 }
7893 // Anything that is not a function parameter is an error.
7894 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause) << FD << 0;
7895 return;
7896 }
7897
7898 // OpenMP 6.0 [9.6.2 (page 332, line 31-33, adjust_args clause, Restrictions]
7899 // If the `need_device_addr` adjust-op modifier is present, each list item
7900 // that appears in the clause must refer to an argument in the declaration of
7901 // the function variant that has a reference type
7902 if (getLangOpts().OpenMP >= 60) {
7903 for (Expr *E : AdjustArgsNeedDeviceAddr) {
7904 E = E->IgnoreParenImpCasts();
7905 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
7906 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl())) {
7907 if (!VD->getType()->isReferenceType())
7908 Diag(Loc: E->getExprLoc(),
7909 DiagID: diag::err_omp_non_by_ref_need_device_addr_modifier_argument);
7910 }
7911 }
7912 }
7913 }
7914
7915 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
7916 Ctx&: getASTContext(), VariantFuncRef: VariantRef, TraitInfos: &TI,
7917 AdjustArgsNothing: const_cast<Expr **>(AdjustArgsNothing.data()), AdjustArgsNothingSize: AdjustArgsNothing.size(),
7918 AdjustArgsNeedDevicePtr: const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()),
7919 AdjustArgsNeedDevicePtrSize: AdjustArgsNeedDevicePtr.size(),
7920 AdjustArgsNeedDeviceAddr: const_cast<Expr **>(AdjustArgsNeedDeviceAddr.data()),
7921 AdjustArgsNeedDeviceAddrSize: AdjustArgsNeedDeviceAddr.size(),
7922 AppendArgs: const_cast<OMPInteropInfo *>(AppendArgs.data()), AppendArgsSize: AppendArgs.size(), Range: SR);
7923 FD->addAttr(A: NewAttr);
7924}
7925
7926static CapturedStmt *
7927setBranchProtectedScope(Sema &SemaRef, OpenMPDirectiveKind DKind, Stmt *AStmt) {
7928 auto *CS = dyn_cast<CapturedStmt>(Val: AStmt);
7929 assert(CS && "Captured statement expected");
7930 // 1.2.2 OpenMP Language Terminology
7931 // Structured block - An executable statement with a single entry at the
7932 // top and a single exit at the bottom.
7933 // The point of exit cannot be a branch out of the structured block.
7934 // longjmp() and throw() must not violate the entry/exit criteria.
7935 CS->getCapturedDecl()->setNothrow();
7936
7937 for (int ThisCaptureLevel = SemaRef.OpenMP().getOpenMPCaptureLevels(DKind);
7938 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7939 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
7940 // 1.2.2 OpenMP Language Terminology
7941 // Structured block - An executable statement with a single entry at the
7942 // top and a single exit at the bottom.
7943 // The point of exit cannot be a branch out of the structured block.
7944 // longjmp() and throw() must not violate the entry/exit criteria.
7945 CS->getCapturedDecl()->setNothrow();
7946 }
7947 SemaRef.setFunctionHasBranchProtectedScope();
7948 return CS;
7949}
7950
7951StmtResult
7952SemaOpenMP::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
7953 Stmt *AStmt, SourceLocation StartLoc,
7954 SourceLocation EndLoc) {
7955 if (!AStmt)
7956 return StmtError();
7957
7958 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel, AStmt);
7959
7960 return OMPParallelDirective::Create(
7961 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
7962 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
7963}
7964
7965namespace {
7966/// Iteration space of a single for loop.
7967struct LoopIterationSpace final {
7968 /// True if the condition operator is the strict compare operator (<, > or
7969 /// !=).
7970 bool IsStrictCompare = false;
7971 /// Condition of the loop.
7972 Expr *PreCond = nullptr;
7973 /// This expression calculates the number of iterations in the loop.
7974 /// It is always possible to calculate it before starting the loop.
7975 Expr *NumIterations = nullptr;
7976 /// The loop counter variable.
7977 Expr *CounterVar = nullptr;
7978 /// Private loop counter variable.
7979 Expr *PrivateCounterVar = nullptr;
7980 /// This is initializer for the initial value of #CounterVar.
7981 Expr *CounterInit = nullptr;
7982 /// This is step for the #CounterVar used to generate its update:
7983 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
7984 Expr *CounterStep = nullptr;
7985 /// Should step be subtracted?
7986 bool Subtract = false;
7987 /// Source range of the loop init.
7988 SourceRange InitSrcRange;
7989 /// Source range of the loop condition.
7990 SourceRange CondSrcRange;
7991 /// Source range of the loop increment.
7992 SourceRange IncSrcRange;
7993 /// Minimum value that can have the loop control variable. Used to support
7994 /// non-rectangular loops. Applied only for LCV with the non-iterator types,
7995 /// since only such variables can be used in non-loop invariant expressions.
7996 Expr *MinValue = nullptr;
7997 /// Maximum value that can have the loop control variable. Used to support
7998 /// non-rectangular loops. Applied only for LCV with the non-iterator type,
7999 /// since only such variables can be used in non-loop invariant expressions.
8000 Expr *MaxValue = nullptr;
8001 /// true, if the lower bound depends on the outer loop control var.
8002 bool IsNonRectangularLB = false;
8003 /// true, if the upper bound depends on the outer loop control var.
8004 bool IsNonRectangularUB = false;
8005 /// Index of the loop this loop depends on and forms non-rectangular loop
8006 /// nest.
8007 unsigned LoopDependentIdx = 0;
8008 /// Final condition for the non-rectangular loop nest support. It is used to
8009 /// check that the number of iterations for this particular counter must be
8010 /// finished.
8011 Expr *FinalCondition = nullptr;
8012};
8013
8014/// Scan an AST subtree, checking that no decls in the CollapsedLoopVarDecls
8015/// set are referenced. Used for verifying loop nest structure before
8016/// performing a loop collapse operation.
8017class ForSubExprChecker : public DynamicRecursiveASTVisitor {
8018 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
8019 VarDecl *ForbiddenVar = nullptr;
8020 SourceRange ErrLoc;
8021
8022public:
8023 explicit ForSubExprChecker(
8024 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls)
8025 : CollapsedLoopVarDecls(CollapsedLoopVarDecls) {
8026 // We want to visit implicit code, i.e. synthetic initialisation statements
8027 // created during range-for lowering.
8028 ShouldVisitImplicitCode = true;
8029 }
8030
8031 bool VisitDeclRefExpr(DeclRefExpr *E) override {
8032 ValueDecl *VD = E->getDecl();
8033 if (!isa<VarDecl, BindingDecl>(Val: VD))
8034 return true;
8035 VarDecl *V = VD->getPotentiallyDecomposedVarDecl();
8036 if (V->getType()->isReferenceType()) {
8037 VarDecl *VD = V->getDefinition();
8038 if (VD->hasInit()) {
8039 Expr *I = VD->getInit();
8040 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: I);
8041 if (!DRE)
8042 return true;
8043 V = DRE->getDecl()->getPotentiallyDecomposedVarDecl();
8044 }
8045 }
8046 Decl *Canon = V->getCanonicalDecl();
8047 if (CollapsedLoopVarDecls.contains(Ptr: Canon)) {
8048 ForbiddenVar = V;
8049 ErrLoc = E->getSourceRange();
8050 return false;
8051 }
8052
8053 return true;
8054 }
8055
8056 VarDecl *getForbiddenVar() const { return ForbiddenVar; }
8057 SourceRange getErrRange() const { return ErrLoc; }
8058};
8059
8060/// Helper class for checking canonical form of the OpenMP loops and
8061/// extracting iteration space of each loop in the loop nest, that will be used
8062/// for IR generation.
8063class OpenMPIterationSpaceChecker {
8064 /// Reference to Sema.
8065 Sema &SemaRef;
8066 /// Does the loop associated directive support non-rectangular loops?
8067 bool SupportsNonRectangular;
8068 /// Data-sharing stack.
8069 DSAStackTy &Stack;
8070 /// A location for diagnostics (when there is no some better location).
8071 SourceLocation DefaultLoc;
8072 /// A location for diagnostics (when increment is not compatible).
8073 SourceLocation ConditionLoc;
8074 /// The set of variables declared within the (to be collapsed) loop nest.
8075 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
8076 /// A source location for referring to loop init later.
8077 SourceRange InitSrcRange;
8078 /// A source location for referring to condition later.
8079 SourceRange ConditionSrcRange;
8080 /// A source location for referring to increment later.
8081 SourceRange IncrementSrcRange;
8082 /// Loop variable.
8083 ValueDecl *LCDecl = nullptr;
8084 /// Reference to loop variable.
8085 Expr *LCRef = nullptr;
8086 /// Lower bound (initializer for the var).
8087 Expr *LB = nullptr;
8088 /// Upper bound.
8089 Expr *UB = nullptr;
8090 /// Loop step (increment).
8091 Expr *Step = nullptr;
8092 /// This flag is true when condition is one of:
8093 /// Var < UB
8094 /// Var <= UB
8095 /// UB > Var
8096 /// UB >= Var
8097 /// This will have no value when the condition is !=
8098 std::optional<bool> TestIsLessOp;
8099 /// This flag is true when condition is strict ( < or > ).
8100 bool TestIsStrictOp = false;
8101 /// This flag is true when step is subtracted on each iteration.
8102 bool SubtractStep = false;
8103 /// The outer loop counter this loop depends on (if any).
8104 const ValueDecl *DepDecl = nullptr;
8105 /// Contains number of loop (starts from 1) on which loop counter init
8106 /// expression of this loop depends on.
8107 std::optional<unsigned> InitDependOnLC;
8108 /// Contains number of loop (starts from 1) on which loop counter condition
8109 /// expression of this loop depends on.
8110 std::optional<unsigned> CondDependOnLC;
8111 /// Checks if the provide statement depends on the loop counter.
8112 std::optional<unsigned> doesDependOnLoopCounter(const Stmt *S,
8113 bool IsInitializer);
8114 /// Original condition required for checking of the exit condition for
8115 /// non-rectangular loop.
8116 Expr *Condition = nullptr;
8117
8118public:
8119 OpenMPIterationSpaceChecker(
8120 Sema &SemaRef, bool SupportsNonRectangular, DSAStackTy &Stack,
8121 SourceLocation DefaultLoc,
8122 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopDecls)
8123 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular),
8124 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
8125 CollapsedLoopVarDecls(CollapsedLoopDecls) {}
8126 /// Check init-expr for canonical loop form and save loop counter
8127 /// variable - #Var and its initialization value - #LB.
8128 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
8129 /// Check test-expr for canonical form, save upper-bound (#UB), flags
8130 /// for less/greater and for strict/non-strict comparison.
8131 bool checkAndSetCond(Expr *S);
8132 /// Check incr-expr for canonical loop form and return true if it
8133 /// does not conform, otherwise save loop step (#Step).
8134 bool checkAndSetInc(Expr *S);
8135 /// Return the loop counter variable.
8136 ValueDecl *getLoopDecl() const { return LCDecl; }
8137 /// Return the reference expression to loop counter variable.
8138 Expr *getLoopDeclRefExpr() const { return LCRef; }
8139 /// Source range of the loop init.
8140 SourceRange getInitSrcRange() const { return InitSrcRange; }
8141 /// Source range of the loop condition.
8142 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
8143 /// Source range of the loop increment.
8144 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
8145 /// True if the step should be subtracted.
8146 bool shouldSubtractStep() const { return SubtractStep; }
8147 /// True, if the compare operator is strict (<, > or !=).
8148 bool isStrictTestOp() const { return TestIsStrictOp; }
8149 /// Build the expression to calculate the number of iterations.
8150 Expr *buildNumIterations(
8151 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8152 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8153 /// Build the precondition expression for the loops.
8154 Expr *
8155 buildPreCond(Scope *S, Expr *Cond,
8156 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8157 /// Build reference expression to the counter be used for codegen.
8158 DeclRefExpr *
8159 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8160 DSAStackTy &DSA) const;
8161 /// Build reference expression to the private counter be used for
8162 /// codegen.
8163 Expr *buildPrivateCounterVar() const;
8164 /// Build initialization of the counter be used for codegen.
8165 Expr *buildCounterInit() const;
8166 /// Build step of the counter be used for codegen.
8167 Expr *buildCounterStep() const;
8168 /// Build loop data with counter value for depend clauses in ordered
8169 /// directives.
8170 Expr *
8171 buildOrderedLoopData(Scope *S, Expr *Counter,
8172 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8173 SourceLocation Loc, Expr *Inc = nullptr,
8174 OverloadedOperatorKind OOK = OO_Amp);
8175 /// Builds the minimum value for the loop counter.
8176 std::pair<Expr *, Expr *> buildMinMaxValues(
8177 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8178 /// Builds final condition for the non-rectangular loops.
8179 Expr *buildFinalCondition(Scope *S) const;
8180 /// Return true if any expression is dependent.
8181 bool dependent() const;
8182 /// Returns true if the initializer forms non-rectangular loop.
8183 bool doesInitDependOnLC() const { return InitDependOnLC.has_value(); }
8184 /// Returns true if the condition forms non-rectangular loop.
8185 bool doesCondDependOnLC() const { return CondDependOnLC.has_value(); }
8186 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
8187 unsigned getLoopDependentIdx() const {
8188 return InitDependOnLC.value_or(u: CondDependOnLC.value_or(u: 0));
8189 }
8190
8191private:
8192 /// Check the right-hand side of an assignment in the increment
8193 /// expression.
8194 bool checkAndSetIncRHS(Expr *RHS);
8195 /// Helper to set loop counter variable and its initializer.
8196 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
8197 bool EmitDiags);
8198 /// Helper to set upper bound.
8199 bool setUB(Expr *NewUB, std::optional<bool> LessOp, bool StrictOp,
8200 SourceRange SR, SourceLocation SL);
8201 /// Helper to set loop increment.
8202 bool setStep(Expr *NewStep, bool Subtract);
8203};
8204
8205bool OpenMPIterationSpaceChecker::dependent() const {
8206 if (!LCDecl) {
8207 assert(!LB && !UB && !Step);
8208 return false;
8209 }
8210 return LCDecl->getType()->isDependentType() ||
8211 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
8212 (Step && Step->isValueDependent());
8213}
8214
8215bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
8216 Expr *NewLCRefExpr,
8217 Expr *NewLB, bool EmitDiags) {
8218 // State consistency checking to ensure correct usage.
8219 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
8220 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8221 if (!NewLCDecl || !NewLB || NewLB->containsErrors())
8222 return true;
8223 LCDecl = getCanonicalDecl(D: NewLCDecl);
8224 LCRef = NewLCRefExpr;
8225 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: NewLB))
8226 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
8227 if ((Ctor->isCopyOrMoveConstructor() ||
8228 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
8229 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
8230 NewLB = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
8231 LB = NewLB;
8232 if (EmitDiags)
8233 InitDependOnLC = doesDependOnLoopCounter(S: LB, /*IsInitializer=*/true);
8234 return false;
8235}
8236
8237bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, std::optional<bool> LessOp,
8238 bool StrictOp, SourceRange SR,
8239 SourceLocation SL) {
8240 // State consistency checking to ensure correct usage.
8241 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
8242 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8243 if (!NewUB || NewUB->containsErrors())
8244 return true;
8245 UB = NewUB;
8246 if (LessOp)
8247 TestIsLessOp = LessOp;
8248 TestIsStrictOp = StrictOp;
8249 ConditionSrcRange = SR;
8250 ConditionLoc = SL;
8251 CondDependOnLC = doesDependOnLoopCounter(S: UB, /*IsInitializer=*/false);
8252 return false;
8253}
8254
8255bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
8256 // State consistency checking to ensure correct usage.
8257 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
8258 if (!NewStep || NewStep->containsErrors())
8259 return true;
8260 if (!NewStep->isValueDependent()) {
8261 // Check that the step is integer expression.
8262 SourceLocation StepLoc = NewStep->getBeginLoc();
8263 ExprResult Val = SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(
8264 OpLoc: StepLoc, Op: getExprAsWritten(E: NewStep));
8265 if (Val.isInvalid())
8266 return true;
8267 NewStep = Val.get();
8268
8269 // OpenMP [2.6, Canonical Loop Form, Restrictions]
8270 // If test-expr is of form var relational-op b and relational-op is < or
8271 // <= then incr-expr must cause var to increase on each iteration of the
8272 // loop. If test-expr is of form var relational-op b and relational-op is
8273 // > or >= then incr-expr must cause var to decrease on each iteration of
8274 // the loop.
8275 // If test-expr is of form b relational-op var and relational-op is < or
8276 // <= then incr-expr must cause var to decrease on each iteration of the
8277 // loop. If test-expr is of form b relational-op var and relational-op is
8278 // > or >= then incr-expr must cause var to increase on each iteration of
8279 // the loop.
8280 std::optional<llvm::APSInt> Result =
8281 NewStep->getIntegerConstantExpr(Ctx: SemaRef.Context);
8282 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
8283 bool IsConstNeg =
8284 Result && Result->isSigned() && (Subtract != Result->isNegative());
8285 bool IsConstPos =
8286 Result && Result->isSigned() && (Subtract == Result->isNegative());
8287 bool IsConstZero = Result && !Result->getBoolValue();
8288
8289 // != with increment is treated as <; != with decrement is treated as >
8290 if (!TestIsLessOp)
8291 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
8292 if (UB && (IsConstZero ||
8293 (*TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
8294 : (IsConstPos || (IsUnsigned && !Subtract))))) {
8295 SemaRef.Diag(Loc: NewStep->getExprLoc(),
8296 DiagID: diag::err_omp_loop_incr_not_compatible)
8297 << LCDecl << *TestIsLessOp << NewStep->getSourceRange();
8298 SemaRef.Diag(Loc: ConditionLoc,
8299 DiagID: diag::note_omp_loop_cond_requires_compatible_incr)
8300 << *TestIsLessOp << ConditionSrcRange;
8301 return true;
8302 }
8303 if (*TestIsLessOp == Subtract) {
8304 NewStep =
8305 SemaRef.CreateBuiltinUnaryOp(OpLoc: NewStep->getExprLoc(), Opc: UO_Minus, InputExpr: NewStep)
8306 .get();
8307 Subtract = !Subtract;
8308 }
8309 }
8310
8311 Step = NewStep;
8312 SubtractStep = Subtract;
8313 return false;
8314}
8315
8316namespace {
8317/// Checker for the non-rectangular loops. Checks if the initializer or
8318/// condition expression references loop counter variable.
8319class LoopCounterRefChecker final
8320 : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
8321 Sema &SemaRef;
8322 DSAStackTy &Stack;
8323 const ValueDecl *CurLCDecl = nullptr;
8324 const ValueDecl *DepDecl = nullptr;
8325 const ValueDecl *PrevDepDecl = nullptr;
8326 bool IsInitializer = true;
8327 bool SupportsNonRectangular;
8328 unsigned BaseLoopId = 0;
8329 bool checkDecl(const Expr *E, const ValueDecl *VD) {
8330 if (getCanonicalDecl(D: VD) == getCanonicalDecl(D: CurLCDecl)) {
8331 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_stmt_depends_on_loop_counter)
8332 << (IsInitializer ? 0 : 1);
8333 return false;
8334 }
8335 const auto &&Data = Stack.isLoopControlVariable(D: VD);
8336 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
8337 // The type of the loop iterator on which we depend may not have a random
8338 // access iterator type.
8339 if (Data.first && VD->getType()->isRecordType()) {
8340 SmallString<128> Name;
8341 llvm::raw_svector_ostream OS(Name);
8342 VD->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
8343 /*Qualified=*/true);
8344 SemaRef.Diag(Loc: E->getExprLoc(),
8345 DiagID: diag::err_omp_wrong_dependency_iterator_type)
8346 << OS.str();
8347 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_previous_decl) << VD;
8348 return false;
8349 }
8350 if (Data.first && !SupportsNonRectangular) {
8351 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_invariant_dependency);
8352 return false;
8353 }
8354 if (Data.first &&
8355 (DepDecl || (PrevDepDecl &&
8356 getCanonicalDecl(D: VD) != getCanonicalDecl(D: PrevDepDecl)))) {
8357 if (!DepDecl && PrevDepDecl)
8358 DepDecl = PrevDepDecl;
8359 SmallString<128> Name;
8360 llvm::raw_svector_ostream OS(Name);
8361 DepDecl->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
8362 /*Qualified=*/true);
8363 SemaRef.Diag(Loc: E->getExprLoc(),
8364 DiagID: diag::err_omp_invariant_or_linear_dependency)
8365 << OS.str();
8366 return false;
8367 }
8368 if (Data.first) {
8369 DepDecl = VD;
8370 BaseLoopId = Data.first;
8371 }
8372 return Data.first;
8373 }
8374
8375public:
8376 bool VisitDeclRefExpr(const DeclRefExpr *E) {
8377 const ValueDecl *VD = E->getDecl();
8378 if (isa<VarDecl>(Val: VD))
8379 return checkDecl(E, VD);
8380 return false;
8381 }
8382 bool VisitMemberExpr(const MemberExpr *E) {
8383 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParens())) {
8384 const ValueDecl *VD = E->getMemberDecl();
8385 if (isa<VarDecl>(Val: VD) || isa<FieldDecl>(Val: VD))
8386 return checkDecl(E, VD);
8387 }
8388 return false;
8389 }
8390 bool VisitStmt(const Stmt *S) {
8391 bool Res = false;
8392 for (const Stmt *Child : S->children())
8393 Res = (Child && Visit(S: Child)) || Res;
8394 return Res;
8395 }
8396 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
8397 const ValueDecl *CurLCDecl, bool IsInitializer,
8398 const ValueDecl *PrevDepDecl = nullptr,
8399 bool SupportsNonRectangular = true)
8400 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
8401 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer),
8402 SupportsNonRectangular(SupportsNonRectangular) {}
8403 unsigned getBaseLoopId() const {
8404 assert(CurLCDecl && "Expected loop dependency.");
8405 return BaseLoopId;
8406 }
8407 const ValueDecl *getDepDecl() const {
8408 assert(CurLCDecl && "Expected loop dependency.");
8409 return DepDecl;
8410 }
8411};
8412} // namespace
8413
8414std::optional<unsigned>
8415OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
8416 bool IsInitializer) {
8417 // Check for the non-rectangular loops.
8418 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
8419 DepDecl, SupportsNonRectangular);
8420 if (LoopStmtChecker.Visit(S)) {
8421 DepDecl = LoopStmtChecker.getDepDecl();
8422 return LoopStmtChecker.getBaseLoopId();
8423 }
8424 return std::nullopt;
8425}
8426
8427bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
8428 // Check init-expr for canonical loop form and save loop counter
8429 // variable - #Var and its initialization value - #LB.
8430 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
8431 // var = lb
8432 // integer-type var = lb
8433 // random-access-iterator-type var = lb
8434 // pointer-type var = lb
8435 //
8436 if (!S) {
8437 if (EmitDiags) {
8438 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_init);
8439 }
8440 return true;
8441 }
8442 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
8443 if (!ExprTemp->cleanupsHaveSideEffects())
8444 S = ExprTemp->getSubExpr();
8445
8446 if (!CollapsedLoopVarDecls.empty()) {
8447 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8448 if (!FSEC.TraverseStmt(S)) {
8449 SourceRange Range = FSEC.getErrRange();
8450 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8451 << Range.getEnd() << 0 << FSEC.getForbiddenVar();
8452 return true;
8453 }
8454 }
8455
8456 InitSrcRange = S->getSourceRange();
8457 if (Expr *E = dyn_cast<Expr>(Val: S))
8458 S = E->IgnoreParens();
8459 if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8460 if (BO->getOpcode() == BO_Assign) {
8461 Expr *LHS = BO->getLHS()->IgnoreParens();
8462 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
8463 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
8464 if (auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
8465 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8466 EmitDiags);
8467 return setLCDeclAndLB(NewLCDecl: DRE->getDecl(), NewLCRefExpr: DRE, NewLB: BO->getRHS(), EmitDiags);
8468 }
8469 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
8470 if (ME->isArrow() &&
8471 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8472 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8473 EmitDiags);
8474 }
8475 }
8476 } else if (auto *DS = dyn_cast<DeclStmt>(Val: S)) {
8477 if (DS->isSingleDecl()) {
8478 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: DS->getSingleDecl())) {
8479 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
8480 // Accept non-canonical init form here but emit ext. warning.
8481 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
8482 SemaRef.Diag(Loc: S->getBeginLoc(),
8483 DiagID: diag::ext_omp_loop_not_canonical_init)
8484 << S->getSourceRange();
8485 return setLCDeclAndLB(
8486 NewLCDecl: Var,
8487 NewLCRefExpr: buildDeclRefExpr(S&: SemaRef, D: Var,
8488 Ty: Var->getType().getNonReferenceType(),
8489 Loc: DS->getBeginLoc()),
8490 NewLB: Var->getInit(), EmitDiags);
8491 }
8492 }
8493 }
8494 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8495 if (CE->getOperator() == OO_Equal) {
8496 Expr *LHS = CE->getArg(Arg: 0);
8497 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
8498 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
8499 if (auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
8500 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8501 EmitDiags);
8502 return setLCDeclAndLB(NewLCDecl: DRE->getDecl(), NewLCRefExpr: DRE, NewLB: CE->getArg(Arg: 1), EmitDiags);
8503 }
8504 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
8505 if (ME->isArrow() &&
8506 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8507 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8508 EmitDiags);
8509 }
8510 }
8511 }
8512
8513 if (dependent() || SemaRef.CurContext->isDependentContext())
8514 return false;
8515 if (EmitDiags) {
8516 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_init)
8517 << S->getSourceRange();
8518 }
8519 return true;
8520}
8521
8522/// Ignore parenthesizes, implicit casts, copy constructor and return the
8523/// variable (which may be the loop variable) if possible.
8524static const ValueDecl *getInitLCDecl(const Expr *E) {
8525 if (!E)
8526 return nullptr;
8527 E = getExprAsWritten(E);
8528 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: E))
8529 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
8530 if ((Ctor->isCopyOrMoveConstructor() ||
8531 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
8532 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
8533 E = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
8534 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(Val: E)) {
8535 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
8536 return getCanonicalDecl(D: VD);
8537 }
8538 if (const auto *ME = dyn_cast_or_null<MemberExpr>(Val: E))
8539 if (ME->isArrow() && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8540 return getCanonicalDecl(D: ME->getMemberDecl());
8541 return nullptr;
8542}
8543
8544bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
8545 // Check test-expr for canonical form, save upper-bound UB, flags for
8546 // less/greater and for strict/non-strict comparison.
8547 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
8548 // var relational-op b
8549 // b relational-op var
8550 //
8551 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
8552 if (!S) {
8553 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
8554 << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
8555 return true;
8556 }
8557 Condition = S;
8558 S = getExprAsWritten(E: S);
8559
8560 if (!CollapsedLoopVarDecls.empty()) {
8561 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8562 if (!FSEC.TraverseStmt(S)) {
8563 SourceRange Range = FSEC.getErrRange();
8564 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8565 << Range.getEnd() << 1 << FSEC.getForbiddenVar();
8566 return true;
8567 }
8568 }
8569
8570 SourceLocation CondLoc = S->getBeginLoc();
8571 auto &&CheckAndSetCond =
8572 [this, IneqCondIsCanonical](BinaryOperatorKind Opcode, const Expr *LHS,
8573 const Expr *RHS, SourceRange SR,
8574 SourceLocation OpLoc) -> std::optional<bool> {
8575 if (BinaryOperator::isRelationalOp(Opc: Opcode)) {
8576 if (getInitLCDecl(E: LHS) == LCDecl)
8577 return setUB(NewUB: const_cast<Expr *>(RHS),
8578 LessOp: (Opcode == BO_LT || Opcode == BO_LE),
8579 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
8580 if (getInitLCDecl(E: RHS) == LCDecl)
8581 return setUB(NewUB: const_cast<Expr *>(LHS),
8582 LessOp: (Opcode == BO_GT || Opcode == BO_GE),
8583 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
8584 } else if (IneqCondIsCanonical && Opcode == BO_NE) {
8585 return setUB(NewUB: const_cast<Expr *>(getInitLCDecl(E: LHS) == LCDecl ? RHS : LHS),
8586 /*LessOp=*/std::nullopt,
8587 /*StrictOp=*/true, SR, SL: OpLoc);
8588 }
8589 return std::nullopt;
8590 };
8591 std::optional<bool> Res;
8592 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(Val: S)) {
8593 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm();
8594 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(),
8595 RBO->getOperatorLoc());
8596 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8597 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(),
8598 BO->getSourceRange(), BO->getOperatorLoc());
8599 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8600 if (CE->getNumArgs() == 2) {
8601 Res = CheckAndSetCond(
8602 BinaryOperator::getOverloadedOpcode(OO: CE->getOperator()), CE->getArg(Arg: 0),
8603 CE->getArg(Arg: 1), CE->getSourceRange(), CE->getOperatorLoc());
8604 }
8605 }
8606 if (Res)
8607 return *Res;
8608 if (dependent() || SemaRef.CurContext->isDependentContext())
8609 return false;
8610 SemaRef.Diag(Loc: CondLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
8611 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
8612 return true;
8613}
8614
8615bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
8616 // RHS of canonical loop form increment can be:
8617 // var + incr
8618 // incr + var
8619 // var - incr
8620 //
8621 RHS = RHS->IgnoreParenImpCasts();
8622 if (auto *BO = dyn_cast<BinaryOperator>(Val: RHS)) {
8623 if (BO->isAdditiveOp()) {
8624 bool IsAdd = BO->getOpcode() == BO_Add;
8625 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8626 return setStep(NewStep: BO->getRHS(), Subtract: !IsAdd);
8627 if (IsAdd && getInitLCDecl(E: BO->getRHS()) == LCDecl)
8628 return setStep(NewStep: BO->getLHS(), /*Subtract=*/false);
8629 }
8630 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: RHS)) {
8631 bool IsAdd = CE->getOperator() == OO_Plus;
8632 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
8633 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8634 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: !IsAdd);
8635 if (IsAdd && getInitLCDecl(E: CE->getArg(Arg: 1)) == LCDecl)
8636 return setStep(NewStep: CE->getArg(Arg: 0), /*Subtract=*/false);
8637 }
8638 }
8639 if (dependent() || SemaRef.CurContext->isDependentContext())
8640 return false;
8641 SemaRef.Diag(Loc: RHS->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
8642 << RHS->getSourceRange() << LCDecl;
8643 return true;
8644}
8645
8646bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
8647 // Check incr-expr for canonical loop form and return true if it
8648 // does not conform.
8649 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
8650 // ++var
8651 // var++
8652 // --var
8653 // var--
8654 // var += incr
8655 // var -= incr
8656 // var = var + incr
8657 // var = incr + var
8658 // var = var - incr
8659 //
8660 if (!S) {
8661 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_incr) << LCDecl;
8662 return true;
8663 }
8664 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
8665 if (!ExprTemp->cleanupsHaveSideEffects())
8666 S = ExprTemp->getSubExpr();
8667
8668 if (!CollapsedLoopVarDecls.empty()) {
8669 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8670 if (!FSEC.TraverseStmt(S)) {
8671 SourceRange Range = FSEC.getErrRange();
8672 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8673 << Range.getEnd() << 2 << FSEC.getForbiddenVar();
8674 return true;
8675 }
8676 }
8677
8678 IncrementSrcRange = S->getSourceRange();
8679 S = S->IgnoreParens();
8680 if (auto *UO = dyn_cast<UnaryOperator>(Val: S)) {
8681 if (UO->isIncrementDecrementOp() &&
8682 getInitLCDecl(E: UO->getSubExpr()) == LCDecl)
8683 return setStep(NewStep: SemaRef
8684 .ActOnIntegerConstant(Loc: UO->getBeginLoc(),
8685 Val: (UO->isDecrementOp() ? -1 : 1))
8686 .get(),
8687 /*Subtract=*/false);
8688 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8689 switch (BO->getOpcode()) {
8690 case BO_AddAssign:
8691 case BO_SubAssign:
8692 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8693 return setStep(NewStep: BO->getRHS(), Subtract: BO->getOpcode() == BO_SubAssign);
8694 break;
8695 case BO_Assign:
8696 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8697 return checkAndSetIncRHS(RHS: BO->getRHS());
8698 break;
8699 default:
8700 break;
8701 }
8702 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8703 switch (CE->getOperator()) {
8704 case OO_PlusPlus:
8705 case OO_MinusMinus:
8706 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8707 return setStep(NewStep: SemaRef
8708 .ActOnIntegerConstant(
8709 Loc: CE->getBeginLoc(),
8710 Val: ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
8711 .get(),
8712 /*Subtract=*/false);
8713 break;
8714 case OO_PlusEqual:
8715 case OO_MinusEqual:
8716 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8717 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: CE->getOperator() == OO_MinusEqual);
8718 break;
8719 case OO_Equal:
8720 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8721 return checkAndSetIncRHS(RHS: CE->getArg(Arg: 1));
8722 break;
8723 default:
8724 break;
8725 }
8726 }
8727 if (dependent() || SemaRef.CurContext->isDependentContext())
8728 return false;
8729 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
8730 << S->getSourceRange() << LCDecl;
8731 return true;
8732}
8733
8734static ExprResult
8735tryBuildCapture(Sema &SemaRef, Expr *Capture,
8736 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8737 StringRef Name = ".capture_expr.") {
8738 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors())
8739 return Capture;
8740 if (Capture->isEvaluatable(Ctx: SemaRef.Context, AllowSideEffects: Expr::SE_AllowSideEffects))
8741 return SemaRef.PerformImplicitConversion(From: Capture->IgnoreImpCasts(),
8742 ToType: Capture->getType(),
8743 Action: AssignmentAction::Converting,
8744 /*AllowExplicit=*/true);
8745 auto I = Captures.find(Key: Capture);
8746 if (I != Captures.end())
8747 return buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref&: I->second, Name);
8748 DeclRefExpr *Ref = nullptr;
8749 ExprResult Res = buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref, Name);
8750 Captures[Capture] = Ref;
8751 return Res;
8752}
8753
8754/// Calculate number of iterations, transforming to unsigned, if number of
8755/// iterations may be larger than the original type.
8756static Expr *
8757calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc,
8758 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy,
8759 bool TestIsStrictOp, bool RoundToStep,
8760 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8761 std::optional<unsigned> InitDependOnLC,
8762 std::optional<unsigned> CondDependOnLC) {
8763 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
8764 if (!NewStep.isUsable())
8765 return nullptr;
8766 llvm::APSInt LRes, SRes;
8767 bool IsLowerConst = false, IsStepConst = false;
8768 if (std::optional<llvm::APSInt> Res =
8769 Lower->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8770 LRes = *Res;
8771 IsLowerConst = true;
8772 }
8773 if (std::optional<llvm::APSInt> Res =
8774 Step->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8775 SRes = *Res;
8776 IsStepConst = true;
8777 }
8778 bool NoNeedToConvert = IsLowerConst && !RoundToStep &&
8779 ((!TestIsStrictOp && LRes.isNonNegative()) ||
8780 (TestIsStrictOp && LRes.isStrictlyPositive()));
8781 bool NeedToReorganize = false;
8782 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow.
8783 if (!NoNeedToConvert && IsLowerConst &&
8784 (TestIsStrictOp || (RoundToStep && IsStepConst))) {
8785 NoNeedToConvert = true;
8786 if (RoundToStep) {
8787 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth()
8788 ? LRes.getBitWidth()
8789 : SRes.getBitWidth();
8790 LRes = LRes.extend(width: BW + 1);
8791 LRes.setIsSigned(true);
8792 SRes = SRes.extend(width: BW + 1);
8793 SRes.setIsSigned(true);
8794 LRes -= SRes;
8795 NoNeedToConvert = LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
8796 LRes = LRes.trunc(width: BW);
8797 }
8798 if (TestIsStrictOp) {
8799 unsigned BW = LRes.getBitWidth();
8800 LRes = LRes.extend(width: BW + 1);
8801 LRes.setIsSigned(true);
8802 ++LRes;
8803 NoNeedToConvert =
8804 NoNeedToConvert && LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
8805 // truncate to the original bitwidth.
8806 LRes = LRes.trunc(width: BW);
8807 }
8808 NeedToReorganize = NoNeedToConvert;
8809 }
8810 llvm::APSInt URes;
8811 bool IsUpperConst = false;
8812 if (std::optional<llvm::APSInt> Res =
8813 Upper->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8814 URes = *Res;
8815 IsUpperConst = true;
8816 }
8817 if (NoNeedToConvert && IsLowerConst && IsUpperConst &&
8818 (!RoundToStep || IsStepConst)) {
8819 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth()
8820 : URes.getBitWidth();
8821 LRes = LRes.extend(width: BW + 1);
8822 LRes.setIsSigned(true);
8823 URes = URes.extend(width: BW + 1);
8824 URes.setIsSigned(true);
8825 URes -= LRes;
8826 NoNeedToConvert = URes.trunc(width: BW).extend(width: BW + 1) == URes;
8827 NeedToReorganize = NoNeedToConvert;
8828 }
8829 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant
8830 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to
8831 // unsigned.
8832 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) &&
8833 !LCTy->isDependentType() && LCTy->isIntegerType()) {
8834 QualType LowerTy = Lower->getType();
8835 QualType UpperTy = Upper->getType();
8836 uint64_t LowerSize = SemaRef.Context.getTypeSize(T: LowerTy);
8837 uint64_t UpperSize = SemaRef.Context.getTypeSize(T: UpperTy);
8838 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) ||
8839 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) {
8840 QualType CastType = SemaRef.Context.getIntTypeForBitwidth(
8841 DestWidth: LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0);
8842 Upper =
8843 SemaRef
8844 .PerformImplicitConversion(
8845 From: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
8846 ToType: CastType, Action: AssignmentAction::Converting)
8847 .get();
8848 Lower = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get();
8849 NewStep = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: NewStep.get());
8850 }
8851 }
8852 if (!Lower || !Upper || NewStep.isInvalid())
8853 return nullptr;
8854
8855 ExprResult Diff;
8856
8857 // For nested triangular loops (depth >= 2), use already computed Upper and
8858 // Lower bounds to calculate the number of iterations: Upper - Lower + 1.
8859 // Don't apply to first-level triangular loops as the standard formula handles
8860 // those correctly.
8861 if (TestIsStrictOp && InitDependOnLC.has_value() &&
8862 InitDependOnLC.value() >= 2 && !CondDependOnLC.has_value()) {
8863 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Lower);
8864 if (!Diff.isUsable())
8865 return nullptr;
8866
8867 Diff =
8868 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(),
8869 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: DefaultLoc, Val: 1).get());
8870 if (!Diff.isUsable())
8871 return nullptr;
8872
8873 return Diff.get();
8874 }
8875
8876 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+
8877 // 1]).
8878 if (NeedToReorganize) {
8879 Diff = Lower;
8880
8881 if (RoundToStep) {
8882 // Lower - Step
8883 Diff =
8884 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8885 if (!Diff.isUsable())
8886 return nullptr;
8887 }
8888
8889 // Lower - Step [+ 1]
8890 if (TestIsStrictOp)
8891 Diff = SemaRef.BuildBinOp(
8892 S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(),
8893 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
8894 if (!Diff.isUsable())
8895 return nullptr;
8896
8897 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
8898 if (!Diff.isUsable())
8899 return nullptr;
8900
8901 // Upper - (Lower - Step [+ 1]).
8902 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Diff.get());
8903 if (!Diff.isUsable())
8904 return nullptr;
8905 } else {
8906 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Lower);
8907
8908 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) {
8909 // BuildBinOp already emitted error, this one is to point user to upper
8910 // and lower bound, and to tell what is passed to 'operator-'.
8911 SemaRef.Diag(Loc: Upper->getBeginLoc(), DiagID: diag::err_omp_loop_diff_cxx)
8912 << Upper->getSourceRange() << Lower->getSourceRange();
8913 return nullptr;
8914 }
8915
8916 if (!Diff.isUsable())
8917 return nullptr;
8918
8919 // Upper - Lower [- 1]
8920 if (TestIsStrictOp)
8921 Diff = SemaRef.BuildBinOp(
8922 S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(),
8923 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
8924 if (!Diff.isUsable())
8925 return nullptr;
8926
8927 if (RoundToStep) {
8928 // Upper - Lower [- 1] + Step
8929 Diff =
8930 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8931 if (!Diff.isUsable())
8932 return nullptr;
8933 }
8934 }
8935
8936 // Parentheses (for dumping/debugging purposes only).
8937 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
8938 if (!Diff.isUsable())
8939 return nullptr;
8940
8941 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step
8942 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Div, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8943 if (!Diff.isUsable())
8944 return nullptr;
8945
8946 return Diff.get();
8947}
8948
8949/// Build the expression to calculate the number of iterations.
8950Expr *OpenMPIterationSpaceChecker::buildNumIterations(
8951 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8952 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8953 QualType VarType = LCDecl->getType().getNonReferenceType();
8954 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
8955 !SemaRef.getLangOpts().CPlusPlus)
8956 return nullptr;
8957 Expr *LBVal = LB;
8958 Expr *UBVal = UB;
8959 // OuterVar = (LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
8960 // max(LB(MinVal), LB(MaxVal)))
8961 if (InitDependOnLC) {
8962 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1];
8963 if (!IS.MinValue || !IS.MaxValue)
8964 return nullptr;
8965 // OuterVar = Min
8966 ExprResult MinValue =
8967 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
8968 if (!MinValue.isUsable())
8969 return nullptr;
8970
8971 ExprResult LBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8972 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
8973 if (!LBMinVal.isUsable())
8974 return nullptr;
8975 // OuterVar = Min, LBVal
8976 LBMinVal =
8977 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMinVal.get(), RHSExpr: LBVal);
8978 if (!LBMinVal.isUsable())
8979 return nullptr;
8980 // (OuterVar = Min, LBVal)
8981 LBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMinVal.get());
8982 if (!LBMinVal.isUsable())
8983 return nullptr;
8984
8985 // OuterVar = Max
8986 ExprResult MaxValue =
8987 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
8988 if (!MaxValue.isUsable())
8989 return nullptr;
8990
8991 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8992 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
8993 if (!LBMaxVal.isUsable())
8994 return nullptr;
8995 // OuterVar = Max, LBVal
8996 LBMaxVal =
8997 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMaxVal.get(), RHSExpr: LBVal);
8998 if (!LBMaxVal.isUsable())
8999 return nullptr;
9000 // (OuterVar = Max, LBVal)
9001 LBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMaxVal.get());
9002 if (!LBMaxVal.isUsable())
9003 return nullptr;
9004
9005 Expr *LBMin =
9006 tryBuildCapture(SemaRef, Capture: LBMinVal.get(), Captures, Name: ".lb_min").get();
9007 Expr *LBMax =
9008 tryBuildCapture(SemaRef, Capture: LBMaxVal.get(), Captures, Name: ".lb_max").get();
9009 if (!LBMin || !LBMax)
9010 return nullptr;
9011 // LB(MinVal) < LB(MaxVal)
9012 ExprResult MinLessMaxRes =
9013 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_LT, LHSExpr: LBMin, RHSExpr: LBMax);
9014 if (!MinLessMaxRes.isUsable())
9015 return nullptr;
9016 Expr *MinLessMax =
9017 tryBuildCapture(SemaRef, Capture: MinLessMaxRes.get(), Captures, Name: ".min_less_max")
9018 .get();
9019 if (!MinLessMax)
9020 return nullptr;
9021 if (*TestIsLessOp) {
9022 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
9023 // LB(MaxVal))
9024 ExprResult MinLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
9025 CondExpr: MinLessMax, LHSExpr: LBMin, RHSExpr: LBMax);
9026 if (!MinLB.isUsable())
9027 return nullptr;
9028 LBVal = MinLB.get();
9029 } else {
9030 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
9031 // LB(MaxVal))
9032 ExprResult MaxLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
9033 CondExpr: MinLessMax, LHSExpr: LBMax, RHSExpr: LBMin);
9034 if (!MaxLB.isUsable())
9035 return nullptr;
9036 LBVal = MaxLB.get();
9037 }
9038 // OuterVar = LB
9039 LBMinVal =
9040 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign, LHSExpr: IS.CounterVar, RHSExpr: LBVal);
9041 if (!LBMinVal.isUsable())
9042 return nullptr;
9043 LBVal = LBMinVal.get();
9044 }
9045 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
9046 // min(UB(MinVal), UB(MaxVal))
9047 if (CondDependOnLC) {
9048 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1];
9049 if (!IS.MinValue || !IS.MaxValue)
9050 return nullptr;
9051 // OuterVar = Min
9052 ExprResult MinValue =
9053 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
9054 if (!MinValue.isUsable())
9055 return nullptr;
9056
9057 ExprResult UBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9058 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
9059 if (!UBMinVal.isUsable())
9060 return nullptr;
9061 // OuterVar = Min, UBVal
9062 UBMinVal =
9063 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMinVal.get(), RHSExpr: UBVal);
9064 if (!UBMinVal.isUsable())
9065 return nullptr;
9066 // (OuterVar = Min, UBVal)
9067 UBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMinVal.get());
9068 if (!UBMinVal.isUsable())
9069 return nullptr;
9070
9071 // OuterVar = Max
9072 ExprResult MaxValue =
9073 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
9074 if (!MaxValue.isUsable())
9075 return nullptr;
9076
9077 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
9078 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
9079 if (!UBMaxVal.isUsable())
9080 return nullptr;
9081 // OuterVar = Max, UBVal
9082 UBMaxVal =
9083 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMaxVal.get(), RHSExpr: UBVal);
9084 if (!UBMaxVal.isUsable())
9085 return nullptr;
9086 // (OuterVar = Max, UBVal)
9087 UBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMaxVal.get());
9088 if (!UBMaxVal.isUsable())
9089 return nullptr;
9090
9091 Expr *UBMin =
9092 tryBuildCapture(SemaRef, Capture: UBMinVal.get(), Captures, Name: ".ub_min").get();
9093 Expr *UBMax =
9094 tryBuildCapture(SemaRef, Capture: UBMaxVal.get(), Captures, Name: ".ub_max").get();
9095 if (!UBMin || !UBMax)
9096 return nullptr;
9097 // UB(MinVal) > UB(MaxVal)
9098 ExprResult MinGreaterMaxRes =
9099 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_GT, LHSExpr: UBMin, RHSExpr: UBMax);
9100 if (!MinGreaterMaxRes.isUsable())
9101 return nullptr;
9102 Expr *MinGreaterMax = tryBuildCapture(SemaRef, Capture: MinGreaterMaxRes.get(),
9103 Captures, Name: ".min_greater_max")
9104 .get();
9105 if (!MinGreaterMax)
9106 return nullptr;
9107 if (*TestIsLessOp) {
9108 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
9109 // UB(MaxVal))
9110 ExprResult MaxUB = SemaRef.ActOnConditionalOp(
9111 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMin, RHSExpr: UBMax);
9112 if (!MaxUB.isUsable())
9113 return nullptr;
9114 UBVal = MaxUB.get();
9115 } else {
9116 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
9117 // UB(MaxVal))
9118 ExprResult MinUB = SemaRef.ActOnConditionalOp(
9119 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMax, RHSExpr: UBMin);
9120 if (!MinUB.isUsable())
9121 return nullptr;
9122 UBVal = MinUB.get();
9123 }
9124 }
9125 Expr *UBExpr = *TestIsLessOp ? UBVal : LBVal;
9126 Expr *LBExpr = *TestIsLessOp ? LBVal : UBVal;
9127 Expr *Upper = tryBuildCapture(SemaRef, Capture: UBExpr, Captures, Name: ".upper").get();
9128 Expr *Lower = tryBuildCapture(SemaRef, Capture: LBExpr, Captures, Name: ".lower").get();
9129 if (!Upper || !Lower)
9130 return nullptr;
9131
9132 ExprResult Diff = calculateNumIters(
9133 SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType, TestIsStrictOp,
9134 /*RoundToStep=*/true, Captures, InitDependOnLC, CondDependOnLC);
9135 if (!Diff.isUsable())
9136 return nullptr;
9137
9138 // OpenMP runtime requires 32-bit or 64-bit loop variables.
9139 QualType Type = Diff.get()->getType();
9140 ASTContext &C = SemaRef.Context;
9141 bool UseVarType = VarType->hasIntegerRepresentation() &&
9142 C.getTypeSize(T: Type) > C.getTypeSize(T: VarType);
9143 if (!Type->isIntegerType() || UseVarType) {
9144 unsigned NewSize =
9145 UseVarType ? C.getTypeSize(T: VarType) : C.getTypeSize(T: Type);
9146 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
9147 : Type->hasSignedIntegerRepresentation();
9148 Type = C.getIntTypeForBitwidth(DestWidth: NewSize, Signed: IsSigned);
9149 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: Type)) {
9150 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: Type,
9151 Action: AssignmentAction::Converting,
9152 /*AllowExplicit=*/true);
9153 if (!Diff.isUsable())
9154 return nullptr;
9155 }
9156 }
9157 if (LimitedType) {
9158 unsigned NewSize = (C.getTypeSize(T: Type) > 32) ? 64 : 32;
9159 if (NewSize != C.getTypeSize(T: Type)) {
9160 if (NewSize < C.getTypeSize(T: Type)) {
9161 assert(NewSize == 64 && "incorrect loop var size");
9162 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::warn_omp_loop_64_bit_var)
9163 << InitSrcRange << ConditionSrcRange;
9164 }
9165 QualType NewType = C.getIntTypeForBitwidth(
9166 DestWidth: NewSize, Signed: Type->hasSignedIntegerRepresentation() ||
9167 C.getTypeSize(T: Type) < NewSize);
9168 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: NewType)) {
9169 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: NewType,
9170 Action: AssignmentAction::Converting,
9171 /*AllowExplicit=*/true);
9172 if (!Diff.isUsable())
9173 return nullptr;
9174 }
9175 }
9176 }
9177
9178 return Diff.get();
9179}
9180
9181std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
9182 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
9183 // Do not build for iterators, they cannot be used in non-rectangular loop
9184 // nests.
9185 if (LCDecl->getType()->isRecordType())
9186 return std::make_pair(x: nullptr, y: nullptr);
9187 // If we subtract, the min is in the condition, otherwise the min is in the
9188 // init value.
9189 Expr *MinExpr = nullptr;
9190 Expr *MaxExpr = nullptr;
9191 Expr *LBExpr = *TestIsLessOp ? LB : UB;
9192 Expr *UBExpr = *TestIsLessOp ? UB : LB;
9193 bool LBNonRect =
9194 *TestIsLessOp ? InitDependOnLC.has_value() : CondDependOnLC.has_value();
9195 bool UBNonRect =
9196 *TestIsLessOp ? CondDependOnLC.has_value() : InitDependOnLC.has_value();
9197 Expr *Lower =
9198 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, Capture: LBExpr, Captures).get();
9199 Expr *Upper =
9200 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, Capture: UBExpr, Captures).get();
9201 if (!Upper || !Lower)
9202 return std::make_pair(x: nullptr, y: nullptr);
9203
9204 if (*TestIsLessOp)
9205 MinExpr = Lower;
9206 else
9207 MaxExpr = Upper;
9208
9209 // Build minimum/maximum value based on number of iterations.
9210 QualType VarType = LCDecl->getType().getNonReferenceType();
9211
9212 ExprResult Diff = calculateNumIters(
9213 SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType, TestIsStrictOp,
9214 /*RoundToStep=*/false, Captures, InitDependOnLC, CondDependOnLC);
9215
9216 if (!Diff.isUsable())
9217 return std::make_pair(x: nullptr, y: nullptr);
9218
9219 // ((Upper - Lower [- 1]) / Step) * Step
9220 // Parentheses (for dumping/debugging purposes only).
9221 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9222 if (!Diff.isUsable())
9223 return std::make_pair(x: nullptr, y: nullptr);
9224
9225 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
9226 if (!NewStep.isUsable())
9227 return std::make_pair(x: nullptr, y: nullptr);
9228 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Mul, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
9229 if (!Diff.isUsable())
9230 return std::make_pair(x: nullptr, y: nullptr);
9231
9232 // Parentheses (for dumping/debugging purposes only).
9233 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9234 if (!Diff.isUsable())
9235 return std::make_pair(x: nullptr, y: nullptr);
9236
9237 // Convert to the ptrdiff_t, if original type is pointer.
9238 if (VarType->isAnyPointerType() &&
9239 !SemaRef.Context.hasSameType(
9240 T1: Diff.get()->getType(),
9241 T2: SemaRef.Context.getUnsignedPointerDiffType())) {
9242 Diff = SemaRef.PerformImplicitConversion(
9243 From: Diff.get(), ToType: SemaRef.Context.getUnsignedPointerDiffType(),
9244 Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
9245 }
9246 if (!Diff.isUsable())
9247 return std::make_pair(x: nullptr, y: nullptr);
9248
9249 if (*TestIsLessOp) {
9250 // MinExpr = Lower;
9251 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
9252 Diff = SemaRef.BuildBinOp(
9253 S, OpLoc: DefaultLoc, Opc: BO_Add,
9254 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get(),
9255 RHSExpr: Diff.get());
9256 if (!Diff.isUsable())
9257 return std::make_pair(x: nullptr, y: nullptr);
9258 } else {
9259 // MaxExpr = Upper;
9260 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
9261 Diff = SemaRef.BuildBinOp(
9262 S, OpLoc: DefaultLoc, Opc: BO_Sub,
9263 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
9264 RHSExpr: Diff.get());
9265 if (!Diff.isUsable())
9266 return std::make_pair(x: nullptr, y: nullptr);
9267 }
9268
9269 // Convert to the original type.
9270 if (SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: VarType))
9271 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: VarType,
9272 Action: AssignmentAction::Converting,
9273 /*AllowExplicit=*/true);
9274 if (!Diff.isUsable())
9275 return std::make_pair(x: nullptr, y: nullptr);
9276
9277 Sema::TentativeAnalysisScope Trap(SemaRef);
9278 Diff = SemaRef.ActOnFinishFullExpr(Expr: Diff.get(), /*DiscardedValue=*/false);
9279 if (!Diff.isUsable())
9280 return std::make_pair(x: nullptr, y: nullptr);
9281
9282 if (*TestIsLessOp)
9283 MaxExpr = Diff.get();
9284 else
9285 MinExpr = Diff.get();
9286
9287 return std::make_pair(x&: MinExpr, y&: MaxExpr);
9288}
9289
9290Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
9291 if (InitDependOnLC || CondDependOnLC)
9292 return Condition;
9293 return nullptr;
9294}
9295
9296Expr *OpenMPIterationSpaceChecker::buildPreCond(
9297 Scope *S, Expr *Cond,
9298 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
9299 // Do not build a precondition when the condition/initialization is dependent
9300 // to prevent pessimistic early loop exit.
9301 // TODO: this can be improved by calculating min/max values but not sure that
9302 // it will be very effective.
9303 if (CondDependOnLC || InitDependOnLC)
9304 return SemaRef
9305 .PerformImplicitConversion(
9306 From: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get(),
9307 ToType: SemaRef.Context.BoolTy, /*Action=*/AssignmentAction::Casting,
9308 /*AllowExplicit=*/true)
9309 .get();
9310
9311 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
9312 Sema::TentativeAnalysisScope Trap(SemaRef);
9313
9314 ExprResult NewLB = tryBuildCapture(SemaRef, Capture: LB, Captures);
9315 ExprResult NewUB = tryBuildCapture(SemaRef, Capture: UB, Captures);
9316 if (!NewLB.isUsable() || !NewUB.isUsable())
9317 return nullptr;
9318
9319 ExprResult CondExpr =
9320 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc,
9321 Opc: *TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
9322 : (TestIsStrictOp ? BO_GT : BO_GE),
9323 LHSExpr: NewLB.get(), RHSExpr: NewUB.get());
9324 if (CondExpr.isUsable()) {
9325 if (!SemaRef.Context.hasSameUnqualifiedType(T1: CondExpr.get()->getType(),
9326 T2: SemaRef.Context.BoolTy))
9327 CondExpr = SemaRef.PerformImplicitConversion(
9328 From: CondExpr.get(), ToType: SemaRef.Context.BoolTy,
9329 /*Action=*/AssignmentAction::Casting,
9330 /*AllowExplicit=*/true);
9331 }
9332
9333 // Otherwise use original loop condition and evaluate it in runtime.
9334 return CondExpr.isUsable() ? CondExpr.get() : Cond;
9335}
9336
9337/// Build reference expression to the counter be used for codegen.
9338DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
9339 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9340 DSAStackTy &DSA) const {
9341 auto *VD = dyn_cast<VarDecl>(Val: LCDecl);
9342 if (!VD) {
9343 VD = SemaRef.OpenMP().isOpenMPCapturedDecl(D: LCDecl);
9344 DeclRefExpr *Ref = buildDeclRefExpr(
9345 S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(), Loc: DefaultLoc);
9346 const DSAStackTy::DSAVarData Data =
9347 DSA.getTopDSA(D: LCDecl, /*FromParent=*/false);
9348 // If the loop control decl is explicitly marked as private, do not mark it
9349 // as captured again.
9350 if (!isOpenMPPrivate(Kind: Data.CKind) || !Data.RefExpr)
9351 Captures.insert(KV: std::make_pair(x: LCRef, y&: Ref));
9352 return Ref;
9353 }
9354 return cast<DeclRefExpr>(Val: LCRef);
9355}
9356
9357Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
9358 if (LCDecl && !LCDecl->isInvalidDecl()) {
9359 QualType Type = LCDecl->getType().getNonReferenceType();
9360 VarDecl *PrivateVar = buildVarDecl(
9361 SemaRef, Loc: DefaultLoc, Type, Name: LCDecl->getName(),
9362 Attrs: LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
9363 OrigRef: isa<VarDecl>(Val: LCDecl)
9364 ? buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: LCDecl), Ty: Type, Loc: DefaultLoc)
9365 : nullptr);
9366 if (PrivateVar->isInvalidDecl())
9367 return nullptr;
9368 return buildDeclRefExpr(S&: SemaRef, D: PrivateVar, Ty: Type, Loc: DefaultLoc);
9369 }
9370 return nullptr;
9371}
9372
9373/// Build initialization of the counter to be used for codegen.
9374Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
9375
9376/// Build step of the counter be used for codegen.
9377Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
9378
9379Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
9380 Scope *S, Expr *Counter,
9381 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
9382 Expr *Inc, OverloadedOperatorKind OOK) {
9383 Expr *Cnt = SemaRef.DefaultLvalueConversion(E: Counter).get();
9384 if (!Cnt)
9385 return nullptr;
9386 if (Inc) {
9387 assert((OOK == OO_Plus || OOK == OO_Minus) &&
9388 "Expected only + or - operations for depend clauses.");
9389 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
9390 Cnt = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BOK, LHSExpr: Cnt, RHSExpr: Inc).get();
9391 if (!Cnt)
9392 return nullptr;
9393 }
9394 QualType VarType = LCDecl->getType().getNonReferenceType();
9395 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
9396 !SemaRef.getLangOpts().CPlusPlus)
9397 return nullptr;
9398 // Upper - Lower
9399 Expr *Upper =
9400 *TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, Capture: LB, Captures).get();
9401 Expr *Lower =
9402 *TestIsLessOp ? tryBuildCapture(SemaRef, Capture: LB, Captures).get() : Cnt;
9403 if (!Upper || !Lower)
9404 return nullptr;
9405
9406 ExprResult Diff =
9407 calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType,
9408 /*TestIsStrictOp=*/false, /*RoundToStep=*/false,
9409 Captures, InitDependOnLC, CondDependOnLC);
9410 if (!Diff.isUsable())
9411 return nullptr;
9412
9413 return Diff.get();
9414}
9415} // namespace
9416
9417void SemaOpenMP::ActOnOpenMPLoopInitialization(SourceLocation ForLoc,
9418 Stmt *Init) {
9419 assert(getLangOpts().OpenMP && "OpenMP is not active.");
9420 assert(Init && "Expected loop in canonical form.");
9421 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
9422 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9423 if (AssociatedLoops == 0 || !isOpenMPLoopDirective(DKind))
9424 return;
9425
9426 DSAStack->loopStart();
9427 llvm::SmallPtrSet<const Decl *, 1> EmptyDeclSet;
9428 OpenMPIterationSpaceChecker ISC(SemaRef, /*SupportsNonRectangular=*/true,
9429 *DSAStack, ForLoc, EmptyDeclSet);
9430 if (!ISC.checkAndSetInit(S: Init, /*EmitDiags=*/false)) {
9431 if (ValueDecl *D = ISC.getLoopDecl()) {
9432 auto *VD = dyn_cast<VarDecl>(Val: D);
9433 DeclRefExpr *PrivateRef = nullptr;
9434 if (!VD) {
9435 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
9436 VD = Private;
9437 } else {
9438 PrivateRef = buildCapture(S&: SemaRef, D, CaptureExpr: ISC.getLoopDeclRefExpr(),
9439 /*WithInit=*/false);
9440 VD = cast<VarDecl>(Val: PrivateRef->getDecl());
9441 }
9442 }
9443 DSAStack->addLoopControlVariable(D, Capture: VD);
9444 const Decl *LD = DSAStack->getPossiblyLoopCounter();
9445 if (LD != D->getCanonicalDecl()) {
9446 DSAStack->resetPossibleLoopCounter();
9447 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: LD))
9448 SemaRef.MarkDeclarationsReferencedInExpr(E: buildDeclRefExpr(
9449 S&: SemaRef, D: const_cast<VarDecl *>(Var),
9450 Ty: Var->getType().getNonLValueExprType(Context: getASTContext()), Loc: ForLoc,
9451 /*RefersToCapture=*/true));
9452 }
9453 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
9454 // Referenced in a Construct, C/C++]. The loop iteration variable in the
9455 // associated for-loop of a simd construct with just one associated
9456 // for-loop may be listed in a linear clause with a constant-linear-step
9457 // that is the increment of the associated for-loop. The loop iteration
9458 // variable(s) in the associated for-loop(s) of a for or parallel for
9459 // construct may be listed in a private or lastprivate clause.
9460 DSAStackTy::DSAVarData DVar =
9461 DSAStack->getTopDSA(D, /*FromParent=*/false);
9462 // If LoopVarRefExpr is nullptr it means the corresponding loop variable
9463 // is declared in the loop and it is predetermined as a private.
9464 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
9465 OpenMPClauseKind PredeterminedCKind =
9466 isOpenMPSimdDirective(DKind)
9467 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
9468 : OMPC_private;
9469 auto IsOpenMPTaskloopDirective = [](OpenMPDirectiveKind DK) {
9470 return getLeafConstructsOrSelf(D: DK).back() == OMPD_taskloop;
9471 };
9472 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
9473 DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
9474 (getLangOpts().OpenMP <= 45 ||
9475 (DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_private))) ||
9476 ((isOpenMPWorksharingDirective(DKind) ||
9477 IsOpenMPTaskloopDirective(DKind) ||
9478 isOpenMPDistributeDirective(DKind)) &&
9479 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
9480 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
9481 (DVar.CKind != OMPC_private || DVar.RefExpr)) {
9482 unsigned OMPVersion = getLangOpts().OpenMP;
9483 Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_var_dsa)
9484 << getOpenMPClauseNameForDiag(C: DVar.CKind)
9485 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion)
9486 << getOpenMPClauseNameForDiag(C: PredeterminedCKind);
9487 if (DVar.RefExpr == nullptr)
9488 DVar.CKind = PredeterminedCKind;
9489 reportOriginalDsa(SemaRef, DSAStack, D, DVar, /*IsLoopIterVar=*/true);
9490 } else if (LoopDeclRefExpr) {
9491 // Make the loop iteration variable private (for worksharing
9492 // constructs), linear (for simd directives with the only one
9493 // associated loop) or lastprivate (for simd directives with several
9494 // collapsed or ordered loops).
9495 if (DVar.CKind == OMPC_unknown)
9496 DSAStack->addDSA(D, E: LoopDeclRefExpr, A: PredeterminedCKind, PrivateCopy: PrivateRef);
9497 }
9498 }
9499 }
9500 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
9501}
9502
9503namespace {
9504// Utility for OpenMP doacross clause kind
9505class OMPDoacrossKind {
9506public:
9507 bool isSource(const OMPDoacrossClause *C) {
9508 return C->getDependenceType() == OMPC_DOACROSS_source ||
9509 C->getDependenceType() == OMPC_DOACROSS_source_omp_cur_iteration;
9510 }
9511 bool isSink(const OMPDoacrossClause *C) {
9512 return C->getDependenceType() == OMPC_DOACROSS_sink;
9513 }
9514 bool isSinkIter(const OMPDoacrossClause *C) {
9515 return C->getDependenceType() == OMPC_DOACROSS_sink_omp_cur_iteration;
9516 }
9517};
9518} // namespace
9519/// Called on a for stmt to check and extract its iteration space
9520/// for further processing (such as collapsing).
9521static bool checkOpenMPIterationSpace(
9522 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
9523 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
9524 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
9525 Expr *OrderedLoopCountExpr,
9526 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9527 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
9528 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9529 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls) {
9530 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind);
9531 // OpenMP [2.9.1, Canonical Loop Form]
9532 // for (init-expr; test-expr; incr-expr) structured-block
9533 // for (range-decl: range-expr) structured-block
9534 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(Val: S))
9535 S = CanonLoop->getLoopStmt();
9536 auto *For = dyn_cast_or_null<ForStmt>(Val: S);
9537 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(Val: S);
9538 // Ranged for is supported only in OpenMP 5.0.
9539 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
9540 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
9541 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_not_for)
9542 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
9543 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion) << TotalNestedLoopCount
9544 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
9545 if (TotalNestedLoopCount > 1) {
9546 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
9547 SemaRef.Diag(Loc: DSA.getConstructLoc(),
9548 DiagID: diag::note_omp_collapse_ordered_expr)
9549 << 2 << CollapseLoopCountExpr->getSourceRange()
9550 << OrderedLoopCountExpr->getSourceRange();
9551 else if (CollapseLoopCountExpr)
9552 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
9553 DiagID: diag::note_omp_collapse_ordered_expr)
9554 << 0 << CollapseLoopCountExpr->getSourceRange();
9555 else if (OrderedLoopCountExpr)
9556 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
9557 DiagID: diag::note_omp_collapse_ordered_expr)
9558 << 1 << OrderedLoopCountExpr->getSourceRange();
9559 }
9560 return true;
9561 }
9562 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
9563 "No loop body.");
9564 // Postpone analysis in dependent contexts for ranged for loops.
9565 if (CXXFor && SemaRef.CurContext->isDependentContext())
9566 return false;
9567
9568 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA,
9569 For ? For->getForLoc() : CXXFor->getForLoc(),
9570 CollapsedLoopVarDecls);
9571
9572 // Check init.
9573 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
9574 if (ISC.checkAndSetInit(S: Init))
9575 return true;
9576
9577 bool HasErrors = false;
9578
9579 // Check loop variable's type.
9580 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
9581 // OpenMP [2.6, Canonical Loop Form]
9582 // Var is one of the following:
9583 // A variable of signed or unsigned integer type.
9584 // For C++, a variable of a random access iterator type.
9585 // For C, a variable of a pointer type.
9586 QualType VarType = LCDecl->getType().getNonReferenceType();
9587 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
9588 !VarType->isPointerType() &&
9589 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
9590 SemaRef.Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_variable_type)
9591 << SemaRef.getLangOpts().CPlusPlus;
9592 HasErrors = true;
9593 }
9594
9595 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
9596 // a Construct
9597 // The loop iteration variable(s) in the associated for-loop(s) of a for or
9598 // parallel for construct is (are) private.
9599 // The loop iteration variable in the associated for-loop of a simd
9600 // construct with just one associated for-loop is linear with a
9601 // constant-linear-step that is the increment of the associated for-loop.
9602 // Exclude loop var from the list of variables with implicitly defined data
9603 // sharing attributes.
9604 VarsWithImplicitDSA.erase(Val: LCDecl);
9605
9606 assert((isOpenMPLoopDirective(DKind) ||
9607 isOpenMPCanonicalLoopSequenceTransformationDirective(DKind)) &&
9608 "DSA for non-loop vars");
9609
9610 // Check test-expr.
9611 HasErrors |= ISC.checkAndSetCond(S: For ? For->getCond() : CXXFor->getCond());
9612
9613 // Check incr-expr.
9614 HasErrors |= ISC.checkAndSetInc(S: For ? For->getInc() : CXXFor->getInc());
9615 }
9616
9617 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
9618 return HasErrors;
9619
9620 // Build the loop's iteration space representation.
9621 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
9622 S: DSA.getCurScope(), Cond: For ? For->getCond() : CXXFor->getCond(), Captures);
9623 ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
9624 ISC.buildNumIterations(S: DSA.getCurScope(), ResultIterSpaces,
9625 LimitedType: (isOpenMPWorksharingDirective(DKind) ||
9626 isOpenMPGenericLoopDirective(DKind) ||
9627 isOpenMPTaskLoopDirective(DKind) ||
9628 isOpenMPDistributeDirective(DKind) ||
9629 isOpenMPLoopTransformationDirective(DKind)),
9630 Captures);
9631 ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
9632 ISC.buildCounterVar(Captures, DSA);
9633 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
9634 ISC.buildPrivateCounterVar();
9635 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
9636 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
9637 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
9638 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
9639 ISC.getConditionSrcRange();
9640 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
9641 ISC.getIncrementSrcRange();
9642 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
9643 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
9644 ISC.isStrictTestOp();
9645 std::tie(args&: ResultIterSpaces[CurrentNestedLoopCount].MinValue,
9646 args&: ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
9647 ISC.buildMinMaxValues(S: DSA.getCurScope(), Captures);
9648 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
9649 ISC.buildFinalCondition(S: DSA.getCurScope());
9650 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
9651 ISC.doesInitDependOnLC();
9652 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
9653 ISC.doesCondDependOnLC();
9654 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
9655 ISC.getLoopDependentIdx();
9656
9657 HasErrors |=
9658 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
9659 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
9660 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
9661 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
9662 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
9663 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
9664 if (!HasErrors && DSA.isOrderedRegion()) {
9665 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
9666 if (CurrentNestedLoopCount <
9667 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
9668 DSA.getOrderedRegionParam().second->setLoopNumIterations(
9669 NumLoop: CurrentNestedLoopCount,
9670 NumIterations: ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
9671 DSA.getOrderedRegionParam().second->setLoopCounter(
9672 NumLoop: CurrentNestedLoopCount,
9673 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
9674 }
9675 }
9676 for (auto &Pair : DSA.getDoacrossDependClauses()) {
9677 auto *DependC = dyn_cast<OMPDependClause>(Val: Pair.first);
9678 auto *DoacrossC = dyn_cast<OMPDoacrossClause>(Val: Pair.first);
9679 unsigned NumLoops =
9680 DependC ? DependC->getNumLoops() : DoacrossC->getNumLoops();
9681 if (CurrentNestedLoopCount >= NumLoops) {
9682 // Erroneous case - clause has some problems.
9683 continue;
9684 }
9685 if (DependC && DependC->getDependencyKind() == OMPC_DEPEND_sink &&
9686 Pair.second.size() <= CurrentNestedLoopCount) {
9687 // Erroneous case - clause has some problems.
9688 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
9689 continue;
9690 }
9691 OMPDoacrossKind ODK;
9692 if (DoacrossC && ODK.isSink(C: DoacrossC) &&
9693 Pair.second.size() <= CurrentNestedLoopCount) {
9694 // Erroneous case - clause has some problems.
9695 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
9696 continue;
9697 }
9698 Expr *CntValue;
9699 SourceLocation DepLoc =
9700 DependC ? DependC->getDependencyLoc() : DoacrossC->getDependenceLoc();
9701 if ((DependC && DependC->getDependencyKind() == OMPC_DEPEND_source) ||
9702 (DoacrossC && ODK.isSource(C: DoacrossC)))
9703 CntValue = ISC.buildOrderedLoopData(
9704 S: DSA.getCurScope(),
9705 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9706 Loc: DepLoc);
9707 else if (DoacrossC && ODK.isSinkIter(C: DoacrossC)) {
9708 Expr *Cnt = SemaRef
9709 .DefaultLvalueConversion(
9710 E: ResultIterSpaces[CurrentNestedLoopCount].CounterVar)
9711 .get();
9712 if (!Cnt)
9713 continue;
9714 // build CounterVar - 1
9715 Expr *Inc =
9716 SemaRef.ActOnIntegerConstant(Loc: DoacrossC->getColonLoc(), /*Val=*/1)
9717 .get();
9718 CntValue = ISC.buildOrderedLoopData(
9719 S: DSA.getCurScope(),
9720 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9721 Loc: DepLoc, Inc, OOK: clang::OO_Minus);
9722 } else
9723 CntValue = ISC.buildOrderedLoopData(
9724 S: DSA.getCurScope(),
9725 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9726 Loc: DepLoc, Inc: Pair.second[CurrentNestedLoopCount].first,
9727 OOK: Pair.second[CurrentNestedLoopCount].second);
9728 if (DependC)
9729 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
9730 else
9731 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
9732 }
9733 }
9734
9735 return HasErrors;
9736}
9737
9738/// Build 'VarRef = Start.
9739static ExprResult
9740buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
9741 ExprResult Start, bool IsNonRectangularLB,
9742 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9743 // Build 'VarRef = Start.
9744 ExprResult NewStart = IsNonRectangularLB
9745 ? Start.get()
9746 : tryBuildCapture(SemaRef, Capture: Start.get(), Captures);
9747 if (!NewStart.isUsable())
9748 return ExprError();
9749 if (!SemaRef.Context.hasSameType(T1: NewStart.get()->getType(),
9750 T2: VarRef.get()->getType())) {
9751 NewStart = SemaRef.PerformImplicitConversion(
9752 From: NewStart.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
9753 /*AllowExplicit=*/true);
9754 if (!NewStart.isUsable())
9755 return ExprError();
9756 }
9757
9758 ExprResult Init =
9759 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
9760 return Init;
9761}
9762
9763/// Build 'VarRef = Start + Iter * Step'.
9764static ExprResult buildCounterUpdate(
9765 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
9766 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
9767 bool IsNonRectangularLB,
9768 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
9769 // Add parentheses (for debugging purposes only).
9770 Iter = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Iter.get());
9771 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
9772 !Step.isUsable())
9773 return ExprError();
9774
9775 ExprResult NewStep = Step;
9776 if (Captures)
9777 NewStep = tryBuildCapture(SemaRef, Capture: Step.get(), Captures&: *Captures);
9778 if (NewStep.isInvalid())
9779 return ExprError();
9780 ExprResult Update =
9781 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Mul, LHSExpr: Iter.get(), RHSExpr: NewStep.get());
9782 if (!Update.isUsable())
9783 return ExprError();
9784
9785 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
9786 // 'VarRef = Start (+|-) Iter * Step'.
9787 if (!Start.isUsable())
9788 return ExprError();
9789 ExprResult NewStart = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Start.get());
9790 if (!NewStart.isUsable())
9791 return ExprError();
9792 if (Captures && !IsNonRectangularLB)
9793 NewStart = tryBuildCapture(SemaRef, Capture: Start.get(), Captures&: *Captures);
9794 if (NewStart.isInvalid())
9795 return ExprError();
9796
9797 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
9798 ExprResult SavedUpdate = Update;
9799 ExprResult UpdateVal;
9800 if (VarRef.get()->getType()->isOverloadableType() ||
9801 NewStart.get()->getType()->isOverloadableType() ||
9802 Update.get()->getType()->isOverloadableType()) {
9803 Sema::TentativeAnalysisScope Trap(SemaRef);
9804
9805 Update =
9806 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
9807 if (Update.isUsable()) {
9808 UpdateVal =
9809 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_SubAssign : BO_AddAssign,
9810 LHSExpr: VarRef.get(), RHSExpr: SavedUpdate.get());
9811 if (UpdateVal.isUsable()) {
9812 Update = SemaRef.CreateBuiltinBinOp(OpLoc: Loc, Opc: BO_Comma, LHSExpr: Update.get(),
9813 RHSExpr: UpdateVal.get());
9814 }
9815 }
9816 }
9817
9818 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
9819 if (!Update.isUsable() || !UpdateVal.isUsable()) {
9820 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_Sub : BO_Add,
9821 LHSExpr: NewStart.get(), RHSExpr: SavedUpdate.get());
9822 if (!Update.isUsable())
9823 return ExprError();
9824
9825 if (!SemaRef.Context.hasSameType(T1: Update.get()->getType(),
9826 T2: VarRef.get()->getType())) {
9827 Update = SemaRef.PerformImplicitConversion(
9828 From: Update.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
9829 /*AllowExplicit=*/true);
9830 if (!Update.isUsable())
9831 return ExprError();
9832 }
9833
9834 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: Update.get());
9835 }
9836 return Update;
9837}
9838
9839/// Convert integer expression \a E to make it have at least \a Bits
9840/// bits.
9841static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
9842 if (E == nullptr)
9843 return ExprError();
9844 ASTContext &C = SemaRef.Context;
9845 QualType OldType = E->getType();
9846 unsigned HasBits = C.getTypeSize(T: OldType);
9847 if (HasBits >= Bits)
9848 return ExprResult(E);
9849 // OK to convert to signed, because new type has more bits than old.
9850 QualType NewType = C.getIntTypeForBitwidth(DestWidth: Bits, /*Signed=*/true);
9851 return SemaRef.PerformImplicitConversion(
9852 From: E, ToType: NewType, Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
9853}
9854
9855/// Check if the given expression \a E is a constant integer that fits
9856/// into \a Bits bits.
9857static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
9858 if (E == nullptr)
9859 return false;
9860 if (std::optional<llvm::APSInt> Result =
9861 E->getIntegerConstantExpr(Ctx: SemaRef.Context))
9862 return Signed ? Result->isSignedIntN(N: Bits) : Result->isIntN(N: Bits);
9863 return false;
9864}
9865
9866/// Build preinits statement for the given declarations.
9867static Stmt *buildPreInits(ASTContext &Context,
9868 MutableArrayRef<Decl *> PreInits) {
9869 if (!PreInits.empty()) {
9870 return new (Context) DeclStmt(
9871 DeclGroupRef::Create(C&: Context, Decls: PreInits.begin(), NumDecls: PreInits.size()),
9872 SourceLocation(), SourceLocation());
9873 }
9874 return nullptr;
9875}
9876
9877/// Append the \p Item or the content of a CompoundStmt to the list \p
9878/// TargetList.
9879///
9880/// A CompoundStmt is used as container in case multiple statements need to be
9881/// stored in lieu of using an explicit list. Flattening is necessary because
9882/// contained DeclStmts need to be visible after the execution of the list. Used
9883/// for OpenMP pre-init declarations/statements.
9884static void appendFlattenedStmtList(SmallVectorImpl<Stmt *> &TargetList,
9885 Stmt *Item) {
9886 // nullptr represents an empty list.
9887 if (!Item)
9888 return;
9889
9890 if (auto *CS = dyn_cast<CompoundStmt>(Val: Item))
9891 llvm::append_range(C&: TargetList, R: CS->body());
9892 else
9893 TargetList.push_back(Elt: Item);
9894}
9895
9896/// Build preinits statement for the given declarations.
9897static Stmt *
9898buildPreInits(ASTContext &Context,
9899 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9900 if (!Captures.empty()) {
9901 SmallVector<Decl *, 16> PreInits;
9902 for (const auto &Pair : Captures)
9903 PreInits.push_back(Elt: Pair.second->getDecl());
9904 return buildPreInits(Context, PreInits);
9905 }
9906 return nullptr;
9907}
9908
9909/// Build pre-init statement for the given statements.
9910static Stmt *buildPreInits(ASTContext &Context, ArrayRef<Stmt *> PreInits) {
9911 if (PreInits.empty())
9912 return nullptr;
9913
9914 SmallVector<Stmt *> Stmts;
9915 for (Stmt *S : PreInits)
9916 appendFlattenedStmtList(TargetList&: Stmts, Item: S);
9917 return CompoundStmt::Create(C: Context, Stmts: PreInits, FPFeatures: FPOptionsOverride(), LB: {}, RB: {});
9918}
9919
9920/// Build postupdate expression for the given list of postupdates expressions.
9921static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
9922 Expr *PostUpdate = nullptr;
9923 if (!PostUpdates.empty()) {
9924 for (Expr *E : PostUpdates) {
9925 Expr *ConvE = S.BuildCStyleCastExpr(
9926 LParenLoc: E->getExprLoc(),
9927 Ty: S.Context.getTrivialTypeSourceInfo(T: S.Context.VoidTy),
9928 RParenLoc: E->getExprLoc(), Op: E)
9929 .get();
9930 PostUpdate = PostUpdate
9931 ? S.CreateBuiltinBinOp(OpLoc: ConvE->getExprLoc(), Opc: BO_Comma,
9932 LHSExpr: PostUpdate, RHSExpr: ConvE)
9933 .get()
9934 : ConvE;
9935 }
9936 }
9937 return PostUpdate;
9938}
9939
9940/// Look for variables declared in the body parts of a for-loop nest. Used
9941/// for verifying loop nest structure before performing a loop collapse
9942/// operation.
9943class ForVarDeclFinder : public DynamicRecursiveASTVisitor {
9944 int NestingDepth = 0;
9945 llvm::SmallPtrSetImpl<const Decl *> &VarDecls;
9946
9947public:
9948 explicit ForVarDeclFinder(llvm::SmallPtrSetImpl<const Decl *> &VD)
9949 : VarDecls(VD) {}
9950
9951 bool VisitForStmt(ForStmt *F) override {
9952 ++NestingDepth;
9953 TraverseStmt(S: F->getBody());
9954 --NestingDepth;
9955 return false;
9956 }
9957
9958 bool VisitCXXForRangeStmt(CXXForRangeStmt *RF) override {
9959 ++NestingDepth;
9960 TraverseStmt(S: RF->getBody());
9961 --NestingDepth;
9962 return false;
9963 }
9964
9965 bool VisitVarDecl(VarDecl *D) override {
9966 Decl *C = D->getCanonicalDecl();
9967 if (NestingDepth > 0)
9968 VarDecls.insert(Ptr: C);
9969 return true;
9970 }
9971};
9972
9973/// Called on a for stmt to check itself and nested loops (if any).
9974/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
9975/// number of collapsed loops otherwise.
9976static unsigned
9977checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
9978 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
9979 DSAStackTy &DSA,
9980 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9981 OMPLoopBasedDirective::HelperExprs &Built) {
9982 // If either of the loop expressions exist and contain errors, we bail out
9983 // early because diagnostics have already been emitted and we can't reliably
9984 // check more about the loop.
9985 if ((CollapseLoopCountExpr && CollapseLoopCountExpr->containsErrors()) ||
9986 (OrderedLoopCountExpr && OrderedLoopCountExpr->containsErrors()))
9987 return 0;
9988
9989 unsigned NestedLoopCount = 1;
9990 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
9991 !isOpenMPLoopTransformationDirective(DKind);
9992 llvm::SmallPtrSet<const Decl *, 4> CollapsedLoopVarDecls;
9993
9994 if (CollapseLoopCountExpr) {
9995 // Found 'collapse' clause - calculate collapse number.
9996 Expr::EvalResult Result;
9997 if (!CollapseLoopCountExpr->isValueDependent() &&
9998 CollapseLoopCountExpr->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext())) {
9999 NestedLoopCount = Result.Val.getInt().getLimitedValue();
10000
10001 ForVarDeclFinder FVDF{CollapsedLoopVarDecls};
10002 FVDF.TraverseStmt(S: AStmt);
10003 } else {
10004 Built.clear(/*Size=*/1);
10005 return 1;
10006 }
10007 }
10008 unsigned OrderedLoopCount = 1;
10009 if (OrderedLoopCountExpr) {
10010 // Found 'ordered' clause - calculate collapse number.
10011 Expr::EvalResult EVResult;
10012 if (!OrderedLoopCountExpr->isValueDependent() &&
10013 OrderedLoopCountExpr->EvaluateAsInt(Result&: EVResult,
10014 Ctx: SemaRef.getASTContext())) {
10015 llvm::APSInt Result = EVResult.Val.getInt();
10016 if (Result.getLimitedValue() < NestedLoopCount) {
10017 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
10018 DiagID: diag::err_omp_wrong_ordered_loop_count)
10019 << OrderedLoopCountExpr->getSourceRange();
10020 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
10021 DiagID: diag::note_collapse_loop_count)
10022 << CollapseLoopCountExpr->getSourceRange();
10023 }
10024 OrderedLoopCount = Result.getLimitedValue();
10025 } else {
10026 Built.clear(/*Size=*/1);
10027 return 1;
10028 }
10029 }
10030 // This is helper routine for loop directives (e.g., 'for', 'simd',
10031 // 'for simd', etc.).
10032 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
10033 unsigned NumLoops = std::max(a: OrderedLoopCount, b: NestedLoopCount);
10034 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops);
10035 if (!OMPLoopBasedDirective::doForAllLoops(
10036 CurStmt: AStmt->IgnoreContainers(
10037 IgnoreCaptured: !isOpenMPCanonicalLoopNestTransformationDirective(DKind)),
10038 TryImperfectlyNestedLoops: SupportsNonPerfectlyNested, NumLoops,
10039 Callback: [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount,
10040 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA,
10041 &IterSpaces, &Captures,
10042 &CollapsedLoopVarDecls](unsigned Cnt, Stmt *CurStmt) {
10043 if (checkOpenMPIterationSpace(
10044 DKind, S: CurStmt, SemaRef, DSA, CurrentNestedLoopCount: Cnt, NestedLoopCount,
10045 TotalNestedLoopCount: NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr,
10046 VarsWithImplicitDSA, ResultIterSpaces: IterSpaces, Captures,
10047 CollapsedLoopVarDecls))
10048 return true;
10049 if (Cnt > 0 && Cnt >= NestedLoopCount &&
10050 IterSpaces[Cnt].CounterVar) {
10051 // Handle initialization of captured loop iterator variables.
10052 auto *DRE = cast<DeclRefExpr>(Val: IterSpaces[Cnt].CounterVar);
10053 if (isa<OMPCapturedExprDecl>(Val: DRE->getDecl())) {
10054 Captures[DRE] = DRE;
10055 }
10056 }
10057 return false;
10058 },
10059 OnTransformationCallback: [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) {
10060 Stmt *DependentPreInits = Transform->getPreInits();
10061 if (!DependentPreInits)
10062 return;
10063
10064 // Search for pre-init declared variables that need to be captured
10065 // to be referenceable inside the directive.
10066 SmallVector<Stmt *> Constituents;
10067 appendFlattenedStmtList(TargetList&: Constituents, Item: DependentPreInits);
10068 for (Stmt *S : Constituents) {
10069 if (auto *DC = dyn_cast<DeclStmt>(Val: S)) {
10070 for (Decl *C : DC->decls()) {
10071 auto *D = cast<VarDecl>(Val: C);
10072 DeclRefExpr *Ref = buildDeclRefExpr(
10073 S&: SemaRef, D, Ty: D->getType().getNonReferenceType(),
10074 Loc: cast<OMPExecutableDirective>(Val: Transform->getDirective())
10075 ->getBeginLoc());
10076 Captures[Ref] = Ref;
10077 }
10078 }
10079 }
10080 }))
10081 return 0;
10082
10083 Built.clear(/*size=*/Size: NestedLoopCount);
10084
10085 if (SemaRef.CurContext->isDependentContext())
10086 return NestedLoopCount;
10087
10088 // An example of what is generated for the following code:
10089 //
10090 // #pragma omp simd collapse(2) ordered(2)
10091 // for (i = 0; i < NI; ++i)
10092 // for (k = 0; k < NK; ++k)
10093 // for (j = J0; j < NJ; j+=2) {
10094 // <loop body>
10095 // }
10096 //
10097 // We generate the code below.
10098 // Note: the loop body may be outlined in CodeGen.
10099 // Note: some counters may be C++ classes, operator- is used to find number of
10100 // iterations and operator+= to calculate counter value.
10101 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
10102 // or i64 is currently supported).
10103 //
10104 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
10105 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
10106 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
10107 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
10108 // // similar updates for vars in clauses (e.g. 'linear')
10109 // <loop body (using local i and j)>
10110 // }
10111 // i = NI; // assign final values of counters
10112 // j = NJ;
10113 //
10114
10115 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
10116 // the iteration counts of the collapsed for loops.
10117 // Precondition tests if there is at least one iteration (all conditions are
10118 // true).
10119 auto PreCond = ExprResult(IterSpaces[0].PreCond);
10120 Expr *N0 = IterSpaces[0].NumIterations;
10121 ExprResult LastIteration32 = widenIterationCount(
10122 /*Bits=*/32,
10123 E: SemaRef
10124 .PerformImplicitConversion(From: N0->IgnoreImpCasts(), ToType: N0->getType(),
10125 Action: AssignmentAction::Converting,
10126 /*AllowExplicit=*/true)
10127 .get(),
10128 SemaRef);
10129 ExprResult LastIteration64 = widenIterationCount(
10130 /*Bits=*/64,
10131 E: SemaRef
10132 .PerformImplicitConversion(From: N0->IgnoreImpCasts(), ToType: N0->getType(),
10133 Action: AssignmentAction::Converting,
10134 /*AllowExplicit=*/true)
10135 .get(),
10136 SemaRef);
10137
10138 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
10139 return NestedLoopCount;
10140
10141 ASTContext &C = SemaRef.Context;
10142 bool AllCountsNeedLessThan32Bits = C.getTypeSize(T: N0->getType()) < 32;
10143
10144 Scope *CurScope = DSA.getCurScope();
10145 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
10146 if (PreCond.isUsable()) {
10147 PreCond =
10148 SemaRef.BuildBinOp(S: CurScope, OpLoc: PreCond.get()->getExprLoc(), Opc: BO_LAnd,
10149 LHSExpr: PreCond.get(), RHSExpr: IterSpaces[Cnt].PreCond);
10150 }
10151 Expr *N = IterSpaces[Cnt].NumIterations;
10152 SourceLocation Loc = N->getExprLoc();
10153 AllCountsNeedLessThan32Bits &= C.getTypeSize(T: N->getType()) < 32;
10154 if (LastIteration32.isUsable())
10155 LastIteration32 = SemaRef.BuildBinOp(
10156 S: CurScope, OpLoc: Loc, Opc: BO_Mul, LHSExpr: LastIteration32.get(),
10157 RHSExpr: SemaRef
10158 .PerformImplicitConversion(From: N->IgnoreImpCasts(), ToType: N->getType(),
10159 Action: AssignmentAction::Converting,
10160 /*AllowExplicit=*/true)
10161 .get());
10162 if (LastIteration64.isUsable())
10163 LastIteration64 = SemaRef.BuildBinOp(
10164 S: CurScope, OpLoc: Loc, Opc: BO_Mul, LHSExpr: LastIteration64.get(),
10165 RHSExpr: SemaRef
10166 .PerformImplicitConversion(From: N->IgnoreImpCasts(), ToType: N->getType(),
10167 Action: AssignmentAction::Converting,
10168 /*AllowExplicit=*/true)
10169 .get());
10170 }
10171
10172 // Choose either the 32-bit or 64-bit version.
10173 ExprResult LastIteration = LastIteration64;
10174 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
10175 (LastIteration32.isUsable() &&
10176 C.getTypeSize(T: LastIteration32.get()->getType()) == 32 &&
10177 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
10178 fitsInto(
10179 /*Bits=*/32,
10180 Signed: LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
10181 E: LastIteration64.get(), SemaRef))))
10182 LastIteration = LastIteration32;
10183 QualType VType = LastIteration.get()->getType();
10184 QualType RealVType = VType;
10185 QualType StrideVType = VType;
10186 if (isOpenMPTaskLoopDirective(DKind)) {
10187 VType =
10188 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
10189 StrideVType =
10190 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
10191 }
10192
10193 if (!LastIteration.isUsable())
10194 return 0;
10195
10196 // Save the number of iterations.
10197 ExprResult NumIterations = LastIteration;
10198 {
10199 LastIteration = SemaRef.BuildBinOp(
10200 S: CurScope, OpLoc: LastIteration.get()->getExprLoc(), Opc: BO_Sub,
10201 LHSExpr: LastIteration.get(),
10202 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
10203 if (!LastIteration.isUsable())
10204 return 0;
10205 }
10206
10207 // Calculate the last iteration number beforehand instead of doing this on
10208 // each iteration. Do not do this if the number of iterations may be kfold-ed.
10209 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(Ctx: SemaRef.Context);
10210 ExprResult CalcLastIteration;
10211 if (!IsConstant) {
10212 ExprResult SaveRef =
10213 tryBuildCapture(SemaRef, Capture: LastIteration.get(), Captures);
10214 LastIteration = SaveRef;
10215
10216 // Prepare SaveRef + 1.
10217 NumIterations = SemaRef.BuildBinOp(
10218 S: CurScope, OpLoc: SaveRef.get()->getExprLoc(), Opc: BO_Add, LHSExpr: SaveRef.get(),
10219 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
10220 if (!NumIterations.isUsable())
10221 return 0;
10222 }
10223
10224 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
10225
10226 // Build variables passed into runtime, necessary for worksharing directives.
10227 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
10228 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
10229 isOpenMPDistributeDirective(DKind) ||
10230 isOpenMPGenericLoopDirective(DKind) ||
10231 isOpenMPLoopTransformationDirective(DKind)) {
10232 // Lower bound variable, initialized with zero.
10233 VarDecl *LBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.lb");
10234 LB = buildDeclRefExpr(S&: SemaRef, D: LBDecl, Ty: VType, Loc: InitLoc);
10235 SemaRef.AddInitializerToDecl(dcl: LBDecl,
10236 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10237 /*DirectInit=*/false);
10238
10239 // Upper bound variable, initialized with last iteration number.
10240 VarDecl *UBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.ub");
10241 UB = buildDeclRefExpr(S&: SemaRef, D: UBDecl, Ty: VType, Loc: InitLoc);
10242 SemaRef.AddInitializerToDecl(dcl: UBDecl, init: LastIteration.get(),
10243 /*DirectInit=*/false);
10244
10245 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
10246 // This will be used to implement clause 'lastprivate'.
10247 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(DestWidth: 32, Signed: true);
10248 VarDecl *ILDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: Int32Ty, Name: ".omp.is_last");
10249 IL = buildDeclRefExpr(S&: SemaRef, D: ILDecl, Ty: Int32Ty, Loc: InitLoc);
10250 SemaRef.AddInitializerToDecl(dcl: ILDecl,
10251 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10252 /*DirectInit=*/false);
10253
10254 // Stride variable returned by runtime (we initialize it to 1 by default).
10255 VarDecl *STDecl =
10256 buildVarDecl(SemaRef, Loc: InitLoc, Type: StrideVType, Name: ".omp.stride");
10257 ST = buildDeclRefExpr(S&: SemaRef, D: STDecl, Ty: StrideVType, Loc: InitLoc);
10258 SemaRef.AddInitializerToDecl(dcl: STDecl,
10259 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 1).get(),
10260 /*DirectInit=*/false);
10261
10262 // Build expression: UB = min(UB, LastIteration)
10263 // It is necessary for CodeGen of directives with static scheduling.
10264 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_GT,
10265 LHSExpr: UB.get(), RHSExpr: LastIteration.get());
10266 ExprResult CondOp = SemaRef.ActOnConditionalOp(
10267 QuestionLoc: LastIteration.get()->getExprLoc(), ColonLoc: InitLoc, CondExpr: IsUBGreater.get(),
10268 LHSExpr: LastIteration.get(), RHSExpr: UB.get());
10269 EUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: UB.get(),
10270 RHSExpr: CondOp.get());
10271 EUB = SemaRef.ActOnFinishFullExpr(Expr: EUB.get(), /*DiscardedValue=*/false);
10272
10273 // If we have a combined directive that combines 'distribute', 'for' or
10274 // 'simd' we need to be able to access the bounds of the schedule of the
10275 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
10276 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
10277 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10278 // Lower bound variable, initialized with zero.
10279 VarDecl *CombLBDecl =
10280 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.lb");
10281 CombLB = buildDeclRefExpr(S&: SemaRef, D: CombLBDecl, Ty: VType, Loc: InitLoc);
10282 SemaRef.AddInitializerToDecl(
10283 dcl: CombLBDecl, init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10284 /*DirectInit=*/false);
10285
10286 // Upper bound variable, initialized with last iteration number.
10287 VarDecl *CombUBDecl =
10288 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.ub");
10289 CombUB = buildDeclRefExpr(S&: SemaRef, D: CombUBDecl, Ty: VType, Loc: InitLoc);
10290 SemaRef.AddInitializerToDecl(dcl: CombUBDecl, init: LastIteration.get(),
10291 /*DirectInit=*/false);
10292
10293 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
10294 S: CurScope, OpLoc: InitLoc, Opc: BO_GT, LHSExpr: CombUB.get(), RHSExpr: LastIteration.get());
10295 ExprResult CombCondOp =
10296 SemaRef.ActOnConditionalOp(QuestionLoc: InitLoc, ColonLoc: InitLoc, CondExpr: CombIsUBGreater.get(),
10297 LHSExpr: LastIteration.get(), RHSExpr: CombUB.get());
10298 CombEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
10299 RHSExpr: CombCondOp.get());
10300 CombEUB =
10301 SemaRef.ActOnFinishFullExpr(Expr: CombEUB.get(), /*DiscardedValue=*/false);
10302
10303 const CapturedDecl *CD = cast<CapturedStmt>(Val: AStmt)->getCapturedDecl();
10304 // We expect to have at least 2 more parameters than the 'parallel'
10305 // directive does - the lower and upper bounds of the previous schedule.
10306 assert(CD->getNumParams() >= 4 &&
10307 "Unexpected number of parameters in loop combined directive");
10308
10309 // Set the proper type for the bounds given what we learned from the
10310 // enclosed loops.
10311 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/i: 2);
10312 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/i: 3);
10313
10314 // Previous lower and upper bounds are obtained from the region
10315 // parameters.
10316 PrevLB =
10317 buildDeclRefExpr(S&: SemaRef, D: PrevLBDecl, Ty: PrevLBDecl->getType(), Loc: InitLoc);
10318 PrevUB =
10319 buildDeclRefExpr(S&: SemaRef, D: PrevUBDecl, Ty: PrevUBDecl->getType(), Loc: InitLoc);
10320 }
10321 }
10322
10323 // Build the iteration variable and its initialization before loop.
10324 ExprResult IV;
10325 ExprResult Init, CombInit;
10326 {
10327 VarDecl *IVDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: RealVType, Name: ".omp.iv");
10328 IV = buildDeclRefExpr(S&: SemaRef, D: IVDecl, Ty: RealVType, Loc: InitLoc);
10329 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
10330 isOpenMPGenericLoopDirective(DKind) ||
10331 isOpenMPTaskLoopDirective(DKind) ||
10332 isOpenMPDistributeDirective(DKind) ||
10333 isOpenMPLoopTransformationDirective(DKind))
10334 ? LB.get()
10335 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
10336 Init = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: RHS);
10337 Init = SemaRef.ActOnFinishFullExpr(Expr: Init.get(), /*DiscardedValue=*/false);
10338
10339 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10340 Expr *CombRHS =
10341 (isOpenMPWorksharingDirective(DKind) ||
10342 isOpenMPGenericLoopDirective(DKind) ||
10343 isOpenMPTaskLoopDirective(DKind) ||
10344 isOpenMPDistributeDirective(DKind))
10345 ? CombLB.get()
10346 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
10347 CombInit =
10348 SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: CombRHS);
10349 CombInit =
10350 SemaRef.ActOnFinishFullExpr(Expr: CombInit.get(), /*DiscardedValue=*/false);
10351 }
10352 }
10353
10354 bool UseStrictCompare =
10355 RealVType->hasUnsignedIntegerRepresentation() &&
10356 llvm::all_of(Range&: IterSpaces, P: [](const LoopIterationSpace &LIS) {
10357 return LIS.IsStrictCompare;
10358 });
10359 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
10360 // unsigned IV)) for worksharing loops.
10361 SourceLocation CondLoc = AStmt->getBeginLoc();
10362 Expr *BoundUB = UB.get();
10363 if (UseStrictCompare) {
10364 BoundUB =
10365 SemaRef
10366 .BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundUB,
10367 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10368 .get();
10369 BoundUB =
10370 SemaRef.ActOnFinishFullExpr(Expr: BoundUB, /*DiscardedValue=*/false).get();
10371 }
10372 ExprResult Cond =
10373 (isOpenMPWorksharingDirective(DKind) ||
10374 isOpenMPGenericLoopDirective(DKind) ||
10375 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) ||
10376 isOpenMPLoopTransformationDirective(DKind))
10377 ? SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc,
10378 Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(),
10379 RHSExpr: BoundUB)
10380 : SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
10381 RHSExpr: NumIterations.get());
10382 ExprResult CombDistCond;
10383 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10384 CombDistCond = SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
10385 RHSExpr: NumIterations.get());
10386 }
10387
10388 ExprResult CombCond;
10389 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10390 Expr *BoundCombUB = CombUB.get();
10391 if (UseStrictCompare) {
10392 BoundCombUB =
10393 SemaRef
10394 .BuildBinOp(
10395 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundCombUB,
10396 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10397 .get();
10398 BoundCombUB =
10399 SemaRef.ActOnFinishFullExpr(Expr: BoundCombUB, /*DiscardedValue=*/false)
10400 .get();
10401 }
10402 CombCond =
10403 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
10404 LHSExpr: IV.get(), RHSExpr: BoundCombUB);
10405 }
10406 // Loop increment (IV = IV + 1)
10407 SourceLocation IncLoc = AStmt->getBeginLoc();
10408 ExprResult Inc =
10409 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: IV.get(),
10410 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: IncLoc, Val: 1).get());
10411 if (!Inc.isUsable())
10412 return 0;
10413 Inc = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: Inc.get());
10414 Inc = SemaRef.ActOnFinishFullExpr(Expr: Inc.get(), /*DiscardedValue=*/false);
10415 if (!Inc.isUsable())
10416 return 0;
10417
10418 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
10419 // Used for directives with static scheduling.
10420 // In combined construct, add combined version that use CombLB and CombUB
10421 // base variables for the update
10422 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
10423 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
10424 isOpenMPGenericLoopDirective(DKind) ||
10425 isOpenMPDistributeDirective(DKind) ||
10426 isOpenMPLoopTransformationDirective(DKind)) {
10427 // LB + ST
10428 NextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: LB.get(), RHSExpr: ST.get());
10429 if (!NextLB.isUsable())
10430 return 0;
10431 // LB = LB + ST
10432 NextLB =
10433 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: LB.get(), RHSExpr: NextLB.get());
10434 NextLB =
10435 SemaRef.ActOnFinishFullExpr(Expr: NextLB.get(), /*DiscardedValue=*/false);
10436 if (!NextLB.isUsable())
10437 return 0;
10438 // UB + ST
10439 NextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: UB.get(), RHSExpr: ST.get());
10440 if (!NextUB.isUsable())
10441 return 0;
10442 // UB = UB + ST
10443 NextUB =
10444 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: UB.get(), RHSExpr: NextUB.get());
10445 NextUB =
10446 SemaRef.ActOnFinishFullExpr(Expr: NextUB.get(), /*DiscardedValue=*/false);
10447 if (!NextUB.isUsable())
10448 return 0;
10449 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10450 CombNextLB =
10451 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombLB.get(), RHSExpr: ST.get());
10452 if (!NextLB.isUsable())
10453 return 0;
10454 // LB = LB + ST
10455 CombNextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombLB.get(),
10456 RHSExpr: CombNextLB.get());
10457 CombNextLB = SemaRef.ActOnFinishFullExpr(Expr: CombNextLB.get(),
10458 /*DiscardedValue=*/false);
10459 if (!CombNextLB.isUsable())
10460 return 0;
10461 // UB + ST
10462 CombNextUB =
10463 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombUB.get(), RHSExpr: ST.get());
10464 if (!CombNextUB.isUsable())
10465 return 0;
10466 // UB = UB + ST
10467 CombNextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
10468 RHSExpr: CombNextUB.get());
10469 CombNextUB = SemaRef.ActOnFinishFullExpr(Expr: CombNextUB.get(),
10470 /*DiscardedValue=*/false);
10471 if (!CombNextUB.isUsable())
10472 return 0;
10473 }
10474 }
10475
10476 // Create increment expression for distribute loop when combined in a same
10477 // directive with for as IV = IV + ST; ensure upper bound expression based
10478 // on PrevUB instead of NumIterations - used to implement 'for' when found
10479 // in combination with 'distribute', like in 'distribute parallel for'
10480 SourceLocation DistIncLoc = AStmt->getBeginLoc();
10481 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
10482 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10483 DistCond = SemaRef.BuildBinOp(
10484 S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(), RHSExpr: BoundUB);
10485 assert(DistCond.isUsable() && "distribute cond expr was not built");
10486
10487 DistInc =
10488 SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Add, LHSExpr: IV.get(), RHSExpr: ST.get());
10489 assert(DistInc.isUsable() && "distribute inc expr was not built");
10490 DistInc = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: IV.get(),
10491 RHSExpr: DistInc.get());
10492 DistInc =
10493 SemaRef.ActOnFinishFullExpr(Expr: DistInc.get(), /*DiscardedValue=*/false);
10494 assert(DistInc.isUsable() && "distribute inc expr was not built");
10495
10496 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
10497 // construct
10498 ExprResult NewPrevUB = PrevUB;
10499 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
10500 if (!SemaRef.Context.hasSameType(T1: UB.get()->getType(),
10501 T2: PrevUB.get()->getType())) {
10502 NewPrevUB = SemaRef.BuildCStyleCastExpr(
10503 LParenLoc: DistEUBLoc,
10504 Ty: SemaRef.Context.getTrivialTypeSourceInfo(T: UB.get()->getType()),
10505 RParenLoc: DistEUBLoc, Op: NewPrevUB.get());
10506 if (!NewPrevUB.isUsable())
10507 return 0;
10508 }
10509 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistEUBLoc, Opc: BO_GT,
10510 LHSExpr: UB.get(), RHSExpr: NewPrevUB.get());
10511 ExprResult CondOp = SemaRef.ActOnConditionalOp(
10512 QuestionLoc: DistEUBLoc, ColonLoc: DistEUBLoc, CondExpr: IsUBGreater.get(), LHSExpr: NewPrevUB.get(), RHSExpr: UB.get());
10513 PrevEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: UB.get(),
10514 RHSExpr: CondOp.get());
10515 PrevEUB =
10516 SemaRef.ActOnFinishFullExpr(Expr: PrevEUB.get(), /*DiscardedValue=*/false);
10517
10518 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
10519 // parallel for is in combination with a distribute directive with
10520 // schedule(static, 1)
10521 Expr *BoundPrevUB = PrevUB.get();
10522 if (UseStrictCompare) {
10523 BoundPrevUB =
10524 SemaRef
10525 .BuildBinOp(
10526 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundPrevUB,
10527 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10528 .get();
10529 BoundPrevUB =
10530 SemaRef.ActOnFinishFullExpr(Expr: BoundPrevUB, /*DiscardedValue=*/false)
10531 .get();
10532 }
10533 ParForInDistCond =
10534 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
10535 LHSExpr: IV.get(), RHSExpr: BoundPrevUB);
10536 }
10537
10538 // Build updates and final values of the loop counters.
10539 bool HasErrors = false;
10540 Built.Counters.resize(N: NestedLoopCount);
10541 Built.Inits.resize(N: NestedLoopCount);
10542 Built.Updates.resize(N: NestedLoopCount);
10543 Built.Finals.resize(N: NestedLoopCount);
10544 Built.DependentCounters.resize(N: NestedLoopCount);
10545 Built.DependentInits.resize(N: NestedLoopCount);
10546 Built.FinalsConditions.resize(N: NestedLoopCount);
10547 {
10548 // We implement the following algorithm for obtaining the
10549 // original loop iteration variable values based on the
10550 // value of the collapsed loop iteration variable IV.
10551 //
10552 // Let n+1 be the number of collapsed loops in the nest.
10553 // Iteration variables (I0, I1, .... In)
10554 // Iteration counts (N0, N1, ... Nn)
10555 //
10556 // Acc = IV;
10557 //
10558 // To compute Ik for loop k, 0 <= k <= n, generate:
10559 // Prod = N(k+1) * N(k+2) * ... * Nn;
10560 // Ik = Acc / Prod;
10561 // Acc -= Ik * Prod;
10562 //
10563 ExprResult Acc = IV;
10564 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
10565 LoopIterationSpace &IS = IterSpaces[Cnt];
10566 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
10567 ExprResult Iter;
10568
10569 // Compute prod
10570 ExprResult Prod = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
10571 for (unsigned int K = Cnt + 1; K < NestedLoopCount; ++K)
10572 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Prod.get(),
10573 RHSExpr: IterSpaces[K].NumIterations);
10574
10575 // Iter = Acc / Prod
10576 // If there is at least one more inner loop to avoid
10577 // multiplication by 1.
10578 if (Cnt + 1 < NestedLoopCount)
10579 Iter =
10580 SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Div, LHSExpr: Acc.get(), RHSExpr: Prod.get());
10581 else
10582 Iter = Acc;
10583 if (!Iter.isUsable()) {
10584 HasErrors = true;
10585 break;
10586 }
10587
10588 // Update Acc:
10589 // Acc -= Iter * Prod
10590 // Check if there is at least one more inner loop to avoid
10591 // multiplication by 1.
10592 if (Cnt + 1 < NestedLoopCount)
10593 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Iter.get(),
10594 RHSExpr: Prod.get());
10595 else
10596 Prod = Iter;
10597 Acc = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Sub, LHSExpr: Acc.get(), RHSExpr: Prod.get());
10598
10599 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
10600 auto *VD = cast<VarDecl>(Val: cast<DeclRefExpr>(Val: IS.CounterVar)->getDecl());
10601 DeclRefExpr *CounterVar = buildDeclRefExpr(
10602 S&: SemaRef, D: VD, Ty: IS.CounterVar->getType(), Loc: IS.CounterVar->getExprLoc(),
10603 /*RefersToCapture=*/true);
10604 ExprResult Init =
10605 buildCounterInit(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
10606 Start: IS.CounterInit, IsNonRectangularLB: IS.IsNonRectangularLB, Captures);
10607 if (!Init.isUsable()) {
10608 HasErrors = true;
10609 break;
10610 }
10611 ExprResult Update = buildCounterUpdate(
10612 SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar, Start: IS.CounterInit, Iter,
10613 Step: IS.CounterStep, Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
10614 if (!Update.isUsable()) {
10615 HasErrors = true;
10616 break;
10617 }
10618
10619 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
10620 ExprResult Final =
10621 buildCounterUpdate(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
10622 Start: IS.CounterInit, Iter: IS.NumIterations, Step: IS.CounterStep,
10623 Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
10624 if (!Final.isUsable()) {
10625 HasErrors = true;
10626 break;
10627 }
10628
10629 if (!Update.isUsable() || !Final.isUsable()) {
10630 HasErrors = true;
10631 break;
10632 }
10633 // Save results
10634 Built.Counters[Cnt] = IS.CounterVar;
10635 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
10636 Built.Inits[Cnt] = Init.get();
10637 Built.Updates[Cnt] = Update.get();
10638 Built.Finals[Cnt] = Final.get();
10639 Built.DependentCounters[Cnt] = nullptr;
10640 Built.DependentInits[Cnt] = nullptr;
10641 Built.FinalsConditions[Cnt] = nullptr;
10642 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
10643 Built.DependentCounters[Cnt] = Built.Counters[IS.LoopDependentIdx - 1];
10644 Built.DependentInits[Cnt] = Built.Inits[IS.LoopDependentIdx - 1];
10645 Built.FinalsConditions[Cnt] = IS.FinalCondition;
10646 }
10647 }
10648 }
10649
10650 if (HasErrors)
10651 return 0;
10652
10653 // Save results
10654 Built.IterationVarRef = IV.get();
10655 Built.LastIteration = LastIteration.get();
10656 Built.NumIterations = NumIterations.get();
10657 Built.CalcLastIteration = SemaRef
10658 .ActOnFinishFullExpr(Expr: CalcLastIteration.get(),
10659 /*DiscardedValue=*/false)
10660 .get();
10661 Built.PreCond = PreCond.get();
10662 Built.PreInits = buildPreInits(Context&: C, Captures);
10663 Built.Cond = Cond.get();
10664 Built.Init = Init.get();
10665 Built.Inc = Inc.get();
10666 Built.LB = LB.get();
10667 Built.UB = UB.get();
10668 Built.IL = IL.get();
10669 Built.ST = ST.get();
10670 Built.EUB = EUB.get();
10671 Built.NLB = NextLB.get();
10672 Built.NUB = NextUB.get();
10673 Built.PrevLB = PrevLB.get();
10674 Built.PrevUB = PrevUB.get();
10675 Built.DistInc = DistInc.get();
10676 Built.PrevEUB = PrevEUB.get();
10677 Built.DistCombinedFields.LB = CombLB.get();
10678 Built.DistCombinedFields.UB = CombUB.get();
10679 Built.DistCombinedFields.EUB = CombEUB.get();
10680 Built.DistCombinedFields.Init = CombInit.get();
10681 Built.DistCombinedFields.Cond = CombCond.get();
10682 Built.DistCombinedFields.NLB = CombNextLB.get();
10683 Built.DistCombinedFields.NUB = CombNextUB.get();
10684 Built.DistCombinedFields.DistCond = CombDistCond.get();
10685 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
10686
10687 return NestedLoopCount;
10688}
10689
10690static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
10691 auto CollapseClauses =
10692 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
10693 if (CollapseClauses.begin() != CollapseClauses.end())
10694 return (*CollapseClauses.begin())->getNumForLoops();
10695 return nullptr;
10696}
10697
10698static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
10699 auto OrderedClauses =
10700 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
10701 if (OrderedClauses.begin() != OrderedClauses.end())
10702 return (*OrderedClauses.begin())->getNumForLoops();
10703 return nullptr;
10704}
10705
10706static bool checkSimdlenSafelenSpecified(Sema &S,
10707 const ArrayRef<OMPClause *> Clauses) {
10708 const OMPSafelenClause *Safelen = nullptr;
10709 const OMPSimdlenClause *Simdlen = nullptr;
10710
10711 for (const OMPClause *Clause : Clauses) {
10712 if (Clause->getClauseKind() == OMPC_safelen)
10713 Safelen = cast<OMPSafelenClause>(Val: Clause);
10714 else if (Clause->getClauseKind() == OMPC_simdlen)
10715 Simdlen = cast<OMPSimdlenClause>(Val: Clause);
10716 if (Safelen && Simdlen)
10717 break;
10718 }
10719
10720 if (Simdlen && Safelen) {
10721 const Expr *SimdlenLength = Simdlen->getSimdlen();
10722 const Expr *SafelenLength = Safelen->getSafelen();
10723 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
10724 SimdlenLength->isInstantiationDependent() ||
10725 SimdlenLength->containsUnexpandedParameterPack())
10726 return false;
10727 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
10728 SafelenLength->isInstantiationDependent() ||
10729 SafelenLength->containsUnexpandedParameterPack())
10730 return false;
10731 Expr::EvalResult SimdlenResult, SafelenResult;
10732 SimdlenLength->EvaluateAsInt(Result&: SimdlenResult, Ctx: S.Context);
10733 SafelenLength->EvaluateAsInt(Result&: SafelenResult, Ctx: S.Context);
10734 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
10735 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
10736 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
10737 // If both simdlen and safelen clauses are specified, the value of the
10738 // simdlen parameter must be less than or equal to the value of the safelen
10739 // parameter.
10740 if (SimdlenRes > SafelenRes) {
10741 S.Diag(Loc: SimdlenLength->getExprLoc(),
10742 DiagID: diag::err_omp_wrong_simdlen_safelen_values)
10743 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
10744 return true;
10745 }
10746 }
10747 return false;
10748}
10749
10750StmtResult SemaOpenMP::ActOnOpenMPSimdDirective(
10751 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10752 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10753 if (!AStmt)
10754 return StmtError();
10755
10756 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_simd, AStmt);
10757
10758 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10759 OMPLoopBasedDirective::HelperExprs B;
10760 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10761 // define the nested loops number.
10762 unsigned NestedLoopCount = checkOpenMPLoop(
10763 DKind: OMPD_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10764 AStmt: CS, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10765 if (NestedLoopCount == 0)
10766 return StmtError();
10767
10768 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10769 return StmtError();
10770
10771 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
10772 return StmtError();
10773
10774 auto *SimdDirective = OMPSimdDirective::Create(
10775 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10776 return SimdDirective;
10777}
10778
10779StmtResult SemaOpenMP::ActOnOpenMPForDirective(
10780 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10781 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10782 if (!AStmt)
10783 return StmtError();
10784
10785 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10786 OMPLoopBasedDirective::HelperExprs B;
10787 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10788 // define the nested loops number.
10789 unsigned NestedLoopCount = checkOpenMPLoop(
10790 DKind: OMPD_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10791 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10792 if (NestedLoopCount == 0)
10793 return StmtError();
10794
10795 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10796 return StmtError();
10797
10798 auto *ForDirective = OMPForDirective::Create(
10799 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
10800 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10801 return ForDirective;
10802}
10803
10804StmtResult SemaOpenMP::ActOnOpenMPForSimdDirective(
10805 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10806 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10807 if (!AStmt)
10808 return StmtError();
10809
10810 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_for_simd, AStmt);
10811
10812 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10813 OMPLoopBasedDirective::HelperExprs B;
10814 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10815 // define the nested loops number.
10816 unsigned NestedLoopCount =
10817 checkOpenMPLoop(DKind: OMPD_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
10818 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
10819 VarsWithImplicitDSA, Built&: B);
10820 if (NestedLoopCount == 0)
10821 return StmtError();
10822
10823 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10824 return StmtError();
10825
10826 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
10827 return StmtError();
10828
10829 return OMPForSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10830 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10831}
10832
10833static bool checkSectionsDirective(Sema &SemaRef, OpenMPDirectiveKind DKind,
10834 Stmt *AStmt, DSAStackTy *Stack) {
10835 if (!AStmt)
10836 return true;
10837
10838 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10839 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
10840 auto BaseStmt = AStmt;
10841 while (auto *CS = dyn_cast_or_null<CapturedStmt>(Val: BaseStmt))
10842 BaseStmt = CS->getCapturedStmt();
10843 if (auto *C = dyn_cast_or_null<CompoundStmt>(Val: BaseStmt)) {
10844 auto S = C->children();
10845 if (S.begin() == S.end())
10846 return true;
10847 // All associated statements must be '#pragma omp section' except for
10848 // the first one.
10849 for (Stmt *SectionStmt : llvm::drop_begin(RangeOrContainer&: S)) {
10850 if (!SectionStmt || !isa<OMPSectionDirective>(Val: SectionStmt)) {
10851 if (SectionStmt)
10852 SemaRef.Diag(Loc: SectionStmt->getBeginLoc(),
10853 DiagID: diag::err_omp_sections_substmt_not_section)
10854 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
10855 return true;
10856 }
10857 cast<OMPSectionDirective>(Val: SectionStmt)
10858 ->setHasCancel(Stack->isCancelRegion());
10859 }
10860 } else {
10861 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_sections_not_compound_stmt)
10862 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
10863 return true;
10864 }
10865 return false;
10866}
10867
10868StmtResult
10869SemaOpenMP::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
10870 Stmt *AStmt, SourceLocation StartLoc,
10871 SourceLocation EndLoc) {
10872 if (checkSectionsDirective(SemaRef, DKind: OMPD_sections, AStmt, DSAStack))
10873 return StmtError();
10874
10875 SemaRef.setFunctionHasBranchProtectedScope();
10876
10877 return OMPSectionsDirective::Create(
10878 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
10879 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10880}
10881
10882StmtResult SemaOpenMP::ActOnOpenMPSectionDirective(Stmt *AStmt,
10883 SourceLocation StartLoc,
10884 SourceLocation EndLoc) {
10885 if (!AStmt)
10886 return StmtError();
10887
10888 SemaRef.setFunctionHasBranchProtectedScope();
10889 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
10890
10891 return OMPSectionDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt,
10892 DSAStack->isCancelRegion());
10893}
10894
10895static Expr *getDirectCallExpr(Expr *E) {
10896 E = E->IgnoreParenCasts()->IgnoreImplicit();
10897 if (auto *CE = dyn_cast<CallExpr>(Val: E))
10898 if (CE->getDirectCallee())
10899 return E;
10900 return nullptr;
10901}
10902
10903StmtResult
10904SemaOpenMP::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses,
10905 Stmt *AStmt, SourceLocation StartLoc,
10906 SourceLocation EndLoc) {
10907 if (!AStmt)
10908 return StmtError();
10909
10910 Stmt *S = cast<CapturedStmt>(Val: AStmt)->getCapturedStmt();
10911
10912 // 5.1 OpenMP
10913 // expression-stmt : an expression statement with one of the following forms:
10914 // expression = target-call ( [expression-list] );
10915 // target-call ( [expression-list] );
10916
10917 SourceLocation TargetCallLoc;
10918
10919 if (!SemaRef.CurContext->isDependentContext()) {
10920 Expr *TargetCall = nullptr;
10921
10922 auto *E = dyn_cast<Expr>(Val: S);
10923 if (!E) {
10924 Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
10925 return StmtError();
10926 }
10927
10928 E = E->IgnoreParenCasts()->IgnoreImplicit();
10929
10930 if (auto *BO = dyn_cast<BinaryOperator>(Val: E)) {
10931 if (BO->getOpcode() == BO_Assign)
10932 TargetCall = getDirectCallExpr(E: BO->getRHS());
10933 } else {
10934 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(Val: E))
10935 if (COCE->getOperator() == OO_Equal)
10936 TargetCall = getDirectCallExpr(E: COCE->getArg(Arg: 1));
10937 if (!TargetCall)
10938 TargetCall = getDirectCallExpr(E);
10939 }
10940 if (!TargetCall) {
10941 Diag(Loc: E->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
10942 return StmtError();
10943 }
10944 TargetCallLoc = TargetCall->getExprLoc();
10945 }
10946
10947 SemaRef.setFunctionHasBranchProtectedScope();
10948
10949 return OMPDispatchDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10950 Clauses, AssociatedStmt: AStmt, TargetCallLoc);
10951}
10952
10953static bool checkGenericLoopLastprivate(Sema &S, ArrayRef<OMPClause *> Clauses,
10954 OpenMPDirectiveKind K,
10955 DSAStackTy *Stack) {
10956 bool ErrorFound = false;
10957 for (OMPClause *C : Clauses) {
10958 if (auto *LPC = dyn_cast<OMPLastprivateClause>(Val: C)) {
10959 for (Expr *RefExpr : LPC->varlist()) {
10960 SourceLocation ELoc;
10961 SourceRange ERange;
10962 Expr *SimpleRefExpr = RefExpr;
10963 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
10964 if (ValueDecl *D = Res.first) {
10965 auto &&Info = Stack->isLoopControlVariable(D);
10966 if (!Info.first) {
10967 unsigned OMPVersion = S.getLangOpts().OpenMP;
10968 S.Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_loop_var_non_loop_iteration)
10969 << getOpenMPDirectiveName(D: K, Ver: OMPVersion);
10970 ErrorFound = true;
10971 }
10972 }
10973 }
10974 }
10975 }
10976 return ErrorFound;
10977}
10978
10979StmtResult SemaOpenMP::ActOnOpenMPGenericLoopDirective(
10980 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10981 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10982 if (!AStmt)
10983 return StmtError();
10984
10985 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
10986 // A list item may not appear in a lastprivate clause unless it is the
10987 // loop iteration variable of a loop that is associated with the construct.
10988 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_loop, DSAStack))
10989 return StmtError();
10990
10991 setBranchProtectedScope(SemaRef, DKind: OMPD_loop, AStmt);
10992
10993 OMPLoopDirective::HelperExprs B;
10994 // In presence of clause 'collapse', it will define the nested loops number.
10995 unsigned NestedLoopCount = checkOpenMPLoop(
10996 DKind: OMPD_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10997 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10998 if (NestedLoopCount == 0)
10999 return StmtError();
11000
11001 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11002 "omp loop exprs were not built");
11003
11004 return OMPGenericLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11005 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11006}
11007
11008StmtResult SemaOpenMP::ActOnOpenMPTeamsGenericLoopDirective(
11009 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11010 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11011 if (!AStmt)
11012 return StmtError();
11013
11014 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11015 // A list item may not appear in a lastprivate clause unless it is the
11016 // loop iteration variable of a loop that is associated with the construct.
11017 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_teams_loop, DSAStack))
11018 return StmtError();
11019
11020 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_teams_loop, AStmt);
11021
11022 OMPLoopDirective::HelperExprs B;
11023 // In presence of clause 'collapse', it will define the nested loops number.
11024 unsigned NestedLoopCount =
11025 checkOpenMPLoop(DKind: OMPD_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11026 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11027 VarsWithImplicitDSA, Built&: B);
11028 if (NestedLoopCount == 0)
11029 return StmtError();
11030
11031 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11032 "omp loop exprs were not built");
11033
11034 DSAStack->setParentTeamsRegionLoc(StartLoc);
11035
11036 return OMPTeamsGenericLoopDirective::Create(
11037 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11038}
11039
11040StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsGenericLoopDirective(
11041 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11042 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11043 if (!AStmt)
11044 return StmtError();
11045
11046 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11047 // A list item may not appear in a lastprivate clause unless it is the
11048 // loop iteration variable of a loop that is associated with the construct.
11049 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_target_teams_loop,
11050 DSAStack))
11051 return StmtError();
11052
11053 CapturedStmt *CS =
11054 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_loop, AStmt);
11055
11056 OMPLoopDirective::HelperExprs B;
11057 // In presence of clause 'collapse', it will define the nested loops number.
11058 unsigned NestedLoopCount =
11059 checkOpenMPLoop(DKind: OMPD_target_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11060 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11061 VarsWithImplicitDSA, Built&: B);
11062 if (NestedLoopCount == 0)
11063 return StmtError();
11064
11065 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11066 "omp loop exprs were not built");
11067
11068 return OMPTargetTeamsGenericLoopDirective::Create(
11069 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
11070 CanBeParallelFor: teamsLoopCanBeParallelFor(AStmt, SemaRef));
11071}
11072
11073StmtResult SemaOpenMP::ActOnOpenMPParallelGenericLoopDirective(
11074 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11075 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11076 if (!AStmt)
11077 return StmtError();
11078
11079 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11080 // A list item may not appear in a lastprivate clause unless it is the
11081 // loop iteration variable of a loop that is associated with the construct.
11082 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_parallel_loop,
11083 DSAStack))
11084 return StmtError();
11085
11086 CapturedStmt *CS =
11087 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_loop, AStmt);
11088
11089 OMPLoopDirective::HelperExprs B;
11090 // In presence of clause 'collapse', it will define the nested loops number.
11091 unsigned NestedLoopCount =
11092 checkOpenMPLoop(DKind: OMPD_parallel_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11093 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11094 VarsWithImplicitDSA, Built&: B);
11095 if (NestedLoopCount == 0)
11096 return StmtError();
11097
11098 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11099 "omp loop exprs were not built");
11100
11101 return OMPParallelGenericLoopDirective::Create(
11102 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11103}
11104
11105StmtResult SemaOpenMP::ActOnOpenMPTargetParallelGenericLoopDirective(
11106 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11107 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11108 if (!AStmt)
11109 return StmtError();
11110
11111 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11112 // A list item may not appear in a lastprivate clause unless it is the
11113 // loop iteration variable of a loop that is associated with the construct.
11114 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_target_parallel_loop,
11115 DSAStack))
11116 return StmtError();
11117
11118 CapturedStmt *CS =
11119 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_loop, AStmt);
11120
11121 OMPLoopDirective::HelperExprs B;
11122 // In presence of clause 'collapse', it will define the nested loops number.
11123 unsigned NestedLoopCount =
11124 checkOpenMPLoop(DKind: OMPD_target_parallel_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11125 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11126 VarsWithImplicitDSA, Built&: B);
11127 if (NestedLoopCount == 0)
11128 return StmtError();
11129
11130 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11131 "omp loop exprs were not built");
11132
11133 return OMPTargetParallelGenericLoopDirective::Create(
11134 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11135}
11136
11137StmtResult SemaOpenMP::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
11138 Stmt *AStmt,
11139 SourceLocation StartLoc,
11140 SourceLocation EndLoc) {
11141 if (!AStmt)
11142 return StmtError();
11143
11144 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11145
11146 SemaRef.setFunctionHasBranchProtectedScope();
11147
11148 // OpenMP [2.7.3, single Construct, Restrictions]
11149 // The copyprivate clause must not be used with the nowait clause.
11150 const OMPClause *Nowait = nullptr;
11151 const OMPClause *Copyprivate = nullptr;
11152 for (const OMPClause *Clause : Clauses) {
11153 if (Clause->getClauseKind() == OMPC_nowait)
11154 Nowait = Clause;
11155 else if (Clause->getClauseKind() == OMPC_copyprivate)
11156 Copyprivate = Clause;
11157 if (Copyprivate && Nowait) {
11158 Diag(Loc: Copyprivate->getBeginLoc(),
11159 DiagID: diag::err_omp_single_copyprivate_with_nowait);
11160 Diag(Loc: Nowait->getBeginLoc(), DiagID: diag::note_omp_nowait_clause_here);
11161 return StmtError();
11162 }
11163 }
11164
11165 return OMPSingleDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11166 AssociatedStmt: AStmt);
11167}
11168
11169StmtResult SemaOpenMP::ActOnOpenMPMasterDirective(Stmt *AStmt,
11170 SourceLocation StartLoc,
11171 SourceLocation EndLoc) {
11172 if (!AStmt)
11173 return StmtError();
11174
11175 SemaRef.setFunctionHasBranchProtectedScope();
11176
11177 return OMPMasterDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt);
11178}
11179
11180StmtResult SemaOpenMP::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses,
11181 Stmt *AStmt,
11182 SourceLocation StartLoc,
11183 SourceLocation EndLoc) {
11184 if (!AStmt)
11185 return StmtError();
11186
11187 SemaRef.setFunctionHasBranchProtectedScope();
11188
11189 return OMPMaskedDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11190 AssociatedStmt: AStmt);
11191}
11192
11193StmtResult SemaOpenMP::ActOnOpenMPCriticalDirective(
11194 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
11195 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
11196 if (!AStmt)
11197 return StmtError();
11198
11199 bool ErrorFound = false;
11200 llvm::APSInt Hint;
11201 SourceLocation HintLoc;
11202 bool DependentHint = false;
11203 for (const OMPClause *C : Clauses) {
11204 if (C->getClauseKind() == OMPC_hint) {
11205 if (!DirName.getName()) {
11206 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_hint_clause_no_name);
11207 ErrorFound = true;
11208 }
11209 Expr *E = cast<OMPHintClause>(Val: C)->getHint();
11210 if (E->isTypeDependent() || E->isValueDependent() ||
11211 E->isInstantiationDependent()) {
11212 DependentHint = true;
11213 } else {
11214 Hint = E->EvaluateKnownConstInt(Ctx: getASTContext());
11215 HintLoc = C->getBeginLoc();
11216 }
11217 }
11218 }
11219 if (ErrorFound)
11220 return StmtError();
11221 const auto Pair = DSAStack->getCriticalWithHint(Name: DirName);
11222 if (Pair.first && DirName.getName() && !DependentHint) {
11223 if (llvm::APSInt::compareValues(I1: Hint, I2: Pair.second) != 0) {
11224 Diag(Loc: StartLoc, DiagID: diag::err_omp_critical_with_hint);
11225 if (HintLoc.isValid())
11226 Diag(Loc: HintLoc, DiagID: diag::note_omp_critical_hint_here)
11227 << 0 << toString(I: Hint, /*Radix=*/10, /*Signed=*/false);
11228 else
11229 Diag(Loc: StartLoc, DiagID: diag::note_omp_critical_no_hint) << 0;
11230 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
11231 Diag(Loc: C->getBeginLoc(), DiagID: diag::note_omp_critical_hint_here)
11232 << 1
11233 << toString(I: C->getHint()->EvaluateKnownConstInt(Ctx: getASTContext()),
11234 /*Radix=*/10, /*Signed=*/false);
11235 } else {
11236 Diag(Loc: Pair.first->getBeginLoc(), DiagID: diag::note_omp_critical_no_hint) << 1;
11237 }
11238 }
11239 }
11240
11241 SemaRef.setFunctionHasBranchProtectedScope();
11242
11243 auto *Dir = OMPCriticalDirective::Create(C: getASTContext(), Name: DirName, StartLoc,
11244 EndLoc, Clauses, AssociatedStmt: AStmt);
11245 if (!Pair.first && DirName.getName() && !DependentHint)
11246 DSAStack->addCriticalWithHint(D: Dir, Hint);
11247 return Dir;
11248}
11249
11250StmtResult SemaOpenMP::ActOnOpenMPParallelForDirective(
11251 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11252 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11253 if (!AStmt)
11254 return StmtError();
11255
11256 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for, AStmt);
11257
11258 OMPLoopBasedDirective::HelperExprs B;
11259 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11260 // define the nested loops number.
11261 unsigned NestedLoopCount =
11262 checkOpenMPLoop(DKind: OMPD_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11263 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt, SemaRef, DSA&: *DSAStack,
11264 VarsWithImplicitDSA, Built&: B);
11265 if (NestedLoopCount == 0)
11266 return StmtError();
11267
11268 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11269 return StmtError();
11270
11271 return OMPParallelForDirective::Create(
11272 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
11273 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11274}
11275
11276StmtResult SemaOpenMP::ActOnOpenMPParallelForSimdDirective(
11277 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11278 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11279 if (!AStmt)
11280 return StmtError();
11281
11282 CapturedStmt *CS =
11283 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for_simd, AStmt);
11284
11285 OMPLoopBasedDirective::HelperExprs B;
11286 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11287 // define the nested loops number.
11288 unsigned NestedLoopCount =
11289 checkOpenMPLoop(DKind: OMPD_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11290 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
11291 VarsWithImplicitDSA, Built&: B);
11292 if (NestedLoopCount == 0)
11293 return StmtError();
11294
11295 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11296 return StmtError();
11297
11298 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
11299 return StmtError();
11300
11301 return OMPParallelForSimdDirective::Create(
11302 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11303}
11304
11305StmtResult SemaOpenMP::ActOnOpenMPParallelMasterDirective(
11306 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11307 SourceLocation EndLoc) {
11308 if (!AStmt)
11309 return StmtError();
11310
11311 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master, AStmt);
11312
11313 return OMPParallelMasterDirective::Create(
11314 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11315 DSAStack->getTaskgroupReductionRef());
11316}
11317
11318StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedDirective(
11319 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11320 SourceLocation EndLoc) {
11321 if (!AStmt)
11322 return StmtError();
11323
11324 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked, AStmt);
11325
11326 return OMPParallelMaskedDirective::Create(
11327 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11328 DSAStack->getTaskgroupReductionRef());
11329}
11330
11331StmtResult SemaOpenMP::ActOnOpenMPParallelSectionsDirective(
11332 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11333 SourceLocation EndLoc) {
11334 if (checkSectionsDirective(SemaRef, DKind: OMPD_parallel_sections, AStmt, DSAStack))
11335 return StmtError();
11336
11337 SemaRef.setFunctionHasBranchProtectedScope();
11338
11339 return OMPParallelSectionsDirective::Create(
11340 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11341 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11342}
11343
11344/// Find and diagnose mutually exclusive clause kinds.
11345static bool checkMutuallyExclusiveClauses(
11346 Sema &S, ArrayRef<OMPClause *> Clauses,
11347 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) {
11348 const OMPClause *PrevClause = nullptr;
11349 bool ErrorFound = false;
11350 for (const OMPClause *C : Clauses) {
11351 if (llvm::is_contained(Range&: MutuallyExclusiveClauses, Element: C->getClauseKind())) {
11352 if (!PrevClause) {
11353 PrevClause = C;
11354 } else if (PrevClause->getClauseKind() != C->getClauseKind()) {
11355 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_clauses_mutually_exclusive)
11356 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
11357 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
11358 S.Diag(Loc: PrevClause->getBeginLoc(), DiagID: diag::note_omp_previous_clause)
11359 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
11360 ErrorFound = true;
11361 }
11362 }
11363 }
11364 return ErrorFound;
11365}
11366
11367StmtResult SemaOpenMP::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
11368 Stmt *AStmt,
11369 SourceLocation StartLoc,
11370 SourceLocation EndLoc) {
11371 if (!AStmt)
11372 return StmtError();
11373
11374 // OpenMP 5.0, 2.10.1 task Construct
11375 // If a detach clause appears on the directive, then a mergeable clause cannot
11376 // appear on the same directive.
11377 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
11378 MutuallyExclusiveClauses: {OMPC_detach, OMPC_mergeable}))
11379 return StmtError();
11380
11381 setBranchProtectedScope(SemaRef, DKind: OMPD_task, AStmt);
11382
11383 return OMPTaskDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11384 AssociatedStmt: AStmt, DSAStack->isCancelRegion());
11385}
11386
11387StmtResult SemaOpenMP::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
11388 SourceLocation EndLoc) {
11389 return OMPTaskyieldDirective::Create(C: getASTContext(), StartLoc, EndLoc);
11390}
11391
11392StmtResult SemaOpenMP::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
11393 SourceLocation EndLoc) {
11394 return OMPBarrierDirective::Create(C: getASTContext(), StartLoc, EndLoc);
11395}
11396
11397StmtResult SemaOpenMP::ActOnOpenMPErrorDirective(ArrayRef<OMPClause *> Clauses,
11398 SourceLocation StartLoc,
11399 SourceLocation EndLoc,
11400 bool InExContext) {
11401 const OMPAtClause *AtC =
11402 OMPExecutableDirective::getSingleClause<OMPAtClause>(Clauses);
11403
11404 if (AtC && !InExContext && AtC->getAtKind() == OMPC_AT_execution) {
11405 Diag(Loc: AtC->getAtKindKwLoc(), DiagID: diag::err_omp_unexpected_execution_modifier);
11406 return StmtError();
11407 }
11408
11409 if (!AtC || AtC->getAtKind() == OMPC_AT_compilation) {
11410 const OMPSeverityClause *SeverityC =
11411 OMPExecutableDirective::getSingleClause<OMPSeverityClause>(Clauses);
11412 const OMPMessageClause *MessageC =
11413 OMPExecutableDirective::getSingleClause<OMPMessageClause>(Clauses);
11414 std::optional<std::string> SL =
11415 MessageC ? MessageC->tryEvaluateString(Ctx&: getASTContext()) : std::nullopt;
11416
11417 if (MessageC && !SL)
11418 Diag(Loc: MessageC->getMessageString()->getBeginLoc(),
11419 DiagID: diag::warn_clause_expected_string)
11420 << getOpenMPClauseNameForDiag(C: OMPC_message) << 1;
11421 if (SeverityC && SeverityC->getSeverityKind() == OMPC_SEVERITY_warning)
11422 Diag(Loc: SeverityC->getSeverityKindKwLoc(), DiagID: diag::warn_diagnose_if_succeeded)
11423 << SL.value_or(u: "WARNING");
11424 else
11425 Diag(Loc: StartLoc, DiagID: diag::err_diagnose_if_succeeded) << SL.value_or(u: "ERROR");
11426 if (!SeverityC || SeverityC->getSeverityKind() != OMPC_SEVERITY_warning)
11427 return StmtError();
11428 }
11429
11430 return OMPErrorDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11431}
11432
11433StmtResult
11434SemaOpenMP::ActOnOpenMPTaskwaitDirective(ArrayRef<OMPClause *> Clauses,
11435 SourceLocation StartLoc,
11436 SourceLocation EndLoc) {
11437 const OMPNowaitClause *NowaitC =
11438 OMPExecutableDirective::getSingleClause<OMPNowaitClause>(Clauses);
11439 bool HasDependC =
11440 !OMPExecutableDirective::getClausesOfKind<OMPDependClause>(Clauses)
11441 .empty();
11442 if (NowaitC && !HasDependC) {
11443 Diag(Loc: StartLoc, DiagID: diag::err_omp_nowait_clause_without_depend);
11444 return StmtError();
11445 }
11446
11447 return OMPTaskwaitDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11448 Clauses);
11449}
11450
11451StmtResult
11452SemaOpenMP::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
11453 Stmt *AStmt, SourceLocation StartLoc,
11454 SourceLocation EndLoc) {
11455 if (!AStmt)
11456 return StmtError();
11457
11458 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11459
11460 SemaRef.setFunctionHasBranchProtectedScope();
11461
11462 return OMPTaskgroupDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11463 Clauses, AssociatedStmt: AStmt,
11464 DSAStack->getTaskgroupReductionRef());
11465}
11466
11467StmtResult SemaOpenMP::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
11468 SourceLocation StartLoc,
11469 SourceLocation EndLoc) {
11470 OMPFlushClause *FC = nullptr;
11471 OMPClause *OrderClause = nullptr;
11472 for (OMPClause *C : Clauses) {
11473 if (C->getClauseKind() == OMPC_flush)
11474 FC = cast<OMPFlushClause>(Val: C);
11475 else
11476 OrderClause = C;
11477 }
11478 unsigned OMPVersion = getLangOpts().OpenMP;
11479 OpenMPClauseKind MemOrderKind = OMPC_unknown;
11480 SourceLocation MemOrderLoc;
11481 for (const OMPClause *C : Clauses) {
11482 if (C->getClauseKind() == OMPC_acq_rel ||
11483 C->getClauseKind() == OMPC_acquire ||
11484 C->getClauseKind() == OMPC_release ||
11485 C->getClauseKind() == OMPC_seq_cst /*OpenMP 5.1*/) {
11486 if (MemOrderKind != OMPC_unknown) {
11487 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
11488 << getOpenMPDirectiveName(D: OMPD_flush, Ver: OMPVersion) << 1
11489 << SourceRange(C->getBeginLoc(), C->getEndLoc());
11490 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
11491 << getOpenMPClauseNameForDiag(C: MemOrderKind);
11492 } else {
11493 MemOrderKind = C->getClauseKind();
11494 MemOrderLoc = C->getBeginLoc();
11495 }
11496 }
11497 }
11498 if (FC && OrderClause) {
11499 Diag(Loc: FC->getLParenLoc(), DiagID: diag::err_omp_flush_order_clause_and_list)
11500 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
11501 Diag(Loc: OrderClause->getBeginLoc(), DiagID: diag::note_omp_flush_order_clause_here)
11502 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
11503 return StmtError();
11504 }
11505 return OMPFlushDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11506}
11507
11508StmtResult SemaOpenMP::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses,
11509 SourceLocation StartLoc,
11510 SourceLocation EndLoc) {
11511 if (Clauses.empty()) {
11512 Diag(Loc: StartLoc, DiagID: diag::err_omp_depobj_expected);
11513 return StmtError();
11514 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) {
11515 Diag(Loc: Clauses[0]->getBeginLoc(), DiagID: diag::err_omp_depobj_expected);
11516 return StmtError();
11517 }
11518 // Only depobj expression and another single clause is allowed.
11519 if (Clauses.size() > 2) {
11520 Diag(Loc: Clauses[2]->getBeginLoc(),
11521 DiagID: diag::err_omp_depobj_single_clause_expected);
11522 return StmtError();
11523 } else if (Clauses.size() < 1) {
11524 Diag(Loc: Clauses[0]->getEndLoc(), DiagID: diag::err_omp_depobj_single_clause_expected);
11525 return StmtError();
11526 }
11527 return OMPDepobjDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11528}
11529
11530StmtResult SemaOpenMP::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses,
11531 SourceLocation StartLoc,
11532 SourceLocation EndLoc) {
11533 // Check that exactly one clause is specified.
11534 if (Clauses.size() != 1) {
11535 Diag(Loc: Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(),
11536 DiagID: diag::err_omp_scan_single_clause_expected);
11537 return StmtError();
11538 }
11539 // Check that scan directive is used in the scope of the OpenMP loop body.
11540 if (Scope *S = DSAStack->getCurScope()) {
11541 Scope *ParentS = S->getParent();
11542 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() ||
11543 !ParentS->getBreakParent()->isOpenMPLoopScope()) {
11544 unsigned OMPVersion = getLangOpts().OpenMP;
11545 return StmtError(Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
11546 << getOpenMPDirectiveName(D: OMPD_scan, Ver: OMPVersion) << 5);
11547 }
11548 }
11549 // Check that only one instance of scan directives is used in the same outer
11550 // region.
11551 if (DSAStack->doesParentHasScanDirective()) {
11552 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "scan";
11553 Diag(DSAStack->getParentScanDirectiveLoc(),
11554 DiagID: diag::note_omp_previous_directive)
11555 << "scan";
11556 return StmtError();
11557 }
11558 DSAStack->setParentHasScanDirective(StartLoc);
11559 return OMPScanDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11560}
11561
11562StmtResult
11563SemaOpenMP::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
11564 Stmt *AStmt, SourceLocation StartLoc,
11565 SourceLocation EndLoc) {
11566 const OMPClause *DependFound = nullptr;
11567 const OMPClause *DependSourceClause = nullptr;
11568 const OMPClause *DependSinkClause = nullptr;
11569 const OMPClause *DoacrossFound = nullptr;
11570 const OMPClause *DoacrossSourceClause = nullptr;
11571 const OMPClause *DoacrossSinkClause = nullptr;
11572 bool ErrorFound = false;
11573 const OMPThreadsClause *TC = nullptr;
11574 const OMPSIMDClause *SC = nullptr;
11575 for (const OMPClause *C : Clauses) {
11576 auto DOC = dyn_cast<OMPDoacrossClause>(Val: C);
11577 auto DC = dyn_cast<OMPDependClause>(Val: C);
11578 if (DC || DOC) {
11579 DependFound = DC ? C : nullptr;
11580 DoacrossFound = DOC ? C : nullptr;
11581 OMPDoacrossKind ODK;
11582 if ((DC && DC->getDependencyKind() == OMPC_DEPEND_source) ||
11583 (DOC && (ODK.isSource(C: DOC)))) {
11584 if ((DC && DependSourceClause) || (DOC && DoacrossSourceClause)) {
11585 unsigned OMPVersion = getLangOpts().OpenMP;
11586 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
11587 << getOpenMPDirectiveName(D: OMPD_ordered, Ver: OMPVersion)
11588 << getOpenMPClauseNameForDiag(C: DC ? OMPC_depend : OMPC_doacross)
11589 << 2;
11590 ErrorFound = true;
11591 } else {
11592 if (DC)
11593 DependSourceClause = C;
11594 else
11595 DoacrossSourceClause = C;
11596 }
11597 if ((DC && DependSinkClause) || (DOC && DoacrossSinkClause)) {
11598 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
11599 << (DC ? "depend" : "doacross") << 0;
11600 ErrorFound = true;
11601 }
11602 } else if ((DC && DC->getDependencyKind() == OMPC_DEPEND_sink) ||
11603 (DOC && (ODK.isSink(C: DOC) || ODK.isSinkIter(C: DOC)))) {
11604 if (DependSourceClause || DoacrossSourceClause) {
11605 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
11606 << (DC ? "depend" : "doacross") << 1;
11607 ErrorFound = true;
11608 }
11609 if (DC)
11610 DependSinkClause = C;
11611 else
11612 DoacrossSinkClause = C;
11613 }
11614 } else if (C->getClauseKind() == OMPC_threads) {
11615 TC = cast<OMPThreadsClause>(Val: C);
11616 } else if (C->getClauseKind() == OMPC_simd) {
11617 SC = cast<OMPSIMDClause>(Val: C);
11618 }
11619 }
11620 if (!ErrorFound && !SC &&
11621 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
11622 // OpenMP [2.8.1,simd Construct, Restrictions]
11623 // An ordered construct with the simd clause is the only OpenMP construct
11624 // that can appear in the simd region.
11625 Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_simd)
11626 << (getLangOpts().OpenMP >= 50 ? 1 : 0);
11627 ErrorFound = true;
11628 } else if ((DependFound || DoacrossFound) && (TC || SC)) {
11629 SourceLocation Loc =
11630 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
11631 Diag(Loc, DiagID: diag::err_omp_depend_clause_thread_simd)
11632 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend : OMPC_doacross)
11633 << getOpenMPClauseNameForDiag(C: TC ? TC->getClauseKind()
11634 : SC->getClauseKind());
11635 ErrorFound = true;
11636 } else if ((DependFound || DoacrossFound) &&
11637 !DSAStack->getParentOrderedRegionParam().first) {
11638 SourceLocation Loc =
11639 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
11640 Diag(Loc, DiagID: diag::err_omp_ordered_directive_without_param)
11641 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend
11642 : OMPC_doacross);
11643 ErrorFound = true;
11644 } else if (TC || Clauses.empty()) {
11645 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
11646 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
11647 Diag(Loc: ErrLoc, DiagID: diag::err_omp_ordered_directive_with_param)
11648 << (TC != nullptr);
11649 Diag(Loc: Param->getBeginLoc(), DiagID: diag::note_omp_ordered_param) << 1;
11650 ErrorFound = true;
11651 }
11652 }
11653 if ((!AStmt && !DependFound && !DoacrossFound) || ErrorFound)
11654 return StmtError();
11655
11656 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions.
11657 // During execution of an iteration of a worksharing-loop or a loop nest
11658 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread
11659 // must not execute more than one ordered region corresponding to an ordered
11660 // construct without a depend clause.
11661 if (!DependFound && !DoacrossFound) {
11662 if (DSAStack->doesParentHasOrderedDirective()) {
11663 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "ordered";
11664 Diag(DSAStack->getParentOrderedDirectiveLoc(),
11665 DiagID: diag::note_omp_previous_directive)
11666 << "ordered";
11667 return StmtError();
11668 }
11669 DSAStack->setParentHasOrderedDirective(StartLoc);
11670 }
11671
11672 if (AStmt) {
11673 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11674
11675 SemaRef.setFunctionHasBranchProtectedScope();
11676 }
11677
11678 return OMPOrderedDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11679 AssociatedStmt: AStmt);
11680}
11681
11682namespace {
11683/// Helper class for checking expression in 'omp atomic [update]'
11684/// construct.
11685class OpenMPAtomicUpdateChecker {
11686 /// Error results for atomic update expressions.
11687 enum ExprAnalysisErrorCode {
11688 /// A statement is not an expression statement.
11689 NotAnExpression,
11690 /// Expression is not builtin binary or unary operation.
11691 NotABinaryOrUnaryExpression,
11692 /// Unary operation is not post-/pre- increment/decrement operation.
11693 NotAnUnaryIncDecExpression,
11694 /// An expression is not of scalar type.
11695 NotAScalarType,
11696 /// A binary operation is not an assignment operation.
11697 NotAnAssignmentOp,
11698 /// RHS part of the binary operation is not a binary expression.
11699 NotABinaryExpression,
11700 /// RHS part is not additive/multiplicative/shift/bitwise binary
11701 /// expression.
11702 NotABinaryOperator,
11703 /// RHS binary operation does not have reference to the updated LHS
11704 /// part.
11705 NotAnUpdateExpression,
11706 /// An expression contains semantical error not related to
11707 /// 'omp atomic [update]'
11708 NotAValidExpression,
11709 /// No errors is found.
11710 NoError
11711 };
11712 /// Reference to Sema.
11713 Sema &SemaRef;
11714 /// A location for note diagnostics (when error is found).
11715 SourceLocation NoteLoc;
11716 /// 'x' lvalue part of the source atomic expression.
11717 Expr *X;
11718 /// 'expr' rvalue part of the source atomic expression.
11719 Expr *E;
11720 /// Helper expression of the form
11721 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
11722 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
11723 Expr *UpdateExpr;
11724 /// Is 'x' a LHS in a RHS part of full update expression. It is
11725 /// important for non-associative operations.
11726 bool IsXLHSInRHSPart;
11727 BinaryOperatorKind Op;
11728 SourceLocation OpLoc;
11729 /// true if the source expression is a postfix unary operation, false
11730 /// if it is a prefix unary operation.
11731 bool IsPostfixUpdate;
11732
11733public:
11734 OpenMPAtomicUpdateChecker(Sema &SemaRef)
11735 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
11736 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
11737 /// Check specified statement that it is suitable for 'atomic update'
11738 /// constructs and extract 'x', 'expr' and Operation from the original
11739 /// expression. If DiagId and NoteId == 0, then only check is performed
11740 /// without error notification.
11741 /// \param DiagId Diagnostic which should be emitted if error is found.
11742 /// \param NoteId Diagnostic note for the main error message.
11743 /// \return true if statement is not an update expression, false otherwise.
11744 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
11745 /// Return the 'x' lvalue part of the source atomic expression.
11746 Expr *getX() const { return X; }
11747 /// Return the 'expr' rvalue part of the source atomic expression.
11748 Expr *getExpr() const { return E; }
11749 /// Return the update expression used in calculation of the updated
11750 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
11751 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
11752 Expr *getUpdateExpr() const { return UpdateExpr; }
11753 /// Return true if 'x' is LHS in RHS part of full update expression,
11754 /// false otherwise.
11755 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
11756
11757 /// true if the source expression is a postfix unary operation, false
11758 /// if it is a prefix unary operation.
11759 bool isPostfixUpdate() const { return IsPostfixUpdate; }
11760
11761private:
11762 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
11763 unsigned NoteId = 0);
11764};
11765
11766bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
11767 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
11768 ExprAnalysisErrorCode ErrorFound = NoError;
11769 SourceLocation ErrorLoc, NoteLoc;
11770 SourceRange ErrorRange, NoteRange;
11771 // Allowed constructs are:
11772 // x = x binop expr;
11773 // x = expr binop x;
11774 if (AtomicBinOp->getOpcode() == BO_Assign) {
11775 X = AtomicBinOp->getLHS();
11776 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
11777 Val: AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
11778 if (AtomicInnerBinOp->isMultiplicativeOp() ||
11779 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
11780 AtomicInnerBinOp->isBitwiseOp()) {
11781 Op = AtomicInnerBinOp->getOpcode();
11782 OpLoc = AtomicInnerBinOp->getOperatorLoc();
11783 Expr *LHS = AtomicInnerBinOp->getLHS();
11784 Expr *RHS = AtomicInnerBinOp->getRHS();
11785 llvm::FoldingSetNodeID XId, LHSId, RHSId;
11786 X->IgnoreParenImpCasts()->Profile(ID&: XId, Context: SemaRef.getASTContext(),
11787 /*Canonical=*/true);
11788 LHS->IgnoreParenImpCasts()->Profile(ID&: LHSId, Context: SemaRef.getASTContext(),
11789 /*Canonical=*/true);
11790 RHS->IgnoreParenImpCasts()->Profile(ID&: RHSId, Context: SemaRef.getASTContext(),
11791 /*Canonical=*/true);
11792 if (XId == LHSId) {
11793 E = RHS;
11794 IsXLHSInRHSPart = true;
11795 } else if (XId == RHSId) {
11796 E = LHS;
11797 IsXLHSInRHSPart = false;
11798 } else {
11799 ErrorLoc = AtomicInnerBinOp->getExprLoc();
11800 ErrorRange = AtomicInnerBinOp->getSourceRange();
11801 NoteLoc = X->getExprLoc();
11802 NoteRange = X->getSourceRange();
11803 ErrorFound = NotAnUpdateExpression;
11804 }
11805 } else {
11806 ErrorLoc = AtomicInnerBinOp->getExprLoc();
11807 ErrorRange = AtomicInnerBinOp->getSourceRange();
11808 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
11809 NoteRange = SourceRange(NoteLoc, NoteLoc);
11810 ErrorFound = NotABinaryOperator;
11811 }
11812 } else {
11813 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
11814 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
11815 ErrorFound = NotABinaryExpression;
11816 }
11817 } else {
11818 ErrorLoc = AtomicBinOp->getExprLoc();
11819 ErrorRange = AtomicBinOp->getSourceRange();
11820 NoteLoc = AtomicBinOp->getOperatorLoc();
11821 NoteRange = SourceRange(NoteLoc, NoteLoc);
11822 ErrorFound = NotAnAssignmentOp;
11823 }
11824 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
11825 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
11826 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
11827 return true;
11828 }
11829 if (SemaRef.CurContext->isDependentContext())
11830 E = X = UpdateExpr = nullptr;
11831 return ErrorFound != NoError;
11832}
11833
11834bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
11835 unsigned NoteId) {
11836 ExprAnalysisErrorCode ErrorFound = NoError;
11837 SourceLocation ErrorLoc, NoteLoc;
11838 SourceRange ErrorRange, NoteRange;
11839 // Allowed constructs are:
11840 // x++;
11841 // x--;
11842 // ++x;
11843 // --x;
11844 // x binop= expr;
11845 // x = x binop expr;
11846 // x = expr binop x;
11847 if (auto *AtomicBody = dyn_cast<Expr>(Val: S)) {
11848 AtomicBody = AtomicBody->IgnoreParenImpCasts();
11849 if (AtomicBody->getType()->isScalarType() ||
11850 AtomicBody->isInstantiationDependent()) {
11851 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
11852 Val: AtomicBody->IgnoreParenImpCasts())) {
11853 // Check for Compound Assignment Operation
11854 Op = BinaryOperator::getOpForCompoundAssignment(
11855 Opc: AtomicCompAssignOp->getOpcode());
11856 OpLoc = AtomicCompAssignOp->getOperatorLoc();
11857 E = AtomicCompAssignOp->getRHS();
11858 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
11859 IsXLHSInRHSPart = true;
11860 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
11861 Val: AtomicBody->IgnoreParenImpCasts())) {
11862 // Check for Binary Operation
11863 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
11864 return true;
11865 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
11866 Val: AtomicBody->IgnoreParenImpCasts())) {
11867 // Check for Unary Operation
11868 if (AtomicUnaryOp->isIncrementDecrementOp()) {
11869 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
11870 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
11871 OpLoc = AtomicUnaryOp->getOperatorLoc();
11872 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
11873 E = SemaRef.ActOnIntegerConstant(Loc: OpLoc, /*uint64_t Val=*/Val: 1).get();
11874 IsXLHSInRHSPart = true;
11875 } else {
11876 ErrorFound = NotAnUnaryIncDecExpression;
11877 ErrorLoc = AtomicUnaryOp->getExprLoc();
11878 ErrorRange = AtomicUnaryOp->getSourceRange();
11879 NoteLoc = AtomicUnaryOp->getOperatorLoc();
11880 NoteRange = SourceRange(NoteLoc, NoteLoc);
11881 }
11882 } else if (!AtomicBody->isInstantiationDependent()) {
11883 ErrorFound = NotABinaryOrUnaryExpression;
11884 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
11885 NoteRange = ErrorRange = AtomicBody->getSourceRange();
11886 } else if (AtomicBody->containsErrors()) {
11887 ErrorFound = NotAValidExpression;
11888 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
11889 NoteRange = ErrorRange = AtomicBody->getSourceRange();
11890 }
11891 } else {
11892 ErrorFound = NotAScalarType;
11893 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
11894 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11895 }
11896 } else {
11897 ErrorFound = NotAnExpression;
11898 NoteLoc = ErrorLoc = S->getBeginLoc();
11899 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11900 }
11901 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
11902 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
11903 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
11904 return true;
11905 }
11906 if (SemaRef.CurContext->isDependentContext())
11907 E = X = UpdateExpr = nullptr;
11908 if (ErrorFound == NoError && E && X) {
11909 // Build an update expression of form 'OpaqueValueExpr(x) binop
11910 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
11911 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
11912 auto *OVEX = new (SemaRef.getASTContext())
11913 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue);
11914 auto *OVEExpr = new (SemaRef.getASTContext())
11915 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue);
11916 ExprResult Update =
11917 SemaRef.CreateBuiltinBinOp(OpLoc, Opc: Op, LHSExpr: IsXLHSInRHSPart ? OVEX : OVEExpr,
11918 RHSExpr: IsXLHSInRHSPart ? OVEExpr : OVEX);
11919 if (Update.isInvalid())
11920 return true;
11921 Update = SemaRef.PerformImplicitConversion(From: Update.get(), ToType: X->getType(),
11922 Action: AssignmentAction::Casting);
11923 if (Update.isInvalid())
11924 return true;
11925 UpdateExpr = Update.get();
11926 }
11927 return ErrorFound != NoError;
11928}
11929
11930/// Get the node id of the fixed point of an expression \a S.
11931llvm::FoldingSetNodeID getNodeId(ASTContext &Context, const Expr *S) {
11932 llvm::FoldingSetNodeID Id;
11933 S->IgnoreParenImpCasts()->Profile(ID&: Id, Context, Canonical: true);
11934 return Id;
11935}
11936
11937/// Check if two expressions are same.
11938bool checkIfTwoExprsAreSame(ASTContext &Context, const Expr *LHS,
11939 const Expr *RHS) {
11940 return getNodeId(Context, S: LHS) == getNodeId(Context, S: RHS);
11941}
11942
11943class OpenMPAtomicCompareChecker {
11944public:
11945 /// All kinds of errors that can occur in `atomic compare`
11946 enum ErrorTy {
11947 /// Empty compound statement.
11948 NoStmt = 0,
11949 /// More than one statement in a compound statement.
11950 MoreThanOneStmt,
11951 /// Not an assignment binary operator.
11952 NotAnAssignment,
11953 /// Not a conditional operator.
11954 NotCondOp,
11955 /// Wrong false expr. According to the spec, 'x' should be at the false
11956 /// expression of a conditional expression.
11957 WrongFalseExpr,
11958 /// The condition of a conditional expression is not a binary operator.
11959 NotABinaryOp,
11960 /// Invalid binary operator (not <, >, or ==).
11961 InvalidBinaryOp,
11962 /// Invalid comparison (not x == e, e == x, x ordop expr, or expr ordop x).
11963 InvalidComparison,
11964 /// X is not a lvalue.
11965 XNotLValue,
11966 /// Not a scalar.
11967 NotScalar,
11968 /// Not an integer.
11969 NotInteger,
11970 /// 'else' statement is not expected.
11971 UnexpectedElse,
11972 /// Not an equality operator.
11973 NotEQ,
11974 /// Invalid assignment (not v == x).
11975 InvalidAssignment,
11976 /// Not if statement
11977 NotIfStmt,
11978 /// More than two statements in a compound statement.
11979 MoreThanTwoStmts,
11980 /// Not a compound statement.
11981 NotCompoundStmt,
11982 /// No else statement.
11983 NoElse,
11984 /// Not 'if (r)'.
11985 InvalidCondition,
11986 /// No error.
11987 NoError,
11988 };
11989
11990 struct ErrorInfoTy {
11991 ErrorTy Error;
11992 SourceLocation ErrorLoc;
11993 SourceRange ErrorRange;
11994 SourceLocation NoteLoc;
11995 SourceRange NoteRange;
11996 };
11997
11998 OpenMPAtomicCompareChecker(Sema &S) : ContextRef(S.getASTContext()) {}
11999
12000 /// Check if statement \a S is valid for <tt>atomic compare</tt>.
12001 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
12002
12003 Expr *getX() const { return X; }
12004 Expr *getE() const { return E; }
12005 Expr *getD() const { return D; }
12006 Expr *getCond() const { return C; }
12007 bool isXBinopExpr() const { return IsXBinopExpr; }
12008
12009protected:
12010 /// Reference to ASTContext
12011 ASTContext &ContextRef;
12012 /// 'x' lvalue part of the source atomic expression.
12013 Expr *X = nullptr;
12014 /// 'expr' or 'e' rvalue part of the source atomic expression.
12015 Expr *E = nullptr;
12016 /// 'd' rvalue part of the source atomic expression.
12017 Expr *D = nullptr;
12018 /// 'cond' part of the source atomic expression. It is in one of the following
12019 /// forms:
12020 /// expr ordop x
12021 /// x ordop expr
12022 /// x == e
12023 /// e == x
12024 Expr *C = nullptr;
12025 /// True if the cond expr is in the form of 'x ordop expr'.
12026 bool IsXBinopExpr = true;
12027
12028 /// Check if it is a valid conditional update statement (cond-update-stmt).
12029 bool checkCondUpdateStmt(IfStmt *S, ErrorInfoTy &ErrorInfo);
12030
12031 /// Check if it is a valid conditional expression statement (cond-expr-stmt).
12032 bool checkCondExprStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
12033
12034 /// Check if all captured values have right type.
12035 bool checkType(ErrorInfoTy &ErrorInfo) const;
12036
12037 static bool CheckValue(const Expr *E, ErrorInfoTy &ErrorInfo,
12038 bool ShouldBeLValue, bool ShouldBeInteger = false) {
12039 if (E->isInstantiationDependent())
12040 return true;
12041
12042 if (ShouldBeLValue && !E->isLValue()) {
12043 ErrorInfo.Error = ErrorTy::XNotLValue;
12044 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
12045 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
12046 return false;
12047 }
12048
12049 QualType QTy = E->getType();
12050 if (!QTy->isScalarType()) {
12051 ErrorInfo.Error = ErrorTy::NotScalar;
12052 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
12053 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
12054 return false;
12055 }
12056 if (ShouldBeInteger && !QTy->isIntegerType()) {
12057 ErrorInfo.Error = ErrorTy::NotInteger;
12058 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
12059 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
12060 return false;
12061 }
12062
12063 return true;
12064 }
12065};
12066
12067bool OpenMPAtomicCompareChecker::checkCondUpdateStmt(IfStmt *S,
12068 ErrorInfoTy &ErrorInfo) {
12069 auto *Then = S->getThen();
12070 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
12071 if (CS->body_empty()) {
12072 ErrorInfo.Error = ErrorTy::NoStmt;
12073 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12074 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12075 return false;
12076 }
12077 if (CS->size() > 1) {
12078 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12079 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12080 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12081 return false;
12082 }
12083 Then = CS->body_front();
12084 }
12085
12086 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
12087 if (!BO) {
12088 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12089 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
12090 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
12091 return false;
12092 }
12093 if (BO->getOpcode() != BO_Assign) {
12094 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12095 ErrorInfo.ErrorLoc = BO->getExprLoc();
12096 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12097 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12098 return false;
12099 }
12100
12101 X = BO->getLHS();
12102
12103 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
12104 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
12105 Expr *LHS = nullptr;
12106 Expr *RHS = nullptr;
12107 if (Cond) {
12108 LHS = Cond->getLHS();
12109 RHS = Cond->getRHS();
12110 } else if (Call) {
12111 LHS = Call->getArg(Arg: 0);
12112 RHS = Call->getArg(Arg: 1);
12113 } else {
12114 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12115 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12116 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12117 return false;
12118 }
12119
12120 if ((Cond && Cond->getOpcode() == BO_EQ) ||
12121 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
12122 C = S->getCond();
12123 D = BO->getRHS();
12124 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12125 E = RHS;
12126 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12127 E = LHS;
12128 } else {
12129 ErrorInfo.Error = ErrorTy::InvalidComparison;
12130 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12131 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12132 S->getCond()->getSourceRange();
12133 return false;
12134 }
12135 } else if ((Cond &&
12136 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
12137 (Call &&
12138 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
12139 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
12140 E = BO->getRHS();
12141 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
12142 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
12143 C = S->getCond();
12144 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
12145 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12146 C = S->getCond();
12147 IsXBinopExpr = false;
12148 } else {
12149 ErrorInfo.Error = ErrorTy::InvalidComparison;
12150 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12151 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12152 S->getCond()->getSourceRange();
12153 return false;
12154 }
12155 } else {
12156 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
12157 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12158 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12159 return false;
12160 }
12161
12162 if (S->getElse()) {
12163 ErrorInfo.Error = ErrorTy::UnexpectedElse;
12164 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getElse()->getBeginLoc();
12165 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getElse()->getSourceRange();
12166 return false;
12167 }
12168
12169 return true;
12170}
12171
12172bool OpenMPAtomicCompareChecker::checkCondExprStmt(Stmt *S,
12173 ErrorInfoTy &ErrorInfo) {
12174 auto *BO = dyn_cast<BinaryOperator>(Val: S);
12175 if (!BO) {
12176 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12177 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12178 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12179 return false;
12180 }
12181 if (BO->getOpcode() != BO_Assign) {
12182 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12183 ErrorInfo.ErrorLoc = BO->getExprLoc();
12184 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12185 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12186 return false;
12187 }
12188
12189 X = BO->getLHS();
12190
12191 auto *CO = dyn_cast<ConditionalOperator>(Val: BO->getRHS()->IgnoreParenImpCasts());
12192 if (!CO) {
12193 ErrorInfo.Error = ErrorTy::NotCondOp;
12194 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getRHS()->getExprLoc();
12195 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getRHS()->getSourceRange();
12196 return false;
12197 }
12198
12199 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: CO->getFalseExpr())) {
12200 ErrorInfo.Error = ErrorTy::WrongFalseExpr;
12201 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getFalseExpr()->getExprLoc();
12202 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12203 CO->getFalseExpr()->getSourceRange();
12204 return false;
12205 }
12206
12207 auto *Cond = dyn_cast<BinaryOperator>(Val: CO->getCond());
12208 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: CO->getCond());
12209 Expr *LHS = nullptr;
12210 Expr *RHS = nullptr;
12211 if (Cond) {
12212 LHS = Cond->getLHS();
12213 RHS = Cond->getRHS();
12214 } else if (Call) {
12215 LHS = Call->getArg(Arg: 0);
12216 RHS = Call->getArg(Arg: 1);
12217 } else {
12218 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12219 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12220 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12221 CO->getCond()->getSourceRange();
12222 return false;
12223 }
12224
12225 if ((Cond && Cond->getOpcode() == BO_EQ) ||
12226 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
12227 C = CO->getCond();
12228 D = CO->getTrueExpr();
12229 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12230 E = RHS;
12231 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12232 E = LHS;
12233 } else {
12234 ErrorInfo.Error = ErrorTy::InvalidComparison;
12235 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12236 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12237 CO->getCond()->getSourceRange();
12238 return false;
12239 }
12240 } else if ((Cond &&
12241 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
12242 (Call &&
12243 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
12244 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
12245
12246 E = CO->getTrueExpr();
12247 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
12248 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
12249 C = CO->getCond();
12250 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
12251 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12252 C = CO->getCond();
12253 IsXBinopExpr = false;
12254 } else {
12255 ErrorInfo.Error = ErrorTy::InvalidComparison;
12256 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12257 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12258 CO->getCond()->getSourceRange();
12259 return false;
12260 }
12261 } else {
12262 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
12263 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12264 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12265 CO->getCond()->getSourceRange();
12266 return false;
12267 }
12268
12269 return true;
12270}
12271
12272bool OpenMPAtomicCompareChecker::checkType(ErrorInfoTy &ErrorInfo) const {
12273 // 'x' and 'e' cannot be nullptr
12274 assert(X && E && "X and E cannot be nullptr");
12275
12276 if (!CheckValue(E: X, ErrorInfo, ShouldBeLValue: true))
12277 return false;
12278
12279 if (!CheckValue(E, ErrorInfo, ShouldBeLValue: false))
12280 return false;
12281
12282 if (D && !CheckValue(E: D, ErrorInfo, ShouldBeLValue: false))
12283 return false;
12284
12285 return true;
12286}
12287
12288bool OpenMPAtomicCompareChecker::checkStmt(
12289 Stmt *S, OpenMPAtomicCompareChecker::ErrorInfoTy &ErrorInfo) {
12290 auto *CS = dyn_cast<CompoundStmt>(Val: S);
12291 if (CS) {
12292 if (CS->body_empty()) {
12293 ErrorInfo.Error = ErrorTy::NoStmt;
12294 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12295 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12296 return false;
12297 }
12298
12299 if (CS->size() != 1) {
12300 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12301 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12302 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12303 return false;
12304 }
12305 S = CS->body_front();
12306 }
12307
12308 auto Res = false;
12309
12310 if (auto *IS = dyn_cast<IfStmt>(Val: S)) {
12311 // Check if the statement is in one of the following forms
12312 // (cond-update-stmt):
12313 // if (expr ordop x) { x = expr; }
12314 // if (x ordop expr) { x = expr; }
12315 // if (x == e) { x = d; }
12316 Res = checkCondUpdateStmt(S: IS, ErrorInfo);
12317 } else {
12318 // Check if the statement is in one of the following forms (cond-expr-stmt):
12319 // x = expr ordop x ? expr : x;
12320 // x = x ordop expr ? expr : x;
12321 // x = x == e ? d : x;
12322 Res = checkCondExprStmt(S, ErrorInfo);
12323 }
12324
12325 if (!Res)
12326 return false;
12327
12328 return checkType(ErrorInfo);
12329}
12330
12331class OpenMPAtomicCompareCaptureChecker final
12332 : public OpenMPAtomicCompareChecker {
12333public:
12334 OpenMPAtomicCompareCaptureChecker(Sema &S) : OpenMPAtomicCompareChecker(S) {}
12335
12336 Expr *getV() const { return V; }
12337 Expr *getR() const { return R; }
12338 bool isFailOnly() const { return IsFailOnly; }
12339 bool isPostfixUpdate() const { return IsPostfixUpdate; }
12340
12341 /// Check if statement \a S is valid for <tt>atomic compare capture</tt>.
12342 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
12343
12344private:
12345 bool checkType(ErrorInfoTy &ErrorInfo);
12346
12347 // NOTE: Form 3, 4, 5 in the following comments mean the 3rd, 4th, and 5th
12348 // form of 'conditional-update-capture-atomic' structured block on the v5.2
12349 // spec p.p. 82:
12350 // (1) { v = x; cond-update-stmt }
12351 // (2) { cond-update-stmt v = x; }
12352 // (3) if(x == e) { x = d; } else { v = x; }
12353 // (4) { r = x == e; if(r) { x = d; } }
12354 // (5) { r = x == e; if(r) { x = d; } else { v = x; } }
12355
12356 /// Check if it is valid 'if(x == e) { x = d; } else { v = x; }' (form 3)
12357 bool checkForm3(IfStmt *S, ErrorInfoTy &ErrorInfo);
12358
12359 /// Check if it is valid '{ r = x == e; if(r) { x = d; } }',
12360 /// or '{ r = x == e; if(r) { x = d; } else { v = x; } }' (form 4 and 5)
12361 bool checkForm45(Stmt *S, ErrorInfoTy &ErrorInfo);
12362
12363 /// 'v' lvalue part of the source atomic expression.
12364 Expr *V = nullptr;
12365 /// 'r' lvalue part of the source atomic expression.
12366 Expr *R = nullptr;
12367 /// If 'v' is only updated when the comparison fails.
12368 bool IsFailOnly = false;
12369 /// If original value of 'x' must be stored in 'v', not an updated one.
12370 bool IsPostfixUpdate = false;
12371};
12372
12373bool OpenMPAtomicCompareCaptureChecker::checkType(ErrorInfoTy &ErrorInfo) {
12374 if (!OpenMPAtomicCompareChecker::checkType(ErrorInfo))
12375 return false;
12376
12377 if (V && !CheckValue(E: V, ErrorInfo, ShouldBeLValue: true))
12378 return false;
12379
12380 if (R && !CheckValue(E: R, ErrorInfo, ShouldBeLValue: true, ShouldBeInteger: true))
12381 return false;
12382
12383 return true;
12384}
12385
12386bool OpenMPAtomicCompareCaptureChecker::checkForm3(IfStmt *S,
12387 ErrorInfoTy &ErrorInfo) {
12388 IsFailOnly = true;
12389
12390 auto *Then = S->getThen();
12391 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
12392 if (CS->body_empty()) {
12393 ErrorInfo.Error = ErrorTy::NoStmt;
12394 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12395 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12396 return false;
12397 }
12398 if (CS->size() > 1) {
12399 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12400 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12401 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12402 return false;
12403 }
12404 Then = CS->body_front();
12405 }
12406
12407 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
12408 if (!BO) {
12409 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12410 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
12411 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
12412 return false;
12413 }
12414 if (BO->getOpcode() != BO_Assign) {
12415 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12416 ErrorInfo.ErrorLoc = BO->getExprLoc();
12417 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12418 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12419 return false;
12420 }
12421
12422 X = BO->getLHS();
12423 D = BO->getRHS();
12424
12425 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
12426 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
12427 Expr *LHS = nullptr;
12428 Expr *RHS = nullptr;
12429 if (Cond) {
12430 LHS = Cond->getLHS();
12431 RHS = Cond->getRHS();
12432 } else if (Call) {
12433 LHS = Call->getArg(Arg: 0);
12434 RHS = Call->getArg(Arg: 1);
12435 } else {
12436 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12437 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12438 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12439 return false;
12440 }
12441 if ((Cond && Cond->getOpcode() != BO_EQ) ||
12442 (Call && Call->getOperator() != OverloadedOperatorKind::OO_EqualEqual)) {
12443 ErrorInfo.Error = ErrorTy::NotEQ;
12444 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12445 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12446 return false;
12447 }
12448
12449 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12450 E = RHS;
12451 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12452 E = LHS;
12453 } else {
12454 ErrorInfo.Error = ErrorTy::InvalidComparison;
12455 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12456 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12457 return false;
12458 }
12459
12460 C = S->getCond();
12461
12462 if (!S->getElse()) {
12463 ErrorInfo.Error = ErrorTy::NoElse;
12464 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12465 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12466 return false;
12467 }
12468
12469 auto *Else = S->getElse();
12470 if (auto *CS = dyn_cast<CompoundStmt>(Val: Else)) {
12471 if (CS->body_empty()) {
12472 ErrorInfo.Error = ErrorTy::NoStmt;
12473 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12474 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12475 return false;
12476 }
12477 if (CS->size() > 1) {
12478 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12479 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12480 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12481 return false;
12482 }
12483 Else = CS->body_front();
12484 }
12485
12486 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
12487 if (!ElseBO) {
12488 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12489 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
12490 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
12491 return false;
12492 }
12493 if (ElseBO->getOpcode() != BO_Assign) {
12494 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12495 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
12496 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
12497 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
12498 return false;
12499 }
12500
12501 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
12502 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12503 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseBO->getRHS()->getExprLoc();
12504 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12505 ElseBO->getRHS()->getSourceRange();
12506 return false;
12507 }
12508
12509 V = ElseBO->getLHS();
12510
12511 return checkType(ErrorInfo);
12512}
12513
12514bool OpenMPAtomicCompareCaptureChecker::checkForm45(Stmt *S,
12515 ErrorInfoTy &ErrorInfo) {
12516 // We don't check here as they should be already done before call this
12517 // function.
12518 auto *CS = cast<CompoundStmt>(Val: S);
12519 assert(CS->size() == 2 && "CompoundStmt size is not expected");
12520 auto *S1 = cast<BinaryOperator>(Val: CS->body_front());
12521 auto *S2 = cast<IfStmt>(Val: CS->body_back());
12522 assert(S1->getOpcode() == BO_Assign && "unexpected binary operator");
12523
12524 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: S1->getLHS(), RHS: S2->getCond())) {
12525 ErrorInfo.Error = ErrorTy::InvalidCondition;
12526 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getCond()->getExprLoc();
12527 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S1->getLHS()->getSourceRange();
12528 return false;
12529 }
12530
12531 R = S1->getLHS();
12532
12533 auto *Then = S2->getThen();
12534 if (auto *ThenCS = dyn_cast<CompoundStmt>(Val: Then)) {
12535 if (ThenCS->body_empty()) {
12536 ErrorInfo.Error = ErrorTy::NoStmt;
12537 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
12538 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
12539 return false;
12540 }
12541 if (ThenCS->size() > 1) {
12542 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12543 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
12544 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
12545 return false;
12546 }
12547 Then = ThenCS->body_front();
12548 }
12549
12550 auto *ThenBO = dyn_cast<BinaryOperator>(Val: Then);
12551 if (!ThenBO) {
12552 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12553 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getBeginLoc();
12554 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S2->getSourceRange();
12555 return false;
12556 }
12557 if (ThenBO->getOpcode() != BO_Assign) {
12558 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12559 ErrorInfo.ErrorLoc = ThenBO->getExprLoc();
12560 ErrorInfo.NoteLoc = ThenBO->getOperatorLoc();
12561 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenBO->getSourceRange();
12562 return false;
12563 }
12564
12565 X = ThenBO->getLHS();
12566 D = ThenBO->getRHS();
12567
12568 auto *BO = cast<BinaryOperator>(Val: S1->getRHS()->IgnoreImpCasts());
12569 if (BO->getOpcode() != BO_EQ) {
12570 ErrorInfo.Error = ErrorTy::NotEQ;
12571 ErrorInfo.ErrorLoc = BO->getExprLoc();
12572 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12573 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12574 return false;
12575 }
12576
12577 C = BO;
12578
12579 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getLHS())) {
12580 E = BO->getRHS();
12581 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getRHS())) {
12582 E = BO->getLHS();
12583 } else {
12584 ErrorInfo.Error = ErrorTy::InvalidComparison;
12585 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getExprLoc();
12586 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12587 return false;
12588 }
12589
12590 if (S2->getElse()) {
12591 IsFailOnly = true;
12592
12593 auto *Else = S2->getElse();
12594 if (auto *ElseCS = dyn_cast<CompoundStmt>(Val: Else)) {
12595 if (ElseCS->body_empty()) {
12596 ErrorInfo.Error = ErrorTy::NoStmt;
12597 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
12598 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
12599 return false;
12600 }
12601 if (ElseCS->size() > 1) {
12602 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12603 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
12604 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
12605 return false;
12606 }
12607 Else = ElseCS->body_front();
12608 }
12609
12610 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
12611 if (!ElseBO) {
12612 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12613 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
12614 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
12615 return false;
12616 }
12617 if (ElseBO->getOpcode() != BO_Assign) {
12618 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12619 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
12620 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
12621 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
12622 return false;
12623 }
12624 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
12625 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12626 ErrorInfo.ErrorLoc = ElseBO->getRHS()->getExprLoc();
12627 ErrorInfo.NoteLoc = X->getExprLoc();
12628 ErrorInfo.ErrorRange = ElseBO->getRHS()->getSourceRange();
12629 ErrorInfo.NoteRange = X->getSourceRange();
12630 return false;
12631 }
12632
12633 V = ElseBO->getLHS();
12634 }
12635
12636 return checkType(ErrorInfo);
12637}
12638
12639bool OpenMPAtomicCompareCaptureChecker::checkStmt(Stmt *S,
12640 ErrorInfoTy &ErrorInfo) {
12641 // if(x == e) { x = d; } else { v = x; }
12642 if (auto *IS = dyn_cast<IfStmt>(Val: S))
12643 return checkForm3(S: IS, ErrorInfo);
12644
12645 auto *CS = dyn_cast<CompoundStmt>(Val: S);
12646 if (!CS) {
12647 ErrorInfo.Error = ErrorTy::NotCompoundStmt;
12648 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12649 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12650 return false;
12651 }
12652 if (CS->body_empty()) {
12653 ErrorInfo.Error = ErrorTy::NoStmt;
12654 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12655 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12656 return false;
12657 }
12658
12659 // { if(x == e) { x = d; } else { v = x; } }
12660 if (CS->size() == 1) {
12661 auto *IS = dyn_cast<IfStmt>(Val: CS->body_front());
12662 if (!IS) {
12663 ErrorInfo.Error = ErrorTy::NotIfStmt;
12664 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->body_front()->getBeginLoc();
12665 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12666 CS->body_front()->getSourceRange();
12667 return false;
12668 }
12669
12670 return checkForm3(S: IS, ErrorInfo);
12671 } else if (CS->size() == 2) {
12672 auto *S1 = CS->body_front();
12673 auto *S2 = CS->body_back();
12674
12675 Stmt *UpdateStmt = nullptr;
12676 Stmt *CondUpdateStmt = nullptr;
12677 Stmt *CondExprStmt = nullptr;
12678
12679 if (auto *BO = dyn_cast<BinaryOperator>(Val: S1)) {
12680 // It could be one of the following cases:
12681 // { v = x; cond-update-stmt }
12682 // { v = x; cond-expr-stmt }
12683 // { cond-expr-stmt; v = x; }
12684 // form 45
12685 if (isa<BinaryOperator>(Val: BO->getRHS()->IgnoreImpCasts()) ||
12686 isa<ConditionalOperator>(Val: BO->getRHS()->IgnoreImpCasts())) {
12687 // check if form 45
12688 if (isa<IfStmt>(Val: S2))
12689 return checkForm45(S: CS, ErrorInfo);
12690 // { cond-expr-stmt; v = x; }
12691 CondExprStmt = S1;
12692 UpdateStmt = S2;
12693 } else {
12694 IsPostfixUpdate = true;
12695 UpdateStmt = S1;
12696 if (isa<IfStmt>(Val: S2)) {
12697 // { v = x; cond-update-stmt }
12698 CondUpdateStmt = S2;
12699 } else {
12700 // { v = x; cond-expr-stmt }
12701 CondExprStmt = S2;
12702 }
12703 }
12704 } else {
12705 // { cond-update-stmt v = x; }
12706 UpdateStmt = S2;
12707 CondUpdateStmt = S1;
12708 }
12709
12710 auto CheckCondUpdateStmt = [this, &ErrorInfo](Stmt *CUS) {
12711 auto *IS = dyn_cast<IfStmt>(Val: CUS);
12712 if (!IS) {
12713 ErrorInfo.Error = ErrorTy::NotIfStmt;
12714 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CUS->getBeginLoc();
12715 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CUS->getSourceRange();
12716 return false;
12717 }
12718
12719 return checkCondUpdateStmt(S: IS, ErrorInfo);
12720 };
12721
12722 // CheckUpdateStmt has to be called *after* CheckCondUpdateStmt.
12723 auto CheckUpdateStmt = [this, &ErrorInfo](Stmt *US) {
12724 auto *BO = dyn_cast<BinaryOperator>(Val: US);
12725 if (!BO) {
12726 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12727 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = US->getBeginLoc();
12728 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = US->getSourceRange();
12729 return false;
12730 }
12731 if (BO->getOpcode() != BO_Assign) {
12732 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12733 ErrorInfo.ErrorLoc = BO->getExprLoc();
12734 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12735 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12736 return false;
12737 }
12738 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: this->X, RHS: BO->getRHS())) {
12739 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12740 ErrorInfo.ErrorLoc = BO->getRHS()->getExprLoc();
12741 ErrorInfo.NoteLoc = this->X->getExprLoc();
12742 ErrorInfo.ErrorRange = BO->getRHS()->getSourceRange();
12743 ErrorInfo.NoteRange = this->X->getSourceRange();
12744 return false;
12745 }
12746
12747 this->V = BO->getLHS();
12748
12749 return true;
12750 };
12751
12752 if (CondUpdateStmt && !CheckCondUpdateStmt(CondUpdateStmt))
12753 return false;
12754 if (CondExprStmt && !checkCondExprStmt(S: CondExprStmt, ErrorInfo))
12755 return false;
12756 if (!CheckUpdateStmt(UpdateStmt))
12757 return false;
12758 } else {
12759 ErrorInfo.Error = ErrorTy::MoreThanTwoStmts;
12760 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12761 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12762 return false;
12763 }
12764
12765 return checkType(ErrorInfo);
12766}
12767} // namespace
12768
12769StmtResult SemaOpenMP::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
12770 Stmt *AStmt,
12771 SourceLocation StartLoc,
12772 SourceLocation EndLoc) {
12773 ASTContext &Context = getASTContext();
12774 unsigned OMPVersion = getLangOpts().OpenMP;
12775 // Register location of the first atomic directive.
12776 DSAStack->addAtomicDirectiveLoc(Loc: StartLoc);
12777 if (!AStmt)
12778 return StmtError();
12779
12780 // 1.2.2 OpenMP Language Terminology
12781 // Structured block - An executable statement with a single entry at the
12782 // top and a single exit at the bottom.
12783 // The point of exit cannot be a branch out of the structured block.
12784 // longjmp() and throw() must not violate the entry/exit criteria.
12785 OpenMPClauseKind AtomicKind = OMPC_unknown;
12786 SourceLocation AtomicKindLoc;
12787 OpenMPClauseKind MemOrderKind = OMPC_unknown;
12788 SourceLocation MemOrderLoc;
12789 bool MutexClauseEncountered = false;
12790 llvm::SmallSet<OpenMPClauseKind, 2> EncounteredAtomicKinds;
12791 for (const OMPClause *C : Clauses) {
12792 switch (C->getClauseKind()) {
12793 case OMPC_read:
12794 case OMPC_write:
12795 case OMPC_update:
12796 MutexClauseEncountered = true;
12797 [[fallthrough]];
12798 case OMPC_capture:
12799 case OMPC_compare: {
12800 if (AtomicKind != OMPC_unknown && MutexClauseEncountered) {
12801 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
12802 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12803 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12804 << getOpenMPClauseNameForDiag(C: AtomicKind);
12805 } else {
12806 AtomicKind = C->getClauseKind();
12807 AtomicKindLoc = C->getBeginLoc();
12808 if (!EncounteredAtomicKinds.insert(V: C->getClauseKind()).second) {
12809 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
12810 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12811 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12812 << getOpenMPClauseNameForDiag(C: AtomicKind);
12813 }
12814 }
12815 break;
12816 }
12817 case OMPC_weak:
12818 case OMPC_fail: {
12819 if (!EncounteredAtomicKinds.contains(V: OMPC_compare)) {
12820 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_no_compare)
12821 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
12822 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12823 return StmtError();
12824 }
12825 break;
12826 }
12827 case OMPC_seq_cst:
12828 case OMPC_acq_rel:
12829 case OMPC_acquire:
12830 case OMPC_release:
12831 case OMPC_relaxed: {
12832 if (MemOrderKind != OMPC_unknown) {
12833 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
12834 << getOpenMPDirectiveName(D: OMPD_atomic, Ver: OMPVersion) << 0
12835 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12836 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12837 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12838 } else {
12839 MemOrderKind = C->getClauseKind();
12840 MemOrderLoc = C->getBeginLoc();
12841 }
12842 break;
12843 }
12844 // The following clauses are allowed, but we don't need to do anything here.
12845 case OMPC_hint:
12846 break;
12847 default:
12848 llvm_unreachable("unknown clause is encountered");
12849 }
12850 }
12851 bool IsCompareCapture = false;
12852 if (EncounteredAtomicKinds.contains(V: OMPC_compare) &&
12853 EncounteredAtomicKinds.contains(V: OMPC_capture)) {
12854 IsCompareCapture = true;
12855 AtomicKind = OMPC_compare;
12856 }
12857 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions
12858 // If atomic-clause is read then memory-order-clause must not be acq_rel or
12859 // release.
12860 // If atomic-clause is write then memory-order-clause must not be acq_rel or
12861 // acquire.
12862 // If atomic-clause is update or not present then memory-order-clause must not
12863 // be acq_rel or acquire.
12864 if ((AtomicKind == OMPC_read &&
12865 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) ||
12866 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update ||
12867 AtomicKind == OMPC_unknown) &&
12868 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) {
12869 SourceLocation Loc = AtomicKindLoc;
12870 if (AtomicKind == OMPC_unknown)
12871 Loc = StartLoc;
12872 Diag(Loc, DiagID: diag::err_omp_atomic_incompatible_mem_order_clause)
12873 << getOpenMPClauseNameForDiag(C: AtomicKind)
12874 << (AtomicKind == OMPC_unknown ? 1 : 0)
12875 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12876 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12877 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12878 }
12879
12880 Stmt *Body = AStmt;
12881 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Body))
12882 Body = EWC->getSubExpr();
12883
12884 Expr *X = nullptr;
12885 Expr *V = nullptr;
12886 Expr *E = nullptr;
12887 Expr *UE = nullptr;
12888 Expr *D = nullptr;
12889 Expr *CE = nullptr;
12890 Expr *R = nullptr;
12891 bool IsXLHSInRHSPart = false;
12892 bool IsPostfixUpdate = false;
12893 bool IsFailOnly = false;
12894 // OpenMP [2.12.6, atomic Construct]
12895 // In the next expressions:
12896 // * x and v (as applicable) are both l-value expressions with scalar type.
12897 // * During the execution of an atomic region, multiple syntactic
12898 // occurrences of x must designate the same storage location.
12899 // * Neither of v and expr (as applicable) may access the storage location
12900 // designated by x.
12901 // * Neither of x and expr (as applicable) may access the storage location
12902 // designated by v.
12903 // * expr is an expression with scalar type.
12904 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
12905 // * binop, binop=, ++, and -- are not overloaded operators.
12906 // * The expression x binop expr must be numerically equivalent to x binop
12907 // (expr). This requirement is satisfied if the operators in expr have
12908 // precedence greater than binop, or by using parentheses around expr or
12909 // subexpressions of expr.
12910 // * The expression expr binop x must be numerically equivalent to (expr)
12911 // binop x. This requirement is satisfied if the operators in expr have
12912 // precedence equal to or greater than binop, or by using parentheses around
12913 // expr or subexpressions of expr.
12914 // * For forms that allow multiple occurrences of x, the number of times
12915 // that x is evaluated is unspecified.
12916 if (AtomicKind == OMPC_read) {
12917 enum {
12918 NotAnExpression,
12919 NotAnAssignmentOp,
12920 NotAScalarType,
12921 NotAnLValue,
12922 NoError
12923 } ErrorFound = NoError;
12924 SourceLocation ErrorLoc, NoteLoc;
12925 SourceRange ErrorRange, NoteRange;
12926 // If clause is read:
12927 // v = x;
12928 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
12929 const auto *AtomicBinOp =
12930 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
12931 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
12932 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
12933 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
12934 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
12935 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
12936 if (!X->isLValue() || !V->isLValue()) {
12937 const Expr *NotLValueExpr = X->isLValue() ? V : X;
12938 ErrorFound = NotAnLValue;
12939 ErrorLoc = AtomicBinOp->getExprLoc();
12940 ErrorRange = AtomicBinOp->getSourceRange();
12941 NoteLoc = NotLValueExpr->getExprLoc();
12942 NoteRange = NotLValueExpr->getSourceRange();
12943 }
12944 } else if (!X->isInstantiationDependent() ||
12945 !V->isInstantiationDependent()) {
12946 const Expr *NotScalarExpr =
12947 (X->isInstantiationDependent() || X->getType()->isScalarType())
12948 ? V
12949 : X;
12950 ErrorFound = NotAScalarType;
12951 ErrorLoc = AtomicBinOp->getExprLoc();
12952 ErrorRange = AtomicBinOp->getSourceRange();
12953 NoteLoc = NotScalarExpr->getExprLoc();
12954 NoteRange = NotScalarExpr->getSourceRange();
12955 }
12956 } else if (!AtomicBody->isInstantiationDependent()) {
12957 ErrorFound = NotAnAssignmentOp;
12958 ErrorLoc = AtomicBody->getExprLoc();
12959 ErrorRange = AtomicBody->getSourceRange();
12960 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
12961 : AtomicBody->getExprLoc();
12962 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
12963 : AtomicBody->getSourceRange();
12964 }
12965 } else {
12966 ErrorFound = NotAnExpression;
12967 NoteLoc = ErrorLoc = Body->getBeginLoc();
12968 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
12969 }
12970 if (ErrorFound != NoError) {
12971 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_read_not_expression_statement)
12972 << ErrorRange;
12973 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
12974 << ErrorFound << NoteRange;
12975 return StmtError();
12976 }
12977 if (SemaRef.CurContext->isDependentContext())
12978 V = X = nullptr;
12979 } else if (AtomicKind == OMPC_write) {
12980 enum {
12981 NotAnExpression,
12982 NotAnAssignmentOp,
12983 NotAScalarType,
12984 NotAnLValue,
12985 NoError
12986 } ErrorFound = NoError;
12987 SourceLocation ErrorLoc, NoteLoc;
12988 SourceRange ErrorRange, NoteRange;
12989 // If clause is write:
12990 // x = expr;
12991 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
12992 const auto *AtomicBinOp =
12993 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
12994 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
12995 X = AtomicBinOp->getLHS();
12996 E = AtomicBinOp->getRHS();
12997 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
12998 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
12999 if (!X->isLValue()) {
13000 ErrorFound = NotAnLValue;
13001 ErrorLoc = AtomicBinOp->getExprLoc();
13002 ErrorRange = AtomicBinOp->getSourceRange();
13003 NoteLoc = X->getExprLoc();
13004 NoteRange = X->getSourceRange();
13005 }
13006 } else if (!X->isInstantiationDependent() ||
13007 !E->isInstantiationDependent()) {
13008 const Expr *NotScalarExpr =
13009 (X->isInstantiationDependent() || X->getType()->isScalarType())
13010 ? E
13011 : X;
13012 ErrorFound = NotAScalarType;
13013 ErrorLoc = AtomicBinOp->getExprLoc();
13014 ErrorRange = AtomicBinOp->getSourceRange();
13015 NoteLoc = NotScalarExpr->getExprLoc();
13016 NoteRange = NotScalarExpr->getSourceRange();
13017 }
13018 } else if (!AtomicBody->isInstantiationDependent()) {
13019 ErrorFound = NotAnAssignmentOp;
13020 ErrorLoc = AtomicBody->getExprLoc();
13021 ErrorRange = AtomicBody->getSourceRange();
13022 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
13023 : AtomicBody->getExprLoc();
13024 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
13025 : AtomicBody->getSourceRange();
13026 }
13027 } else {
13028 ErrorFound = NotAnExpression;
13029 NoteLoc = ErrorLoc = Body->getBeginLoc();
13030 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
13031 }
13032 if (ErrorFound != NoError) {
13033 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_write_not_expression_statement)
13034 << ErrorRange;
13035 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
13036 << ErrorFound << NoteRange;
13037 return StmtError();
13038 }
13039 if (SemaRef.CurContext->isDependentContext())
13040 E = X = nullptr;
13041 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
13042 // If clause is update:
13043 // x++;
13044 // x--;
13045 // ++x;
13046 // --x;
13047 // x binop= expr;
13048 // x = x binop expr;
13049 // x = expr binop x;
13050 OpenMPAtomicUpdateChecker Checker(SemaRef);
13051 if (Checker.checkStatement(
13052 S: Body,
13053 DiagId: (AtomicKind == OMPC_update)
13054 ? diag::err_omp_atomic_update_not_expression_statement
13055 : diag::err_omp_atomic_not_expression_statement,
13056 NoteId: diag::note_omp_atomic_update))
13057 return StmtError();
13058 if (!SemaRef.CurContext->isDependentContext()) {
13059 E = Checker.getExpr();
13060 X = Checker.getX();
13061 UE = Checker.getUpdateExpr();
13062 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13063 }
13064 } else if (AtomicKind == OMPC_capture) {
13065 enum {
13066 NotAnAssignmentOp,
13067 NotACompoundStatement,
13068 NotTwoSubstatements,
13069 NotASpecificExpression,
13070 NoError
13071 } ErrorFound = NoError;
13072 SourceLocation ErrorLoc, NoteLoc;
13073 SourceRange ErrorRange, NoteRange;
13074 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
13075 // If clause is a capture:
13076 // v = x++;
13077 // v = x--;
13078 // v = ++x;
13079 // v = --x;
13080 // v = x binop= expr;
13081 // v = x = x binop expr;
13082 // v = x = expr binop x;
13083 const auto *AtomicBinOp =
13084 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
13085 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
13086 V = AtomicBinOp->getLHS();
13087 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
13088 OpenMPAtomicUpdateChecker Checker(SemaRef);
13089 if (Checker.checkStatement(
13090 S: Body, DiagId: diag::err_omp_atomic_capture_not_expression_statement,
13091 NoteId: diag::note_omp_atomic_update))
13092 return StmtError();
13093 E = Checker.getExpr();
13094 X = Checker.getX();
13095 UE = Checker.getUpdateExpr();
13096 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13097 IsPostfixUpdate = Checker.isPostfixUpdate();
13098 } else if (!AtomicBody->isInstantiationDependent()) {
13099 ErrorLoc = AtomicBody->getExprLoc();
13100 ErrorRange = AtomicBody->getSourceRange();
13101 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
13102 : AtomicBody->getExprLoc();
13103 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
13104 : AtomicBody->getSourceRange();
13105 ErrorFound = NotAnAssignmentOp;
13106 }
13107 if (ErrorFound != NoError) {
13108 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_expression_statement)
13109 << ErrorRange;
13110 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
13111 return StmtError();
13112 }
13113 if (SemaRef.CurContext->isDependentContext())
13114 UE = V = E = X = nullptr;
13115 } else {
13116 // If clause is a capture:
13117 // { v = x; x = expr; }
13118 // { v = x; x++; }
13119 // { v = x; x--; }
13120 // { v = x; ++x; }
13121 // { v = x; --x; }
13122 // { v = x; x binop= expr; }
13123 // { v = x; x = x binop expr; }
13124 // { v = x; x = expr binop x; }
13125 // { x++; v = x; }
13126 // { x--; v = x; }
13127 // { ++x; v = x; }
13128 // { --x; v = x; }
13129 // { x binop= expr; v = x; }
13130 // { x = x binop expr; v = x; }
13131 // { x = expr binop x; v = x; }
13132 if (auto *CS = dyn_cast<CompoundStmt>(Val: Body)) {
13133 // Check that this is { expr1; expr2; }
13134 if (CS->size() == 2) {
13135 Stmt *First = CS->body_front();
13136 Stmt *Second = CS->body_back();
13137 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: First))
13138 First = EWC->getSubExpr()->IgnoreParenImpCasts();
13139 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Second))
13140 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
13141 // Need to find what subexpression is 'v' and what is 'x'.
13142 OpenMPAtomicUpdateChecker Checker(SemaRef);
13143 bool IsUpdateExprFound = !Checker.checkStatement(S: Second);
13144 BinaryOperator *BinOp = nullptr;
13145 if (IsUpdateExprFound) {
13146 BinOp = dyn_cast<BinaryOperator>(Val: First);
13147 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
13148 }
13149 if (IsUpdateExprFound && !SemaRef.CurContext->isDependentContext()) {
13150 // { v = x; x++; }
13151 // { v = x; x--; }
13152 // { v = x; ++x; }
13153 // { v = x; --x; }
13154 // { v = x; x binop= expr; }
13155 // { v = x; x = x binop expr; }
13156 // { v = x; x = expr binop x; }
13157 // Check that the first expression has form v = x.
13158 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
13159 llvm::FoldingSetNodeID XId, PossibleXId;
13160 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
13161 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
13162 IsUpdateExprFound = XId == PossibleXId;
13163 if (IsUpdateExprFound) {
13164 V = BinOp->getLHS();
13165 X = Checker.getX();
13166 E = Checker.getExpr();
13167 UE = Checker.getUpdateExpr();
13168 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13169 IsPostfixUpdate = true;
13170 }
13171 }
13172 if (!IsUpdateExprFound) {
13173 IsUpdateExprFound = !Checker.checkStatement(S: First);
13174 BinOp = nullptr;
13175 if (IsUpdateExprFound) {
13176 BinOp = dyn_cast<BinaryOperator>(Val: Second);
13177 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
13178 }
13179 if (IsUpdateExprFound &&
13180 !SemaRef.CurContext->isDependentContext()) {
13181 // { x++; v = x; }
13182 // { x--; v = x; }
13183 // { ++x; v = x; }
13184 // { --x; v = x; }
13185 // { x binop= expr; v = x; }
13186 // { x = x binop expr; v = x; }
13187 // { x = expr binop x; v = x; }
13188 // Check that the second expression has form v = x.
13189 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
13190 llvm::FoldingSetNodeID XId, PossibleXId;
13191 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
13192 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
13193 IsUpdateExprFound = XId == PossibleXId;
13194 if (IsUpdateExprFound) {
13195 V = BinOp->getLHS();
13196 X = Checker.getX();
13197 E = Checker.getExpr();
13198 UE = Checker.getUpdateExpr();
13199 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13200 IsPostfixUpdate = false;
13201 }
13202 }
13203 }
13204 if (!IsUpdateExprFound) {
13205 // { v = x; x = expr; }
13206 auto *FirstExpr = dyn_cast<Expr>(Val: First);
13207 auto *SecondExpr = dyn_cast<Expr>(Val: Second);
13208 if (!FirstExpr || !SecondExpr ||
13209 !(FirstExpr->isInstantiationDependent() ||
13210 SecondExpr->isInstantiationDependent())) {
13211 auto *FirstBinOp = dyn_cast<BinaryOperator>(Val: First);
13212 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
13213 ErrorFound = NotAnAssignmentOp;
13214 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
13215 : First->getBeginLoc();
13216 NoteRange = ErrorRange = FirstBinOp
13217 ? FirstBinOp->getSourceRange()
13218 : SourceRange(ErrorLoc, ErrorLoc);
13219 } else {
13220 auto *SecondBinOp = dyn_cast<BinaryOperator>(Val: Second);
13221 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
13222 ErrorFound = NotAnAssignmentOp;
13223 NoteLoc = ErrorLoc = SecondBinOp
13224 ? SecondBinOp->getOperatorLoc()
13225 : Second->getBeginLoc();
13226 NoteRange = ErrorRange =
13227 SecondBinOp ? SecondBinOp->getSourceRange()
13228 : SourceRange(ErrorLoc, ErrorLoc);
13229 } else {
13230 Expr *PossibleXRHSInFirst =
13231 FirstBinOp->getRHS()->IgnoreParenImpCasts();
13232 Expr *PossibleXLHSInSecond =
13233 SecondBinOp->getLHS()->IgnoreParenImpCasts();
13234 llvm::FoldingSetNodeID X1Id, X2Id;
13235 PossibleXRHSInFirst->Profile(ID&: X1Id, Context,
13236 /*Canonical=*/true);
13237 PossibleXLHSInSecond->Profile(ID&: X2Id, Context,
13238 /*Canonical=*/true);
13239 IsUpdateExprFound = X1Id == X2Id;
13240 if (IsUpdateExprFound) {
13241 V = FirstBinOp->getLHS();
13242 X = SecondBinOp->getLHS();
13243 E = SecondBinOp->getRHS();
13244 UE = nullptr;
13245 IsXLHSInRHSPart = false;
13246 IsPostfixUpdate = true;
13247 } else {
13248 ErrorFound = NotASpecificExpression;
13249 ErrorLoc = FirstBinOp->getExprLoc();
13250 ErrorRange = FirstBinOp->getSourceRange();
13251 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
13252 NoteRange = SecondBinOp->getRHS()->getSourceRange();
13253 }
13254 }
13255 }
13256 }
13257 }
13258 } else {
13259 NoteLoc = ErrorLoc = Body->getBeginLoc();
13260 NoteRange = ErrorRange =
13261 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
13262 ErrorFound = NotTwoSubstatements;
13263 }
13264 } else {
13265 NoteLoc = ErrorLoc = Body->getBeginLoc();
13266 NoteRange = ErrorRange =
13267 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
13268 ErrorFound = NotACompoundStatement;
13269 }
13270 }
13271 if (ErrorFound != NoError) {
13272 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_compound_statement)
13273 << ErrorRange;
13274 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
13275 return StmtError();
13276 }
13277 if (SemaRef.CurContext->isDependentContext())
13278 UE = V = E = X = nullptr;
13279 } else if (AtomicKind == OMPC_compare) {
13280 if (IsCompareCapture) {
13281 OpenMPAtomicCompareCaptureChecker::ErrorInfoTy ErrorInfo;
13282 OpenMPAtomicCompareCaptureChecker Checker(SemaRef);
13283 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
13284 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare_capture)
13285 << ErrorInfo.ErrorRange;
13286 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
13287 << ErrorInfo.Error << ErrorInfo.NoteRange;
13288 return StmtError();
13289 }
13290 X = Checker.getX();
13291 E = Checker.getE();
13292 D = Checker.getD();
13293 CE = Checker.getCond();
13294 V = Checker.getV();
13295 R = Checker.getR();
13296 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
13297 IsXLHSInRHSPart = Checker.isXBinopExpr();
13298 IsFailOnly = Checker.isFailOnly();
13299 IsPostfixUpdate = Checker.isPostfixUpdate();
13300 } else {
13301 OpenMPAtomicCompareChecker::ErrorInfoTy ErrorInfo;
13302 OpenMPAtomicCompareChecker Checker(SemaRef);
13303 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
13304 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare)
13305 << ErrorInfo.ErrorRange;
13306 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
13307 << ErrorInfo.Error << ErrorInfo.NoteRange;
13308 return StmtError();
13309 }
13310 X = Checker.getX();
13311 E = Checker.getE();
13312 D = Checker.getD();
13313 CE = Checker.getCond();
13314 // The weak clause may only appear if the resulting atomic operation is
13315 // an atomic conditional update for which the comparison tests for
13316 // equality. It was not possible to do this check in
13317 // OpenMPAtomicCompareChecker::checkStmt() as the check for OMPC_weak
13318 // could not be performed (Clauses are not available).
13319 auto *It = find_if(Range&: Clauses, P: [](OMPClause *C) {
13320 return C->getClauseKind() == llvm::omp::Clause::OMPC_weak;
13321 });
13322 if (It != Clauses.end()) {
13323 auto *Cond = dyn_cast<BinaryOperator>(Val: CE);
13324 if (Cond->getOpcode() != BO_EQ) {
13325 ErrorInfo.Error = Checker.ErrorTy::NotAnAssignment;
13326 ErrorInfo.ErrorLoc = Cond->getExprLoc();
13327 ErrorInfo.NoteLoc = Cond->getOperatorLoc();
13328 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
13329
13330 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_weak_no_equality)
13331 << ErrorInfo.ErrorRange;
13332 return StmtError();
13333 }
13334 }
13335 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
13336 IsXLHSInRHSPart = Checker.isXBinopExpr();
13337 }
13338 }
13339
13340 SemaRef.setFunctionHasBranchProtectedScope();
13341
13342 return OMPAtomicDirective::Create(
13343 C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
13344 Exprs: {.X: X, .V: V, .R: R, .E: E, .UE: UE, .D: D, .Cond: CE, .IsXLHSInRHSPart: IsXLHSInRHSPart, .IsPostfixUpdate: IsPostfixUpdate, .IsFailOnly: IsFailOnly});
13345}
13346
13347StmtResult SemaOpenMP::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
13348 Stmt *AStmt,
13349 SourceLocation StartLoc,
13350 SourceLocation EndLoc) {
13351 if (!AStmt)
13352 return StmtError();
13353
13354 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target, AStmt);
13355
13356 // OpenMP [2.16, Nesting of Regions]
13357 // If specified, a teams construct must be contained within a target
13358 // construct. That target construct must contain no statements or directives
13359 // outside of the teams construct.
13360 if (DSAStack->hasInnerTeamsRegion()) {
13361 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
13362 bool OMPTeamsFound = true;
13363 if (const auto *CS = dyn_cast<CompoundStmt>(Val: S)) {
13364 auto I = CS->body_begin();
13365 while (I != CS->body_end()) {
13366 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: *I);
13367 bool IsTeams = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
13368 if (!IsTeams || I != CS->body_begin()) {
13369 OMPTeamsFound = false;
13370 if (IsTeams && I != CS->body_begin()) {
13371 // This is the two teams case. Since the InnerTeamsRegionLoc will
13372 // point to this second one reset the iterator to the other teams.
13373 --I;
13374 }
13375 break;
13376 }
13377 ++I;
13378 }
13379 assert(I != CS->body_end() && "Not found statement");
13380 S = *I;
13381 } else {
13382 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: S);
13383 OMPTeamsFound = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
13384 }
13385 if (!OMPTeamsFound) {
13386 Diag(Loc: StartLoc, DiagID: diag::err_omp_target_contains_not_only_teams);
13387 Diag(DSAStack->getInnerTeamsRegionLoc(),
13388 DiagID: diag::note_omp_nested_teams_construct_here);
13389 Diag(Loc: S->getBeginLoc(), DiagID: diag::note_omp_nested_statement_here)
13390 << isa<OMPExecutableDirective>(Val: S);
13391 return StmtError();
13392 }
13393 }
13394
13395 return OMPTargetDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13396 AssociatedStmt: AStmt);
13397}
13398
13399StmtResult SemaOpenMP::ActOnOpenMPTargetParallelDirective(
13400 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13401 SourceLocation EndLoc) {
13402 if (!AStmt)
13403 return StmtError();
13404
13405 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel, AStmt);
13406
13407 return OMPTargetParallelDirective::Create(
13408 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
13409 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
13410}
13411
13412StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForDirective(
13413 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13414 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13415 if (!AStmt)
13416 return StmtError();
13417
13418 CapturedStmt *CS =
13419 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for, AStmt);
13420
13421 OMPLoopBasedDirective::HelperExprs B;
13422 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13423 // define the nested loops number.
13424 unsigned NestedLoopCount =
13425 checkOpenMPLoop(DKind: OMPD_target_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13426 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
13427 VarsWithImplicitDSA, Built&: B);
13428 if (NestedLoopCount == 0)
13429 return StmtError();
13430
13431 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13432 return StmtError();
13433
13434 return OMPTargetParallelForDirective::Create(
13435 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13436 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
13437}
13438
13439/// Check for existence of a map clause in the list of clauses.
13440static bool hasClauses(ArrayRef<OMPClause *> Clauses,
13441 const OpenMPClauseKind K) {
13442 return llvm::any_of(
13443 Range&: Clauses, P: [K](const OMPClause *C) { return C->getClauseKind() == K; });
13444}
13445
13446template <typename... Params>
13447static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
13448 const Params... ClauseTypes) {
13449 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
13450}
13451
13452/// Check if the variables in the mapping clause are externally visible.
13453static bool isClauseMappable(ArrayRef<OMPClause *> Clauses) {
13454 for (const OMPClause *C : Clauses) {
13455 if (auto *TC = dyn_cast<OMPToClause>(Val: C))
13456 return llvm::all_of(Range: TC->all_decls(), P: [](ValueDecl *VD) {
13457 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13458 (VD->isExternallyVisible() &&
13459 VD->getVisibility() != HiddenVisibility);
13460 });
13461 else if (auto *FC = dyn_cast<OMPFromClause>(Val: C))
13462 return llvm::all_of(Range: FC->all_decls(), P: [](ValueDecl *VD) {
13463 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13464 (VD->isExternallyVisible() &&
13465 VD->getVisibility() != HiddenVisibility);
13466 });
13467 }
13468
13469 return true;
13470}
13471
13472StmtResult
13473SemaOpenMP::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
13474 Stmt *AStmt, SourceLocation StartLoc,
13475 SourceLocation EndLoc) {
13476 if (!AStmt)
13477 return StmtError();
13478
13479 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13480
13481 // OpenMP [2.12.2, target data Construct, Restrictions]
13482 // At least one map, use_device_addr or use_device_ptr clause must appear on
13483 // the directive.
13484 if (!hasClauses(Clauses, K: OMPC_map, ClauseTypes: OMPC_use_device_ptr) &&
13485 (getLangOpts().OpenMP < 50 ||
13486 !hasClauses(Clauses, K: OMPC_use_device_addr))) {
13487 StringRef Expected;
13488 if (getLangOpts().OpenMP < 50)
13489 Expected = "'map' or 'use_device_ptr'";
13490 else
13491 Expected = "'map', 'use_device_ptr', or 'use_device_addr'";
13492 unsigned OMPVersion = getLangOpts().OpenMP;
13493 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13494 << Expected << getOpenMPDirectiveName(D: OMPD_target_data, Ver: OMPVersion);
13495 return StmtError();
13496 }
13497
13498 SemaRef.setFunctionHasBranchProtectedScope();
13499
13500 return OMPTargetDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13501 Clauses, AssociatedStmt: AStmt);
13502}
13503
13504StmtResult SemaOpenMP::ActOnOpenMPTargetEnterDataDirective(
13505 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13506 SourceLocation EndLoc, Stmt *AStmt) {
13507 if (!AStmt)
13508 return StmtError();
13509
13510 setBranchProtectedScope(SemaRef, DKind: OMPD_target_enter_data, AStmt);
13511
13512 // OpenMP [2.10.2, Restrictions, p. 99]
13513 // At least one map clause must appear on the directive.
13514 if (!hasClauses(Clauses, K: OMPC_map)) {
13515 unsigned OMPVersion = getLangOpts().OpenMP;
13516 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13517 << "'map'"
13518 << getOpenMPDirectiveName(D: OMPD_target_enter_data, Ver: OMPVersion);
13519 return StmtError();
13520 }
13521
13522 return OMPTargetEnterDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13523 Clauses, AssociatedStmt: AStmt);
13524}
13525
13526StmtResult SemaOpenMP::ActOnOpenMPTargetExitDataDirective(
13527 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13528 SourceLocation EndLoc, Stmt *AStmt) {
13529 if (!AStmt)
13530 return StmtError();
13531
13532 setBranchProtectedScope(SemaRef, DKind: OMPD_target_exit_data, AStmt);
13533
13534 // OpenMP [2.10.3, Restrictions, p. 102]
13535 // At least one map clause must appear on the directive.
13536 if (!hasClauses(Clauses, K: OMPC_map)) {
13537 unsigned OMPVersion = getLangOpts().OpenMP;
13538 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13539 << "'map'" << getOpenMPDirectiveName(D: OMPD_target_exit_data, Ver: OMPVersion);
13540 return StmtError();
13541 }
13542
13543 return OMPTargetExitDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13544 Clauses, AssociatedStmt: AStmt);
13545}
13546
13547StmtResult SemaOpenMP::ActOnOpenMPTargetUpdateDirective(
13548 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13549 SourceLocation EndLoc, Stmt *AStmt) {
13550 if (!AStmt)
13551 return StmtError();
13552
13553 setBranchProtectedScope(SemaRef, DKind: OMPD_target_update, AStmt);
13554
13555 if (!hasClauses(Clauses, K: OMPC_to, ClauseTypes: OMPC_from)) {
13556 Diag(Loc: StartLoc, DiagID: diag::err_omp_at_least_one_motion_clause_required);
13557 return StmtError();
13558 }
13559
13560 if (!isClauseMappable(Clauses)) {
13561 Diag(Loc: StartLoc, DiagID: diag::err_omp_cannot_update_with_internal_linkage);
13562 return StmtError();
13563 }
13564
13565 return OMPTargetUpdateDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13566 Clauses, AssociatedStmt: AStmt);
13567}
13568
13569/// This checks whether a \p ClauseType clause \p C has at most \p Max
13570/// expression. If not, a diag of number \p Diag will be emitted.
13571template <typename ClauseType>
13572static bool checkNumExprsInClause(SemaBase &SemaRef,
13573 ArrayRef<OMPClause *> Clauses,
13574 unsigned MaxNum, unsigned Diag) {
13575 auto ClauseItr = llvm::find_if(Clauses, llvm::IsaPred<ClauseType>);
13576 if (ClauseItr == Clauses.end())
13577 return true;
13578 const auto *C = cast<ClauseType>(*ClauseItr);
13579 auto VarList = C->getVarRefs();
13580 if (VarList.size() > MaxNum) {
13581 SemaRef.Diag(VarList[MaxNum]->getBeginLoc(), Diag)
13582 << getOpenMPClauseNameForDiag(C->getClauseKind());
13583 return false;
13584 }
13585 return true;
13586}
13587
13588StmtResult SemaOpenMP::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
13589 Stmt *AStmt,
13590 SourceLocation StartLoc,
13591 SourceLocation EndLoc) {
13592 if (!AStmt)
13593 return StmtError();
13594
13595 if (!checkNumExprsInClause<OMPNumTeamsClause>(
13596 SemaRef&: *this, Clauses, /*MaxNum=*/2,
13597 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
13598 !checkNumExprsInClause<OMPThreadLimitClause>(
13599 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
13600 return StmtError();
13601
13602 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
13603 if (getLangOpts().HIP && (DSAStack->getParentDirective() == OMPD_target))
13604 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
13605
13606 setBranchProtectedScope(SemaRef, DKind: OMPD_teams, AStmt);
13607
13608 DSAStack->setParentTeamsRegionLoc(StartLoc);
13609
13610 return OMPTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13611 AssociatedStmt: AStmt);
13612}
13613
13614StmtResult SemaOpenMP::ActOnOpenMPCancellationPointDirective(
13615 SourceLocation StartLoc, SourceLocation EndLoc,
13616 OpenMPDirectiveKind CancelRegion) {
13617 if (DSAStack->isParentNowaitRegion()) {
13618 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 0;
13619 return StmtError();
13620 }
13621 if (DSAStack->isParentOrderedRegion()) {
13622 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 0;
13623 return StmtError();
13624 }
13625 return OMPCancellationPointDirective::Create(C: getASTContext(), StartLoc,
13626 EndLoc, CancelRegion);
13627}
13628
13629StmtResult SemaOpenMP::ActOnOpenMPCancelDirective(
13630 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13631 SourceLocation EndLoc, OpenMPDirectiveKind CancelRegion) {
13632 if (DSAStack->isParentNowaitRegion()) {
13633 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 1;
13634 return StmtError();
13635 }
13636 if (DSAStack->isParentOrderedRegion()) {
13637 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 1;
13638 return StmtError();
13639 }
13640 DSAStack->setParentCancelRegion(/*Cancel=*/true);
13641 return OMPCancelDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13642 CancelRegion);
13643}
13644
13645static bool checkReductionClauseWithNogroup(Sema &S,
13646 ArrayRef<OMPClause *> Clauses) {
13647 const OMPClause *ReductionClause = nullptr;
13648 const OMPClause *NogroupClause = nullptr;
13649 for (const OMPClause *C : Clauses) {
13650 if (C->getClauseKind() == OMPC_reduction) {
13651 ReductionClause = C;
13652 if (NogroupClause)
13653 break;
13654 continue;
13655 }
13656 if (C->getClauseKind() == OMPC_nogroup) {
13657 NogroupClause = C;
13658 if (ReductionClause)
13659 break;
13660 continue;
13661 }
13662 }
13663 if (ReductionClause && NogroupClause) {
13664 S.Diag(Loc: ReductionClause->getBeginLoc(), DiagID: diag::err_omp_reduction_with_nogroup)
13665 << SourceRange(NogroupClause->getBeginLoc(),
13666 NogroupClause->getEndLoc());
13667 return true;
13668 }
13669 return false;
13670}
13671
13672StmtResult SemaOpenMP::ActOnOpenMPTaskLoopDirective(
13673 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13674 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13675 if (!AStmt)
13676 return StmtError();
13677
13678 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13679 OMPLoopBasedDirective::HelperExprs B;
13680 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13681 // define the nested loops number.
13682 unsigned NestedLoopCount =
13683 checkOpenMPLoop(DKind: OMPD_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13684 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13685 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13686 if (NestedLoopCount == 0)
13687 return StmtError();
13688
13689 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13690 "omp for loop exprs were not built");
13691
13692 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13693 // The grainsize clause and num_tasks clause are mutually exclusive and may
13694 // not appear on the same taskloop directive.
13695 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13696 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13697 return StmtError();
13698 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13699 // If a reduction clause is present on the taskloop directive, the nogroup
13700 // clause must not be specified.
13701 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13702 return StmtError();
13703
13704 SemaRef.setFunctionHasBranchProtectedScope();
13705 return OMPTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13706 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13707 DSAStack->isCancelRegion());
13708}
13709
13710StmtResult SemaOpenMP::ActOnOpenMPTaskLoopSimdDirective(
13711 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13712 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13713 if (!AStmt)
13714 return StmtError();
13715
13716 CapturedStmt *CS =
13717 setBranchProtectedScope(SemaRef, DKind: OMPD_taskloop_simd, AStmt);
13718
13719 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13720 OMPLoopBasedDirective::HelperExprs B;
13721 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13722 // define the nested loops number.
13723 unsigned NestedLoopCount =
13724 checkOpenMPLoop(DKind: OMPD_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13725 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13726 VarsWithImplicitDSA, Built&: B);
13727 if (NestedLoopCount == 0)
13728 return StmtError();
13729
13730 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13731 return StmtError();
13732
13733 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13734 // The grainsize clause and num_tasks clause are mutually exclusive and may
13735 // not appear on the same taskloop directive.
13736 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13737 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13738 return StmtError();
13739 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13740 // If a reduction clause is present on the taskloop directive, the nogroup
13741 // clause must not be specified.
13742 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13743 return StmtError();
13744 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13745 return StmtError();
13746
13747 return OMPTaskLoopSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13748 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13749}
13750
13751StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopDirective(
13752 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13753 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13754 if (!AStmt)
13755 return StmtError();
13756
13757 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13758 OMPLoopBasedDirective::HelperExprs B;
13759 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13760 // define the nested loops number.
13761 unsigned NestedLoopCount =
13762 checkOpenMPLoop(DKind: OMPD_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13763 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13764 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13765 if (NestedLoopCount == 0)
13766 return StmtError();
13767
13768 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13769 "omp for loop exprs were not built");
13770
13771 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13772 // The grainsize clause and num_tasks clause are mutually exclusive and may
13773 // not appear on the same taskloop directive.
13774 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13775 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13776 return StmtError();
13777 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13778 // If a reduction clause is present on the taskloop directive, the nogroup
13779 // clause must not be specified.
13780 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13781 return StmtError();
13782
13783 SemaRef.setFunctionHasBranchProtectedScope();
13784 return OMPMasterTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13785 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13786 DSAStack->isCancelRegion());
13787}
13788
13789StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopDirective(
13790 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13791 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13792 if (!AStmt)
13793 return StmtError();
13794
13795 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13796 OMPLoopBasedDirective::HelperExprs B;
13797 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13798 // define the nested loops number.
13799 unsigned NestedLoopCount =
13800 checkOpenMPLoop(DKind: OMPD_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13801 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13802 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13803 if (NestedLoopCount == 0)
13804 return StmtError();
13805
13806 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13807 "omp for loop exprs were not built");
13808
13809 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13810 // The grainsize clause and num_tasks clause are mutually exclusive and may
13811 // not appear on the same taskloop directive.
13812 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13813 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13814 return StmtError();
13815 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13816 // If a reduction clause is present on the taskloop directive, the nogroup
13817 // clause must not be specified.
13818 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13819 return StmtError();
13820
13821 SemaRef.setFunctionHasBranchProtectedScope();
13822 return OMPMaskedTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13823 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13824 DSAStack->isCancelRegion());
13825}
13826
13827StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopSimdDirective(
13828 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13829 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13830 if (!AStmt)
13831 return StmtError();
13832
13833 CapturedStmt *CS =
13834 setBranchProtectedScope(SemaRef, DKind: OMPD_master_taskloop_simd, AStmt);
13835
13836 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13837 OMPLoopBasedDirective::HelperExprs B;
13838 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13839 // define the nested loops number.
13840 unsigned NestedLoopCount =
13841 checkOpenMPLoop(DKind: OMPD_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13842 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13843 VarsWithImplicitDSA, Built&: B);
13844 if (NestedLoopCount == 0)
13845 return StmtError();
13846
13847 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13848 return StmtError();
13849
13850 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13851 // The grainsize clause and num_tasks clause are mutually exclusive and may
13852 // not appear on the same taskloop directive.
13853 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13854 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13855 return StmtError();
13856 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13857 // If a reduction clause is present on the taskloop directive, the nogroup
13858 // clause must not be specified.
13859 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13860 return StmtError();
13861 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13862 return StmtError();
13863
13864 return OMPMasterTaskLoopSimdDirective::Create(
13865 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13866}
13867
13868StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopSimdDirective(
13869 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13870 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13871 if (!AStmt)
13872 return StmtError();
13873
13874 CapturedStmt *CS =
13875 setBranchProtectedScope(SemaRef, DKind: OMPD_masked_taskloop_simd, AStmt);
13876
13877 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13878 OMPLoopBasedDirective::HelperExprs B;
13879 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13880 // define the nested loops number.
13881 unsigned NestedLoopCount =
13882 checkOpenMPLoop(DKind: OMPD_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13883 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13884 VarsWithImplicitDSA, Built&: B);
13885 if (NestedLoopCount == 0)
13886 return StmtError();
13887
13888 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13889 return StmtError();
13890
13891 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13892 // The grainsize clause and num_tasks clause are mutually exclusive and may
13893 // not appear on the same taskloop directive.
13894 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13895 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13896 return StmtError();
13897 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13898 // If a reduction clause is present on the taskloop directive, the nogroup
13899 // clause must not be specified.
13900 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13901 return StmtError();
13902 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13903 return StmtError();
13904
13905 return OMPMaskedTaskLoopSimdDirective::Create(
13906 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13907}
13908
13909StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopDirective(
13910 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13911 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13912 if (!AStmt)
13913 return StmtError();
13914
13915 CapturedStmt *CS =
13916 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master_taskloop, AStmt);
13917
13918 OMPLoopBasedDirective::HelperExprs B;
13919 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13920 // define the nested loops number.
13921 unsigned NestedLoopCount = checkOpenMPLoop(
13922 DKind: OMPD_parallel_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13923 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13924 VarsWithImplicitDSA, Built&: B);
13925 if (NestedLoopCount == 0)
13926 return StmtError();
13927
13928 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13929 "omp for loop exprs were not built");
13930
13931 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13932 // The grainsize clause and num_tasks clause are mutually exclusive and may
13933 // not appear on the same taskloop directive.
13934 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13935 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13936 return StmtError();
13937 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13938 // If a reduction clause is present on the taskloop directive, the nogroup
13939 // clause must not be specified.
13940 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13941 return StmtError();
13942
13943 return OMPParallelMasterTaskLoopDirective::Create(
13944 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13945 DSAStack->isCancelRegion());
13946}
13947
13948StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopDirective(
13949 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13950 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13951 if (!AStmt)
13952 return StmtError();
13953
13954 CapturedStmt *CS =
13955 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked_taskloop, AStmt);
13956
13957 OMPLoopBasedDirective::HelperExprs B;
13958 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13959 // define the nested loops number.
13960 unsigned NestedLoopCount = checkOpenMPLoop(
13961 DKind: OMPD_parallel_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13962 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13963 VarsWithImplicitDSA, Built&: B);
13964 if (NestedLoopCount == 0)
13965 return StmtError();
13966
13967 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13968 "omp for loop exprs were not built");
13969
13970 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13971 // The grainsize clause and num_tasks clause are mutually exclusive and may
13972 // not appear on the same taskloop directive.
13973 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13974 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13975 return StmtError();
13976 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13977 // If a reduction clause is present on the taskloop directive, the nogroup
13978 // clause must not be specified.
13979 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13980 return StmtError();
13981
13982 return OMPParallelMaskedTaskLoopDirective::Create(
13983 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13984 DSAStack->isCancelRegion());
13985}
13986
13987StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
13988 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13989 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13990 if (!AStmt)
13991 return StmtError();
13992
13993 CapturedStmt *CS = setBranchProtectedScope(
13994 SemaRef, DKind: OMPD_parallel_master_taskloop_simd, AStmt);
13995
13996 OMPLoopBasedDirective::HelperExprs B;
13997 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13998 // define the nested loops number.
13999 unsigned NestedLoopCount = checkOpenMPLoop(
14000 DKind: OMPD_parallel_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14001 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
14002 VarsWithImplicitDSA, Built&: B);
14003 if (NestedLoopCount == 0)
14004 return StmtError();
14005
14006 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14007 return StmtError();
14008
14009 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14010 // The grainsize clause and num_tasks clause are mutually exclusive and may
14011 // not appear on the same taskloop directive.
14012 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14013 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14014 return StmtError();
14015 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14016 // If a reduction clause is present on the taskloop directive, the nogroup
14017 // clause must not be specified.
14018 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14019 return StmtError();
14020 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14021 return StmtError();
14022
14023 return OMPParallelMasterTaskLoopSimdDirective::Create(
14024 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14025}
14026
14027StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
14028 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14029 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14030 if (!AStmt)
14031 return StmtError();
14032
14033 CapturedStmt *CS = setBranchProtectedScope(
14034 SemaRef, DKind: OMPD_parallel_masked_taskloop_simd, AStmt);
14035
14036 OMPLoopBasedDirective::HelperExprs B;
14037 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14038 // define the nested loops number.
14039 unsigned NestedLoopCount = checkOpenMPLoop(
14040 DKind: OMPD_parallel_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14041 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
14042 VarsWithImplicitDSA, Built&: B);
14043 if (NestedLoopCount == 0)
14044 return StmtError();
14045
14046 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14047 return StmtError();
14048
14049 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14050 // The grainsize clause and num_tasks clause are mutually exclusive and may
14051 // not appear on the same taskloop directive.
14052 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
14053 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
14054 return StmtError();
14055 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
14056 // If a reduction clause is present on the taskloop directive, the nogroup
14057 // clause must not be specified.
14058 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
14059 return StmtError();
14060 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14061 return StmtError();
14062
14063 return OMPParallelMaskedTaskLoopSimdDirective::Create(
14064 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14065}
14066
14067StmtResult SemaOpenMP::ActOnOpenMPDistributeDirective(
14068 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14069 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14070 if (!AStmt)
14071 return StmtError();
14072
14073 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
14074 OMPLoopBasedDirective::HelperExprs B;
14075 // In presence of clause 'collapse' with number of loops, it will
14076 // define the nested loops number.
14077 unsigned NestedLoopCount =
14078 checkOpenMPLoop(DKind: OMPD_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14079 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt,
14080 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14081 if (NestedLoopCount == 0)
14082 return StmtError();
14083
14084 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14085 "omp for loop exprs were not built");
14086
14087 SemaRef.setFunctionHasBranchProtectedScope();
14088 auto *DistributeDirective = OMPDistributeDirective::Create(
14089 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14090 return DistributeDirective;
14091}
14092
14093StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForDirective(
14094 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14095 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14096 if (!AStmt)
14097 return StmtError();
14098
14099 CapturedStmt *CS =
14100 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_parallel_for, AStmt);
14101
14102 OMPLoopBasedDirective::HelperExprs B;
14103 // In presence of clause 'collapse' with number of loops, it will
14104 // define the nested loops number.
14105 unsigned NestedLoopCount = checkOpenMPLoop(
14106 DKind: OMPD_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14107 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14108 VarsWithImplicitDSA, Built&: B);
14109 if (NestedLoopCount == 0)
14110 return StmtError();
14111
14112 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14113 "omp for loop exprs were not built");
14114
14115 return OMPDistributeParallelForDirective::Create(
14116 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14117 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14118}
14119
14120StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForSimdDirective(
14121 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14122 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14123 if (!AStmt)
14124 return StmtError();
14125
14126 CapturedStmt *CS = setBranchProtectedScope(
14127 SemaRef, DKind: OMPD_distribute_parallel_for_simd, AStmt);
14128
14129 OMPLoopBasedDirective::HelperExprs B;
14130 // In presence of clause 'collapse' with number of loops, it will
14131 // define the nested loops number.
14132 unsigned NestedLoopCount = checkOpenMPLoop(
14133 DKind: OMPD_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14134 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14135 VarsWithImplicitDSA, Built&: B);
14136 if (NestedLoopCount == 0)
14137 return StmtError();
14138
14139 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14140 return StmtError();
14141
14142 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14143 return StmtError();
14144
14145 return OMPDistributeParallelForSimdDirective::Create(
14146 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14147}
14148
14149StmtResult SemaOpenMP::ActOnOpenMPDistributeSimdDirective(
14150 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14151 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14152 if (!AStmt)
14153 return StmtError();
14154
14155 CapturedStmt *CS =
14156 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_simd, AStmt);
14157
14158 OMPLoopBasedDirective::HelperExprs B;
14159 // In presence of clause 'collapse' with number of loops, it will
14160 // define the nested loops number.
14161 unsigned NestedLoopCount =
14162 checkOpenMPLoop(DKind: OMPD_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14163 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14164 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14165 if (NestedLoopCount == 0)
14166 return StmtError();
14167
14168 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14169 return StmtError();
14170
14171 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14172 return StmtError();
14173
14174 return OMPDistributeSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14175 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14176}
14177
14178StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForSimdDirective(
14179 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14180 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14181 if (!AStmt)
14182 return StmtError();
14183
14184 CapturedStmt *CS =
14185 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for_simd, AStmt);
14186
14187 OMPLoopBasedDirective::HelperExprs B;
14188 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14189 // define the nested loops number.
14190 unsigned NestedLoopCount = checkOpenMPLoop(
14191 DKind: OMPD_target_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14192 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
14193 VarsWithImplicitDSA, Built&: B);
14194 if (NestedLoopCount == 0)
14195 return StmtError();
14196
14197 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14198 return StmtError();
14199
14200 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14201 return StmtError();
14202
14203 return OMPTargetParallelForSimdDirective::Create(
14204 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14205}
14206
14207StmtResult SemaOpenMP::ActOnOpenMPTargetSimdDirective(
14208 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14209 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14210 if (!AStmt)
14211 return StmtError();
14212
14213 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target_simd, AStmt);
14214
14215 OMPLoopBasedDirective::HelperExprs B;
14216 // In presence of clause 'collapse' with number of loops, it will define the
14217 // nested loops number.
14218 unsigned NestedLoopCount =
14219 checkOpenMPLoop(DKind: OMPD_target_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14220 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
14221 VarsWithImplicitDSA, Built&: B);
14222 if (NestedLoopCount == 0)
14223 return StmtError();
14224
14225 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14226 return StmtError();
14227
14228 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14229 return StmtError();
14230
14231 return OMPTargetSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14232 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14233}
14234
14235StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeDirective(
14236 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14237 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14238 if (!AStmt)
14239 return StmtError();
14240
14241 CapturedStmt *CS =
14242 setBranchProtectedScope(SemaRef, DKind: OMPD_teams_distribute, AStmt);
14243
14244 OMPLoopBasedDirective::HelperExprs B;
14245 // In presence of clause 'collapse' with number of loops, it will
14246 // define the nested loops number.
14247 unsigned NestedLoopCount =
14248 checkOpenMPLoop(DKind: OMPD_teams_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14249 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14250 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14251 if (NestedLoopCount == 0)
14252 return StmtError();
14253
14254 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14255 "omp teams distribute loop exprs were not built");
14256
14257 DSAStack->setParentTeamsRegionLoc(StartLoc);
14258
14259 return OMPTeamsDistributeDirective::Create(
14260 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14261}
14262
14263StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeSimdDirective(
14264 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14265 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14266 if (!AStmt)
14267 return StmtError();
14268
14269 CapturedStmt *CS =
14270 setBranchProtectedScope(SemaRef, DKind: OMPD_teams_distribute_simd, AStmt);
14271
14272 OMPLoopBasedDirective::HelperExprs B;
14273 // In presence of clause 'collapse' with number of loops, it will
14274 // define the nested loops number.
14275 unsigned NestedLoopCount = checkOpenMPLoop(
14276 DKind: OMPD_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14277 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14278 VarsWithImplicitDSA, Built&: B);
14279 if (NestedLoopCount == 0)
14280 return StmtError();
14281
14282 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14283 return StmtError();
14284
14285 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14286 return StmtError();
14287
14288 DSAStack->setParentTeamsRegionLoc(StartLoc);
14289
14290 return OMPTeamsDistributeSimdDirective::Create(
14291 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14292}
14293
14294StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
14295 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14296 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14297 if (!AStmt)
14298 return StmtError();
14299
14300 CapturedStmt *CS = setBranchProtectedScope(
14301 SemaRef, DKind: OMPD_teams_distribute_parallel_for_simd, AStmt);
14302
14303 OMPLoopBasedDirective::HelperExprs B;
14304 // In presence of clause 'collapse' with number of loops, it will
14305 // define the nested loops number.
14306 unsigned NestedLoopCount = checkOpenMPLoop(
14307 DKind: OMPD_teams_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14308 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14309 VarsWithImplicitDSA, Built&: B);
14310 if (NestedLoopCount == 0)
14311 return StmtError();
14312
14313 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14314 return StmtError();
14315
14316 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14317 return StmtError();
14318
14319 DSAStack->setParentTeamsRegionLoc(StartLoc);
14320
14321 return OMPTeamsDistributeParallelForSimdDirective::Create(
14322 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14323}
14324
14325StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForDirective(
14326 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14327 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14328 if (!AStmt)
14329 return StmtError();
14330
14331 CapturedStmt *CS = setBranchProtectedScope(
14332 SemaRef, DKind: OMPD_teams_distribute_parallel_for, AStmt);
14333
14334 OMPLoopBasedDirective::HelperExprs B;
14335 // In presence of clause 'collapse' with number of loops, it will
14336 // define the nested loops number.
14337 unsigned NestedLoopCount = checkOpenMPLoop(
14338 DKind: OMPD_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14339 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14340 VarsWithImplicitDSA, Built&: B);
14341
14342 if (NestedLoopCount == 0)
14343 return StmtError();
14344
14345 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14346 "omp for loop exprs were not built");
14347
14348 DSAStack->setParentTeamsRegionLoc(StartLoc);
14349
14350 return OMPTeamsDistributeParallelForDirective::Create(
14351 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14352 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14353}
14354
14355StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDirective(
14356 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14357 SourceLocation EndLoc) {
14358 if (!AStmt)
14359 return StmtError();
14360
14361 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams, AStmt);
14362
14363 const OMPClause *BareClause = nullptr;
14364 bool HasThreadLimitAndNumTeamsClause = hasClauses(Clauses, K: OMPC_num_teams) &&
14365 hasClauses(Clauses, K: OMPC_thread_limit);
14366 bool HasBareClause = llvm::any_of(Range&: Clauses, P: [&](const OMPClause *C) {
14367 BareClause = C;
14368 return C->getClauseKind() == OMPC_ompx_bare;
14369 });
14370
14371 if (HasBareClause && !HasThreadLimitAndNumTeamsClause) {
14372 Diag(Loc: BareClause->getBeginLoc(), DiagID: diag::err_ompx_bare_no_grid);
14373 return StmtError();
14374 }
14375
14376 unsigned ClauseMaxNumExprs = HasBareClause ? 3 : 2;
14377 unsigned NumTeamsDiag = HasBareClause
14378 ? diag::err_ompx_more_than_three_expr_not_allowed
14379 : diag::err_omp_num_teams_multi_expr_not_allowed;
14380 unsigned ThreadLimitDiag =
14381 HasBareClause ? diag::err_ompx_more_than_three_expr_not_allowed
14382 : diag::err_omp_multi_expr_not_allowed;
14383
14384 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14385 SemaRef&: *this, Clauses, MaxNum: ClauseMaxNumExprs, Diag: NumTeamsDiag) ||
14386 !checkNumExprsInClause<OMPThreadLimitClause>(
14387 SemaRef&: *this, Clauses, MaxNum: ClauseMaxNumExprs, Diag: ThreadLimitDiag)) {
14388 return StmtError();
14389 }
14390 return OMPTargetTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14391 Clauses, AssociatedStmt: AStmt);
14392}
14393
14394StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeDirective(
14395 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14396 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14397 if (!AStmt)
14398 return StmtError();
14399
14400 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14401 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14402 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14403 !checkNumExprsInClause<OMPThreadLimitClause>(
14404 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14405 return StmtError();
14406
14407 CapturedStmt *CS =
14408 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_distribute, AStmt);
14409
14410 OMPLoopBasedDirective::HelperExprs B;
14411 // In presence of clause 'collapse' with number of loops, it will
14412 // define the nested loops number.
14413 unsigned NestedLoopCount = checkOpenMPLoop(
14414 DKind: OMPD_target_teams_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14415 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14416 VarsWithImplicitDSA, Built&: B);
14417 if (NestedLoopCount == 0)
14418 return StmtError();
14419
14420 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14421 "omp target teams distribute loop exprs were not built");
14422
14423 return OMPTargetTeamsDistributeDirective::Create(
14424 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14425}
14426
14427StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
14428 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14429 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14430 if (!AStmt)
14431 return StmtError();
14432
14433 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14434 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14435 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14436 !checkNumExprsInClause<OMPThreadLimitClause>(
14437 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14438 return StmtError();
14439
14440 CapturedStmt *CS = setBranchProtectedScope(
14441 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for, AStmt);
14442
14443 OMPLoopBasedDirective::HelperExprs B;
14444 // In presence of clause 'collapse' with number of loops, it will
14445 // define the nested loops number.
14446 unsigned NestedLoopCount = checkOpenMPLoop(
14447 DKind: OMPD_target_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14448 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14449 VarsWithImplicitDSA, Built&: B);
14450 if (NestedLoopCount == 0)
14451 return StmtError();
14452
14453 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14454 return StmtError();
14455
14456 return OMPTargetTeamsDistributeParallelForDirective::Create(
14457 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14458 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14459}
14460
14461StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
14462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14463 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14464 if (!AStmt)
14465 return StmtError();
14466
14467 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14468 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14469 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14470 !checkNumExprsInClause<OMPThreadLimitClause>(
14471 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14472 return StmtError();
14473
14474 CapturedStmt *CS = setBranchProtectedScope(
14475 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for_simd, AStmt);
14476
14477 OMPLoopBasedDirective::HelperExprs B;
14478 // In presence of clause 'collapse' with number of loops, it will
14479 // define the nested loops number.
14480 unsigned NestedLoopCount =
14481 checkOpenMPLoop(DKind: OMPD_target_teams_distribute_parallel_for_simd,
14482 CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14483 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14484 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14485 if (NestedLoopCount == 0)
14486 return StmtError();
14487
14488 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14489 return StmtError();
14490
14491 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14492 return StmtError();
14493
14494 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
14495 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14496}
14497
14498StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeSimdDirective(
14499 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14500 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14501 if (!AStmt)
14502 return StmtError();
14503
14504 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14505 SemaRef&: *this, Clauses, /*MaxNum=*/2,
14506 Diag: diag::err_omp_num_teams_multi_expr_not_allowed) ||
14507 !checkNumExprsInClause<OMPThreadLimitClause>(
14508 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14509 return StmtError();
14510
14511 CapturedStmt *CS = setBranchProtectedScope(
14512 SemaRef, DKind: OMPD_target_teams_distribute_simd, AStmt);
14513
14514 OMPLoopBasedDirective::HelperExprs B;
14515 // In presence of clause 'collapse' with number of loops, it will
14516 // define the nested loops number.
14517 unsigned NestedLoopCount = checkOpenMPLoop(
14518 DKind: OMPD_target_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14519 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14520 VarsWithImplicitDSA, Built&: B);
14521 if (NestedLoopCount == 0)
14522 return StmtError();
14523
14524 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14525 return StmtError();
14526
14527 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14528 return StmtError();
14529
14530 return OMPTargetTeamsDistributeSimdDirective::Create(
14531 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14532}
14533
14534/// Updates OriginalInits by checking Transform against loop transformation
14535/// directives and appending their pre-inits if a match is found.
14536static void updatePreInits(OMPLoopTransformationDirective *Transform,
14537 SmallVectorImpl<Stmt *> &PreInits) {
14538 Stmt *Dir = Transform->getDirective();
14539 switch (Dir->getStmtClass()) {
14540#define STMT(CLASS, PARENT)
14541#define ABSTRACT_STMT(CLASS)
14542#define COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT) \
14543 case Stmt::CLASS##Class: \
14544 appendFlattenedStmtList(PreInits, \
14545 static_cast<const CLASS *>(Dir)->getPreInits()); \
14546 break;
14547#define OMPCANONICALLOOPNESTTRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
14548 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
14549#define OMPCANONICALLOOPSEQUENCETRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
14550 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
14551#include "clang/AST/StmtNodes.inc"
14552#undef COMMON_OMP_LOOP_TRANSFORMATION
14553 default:
14554 llvm_unreachable("Not a loop transformation");
14555 }
14556}
14557
14558bool SemaOpenMP::checkTransformableLoopNest(
14559 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops,
14560 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers,
14561 Stmt *&Body, SmallVectorImpl<SmallVector<Stmt *>> &OriginalInits) {
14562 OriginalInits.emplace_back();
14563 bool Result = OMPLoopBasedDirective::doForAllLoops(
14564 CurStmt: AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops,
14565 Callback: [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt,
14566 Stmt *CurStmt) {
14567 VarsWithInheritedDSAType TmpDSA;
14568 unsigned SingleNumLoops =
14569 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: CurStmt, SemaRef, DSA&: *DSAStack,
14570 VarsWithImplicitDSA&: TmpDSA, Built&: LoopHelpers[Cnt]);
14571 if (SingleNumLoops == 0)
14572 return true;
14573 assert(SingleNumLoops == 1 && "Expect single loop iteration space");
14574 if (auto *For = dyn_cast<ForStmt>(Val: CurStmt)) {
14575 OriginalInits.back().push_back(Elt: For->getInit());
14576 Body = For->getBody();
14577 } else {
14578 assert(isa<CXXForRangeStmt>(CurStmt) &&
14579 "Expected canonical for or range-based for loops.");
14580 auto *CXXFor = cast<CXXForRangeStmt>(Val: CurStmt);
14581 OriginalInits.back().push_back(Elt: CXXFor->getBeginStmt());
14582 Body = CXXFor->getBody();
14583 }
14584 OriginalInits.emplace_back();
14585 return false;
14586 },
14587 OnTransformationCallback: [&OriginalInits](OMPLoopTransformationDirective *Transform) {
14588 updatePreInits(Transform, PreInits&: OriginalInits.back());
14589 });
14590 assert(OriginalInits.back().empty() && "No preinit after innermost loop");
14591 OriginalInits.pop_back();
14592 return Result;
14593}
14594
14595/// Counts the total number of OpenMP canonical nested loops, including the
14596/// outermost loop (the original loop). PRECONDITION of this visitor is that it
14597/// must be invoked from the original loop to be analyzed. The traversal stops
14598/// for Decl's and Expr's given that they may contain inner loops that must not
14599/// be counted.
14600///
14601/// Example AST structure for the code:
14602///
14603/// int main() {
14604/// #pragma omp fuse
14605/// {
14606/// for (int i = 0; i < 100; i++) { <-- Outer loop
14607/// []() {
14608/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (1)
14609/// };
14610/// for(int j = 0; j < 5; ++j) {} <-- Inner loop
14611/// }
14612/// for (int r = 0; i < 100; i++) { <-- Outer loop
14613/// struct LocalClass {
14614/// void bar() {
14615/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (2)
14616/// }
14617/// };
14618/// for(int k = 0; k < 10; ++k) {} <-- Inner loop
14619/// {x = 5; for(k = 0; k < 10; ++k) x += k; x}; <-- NOT A LOOP (3)
14620/// }
14621/// }
14622/// }
14623/// (1) because in a different function (here: a lambda)
14624/// (2) because in a different function (here: class method)
14625/// (3) because considered to be intervening-code of non-perfectly nested loop
14626/// Result: Loop 'i' contains 2 loops, Loop 'r' also contains 2 loops.
14627class NestedLoopCounterVisitor final : public DynamicRecursiveASTVisitor {
14628private:
14629 unsigned NestedLoopCount = 0;
14630
14631public:
14632 explicit NestedLoopCounterVisitor() = default;
14633
14634 unsigned getNestedLoopCount() const { return NestedLoopCount; }
14635
14636 bool VisitForStmt(ForStmt *FS) override {
14637 ++NestedLoopCount;
14638 return true;
14639 }
14640
14641 bool VisitCXXForRangeStmt(CXXForRangeStmt *FRS) override {
14642 ++NestedLoopCount;
14643 return true;
14644 }
14645
14646 bool TraverseStmt(Stmt *S) override {
14647 if (!S)
14648 return true;
14649
14650 // Skip traversal of all expressions, including special cases like
14651 // LambdaExpr, StmtExpr, BlockExpr, and RequiresExpr. These expressions
14652 // may contain inner statements (and even loops), but they are not part
14653 // of the syntactic body of the surrounding loop structure.
14654 // Therefore must not be counted.
14655 if (isa<Expr>(Val: S))
14656 return true;
14657
14658 // Only recurse into CompoundStmt (block {}) and loop bodies.
14659 if (isa<CompoundStmt, ForStmt, CXXForRangeStmt>(Val: S)) {
14660 return DynamicRecursiveASTVisitor::TraverseStmt(S);
14661 }
14662
14663 // Stop traversal of the rest of statements, that break perfect
14664 // loop nesting, such as control flow (IfStmt, SwitchStmt...).
14665 return true;
14666 }
14667
14668 bool TraverseDecl(Decl *D) override {
14669 // Stop in the case of finding a declaration, it is not important
14670 // in order to find nested loops (Possible CXXRecordDecl, RecordDecl,
14671 // FunctionDecl...).
14672 return true;
14673 }
14674};
14675
14676bool SemaOpenMP::analyzeLoopSequence(Stmt *LoopSeqStmt,
14677 LoopSequenceAnalysis &SeqAnalysis,
14678 ASTContext &Context,
14679 OpenMPDirectiveKind Kind) {
14680 VarsWithInheritedDSAType TmpDSA;
14681 // Helper Lambda to handle storing initialization and body statements for
14682 // both ForStmt and CXXForRangeStmt.
14683 auto StoreLoopStatements = [](LoopAnalysis &Analysis, Stmt *LoopStmt) {
14684 if (auto *For = dyn_cast<ForStmt>(Val: LoopStmt)) {
14685 Analysis.OriginalInits.push_back(Elt: For->getInit());
14686 Analysis.TheForStmt = For;
14687 } else {
14688 auto *CXXFor = cast<CXXForRangeStmt>(Val: LoopStmt);
14689 Analysis.OriginalInits.push_back(Elt: CXXFor->getBeginStmt());
14690 Analysis.TheForStmt = CXXFor;
14691 }
14692 };
14693
14694 // Helper lambda functions to encapsulate the processing of different
14695 // derivations of the canonical loop sequence grammar
14696 // Modularized code for handling loop generation and transformations.
14697 auto AnalyzeLoopGeneration = [&](Stmt *Child) {
14698 auto *LoopTransform = cast<OMPLoopTransformationDirective>(Val: Child);
14699 Stmt *TransformedStmt = LoopTransform->getTransformedStmt();
14700 unsigned NumGeneratedTopLevelLoops =
14701 LoopTransform->getNumGeneratedTopLevelLoops();
14702 // Handle the case where transformed statement is not available due to
14703 // dependent contexts
14704 if (!TransformedStmt) {
14705 if (NumGeneratedTopLevelLoops > 0) {
14706 SeqAnalysis.LoopSeqSize += NumGeneratedTopLevelLoops;
14707 return true;
14708 }
14709 // Unroll full (0 loops produced)
14710 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14711 << 0 << getOpenMPDirectiveName(D: Kind);
14712 return false;
14713 }
14714 // Handle loop transformations with multiple loop nests
14715 // Unroll full
14716 if (!NumGeneratedTopLevelLoops) {
14717 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14718 << 0 << getOpenMPDirectiveName(D: Kind);
14719 return false;
14720 }
14721 // Loop transformatons such as split or loopranged fuse
14722 if (NumGeneratedTopLevelLoops > 1) {
14723 // Get the preinits related to this loop sequence generating
14724 // loop transformation (i.e loopranged fuse, split...)
14725 // These preinits differ slightly from regular inits/pre-inits related
14726 // to single loop generating loop transformations (interchange, unroll)
14727 // given that they are not bounded to a particular loop nest
14728 // so they need to be treated independently
14729 updatePreInits(Transform: LoopTransform, PreInits&: SeqAnalysis.LoopSequencePreInits);
14730 return analyzeLoopSequence(LoopSeqStmt: TransformedStmt, SeqAnalysis, Context, Kind);
14731 }
14732 // Vast majority: (Tile, Unroll, Stripe, Reverse, Interchange, Fuse all)
14733 // Process the transformed loop statement
14734 LoopAnalysis &NewTransformedSingleLoop =
14735 SeqAnalysis.Loops.emplace_back(Args&: Child);
14736 unsigned IsCanonical = checkOpenMPLoop(
14737 DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: TransformedStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA&: TmpDSA,
14738 Built&: NewTransformedSingleLoop.HelperExprs);
14739
14740 if (!IsCanonical)
14741 return false;
14742
14743 StoreLoopStatements(NewTransformedSingleLoop, TransformedStmt);
14744 updatePreInits(Transform: LoopTransform, PreInits&: NewTransformedSingleLoop.TransformsPreInits);
14745
14746 SeqAnalysis.LoopSeqSize++;
14747 return true;
14748 };
14749
14750 // Modularized code for handling regular canonical loops.
14751 auto AnalyzeRegularLoop = [&](Stmt *Child) {
14752 LoopAnalysis &NewRegularLoop = SeqAnalysis.Loops.emplace_back(Args&: Child);
14753 unsigned IsCanonical =
14754 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: Child, SemaRef, DSA&: *DSAStack,
14755 VarsWithImplicitDSA&: TmpDSA, Built&: NewRegularLoop.HelperExprs);
14756
14757 if (!IsCanonical)
14758 return false;
14759
14760 StoreLoopStatements(NewRegularLoop, Child);
14761 NestedLoopCounterVisitor NLCV;
14762 NLCV.TraverseStmt(S: Child);
14763 return true;
14764 };
14765
14766 // High level grammar validation.
14767 for (Stmt *Child : LoopSeqStmt->children()) {
14768 if (!Child)
14769 continue;
14770 // Skip over non-loop-sequence statements.
14771 if (!LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
14772 Child = Child->IgnoreContainers();
14773 // Ignore empty compound statement.
14774 if (!Child)
14775 continue;
14776 // In the case of a nested loop sequence ignoring containers would not
14777 // be enough, a recurisve transversal of the loop sequence is required.
14778 if (isa<CompoundStmt>(Val: Child)) {
14779 if (!analyzeLoopSequence(LoopSeqStmt: Child, SeqAnalysis, Context, Kind))
14780 return false;
14781 // Already been treated, skip this children
14782 continue;
14783 }
14784 }
14785 // Regular loop sequence handling.
14786 if (LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
14787 if (LoopAnalysis::isLoopTransformation(S: Child)) {
14788 if (!AnalyzeLoopGeneration(Child))
14789 return false;
14790 // AnalyzeLoopGeneration updates SeqAnalysis.LoopSeqSize accordingly.
14791 } else {
14792 if (!AnalyzeRegularLoop(Child))
14793 return false;
14794 SeqAnalysis.LoopSeqSize++;
14795 }
14796 } else {
14797 // Report error for invalid statement inside canonical loop sequence.
14798 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14799 << 0 << getOpenMPDirectiveName(D: Kind);
14800 return false;
14801 }
14802 }
14803 return true;
14804}
14805
14806bool SemaOpenMP::checkTransformableLoopSequence(
14807 OpenMPDirectiveKind Kind, Stmt *AStmt, LoopSequenceAnalysis &SeqAnalysis,
14808 ASTContext &Context) {
14809 // Following OpenMP 6.0 API Specification, a Canonical Loop Sequence follows
14810 // the grammar:
14811 //
14812 // canonical-loop-sequence:
14813 // {
14814 // loop-sequence+
14815 // }
14816 // where loop-sequence can be any of the following:
14817 // 1. canonical-loop-sequence
14818 // 2. loop-nest
14819 // 3. loop-sequence-generating-construct (i.e OMPLoopTransformationDirective)
14820 //
14821 // To recognise and traverse this structure the helper function
14822 // analyzeLoopSequence serves as the recurisve entry point
14823 // and tries to match the input AST to the canonical loop sequence grammar
14824 // structure. This function will perform both a semantic and syntactical
14825 // analysis of the given statement according to OpenMP 6.0 definition of
14826 // the aforementioned canonical loop sequence.
14827
14828 // We expect an outer compound statement.
14829 if (!isa<CompoundStmt>(Val: AStmt)) {
14830 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_not_a_loop_sequence)
14831 << getOpenMPDirectiveName(D: Kind);
14832 return false;
14833 }
14834
14835 // Recursive entry point to process the main loop sequence
14836 if (!analyzeLoopSequence(LoopSeqStmt: AStmt, SeqAnalysis, Context, Kind))
14837 return false;
14838
14839 // Diagnose an empty loop sequence.
14840 if (!SeqAnalysis.LoopSeqSize) {
14841 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_empty_loop_sequence)
14842 << getOpenMPDirectiveName(D: Kind);
14843 return false;
14844 }
14845 return true;
14846}
14847
14848/// Add preinit statements that need to be propagated from the selected loop.
14849static void addLoopPreInits(ASTContext &Context,
14850 OMPLoopBasedDirective::HelperExprs &LoopHelper,
14851 Stmt *LoopStmt, ArrayRef<Stmt *> OriginalInit,
14852 SmallVectorImpl<Stmt *> &PreInits) {
14853
14854 // For range-based for-statements, ensure that their syntactic sugar is
14855 // executed by adding them as pre-init statements.
14856 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt)) {
14857 Stmt *RangeInit = CXXRangeFor->getInit();
14858 if (RangeInit)
14859 PreInits.push_back(Elt: RangeInit);
14860
14861 DeclStmt *RangeStmt = CXXRangeFor->getRangeStmt();
14862 PreInits.push_back(Elt: new (Context) DeclStmt(RangeStmt->getDeclGroup(),
14863 RangeStmt->getBeginLoc(),
14864 RangeStmt->getEndLoc()));
14865
14866 DeclStmt *RangeEnd = CXXRangeFor->getEndStmt();
14867 PreInits.push_back(Elt: new (Context) DeclStmt(RangeEnd->getDeclGroup(),
14868 RangeEnd->getBeginLoc(),
14869 RangeEnd->getEndLoc()));
14870 }
14871
14872 llvm::append_range(C&: PreInits, R&: OriginalInit);
14873
14874 // List of OMPCapturedExprDecl, for __begin, __end, and NumIterations
14875 if (auto *PI = cast_or_null<DeclStmt>(Val: LoopHelper.PreInits)) {
14876 PreInits.push_back(Elt: new (Context) DeclStmt(
14877 PI->getDeclGroup(), PI->getBeginLoc(), PI->getEndLoc()));
14878 }
14879
14880 // Gather declarations for the data members used as counters.
14881 for (Expr *CounterRef : LoopHelper.Counters) {
14882 auto *CounterDecl = cast<DeclRefExpr>(Val: CounterRef)->getDecl();
14883 if (isa<OMPCapturedExprDecl>(Val: CounterDecl))
14884 PreInits.push_back(Elt: new (Context) DeclStmt(
14885 DeclGroupRef(CounterDecl), SourceLocation(), SourceLocation()));
14886 }
14887}
14888
14889/// Collect the loop statements (ForStmt or CXXRangeForStmt) of the affected
14890/// loop of a construct.
14891static void collectLoopStmts(Stmt *AStmt, MutableArrayRef<Stmt *> LoopStmts) {
14892 size_t NumLoops = LoopStmts.size();
14893 OMPLoopBasedDirective::doForAllLoops(
14894 CurStmt: AStmt, /*TryImperfectlyNestedLoops=*/false, NumLoops,
14895 Callback: [LoopStmts](unsigned Cnt, Stmt *CurStmt) {
14896 assert(!LoopStmts[Cnt] && "Loop statement must not yet be assigned");
14897 LoopStmts[Cnt] = CurStmt;
14898 return false;
14899 });
14900 assert(!is_contained(LoopStmts, nullptr) &&
14901 "Expecting a loop statement for each affected loop");
14902}
14903
14904/// Build and return a DeclRefExpr for the floor induction variable using the
14905/// SemaRef and the provided parameters.
14906static Expr *makeFloorIVRef(Sema &SemaRef, ArrayRef<VarDecl *> FloorIndVars,
14907 int I, QualType IVTy, DeclRefExpr *OrigCntVar) {
14908 return buildDeclRefExpr(S&: SemaRef, D: FloorIndVars[I], Ty: IVTy,
14909 Loc: OrigCntVar->getExprLoc());
14910}
14911
14912StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
14913 Stmt *AStmt,
14914 SourceLocation StartLoc,
14915 SourceLocation EndLoc) {
14916 ASTContext &Context = getASTContext();
14917 Scope *CurScope = SemaRef.getCurScope();
14918
14919 const auto *SizesClause =
14920 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
14921 if (!SizesClause ||
14922 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](Expr *E) { return !E; }))
14923 return StmtError();
14924 unsigned NumLoops = SizesClause->getNumSizes();
14925
14926 // Empty statement should only be possible if there already was an error.
14927 if (!AStmt)
14928 return StmtError();
14929
14930 // Verify and diagnose loop nest.
14931 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
14932 Stmt *Body = nullptr;
14933 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
14934 if (!checkTransformableLoopNest(Kind: OMPD_tile, AStmt, NumLoops, LoopHelpers, Body,
14935 OriginalInits))
14936 return StmtError();
14937
14938 // Delay tiling to when template is completely instantiated.
14939 if (SemaRef.CurContext->isDependentContext())
14940 return OMPTileDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
14941 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
14942
14943 assert(LoopHelpers.size() == NumLoops &&
14944 "Expecting loop iteration space dimensionality to match number of "
14945 "affected loops");
14946 assert(OriginalInits.size() == NumLoops &&
14947 "Expecting loop iteration space dimensionality to match number of "
14948 "affected loops");
14949
14950 // Collect all affected loop statements.
14951 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
14952 collectLoopStmts(AStmt, LoopStmts);
14953
14954 SmallVector<Stmt *, 4> PreInits;
14955 CaptureVars CopyTransformer(SemaRef);
14956
14957 // Create iteration variables for the generated loops.
14958 SmallVector<VarDecl *, 4> FloorIndVars;
14959 SmallVector<VarDecl *, 4> TileIndVars;
14960 FloorIndVars.resize(N: NumLoops);
14961 TileIndVars.resize(N: NumLoops);
14962 for (unsigned I = 0; I < NumLoops; ++I) {
14963 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
14964
14965 assert(LoopHelper.Counters.size() == 1 &&
14966 "Expect single-dimensional loop iteration space");
14967 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
14968 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
14969 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
14970 QualType CntTy = IterVarRef->getType();
14971
14972 // Iteration variable for the floor (i.e. outer) loop.
14973 {
14974 std::string FloorCntName =
14975 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
14976 VarDecl *FloorCntDecl =
14977 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
14978 FloorIndVars[I] = FloorCntDecl;
14979 }
14980
14981 // Iteration variable for the tile (i.e. inner) loop.
14982 {
14983 std::string TileCntName =
14984 (Twine(".tile_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
14985
14986 // Reuse the iteration variable created by checkOpenMPLoop. It is also
14987 // used by the expressions to derive the original iteration variable's
14988 // value from the logical iteration number.
14989 auto *TileCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
14990 TileCntDecl->setDeclName(
14991 &SemaRef.PP.getIdentifierTable().get(Name: TileCntName));
14992 TileIndVars[I] = TileCntDecl;
14993 }
14994
14995 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
14996 PreInits);
14997 }
14998
14999 // Once the original iteration values are set, append the innermost body.
15000 Stmt *Inner = Body;
15001
15002 auto MakeDimTileSize = [&SemaRef = this->SemaRef, &CopyTransformer, &Context,
15003 SizesClause, CurScope](int I) -> Expr * {
15004 Expr *DimTileSizeExpr = SizesClause->getSizesRefs()[I];
15005
15006 if (DimTileSizeExpr->containsErrors())
15007 return nullptr;
15008
15009 if (isa<ConstantExpr>(Val: DimTileSizeExpr))
15010 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr));
15011
15012 // When the tile size is not a constant but a variable, it is possible to
15013 // pass non-positive numbers. For instance:
15014 // \code{c}
15015 // int a = 0;
15016 // #pragma omp tile sizes(a)
15017 // for (int i = 0; i < 42; ++i)
15018 // body(i);
15019 // \endcode
15020 // Although there is no meaningful interpretation of the tile size, the body
15021 // should still be executed 42 times to avoid surprises. To preserve the
15022 // invariant that every loop iteration is executed exactly once and not
15023 // cause an infinite loop, apply a minimum tile size of one.
15024 // Build expr:
15025 // \code{c}
15026 // (TS <= 0) ? 1 : TS
15027 // \endcode
15028 QualType DimTy = DimTileSizeExpr->getType();
15029 uint64_t DimWidth = Context.getTypeSize(T: DimTy);
15030 IntegerLiteral *Zero = IntegerLiteral::Create(
15031 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
15032 IntegerLiteral *One =
15033 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
15034 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
15035 S: CurScope, OpLoc: {}, Opc: BO_LE,
15036 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), RHSExpr: Zero));
15037 Expr *MinOne = new (Context) ConditionalOperator(
15038 Cond, {}, One, {},
15039 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), DimTy,
15040 VK_PRValue, OK_Ordinary);
15041 return MinOne;
15042 };
15043
15044 // Create tile loops from the inside to the outside.
15045 for (int I = NumLoops - 1; I >= 0; --I) {
15046 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15047 Expr *NumIterations = LoopHelper.NumIterations;
15048 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15049 QualType IVTy = NumIterations->getType();
15050 Stmt *LoopStmt = LoopStmts[I];
15051
15052 // Commonly used variables. One of the constraints of an AST is that every
15053 // node object must appear at most once, hence we define a lambda that
15054 // creates a new AST node at every use.
15055 auto MakeTileIVRef = [&SemaRef = this->SemaRef, &TileIndVars, I, IVTy,
15056 OrigCntVar]() {
15057 return buildDeclRefExpr(S&: SemaRef, D: TileIndVars[I], Ty: IVTy,
15058 Loc: OrigCntVar->getExprLoc());
15059 };
15060
15061 // For init-statement: auto .tile.iv = .floor.iv
15062 SemaRef.AddInitializerToDecl(
15063 dcl: TileIndVars[I],
15064 init: SemaRef
15065 .DefaultLvalueConversion(
15066 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
15067 .get(),
15068 /*DirectInit=*/false);
15069 Decl *CounterDecl = TileIndVars[I];
15070 StmtResult InitStmt = new (Context)
15071 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15072 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15073 if (!InitStmt.isUsable())
15074 return StmtError();
15075
15076 // For cond-expression:
15077 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations)
15078 Expr *DimTileSize = MakeDimTileSize(I);
15079 if (!DimTileSize)
15080 return StmtError();
15081 ExprResult EndOfTile = SemaRef.BuildBinOp(
15082 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15083 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15084 RHSExpr: DimTileSize);
15085 if (!EndOfTile.isUsable())
15086 return StmtError();
15087 ExprResult IsPartialTile =
15088 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15089 LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
15090 if (!IsPartialTile.isUsable())
15091 return StmtError();
15092 ExprResult MinTileAndIterSpace = SemaRef.ActOnConditionalOp(
15093 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
15094 CondExpr: IsPartialTile.get(), LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
15095 if (!MinTileAndIterSpace.isUsable())
15096 return StmtError();
15097 ExprResult CondExpr =
15098 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15099 LHSExpr: MakeTileIVRef(), RHSExpr: MinTileAndIterSpace.get());
15100 if (!CondExpr.isUsable())
15101 return StmtError();
15102
15103 // For incr-statement: ++.tile.iv
15104 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15105 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeTileIVRef());
15106 if (!IncrStmt.isUsable())
15107 return StmtError();
15108
15109 // Statements to set the original iteration variable's value from the
15110 // logical iteration number.
15111 // Generated for loop is:
15112 // \code
15113 // Original_for_init;
15114 // for (auto .tile.iv = .floor.iv;
15115 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations);
15116 // ++.tile.iv) {
15117 // Original_Body;
15118 // Original_counter_update;
15119 // }
15120 // \endcode
15121 // FIXME: If the innermost body is an loop itself, inserting these
15122 // statements stops it being recognized as a perfectly nested loop (e.g.
15123 // for applying tiling again). If this is the case, sink the expressions
15124 // further into the inner loop.
15125 SmallVector<Stmt *, 4> BodyParts;
15126 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15127 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15128 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
15129 BodyParts.push_back(Elt: Inner);
15130 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
15131 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
15132 Inner = new (Context)
15133 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15134 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15135 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15136 }
15137
15138 // Create floor loops from the inside to the outside.
15139 for (int I = NumLoops - 1; I >= 0; --I) {
15140 auto &LoopHelper = LoopHelpers[I];
15141 Expr *NumIterations = LoopHelper.NumIterations;
15142 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15143 QualType IVTy = NumIterations->getType();
15144
15145 // For init-statement: auto .floor.iv = 0
15146 SemaRef.AddInitializerToDecl(
15147 dcl: FloorIndVars[I],
15148 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15149 /*DirectInit=*/false);
15150 Decl *CounterDecl = FloorIndVars[I];
15151 StmtResult InitStmt = new (Context)
15152 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15153 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15154 if (!InitStmt.isUsable())
15155 return StmtError();
15156
15157 // For cond-expression: .floor.iv < NumIterations
15158 ExprResult CondExpr = SemaRef.BuildBinOp(
15159 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15160 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15161 RHSExpr: NumIterations);
15162 if (!CondExpr.isUsable())
15163 return StmtError();
15164
15165 // For incr-statement: .floor.iv += DimTileSize
15166 Expr *DimTileSize = MakeDimTileSize(I);
15167 if (!DimTileSize)
15168 return StmtError();
15169 ExprResult IncrStmt = SemaRef.BuildBinOp(
15170 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15171 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15172 RHSExpr: DimTileSize);
15173 if (!IncrStmt.isUsable())
15174 return StmtError();
15175
15176 Inner = new (Context)
15177 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15178 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15179 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15180 }
15181
15182 return OMPTileDirective::Create(C: Context, StartLoc, EndLoc, Clauses, NumLoops,
15183 AssociatedStmt: AStmt, TransformedStmt: Inner,
15184 PreInits: buildPreInits(Context, PreInits));
15185}
15186
15187StmtResult SemaOpenMP::ActOnOpenMPStripeDirective(ArrayRef<OMPClause *> Clauses,
15188 Stmt *AStmt,
15189 SourceLocation StartLoc,
15190 SourceLocation EndLoc) {
15191 ASTContext &Context = getASTContext();
15192 Scope *CurScope = SemaRef.getCurScope();
15193
15194 const auto *SizesClause =
15195 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
15196 if (!SizesClause ||
15197 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](const Expr *SizeExpr) {
15198 return !SizeExpr || SizeExpr->containsErrors();
15199 }))
15200 return StmtError();
15201 unsigned NumLoops = SizesClause->getNumSizes();
15202
15203 // Empty statement should only be possible if there already was an error.
15204 if (!AStmt)
15205 return StmtError();
15206
15207 // Verify and diagnose loop nest.
15208 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
15209 Stmt *Body = nullptr;
15210 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
15211 if (!checkTransformableLoopNest(Kind: OMPD_stripe, AStmt, NumLoops, LoopHelpers,
15212 Body, OriginalInits))
15213 return StmtError();
15214
15215 // Delay striping to when template is completely instantiated.
15216 if (SemaRef.CurContext->isDependentContext())
15217 return OMPStripeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15218 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
15219
15220 assert(LoopHelpers.size() == NumLoops &&
15221 "Expecting loop iteration space dimensionality to match number of "
15222 "affected loops");
15223 assert(OriginalInits.size() == NumLoops &&
15224 "Expecting loop iteration space dimensionality to match number of "
15225 "affected loops");
15226
15227 // Collect all affected loop statements.
15228 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
15229 collectLoopStmts(AStmt, LoopStmts);
15230
15231 SmallVector<Stmt *, 4> PreInits;
15232 CaptureVars CopyTransformer(SemaRef);
15233
15234 // Create iteration variables for the generated loops.
15235 SmallVector<VarDecl *, 4> FloorIndVars;
15236 SmallVector<VarDecl *, 4> StripeIndVars;
15237 FloorIndVars.resize(N: NumLoops);
15238 StripeIndVars.resize(N: NumLoops);
15239 for (unsigned I : llvm::seq<unsigned>(Size: NumLoops)) {
15240 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15241
15242 assert(LoopHelper.Counters.size() == 1 &&
15243 "Expect single-dimensional loop iteration space");
15244 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15245 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
15246 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15247 QualType CntTy = IterVarRef->getType();
15248
15249 // Iteration variable for the stripe (i.e. outer) loop.
15250 {
15251 std::string FloorCntName =
15252 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
15253 VarDecl *FloorCntDecl =
15254 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
15255 FloorIndVars[I] = FloorCntDecl;
15256 }
15257
15258 // Iteration variable for the stripe (i.e. inner) loop.
15259 {
15260 std::string StripeCntName =
15261 (Twine(".stripe_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
15262
15263 // Reuse the iteration variable created by checkOpenMPLoop. It is also
15264 // used by the expressions to derive the original iteration variable's
15265 // value from the logical iteration number.
15266 auto *StripeCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
15267 StripeCntDecl->setDeclName(
15268 &SemaRef.PP.getIdentifierTable().get(Name: StripeCntName));
15269 StripeIndVars[I] = StripeCntDecl;
15270 }
15271
15272 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
15273 PreInits);
15274 }
15275
15276 // Once the original iteration values are set, append the innermost body.
15277 Stmt *Inner = Body;
15278
15279 auto MakeDimStripeSize = [&](int I) -> Expr * {
15280 Expr *DimStripeSizeExpr = SizesClause->getSizesRefs()[I];
15281 if (isa<ConstantExpr>(Val: DimStripeSizeExpr))
15282 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr));
15283
15284 // When the stripe size is not a constant but a variable, it is possible to
15285 // pass non-positive numbers. For instance:
15286 // \code{c}
15287 // int a = 0;
15288 // #pragma omp stripe sizes(a)
15289 // for (int i = 0; i < 42; ++i)
15290 // body(i);
15291 // \endcode
15292 // Although there is no meaningful interpretation of the stripe size, the
15293 // body should still be executed 42 times to avoid surprises. To preserve
15294 // the invariant that every loop iteration is executed exactly once and not
15295 // cause an infinite loop, apply a minimum stripe size of one.
15296 // Build expr:
15297 // \code{c}
15298 // (TS <= 0) ? 1 : TS
15299 // \endcode
15300 QualType DimTy = DimStripeSizeExpr->getType();
15301 uint64_t DimWidth = Context.getTypeSize(T: DimTy);
15302 IntegerLiteral *Zero = IntegerLiteral::Create(
15303 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
15304 IntegerLiteral *One =
15305 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
15306 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
15307 S: CurScope, OpLoc: {}, Opc: BO_LE,
15308 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), RHSExpr: Zero));
15309 Expr *MinOne = new (Context) ConditionalOperator(
15310 Cond, {}, One, {},
15311 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), DimTy,
15312 VK_PRValue, OK_Ordinary);
15313 return MinOne;
15314 };
15315
15316 // Create stripe loops from the inside to the outside.
15317 for (int I = NumLoops - 1; I >= 0; --I) {
15318 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15319 Expr *NumIterations = LoopHelper.NumIterations;
15320 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15321 QualType IVTy = NumIterations->getType();
15322 Stmt *LoopStmt = LoopStmts[I];
15323
15324 // For init-statement: auto .stripe.iv = .floor.iv
15325 SemaRef.AddInitializerToDecl(
15326 dcl: StripeIndVars[I],
15327 init: SemaRef
15328 .DefaultLvalueConversion(
15329 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
15330 .get(),
15331 /*DirectInit=*/false);
15332 Decl *CounterDecl = StripeIndVars[I];
15333 StmtResult InitStmt = new (Context)
15334 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15335 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15336 if (!InitStmt.isUsable())
15337 return StmtError();
15338
15339 // For cond-expression:
15340 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations)
15341 ExprResult EndOfStripe = SemaRef.BuildBinOp(
15342 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15343 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15344 RHSExpr: MakeDimStripeSize(I));
15345 if (!EndOfStripe.isUsable())
15346 return StmtError();
15347 ExprResult IsPartialStripe =
15348 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15349 LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
15350 if (!IsPartialStripe.isUsable())
15351 return StmtError();
15352 ExprResult MinStripeAndIterSpace = SemaRef.ActOnConditionalOp(
15353 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
15354 CondExpr: IsPartialStripe.get(), LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
15355 if (!MinStripeAndIterSpace.isUsable())
15356 return StmtError();
15357 ExprResult CondExpr = SemaRef.BuildBinOp(
15358 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15359 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar),
15360 RHSExpr: MinStripeAndIterSpace.get());
15361 if (!CondExpr.isUsable())
15362 return StmtError();
15363
15364 // For incr-statement: ++.stripe.iv
15365 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15366 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc,
15367 Input: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar));
15368 if (!IncrStmt.isUsable())
15369 return StmtError();
15370
15371 // Statements to set the original iteration variable's value from the
15372 // logical iteration number.
15373 // Generated for loop is:
15374 // \code
15375 // Original_for_init;
15376 // for (auto .stripe.iv = .floor.iv;
15377 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations);
15378 // ++.stripe.iv) {
15379 // Original_Body;
15380 // Original_counter_update;
15381 // }
15382 // \endcode
15383 // FIXME: If the innermost body is a loop itself, inserting these
15384 // statements stops it being recognized as a perfectly nested loop (e.g.
15385 // for applying another loop transformation). If this is the case, sink the
15386 // expressions further into the inner loop.
15387 SmallVector<Stmt *, 4> BodyParts;
15388 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15389 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15390 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
15391 BodyParts.push_back(Elt: Inner);
15392 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
15393 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
15394 Inner = new (Context)
15395 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15396 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15397 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15398 }
15399
15400 // Create grid loops from the inside to the outside.
15401 for (int I = NumLoops - 1; I >= 0; --I) {
15402 auto &LoopHelper = LoopHelpers[I];
15403 Expr *NumIterations = LoopHelper.NumIterations;
15404 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15405 QualType IVTy = NumIterations->getType();
15406
15407 // For init-statement: auto .grid.iv = 0
15408 SemaRef.AddInitializerToDecl(
15409 dcl: FloorIndVars[I],
15410 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15411 /*DirectInit=*/false);
15412 Decl *CounterDecl = FloorIndVars[I];
15413 StmtResult InitStmt = new (Context)
15414 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15415 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15416 if (!InitStmt.isUsable())
15417 return StmtError();
15418
15419 // For cond-expression: .floor.iv < NumIterations
15420 ExprResult CondExpr = SemaRef.BuildBinOp(
15421 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15422 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15423 RHSExpr: NumIterations);
15424 if (!CondExpr.isUsable())
15425 return StmtError();
15426
15427 // For incr-statement: .floor.iv += DimStripeSize
15428 ExprResult IncrStmt = SemaRef.BuildBinOp(
15429 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15430 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15431 RHSExpr: MakeDimStripeSize(I));
15432 if (!IncrStmt.isUsable())
15433 return StmtError();
15434
15435 Inner = new (Context)
15436 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15437 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15438 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15439 }
15440
15441 return OMPStripeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15442 NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
15443 PreInits: buildPreInits(Context, PreInits));
15444}
15445
15446StmtResult SemaOpenMP::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses,
15447 Stmt *AStmt,
15448 SourceLocation StartLoc,
15449 SourceLocation EndLoc) {
15450 ASTContext &Context = getASTContext();
15451 Scope *CurScope = SemaRef.getCurScope();
15452 // Empty statement should only be possible if there already was an error.
15453 if (!AStmt)
15454 return StmtError();
15455
15456 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
15457 MutuallyExclusiveClauses: {OMPC_partial, OMPC_full}))
15458 return StmtError();
15459
15460 const OMPFullClause *FullClause =
15461 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses);
15462 const OMPPartialClause *PartialClause =
15463 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses);
15464 assert(!(FullClause && PartialClause) &&
15465 "mutual exclusivity must have been checked before");
15466
15467 constexpr unsigned NumLoops = 1;
15468 Stmt *Body = nullptr;
15469 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
15470 NumLoops);
15471 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
15472 if (!checkTransformableLoopNest(Kind: OMPD_unroll, AStmt, NumLoops, LoopHelpers,
15473 Body, OriginalInits))
15474 return StmtError();
15475
15476 unsigned NumGeneratedTopLevelLoops = PartialClause ? 1 : 0;
15477
15478 // Delay unrolling to when template is completely instantiated.
15479 if (SemaRef.CurContext->isDependentContext())
15480 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15481 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
15482 PreInits: nullptr);
15483
15484 assert(LoopHelpers.size() == NumLoops &&
15485 "Expecting a single-dimensional loop iteration space");
15486 assert(OriginalInits.size() == NumLoops &&
15487 "Expecting a single-dimensional loop iteration space");
15488 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
15489
15490 if (FullClause) {
15491 if (!VerifyPositiveIntegerConstantInClause(
15492 Op: LoopHelper.NumIterations, CKind: OMPC_full, /*StrictlyPositive=*/false,
15493 /*SuppressExprDiags=*/true)
15494 .isUsable()) {
15495 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_unroll_full_variable_trip_count);
15496 Diag(Loc: FullClause->getBeginLoc(), DiagID: diag::note_omp_directive_here)
15497 << "#pragma omp unroll full";
15498 return StmtError();
15499 }
15500 }
15501
15502 // The generated loop may only be passed to other loop-associated directive
15503 // when a partial clause is specified. Without the requirement it is
15504 // sufficient to generate loop unroll metadata at code-generation.
15505 if (NumGeneratedTopLevelLoops == 0)
15506 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15507 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
15508 PreInits: nullptr);
15509
15510 // Otherwise, we need to provide a de-sugared/transformed AST that can be
15511 // associated with another loop directive.
15512 //
15513 // The canonical loop analysis return by checkTransformableLoopNest assumes
15514 // the following structure to be the same loop without transformations or
15515 // directives applied: \code OriginalInits; LoopHelper.PreInits;
15516 // LoopHelper.Counters;
15517 // for (; IV < LoopHelper.NumIterations; ++IV) {
15518 // LoopHelper.Updates;
15519 // Body;
15520 // }
15521 // \endcode
15522 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits
15523 // and referenced by LoopHelper.IterationVarRef.
15524 //
15525 // The unrolling directive transforms this into the following loop:
15526 // \code
15527 // OriginalInits; \
15528 // LoopHelper.PreInits; > NewPreInits
15529 // LoopHelper.Counters; /
15530 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) {
15531 // #pragma clang loop unroll_count(Factor)
15532 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV)
15533 // {
15534 // LoopHelper.Updates;
15535 // Body;
15536 // }
15537 // }
15538 // \endcode
15539 // where UIV is a new logical iteration counter. IV must be the same VarDecl
15540 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates
15541 // references it. If the partially unrolled loop is associated with another
15542 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to
15543 // analyze this loop, i.e. the outer loop must fulfill the constraints of an
15544 // OpenMP canonical loop. The inner loop is not an associable canonical loop
15545 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of
15546 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a
15547 // property of the OMPLoopBasedDirective instead of statements in
15548 // CompoundStatement. This is to allow the loop to become a non-outermost loop
15549 // of a canonical loop nest where these PreInits are emitted before the
15550 // outermost directive.
15551
15552 // Find the loop statement.
15553 Stmt *LoopStmt = nullptr;
15554 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
15555
15556 // Determine the PreInit declarations.
15557 SmallVector<Stmt *, 4> PreInits;
15558 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
15559
15560 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15561 QualType IVTy = IterationVarRef->getType();
15562 assert(LoopHelper.Counters.size() == 1 &&
15563 "Expecting a single-dimensional loop iteration space");
15564 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15565
15566 // Determine the unroll factor.
15567 uint64_t Factor;
15568 SourceLocation FactorLoc;
15569 if (Expr *FactorVal = PartialClause->getFactor();
15570 FactorVal && !FactorVal->containsErrors()) {
15571 Factor = FactorVal->getIntegerConstantExpr(Ctx: Context)->getZExtValue();
15572 FactorLoc = FactorVal->getExprLoc();
15573 } else {
15574 // TODO: Use a better profitability model.
15575 Factor = 2;
15576 }
15577 assert(Factor > 0 && "Expected positive unroll factor");
15578 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() {
15579 return IntegerLiteral::Create(
15580 C: getASTContext(), V: llvm::APInt(getASTContext().getIntWidth(T: IVTy), Factor),
15581 type: IVTy, l: FactorLoc);
15582 };
15583
15584 // Iteration variable SourceLocations.
15585 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
15586 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
15587 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
15588
15589 // Internal variable names.
15590 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
15591 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str();
15592 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str();
15593
15594 // Create the iteration variable for the unrolled loop.
15595 VarDecl *OuterIVDecl =
15596 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: OuterIVName, Attrs: nullptr, OrigRef: OrigVar);
15597 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() {
15598 return buildDeclRefExpr(S&: SemaRef, D: OuterIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15599 };
15600
15601 // Iteration variable for the inner loop: Reuse the iteration variable created
15602 // by checkOpenMPLoop.
15603 auto *InnerIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
15604 InnerIVDecl->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name: InnerIVName));
15605 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() {
15606 return buildDeclRefExpr(S&: SemaRef, D: InnerIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15607 };
15608
15609 // Make a copy of the NumIterations expression for each use: By the AST
15610 // constraints, every expression object in a DeclContext must be unique.
15611 CaptureVars CopyTransformer(SemaRef);
15612 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
15613 return AssertSuccess(
15614 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
15615 };
15616
15617 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv
15618 ExprResult LValueConv = SemaRef.DefaultLvalueConversion(E: MakeOuterRef());
15619 SemaRef.AddInitializerToDecl(dcl: InnerIVDecl, init: LValueConv.get(),
15620 /*DirectInit=*/false);
15621 StmtResult InnerInit = new (Context)
15622 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15623 if (!InnerInit.isUsable())
15624 return StmtError();
15625
15626 // Inner For cond-expression:
15627 // \code
15628 // .unroll_inner.iv < .unrolled.iv + Factor &&
15629 // .unroll_inner.iv < NumIterations
15630 // \endcode
15631 // This conjunction of two conditions allows ScalarEvolution to derive the
15632 // maximum trip count of the inner loop.
15633 ExprResult EndOfTile =
15634 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15635 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
15636 if (!EndOfTile.isUsable())
15637 return StmtError();
15638 ExprResult InnerCond1 =
15639 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15640 LHSExpr: MakeInnerRef(), RHSExpr: EndOfTile.get());
15641 if (!InnerCond1.isUsable())
15642 return StmtError();
15643 ExprResult InnerCond2 =
15644 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15645 LHSExpr: MakeInnerRef(), RHSExpr: MakeNumIterations());
15646 if (!InnerCond2.isUsable())
15647 return StmtError();
15648 ExprResult InnerCond =
15649 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LAnd,
15650 LHSExpr: InnerCond1.get(), RHSExpr: InnerCond2.get());
15651 if (!InnerCond.isUsable())
15652 return StmtError();
15653
15654 // Inner For incr-statement: ++.unroll_inner.iv
15655 ExprResult InnerIncr = SemaRef.BuildUnaryOp(
15656 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeInnerRef());
15657 if (!InnerIncr.isUsable())
15658 return StmtError();
15659
15660 // Inner For statement.
15661 SmallVector<Stmt *> InnerBodyStmts;
15662 InnerBodyStmts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15663 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15664 InnerBodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
15665 InnerBodyStmts.push_back(Elt: Body);
15666 CompoundStmt *InnerBody =
15667 CompoundStmt::Create(C: getASTContext(), Stmts: InnerBodyStmts, FPFeatures: FPOptionsOverride(),
15668 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
15669 ForStmt *InnerFor = new (Context)
15670 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr,
15671 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(),
15672 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15673
15674 // Unroll metadata for the inner loop.
15675 // This needs to take into account the remainder portion of the unrolled loop,
15676 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass
15677 // supports multiple loop exits. Instead, unroll using a factor equivalent to
15678 // the maximum trip count, which will also generate a remainder loop. Just
15679 // `unroll(enable)` (which could have been useful if the user has not
15680 // specified a concrete factor; even though the outer loop cannot be
15681 // influenced anymore, would avoid more code bloat than necessary) will refuse
15682 // the loop because "Won't unroll; remainder loop could not be generated when
15683 // assuming runtime trip count". Even if it did work, it must not choose a
15684 // larger unroll factor than the maximum loop length, or it would always just
15685 // execute the remainder loop.
15686 LoopHintAttr *UnrollHintAttr =
15687 LoopHintAttr::CreateImplicit(Ctx&: Context, Option: LoopHintAttr::UnrollCount,
15688 State: LoopHintAttr::Numeric, Value: MakeFactorExpr());
15689 AttributedStmt *InnerUnrolled = AttributedStmt::Create(
15690 C: getASTContext(), Loc: StartLoc, Attrs: {UnrollHintAttr}, SubStmt: InnerFor);
15691
15692 // Outer For init-statement: auto .unrolled.iv = 0
15693 SemaRef.AddInitializerToDecl(
15694 dcl: OuterIVDecl,
15695 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15696 /*DirectInit=*/false);
15697 StmtResult OuterInit = new (Context)
15698 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15699 if (!OuterInit.isUsable())
15700 return StmtError();
15701
15702 // Outer For cond-expression: .unrolled.iv < NumIterations
15703 ExprResult OuterConde =
15704 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15705 LHSExpr: MakeOuterRef(), RHSExpr: MakeNumIterations());
15706 if (!OuterConde.isUsable())
15707 return StmtError();
15708
15709 // Outer For incr-statement: .unrolled.iv += Factor
15710 ExprResult OuterIncr =
15711 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15712 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
15713 if (!OuterIncr.isUsable())
15714 return StmtError();
15715
15716 // Outer For statement.
15717 ForStmt *OuterFor = new (Context)
15718 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr,
15719 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(),
15720 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15721
15722 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15723 NumGeneratedTopLevelLoops, TransformedStmt: OuterFor,
15724 PreInits: buildPreInits(Context, PreInits));
15725}
15726
15727StmtResult SemaOpenMP::ActOnOpenMPReverseDirective(Stmt *AStmt,
15728 SourceLocation StartLoc,
15729 SourceLocation EndLoc) {
15730 ASTContext &Context = getASTContext();
15731 Scope *CurScope = SemaRef.getCurScope();
15732
15733 // Empty statement should only be possible if there already was an error.
15734 if (!AStmt)
15735 return StmtError();
15736
15737 constexpr unsigned NumLoops = 1;
15738 Stmt *Body = nullptr;
15739 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
15740 NumLoops);
15741 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
15742 if (!checkTransformableLoopNest(Kind: OMPD_reverse, AStmt, NumLoops, LoopHelpers,
15743 Body, OriginalInits))
15744 return StmtError();
15745
15746 // Delay applying the transformation to when template is completely
15747 // instantiated.
15748 if (SemaRef.CurContext->isDependentContext())
15749 return OMPReverseDirective::Create(C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt,
15750 NumLoops, TransformedStmt: nullptr, PreInits: nullptr);
15751
15752 assert(LoopHelpers.size() == NumLoops &&
15753 "Expecting a single-dimensional loop iteration space");
15754 assert(OriginalInits.size() == NumLoops &&
15755 "Expecting a single-dimensional loop iteration space");
15756 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
15757
15758 // Find the loop statement.
15759 Stmt *LoopStmt = nullptr;
15760 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
15761
15762 // Determine the PreInit declarations.
15763 SmallVector<Stmt *> PreInits;
15764 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
15765
15766 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15767 QualType IVTy = IterationVarRef->getType();
15768 uint64_t IVWidth = Context.getTypeSize(T: IVTy);
15769 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15770
15771 // Iteration variable SourceLocations.
15772 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
15773 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
15774 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
15775
15776 // Locations pointing to the transformation.
15777 SourceLocation TransformLoc = StartLoc;
15778 SourceLocation TransformLocBegin = StartLoc;
15779 SourceLocation TransformLocEnd = EndLoc;
15780
15781 // Internal variable names.
15782 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
15783 SmallString<64> ForwardIVName(".forward.iv.");
15784 ForwardIVName += OrigVarName;
15785 SmallString<64> ReversedIVName(".reversed.iv.");
15786 ReversedIVName += OrigVarName;
15787
15788 // LoopHelper.Updates will read the logical iteration number from
15789 // LoopHelper.IterationVarRef, compute the value of the user loop counter of
15790 // that logical iteration from it, then assign it to the user loop counter
15791 // variable. We cannot directly use LoopHelper.IterationVarRef as the
15792 // induction variable of the generated loop because it may cause an underflow:
15793 // \code{.c}
15794 // for (unsigned i = 0; i < n; ++i)
15795 // body(i);
15796 // \endcode
15797 //
15798 // Naive reversal:
15799 // \code{.c}
15800 // for (unsigned i = n-1; i >= 0; --i)
15801 // body(i);
15802 // \endcode
15803 //
15804 // Instead, we introduce a new iteration variable representing the logical
15805 // iteration counter of the original loop, convert it to the logical iteration
15806 // number of the reversed loop, then let LoopHelper.Updates compute the user's
15807 // loop iteration variable from it.
15808 // \code{.cpp}
15809 // for (auto .forward.iv = 0; .forward.iv < n; ++.forward.iv) {
15810 // auto .reversed.iv = n - .forward.iv - 1;
15811 // i = (.reversed.iv + 0) * 1; // LoopHelper.Updates
15812 // body(i); // Body
15813 // }
15814 // \endcode
15815
15816 // Subexpressions with more than one use. One of the constraints of an AST is
15817 // that every node object must appear at most once, hence we define a lambda
15818 // that creates a new AST node at every use.
15819 CaptureVars CopyTransformer(SemaRef);
15820 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
15821 return AssertSuccess(
15822 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
15823 };
15824
15825 // Create the iteration variable for the forward loop (from 0 to n-1).
15826 VarDecl *ForwardIVDecl =
15827 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: ForwardIVName, Attrs: nullptr, OrigRef: OrigVar);
15828 auto MakeForwardRef = [&SemaRef = this->SemaRef, ForwardIVDecl, IVTy,
15829 OrigVarLoc]() {
15830 return buildDeclRefExpr(S&: SemaRef, D: ForwardIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15831 };
15832
15833 // Iteration variable for the reversed induction variable (from n-1 downto 0):
15834 // Reuse the iteration variable created by checkOpenMPLoop.
15835 auto *ReversedIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
15836 ReversedIVDecl->setDeclName(
15837 &SemaRef.PP.getIdentifierTable().get(Name: ReversedIVName));
15838
15839 // For init-statement:
15840 // \code{.cpp}
15841 // auto .forward.iv = 0;
15842 // \endcode
15843 auto *Zero = IntegerLiteral::Create(C: Context, V: llvm::APInt::getZero(numBits: IVWidth),
15844 type: ForwardIVDecl->getType(), l: OrigVarLoc);
15845 SemaRef.AddInitializerToDecl(dcl: ForwardIVDecl, init: Zero, /*DirectInit=*/false);
15846 StmtResult Init = new (Context)
15847 DeclStmt(DeclGroupRef(ForwardIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15848 if (!Init.isUsable())
15849 return StmtError();
15850
15851 // Forward iv cond-expression:
15852 // \code{.cpp}
15853 // .forward.iv < MakeNumIterations()
15854 // \endcode
15855 ExprResult Cond =
15856 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15857 LHSExpr: MakeForwardRef(), RHSExpr: MakeNumIterations());
15858 if (!Cond.isUsable())
15859 return StmtError();
15860
15861 // Forward incr-statement:
15862 // \code{.c}
15863 // ++.forward.iv
15864 // \endcode
15865 ExprResult Incr = SemaRef.BuildUnaryOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(),
15866 Opc: UO_PreInc, Input: MakeForwardRef());
15867 if (!Incr.isUsable())
15868 return StmtError();
15869
15870 // Reverse the forward-iv:
15871 // \code{.cpp}
15872 // auto .reversed.iv = MakeNumIterations() - 1 - .forward.iv
15873 // \endcode
15874 auto *One = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVWidth, 1), type: IVTy,
15875 l: TransformLoc);
15876 ExprResult Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub,
15877 LHSExpr: MakeNumIterations(), RHSExpr: One);
15878 if (!Minus.isUsable())
15879 return StmtError();
15880 Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub, LHSExpr: Minus.get(),
15881 RHSExpr: MakeForwardRef());
15882 if (!Minus.isUsable())
15883 return StmtError();
15884 StmtResult InitReversed = new (Context) DeclStmt(
15885 DeclGroupRef(ReversedIVDecl), TransformLocBegin, TransformLocEnd);
15886 if (!InitReversed.isUsable())
15887 return StmtError();
15888 SemaRef.AddInitializerToDecl(dcl: ReversedIVDecl, init: Minus.get(),
15889 /*DirectInit=*/false);
15890
15891 // The new loop body.
15892 SmallVector<Stmt *, 4> BodyStmts;
15893 BodyStmts.reserve(N: LoopHelper.Updates.size() + 2 +
15894 (isa<CXXForRangeStmt>(Val: LoopStmt) ? 1 : 0));
15895 BodyStmts.push_back(Elt: InitReversed.get());
15896 llvm::append_range(C&: BodyStmts, R&: LoopHelper.Updates);
15897 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15898 BodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
15899 BodyStmts.push_back(Elt: Body);
15900 auto *ReversedBody =
15901 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
15902 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
15903
15904 // Finally create the reversed For-statement.
15905 auto *ReversedFor = new (Context)
15906 ForStmt(Context, Init.get(), Cond.get(), nullptr, Incr.get(),
15907 ReversedBody, LoopHelper.Init->getBeginLoc(),
15908 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15909 return OMPReverseDirective::Create(C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt, NumLoops,
15910 TransformedStmt: ReversedFor,
15911 PreInits: buildPreInits(Context, PreInits));
15912}
15913
15914StmtResult SemaOpenMP::ActOnOpenMPInterchangeDirective(
15915 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15916 SourceLocation EndLoc) {
15917 ASTContext &Context = getASTContext();
15918 DeclContext *CurContext = SemaRef.CurContext;
15919 Scope *CurScope = SemaRef.getCurScope();
15920
15921 // Empty statement should only be possible if there already was an error.
15922 if (!AStmt)
15923 return StmtError();
15924
15925 // interchange without permutation clause swaps two loops.
15926 const OMPPermutationClause *PermutationClause =
15927 OMPExecutableDirective::getSingleClause<OMPPermutationClause>(Clauses);
15928 size_t NumLoops = PermutationClause ? PermutationClause->getNumLoops() : 2;
15929
15930 // Verify and diagnose loop nest.
15931 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
15932 Stmt *Body = nullptr;
15933 SmallVector<SmallVector<Stmt *>, 2> OriginalInits;
15934 if (!checkTransformableLoopNest(Kind: OMPD_interchange, AStmt, NumLoops,
15935 LoopHelpers, Body, OriginalInits))
15936 return StmtError();
15937
15938 // Delay interchange to when template is completely instantiated.
15939 if (CurContext->isDependentContext())
15940 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15941 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
15942
15943 // An invalid expression in the permutation clause is set to nullptr in
15944 // ActOnOpenMPPermutationClause.
15945 if (PermutationClause &&
15946 llvm::is_contained(Range: PermutationClause->getArgsRefs(), Element: nullptr))
15947 return StmtError();
15948
15949 assert(LoopHelpers.size() == NumLoops &&
15950 "Expecting loop iteration space dimensionaly to match number of "
15951 "affected loops");
15952 assert(OriginalInits.size() == NumLoops &&
15953 "Expecting loop iteration space dimensionaly to match number of "
15954 "affected loops");
15955
15956 // Decode the permutation clause.
15957 SmallVector<uint64_t, 2> Permutation;
15958 if (!PermutationClause) {
15959 Permutation = {1, 0};
15960 } else {
15961 ArrayRef<Expr *> PermArgs = PermutationClause->getArgsRefs();
15962 llvm::BitVector Flags(PermArgs.size());
15963 for (Expr *PermArg : PermArgs) {
15964 std::optional<llvm::APSInt> PermCstExpr =
15965 PermArg->getIntegerConstantExpr(Ctx: Context);
15966 if (!PermCstExpr)
15967 continue;
15968 uint64_t PermInt = PermCstExpr->getZExtValue();
15969 assert(1 <= PermInt && PermInt <= NumLoops &&
15970 "Must be a permutation; diagnostic emitted in "
15971 "ActOnOpenMPPermutationClause");
15972 if (Flags[PermInt - 1]) {
15973 SourceRange ExprRange(PermArg->getBeginLoc(), PermArg->getEndLoc());
15974 Diag(Loc: PermArg->getExprLoc(),
15975 DiagID: diag::err_omp_interchange_permutation_value_repeated)
15976 << PermInt << ExprRange;
15977 continue;
15978 }
15979 Flags[PermInt - 1] = true;
15980
15981 Permutation.push_back(Elt: PermInt - 1);
15982 }
15983
15984 if (Permutation.size() != NumLoops)
15985 return StmtError();
15986 }
15987
15988 // Nothing to transform with trivial permutation.
15989 if (NumLoops <= 1 || llvm::all_of(Range: llvm::enumerate(First&: Permutation), P: [](auto P) {
15990 auto [Idx, Arg] = P;
15991 return Idx == Arg;
15992 }))
15993 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15994 NumLoops, AssociatedStmt: AStmt, TransformedStmt: AStmt, PreInits: nullptr);
15995
15996 // Find the affected loops.
15997 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
15998 collectLoopStmts(AStmt, LoopStmts);
15999
16000 // Collect pre-init statements on the order before the permuation.
16001 SmallVector<Stmt *> PreInits;
16002 for (auto I : llvm::seq<int>(Size: NumLoops)) {
16003 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
16004
16005 assert(LoopHelper.Counters.size() == 1 &&
16006 "Single-dimensional loop iteration space expected");
16007
16008 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
16009 PreInits);
16010 }
16011
16012 SmallVector<VarDecl *> PermutedIndVars(NumLoops);
16013 CaptureVars CopyTransformer(SemaRef);
16014
16015 // Create the permuted loops from the inside to the outside of the
16016 // interchanged loop nest. Body of the innermost new loop is the original
16017 // innermost body.
16018 Stmt *Inner = Body;
16019 for (auto TargetIdx : llvm::reverse(C: llvm::seq<int>(Size: NumLoops))) {
16020 // Get the original loop that belongs to this new position.
16021 uint64_t SourceIdx = Permutation[TargetIdx];
16022 OMPLoopBasedDirective::HelperExprs &SourceHelper = LoopHelpers[SourceIdx];
16023 Stmt *SourceLoopStmt = LoopStmts[SourceIdx];
16024 assert(SourceHelper.Counters.size() == 1 &&
16025 "Single-dimensional loop iteration space expected");
16026 auto *OrigCntVar = cast<DeclRefExpr>(Val: SourceHelper.Counters.front());
16027
16028 // Normalized loop counter variable: From 0 to n-1, always an integer type.
16029 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: SourceHelper.IterationVarRef);
16030 QualType IVTy = IterVarRef->getType();
16031 assert(IVTy->isIntegerType() &&
16032 "Expected the logical iteration counter to be an integer");
16033
16034 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
16035 SourceLocation OrigVarLoc = IterVarRef->getExprLoc();
16036
16037 // Make a copy of the NumIterations expression for each use: By the AST
16038 // constraints, every expression object in a DeclContext must be unique.
16039 auto MakeNumIterations = [&CopyTransformer, &SourceHelper]() -> Expr * {
16040 return AssertSuccess(
16041 R: CopyTransformer.TransformExpr(E: SourceHelper.NumIterations));
16042 };
16043
16044 // Iteration variable for the permuted loop. Reuse the one from
16045 // checkOpenMPLoop which will also be used to update the original loop
16046 // variable.
16047 SmallString<64> PermutedCntName(".permuted_");
16048 PermutedCntName.append(Refs: {llvm::utostr(X: TargetIdx), ".iv.", OrigVarName});
16049 auto *PermutedCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
16050 PermutedCntDecl->setDeclName(
16051 &SemaRef.PP.getIdentifierTable().get(Name: PermutedCntName));
16052 PermutedIndVars[TargetIdx] = PermutedCntDecl;
16053 auto MakePermutedRef = [this, PermutedCntDecl, IVTy, OrigVarLoc]() {
16054 return buildDeclRefExpr(S&: SemaRef, D: PermutedCntDecl, Ty: IVTy, Loc: OrigVarLoc);
16055 };
16056
16057 // For init-statement:
16058 // \code
16059 // auto .permuted_{target}.iv = 0
16060 // \endcode
16061 ExprResult Zero = SemaRef.ActOnIntegerConstant(Loc: OrigVarLoc, Val: 0);
16062 if (!Zero.isUsable())
16063 return StmtError();
16064 SemaRef.AddInitializerToDecl(dcl: PermutedCntDecl, init: Zero.get(),
16065 /*DirectInit=*/false);
16066 StmtResult InitStmt = new (Context)
16067 DeclStmt(DeclGroupRef(PermutedCntDecl), OrigCntVar->getBeginLoc(),
16068 OrigCntVar->getEndLoc());
16069 if (!InitStmt.isUsable())
16070 return StmtError();
16071
16072 // For cond-expression:
16073 // \code
16074 // .permuted_{target}.iv < MakeNumIterations()
16075 // \endcode
16076 ExprResult CondExpr =
16077 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceHelper.Cond->getExprLoc(), Opc: BO_LT,
16078 LHSExpr: MakePermutedRef(), RHSExpr: MakeNumIterations());
16079 if (!CondExpr.isUsable())
16080 return StmtError();
16081
16082 // For incr-statement:
16083 // \code
16084 // ++.tile.iv
16085 // \endcode
16086 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
16087 S: CurScope, OpLoc: SourceHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakePermutedRef());
16088 if (!IncrStmt.isUsable())
16089 return StmtError();
16090
16091 SmallVector<Stmt *, 4> BodyParts(SourceHelper.Updates.begin(),
16092 SourceHelper.Updates.end());
16093 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: SourceLoopStmt))
16094 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16095 BodyParts.push_back(Elt: Inner);
16096 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
16097 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
16098 Inner = new (Context) ForStmt(
16099 Context, InitStmt.get(), CondExpr.get(), nullptr, IncrStmt.get(), Inner,
16100 SourceHelper.Init->getBeginLoc(), SourceHelper.Init->getBeginLoc(),
16101 SourceHelper.Inc->getEndLoc());
16102 }
16103
16104 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16105 NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
16106 PreInits: buildPreInits(Context, PreInits));
16107}
16108
16109StmtResult SemaOpenMP::ActOnOpenMPFuseDirective(ArrayRef<OMPClause *> Clauses,
16110 Stmt *AStmt,
16111 SourceLocation StartLoc,
16112 SourceLocation EndLoc) {
16113
16114 ASTContext &Context = getASTContext();
16115 DeclContext *CurrContext = SemaRef.CurContext;
16116 Scope *CurScope = SemaRef.getCurScope();
16117 CaptureVars CopyTransformer(SemaRef);
16118
16119 // Ensure the structured block is not empty
16120 if (!AStmt)
16121 return StmtError();
16122
16123 // Defer transformation in dependent contexts
16124 // The NumLoopNests argument is set to a placeholder 1 (even though
16125 // using looprange fuse could yield up to 3 top level loop nests)
16126 // because a dependent context could prevent determining its true value
16127 if (CurrContext->isDependentContext())
16128 return OMPFuseDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16129 /* NumLoops */ NumGeneratedTopLevelLoops: 1, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
16130
16131 // Validate that the potential loop sequence is transformable for fusion
16132 // Also collect the HelperExprs, Loop Stmts, Inits, and Number of loops
16133 LoopSequenceAnalysis SeqAnalysis;
16134 if (!checkTransformableLoopSequence(Kind: OMPD_fuse, AStmt, SeqAnalysis, Context))
16135 return StmtError();
16136
16137 // SeqAnalysis.LoopSeqSize exists mostly to handle dependent contexts,
16138 // otherwise it must be the same as SeqAnalysis.Loops.size().
16139 assert(SeqAnalysis.LoopSeqSize == SeqAnalysis.Loops.size() &&
16140 "Inconsistent size of the loop sequence and the number of loops "
16141 "found in the sequence");
16142
16143 // Handle clauses, which can be any of the following: [looprange, apply]
16144 const auto *LRC =
16145 OMPExecutableDirective::getSingleClause<OMPLoopRangeClause>(Clauses);
16146
16147 // The clause arguments are invalidated if any error arises
16148 // such as non-constant or non-positive arguments
16149 if (LRC && (!LRC->getFirst() || !LRC->getCount()))
16150 return StmtError();
16151
16152 // Delayed semantic check of LoopRange constraint
16153 // Evaluates the loop range arguments and returns the first and count values
16154 auto EvaluateLoopRangeArguments = [&Context](Expr *First, Expr *Count,
16155 uint64_t &FirstVal,
16156 uint64_t &CountVal) {
16157 llvm::APSInt FirstInt = First->EvaluateKnownConstInt(Ctx: Context);
16158 llvm::APSInt CountInt = Count->EvaluateKnownConstInt(Ctx: Context);
16159 FirstVal = FirstInt.getZExtValue();
16160 CountVal = CountInt.getZExtValue();
16161 };
16162
16163 // OpenMP [6.0, Restrictions]
16164 // first + count - 1 must not evaluate to a value greater than the
16165 // loop sequence length of the associated canonical loop sequence.
16166 auto ValidLoopRange = [](uint64_t FirstVal, uint64_t CountVal,
16167 unsigned NumLoops) -> bool {
16168 return FirstVal + CountVal - 1 <= NumLoops;
16169 };
16170 uint64_t FirstVal = 1, CountVal = 0, LastVal = SeqAnalysis.LoopSeqSize;
16171
16172 // Validates the loop range after evaluating the semantic information
16173 // and ensures that the range is valid for the given loop sequence size.
16174 // Expressions are evaluated at compile time to obtain constant values.
16175 if (LRC) {
16176 EvaluateLoopRangeArguments(LRC->getFirst(), LRC->getCount(), FirstVal,
16177 CountVal);
16178 if (CountVal == 1)
16179 SemaRef.Diag(Loc: LRC->getCountLoc(), DiagID: diag::warn_omp_redundant_fusion)
16180 << getOpenMPDirectiveName(D: OMPD_fuse);
16181
16182 if (!ValidLoopRange(FirstVal, CountVal, SeqAnalysis.LoopSeqSize)) {
16183 SemaRef.Diag(Loc: LRC->getFirstLoc(), DiagID: diag::err_omp_invalid_looprange)
16184 << getOpenMPDirectiveName(D: OMPD_fuse) << FirstVal
16185 << (FirstVal + CountVal - 1) << SeqAnalysis.LoopSeqSize;
16186 return StmtError();
16187 }
16188
16189 LastVal = FirstVal + CountVal - 1;
16190 }
16191
16192 // Complete fusion generates a single canonical loop nest
16193 // However looprange clause may generate several loop nests
16194 unsigned NumGeneratedTopLevelLoops =
16195 LRC ? SeqAnalysis.LoopSeqSize - CountVal + 1 : 1;
16196
16197 // Emit a warning for redundant loop fusion when the sequence contains only
16198 // one loop.
16199 if (SeqAnalysis.LoopSeqSize == 1)
16200 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::warn_omp_redundant_fusion)
16201 << getOpenMPDirectiveName(D: OMPD_fuse);
16202
16203 // Select the type with the largest bit width among all induction variables
16204 QualType IVType =
16205 SeqAnalysis.Loops[FirstVal - 1].HelperExprs.IterationVarRef->getType();
16206 for (unsigned I : llvm::seq<unsigned>(Begin: FirstVal, End: LastVal)) {
16207 QualType CurrentIVType =
16208 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef->getType();
16209 if (Context.getTypeSize(T: CurrentIVType) > Context.getTypeSize(T: IVType)) {
16210 IVType = CurrentIVType;
16211 }
16212 }
16213 uint64_t IVBitWidth = Context.getIntWidth(T: IVType);
16214
16215 // Create pre-init declarations for all loops lower bounds, upper bounds,
16216 // strides and num-iterations for every top level loop in the fusion
16217 SmallVector<VarDecl *, 4> LBVarDecls;
16218 SmallVector<VarDecl *, 4> STVarDecls;
16219 SmallVector<VarDecl *, 4> NIVarDecls;
16220 SmallVector<VarDecl *, 4> UBVarDecls;
16221 SmallVector<VarDecl *, 4> IVVarDecls;
16222
16223 // Helper lambda to create variables for bounds, strides, and other
16224 // expressions. Generates both the variable declaration and the corresponding
16225 // initialization statement.
16226 auto CreateHelperVarAndStmt =
16227 [&, &SemaRef = SemaRef](Expr *ExprToCopy, const std::string &BaseName,
16228 unsigned I, bool NeedsNewVD = false) {
16229 Expr *TransformedExpr =
16230 AssertSuccess(R: CopyTransformer.TransformExpr(E: ExprToCopy));
16231 if (!TransformedExpr)
16232 return std::pair<VarDecl *, StmtResult>(nullptr, StmtError());
16233
16234 auto Name = (Twine(".omp.") + BaseName + std::to_string(val: I)).str();
16235
16236 VarDecl *VD;
16237 if (NeedsNewVD) {
16238 VD = buildVarDecl(SemaRef, Loc: SourceLocation(), Type: IVType, Name);
16239 SemaRef.AddInitializerToDecl(dcl: VD, init: TransformedExpr, DirectInit: false);
16240 } else {
16241 // Create a unique variable name
16242 DeclRefExpr *DRE = cast<DeclRefExpr>(Val: TransformedExpr);
16243 VD = cast<VarDecl>(Val: DRE->getDecl());
16244 VD->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name));
16245 }
16246 // Create the corresponding declaration statement
16247 StmtResult DeclStmt = new (Context) class DeclStmt(
16248 DeclGroupRef(VD), SourceLocation(), SourceLocation());
16249 return std::make_pair(x&: VD, y&: DeclStmt);
16250 };
16251
16252 // PreInits hold a sequence of variable declarations that must be executed
16253 // before the fused loop begins. These include bounds, strides, and other
16254 // helper variables required for the transformation. Other loop transforms
16255 // also contain their own preinits
16256 SmallVector<Stmt *> PreInits;
16257
16258 // Update the general preinits using the preinits generated by loop sequence
16259 // generating loop transformations. These preinits differ slightly from
16260 // single-loop transformation preinits, as they can be detached from a
16261 // specific loop inside multiple generated loop nests. This happens
16262 // because certain helper variables, like '.omp.fuse.max', are introduced to
16263 // handle fused iteration spaces and may not be directly tied to a single
16264 // original loop. The preinit structure must ensure that hidden variables
16265 // like '.omp.fuse.max' are still properly handled.
16266 // Transformations that apply this concept: Loopranged Fuse, Split
16267 llvm::append_range(C&: PreInits, R&: SeqAnalysis.LoopSequencePreInits);
16268
16269 // Process each single loop to generate and collect declarations
16270 // and statements for all helper expressions related to
16271 // particular single loop nests
16272
16273 // Also In the case of the fused loops, we keep track of their original
16274 // inits by appending them to their preinits statement, and in the case of
16275 // transformations, also append their preinits (which contain the original
16276 // loop initialization statement or other statements)
16277
16278 // Firstly we need to set TransformIndex to match the begining of the
16279 // looprange section
16280 unsigned int TransformIndex = 0;
16281 for (unsigned I : llvm::seq<unsigned>(Size: FirstVal - 1)) {
16282 if (SeqAnalysis.Loops[I].isLoopTransformation())
16283 ++TransformIndex;
16284 }
16285
16286 for (unsigned int I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16287 if (SeqAnalysis.Loops[I].isRegularLoop()) {
16288 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
16289 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
16290 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
16291 } else if (SeqAnalysis.Loops[I].isLoopTransformation()) {
16292 // For transformed loops, insert both pre-inits and original inits.
16293 // Order matters: pre-inits may define variables used in the original
16294 // inits such as upper bounds...
16295 SmallVector<Stmt *> &TransformPreInit =
16296 SeqAnalysis.Loops[TransformIndex++].TransformsPreInits;
16297 llvm::append_range(C&: PreInits, R&: TransformPreInit);
16298
16299 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
16300 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
16301 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
16302 }
16303 auto [UBVD, UBDStmt] =
16304 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.UB, "ub", J);
16305 auto [LBVD, LBDStmt] =
16306 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.LB, "lb", J);
16307 auto [STVD, STDStmt] =
16308 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.ST, "st", J);
16309 auto [NIVD, NIDStmt] = CreateHelperVarAndStmt(
16310 SeqAnalysis.Loops[I].HelperExprs.NumIterations, "ni", J, true);
16311 auto [IVVD, IVDStmt] = CreateHelperVarAndStmt(
16312 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef, "iv", J);
16313
16314 assert(LBVD && STVD && NIVD && IVVD &&
16315 "OpenMP Fuse Helper variables creation failed");
16316
16317 UBVarDecls.push_back(Elt: UBVD);
16318 LBVarDecls.push_back(Elt: LBVD);
16319 STVarDecls.push_back(Elt: STVD);
16320 NIVarDecls.push_back(Elt: NIVD);
16321 IVVarDecls.push_back(Elt: IVVD);
16322
16323 PreInits.push_back(Elt: LBDStmt.get());
16324 PreInits.push_back(Elt: STDStmt.get());
16325 PreInits.push_back(Elt: NIDStmt.get());
16326 PreInits.push_back(Elt: IVDStmt.get());
16327 }
16328
16329 auto MakeVarDeclRef = [&SemaRef = this->SemaRef](VarDecl *VD) {
16330 return buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType(), Loc: VD->getLocation(),
16331 RefersToCapture: false);
16332 };
16333
16334 // Following up the creation of the final fused loop will be performed
16335 // which has the following shape (considering the selected loops):
16336 //
16337 // for (fuse.index = 0; fuse.index < max(ni0, ni1..., nik); ++fuse.index) {
16338 // if (fuse.index < ni0){
16339 // iv0 = lb0 + st0 * fuse.index;
16340 // original.index0 = iv0
16341 // body(0);
16342 // }
16343 // if (fuse.index < ni1){
16344 // iv1 = lb1 + st1 * fuse.index;
16345 // original.index1 = iv1
16346 // body(1);
16347 // }
16348 //
16349 // ...
16350 //
16351 // if (fuse.index < nik){
16352 // ivk = lbk + stk * fuse.index;
16353 // original.indexk = ivk
16354 // body(k); Expr *InitVal = IntegerLiteral::Create(Context,
16355 // llvm::APInt(IVWidth, 0),
16356 // }
16357
16358 // 1. Create the initialized fuse index
16359 StringRef IndexName = ".omp.fuse.index";
16360 Expr *InitVal = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVBitWidth, 0),
16361 type: IVType, l: SourceLocation());
16362 VarDecl *IndexDecl =
16363 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: IndexName, Attrs: nullptr, OrigRef: nullptr);
16364 SemaRef.AddInitializerToDecl(dcl: IndexDecl, init: InitVal, DirectInit: false);
16365 StmtResult InitStmt = new (Context)
16366 DeclStmt(DeclGroupRef(IndexDecl), SourceLocation(), SourceLocation());
16367
16368 if (!InitStmt.isUsable())
16369 return StmtError();
16370
16371 auto MakeIVRef = [&SemaRef = this->SemaRef, IndexDecl, IVType,
16372 Loc = InitVal->getExprLoc()]() {
16373 return buildDeclRefExpr(S&: SemaRef, D: IndexDecl, Ty: IVType, Loc, RefersToCapture: false);
16374 };
16375
16376 // 2. Iteratively compute the max number of logical iterations Max(NI_1, NI_2,
16377 // ..., NI_k)
16378 //
16379 // This loop accumulates the maximum value across multiple expressions,
16380 // ensuring each step constructs a unique AST node for correctness. By using
16381 // intermediate temporary variables and conditional operators, we maintain
16382 // distinct nodes and avoid duplicating subtrees, For instance, max(a,b,c):
16383 // omp.temp0 = max(a, b)
16384 // omp.temp1 = max(omp.temp0, c)
16385 // omp.fuse.max = max(omp.temp1, omp.temp0)
16386
16387 ExprResult MaxExpr;
16388 // I is the range of loops in the sequence that we fuse.
16389 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16390 DeclRefExpr *NIRef = MakeVarDeclRef(NIVarDecls[J]);
16391 QualType NITy = NIRef->getType();
16392
16393 if (MaxExpr.isUnset()) {
16394 // Initialize MaxExpr with the first NI expression
16395 MaxExpr = NIRef;
16396 } else {
16397 // Create a new acummulator variable t_i = MaxExpr
16398 std::string TempName = (Twine(".omp.temp.") + Twine(J)).str();
16399 VarDecl *TempDecl =
16400 buildVarDecl(SemaRef, Loc: {}, Type: NITy, Name: TempName, Attrs: nullptr, OrigRef: nullptr);
16401 TempDecl->setInit(MaxExpr.get());
16402 DeclRefExpr *TempRef =
16403 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
16404 DeclRefExpr *TempRef2 =
16405 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
16406 // Add a DeclStmt to PreInits to ensure the variable is declared.
16407 StmtResult TempStmt = new (Context)
16408 DeclStmt(DeclGroupRef(TempDecl), SourceLocation(), SourceLocation());
16409
16410 if (!TempStmt.isUsable())
16411 return StmtError();
16412 PreInits.push_back(Elt: TempStmt.get());
16413
16414 // Build MaxExpr <-(MaxExpr > NIRef ? MaxExpr : NIRef)
16415 ExprResult Comparison =
16416 SemaRef.BuildBinOp(S: nullptr, OpLoc: SourceLocation(), Opc: BO_GT, LHSExpr: TempRef, RHSExpr: NIRef);
16417 // Handle any errors in Comparison creation
16418 if (!Comparison.isUsable())
16419 return StmtError();
16420
16421 DeclRefExpr *NIRef2 = MakeVarDeclRef(NIVarDecls[J]);
16422 // Update MaxExpr using a conditional expression to hold the max value
16423 MaxExpr = new (Context) ConditionalOperator(
16424 Comparison.get(), SourceLocation(), TempRef2, SourceLocation(),
16425 NIRef2->getExprStmt(), NITy, VK_LValue, OK_Ordinary);
16426
16427 if (!MaxExpr.isUsable())
16428 return StmtError();
16429 }
16430 }
16431 if (!MaxExpr.isUsable())
16432 return StmtError();
16433
16434 // 3. Declare the max variable
16435 const std::string MaxName = Twine(".omp.fuse.max").str();
16436 VarDecl *MaxDecl =
16437 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: MaxName, Attrs: nullptr, OrigRef: nullptr);
16438 MaxDecl->setInit(MaxExpr.get());
16439 DeclRefExpr *MaxRef = buildDeclRefExpr(S&: SemaRef, D: MaxDecl, Ty: IVType, Loc: {}, RefersToCapture: false);
16440 StmtResult MaxStmt = new (Context)
16441 DeclStmt(DeclGroupRef(MaxDecl), SourceLocation(), SourceLocation());
16442
16443 if (MaxStmt.isInvalid())
16444 return StmtError();
16445 PreInits.push_back(Elt: MaxStmt.get());
16446
16447 // 4. Create condition Expr: index < n_max
16448 ExprResult CondExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT,
16449 LHSExpr: MakeIVRef(), RHSExpr: MaxRef);
16450 if (!CondExpr.isUsable())
16451 return StmtError();
16452
16453 // 5. Increment Expr: ++index
16454 ExprResult IncrExpr =
16455 SemaRef.BuildUnaryOp(S: CurScope, OpLoc: SourceLocation(), Opc: UO_PreInc, Input: MakeIVRef());
16456 if (!IncrExpr.isUsable())
16457 return StmtError();
16458
16459 // 6. Build the Fused Loop Body
16460 // The final fused loop iterates over the maximum logical range. Inside the
16461 // loop, each original loop's index is calculated dynamically, and its body
16462 // is executed conditionally.
16463 //
16464 // Each sub-loop's body is guarded by a conditional statement to ensure
16465 // it executes only within its logical iteration range:
16466 //
16467 // if (fuse.index < ni_k){
16468 // iv_k = lb_k + st_k * fuse.index;
16469 // original.index = iv_k
16470 // body(k);
16471 // }
16472
16473 CompoundStmt *FusedBody = nullptr;
16474 SmallVector<Stmt *, 4> FusedBodyStmts;
16475 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16476 // Assingment of the original sub-loop index to compute the logical index
16477 // IV_k = LB_k + omp.fuse.index * ST_k
16478 ExprResult IdxExpr =
16479 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Mul,
16480 LHSExpr: MakeVarDeclRef(STVarDecls[J]), RHSExpr: MakeIVRef());
16481 if (!IdxExpr.isUsable())
16482 return StmtError();
16483 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Add,
16484 LHSExpr: MakeVarDeclRef(LBVarDecls[J]), RHSExpr: IdxExpr.get());
16485
16486 if (!IdxExpr.isUsable())
16487 return StmtError();
16488 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Assign,
16489 LHSExpr: MakeVarDeclRef(IVVarDecls[J]), RHSExpr: IdxExpr.get());
16490 if (!IdxExpr.isUsable())
16491 return StmtError();
16492
16493 // Update the original i_k = IV_k
16494 SmallVector<Stmt *, 4> BodyStmts;
16495 BodyStmts.push_back(Elt: IdxExpr.get());
16496 llvm::append_range(C&: BodyStmts, R&: SeqAnalysis.Loops[I].HelperExprs.Updates);
16497
16498 // If the loop is a CXXForRangeStmt then the iterator variable is needed
16499 if (auto *SourceCXXFor =
16500 dyn_cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
16501 BodyStmts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16502
16503 Stmt *Body =
16504 (isa<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
16505 ? cast<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody()
16506 : cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody();
16507 BodyStmts.push_back(Elt: Body);
16508
16509 CompoundStmt *CombinedBody =
16510 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
16511 LB: SourceLocation(), RB: SourceLocation());
16512 ExprResult Condition =
16513 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT, LHSExpr: MakeIVRef(),
16514 RHSExpr: MakeVarDeclRef(NIVarDecls[J]));
16515
16516 if (!Condition.isUsable())
16517 return StmtError();
16518
16519 IfStmt *IfStatement = IfStmt::Create(
16520 Ctx: Context, IL: SourceLocation(), Kind: IfStatementKind::Ordinary, Init: nullptr, Var: nullptr,
16521 Cond: Condition.get(), LPL: SourceLocation(), RPL: SourceLocation(), Then: CombinedBody,
16522 EL: SourceLocation(), Else: nullptr);
16523
16524 FusedBodyStmts.push_back(Elt: IfStatement);
16525 }
16526 FusedBody = CompoundStmt::Create(C: Context, Stmts: FusedBodyStmts, FPFeatures: FPOptionsOverride(),
16527 LB: SourceLocation(), RB: SourceLocation());
16528
16529 // 7. Construct the final fused loop
16530 ForStmt *FusedForStmt = new (Context)
16531 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, IncrExpr.get(),
16532 FusedBody, InitStmt.get()->getBeginLoc(), SourceLocation(),
16533 IncrExpr.get()->getEndLoc());
16534
16535 // In the case of looprange, the result of fuse won't simply
16536 // be a single loop (ForStmt), but rather a loop sequence
16537 // (CompoundStmt) of 3 parts: the pre-fusion loops, the fused loop
16538 // and the post-fusion loops, preserving its original order.
16539 //
16540 // Note: If looprange clause produces a single fused loop nest then
16541 // this compound statement wrapper is unnecessary (Therefore this
16542 // treatment is skipped)
16543
16544 Stmt *FusionStmt = FusedForStmt;
16545 if (LRC && CountVal != SeqAnalysis.LoopSeqSize) {
16546 SmallVector<Stmt *, 4> FinalLoops;
16547
16548 // Reset the transform index
16549 TransformIndex = 0;
16550
16551 // Collect all non-fused loops before and after the fused region.
16552 // Pre-fusion and post-fusion loops are inserted in order exploiting their
16553 // symmetry, along with their corresponding transformation pre-inits if
16554 // needed. The fused loop is added between the two regions.
16555 for (unsigned I : llvm::seq<unsigned>(Size: SeqAnalysis.LoopSeqSize)) {
16556 if (I >= FirstVal - 1 && I < FirstVal + CountVal - 1) {
16557 // Update the Transformation counter to skip already treated
16558 // loop transformations
16559 if (!SeqAnalysis.Loops[I].isLoopTransformation())
16560 ++TransformIndex;
16561 continue;
16562 }
16563
16564 // No need to handle:
16565 // Regular loops: they are kept intact as-is.
16566 // Loop-sequence-generating transformations: already handled earlier.
16567 // Only TransformSingleLoop requires inserting pre-inits here
16568 if (SeqAnalysis.Loops[I].isRegularLoop()) {
16569 const auto &TransformPreInit =
16570 SeqAnalysis.Loops[TransformIndex++].TransformsPreInits;
16571 if (!TransformPreInit.empty())
16572 llvm::append_range(C&: PreInits, R: TransformPreInit);
16573 }
16574
16575 FinalLoops.push_back(Elt: SeqAnalysis.Loops[I].TheForStmt);
16576 }
16577
16578 FinalLoops.insert(I: FinalLoops.begin() + (FirstVal - 1), Elt: FusedForStmt);
16579 FusionStmt = CompoundStmt::Create(C: Context, Stmts: FinalLoops, FPFeatures: FPOptionsOverride(),
16580 LB: SourceLocation(), RB: SourceLocation());
16581 }
16582 return OMPFuseDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16583 NumGeneratedTopLevelLoops, AssociatedStmt: AStmt, TransformedStmt: FusionStmt,
16584 PreInits: buildPreInits(Context, PreInits));
16585}
16586
16587OMPClause *SemaOpenMP::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
16588 Expr *Expr,
16589 SourceLocation StartLoc,
16590 SourceLocation LParenLoc,
16591 SourceLocation EndLoc) {
16592 OMPClause *Res = nullptr;
16593 switch (Kind) {
16594 case OMPC_final:
16595 Res = ActOnOpenMPFinalClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16596 break;
16597 case OMPC_safelen:
16598 Res = ActOnOpenMPSafelenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
16599 break;
16600 case OMPC_simdlen:
16601 Res = ActOnOpenMPSimdlenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
16602 break;
16603 case OMPC_allocator:
16604 Res = ActOnOpenMPAllocatorClause(Allocator: Expr, StartLoc, LParenLoc, EndLoc);
16605 break;
16606 case OMPC_collapse:
16607 Res = ActOnOpenMPCollapseClause(NumForLoops: Expr, StartLoc, LParenLoc, EndLoc);
16608 break;
16609 case OMPC_ordered:
16610 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, NumForLoops: Expr);
16611 break;
16612 case OMPC_nowait:
16613 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc, LParenLoc, Condition: Expr);
16614 break;
16615 case OMPC_priority:
16616 Res = ActOnOpenMPPriorityClause(Priority: Expr, StartLoc, LParenLoc, EndLoc);
16617 break;
16618 case OMPC_hint:
16619 Res = ActOnOpenMPHintClause(Hint: Expr, StartLoc, LParenLoc, EndLoc);
16620 break;
16621 case OMPC_depobj:
16622 Res = ActOnOpenMPDepobjClause(Depobj: Expr, StartLoc, LParenLoc, EndLoc);
16623 break;
16624 case OMPC_detach:
16625 Res = ActOnOpenMPDetachClause(Evt: Expr, StartLoc, LParenLoc, EndLoc);
16626 break;
16627 case OMPC_novariants:
16628 Res = ActOnOpenMPNovariantsClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16629 break;
16630 case OMPC_nocontext:
16631 Res = ActOnOpenMPNocontextClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16632 break;
16633 case OMPC_filter:
16634 Res = ActOnOpenMPFilterClause(ThreadID: Expr, StartLoc, LParenLoc, EndLoc);
16635 break;
16636 case OMPC_partial:
16637 Res = ActOnOpenMPPartialClause(FactorExpr: Expr, StartLoc, LParenLoc, EndLoc);
16638 break;
16639 case OMPC_message:
16640 Res = ActOnOpenMPMessageClause(MS: Expr, StartLoc, LParenLoc, EndLoc);
16641 break;
16642 case OMPC_align:
16643 Res = ActOnOpenMPAlignClause(Alignment: Expr, StartLoc, LParenLoc, EndLoc);
16644 break;
16645 case OMPC_ompx_dyn_cgroup_mem:
16646 Res = ActOnOpenMPXDynCGroupMemClause(Size: Expr, StartLoc, LParenLoc, EndLoc);
16647 break;
16648 case OMPC_holds:
16649 Res = ActOnOpenMPHoldsClause(E: Expr, StartLoc, LParenLoc, EndLoc);
16650 break;
16651 case OMPC_transparent:
16652 Res = ActOnOpenMPTransparentClause(Transparent: Expr, StartLoc, LParenLoc, EndLoc);
16653 break;
16654 case OMPC_dyn_groupprivate:
16655 case OMPC_grainsize:
16656 case OMPC_num_tasks:
16657 case OMPC_num_threads:
16658 case OMPC_device:
16659 case OMPC_if:
16660 case OMPC_default:
16661 case OMPC_proc_bind:
16662 case OMPC_schedule:
16663 case OMPC_private:
16664 case OMPC_firstprivate:
16665 case OMPC_lastprivate:
16666 case OMPC_shared:
16667 case OMPC_reduction:
16668 case OMPC_task_reduction:
16669 case OMPC_in_reduction:
16670 case OMPC_linear:
16671 case OMPC_aligned:
16672 case OMPC_copyin:
16673 case OMPC_copyprivate:
16674 case OMPC_untied:
16675 case OMPC_mergeable:
16676 case OMPC_threadprivate:
16677 case OMPC_groupprivate:
16678 case OMPC_sizes:
16679 case OMPC_allocate:
16680 case OMPC_flush:
16681 case OMPC_read:
16682 case OMPC_write:
16683 case OMPC_update:
16684 case OMPC_capture:
16685 case OMPC_compare:
16686 case OMPC_seq_cst:
16687 case OMPC_acq_rel:
16688 case OMPC_acquire:
16689 case OMPC_release:
16690 case OMPC_relaxed:
16691 case OMPC_depend:
16692 case OMPC_threads:
16693 case OMPC_simd:
16694 case OMPC_map:
16695 case OMPC_nogroup:
16696 case OMPC_dist_schedule:
16697 case OMPC_defaultmap:
16698 case OMPC_unknown:
16699 case OMPC_uniform:
16700 case OMPC_to:
16701 case OMPC_from:
16702 case OMPC_use_device_ptr:
16703 case OMPC_use_device_addr:
16704 case OMPC_is_device_ptr:
16705 case OMPC_unified_address:
16706 case OMPC_unified_shared_memory:
16707 case OMPC_reverse_offload:
16708 case OMPC_dynamic_allocators:
16709 case OMPC_atomic_default_mem_order:
16710 case OMPC_self_maps:
16711 case OMPC_device_type:
16712 case OMPC_match:
16713 case OMPC_nontemporal:
16714 case OMPC_order:
16715 case OMPC_at:
16716 case OMPC_severity:
16717 case OMPC_destroy:
16718 case OMPC_inclusive:
16719 case OMPC_exclusive:
16720 case OMPC_uses_allocators:
16721 case OMPC_affinity:
16722 case OMPC_when:
16723 case OMPC_bind:
16724 case OMPC_num_teams:
16725 case OMPC_thread_limit:
16726 default:
16727 llvm_unreachable("Clause is not allowed.");
16728 }
16729 return Res;
16730}
16731
16732// An OpenMP directive such as 'target parallel' has two captured regions:
16733// for the 'target' and 'parallel' respectively. This function returns
16734// the region in which to capture expressions associated with a clause.
16735// A return value of OMPD_unknown signifies that the expression should not
16736// be captured.
16737static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
16738 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion,
16739 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
16740 assert(isAllowedClauseForDirective(DKind, CKind, OpenMPVersion) &&
16741 "Invalid directive with CKind-clause");
16742
16743 // Invalid modifier will be diagnosed separately, just return OMPD_unknown.
16744 if (NameModifier != OMPD_unknown &&
16745 !isAllowedClauseForDirective(D: NameModifier, C: CKind, Version: OpenMPVersion))
16746 return OMPD_unknown;
16747
16748 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: DKind);
16749
16750 // [5.2:341:24-30]
16751 // If the clauses have expressions on them, such as for various clauses where
16752 // the argument of the clause is an expression, or lower-bound, length, or
16753 // stride expressions inside array sections (or subscript and stride
16754 // expressions in subscript-triplet for Fortran), or linear-step or alignment
16755 // expressions, the expressions are evaluated immediately before the construct
16756 // to which the clause has been split or duplicated per the above rules
16757 // (therefore inside of the outer leaf constructs). However, the expressions
16758 // inside the num_teams and thread_limit clauses are always evaluated before
16759 // the outermost leaf construct.
16760
16761 // Process special cases first.
16762 switch (CKind) {
16763 case OMPC_if:
16764 switch (DKind) {
16765 case OMPD_teams_loop:
16766 case OMPD_target_teams_loop:
16767 // For [target] teams loop, assume capture region is 'teams' so it's
16768 // available for codegen later to use if/when necessary.
16769 return OMPD_teams;
16770 case OMPD_target_update:
16771 case OMPD_target_enter_data:
16772 case OMPD_target_exit_data:
16773 return OMPD_task;
16774 default:
16775 break;
16776 }
16777 break;
16778 case OMPC_num_teams:
16779 case OMPC_thread_limit:
16780 case OMPC_ompx_dyn_cgroup_mem:
16781 case OMPC_dyn_groupprivate:
16782 // TODO: This may need to consider teams too.
16783 if (Leafs[0] == OMPD_target)
16784 return OMPD_target;
16785 break;
16786 case OMPC_device:
16787 if (Leafs[0] == OMPD_target ||
16788 llvm::is_contained(Set: {OMPD_dispatch, OMPD_target_update,
16789 OMPD_target_enter_data, OMPD_target_exit_data},
16790 Element: DKind))
16791 return OMPD_task;
16792 break;
16793 case OMPC_novariants:
16794 case OMPC_nocontext:
16795 if (DKind == OMPD_dispatch)
16796 return OMPD_task;
16797 break;
16798 case OMPC_when:
16799 if (DKind == OMPD_metadirective)
16800 return OMPD_metadirective;
16801 break;
16802 case OMPC_filter:
16803 return OMPD_unknown;
16804 default:
16805 break;
16806 }
16807
16808 // If none of the special cases above applied, and DKind is a capturing
16809 // directive, find the innermost enclosing leaf construct that allows the
16810 // clause, and returns the corresponding capture region.
16811
16812 auto GetEnclosingRegion = [&](int EndIdx, OpenMPClauseKind Clause) {
16813 // Find the index in "Leafs" of the last leaf that allows the given
16814 // clause. The search will only include indexes [0, EndIdx).
16815 // EndIdx may be set to the index of the NameModifier, if present.
16816 int InnermostIdx = [&]() {
16817 for (int I = EndIdx - 1; I >= 0; --I) {
16818 if (isAllowedClauseForDirective(D: Leafs[I], C: Clause, Version: OpenMPVersion))
16819 return I;
16820 }
16821 return -1;
16822 }();
16823
16824 // Find the nearest enclosing capture region.
16825 SmallVector<OpenMPDirectiveKind, 2> Regions;
16826 for (int I = InnermostIdx - 1; I >= 0; --I) {
16827 if (!isOpenMPCapturingDirective(DKind: Leafs[I]))
16828 continue;
16829 Regions.clear();
16830 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind: Leafs[I]);
16831 if (Regions[0] != OMPD_unknown)
16832 return Regions.back();
16833 }
16834 return OMPD_unknown;
16835 };
16836
16837 if (isOpenMPCapturingDirective(DKind)) {
16838 auto GetLeafIndex = [&](OpenMPDirectiveKind Dir) {
16839 for (int I = 0, E = Leafs.size(); I != E; ++I) {
16840 if (Leafs[I] == Dir)
16841 return I + 1;
16842 }
16843 return 0;
16844 };
16845
16846 int End = NameModifier == OMPD_unknown ? Leafs.size()
16847 : GetLeafIndex(NameModifier);
16848 return GetEnclosingRegion(End, CKind);
16849 }
16850
16851 return OMPD_unknown;
16852}
16853
16854OMPClause *SemaOpenMP::ActOnOpenMPIfClause(
16855 OpenMPDirectiveKind NameModifier, Expr *Condition, SourceLocation StartLoc,
16856 SourceLocation LParenLoc, SourceLocation NameModifierLoc,
16857 SourceLocation ColonLoc, SourceLocation EndLoc) {
16858 Expr *ValExpr = Condition;
16859 Stmt *HelperValStmt = nullptr;
16860 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16861 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
16862 !Condition->isInstantiationDependent() &&
16863 !Condition->containsUnexpandedParameterPack()) {
16864 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
16865 if (Val.isInvalid())
16866 return nullptr;
16867
16868 ValExpr = Val.get();
16869
16870 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16871 CaptureRegion = getOpenMPCaptureRegionForClause(
16872 DKind, CKind: OMPC_if, OpenMPVersion: getLangOpts().OpenMP, NameModifier);
16873 if (CaptureRegion != OMPD_unknown &&
16874 !SemaRef.CurContext->isDependentContext()) {
16875 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16876 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16877 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16878 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
16879 }
16880 }
16881
16882 return new (getASTContext())
16883 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
16884 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
16885}
16886
16887OMPClause *SemaOpenMP::ActOnOpenMPFinalClause(Expr *Condition,
16888 SourceLocation StartLoc,
16889 SourceLocation LParenLoc,
16890 SourceLocation EndLoc) {
16891 Expr *ValExpr = Condition;
16892 Stmt *HelperValStmt = nullptr;
16893 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16894 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
16895 !Condition->isInstantiationDependent() &&
16896 !Condition->containsUnexpandedParameterPack()) {
16897 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
16898 if (Val.isInvalid())
16899 return nullptr;
16900
16901 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
16902
16903 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16904 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_final,
16905 OpenMPVersion: getLangOpts().OpenMP);
16906 if (CaptureRegion != OMPD_unknown &&
16907 !SemaRef.CurContext->isDependentContext()) {
16908 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16909 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16910 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16911 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
16912 }
16913 }
16914
16915 return new (getASTContext()) OMPFinalClause(
16916 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
16917}
16918
16919ExprResult
16920SemaOpenMP::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
16921 Expr *Op) {
16922 if (!Op)
16923 return ExprError();
16924
16925 class IntConvertDiagnoser : public Sema::ICEConvertDiagnoser {
16926 public:
16927 IntConvertDiagnoser()
16928 : ICEConvertDiagnoser(/*AllowScopedEnumerations=*/false, false, true) {}
16929 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
16930 QualType T) override {
16931 return S.Diag(Loc, DiagID: diag::err_omp_not_integral) << T;
16932 }
16933 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
16934 QualType T) override {
16935 return S.Diag(Loc, DiagID: diag::err_omp_incomplete_type) << T;
16936 }
16937 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
16938 QualType T,
16939 QualType ConvTy) override {
16940 return S.Diag(Loc, DiagID: diag::err_omp_explicit_conversion) << T << ConvTy;
16941 }
16942 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
16943 QualType ConvTy) override {
16944 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
16945 << ConvTy->isEnumeralType() << ConvTy;
16946 }
16947 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
16948 QualType T) override {
16949 return S.Diag(Loc, DiagID: diag::err_omp_ambiguous_conversion) << T;
16950 }
16951 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
16952 QualType ConvTy) override {
16953 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
16954 << ConvTy->isEnumeralType() << ConvTy;
16955 }
16956 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
16957 QualType) override {
16958 llvm_unreachable("conversion functions are permitted");
16959 }
16960 } ConvertDiagnoser;
16961 return SemaRef.PerformContextualImplicitConversion(Loc, FromE: Op, Converter&: ConvertDiagnoser);
16962}
16963
16964static bool
16965isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
16966 bool StrictlyPositive, bool BuildCapture = false,
16967 OpenMPDirectiveKind DKind = OMPD_unknown,
16968 OpenMPDirectiveKind *CaptureRegion = nullptr,
16969 Stmt **HelperValStmt = nullptr) {
16970 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
16971 !ValExpr->isInstantiationDependent()) {
16972 SourceLocation Loc = ValExpr->getExprLoc();
16973 ExprResult Value =
16974 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc, Op: ValExpr);
16975 if (Value.isInvalid())
16976 return false;
16977
16978 ValExpr = Value.get();
16979 // The expression must evaluate to a non-negative integer value.
16980 if (std::optional<llvm::APSInt> Result =
16981 ValExpr->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
16982 if (Result->isSigned() &&
16983 !((!StrictlyPositive && Result->isNonNegative()) ||
16984 (StrictlyPositive && Result->isStrictlyPositive()))) {
16985 SemaRef.Diag(Loc, DiagID: diag::err_omp_negative_expression_in_clause)
16986 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
16987 << ValExpr->getSourceRange();
16988 return false;
16989 }
16990 }
16991 if (!BuildCapture)
16992 return true;
16993 *CaptureRegion =
16994 getOpenMPCaptureRegionForClause(DKind, CKind, OpenMPVersion: SemaRef.LangOpts.OpenMP);
16995 if (*CaptureRegion != OMPD_unknown &&
16996 !SemaRef.CurContext->isDependentContext()) {
16997 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16998 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16999 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
17000 *HelperValStmt = buildPreInits(Context&: SemaRef.Context, Captures);
17001 }
17002 }
17003 return true;
17004}
17005
17006static std::string getListOfPossibleValues(OpenMPClauseKind K, unsigned First,
17007 unsigned Last,
17008 ArrayRef<unsigned> Exclude = {}) {
17009 SmallString<256> Buffer;
17010 llvm::raw_svector_ostream Out(Buffer);
17011 unsigned Skipped = Exclude.size();
17012 for (unsigned I = First; I < Last; ++I) {
17013 if (llvm::is_contained(Range&: Exclude, Element: I)) {
17014 --Skipped;
17015 continue;
17016 }
17017 Out << "'" << getOpenMPSimpleClauseTypeName(Kind: K, Type: I) << "'";
17018 if (I + Skipped + 2 == Last)
17019 Out << " or ";
17020 else if (I + Skipped + 1 != Last)
17021 Out << ", ";
17022 }
17023 return std::string(Out.str());
17024}
17025
17026OMPClause *SemaOpenMP::ActOnOpenMPNumThreadsClause(
17027 OpenMPNumThreadsClauseModifier Modifier, Expr *NumThreads,
17028 SourceLocation StartLoc, SourceLocation LParenLoc,
17029 SourceLocation ModifierLoc, SourceLocation EndLoc) {
17030 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 60) &&
17031 "Unexpected num_threads modifier in OpenMP < 60.");
17032
17033 if (ModifierLoc.isValid() && Modifier == OMPC_NUMTHREADS_unknown) {
17034 std::string Values = getListOfPossibleValues(K: OMPC_num_threads, /*First=*/0,
17035 Last: OMPC_NUMTHREADS_unknown);
17036 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
17037 << Values << getOpenMPClauseNameForDiag(C: OMPC_num_threads);
17038 return nullptr;
17039 }
17040
17041 Expr *ValExpr = NumThreads;
17042 Stmt *HelperValStmt = nullptr;
17043
17044 // OpenMP [2.5, Restrictions]
17045 // The num_threads expression must evaluate to a positive integer value.
17046 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_threads,
17047 /*StrictlyPositive=*/true))
17048 return nullptr;
17049
17050 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17051 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
17052 DKind, CKind: OMPC_num_threads, OpenMPVersion: getLangOpts().OpenMP);
17053 if (CaptureRegion != OMPD_unknown &&
17054 !SemaRef.CurContext->isDependentContext()) {
17055 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
17056 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17057 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
17058 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
17059 }
17060
17061 return new (getASTContext())
17062 OMPNumThreadsClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
17063 StartLoc, LParenLoc, ModifierLoc, EndLoc);
17064}
17065
17066ExprResult SemaOpenMP::VerifyPositiveIntegerConstantInClause(
17067 Expr *E, OpenMPClauseKind CKind, bool StrictlyPositive,
17068 bool SuppressExprDiags) {
17069 if (!E)
17070 return ExprError();
17071 if (E->isValueDependent() || E->isTypeDependent() ||
17072 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
17073 return E;
17074
17075 llvm::APSInt Result;
17076 ExprResult ICE;
17077 if (SuppressExprDiags) {
17078 // Use a custom diagnoser that suppresses 'note' diagnostics about the
17079 // expression.
17080 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser {
17081 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {}
17082 SemaBase::SemaDiagnosticBuilder
17083 diagnoseNotICE(Sema &S, SourceLocation Loc) override {
17084 llvm_unreachable("Diagnostic suppressed");
17085 }
17086 } Diagnoser;
17087 ICE = SemaRef.VerifyIntegerConstantExpression(E, Result: &Result, Diagnoser,
17088 CanFold: AllowFoldKind::Allow);
17089 } else {
17090 ICE =
17091 SemaRef.VerifyIntegerConstantExpression(E, Result: &Result,
17092 /*FIXME*/ CanFold: AllowFoldKind::Allow);
17093 }
17094 if (ICE.isInvalid())
17095 return ExprError();
17096
17097 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
17098 (!StrictlyPositive && !Result.isNonNegative())) {
17099 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_negative_expression_in_clause)
17100 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
17101 << E->getSourceRange();
17102 return ExprError();
17103 }
17104 if ((CKind == OMPC_aligned || CKind == OMPC_align ||
17105 CKind == OMPC_allocate) &&
17106 !Result.isPowerOf2()) {
17107 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_alignment_not_power_of_two)
17108 << E->getSourceRange();
17109 return ExprError();
17110 }
17111
17112 if (!Result.isRepresentableByInt64()) {
17113 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_large_expression_in_clause)
17114 << getOpenMPClauseNameForDiag(C: CKind) << E->getSourceRange();
17115 return ExprError();
17116 }
17117
17118 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
17119 DSAStack->setAssociatedLoops(Result.getExtValue());
17120 else if (CKind == OMPC_ordered)
17121 DSAStack->setAssociatedLoops(Result.getExtValue());
17122 return ICE;
17123}
17124
17125void SemaOpenMP::setOpenMPDeviceNum(int Num) { DeviceNum = Num; }
17126
17127void SemaOpenMP::setOpenMPDeviceNumID(StringRef ID) { DeviceNumID = ID; }
17128
17129int SemaOpenMP::getOpenMPDeviceNum() const { return DeviceNum; }
17130
17131void SemaOpenMP::ActOnOpenMPDeviceNum(Expr *DeviceNumExpr) {
17132 llvm::APSInt Result;
17133 Expr::EvalResult EvalResult;
17134 // Evaluate the expression to an integer value
17135 if (!DeviceNumExpr->isValueDependent() &&
17136 DeviceNumExpr->EvaluateAsInt(Result&: EvalResult, Ctx: SemaRef.Context)) {
17137 // The device expression must evaluate to a non-negative integer value.
17138 Result = EvalResult.Val.getInt();
17139 if (Result.isNonNegative()) {
17140 setOpenMPDeviceNum(Result.getZExtValue());
17141 } else {
17142 Diag(Loc: DeviceNumExpr->getExprLoc(),
17143 DiagID: diag::err_omp_negative_expression_in_clause)
17144 << "device_num" << 0 << DeviceNumExpr->getSourceRange();
17145 }
17146 } else if (auto *DeclRef = dyn_cast<DeclRefExpr>(Val: DeviceNumExpr)) {
17147 // Check if the expression is an identifier
17148 IdentifierInfo *IdInfo = DeclRef->getDecl()->getIdentifier();
17149 if (IdInfo) {
17150 setOpenMPDeviceNumID(IdInfo->getName());
17151 }
17152 } else {
17153 Diag(Loc: DeviceNumExpr->getExprLoc(), DiagID: diag::err_expected_expression);
17154 }
17155}
17156
17157OMPClause *SemaOpenMP::ActOnOpenMPSafelenClause(Expr *Len,
17158 SourceLocation StartLoc,
17159 SourceLocation LParenLoc,
17160 SourceLocation EndLoc) {
17161 // OpenMP [2.8.1, simd construct, Description]
17162 // The parameter of the safelen clause must be a constant
17163 // positive integer expression.
17164 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_safelen);
17165 if (Safelen.isInvalid())
17166 return nullptr;
17167 return new (getASTContext())
17168 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
17169}
17170
17171OMPClause *SemaOpenMP::ActOnOpenMPSimdlenClause(Expr *Len,
17172 SourceLocation StartLoc,
17173 SourceLocation LParenLoc,
17174 SourceLocation EndLoc) {
17175 // OpenMP [2.8.1, simd construct, Description]
17176 // The parameter of the simdlen clause must be a constant
17177 // positive integer expression.
17178 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_simdlen);
17179 if (Simdlen.isInvalid())
17180 return nullptr;
17181 return new (getASTContext())
17182 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
17183}
17184
17185/// Tries to find omp_allocator_handle_t type.
17186static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
17187 DSAStackTy *Stack) {
17188 if (!Stack->getOMPAllocatorHandleT().isNull())
17189 return true;
17190
17191 // Set the allocator handle type.
17192 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_allocator_handle_t");
17193 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
17194 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
17195 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
17196 << "omp_allocator_handle_t";
17197 return false;
17198 }
17199 QualType AllocatorHandleEnumTy = PT.get();
17200 AllocatorHandleEnumTy.addConst();
17201 Stack->setOMPAllocatorHandleT(AllocatorHandleEnumTy);
17202
17203 // Fill the predefined allocator map.
17204 bool ErrorFound = false;
17205 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
17206 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
17207 StringRef Allocator =
17208 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
17209 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Name: Allocator);
17210 auto *VD = dyn_cast_or_null<ValueDecl>(
17211 Val: S.LookupSingleName(S: S.TUScope, Name: AllocatorName, Loc, NameKind: Sema::LookupAnyName));
17212 if (!VD) {
17213 ErrorFound = true;
17214 break;
17215 }
17216 QualType AllocatorType =
17217 VD->getType().getNonLValueExprType(Context: S.getASTContext());
17218 ExprResult Res = S.BuildDeclRefExpr(D: VD, Ty: AllocatorType, VK: VK_LValue, Loc);
17219 if (!Res.isUsable()) {
17220 ErrorFound = true;
17221 break;
17222 }
17223 Res = S.PerformImplicitConversion(From: Res.get(), ToType: AllocatorHandleEnumTy,
17224 Action: AssignmentAction::Initializing,
17225 /*AllowExplicit=*/true);
17226 if (!Res.isUsable()) {
17227 ErrorFound = true;
17228 break;
17229 }
17230 Stack->setAllocator(AllocatorKind, Allocator: Res.get());
17231 }
17232 if (ErrorFound) {
17233 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
17234 << "omp_allocator_handle_t";
17235 return false;
17236 }
17237
17238 return true;
17239}
17240
17241OMPClause *SemaOpenMP::ActOnOpenMPAllocatorClause(Expr *A,
17242 SourceLocation StartLoc,
17243 SourceLocation LParenLoc,
17244 SourceLocation EndLoc) {
17245 // OpenMP [2.11.3, allocate Directive, Description]
17246 // allocator is an expression of omp_allocator_handle_t type.
17247 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: A->getExprLoc(), DSAStack))
17248 return nullptr;
17249
17250 ExprResult Allocator = SemaRef.DefaultLvalueConversion(E: A);
17251 if (Allocator.isInvalid())
17252 return nullptr;
17253 Allocator = SemaRef.PerformImplicitConversion(
17254 From: Allocator.get(), DSAStack->getOMPAllocatorHandleT(),
17255 Action: AssignmentAction::Initializing,
17256 /*AllowExplicit=*/true);
17257 if (Allocator.isInvalid())
17258 return nullptr;
17259 return new (getASTContext())
17260 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
17261}
17262
17263OMPClause *SemaOpenMP::ActOnOpenMPCollapseClause(Expr *NumForLoops,
17264 SourceLocation StartLoc,
17265 SourceLocation LParenLoc,
17266 SourceLocation EndLoc) {
17267 // OpenMP [2.7.1, loop construct, Description]
17268 // OpenMP [2.8.1, simd construct, Description]
17269 // OpenMP [2.9.6, distribute construct, Description]
17270 // The parameter of the collapse clause must be a constant
17271 // positive integer expression.
17272 ExprResult NumForLoopsResult =
17273 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_collapse);
17274 if (NumForLoopsResult.isInvalid())
17275 return nullptr;
17276 return new (getASTContext())
17277 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
17278}
17279
17280OMPClause *SemaOpenMP::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
17281 SourceLocation EndLoc,
17282 SourceLocation LParenLoc,
17283 Expr *NumForLoops) {
17284 // OpenMP [2.7.1, loop construct, Description]
17285 // OpenMP [2.8.1, simd construct, Description]
17286 // OpenMP [2.9.6, distribute construct, Description]
17287 // The parameter of the ordered clause must be a constant
17288 // positive integer expression if any.
17289 if (NumForLoops && LParenLoc.isValid()) {
17290 ExprResult NumForLoopsResult =
17291 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_ordered);
17292 if (NumForLoopsResult.isInvalid())
17293 return nullptr;
17294 NumForLoops = NumForLoopsResult.get();
17295 } else {
17296 NumForLoops = nullptr;
17297 }
17298 auto *Clause =
17299 OMPOrderedClause::Create(C: getASTContext(), Num: NumForLoops,
17300 NumLoops: NumForLoops ? DSAStack->getAssociatedLoops() : 0,
17301 StartLoc, LParenLoc, EndLoc);
17302 DSAStack->setOrderedRegion(/*IsOrdered=*/true, Param: NumForLoops, Clause);
17303 return Clause;
17304}
17305
17306OMPClause *SemaOpenMP::ActOnOpenMPSimpleClause(
17307 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
17308 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17309 OMPClause *Res = nullptr;
17310 switch (Kind) {
17311 case OMPC_proc_bind:
17312 Res = ActOnOpenMPProcBindClause(Kind: static_cast<ProcBindKind>(Argument),
17313 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17314 break;
17315 case OMPC_atomic_default_mem_order:
17316 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
17317 Kind: static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
17318 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17319 break;
17320 case OMPC_fail:
17321 Res = ActOnOpenMPFailClause(Kind: static_cast<OpenMPClauseKind>(Argument),
17322 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17323 break;
17324 case OMPC_update:
17325 Res = ActOnOpenMPUpdateClause(Kind: static_cast<OpenMPDependClauseKind>(Argument),
17326 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17327 break;
17328 case OMPC_bind:
17329 Res = ActOnOpenMPBindClause(Kind: static_cast<OpenMPBindClauseKind>(Argument),
17330 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17331 break;
17332 case OMPC_at:
17333 Res = ActOnOpenMPAtClause(Kind: static_cast<OpenMPAtClauseKind>(Argument),
17334 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17335 break;
17336 case OMPC_severity:
17337 Res = ActOnOpenMPSeverityClause(
17338 Kind: static_cast<OpenMPSeverityClauseKind>(Argument), KindLoc: ArgumentLoc, StartLoc,
17339 LParenLoc, EndLoc);
17340 break;
17341 case OMPC_threadset:
17342 Res = ActOnOpenMPThreadsetClause(Kind: static_cast<OpenMPThreadsetKind>(Argument),
17343 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17344 break;
17345 case OMPC_if:
17346 case OMPC_final:
17347 case OMPC_num_threads:
17348 case OMPC_safelen:
17349 case OMPC_simdlen:
17350 case OMPC_sizes:
17351 case OMPC_allocator:
17352 case OMPC_collapse:
17353 case OMPC_schedule:
17354 case OMPC_private:
17355 case OMPC_firstprivate:
17356 case OMPC_lastprivate:
17357 case OMPC_shared:
17358 case OMPC_reduction:
17359 case OMPC_task_reduction:
17360 case OMPC_in_reduction:
17361 case OMPC_linear:
17362 case OMPC_aligned:
17363 case OMPC_copyin:
17364 case OMPC_copyprivate:
17365 case OMPC_ordered:
17366 case OMPC_nowait:
17367 case OMPC_untied:
17368 case OMPC_mergeable:
17369 case OMPC_threadprivate:
17370 case OMPC_groupprivate:
17371 case OMPC_allocate:
17372 case OMPC_flush:
17373 case OMPC_depobj:
17374 case OMPC_read:
17375 case OMPC_write:
17376 case OMPC_capture:
17377 case OMPC_compare:
17378 case OMPC_seq_cst:
17379 case OMPC_acq_rel:
17380 case OMPC_acquire:
17381 case OMPC_release:
17382 case OMPC_relaxed:
17383 case OMPC_depend:
17384 case OMPC_device:
17385 case OMPC_threads:
17386 case OMPC_simd:
17387 case OMPC_map:
17388 case OMPC_num_teams:
17389 case OMPC_thread_limit:
17390 case OMPC_priority:
17391 case OMPC_grainsize:
17392 case OMPC_nogroup:
17393 case OMPC_num_tasks:
17394 case OMPC_hint:
17395 case OMPC_dist_schedule:
17396 case OMPC_default:
17397 case OMPC_defaultmap:
17398 case OMPC_unknown:
17399 case OMPC_uniform:
17400 case OMPC_to:
17401 case OMPC_from:
17402 case OMPC_use_device_ptr:
17403 case OMPC_use_device_addr:
17404 case OMPC_is_device_ptr:
17405 case OMPC_has_device_addr:
17406 case OMPC_unified_address:
17407 case OMPC_unified_shared_memory:
17408 case OMPC_reverse_offload:
17409 case OMPC_dynamic_allocators:
17410 case OMPC_self_maps:
17411 case OMPC_device_type:
17412 case OMPC_match:
17413 case OMPC_nontemporal:
17414 case OMPC_destroy:
17415 case OMPC_novariants:
17416 case OMPC_nocontext:
17417 case OMPC_detach:
17418 case OMPC_inclusive:
17419 case OMPC_exclusive:
17420 case OMPC_uses_allocators:
17421 case OMPC_affinity:
17422 case OMPC_when:
17423 case OMPC_message:
17424 default:
17425 llvm_unreachable("Clause is not allowed.");
17426 }
17427 return Res;
17428}
17429
17430OMPClause *SemaOpenMP::ActOnOpenMPDefaultClause(
17431 llvm::omp::DefaultKind M, SourceLocation MLoc,
17432 OpenMPDefaultClauseVariableCategory VCKind, SourceLocation VCKindLoc,
17433 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17434 if (M == OMP_DEFAULT_unknown) {
17435 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
17436 << getListOfPossibleValues(K: OMPC_default, /*First=*/0,
17437 /*Last=*/unsigned(OMP_DEFAULT_unknown))
17438 << getOpenMPClauseNameForDiag(C: OMPC_default);
17439 return nullptr;
17440 }
17441 if (VCKind == OMPC_DEFAULT_VC_unknown) {
17442 Diag(Loc: VCKindLoc, DiagID: diag::err_omp_default_vc)
17443 << getOpenMPSimpleClauseTypeName(Kind: OMPC_default, Type: unsigned(M));
17444 return nullptr;
17445 }
17446
17447 bool IsTargetDefault =
17448 getLangOpts().OpenMP >= 60 &&
17449 isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective());
17450
17451 // OpenMP 6.0, page 224, lines 3-4 default Clause, Semantics
17452 // If data-sharing-attribute is shared then the clause has no effect
17453 // on a target construct;
17454 if (IsTargetDefault && M == OMP_DEFAULT_shared)
17455 return nullptr;
17456
17457 auto SetDefaultClauseAttrs = [&](llvm::omp::DefaultKind M,
17458 OpenMPDefaultClauseVariableCategory VCKind) {
17459 OpenMPDefaultmapClauseModifier DefMapMod;
17460 OpenMPDefaultmapClauseKind DefMapKind;
17461 // default data-sharing-attribute
17462 switch (M) {
17463 case OMP_DEFAULT_none:
17464 if (IsTargetDefault)
17465 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_none;
17466 else
17467 DSAStack->setDefaultDSANone(MLoc);
17468 break;
17469 case OMP_DEFAULT_firstprivate:
17470 if (IsTargetDefault)
17471 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_firstprivate;
17472 else
17473 DSAStack->setDefaultDSAFirstPrivate(MLoc);
17474 break;
17475 case OMP_DEFAULT_private:
17476 if (IsTargetDefault)
17477 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_private;
17478 else
17479 DSAStack->setDefaultDSAPrivate(MLoc);
17480 break;
17481 case OMP_DEFAULT_shared:
17482 assert(!IsTargetDefault && "DSA shared invalid with target directive");
17483 DSAStack->setDefaultDSAShared(MLoc);
17484 break;
17485 default:
17486 llvm_unreachable("unexpected DSA in OpenMP default clause");
17487 }
17488 // default variable-category
17489 switch (VCKind) {
17490 case OMPC_DEFAULT_VC_aggregate:
17491 if (IsTargetDefault)
17492 DefMapKind = OMPC_DEFAULTMAP_aggregate;
17493 else
17494 DSAStack->setDefaultDSAVCAggregate(VCKindLoc);
17495 break;
17496 case OMPC_DEFAULT_VC_pointer:
17497 if (IsTargetDefault)
17498 DefMapKind = OMPC_DEFAULTMAP_pointer;
17499 else
17500 DSAStack->setDefaultDSAVCPointer(VCKindLoc);
17501 break;
17502 case OMPC_DEFAULT_VC_scalar:
17503 if (IsTargetDefault)
17504 DefMapKind = OMPC_DEFAULTMAP_scalar;
17505 else
17506 DSAStack->setDefaultDSAVCScalar(VCKindLoc);
17507 break;
17508 case OMPC_DEFAULT_VC_all:
17509 if (IsTargetDefault)
17510 DefMapKind = OMPC_DEFAULTMAP_all;
17511 else
17512 DSAStack->setDefaultDSAVCAll(VCKindLoc);
17513 break;
17514 default:
17515 llvm_unreachable("unexpected variable category in OpenMP default clause");
17516 }
17517 // OpenMP 6.0, page 224, lines 4-5 default Clause, Semantics
17518 // otherwise, its effect on a target construct is equivalent to
17519 // specifying the defaultmap clause with the same data-sharing-attribute
17520 // and variable-category.
17521 //
17522 // If earlier than OpenMP 6.0, or not a target directive, the default DSA
17523 // is/was set as before.
17524 if (IsTargetDefault) {
17525 if (DefMapKind == OMPC_DEFAULTMAP_all) {
17526 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_aggregate, Loc: MLoc);
17527 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_scalar, Loc: MLoc);
17528 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_pointer, Loc: MLoc);
17529 } else {
17530 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: DefMapKind, Loc: MLoc);
17531 }
17532 }
17533 };
17534
17535 SetDefaultClauseAttrs(M, VCKind);
17536 return new (getASTContext())
17537 OMPDefaultClause(M, MLoc, VCKind, VCKindLoc, StartLoc, LParenLoc, EndLoc);
17538}
17539
17540OMPClause *SemaOpenMP::ActOnOpenMPThreadsetClause(OpenMPThreadsetKind Kind,
17541 SourceLocation KindLoc,
17542 SourceLocation StartLoc,
17543 SourceLocation LParenLoc,
17544 SourceLocation EndLoc) {
17545 if (Kind == OMPC_THREADSET_unknown) {
17546 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
17547 << getListOfPossibleValues(K: OMPC_threadset, /*First=*/0,
17548 /*Last=*/unsigned(OMPC_THREADSET_unknown))
17549 << getOpenMPClauseName(C: OMPC_threadset);
17550 return nullptr;
17551 }
17552
17553 return new (getASTContext())
17554 OMPThreadsetClause(Kind, KindLoc, StartLoc, LParenLoc, EndLoc);
17555}
17556
17557static OMPClause *
17558createTransparentClause(Sema &SemaRef, ASTContext &Ctx, Expr *ImpexTypeArg,
17559 Stmt *HelperValStmt, OpenMPDirectiveKind CaptureRegion,
17560 SourceLocation StartLoc, SourceLocation LParenLoc,
17561 SourceLocation EndLoc) {
17562 ExprResult ER = SemaRef.DefaultLvalueConversion(E: ImpexTypeArg);
17563 if (ER.isInvalid())
17564 return nullptr;
17565
17566 return new (Ctx) OMPTransparentClause(ER.get(), HelperValStmt, CaptureRegion,
17567 StartLoc, LParenLoc, EndLoc);
17568}
17569
17570OMPClause *SemaOpenMP::ActOnOpenMPTransparentClause(Expr *ImpexTypeArg,
17571 SourceLocation StartLoc,
17572 SourceLocation LParenLoc,
17573 SourceLocation EndLoc) {
17574 Stmt *HelperValStmt = nullptr;
17575 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17576 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
17577 DKind, CKind: OMPC_transparent, OpenMPVersion: getLangOpts().OpenMP);
17578 if (CaptureRegion != OMPD_unknown &&
17579 !SemaRef.CurContext->isDependentContext()) {
17580 Expr *ValExpr = SemaRef.MakeFullExpr(Arg: ImpexTypeArg).get();
17581 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17582 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
17583 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
17584 }
17585 if (!ImpexTypeArg) {
17586 return new (getASTContext())
17587 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
17588 StartLoc, LParenLoc, EndLoc);
17589 }
17590 QualType Ty = ImpexTypeArg->getType();
17591
17592 if (const auto *TT = Ty->getAs<TypedefType>()) {
17593 const TypedefNameDecl *TypedefDecl = TT->getDecl();
17594 llvm::StringRef TypedefName = TypedefDecl->getName();
17595 IdentifierInfo &II = SemaRef.PP.getIdentifierTable().get(Name: TypedefName);
17596 ParsedType ImpexTy =
17597 SemaRef.getTypeName(II, NameLoc: StartLoc, S: SemaRef.getCurScope());
17598 if (!ImpexTy.getAsOpaquePtr() || ImpexTy.get().isNull()) {
17599 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_implied_type_not_found)
17600 << TypedefName;
17601 return nullptr;
17602 }
17603 return new (getASTContext())
17604 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
17605 StartLoc, LParenLoc, EndLoc);
17606 }
17607
17608 if (Ty->isEnumeralType())
17609 return createTransparentClause(SemaRef, Ctx&: getASTContext(), ImpexTypeArg,
17610 HelperValStmt, CaptureRegion, StartLoc,
17611 LParenLoc, EndLoc);
17612 if (Ty->isIntegerType()) {
17613 if (isNonNegativeIntegerValue(ValExpr&: ImpexTypeArg, SemaRef, CKind: OMPC_transparent,
17614 /*StrictlyPositive=*/false)) {
17615 ExprResult Value =
17616 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc: StartLoc,
17617 Op: ImpexTypeArg);
17618 if (std::optional<llvm::APSInt> Result =
17619 Value.get()->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
17620 if (Result->isNegative() ||
17621 Result >
17622 static_cast<int64_t>(SemaOpenMP::OpenMPImpexType::OMP_Export))
17623 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_transparent_invalid_value);
17624 }
17625 return new (getASTContext())
17626 OMPTransparentClause(ImpexTypeArg, HelperValStmt, CaptureRegion,
17627 StartLoc, LParenLoc, EndLoc);
17628 }
17629 }
17630 if (!isNonNegativeIntegerValue(ValExpr&: ImpexTypeArg, SemaRef, CKind: OMPC_transparent,
17631 /*StrictlyPositive=*/true))
17632 return nullptr;
17633 return new (getASTContext()) OMPTransparentClause(
17634 ImpexTypeArg, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
17635}
17636
17637OMPClause *SemaOpenMP::ActOnOpenMPProcBindClause(ProcBindKind Kind,
17638 SourceLocation KindKwLoc,
17639 SourceLocation StartLoc,
17640 SourceLocation LParenLoc,
17641 SourceLocation EndLoc) {
17642 if (Kind == OMP_PROC_BIND_unknown) {
17643 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17644 << getListOfPossibleValues(K: OMPC_proc_bind,
17645 /*First=*/unsigned(OMP_PROC_BIND_master),
17646 /*Last=*/
17647 unsigned(getLangOpts().OpenMP > 50
17648 ? OMP_PROC_BIND_primary
17649 : OMP_PROC_BIND_spread) +
17650 1)
17651 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
17652 return nullptr;
17653 }
17654 if (Kind == OMP_PROC_BIND_primary && getLangOpts().OpenMP < 51)
17655 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17656 << getListOfPossibleValues(K: OMPC_proc_bind,
17657 /*First=*/unsigned(OMP_PROC_BIND_master),
17658 /*Last=*/
17659 unsigned(OMP_PROC_BIND_spread) + 1)
17660 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
17661 return new (getASTContext())
17662 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17663}
17664
17665OMPClause *SemaOpenMP::ActOnOpenMPAtomicDefaultMemOrderClause(
17666 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
17667 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17668 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
17669 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17670 << getListOfPossibleValues(
17671 K: OMPC_atomic_default_mem_order, /*First=*/0,
17672 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
17673 << getOpenMPClauseNameForDiag(C: OMPC_atomic_default_mem_order);
17674 return nullptr;
17675 }
17676 return new (getASTContext()) OMPAtomicDefaultMemOrderClause(
17677 Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17678}
17679
17680OMPClause *SemaOpenMP::ActOnOpenMPAtClause(OpenMPAtClauseKind Kind,
17681 SourceLocation KindKwLoc,
17682 SourceLocation StartLoc,
17683 SourceLocation LParenLoc,
17684 SourceLocation EndLoc) {
17685 if (Kind == OMPC_AT_unknown) {
17686 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17687 << getListOfPossibleValues(K: OMPC_at, /*First=*/0,
17688 /*Last=*/OMPC_AT_unknown)
17689 << getOpenMPClauseNameForDiag(C: OMPC_at);
17690 return nullptr;
17691 }
17692 return new (getASTContext())
17693 OMPAtClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17694}
17695
17696OMPClause *SemaOpenMP::ActOnOpenMPSeverityClause(OpenMPSeverityClauseKind Kind,
17697 SourceLocation KindKwLoc,
17698 SourceLocation StartLoc,
17699 SourceLocation LParenLoc,
17700 SourceLocation EndLoc) {
17701 if (Kind == OMPC_SEVERITY_unknown) {
17702 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17703 << getListOfPossibleValues(K: OMPC_severity, /*First=*/0,
17704 /*Last=*/OMPC_SEVERITY_unknown)
17705 << getOpenMPClauseNameForDiag(C: OMPC_severity);
17706 return nullptr;
17707 }
17708 return new (getASTContext())
17709 OMPSeverityClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17710}
17711
17712OMPClause *SemaOpenMP::ActOnOpenMPMessageClause(Expr *ME,
17713 SourceLocation StartLoc,
17714 SourceLocation LParenLoc,
17715 SourceLocation EndLoc) {
17716 assert(ME && "NULL expr in Message clause");
17717 QualType Type = ME->getType();
17718 if ((!Type->isPointerType() && !Type->isArrayType()) ||
17719 !Type->getPointeeOrArrayElementType()->isAnyCharacterType()) {
17720 Diag(Loc: ME->getBeginLoc(), DiagID: diag::warn_clause_expected_string)
17721 << getOpenMPClauseNameForDiag(C: OMPC_message) << 0;
17722 return nullptr;
17723 }
17724
17725 Stmt *HelperValStmt = nullptr;
17726
17727 // Depending on whether this clause appears in an executable context or not,
17728 // we may or may not build a capture.
17729 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17730 OpenMPDirectiveKind CaptureRegion =
17731 DKind == OMPD_unknown ? OMPD_unknown
17732 : getOpenMPCaptureRegionForClause(
17733 DKind, CKind: OMPC_message, OpenMPVersion: getLangOpts().OpenMP);
17734 if (CaptureRegion != OMPD_unknown &&
17735 !SemaRef.CurContext->isDependentContext()) {
17736 ME = SemaRef.MakeFullExpr(Arg: ME).get();
17737 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17738 ME = tryBuildCapture(SemaRef, Capture: ME, Captures).get();
17739 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
17740 }
17741
17742 // Convert array type to pointer type if needed.
17743 ME = SemaRef.DefaultFunctionArrayLvalueConversion(E: ME).get();
17744
17745 return new (getASTContext()) OMPMessageClause(
17746 ME, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
17747}
17748
17749OMPClause *SemaOpenMP::ActOnOpenMPOrderClause(
17750 OpenMPOrderClauseModifier Modifier, OpenMPOrderClauseKind Kind,
17751 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
17752 SourceLocation KindLoc, SourceLocation EndLoc) {
17753 if (Kind != OMPC_ORDER_concurrent ||
17754 (getLangOpts().OpenMP < 51 && MLoc.isValid())) {
17755 // Kind should be concurrent,
17756 // Modifiers introduced in OpenMP 5.1
17757 static_assert(OMPC_ORDER_unknown > 0,
17758 "OMPC_ORDER_unknown not greater than 0");
17759
17760 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
17761 << getListOfPossibleValues(K: OMPC_order,
17762 /*First=*/0,
17763 /*Last=*/OMPC_ORDER_unknown)
17764 << getOpenMPClauseNameForDiag(C: OMPC_order);
17765 return nullptr;
17766 }
17767 if (getLangOpts().OpenMP >= 51 && Modifier == OMPC_ORDER_MODIFIER_unknown &&
17768 MLoc.isValid()) {
17769 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
17770 << getListOfPossibleValues(K: OMPC_order,
17771 /*First=*/OMPC_ORDER_MODIFIER_unknown + 1,
17772 /*Last=*/OMPC_ORDER_MODIFIER_last)
17773 << getOpenMPClauseNameForDiag(C: OMPC_order);
17774 } else if (getLangOpts().OpenMP >= 50) {
17775 DSAStack->setRegionHasOrderConcurrent(/*HasOrderConcurrent=*/true);
17776 if (DSAStack->getCurScope()) {
17777 // mark the current scope with 'order' flag
17778 unsigned existingFlags = DSAStack->getCurScope()->getFlags();
17779 DSAStack->getCurScope()->setFlags(existingFlags |
17780 Scope::OpenMPOrderClauseScope);
17781 }
17782 }
17783 return new (getASTContext()) OMPOrderClause(
17784 Kind, KindLoc, StartLoc, LParenLoc, EndLoc, Modifier, MLoc);
17785}
17786
17787OMPClause *SemaOpenMP::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind,
17788 SourceLocation KindKwLoc,
17789 SourceLocation StartLoc,
17790 SourceLocation LParenLoc,
17791 SourceLocation EndLoc) {
17792 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source ||
17793 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) {
17794 SmallVector<unsigned> Except = {
17795 OMPC_DEPEND_source, OMPC_DEPEND_sink, OMPC_DEPEND_depobj,
17796 OMPC_DEPEND_outallmemory, OMPC_DEPEND_inoutallmemory};
17797 if (getLangOpts().OpenMP < 51)
17798 Except.push_back(Elt: OMPC_DEPEND_inoutset);
17799 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17800 << getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
17801 /*Last=*/OMPC_DEPEND_unknown, Exclude: Except)
17802 << getOpenMPClauseNameForDiag(C: OMPC_update);
17803 return nullptr;
17804 }
17805 return OMPUpdateClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17806 ArgumentLoc: KindKwLoc, DK: Kind, EndLoc);
17807}
17808
17809OMPClause *SemaOpenMP::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs,
17810 SourceLocation StartLoc,
17811 SourceLocation LParenLoc,
17812 SourceLocation EndLoc) {
17813 SmallVector<Expr *> SanitizedSizeExprs(SizeExprs);
17814
17815 for (Expr *&SizeExpr : SanitizedSizeExprs) {
17816 // Skip if already sanitized, e.g. during a partial template instantiation.
17817 if (!SizeExpr)
17818 continue;
17819
17820 bool IsValid = isNonNegativeIntegerValue(ValExpr&: SizeExpr, SemaRef, CKind: OMPC_sizes,
17821 /*StrictlyPositive=*/true);
17822
17823 // isNonNegativeIntegerValue returns true for non-integral types (but still
17824 // emits error diagnostic), so check for the expected type explicitly.
17825 QualType SizeTy = SizeExpr->getType();
17826 if (!SizeTy->isIntegerType())
17827 IsValid = false;
17828
17829 // Handling in templates is tricky. There are four possibilities to
17830 // consider:
17831 //
17832 // 1a. The expression is valid and we are in a instantiated template or not
17833 // in a template:
17834 // Pass valid expression to be further analysed later in Sema.
17835 // 1b. The expression is valid and we are in a template (including partial
17836 // instantiation):
17837 // isNonNegativeIntegerValue skipped any checks so there is no
17838 // guarantee it will be correct after instantiation.
17839 // ActOnOpenMPSizesClause will be called again at instantiation when
17840 // it is not in a dependent context anymore. This may cause warnings
17841 // to be emitted multiple times.
17842 // 2a. The expression is invalid and we are in an instantiated template or
17843 // not in a template:
17844 // Invalidate the expression with a clearly wrong value (nullptr) so
17845 // later in Sema we do not have to do the same validity analysis again
17846 // or crash from unexpected data. Error diagnostics have already been
17847 // emitted.
17848 // 2b. The expression is invalid and we are in a template (including partial
17849 // instantiation):
17850 // Pass the invalid expression as-is, template instantiation may
17851 // replace unexpected types/values with valid ones. The directives
17852 // with this clause must not try to use these expressions in dependent
17853 // contexts, but delay analysis until full instantiation.
17854 if (!SizeExpr->isInstantiationDependent() && !IsValid)
17855 SizeExpr = nullptr;
17856 }
17857
17858 return OMPSizesClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
17859 Sizes: SanitizedSizeExprs);
17860}
17861
17862OMPClause *SemaOpenMP::ActOnOpenMPPermutationClause(ArrayRef<Expr *> PermExprs,
17863 SourceLocation StartLoc,
17864 SourceLocation LParenLoc,
17865 SourceLocation EndLoc) {
17866 size_t NumLoops = PermExprs.size();
17867 SmallVector<Expr *> SanitizedPermExprs;
17868 llvm::append_range(C&: SanitizedPermExprs, R&: PermExprs);
17869
17870 for (Expr *&PermExpr : SanitizedPermExprs) {
17871 // Skip if template-dependent or already sanitized, e.g. during a partial
17872 // template instantiation.
17873 if (!PermExpr || PermExpr->isInstantiationDependent())
17874 continue;
17875
17876 llvm::APSInt PermVal;
17877 ExprResult PermEvalExpr = SemaRef.VerifyIntegerConstantExpression(
17878 E: PermExpr, Result: &PermVal, CanFold: AllowFoldKind::Allow);
17879 bool IsValid = PermEvalExpr.isUsable();
17880 if (IsValid)
17881 PermExpr = PermEvalExpr.get();
17882
17883 if (IsValid && (PermVal < 1 || NumLoops < PermVal)) {
17884 SourceRange ExprRange(PermEvalExpr.get()->getBeginLoc(),
17885 PermEvalExpr.get()->getEndLoc());
17886 Diag(Loc: PermEvalExpr.get()->getExprLoc(),
17887 DiagID: diag::err_omp_interchange_permutation_value_range)
17888 << NumLoops << ExprRange;
17889 IsValid = false;
17890 }
17891
17892 if (!PermExpr->isInstantiationDependent() && !IsValid)
17893 PermExpr = nullptr;
17894 }
17895
17896 return OMPPermutationClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17897 EndLoc, Args: SanitizedPermExprs);
17898}
17899
17900OMPClause *SemaOpenMP::ActOnOpenMPFullClause(SourceLocation StartLoc,
17901 SourceLocation EndLoc) {
17902 return OMPFullClause::Create(C: getASTContext(), StartLoc, EndLoc);
17903}
17904
17905OMPClause *SemaOpenMP::ActOnOpenMPPartialClause(Expr *FactorExpr,
17906 SourceLocation StartLoc,
17907 SourceLocation LParenLoc,
17908 SourceLocation EndLoc) {
17909 if (FactorExpr) {
17910 // If an argument is specified, it must be a constant (or an unevaluated
17911 // template expression).
17912 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause(
17913 E: FactorExpr, CKind: OMPC_partial, /*StrictlyPositive=*/true);
17914 if (FactorResult.isInvalid())
17915 return nullptr;
17916 FactorExpr = FactorResult.get();
17917 }
17918
17919 return OMPPartialClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
17920 Factor: FactorExpr);
17921}
17922
17923OMPClause *SemaOpenMP::ActOnOpenMPLoopRangeClause(
17924 Expr *First, Expr *Count, SourceLocation StartLoc, SourceLocation LParenLoc,
17925 SourceLocation FirstLoc, SourceLocation CountLoc, SourceLocation EndLoc) {
17926
17927 // OpenMP [6.0, Restrictions]
17928 // First and Count must be integer expressions with positive value
17929 ExprResult FirstVal =
17930 VerifyPositiveIntegerConstantInClause(E: First, CKind: OMPC_looprange);
17931 if (FirstVal.isInvalid())
17932 First = nullptr;
17933
17934 ExprResult CountVal =
17935 VerifyPositiveIntegerConstantInClause(E: Count, CKind: OMPC_looprange);
17936 if (CountVal.isInvalid())
17937 Count = nullptr;
17938
17939 // OpenMP [6.0, Restrictions]
17940 // first + count - 1 must not evaluate to a value greater than the
17941 // loop sequence length of the associated canonical loop sequence.
17942 // This check must be performed afterwards due to the delayed
17943 // parsing and computation of the associated loop sequence
17944 return OMPLoopRangeClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17945 FirstLoc, CountLoc, EndLoc, First, Count);
17946}
17947
17948OMPClause *SemaOpenMP::ActOnOpenMPAlignClause(Expr *A, SourceLocation StartLoc,
17949 SourceLocation LParenLoc,
17950 SourceLocation EndLoc) {
17951 ExprResult AlignVal;
17952 AlignVal = VerifyPositiveIntegerConstantInClause(E: A, CKind: OMPC_align);
17953 if (AlignVal.isInvalid())
17954 return nullptr;
17955 return OMPAlignClause::Create(C: getASTContext(), A: AlignVal.get(), StartLoc,
17956 LParenLoc, EndLoc);
17957}
17958
17959OMPClause *SemaOpenMP::ActOnOpenMPSingleExprWithArgClause(
17960 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
17961 SourceLocation StartLoc, SourceLocation LParenLoc,
17962 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
17963 SourceLocation EndLoc) {
17964 OMPClause *Res = nullptr;
17965 switch (Kind) {
17966 case OMPC_schedule: {
17967 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
17968 assert(Argument.size() == NumberOfElements &&
17969 ArgumentLoc.size() == NumberOfElements);
17970 Res = ActOnOpenMPScheduleClause(
17971 M1: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
17972 M2: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
17973 Kind: static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), ChunkSize: Expr,
17974 StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1], M2Loc: ArgumentLoc[Modifier2],
17975 KindLoc: ArgumentLoc[ScheduleKind], CommaLoc: DelimLoc, EndLoc);
17976 break;
17977 }
17978 case OMPC_if:
17979 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
17980 Res = ActOnOpenMPIfClause(NameModifier: static_cast<OpenMPDirectiveKind>(Argument.back()),
17981 Condition: Expr, StartLoc, LParenLoc, NameModifierLoc: ArgumentLoc.back(),
17982 ColonLoc: DelimLoc, EndLoc);
17983 break;
17984 case OMPC_dist_schedule:
17985 Res = ActOnOpenMPDistScheduleClause(
17986 Kind: static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), ChunkSize: Expr,
17987 StartLoc, LParenLoc, KindLoc: ArgumentLoc.back(), CommaLoc: DelimLoc, EndLoc);
17988 break;
17989 case OMPC_default:
17990 enum { DefaultModifier, DefaultVarCategory };
17991 Res = ActOnOpenMPDefaultClause(
17992 M: static_cast<llvm::omp::DefaultKind>(Argument[DefaultModifier]),
17993 MLoc: ArgumentLoc[DefaultModifier],
17994 VCKind: static_cast<OpenMPDefaultClauseVariableCategory>(
17995 Argument[DefaultVarCategory]),
17996 VCKindLoc: ArgumentLoc[DefaultVarCategory], StartLoc, LParenLoc, EndLoc);
17997 break;
17998 case OMPC_defaultmap:
17999 enum { Modifier, DefaultmapKind };
18000 Res = ActOnOpenMPDefaultmapClause(
18001 M: static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
18002 Kind: static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
18003 StartLoc, LParenLoc, MLoc: ArgumentLoc[Modifier], KindLoc: ArgumentLoc[DefaultmapKind],
18004 EndLoc);
18005 break;
18006 case OMPC_order:
18007 enum { OrderModifier, OrderKind };
18008 Res = ActOnOpenMPOrderClause(
18009 Modifier: static_cast<OpenMPOrderClauseModifier>(Argument[OrderModifier]),
18010 Kind: static_cast<OpenMPOrderClauseKind>(Argument[OrderKind]), StartLoc,
18011 LParenLoc, MLoc: ArgumentLoc[OrderModifier], KindLoc: ArgumentLoc[OrderKind], EndLoc);
18012 break;
18013 case OMPC_device:
18014 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
18015 Res = ActOnOpenMPDeviceClause(
18016 Modifier: static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Device: Expr,
18017 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
18018 break;
18019 case OMPC_grainsize:
18020 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
18021 "Modifier for grainsize clause and its location are expected.");
18022 Res = ActOnOpenMPGrainsizeClause(
18023 Modifier: static_cast<OpenMPGrainsizeClauseModifier>(Argument.back()), Size: Expr,
18024 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
18025 break;
18026 case OMPC_num_tasks:
18027 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
18028 "Modifier for num_tasks clause and its location are expected.");
18029 Res = ActOnOpenMPNumTasksClause(
18030 Modifier: static_cast<OpenMPNumTasksClauseModifier>(Argument.back()), NumTasks: Expr,
18031 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
18032 break;
18033 case OMPC_dyn_groupprivate: {
18034 enum { Modifier1, Modifier2, NumberOfElements };
18035 assert(Argument.size() == NumberOfElements &&
18036 ArgumentLoc.size() == NumberOfElements &&
18037 "Modifiers for dyn_groupprivate clause and their locations are "
18038 "expected.");
18039 Res = ActOnOpenMPDynGroupprivateClause(
18040 M1: static_cast<OpenMPDynGroupprivateClauseModifier>(Argument[Modifier1]),
18041 M2: static_cast<OpenMPDynGroupprivateClauseFallbackModifier>(
18042 Argument[Modifier2]),
18043 Size: Expr, StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1],
18044 M2Loc: ArgumentLoc[Modifier2], EndLoc);
18045 break;
18046 }
18047 case OMPC_num_threads:
18048 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
18049 "Modifier for num_threads clause and its location are expected.");
18050 Res = ActOnOpenMPNumThreadsClause(
18051 Modifier: static_cast<OpenMPNumThreadsClauseModifier>(Argument.back()), NumThreads: Expr,
18052 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
18053 break;
18054 case OMPC_final:
18055 case OMPC_safelen:
18056 case OMPC_simdlen:
18057 case OMPC_sizes:
18058 case OMPC_allocator:
18059 case OMPC_collapse:
18060 case OMPC_proc_bind:
18061 case OMPC_private:
18062 case OMPC_firstprivate:
18063 case OMPC_lastprivate:
18064 case OMPC_shared:
18065 case OMPC_reduction:
18066 case OMPC_task_reduction:
18067 case OMPC_in_reduction:
18068 case OMPC_linear:
18069 case OMPC_aligned:
18070 case OMPC_copyin:
18071 case OMPC_copyprivate:
18072 case OMPC_ordered:
18073 case OMPC_nowait:
18074 case OMPC_untied:
18075 case OMPC_mergeable:
18076 case OMPC_threadprivate:
18077 case OMPC_groupprivate:
18078 case OMPC_allocate:
18079 case OMPC_flush:
18080 case OMPC_depobj:
18081 case OMPC_read:
18082 case OMPC_write:
18083 case OMPC_update:
18084 case OMPC_capture:
18085 case OMPC_compare:
18086 case OMPC_seq_cst:
18087 case OMPC_acq_rel:
18088 case OMPC_acquire:
18089 case OMPC_release:
18090 case OMPC_relaxed:
18091 case OMPC_depend:
18092 case OMPC_threads:
18093 case OMPC_simd:
18094 case OMPC_map:
18095 case OMPC_num_teams:
18096 case OMPC_thread_limit:
18097 case OMPC_priority:
18098 case OMPC_nogroup:
18099 case OMPC_hint:
18100 case OMPC_unknown:
18101 case OMPC_uniform:
18102 case OMPC_to:
18103 case OMPC_from:
18104 case OMPC_use_device_ptr:
18105 case OMPC_use_device_addr:
18106 case OMPC_is_device_ptr:
18107 case OMPC_has_device_addr:
18108 case OMPC_unified_address:
18109 case OMPC_unified_shared_memory:
18110 case OMPC_reverse_offload:
18111 case OMPC_dynamic_allocators:
18112 case OMPC_atomic_default_mem_order:
18113 case OMPC_self_maps:
18114 case OMPC_device_type:
18115 case OMPC_match:
18116 case OMPC_nontemporal:
18117 case OMPC_at:
18118 case OMPC_severity:
18119 case OMPC_message:
18120 case OMPC_destroy:
18121 case OMPC_novariants:
18122 case OMPC_nocontext:
18123 case OMPC_detach:
18124 case OMPC_inclusive:
18125 case OMPC_exclusive:
18126 case OMPC_uses_allocators:
18127 case OMPC_affinity:
18128 case OMPC_when:
18129 case OMPC_bind:
18130 default:
18131 llvm_unreachable("Clause is not allowed.");
18132 }
18133 return Res;
18134}
18135
18136static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
18137 OpenMPScheduleClauseModifier M2,
18138 SourceLocation M1Loc, SourceLocation M2Loc) {
18139 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
18140 SmallVector<unsigned, 2> Excluded;
18141 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
18142 Excluded.push_back(Elt: M2);
18143 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
18144 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_monotonic);
18145 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
18146 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_nonmonotonic);
18147 S.Diag(Loc: M1Loc, DiagID: diag::err_omp_unexpected_clause_value)
18148 << getListOfPossibleValues(K: OMPC_schedule,
18149 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
18150 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
18151 Exclude: Excluded)
18152 << getOpenMPClauseNameForDiag(C: OMPC_schedule);
18153 return true;
18154 }
18155 return false;
18156}
18157
18158OMPClause *SemaOpenMP::ActOnOpenMPScheduleClause(
18159 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
18160 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
18161 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
18162 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
18163 if (checkScheduleModifiers(S&: SemaRef, M1, M2, M1Loc, M2Loc) ||
18164 checkScheduleModifiers(S&: SemaRef, M1: M2, M2: M1, M1Loc: M2Loc, M2Loc: M1Loc))
18165 return nullptr;
18166 // OpenMP, 2.7.1, Loop Construct, Restrictions
18167 // Either the monotonic modifier or the nonmonotonic modifier can be specified
18168 // but not both.
18169 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
18170 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
18171 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
18172 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
18173 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
18174 Diag(Loc: M2Loc, DiagID: diag::err_omp_unexpected_schedule_modifier)
18175 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M2)
18176 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M1);
18177 return nullptr;
18178 }
18179 if (Kind == OMPC_SCHEDULE_unknown) {
18180 std::string Values;
18181 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
18182 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
18183 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
18184 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
18185 Exclude);
18186 } else {
18187 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
18188 /*Last=*/OMPC_SCHEDULE_unknown);
18189 }
18190 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
18191 << Values << getOpenMPClauseNameForDiag(C: OMPC_schedule);
18192 return nullptr;
18193 }
18194 // OpenMP, 2.7.1, Loop Construct, Restrictions
18195 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
18196 // schedule(guided).
18197 // OpenMP 5.0 does not have this restriction.
18198 if (getLangOpts().OpenMP < 50 &&
18199 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
18200 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
18201 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
18202 Diag(Loc: M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
18203 DiagID: diag::err_omp_schedule_nonmonotonic_static);
18204 return nullptr;
18205 }
18206 Expr *ValExpr = ChunkSize;
18207 Stmt *HelperValStmt = nullptr;
18208 if (ChunkSize) {
18209 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
18210 !ChunkSize->isInstantiationDependent() &&
18211 !ChunkSize->containsUnexpandedParameterPack()) {
18212 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
18213 ExprResult Val =
18214 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
18215 if (Val.isInvalid())
18216 return nullptr;
18217
18218 ValExpr = Val.get();
18219
18220 // OpenMP [2.7.1, Restrictions]
18221 // chunk_size must be a loop invariant integer expression with a positive
18222 // value.
18223 if (std::optional<llvm::APSInt> Result =
18224 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
18225 if (Result->isSigned() && !Result->isStrictlyPositive()) {
18226 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
18227 << "schedule" << 1 << ChunkSize->getSourceRange();
18228 return nullptr;
18229 }
18230 } else if (getOpenMPCaptureRegionForClause(
18231 DSAStack->getCurrentDirective(), CKind: OMPC_schedule,
18232 OpenMPVersion: getLangOpts().OpenMP) != OMPD_unknown &&
18233 !SemaRef.CurContext->isDependentContext()) {
18234 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18235 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18236 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18237 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18238 }
18239 }
18240 }
18241
18242 return new (getASTContext())
18243 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
18244 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
18245}
18246
18247OMPClause *SemaOpenMP::ActOnOpenMPClause(OpenMPClauseKind Kind,
18248 SourceLocation StartLoc,
18249 SourceLocation EndLoc) {
18250 OMPClause *Res = nullptr;
18251 switch (Kind) {
18252 case OMPC_ordered:
18253 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
18254 break;
18255 case OMPC_nowait:
18256 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc,
18257 /*LParenLoc=*/SourceLocation(),
18258 /*Condition=*/nullptr);
18259 break;
18260 case OMPC_untied:
18261 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
18262 break;
18263 case OMPC_mergeable:
18264 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
18265 break;
18266 case OMPC_read:
18267 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
18268 break;
18269 case OMPC_write:
18270 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
18271 break;
18272 case OMPC_update:
18273 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
18274 break;
18275 case OMPC_capture:
18276 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
18277 break;
18278 case OMPC_compare:
18279 Res = ActOnOpenMPCompareClause(StartLoc, EndLoc);
18280 break;
18281 case OMPC_fail:
18282 Res = ActOnOpenMPFailClause(StartLoc, EndLoc);
18283 break;
18284 case OMPC_seq_cst:
18285 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
18286 break;
18287 case OMPC_acq_rel:
18288 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc);
18289 break;
18290 case OMPC_acquire:
18291 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc);
18292 break;
18293 case OMPC_release:
18294 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc);
18295 break;
18296 case OMPC_relaxed:
18297 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
18298 break;
18299 case OMPC_weak:
18300 Res = ActOnOpenMPWeakClause(StartLoc, EndLoc);
18301 break;
18302 case OMPC_threads:
18303 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
18304 break;
18305 case OMPC_simd:
18306 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
18307 break;
18308 case OMPC_nogroup:
18309 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
18310 break;
18311 case OMPC_unified_address:
18312 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
18313 break;
18314 case OMPC_unified_shared_memory:
18315 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
18316 break;
18317 case OMPC_reverse_offload:
18318 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
18319 break;
18320 case OMPC_dynamic_allocators:
18321 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
18322 break;
18323 case OMPC_self_maps:
18324 Res = ActOnOpenMPSelfMapsClause(StartLoc, EndLoc);
18325 break;
18326 case OMPC_destroy:
18327 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc,
18328 /*LParenLoc=*/SourceLocation(),
18329 /*VarLoc=*/SourceLocation(), EndLoc);
18330 break;
18331 case OMPC_full:
18332 Res = ActOnOpenMPFullClause(StartLoc, EndLoc);
18333 break;
18334 case OMPC_partial:
18335 Res = ActOnOpenMPPartialClause(FactorExpr: nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc);
18336 break;
18337 case OMPC_ompx_bare:
18338 Res = ActOnOpenMPXBareClause(StartLoc, EndLoc);
18339 break;
18340 case OMPC_if:
18341 case OMPC_final:
18342 case OMPC_num_threads:
18343 case OMPC_safelen:
18344 case OMPC_simdlen:
18345 case OMPC_sizes:
18346 case OMPC_allocator:
18347 case OMPC_collapse:
18348 case OMPC_schedule:
18349 case OMPC_private:
18350 case OMPC_firstprivate:
18351 case OMPC_lastprivate:
18352 case OMPC_shared:
18353 case OMPC_reduction:
18354 case OMPC_task_reduction:
18355 case OMPC_in_reduction:
18356 case OMPC_linear:
18357 case OMPC_aligned:
18358 case OMPC_copyin:
18359 case OMPC_copyprivate:
18360 case OMPC_default:
18361 case OMPC_proc_bind:
18362 case OMPC_threadprivate:
18363 case OMPC_groupprivate:
18364 case OMPC_allocate:
18365 case OMPC_flush:
18366 case OMPC_depobj:
18367 case OMPC_depend:
18368 case OMPC_device:
18369 case OMPC_map:
18370 case OMPC_num_teams:
18371 case OMPC_thread_limit:
18372 case OMPC_priority:
18373 case OMPC_grainsize:
18374 case OMPC_num_tasks:
18375 case OMPC_hint:
18376 case OMPC_dist_schedule:
18377 case OMPC_defaultmap:
18378 case OMPC_unknown:
18379 case OMPC_uniform:
18380 case OMPC_to:
18381 case OMPC_from:
18382 case OMPC_use_device_ptr:
18383 case OMPC_use_device_addr:
18384 case OMPC_is_device_ptr:
18385 case OMPC_has_device_addr:
18386 case OMPC_atomic_default_mem_order:
18387 case OMPC_device_type:
18388 case OMPC_match:
18389 case OMPC_nontemporal:
18390 case OMPC_order:
18391 case OMPC_at:
18392 case OMPC_severity:
18393 case OMPC_message:
18394 case OMPC_novariants:
18395 case OMPC_nocontext:
18396 case OMPC_detach:
18397 case OMPC_inclusive:
18398 case OMPC_exclusive:
18399 case OMPC_uses_allocators:
18400 case OMPC_affinity:
18401 case OMPC_when:
18402 case OMPC_ompx_dyn_cgroup_mem:
18403 case OMPC_dyn_groupprivate:
18404 default:
18405 llvm_unreachable("Clause is not allowed.");
18406 }
18407 return Res;
18408}
18409
18410OMPClause *SemaOpenMP::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
18411 SourceLocation EndLoc,
18412 SourceLocation LParenLoc,
18413 Expr *Condition) {
18414 Expr *ValExpr = Condition;
18415 if (Condition && LParenLoc.isValid()) {
18416 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18417 !Condition->isInstantiationDependent() &&
18418 !Condition->containsUnexpandedParameterPack()) {
18419 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18420 if (Val.isInvalid())
18421 return nullptr;
18422
18423 ValExpr = Val.get();
18424 }
18425 }
18426 DSAStack->setNowaitRegion();
18427 return new (getASTContext())
18428 OMPNowaitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
18429}
18430
18431OMPClause *SemaOpenMP::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
18432 SourceLocation EndLoc) {
18433 DSAStack->setUntiedRegion();
18434 return new (getASTContext()) OMPUntiedClause(StartLoc, EndLoc);
18435}
18436
18437OMPClause *SemaOpenMP::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
18438 SourceLocation EndLoc) {
18439 return new (getASTContext()) OMPMergeableClause(StartLoc, EndLoc);
18440}
18441
18442OMPClause *SemaOpenMP::ActOnOpenMPReadClause(SourceLocation StartLoc,
18443 SourceLocation EndLoc) {
18444 return new (getASTContext()) OMPReadClause(StartLoc, EndLoc);
18445}
18446
18447OMPClause *SemaOpenMP::ActOnOpenMPWriteClause(SourceLocation StartLoc,
18448 SourceLocation EndLoc) {
18449 return new (getASTContext()) OMPWriteClause(StartLoc, EndLoc);
18450}
18451
18452OMPClause *SemaOpenMP::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
18453 SourceLocation EndLoc) {
18454 return OMPUpdateClause::Create(C: getASTContext(), StartLoc, EndLoc);
18455}
18456
18457OMPClause *SemaOpenMP::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
18458 SourceLocation EndLoc) {
18459 return new (getASTContext()) OMPCaptureClause(StartLoc, EndLoc);
18460}
18461
18462OMPClause *SemaOpenMP::ActOnOpenMPCompareClause(SourceLocation StartLoc,
18463 SourceLocation EndLoc) {
18464 return new (getASTContext()) OMPCompareClause(StartLoc, EndLoc);
18465}
18466
18467OMPClause *SemaOpenMP::ActOnOpenMPFailClause(SourceLocation StartLoc,
18468 SourceLocation EndLoc) {
18469 return new (getASTContext()) OMPFailClause(StartLoc, EndLoc);
18470}
18471
18472OMPClause *SemaOpenMP::ActOnOpenMPFailClause(OpenMPClauseKind Parameter,
18473 SourceLocation KindLoc,
18474 SourceLocation StartLoc,
18475 SourceLocation LParenLoc,
18476 SourceLocation EndLoc) {
18477
18478 if (!checkFailClauseParameter(FailClauseParameter: Parameter)) {
18479 Diag(Loc: KindLoc, DiagID: diag::err_omp_atomic_fail_wrong_or_no_clauses);
18480 return nullptr;
18481 }
18482 return new (getASTContext())
18483 OMPFailClause(Parameter, KindLoc, StartLoc, LParenLoc, EndLoc);
18484}
18485
18486OMPClause *SemaOpenMP::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
18487 SourceLocation EndLoc) {
18488 return new (getASTContext()) OMPSeqCstClause(StartLoc, EndLoc);
18489}
18490
18491OMPClause *SemaOpenMP::ActOnOpenMPAcqRelClause(SourceLocation StartLoc,
18492 SourceLocation EndLoc) {
18493 return new (getASTContext()) OMPAcqRelClause(StartLoc, EndLoc);
18494}
18495
18496OMPClause *SemaOpenMP::ActOnOpenMPAcquireClause(SourceLocation StartLoc,
18497 SourceLocation EndLoc) {
18498 return new (getASTContext()) OMPAcquireClause(StartLoc, EndLoc);
18499}
18500
18501OMPClause *SemaOpenMP::ActOnOpenMPReleaseClause(SourceLocation StartLoc,
18502 SourceLocation EndLoc) {
18503 return new (getASTContext()) OMPReleaseClause(StartLoc, EndLoc);
18504}
18505
18506OMPClause *SemaOpenMP::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
18507 SourceLocation EndLoc) {
18508 return new (getASTContext()) OMPRelaxedClause(StartLoc, EndLoc);
18509}
18510
18511OMPClause *SemaOpenMP::ActOnOpenMPWeakClause(SourceLocation StartLoc,
18512 SourceLocation EndLoc) {
18513 return new (getASTContext()) OMPWeakClause(StartLoc, EndLoc);
18514}
18515
18516OMPClause *SemaOpenMP::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
18517 SourceLocation EndLoc) {
18518 return new (getASTContext()) OMPThreadsClause(StartLoc, EndLoc);
18519}
18520
18521OMPClause *SemaOpenMP::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
18522 SourceLocation EndLoc) {
18523 return new (getASTContext()) OMPSIMDClause(StartLoc, EndLoc);
18524}
18525
18526OMPClause *SemaOpenMP::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
18527 SourceLocation EndLoc) {
18528 return new (getASTContext()) OMPNogroupClause(StartLoc, EndLoc);
18529}
18530
18531OMPClause *SemaOpenMP::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
18532 SourceLocation EndLoc) {
18533 return new (getASTContext()) OMPUnifiedAddressClause(StartLoc, EndLoc);
18534}
18535
18536OMPClause *
18537SemaOpenMP::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
18538 SourceLocation EndLoc) {
18539 return new (getASTContext()) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
18540}
18541
18542OMPClause *SemaOpenMP::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
18543 SourceLocation EndLoc) {
18544 return new (getASTContext()) OMPReverseOffloadClause(StartLoc, EndLoc);
18545}
18546
18547OMPClause *
18548SemaOpenMP::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
18549 SourceLocation EndLoc) {
18550 return new (getASTContext()) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
18551}
18552
18553OMPClause *SemaOpenMP::ActOnOpenMPSelfMapsClause(SourceLocation StartLoc,
18554 SourceLocation EndLoc) {
18555 return new (getASTContext()) OMPSelfMapsClause(StartLoc, EndLoc);
18556}
18557
18558StmtResult
18559SemaOpenMP::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses,
18560 SourceLocation StartLoc,
18561 SourceLocation EndLoc) {
18562
18563 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18564 // At least one action-clause must appear on a directive.
18565 if (!hasClauses(Clauses, K: OMPC_init, ClauseTypes: OMPC_use, ClauseTypes: OMPC_destroy, ClauseTypes: OMPC_nowait)) {
18566 unsigned OMPVersion = getLangOpts().OpenMP;
18567 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'";
18568 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
18569 << Expected << getOpenMPDirectiveName(D: OMPD_interop, Ver: OMPVersion);
18570 return StmtError();
18571 }
18572
18573 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18574 // A depend clause can only appear on the directive if a targetsync
18575 // interop-type is present or the interop-var was initialized with
18576 // the targetsync interop-type.
18577
18578 // If there is any 'init' clause diagnose if there is no 'init' clause with
18579 // interop-type of 'targetsync'. Cases involving other directives cannot be
18580 // diagnosed.
18581 const OMPDependClause *DependClause = nullptr;
18582 bool HasInitClause = false;
18583 bool IsTargetSync = false;
18584 for (const OMPClause *C : Clauses) {
18585 if (IsTargetSync)
18586 break;
18587 if (const auto *InitClause = dyn_cast<OMPInitClause>(Val: C)) {
18588 HasInitClause = true;
18589 if (InitClause->getIsTargetSync())
18590 IsTargetSync = true;
18591 } else if (const auto *DC = dyn_cast<OMPDependClause>(Val: C)) {
18592 DependClause = DC;
18593 }
18594 }
18595 if (DependClause && HasInitClause && !IsTargetSync) {
18596 Diag(Loc: DependClause->getBeginLoc(), DiagID: diag::err_omp_interop_bad_depend_clause);
18597 return StmtError();
18598 }
18599
18600 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18601 // Each interop-var may be specified for at most one action-clause of each
18602 // interop construct.
18603 llvm::SmallPtrSet<const ValueDecl *, 4> InteropVars;
18604 for (OMPClause *C : Clauses) {
18605 OpenMPClauseKind ClauseKind = C->getClauseKind();
18606 std::pair<ValueDecl *, bool> DeclResult;
18607 SourceLocation ELoc;
18608 SourceRange ERange;
18609
18610 if (ClauseKind == OMPC_init) {
18611 auto *E = cast<OMPInitClause>(Val: C)->getInteropVar();
18612 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18613 } else if (ClauseKind == OMPC_use) {
18614 auto *E = cast<OMPUseClause>(Val: C)->getInteropVar();
18615 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18616 } else if (ClauseKind == OMPC_destroy) {
18617 auto *E = cast<OMPDestroyClause>(Val: C)->getInteropVar();
18618 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18619 }
18620
18621 if (DeclResult.first) {
18622 if (!InteropVars.insert(Ptr: DeclResult.first).second) {
18623 Diag(Loc: ELoc, DiagID: diag::err_omp_interop_var_multiple_actions)
18624 << DeclResult.first;
18625 return StmtError();
18626 }
18627 }
18628 }
18629
18630 return OMPInteropDirective::Create(C: getASTContext(), StartLoc, EndLoc,
18631 Clauses);
18632}
18633
18634static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr,
18635 SourceLocation VarLoc,
18636 OpenMPClauseKind Kind) {
18637 SourceLocation ELoc;
18638 SourceRange ERange;
18639 Expr *RefExpr = InteropVarExpr;
18640 auto Res = getPrivateItem(S&: SemaRef, RefExpr, ELoc, ERange,
18641 /*AllowArraySection=*/false,
18642 /*AllowAssumedSizeArray=*/false,
18643 /*DiagType=*/"omp_interop_t");
18644
18645 if (Res.second) {
18646 // It will be analyzed later.
18647 return true;
18648 }
18649
18650 if (!Res.first)
18651 return false;
18652
18653 // Interop variable should be of type omp_interop_t.
18654 bool HasError = false;
18655 QualType InteropType;
18656 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get(Name: "omp_interop_t"),
18657 VarLoc, Sema::LookupOrdinaryName);
18658 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
18659 NamedDecl *ND = Result.getFoundDecl();
18660 if (const auto *TD = dyn_cast<TypeDecl>(Val: ND)) {
18661 InteropType = QualType(TD->getTypeForDecl(), 0);
18662 } else {
18663 HasError = true;
18664 }
18665 } else {
18666 HasError = true;
18667 }
18668
18669 if (HasError) {
18670 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_implied_type_not_found)
18671 << "omp_interop_t";
18672 return false;
18673 }
18674
18675 QualType VarType = InteropVarExpr->getType().getUnqualifiedType();
18676 if (!SemaRef.Context.hasSameType(T1: InteropType, T2: VarType)) {
18677 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_wrong_type);
18678 return false;
18679 }
18680
18681 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18682 // The interop-var passed to init or destroy must be non-const.
18683 if ((Kind == OMPC_init || Kind == OMPC_destroy) &&
18684 isConstNotMutableType(SemaRef, Type: InteropVarExpr->getType())) {
18685 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_expected)
18686 << /*non-const*/ 1;
18687 return false;
18688 }
18689 return true;
18690}
18691
18692OMPClause *SemaOpenMP::ActOnOpenMPInitClause(
18693 Expr *InteropVar, OMPInteropInfo &InteropInfo, SourceLocation StartLoc,
18694 SourceLocation LParenLoc, SourceLocation VarLoc, SourceLocation EndLoc) {
18695
18696 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_init))
18697 return nullptr;
18698
18699 // Check prefer_type values. These foreign-runtime-id values are either
18700 // string literals or constant integral expressions.
18701 for (const Expr *E : InteropInfo.PreferTypes) {
18702 if (E->isValueDependent() || E->isTypeDependent() ||
18703 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
18704 continue;
18705 if (E->isIntegerConstantExpr(Ctx: getASTContext()))
18706 continue;
18707 if (isa<StringLiteral>(Val: E))
18708 continue;
18709 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_interop_prefer_type);
18710 return nullptr;
18711 }
18712
18713 return OMPInitClause::Create(C: getASTContext(), InteropVar, InteropInfo,
18714 StartLoc, LParenLoc, VarLoc, EndLoc);
18715}
18716
18717OMPClause *SemaOpenMP::ActOnOpenMPUseClause(Expr *InteropVar,
18718 SourceLocation StartLoc,
18719 SourceLocation LParenLoc,
18720 SourceLocation VarLoc,
18721 SourceLocation EndLoc) {
18722
18723 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_use))
18724 return nullptr;
18725
18726 return new (getASTContext())
18727 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
18728}
18729
18730OMPClause *SemaOpenMP::ActOnOpenMPDestroyClause(Expr *InteropVar,
18731 SourceLocation StartLoc,
18732 SourceLocation LParenLoc,
18733 SourceLocation VarLoc,
18734 SourceLocation EndLoc) {
18735 if (!InteropVar && getLangOpts().OpenMP >= 52 &&
18736 DSAStack->getCurrentDirective() == OMPD_depobj) {
18737 unsigned OMPVersion = getLangOpts().OpenMP;
18738 Diag(Loc: StartLoc, DiagID: diag::err_omp_expected_clause_argument)
18739 << getOpenMPClauseNameForDiag(C: OMPC_destroy)
18740 << getOpenMPDirectiveName(D: OMPD_depobj, Ver: OMPVersion);
18741 return nullptr;
18742 }
18743 if (InteropVar &&
18744 !isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_destroy))
18745 return nullptr;
18746
18747 return new (getASTContext())
18748 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
18749}
18750
18751OMPClause *SemaOpenMP::ActOnOpenMPNovariantsClause(Expr *Condition,
18752 SourceLocation StartLoc,
18753 SourceLocation LParenLoc,
18754 SourceLocation EndLoc) {
18755 Expr *ValExpr = Condition;
18756 Stmt *HelperValStmt = nullptr;
18757 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18758 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18759 !Condition->isInstantiationDependent() &&
18760 !Condition->containsUnexpandedParameterPack()) {
18761 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18762 if (Val.isInvalid())
18763 return nullptr;
18764
18765 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
18766
18767 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18768 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_novariants,
18769 OpenMPVersion: getLangOpts().OpenMP);
18770 if (CaptureRegion != OMPD_unknown &&
18771 !SemaRef.CurContext->isDependentContext()) {
18772 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18773 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18774 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18775 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18776 }
18777 }
18778
18779 return new (getASTContext()) OMPNovariantsClause(
18780 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18781}
18782
18783OMPClause *SemaOpenMP::ActOnOpenMPNocontextClause(Expr *Condition,
18784 SourceLocation StartLoc,
18785 SourceLocation LParenLoc,
18786 SourceLocation EndLoc) {
18787 Expr *ValExpr = Condition;
18788 Stmt *HelperValStmt = nullptr;
18789 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18790 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18791 !Condition->isInstantiationDependent() &&
18792 !Condition->containsUnexpandedParameterPack()) {
18793 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18794 if (Val.isInvalid())
18795 return nullptr;
18796
18797 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
18798
18799 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18800 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_nocontext,
18801 OpenMPVersion: getLangOpts().OpenMP);
18802 if (CaptureRegion != OMPD_unknown &&
18803 !SemaRef.CurContext->isDependentContext()) {
18804 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18805 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18806 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18807 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18808 }
18809 }
18810
18811 return new (getASTContext()) OMPNocontextClause(
18812 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18813}
18814
18815OMPClause *SemaOpenMP::ActOnOpenMPFilterClause(Expr *ThreadID,
18816 SourceLocation StartLoc,
18817 SourceLocation LParenLoc,
18818 SourceLocation EndLoc) {
18819 Expr *ValExpr = ThreadID;
18820 Stmt *HelperValStmt = nullptr;
18821
18822 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18823 OpenMPDirectiveKind CaptureRegion =
18824 getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_filter, OpenMPVersion: getLangOpts().OpenMP);
18825 if (CaptureRegion != OMPD_unknown &&
18826 !SemaRef.CurContext->isDependentContext()) {
18827 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18828 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18829 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18830 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18831 }
18832
18833 return new (getASTContext()) OMPFilterClause(
18834 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18835}
18836
18837OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
18838 ArrayRef<Expr *> VarList,
18839 const OMPVarListLocTy &Locs,
18840 OpenMPVarListDataTy &Data) {
18841 SourceLocation StartLoc = Locs.StartLoc;
18842 SourceLocation LParenLoc = Locs.LParenLoc;
18843 SourceLocation EndLoc = Locs.EndLoc;
18844 OMPClause *Res = nullptr;
18845 int ExtraModifier = Data.ExtraModifier;
18846 int OriginalSharingModifier = Data.OriginalSharingModifier;
18847 SourceLocation ExtraModifierLoc = Data.ExtraModifierLoc;
18848 SourceLocation ColonLoc = Data.ColonLoc;
18849 switch (Kind) {
18850 case OMPC_private:
18851 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18852 break;
18853 case OMPC_firstprivate:
18854 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18855 break;
18856 case OMPC_lastprivate:
18857 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown &&
18858 "Unexpected lastprivate modifier.");
18859 Res = ActOnOpenMPLastprivateClause(
18860 VarList, LPKind: static_cast<OpenMPLastprivateModifier>(ExtraModifier),
18861 LPKindLoc: ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc);
18862 break;
18863 case OMPC_shared:
18864 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
18865 break;
18866 case OMPC_reduction:
18867 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown &&
18868 "Unexpected lastprivate modifier.");
18869 Res = ActOnOpenMPReductionClause(
18870 VarList,
18871 Modifiers: OpenMPVarListDataTy::OpenMPReductionClauseModifiers(
18872 ExtraModifier, OriginalSharingModifier),
18873 StartLoc, LParenLoc, ModifierLoc: ExtraModifierLoc, ColonLoc, EndLoc,
18874 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18875 break;
18876 case OMPC_task_reduction:
18877 Res = ActOnOpenMPTaskReductionClause(
18878 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
18879 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18880 break;
18881 case OMPC_in_reduction:
18882 Res = ActOnOpenMPInReductionClause(
18883 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
18884 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18885 break;
18886 case OMPC_linear:
18887 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown &&
18888 "Unexpected linear modifier.");
18889 Res = ActOnOpenMPLinearClause(
18890 VarList, Step: Data.DepModOrTailExpr, StartLoc, LParenLoc,
18891 LinKind: static_cast<OpenMPLinearClauseKind>(ExtraModifier), LinLoc: ExtraModifierLoc,
18892 ColonLoc, StepModifierLoc: Data.StepModifierLoc, EndLoc);
18893 break;
18894 case OMPC_aligned:
18895 Res = ActOnOpenMPAlignedClause(VarList, Alignment: Data.DepModOrTailExpr, StartLoc,
18896 LParenLoc, ColonLoc, EndLoc);
18897 break;
18898 case OMPC_copyin:
18899 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
18900 break;
18901 case OMPC_copyprivate:
18902 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18903 break;
18904 case OMPC_flush:
18905 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
18906 break;
18907 case OMPC_depend:
18908 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown &&
18909 "Unexpected depend modifier.");
18910 Res = ActOnOpenMPDependClause(
18911 Data: {.DepKind: static_cast<OpenMPDependClauseKind>(ExtraModifier), .DepLoc: ExtraModifierLoc,
18912 .ColonLoc: ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc},
18913 DepModifier: Data.DepModOrTailExpr, VarList, StartLoc, LParenLoc, EndLoc);
18914 break;
18915 case OMPC_map:
18916 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown &&
18917 "Unexpected map modifier.");
18918 Res = ActOnOpenMPMapClause(
18919 IteratorModifier: Data.IteratorExpr, MapTypeModifiers: Data.MapTypeModifiers, MapTypeModifiersLoc: Data.MapTypeModifiersLoc,
18920 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId,
18921 MapType: static_cast<OpenMPMapClauseKind>(ExtraModifier), IsMapTypeImplicit: Data.IsMapTypeImplicit,
18922 MapLoc: ExtraModifierLoc, ColonLoc, VarList, Locs);
18923 break;
18924 case OMPC_to:
18925 Res = ActOnOpenMPToClause(
18926 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
18927 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
18928 VarList, Locs);
18929 break;
18930 case OMPC_from:
18931 Res = ActOnOpenMPFromClause(
18932 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
18933 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
18934 VarList, Locs);
18935 break;
18936 case OMPC_use_device_ptr:
18937 assert(0 <= Data.ExtraModifier &&
18938 Data.ExtraModifier <= OMPC_USE_DEVICE_PTR_FALLBACK_unknown &&
18939 "Unexpected use_device_ptr fallback modifier.");
18940 Res = ActOnOpenMPUseDevicePtrClause(
18941 VarList, Locs,
18942 FallbackModifier: static_cast<OpenMPUseDevicePtrFallbackModifier>(Data.ExtraModifier),
18943 FallbackModifierLoc: Data.ExtraModifierLoc);
18944 break;
18945 case OMPC_use_device_addr:
18946 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs);
18947 break;
18948 case OMPC_is_device_ptr:
18949 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
18950 break;
18951 case OMPC_has_device_addr:
18952 Res = ActOnOpenMPHasDeviceAddrClause(VarList, Locs);
18953 break;
18954 case OMPC_allocate: {
18955 OpenMPAllocateClauseModifier Modifier1 = OMPC_ALLOCATE_unknown;
18956 OpenMPAllocateClauseModifier Modifier2 = OMPC_ALLOCATE_unknown;
18957 SourceLocation Modifier1Loc, Modifier2Loc;
18958 if (!Data.AllocClauseModifiers.empty()) {
18959 assert(Data.AllocClauseModifiers.size() <= 2 &&
18960 "More allocate modifiers than expected");
18961 Modifier1 = Data.AllocClauseModifiers[0];
18962 Modifier1Loc = Data.AllocClauseModifiersLoc[0];
18963 if (Data.AllocClauseModifiers.size() == 2) {
18964 Modifier2 = Data.AllocClauseModifiers[1];
18965 Modifier2Loc = Data.AllocClauseModifiersLoc[1];
18966 }
18967 }
18968 Res = ActOnOpenMPAllocateClause(
18969 Allocator: Data.DepModOrTailExpr, Alignment: Data.AllocateAlignment, FirstModifier: Modifier1, FirstModifierLoc: Modifier1Loc,
18970 SecondModifier: Modifier2, SecondModifierLoc: Modifier2Loc, VarList, StartLoc, ColonLoc: LParenLoc, LParenLoc: ColonLoc,
18971 EndLoc);
18972 break;
18973 }
18974 case OMPC_nontemporal:
18975 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc);
18976 break;
18977 case OMPC_inclusive:
18978 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
18979 break;
18980 case OMPC_exclusive:
18981 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
18982 break;
18983 case OMPC_affinity:
18984 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc,
18985 Modifier: Data.DepModOrTailExpr, Locators: VarList);
18986 break;
18987 case OMPC_doacross:
18988 Res = ActOnOpenMPDoacrossClause(
18989 DepType: static_cast<OpenMPDoacrossClauseModifier>(ExtraModifier),
18990 DepLoc: ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc);
18991 break;
18992 case OMPC_num_teams:
18993 Res = ActOnOpenMPNumTeamsClause(VarList, StartLoc, LParenLoc, EndLoc);
18994 break;
18995 case OMPC_thread_limit:
18996 Res = ActOnOpenMPThreadLimitClause(VarList, StartLoc, LParenLoc, EndLoc);
18997 break;
18998 case OMPC_if:
18999 case OMPC_depobj:
19000 case OMPC_final:
19001 case OMPC_num_threads:
19002 case OMPC_safelen:
19003 case OMPC_simdlen:
19004 case OMPC_sizes:
19005 case OMPC_allocator:
19006 case OMPC_collapse:
19007 case OMPC_default:
19008 case OMPC_proc_bind:
19009 case OMPC_schedule:
19010 case OMPC_ordered:
19011 case OMPC_nowait:
19012 case OMPC_untied:
19013 case OMPC_mergeable:
19014 case OMPC_threadprivate:
19015 case OMPC_groupprivate:
19016 case OMPC_read:
19017 case OMPC_write:
19018 case OMPC_update:
19019 case OMPC_capture:
19020 case OMPC_compare:
19021 case OMPC_seq_cst:
19022 case OMPC_acq_rel:
19023 case OMPC_acquire:
19024 case OMPC_release:
19025 case OMPC_relaxed:
19026 case OMPC_device:
19027 case OMPC_threads:
19028 case OMPC_simd:
19029 case OMPC_priority:
19030 case OMPC_grainsize:
19031 case OMPC_nogroup:
19032 case OMPC_num_tasks:
19033 case OMPC_hint:
19034 case OMPC_dist_schedule:
19035 case OMPC_defaultmap:
19036 case OMPC_unknown:
19037 case OMPC_uniform:
19038 case OMPC_unified_address:
19039 case OMPC_unified_shared_memory:
19040 case OMPC_reverse_offload:
19041 case OMPC_dynamic_allocators:
19042 case OMPC_atomic_default_mem_order:
19043 case OMPC_self_maps:
19044 case OMPC_device_type:
19045 case OMPC_match:
19046 case OMPC_order:
19047 case OMPC_at:
19048 case OMPC_severity:
19049 case OMPC_message:
19050 case OMPC_destroy:
19051 case OMPC_novariants:
19052 case OMPC_nocontext:
19053 case OMPC_detach:
19054 case OMPC_uses_allocators:
19055 case OMPC_when:
19056 case OMPC_bind:
19057 default:
19058 llvm_unreachable("Clause is not allowed.");
19059 }
19060 return Res;
19061}
19062
19063ExprResult SemaOpenMP::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
19064 ExprObjectKind OK,
19065 SourceLocation Loc) {
19066 ExprResult Res = SemaRef.BuildDeclRefExpr(
19067 D: Capture, Ty: Capture->getType().getNonReferenceType(), VK: VK_LValue, Loc);
19068 if (!Res.isUsable())
19069 return ExprError();
19070 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
19071 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: Loc, Opc: UO_Deref, InputExpr: Res.get());
19072 if (!Res.isUsable())
19073 return ExprError();
19074 }
19075 if (VK != VK_LValue && Res.get()->isGLValue()) {
19076 Res = SemaRef.DefaultLvalueConversion(E: Res.get());
19077 if (!Res.isUsable())
19078 return ExprError();
19079 }
19080 return Res;
19081}
19082
19083OMPClause *SemaOpenMP::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
19084 SourceLocation StartLoc,
19085 SourceLocation LParenLoc,
19086 SourceLocation EndLoc) {
19087 SmallVector<Expr *, 8> Vars;
19088 SmallVector<Expr *, 8> PrivateCopies;
19089 unsigned OMPVersion = getLangOpts().OpenMP;
19090 bool IsImplicitClause =
19091 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
19092 for (Expr *RefExpr : VarList) {
19093 assert(RefExpr && "NULL expr in OpenMP private clause.");
19094 SourceLocation ELoc;
19095 SourceRange ERange;
19096 Expr *SimpleRefExpr = RefExpr;
19097 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19098 if (Res.second) {
19099 // It will be analyzed later.
19100 Vars.push_back(Elt: RefExpr);
19101 PrivateCopies.push_back(Elt: nullptr);
19102 }
19103 ValueDecl *D = Res.first;
19104 if (!D)
19105 continue;
19106
19107 QualType Type = D->getType();
19108 auto *VD = dyn_cast<VarDecl>(Val: D);
19109
19110 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
19111 // A variable that appears in a private clause must not have an incomplete
19112 // type or a reference type.
19113 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19114 DiagID: diag::err_omp_private_incomplete_type))
19115 continue;
19116 Type = Type.getNonReferenceType();
19117
19118 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
19119 // A variable that is privatized must not have a const-qualified type
19120 // unless it is of class type with a mutable member. This restriction does
19121 // not apply to the firstprivate clause.
19122 //
19123 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
19124 // A variable that appears in a private clause must not have a
19125 // const-qualified type unless it is of class type with a mutable member.
19126 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_private, ELoc))
19127 continue;
19128
19129 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19130 // in a Construct]
19131 // Variables with the predetermined data-sharing attributes may not be
19132 // listed in data-sharing attributes clauses, except for the cases
19133 // listed below. For these exceptions only, listing a predetermined
19134 // variable in a data-sharing attribute clause is allowed and overrides
19135 // the variable's predetermined data-sharing attributes.
19136 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19137 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
19138 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19139 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19140 << getOpenMPClauseNameForDiag(C: OMPC_private);
19141 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19142 continue;
19143 }
19144
19145 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19146 // Variably modified types are not supported for tasks.
19147 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
19148 isOpenMPTaskingDirective(Kind: CurrDir)) {
19149 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
19150 << getOpenMPClauseNameForDiag(C: OMPC_private) << Type
19151 << getOpenMPDirectiveName(D: CurrDir, Ver: OMPVersion);
19152 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19153 VarDecl::DeclarationOnly;
19154 Diag(Loc: D->getLocation(),
19155 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19156 << D;
19157 continue;
19158 }
19159
19160 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19161 // A list item cannot appear in both a map clause and a data-sharing
19162 // attribute clause on the same construct
19163 //
19164 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19165 // A list item cannot appear in both a map clause and a data-sharing
19166 // attribute clause on the same construct unless the construct is a
19167 // combined construct.
19168 if ((getLangOpts().OpenMP <= 45 &&
19169 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
19170 CurrDir == OMPD_target) {
19171 OpenMPClauseKind ConflictKind;
19172 if (DSAStack->checkMappableExprComponentListsForDecl(
19173 VD, /*CurrentRegionOnly=*/true,
19174 Check: [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
19175 OpenMPClauseKind WhereFoundClauseKind) -> bool {
19176 ConflictKind = WhereFoundClauseKind;
19177 return true;
19178 })) {
19179 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
19180 << getOpenMPClauseNameForDiag(C: OMPC_private)
19181 << getOpenMPClauseNameForDiag(C: ConflictKind)
19182 << getOpenMPDirectiveName(D: CurrDir, Ver: OMPVersion);
19183 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19184 continue;
19185 }
19186 }
19187
19188 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
19189 // A variable of class type (or array thereof) that appears in a private
19190 // clause requires an accessible, unambiguous default constructor for the
19191 // class type.
19192 // Generate helper private variable and initialize it with the default
19193 // value. The address of the original variable is replaced by the address of
19194 // the new private variable in CodeGen. This new variable is not added to
19195 // IdResolver, so the code in the OpenMP region uses original variable for
19196 // proper diagnostics.
19197 Type = Type.getUnqualifiedType();
19198 VarDecl *VDPrivate =
19199 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
19200 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
19201 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
19202 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
19203 if (VDPrivate->isInvalidDecl())
19204 continue;
19205 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
19206 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
19207
19208 DeclRefExpr *Ref = nullptr;
19209 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19210 auto *FD = dyn_cast<FieldDecl>(Val: D);
19211 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
19212 if (VD)
19213 Ref = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
19214 Loc: RefExpr->getExprLoc());
19215 else
19216 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
19217 }
19218 if (!IsImplicitClause)
19219 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_private, PrivateCopy: Ref);
19220 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19221 ? RefExpr->IgnoreParens()
19222 : Ref);
19223 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
19224 }
19225
19226 if (Vars.empty())
19227 return nullptr;
19228
19229 return OMPPrivateClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
19230 VL: Vars, PrivateVL: PrivateCopies);
19231}
19232
19233OMPClause *SemaOpenMP::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
19234 SourceLocation StartLoc,
19235 SourceLocation LParenLoc,
19236 SourceLocation EndLoc) {
19237 SmallVector<Expr *, 8> Vars;
19238 SmallVector<Expr *, 8> PrivateCopies;
19239 SmallVector<Expr *, 8> Inits;
19240 SmallVector<Decl *, 4> ExprCaptures;
19241 bool IsImplicitClause =
19242 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
19243 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
19244 unsigned OMPVersion = getLangOpts().OpenMP;
19245
19246 for (Expr *RefExpr : VarList) {
19247 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
19248 SourceLocation ELoc;
19249 SourceRange ERange;
19250 Expr *SimpleRefExpr = RefExpr;
19251 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19252 if (Res.second) {
19253 // It will be analyzed later.
19254 Vars.push_back(Elt: RefExpr);
19255 PrivateCopies.push_back(Elt: nullptr);
19256 Inits.push_back(Elt: nullptr);
19257 }
19258 ValueDecl *D = Res.first;
19259 if (!D)
19260 continue;
19261
19262 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
19263 QualType Type = D->getType();
19264 auto *VD = dyn_cast<VarDecl>(Val: D);
19265
19266 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
19267 // A variable that appears in a private clause must not have an incomplete
19268 // type or a reference type.
19269 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19270 DiagID: diag::err_omp_firstprivate_incomplete_type))
19271 continue;
19272 Type = Type.getNonReferenceType();
19273
19274 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
19275 // A variable of class type (or array thereof) that appears in a private
19276 // clause requires an accessible, unambiguous copy constructor for the
19277 // class type.
19278 QualType ElemType =
19279 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
19280
19281 // If an implicit firstprivate variable found it was checked already.
19282 DSAStackTy::DSAVarData TopDVar;
19283 if (!IsImplicitClause) {
19284 DSAStackTy::DSAVarData DVar =
19285 DSAStack->getTopDSA(D, /*FromParent=*/false);
19286 TopDVar = DVar;
19287 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19288 bool IsConstant = ElemType.isConstant(Ctx: getASTContext());
19289 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
19290 // A list item that specifies a given variable may not appear in more
19291 // than one clause on the same directive, except that a variable may be
19292 // specified in both firstprivate and lastprivate clauses.
19293 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
19294 // A list item may appear in a firstprivate or lastprivate clause but not
19295 // both.
19296 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
19297 (isOpenMPDistributeDirective(DKind: CurrDir) ||
19298 DVar.CKind != OMPC_lastprivate) &&
19299 DVar.RefExpr) {
19300 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19301 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19302 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
19303 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19304 continue;
19305 }
19306
19307 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19308 // in a Construct]
19309 // Variables with the predetermined data-sharing attributes may not be
19310 // listed in data-sharing attributes clauses, except for the cases
19311 // listed below. For these exceptions only, listing a predetermined
19312 // variable in a data-sharing attribute clause is allowed and overrides
19313 // the variable's predetermined data-sharing attributes.
19314 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19315 // in a Construct, C/C++, p.2]
19316 // Variables with const-qualified type having no mutable member may be
19317 // listed in a firstprivate clause, even if they are static data members.
19318 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
19319 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
19320 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19321 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19322 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
19323 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19324 continue;
19325 }
19326
19327 // OpenMP [2.9.3.4, Restrictions, p.2]
19328 // A list item that is private within a parallel region must not appear
19329 // in a firstprivate clause on a worksharing construct if any of the
19330 // worksharing regions arising from the worksharing construct ever bind
19331 // to any of the parallel regions arising from the parallel construct.
19332 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
19333 // A list item that is private within a teams region must not appear in a
19334 // firstprivate clause on a distribute construct if any of the distribute
19335 // regions arising from the distribute construct ever bind to any of the
19336 // teams regions arising from the teams construct.
19337 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
19338 // A list item that appears in a reduction clause of a teams construct
19339 // must not appear in a firstprivate clause on a distribute construct if
19340 // any of the distribute regions arising from the distribute construct
19341 // ever bind to any of the teams regions arising from the teams construct.
19342 if ((isOpenMPWorksharingDirective(DKind: CurrDir) ||
19343 isOpenMPDistributeDirective(DKind: CurrDir)) &&
19344 !isOpenMPParallelDirective(DKind: CurrDir) &&
19345 !isOpenMPTeamsDirective(DKind: CurrDir)) {
19346 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
19347 if (DVar.CKind != OMPC_shared &&
19348 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
19349 isOpenMPTeamsDirective(DKind: DVar.DKind) ||
19350 DVar.DKind == OMPD_unknown)) {
19351 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
19352 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
19353 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19354 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19355 continue;
19356 }
19357 }
19358 // OpenMP [2.9.3.4, Restrictions, p.3]
19359 // A list item that appears in a reduction clause of a parallel construct
19360 // must not appear in a firstprivate clause on a worksharing or task
19361 // construct if any of the worksharing or task regions arising from the
19362 // worksharing or task construct ever bind to any of the parallel regions
19363 // arising from the parallel construct.
19364 // OpenMP [2.9.3.4, Restrictions, p.4]
19365 // A list item that appears in a reduction clause in worksharing
19366 // construct must not appear in a firstprivate clause in a task construct
19367 // encountered during execution of any of the worksharing regions arising
19368 // from the worksharing construct.
19369 if (isOpenMPTaskingDirective(Kind: CurrDir)) {
19370 DVar = DSAStack->hasInnermostDSA(
19371 D,
19372 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
19373 return C == OMPC_reduction && !AppliedToPointee;
19374 },
19375 DPred: [](OpenMPDirectiveKind K) {
19376 return isOpenMPParallelDirective(DKind: K) ||
19377 isOpenMPWorksharingDirective(DKind: K) ||
19378 isOpenMPTeamsDirective(DKind: K);
19379 },
19380 /*FromParent=*/true);
19381 if (DVar.CKind == OMPC_reduction &&
19382 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
19383 isOpenMPWorksharingDirective(DKind: DVar.DKind) ||
19384 isOpenMPTeamsDirective(DKind: DVar.DKind))) {
19385 Diag(Loc: ELoc, DiagID: diag::err_omp_parallel_reduction_in_task_firstprivate)
19386 << getOpenMPDirectiveName(D: DVar.DKind, Ver: OMPVersion);
19387 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19388 continue;
19389 }
19390 }
19391
19392 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19393 // A list item cannot appear in both a map clause and a data-sharing
19394 // attribute clause on the same construct
19395 //
19396 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19397 // A list item cannot appear in both a map clause and a data-sharing
19398 // attribute clause on the same construct unless the construct is a
19399 // combined construct.
19400 if ((getLangOpts().OpenMP <= 45 &&
19401 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
19402 CurrDir == OMPD_target) {
19403 OpenMPClauseKind ConflictKind;
19404 if (DSAStack->checkMappableExprComponentListsForDecl(
19405 VD, /*CurrentRegionOnly=*/true,
19406 Check: [&ConflictKind](
19407 OMPClauseMappableExprCommon::MappableExprComponentListRef,
19408 OpenMPClauseKind WhereFoundClauseKind) {
19409 ConflictKind = WhereFoundClauseKind;
19410 return true;
19411 })) {
19412 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
19413 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
19414 << getOpenMPClauseNameForDiag(C: ConflictKind)
19415 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
19416 Ver: OMPVersion);
19417 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19418 continue;
19419 }
19420 }
19421 }
19422
19423 // Variably modified types are not supported for tasks.
19424 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
19425 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
19426 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
19427 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate) << Type
19428 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
19429 Ver: OMPVersion);
19430 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19431 VarDecl::DeclarationOnly;
19432 Diag(Loc: D->getLocation(),
19433 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19434 << D;
19435 continue;
19436 }
19437
19438 Type = Type.getUnqualifiedType();
19439 VarDecl *VDPrivate =
19440 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
19441 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
19442 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
19443 // Generate helper private variable and initialize it with the value of the
19444 // original variable. The address of the original variable is replaced by
19445 // the address of the new private variable in the CodeGen. This new variable
19446 // is not added to IdResolver, so the code in the OpenMP region uses
19447 // original variable for proper diagnostics and variable capturing.
19448 Expr *VDInitRefExpr = nullptr;
19449 // For arrays generate initializer for single element and replace it by the
19450 // original array element in CodeGen.
19451 if (Type->isArrayType()) {
19452 VarDecl *VDInit =
19453 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type: ElemType, Name: D->getName());
19454 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: ElemType, Loc: ELoc);
19455 Expr *Init = SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get();
19456 ElemType = ElemType.getUnqualifiedType();
19457 VarDecl *VDInitTemp = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(),
19458 Type: ElemType, Name: ".firstprivate.temp");
19459 InitializedEntity Entity =
19460 InitializedEntity::InitializeVariable(Var: VDInitTemp);
19461 InitializationKind Kind = InitializationKind::CreateCopy(InitLoc: ELoc, EqualLoc: ELoc);
19462
19463 InitializationSequence InitSeq(SemaRef, Entity, Kind, Init);
19464 ExprResult Result = InitSeq.Perform(S&: SemaRef, Entity, Kind, Args: Init);
19465 if (Result.isInvalid())
19466 VDPrivate->setInvalidDecl();
19467 else
19468 VDPrivate->setInit(Result.getAs<Expr>());
19469 // Remove temp variable declaration.
19470 getASTContext().Deallocate(Ptr: VDInitTemp);
19471 } else {
19472 VarDecl *VDInit = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type,
19473 Name: ".firstprivate.temp");
19474 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: RefExpr->getType(),
19475 Loc: RefExpr->getExprLoc());
19476 SemaRef.AddInitializerToDecl(
19477 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
19478 /*DirectInit=*/false);
19479 }
19480 if (VDPrivate->isInvalidDecl()) {
19481 if (IsImplicitClause) {
19482 Diag(Loc: RefExpr->getExprLoc(),
19483 DiagID: diag::note_omp_task_predetermined_firstprivate_here);
19484 }
19485 continue;
19486 }
19487 SemaRef.CurContext->addDecl(D: VDPrivate);
19488 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
19489 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(),
19490 Loc: RefExpr->getExprLoc());
19491 DeclRefExpr *Ref = nullptr;
19492 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19493 if (TopDVar.CKind == OMPC_lastprivate) {
19494 Ref = TopDVar.PrivateCopy;
19495 } else {
19496 auto *FD = dyn_cast<FieldDecl>(Val: D);
19497 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
19498 if (VD)
19499 Ref =
19500 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
19501 Loc: RefExpr->getExprLoc());
19502 else
19503 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
19504 if (VD || !isOpenMPCapturedDecl(D))
19505 ExprCaptures.push_back(Elt: Ref->getDecl());
19506 }
19507 }
19508 if (!IsImplicitClause)
19509 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
19510 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19511 ? RefExpr->IgnoreParens()
19512 : Ref);
19513 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
19514 Inits.push_back(Elt: VDInitRefExpr);
19515 }
19516
19517 if (Vars.empty())
19518 return nullptr;
19519
19520 return OMPFirstprivateClause::Create(
19521 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, PrivateVL: PrivateCopies, InitVL: Inits,
19522 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures));
19523}
19524
19525OMPClause *SemaOpenMP::ActOnOpenMPLastprivateClause(
19526 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind,
19527 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc,
19528 SourceLocation LParenLoc, SourceLocation EndLoc) {
19529 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) {
19530 assert(ColonLoc.isValid() && "Colon location must be valid.");
19531 Diag(Loc: LPKindLoc, DiagID: diag::err_omp_unexpected_clause_value)
19532 << getListOfPossibleValues(K: OMPC_lastprivate, /*First=*/0,
19533 /*Last=*/OMPC_LASTPRIVATE_unknown)
19534 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
19535 return nullptr;
19536 }
19537
19538 SmallVector<Expr *, 8> Vars;
19539 SmallVector<Expr *, 8> SrcExprs;
19540 SmallVector<Expr *, 8> DstExprs;
19541 SmallVector<Expr *, 8> AssignmentOps;
19542 SmallVector<Decl *, 4> ExprCaptures;
19543 SmallVector<Expr *, 4> ExprPostUpdates;
19544 for (Expr *RefExpr : VarList) {
19545 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
19546 SourceLocation ELoc;
19547 SourceRange ERange;
19548 Expr *SimpleRefExpr = RefExpr;
19549 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19550 if (Res.second) {
19551 // It will be analyzed later.
19552 Vars.push_back(Elt: RefExpr);
19553 SrcExprs.push_back(Elt: nullptr);
19554 DstExprs.push_back(Elt: nullptr);
19555 AssignmentOps.push_back(Elt: nullptr);
19556 }
19557 ValueDecl *D = Res.first;
19558 if (!D)
19559 continue;
19560
19561 QualType Type = D->getType();
19562 auto *VD = dyn_cast<VarDecl>(Val: D);
19563
19564 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
19565 // A variable that appears in a lastprivate clause must not have an
19566 // incomplete type or a reference type.
19567 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19568 DiagID: diag::err_omp_lastprivate_incomplete_type))
19569 continue;
19570 Type = Type.getNonReferenceType();
19571
19572 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
19573 // A variable that is privatized must not have a const-qualified type
19574 // unless it is of class type with a mutable member. This restriction does
19575 // not apply to the firstprivate clause.
19576 //
19577 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
19578 // A variable that appears in a lastprivate clause must not have a
19579 // const-qualified type unless it is of class type with a mutable member.
19580 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_lastprivate, ELoc))
19581 continue;
19582
19583 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions]
19584 // A list item that appears in a lastprivate clause with the conditional
19585 // modifier must be a scalar variable.
19586 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) {
19587 Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_conditional_non_scalar);
19588 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19589 VarDecl::DeclarationOnly;
19590 Diag(Loc: D->getLocation(),
19591 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19592 << D;
19593 continue;
19594 }
19595
19596 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19597 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
19598 // in a Construct]
19599 // Variables with the predetermined data-sharing attributes may not be
19600 // listed in data-sharing attributes clauses, except for the cases
19601 // listed below.
19602 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
19603 // A list item may appear in a firstprivate or lastprivate clause but not
19604 // both.
19605 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19606 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
19607 (isOpenMPDistributeDirective(DKind: CurrDir) ||
19608 DVar.CKind != OMPC_firstprivate) &&
19609 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
19610 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19611 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19612 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
19613 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19614 continue;
19615 }
19616
19617 // OpenMP [2.14.3.5, Restrictions, p.2]
19618 // A list item that is private within a parallel region, or that appears in
19619 // the reduction clause of a parallel construct, must not appear in a
19620 // lastprivate clause on a worksharing construct if any of the corresponding
19621 // worksharing regions ever binds to any of the corresponding parallel
19622 // regions.
19623 DSAStackTy::DSAVarData TopDVar = DVar;
19624 if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
19625 !isOpenMPParallelDirective(DKind: CurrDir) &&
19626 !isOpenMPTeamsDirective(DKind: CurrDir)) {
19627 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
19628 if (DVar.CKind != OMPC_shared) {
19629 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
19630 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate)
19631 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19632 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19633 continue;
19634 }
19635 }
19636
19637 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
19638 // A variable of class type (or array thereof) that appears in a
19639 // lastprivate clause requires an accessible, unambiguous default
19640 // constructor for the class type, unless the list item is also specified
19641 // in a firstprivate clause.
19642 // A variable of class type (or array thereof) that appears in a
19643 // lastprivate clause requires an accessible, unambiguous copy assignment
19644 // operator for the class type.
19645 Type = getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
19646 VarDecl *SrcVD = buildVarDecl(SemaRef, Loc: ERange.getBegin(),
19647 Type: Type.getUnqualifiedType(), Name: ".lastprivate.src",
19648 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
19649 DeclRefExpr *PseudoSrcExpr =
19650 buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type.getUnqualifiedType(), Loc: ELoc);
19651 VarDecl *DstVD =
19652 buildVarDecl(SemaRef, Loc: ERange.getBegin(), Type, Name: ".lastprivate.dst",
19653 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
19654 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
19655 // For arrays generate assignment operation for single element and replace
19656 // it by the original array element in CodeGen.
19657 ExprResult AssignmentOp = SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: ELoc, Opc: BO_Assign,
19658 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
19659 if (AssignmentOp.isInvalid())
19660 continue;
19661 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
19662 /*DiscardedValue=*/false);
19663 if (AssignmentOp.isInvalid())
19664 continue;
19665
19666 DeclRefExpr *Ref = nullptr;
19667 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19668 if (TopDVar.CKind == OMPC_firstprivate) {
19669 Ref = TopDVar.PrivateCopy;
19670 } else {
19671 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
19672 if (!isOpenMPCapturedDecl(D))
19673 ExprCaptures.push_back(Elt: Ref->getDecl());
19674 }
19675 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) ||
19676 (!isOpenMPCapturedDecl(D) &&
19677 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
19678 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
19679 if (!RefRes.isUsable())
19680 continue;
19681 ExprResult PostUpdateRes =
19682 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
19683 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
19684 if (!PostUpdateRes.isUsable())
19685 continue;
19686 ExprPostUpdates.push_back(
19687 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
19688 }
19689 }
19690 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_lastprivate, PrivateCopy: Ref);
19691 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19692 ? RefExpr->IgnoreParens()
19693 : Ref);
19694 SrcExprs.push_back(Elt: PseudoSrcExpr);
19695 DstExprs.push_back(Elt: PseudoDstExpr);
19696 AssignmentOps.push_back(Elt: AssignmentOp.get());
19697 }
19698
19699 if (Vars.empty())
19700 return nullptr;
19701
19702 return OMPLastprivateClause::Create(
19703 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, SrcExprs, DstExprs,
19704 AssignmentOps, LPKind, LPKindLoc, ColonLoc,
19705 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
19706 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
19707}
19708
19709OMPClause *SemaOpenMP::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
19710 SourceLocation StartLoc,
19711 SourceLocation LParenLoc,
19712 SourceLocation EndLoc) {
19713 SmallVector<Expr *, 8> Vars;
19714 for (Expr *RefExpr : VarList) {
19715 assert(RefExpr && "NULL expr in OpenMP shared clause.");
19716 SourceLocation ELoc;
19717 SourceRange ERange;
19718 Expr *SimpleRefExpr = RefExpr;
19719 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19720 if (Res.second) {
19721 // It will be analyzed later.
19722 Vars.push_back(Elt: RefExpr);
19723 }
19724 ValueDecl *D = Res.first;
19725 if (!D)
19726 continue;
19727
19728 auto *VD = dyn_cast<VarDecl>(Val: D);
19729 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19730 // in a Construct]
19731 // Variables with the predetermined data-sharing attributes may not be
19732 // listed in data-sharing attributes clauses, except for the cases
19733 // listed below. For these exceptions only, listing a predetermined
19734 // variable in a data-sharing attribute clause is allowed and overrides
19735 // the variable's predetermined data-sharing attributes.
19736 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19737 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
19738 DVar.RefExpr) {
19739 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19740 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19741 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19742 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19743 continue;
19744 }
19745
19746 DeclRefExpr *Ref = nullptr;
19747 if (!VD && isOpenMPCapturedDecl(D) &&
19748 !SemaRef.CurContext->isDependentContext())
19749 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
19750 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_shared, PrivateCopy: Ref);
19751 Vars.push_back(Elt: (VD || !Ref || SemaRef.CurContext->isDependentContext())
19752 ? RefExpr->IgnoreParens()
19753 : Ref);
19754 }
19755
19756 if (Vars.empty())
19757 return nullptr;
19758
19759 return OMPSharedClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
19760 VL: Vars);
19761}
19762
19763namespace {
19764class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
19765 DSAStackTy *Stack;
19766
19767public:
19768 bool VisitDeclRefExpr(DeclRefExpr *E) {
19769 if (auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
19770 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
19771 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
19772 return false;
19773 if (DVar.CKind != OMPC_unknown)
19774 return true;
19775 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
19776 D: VD,
19777 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
19778 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
19779 },
19780 DPred: [](OpenMPDirectiveKind) { return true; },
19781 /*FromParent=*/true);
19782 return DVarPrivate.CKind != OMPC_unknown;
19783 }
19784 return false;
19785 }
19786 bool VisitStmt(Stmt *S) {
19787 for (Stmt *Child : S->children()) {
19788 if (Child && Visit(S: Child))
19789 return true;
19790 }
19791 return false;
19792 }
19793 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
19794};
19795} // namespace
19796
19797namespace {
19798// Transform MemberExpression for specified FieldDecl of current class to
19799// DeclRefExpr to specified OMPCapturedExprDecl.
19800class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
19801 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
19802 ValueDecl *Field = nullptr;
19803 DeclRefExpr *CapturedExpr = nullptr;
19804
19805public:
19806 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
19807 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
19808
19809 ExprResult TransformMemberExpr(MemberExpr *E) {
19810 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParenImpCasts()) &&
19811 E->getMemberDecl() == Field) {
19812 CapturedExpr = buildCapture(S&: SemaRef, D: Field, CaptureExpr: E, /*WithInit=*/false);
19813 return CapturedExpr;
19814 }
19815 return BaseTransform::TransformMemberExpr(E);
19816 }
19817 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
19818};
19819} // namespace
19820
19821template <typename T, typename U>
19822static T filterLookupForUDReductionAndMapper(
19823 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
19824 for (U &Set : Lookups) {
19825 for (auto *D : Set) {
19826 if (T Res = Gen(cast<ValueDecl>(D)))
19827 return Res;
19828 }
19829 }
19830 return T();
19831}
19832
19833static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
19834 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
19835
19836 for (auto *RD : D->redecls()) {
19837 // Don't bother with extra checks if we already know this one isn't visible.
19838 if (RD == D)
19839 continue;
19840
19841 auto ND = cast<NamedDecl>(Val: RD);
19842 if (LookupResult::isVisible(SemaRef, D: ND))
19843 return ND;
19844 }
19845
19846 return nullptr;
19847}
19848
19849static void
19850argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
19851 SourceLocation Loc, QualType Ty,
19852 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
19853 // Find all of the associated namespaces and classes based on the
19854 // arguments we have.
19855 Sema::AssociatedNamespaceSet AssociatedNamespaces;
19856 Sema::AssociatedClassSet AssociatedClasses;
19857 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
19858 SemaRef.FindAssociatedClassesAndNamespaces(InstantiationLoc: Loc, Args: &OVE, AssociatedNamespaces,
19859 AssociatedClasses);
19860
19861 // C++ [basic.lookup.argdep]p3:
19862 // Let X be the lookup set produced by unqualified lookup (3.4.1)
19863 // and let Y be the lookup set produced by argument dependent
19864 // lookup (defined as follows). If X contains [...] then Y is
19865 // empty. Otherwise Y is the set of declarations found in the
19866 // namespaces associated with the argument types as described
19867 // below. The set of declarations found by the lookup of the name
19868 // is the union of X and Y.
19869 //
19870 // Here, we compute Y and add its members to the overloaded
19871 // candidate set.
19872 for (auto *NS : AssociatedNamespaces) {
19873 // When considering an associated namespace, the lookup is the
19874 // same as the lookup performed when the associated namespace is
19875 // used as a qualifier (3.4.3.2) except that:
19876 //
19877 // -- Any using-directives in the associated namespace are
19878 // ignored.
19879 //
19880 // -- Any namespace-scope friend functions declared in
19881 // associated classes are visible within their respective
19882 // namespaces even if they are not visible during an ordinary
19883 // lookup (11.4).
19884 DeclContext::lookup_result R = NS->lookup(Name: Id.getName());
19885 for (auto *D : R) {
19886 auto *Underlying = D;
19887 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
19888 Underlying = USD->getTargetDecl();
19889
19890 if (!isa<OMPDeclareReductionDecl>(Val: Underlying) &&
19891 !isa<OMPDeclareMapperDecl>(Val: Underlying))
19892 continue;
19893
19894 if (!SemaRef.isVisible(D)) {
19895 D = findAcceptableDecl(SemaRef, D);
19896 if (!D)
19897 continue;
19898 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
19899 Underlying = USD->getTargetDecl();
19900 }
19901 Lookups.emplace_back();
19902 Lookups.back().addDecl(D: Underlying);
19903 }
19904 }
19905}
19906
19907static ExprResult
19908buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
19909 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
19910 const DeclarationNameInfo &ReductionId, QualType Ty,
19911 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
19912 if (ReductionIdScopeSpec.isInvalid())
19913 return ExprError();
19914 SmallVector<UnresolvedSet<8>, 4> Lookups;
19915 if (S) {
19916 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
19917 Lookup.suppressDiagnostics();
19918 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &ReductionIdScopeSpec,
19919 /*ObjectType=*/QualType())) {
19920 NamedDecl *D = Lookup.getRepresentativeDecl();
19921 do {
19922 S = S->getParent();
19923 } while (S && !S->isDeclScope(D));
19924 if (S)
19925 S = S->getParent();
19926 Lookups.emplace_back();
19927 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
19928 Lookup.clear();
19929 }
19930 } else if (auto *ULE =
19931 cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedReduction)) {
19932 Lookups.push_back(Elt: UnresolvedSet<8>());
19933 Decl *PrevD = nullptr;
19934 for (NamedDecl *D : ULE->decls()) {
19935 if (D == PrevD)
19936 Lookups.push_back(Elt: UnresolvedSet<8>());
19937 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Val: D))
19938 Lookups.back().addDecl(D: DRD);
19939 PrevD = D;
19940 }
19941 }
19942 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
19943 Ty->isInstantiationDependentType() ||
19944 Ty->containsUnexpandedParameterPack() ||
19945 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
19946 return !D->isInvalidDecl() &&
19947 (D->getType()->isDependentType() ||
19948 D->getType()->isInstantiationDependentType() ||
19949 D->getType()->containsUnexpandedParameterPack());
19950 })) {
19951 UnresolvedSet<8> ResSet;
19952 for (const UnresolvedSet<8> &Set : Lookups) {
19953 if (Set.empty())
19954 continue;
19955 ResSet.append(I: Set.begin(), E: Set.end());
19956 // The last item marks the end of all declarations at the specified scope.
19957 ResSet.addDecl(D: Set[Set.size() - 1]);
19958 }
19959 return UnresolvedLookupExpr::Create(
19960 Context: SemaRef.Context, /*NamingClass=*/nullptr,
19961 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: ReductionId,
19962 /*ADL=*/RequiresADL: true, Begin: ResSet.begin(), End: ResSet.end(), /*KnownDependent=*/false,
19963 /*KnownInstantiationDependent=*/false);
19964 }
19965 // Lookup inside the classes.
19966 // C++ [over.match.oper]p3:
19967 // For a unary operator @ with an operand of a type whose
19968 // cv-unqualified version is T1, and for a binary operator @ with
19969 // a left operand of a type whose cv-unqualified version is T1 and
19970 // a right operand of a type whose cv-unqualified version is T2,
19971 // three sets of candidate functions, designated member
19972 // candidates, non-member candidates and built-in candidates, are
19973 // constructed as follows:
19974 // -- If T1 is a complete class type or a class currently being
19975 // defined, the set of member candidates is the result of the
19976 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
19977 // the set of member candidates is empty.
19978 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
19979 Lookup.suppressDiagnostics();
19980 if (Ty->isRecordType()) {
19981 // Complete the type if it can be completed.
19982 // If the type is neither complete nor being defined, bail out now.
19983 bool IsComplete = SemaRef.isCompleteType(Loc, T: Ty);
19984 auto *RD = Ty->castAsRecordDecl();
19985 if (IsComplete || RD->isBeingDefined()) {
19986 Lookup.clear();
19987 SemaRef.LookupQualifiedName(R&: Lookup, LookupCtx: RD);
19988 if (Lookup.empty()) {
19989 Lookups.emplace_back();
19990 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
19991 }
19992 }
19993 }
19994 // Perform ADL.
19995 if (SemaRef.getLangOpts().CPlusPlus)
19996 argumentDependentLookup(SemaRef, Id: ReductionId, Loc, Ty, Lookups);
19997 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19998 Lookups, Gen: [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
19999 if (!D->isInvalidDecl() &&
20000 SemaRef.Context.hasSameType(T1: D->getType(), T2: Ty))
20001 return D;
20002 return nullptr;
20003 }))
20004 return SemaRef.BuildDeclRefExpr(D: VD, Ty: VD->getType().getNonReferenceType(),
20005 VK: VK_LValue, Loc);
20006 if (SemaRef.getLangOpts().CPlusPlus) {
20007 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
20008 Lookups, Gen: [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
20009 if (!D->isInvalidDecl() &&
20010 SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: D->getType()) &&
20011 !Ty.isMoreQualifiedThan(other: D->getType(),
20012 Ctx: SemaRef.getASTContext()))
20013 return D;
20014 return nullptr;
20015 })) {
20016 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
20017 /*DetectVirtual=*/false);
20018 if (SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: VD->getType(), Paths)) {
20019 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
20020 T: VD->getType().getUnqualifiedType()))) {
20021 if (SemaRef.CheckBaseClassAccess(
20022 AccessLoc: Loc, Base: VD->getType(), Derived: Ty, Path: Paths.front(),
20023 /*DiagID=*/0) != Sema::AR_inaccessible) {
20024 SemaRef.BuildBasePathArray(Paths, BasePath);
20025 return SemaRef.BuildDeclRefExpr(
20026 D: VD, Ty: VD->getType().getNonReferenceType(), VK: VK_LValue, Loc);
20027 }
20028 }
20029 }
20030 }
20031 }
20032 if (ReductionIdScopeSpec.isSet()) {
20033 SemaRef.Diag(Loc, DiagID: diag::err_omp_not_resolved_reduction_identifier)
20034 << Ty << Range;
20035 return ExprError();
20036 }
20037 return ExprEmpty();
20038}
20039
20040namespace {
20041/// Data for the reduction-based clauses.
20042struct ReductionData {
20043 /// List of original reduction items.
20044 SmallVector<Expr *, 8> Vars;
20045 /// List of private copies of the reduction items.
20046 SmallVector<Expr *, 8> Privates;
20047 /// LHS expressions for the reduction_op expressions.
20048 SmallVector<Expr *, 8> LHSs;
20049 /// RHS expressions for the reduction_op expressions.
20050 SmallVector<Expr *, 8> RHSs;
20051 /// Reduction operation expression.
20052 SmallVector<Expr *, 8> ReductionOps;
20053 /// inscan copy operation expressions.
20054 SmallVector<Expr *, 8> InscanCopyOps;
20055 /// inscan copy temp array expressions for prefix sums.
20056 SmallVector<Expr *, 8> InscanCopyArrayTemps;
20057 /// inscan copy temp array element expressions for prefix sums.
20058 SmallVector<Expr *, 8> InscanCopyArrayElems;
20059 /// Taskgroup descriptors for the corresponding reduction items in
20060 /// in_reduction clauses.
20061 SmallVector<Expr *, 8> TaskgroupDescriptors;
20062 /// List of captures for clause.
20063 SmallVector<Decl *, 4> ExprCaptures;
20064 /// List of postupdate expressions.
20065 SmallVector<Expr *, 4> ExprPostUpdates;
20066 /// Reduction modifier.
20067 unsigned RedModifier = 0;
20068 /// Original modifier.
20069 unsigned OrigSharingModifier = 0;
20070 /// Private Variable Reduction
20071 SmallVector<bool, 8> IsPrivateVarReduction;
20072 ReductionData() = delete;
20073 /// Reserves required memory for the reduction data.
20074 ReductionData(unsigned Size, unsigned Modifier = 0, unsigned OrgModifier = 0)
20075 : RedModifier(Modifier), OrigSharingModifier(OrgModifier) {
20076 Vars.reserve(N: Size);
20077 Privates.reserve(N: Size);
20078 LHSs.reserve(N: Size);
20079 RHSs.reserve(N: Size);
20080 ReductionOps.reserve(N: Size);
20081 IsPrivateVarReduction.reserve(N: Size);
20082 if (RedModifier == OMPC_REDUCTION_inscan) {
20083 InscanCopyOps.reserve(N: Size);
20084 InscanCopyArrayTemps.reserve(N: Size);
20085 InscanCopyArrayElems.reserve(N: Size);
20086 }
20087 TaskgroupDescriptors.reserve(N: Size);
20088 ExprCaptures.reserve(N: Size);
20089 ExprPostUpdates.reserve(N: Size);
20090 }
20091 /// Stores reduction item and reduction operation only (required for dependent
20092 /// reduction item).
20093 void push(Expr *Item, Expr *ReductionOp) {
20094 Vars.emplace_back(Args&: Item);
20095 Privates.emplace_back(Args: nullptr);
20096 LHSs.emplace_back(Args: nullptr);
20097 RHSs.emplace_back(Args: nullptr);
20098 ReductionOps.emplace_back(Args&: ReductionOp);
20099 IsPrivateVarReduction.emplace_back(Args: false);
20100 TaskgroupDescriptors.emplace_back(Args: nullptr);
20101 if (RedModifier == OMPC_REDUCTION_inscan) {
20102 InscanCopyOps.push_back(Elt: nullptr);
20103 InscanCopyArrayTemps.push_back(Elt: nullptr);
20104 InscanCopyArrayElems.push_back(Elt: nullptr);
20105 }
20106 }
20107 /// Stores reduction data.
20108 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
20109 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp,
20110 Expr *CopyArrayElem, bool IsPrivate) {
20111 Vars.emplace_back(Args&: Item);
20112 Privates.emplace_back(Args&: Private);
20113 LHSs.emplace_back(Args&: LHS);
20114 RHSs.emplace_back(Args&: RHS);
20115 ReductionOps.emplace_back(Args&: ReductionOp);
20116 TaskgroupDescriptors.emplace_back(Args&: TaskgroupDescriptor);
20117 if (RedModifier == OMPC_REDUCTION_inscan) {
20118 InscanCopyOps.push_back(Elt: CopyOp);
20119 InscanCopyArrayTemps.push_back(Elt: CopyArrayTemp);
20120 InscanCopyArrayElems.push_back(Elt: CopyArrayElem);
20121 } else {
20122 assert(CopyOp == nullptr && CopyArrayTemp == nullptr &&
20123 CopyArrayElem == nullptr &&
20124 "Copy operation must be used for inscan reductions only.");
20125 }
20126 IsPrivateVarReduction.emplace_back(Args&: IsPrivate);
20127 }
20128};
20129} // namespace
20130
20131static bool checkOMPArraySectionConstantForReduction(
20132 ASTContext &Context, const ArraySectionExpr *OASE, bool &SingleElement,
20133 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
20134 const Expr *Length = OASE->getLength();
20135 if (Length == nullptr) {
20136 // For array sections of the form [1:] or [:], we would need to analyze
20137 // the lower bound...
20138 if (OASE->getColonLocFirst().isValid())
20139 return false;
20140
20141 // This is an array subscript which has implicit length 1!
20142 SingleElement = true;
20143 ArraySizes.push_back(Elt: llvm::APSInt::get(X: 1));
20144 } else {
20145 Expr::EvalResult Result;
20146 if (!Length->EvaluateAsInt(Result, Ctx: Context))
20147 return false;
20148
20149 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
20150 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
20151 ArraySizes.push_back(Elt: ConstantLengthValue);
20152 }
20153
20154 // Get the base of this array section and walk up from there.
20155 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
20156
20157 // We require length = 1 for all array sections except the right-most to
20158 // guarantee that the memory region is contiguous and has no holes in it.
20159 while (const auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base)) {
20160 Length = TempOASE->getLength();
20161 if (Length == nullptr) {
20162 // For array sections of the form [1:] or [:], we would need to analyze
20163 // the lower bound...
20164 if (OASE->getColonLocFirst().isValid())
20165 return false;
20166
20167 // This is an array subscript which has implicit length 1!
20168 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
20169 ArraySizes.push_back(Elt: ConstantOne);
20170 } else {
20171 Expr::EvalResult Result;
20172 if (!Length->EvaluateAsInt(Result, Ctx: Context))
20173 return false;
20174
20175 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
20176 if (ConstantLengthValue.getSExtValue() != 1)
20177 return false;
20178
20179 ArraySizes.push_back(Elt: ConstantLengthValue);
20180 }
20181 Base = TempOASE->getBase()->IgnoreParenImpCasts();
20182 }
20183
20184 // If we have a single element, we don't need to add the implicit lengths.
20185 if (!SingleElement) {
20186 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base)) {
20187 // Has implicit length 1!
20188 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
20189 ArraySizes.push_back(Elt: ConstantOne);
20190 Base = TempASE->getBase()->IgnoreParenImpCasts();
20191 }
20192 }
20193
20194 // This array section can be privatized as a single value or as a constant
20195 // sized array.
20196 return true;
20197}
20198
20199static BinaryOperatorKind
20200getRelatedCompoundReductionOp(BinaryOperatorKind BOK) {
20201 if (BOK == BO_Add)
20202 return BO_AddAssign;
20203 if (BOK == BO_Mul)
20204 return BO_MulAssign;
20205 if (BOK == BO_And)
20206 return BO_AndAssign;
20207 if (BOK == BO_Or)
20208 return BO_OrAssign;
20209 if (BOK == BO_Xor)
20210 return BO_XorAssign;
20211 return BOK;
20212}
20213
20214static bool actOnOMPReductionKindClause(
20215 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
20216 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
20217 SourceLocation ColonLoc, SourceLocation EndLoc,
20218 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20219 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
20220 DeclarationName DN = ReductionId.getName();
20221 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
20222 BinaryOperatorKind BOK = BO_Comma;
20223
20224 ASTContext &Context = S.Context;
20225 // OpenMP [2.14.3.6, reduction clause]
20226 // C
20227 // reduction-identifier is either an identifier or one of the following
20228 // operators: +, -, *, &, |, ^, && and ||
20229 // C++
20230 // reduction-identifier is either an id-expression or one of the following
20231 // operators: +, -, *, &, |, ^, && and ||
20232 switch (OOK) {
20233 case OO_Plus:
20234 BOK = BO_Add;
20235 break;
20236 case OO_Minus:
20237 // Minus(-) operator is not supported in TR11 (OpenMP 6.0). Setting BOK to
20238 // BO_Comma will automatically diagnose it for OpenMP > 52 as not allowed
20239 // reduction identifier.
20240 if (S.LangOpts.OpenMP > 52)
20241 BOK = BO_Comma;
20242 else
20243 BOK = BO_Add;
20244 break;
20245 case OO_Star:
20246 BOK = BO_Mul;
20247 break;
20248 case OO_Amp:
20249 BOK = BO_And;
20250 break;
20251 case OO_Pipe:
20252 BOK = BO_Or;
20253 break;
20254 case OO_Caret:
20255 BOK = BO_Xor;
20256 break;
20257 case OO_AmpAmp:
20258 BOK = BO_LAnd;
20259 break;
20260 case OO_PipePipe:
20261 BOK = BO_LOr;
20262 break;
20263 case OO_New:
20264 case OO_Delete:
20265 case OO_Array_New:
20266 case OO_Array_Delete:
20267 case OO_Slash:
20268 case OO_Percent:
20269 case OO_Tilde:
20270 case OO_Exclaim:
20271 case OO_Equal:
20272 case OO_Less:
20273 case OO_Greater:
20274 case OO_LessEqual:
20275 case OO_GreaterEqual:
20276 case OO_PlusEqual:
20277 case OO_MinusEqual:
20278 case OO_StarEqual:
20279 case OO_SlashEqual:
20280 case OO_PercentEqual:
20281 case OO_CaretEqual:
20282 case OO_AmpEqual:
20283 case OO_PipeEqual:
20284 case OO_LessLess:
20285 case OO_GreaterGreater:
20286 case OO_LessLessEqual:
20287 case OO_GreaterGreaterEqual:
20288 case OO_EqualEqual:
20289 case OO_ExclaimEqual:
20290 case OO_Spaceship:
20291 case OO_PlusPlus:
20292 case OO_MinusMinus:
20293 case OO_Comma:
20294 case OO_ArrowStar:
20295 case OO_Arrow:
20296 case OO_Call:
20297 case OO_Subscript:
20298 case OO_Conditional:
20299 case OO_Coawait:
20300 case NUM_OVERLOADED_OPERATORS:
20301 llvm_unreachable("Unexpected reduction identifier");
20302 case OO_None:
20303 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
20304 if (II->isStr(Str: "max"))
20305 BOK = BO_GT;
20306 else if (II->isStr(Str: "min"))
20307 BOK = BO_LT;
20308 }
20309 break;
20310 }
20311
20312 // OpenMP 5.2, 5.5.5 (see page 627, line 18) reduction Clause, Restrictions
20313 // A reduction clause with the minus (-) operator was deprecated
20314 if (OOK == OO_Minus && S.LangOpts.OpenMP == 52)
20315 S.Diag(Loc: ReductionId.getLoc(), DiagID: diag::warn_omp_minus_in_reduction_deprecated);
20316
20317 SourceRange ReductionIdRange;
20318 if (ReductionIdScopeSpec.isValid())
20319 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
20320 else
20321 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
20322 ReductionIdRange.setEnd(ReductionId.getEndLoc());
20323
20324 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
20325 bool FirstIter = true;
20326 for (Expr *RefExpr : VarList) {
20327 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
20328 // OpenMP [2.1, C/C++]
20329 // A list item is a variable or array section, subject to the restrictions
20330 // specified in Section 2.4 on page 42 and in each of the sections
20331 // describing clauses and directives for which a list appears.
20332 // OpenMP [2.14.3.3, Restrictions, p.1]
20333 // A variable that is part of another variable (as an array or
20334 // structure element) cannot appear in a private clause.
20335 if (!FirstIter && IR != ER)
20336 ++IR;
20337 FirstIter = false;
20338 SourceLocation ELoc;
20339 SourceRange ERange;
20340 bool IsPrivate = false;
20341 Expr *SimpleRefExpr = RefExpr;
20342 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
20343 /*AllowArraySection=*/true);
20344 if (Res.second) {
20345 // Try to find 'declare reduction' corresponding construct before using
20346 // builtin/overloaded operators.
20347 QualType Type = Context.DependentTy;
20348 CXXCastPath BasePath;
20349 ExprResult DeclareReductionRef = buildDeclareReductionRef(
20350 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
20351 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
20352 Expr *ReductionOp = nullptr;
20353 if (S.CurContext->isDependentContext() &&
20354 (DeclareReductionRef.isUnset() ||
20355 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get())))
20356 ReductionOp = DeclareReductionRef.get();
20357 // It will be analyzed later.
20358 RD.push(Item: RefExpr, ReductionOp);
20359 }
20360 ValueDecl *D = Res.first;
20361 if (!D)
20362 continue;
20363
20364 Expr *TaskgroupDescriptor = nullptr;
20365 QualType Type;
20366 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: RefExpr->IgnoreParens());
20367 auto *OASE = dyn_cast<ArraySectionExpr>(Val: RefExpr->IgnoreParens());
20368 if (ASE) {
20369 Type = ASE->getType().getNonReferenceType();
20370 } else if (OASE) {
20371 QualType BaseType =
20372 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
20373 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
20374 Type = ATy->getElementType();
20375 else
20376 Type = BaseType->getPointeeType();
20377 Type = Type.getNonReferenceType();
20378 } else {
20379 Type = Context.getBaseElementType(QT: D->getType().getNonReferenceType());
20380 }
20381 auto *VD = dyn_cast<VarDecl>(Val: D);
20382
20383 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
20384 // A variable that appears in a private clause must not have an incomplete
20385 // type or a reference type.
20386 if (S.RequireCompleteType(Loc: ELoc, T: D->getType(),
20387 DiagID: diag::err_omp_reduction_incomplete_type))
20388 continue;
20389 // OpenMP [2.14.3.6, reduction clause, Restrictions]
20390 // A list item that appears in a reduction clause must not be
20391 // const-qualified.
20392 if (rejectConstNotMutableType(SemaRef&: S, D, Type, CKind: ClauseKind, ELoc,
20393 /*AcceptIfMutable=*/false, ListItemNotVar: ASE || OASE))
20394 continue;
20395
20396 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
20397 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
20398 // If a list-item is a reference type then it must bind to the same object
20399 // for all threads of the team.
20400 if (!ASE && !OASE) {
20401 if (VD) {
20402 VarDecl *VDDef = VD->getDefinition();
20403 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
20404 DSARefChecker Check(Stack);
20405 if (Check.Visit(S: VDDef->getInit())) {
20406 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_ref_type_arg)
20407 << getOpenMPClauseNameForDiag(C: ClauseKind) << ERange;
20408 S.Diag(Loc: VDDef->getLocation(), DiagID: diag::note_defined_here) << VDDef;
20409 continue;
20410 }
20411 }
20412 }
20413
20414 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
20415 // in a Construct]
20416 // Variables with the predetermined data-sharing attributes may not be
20417 // listed in data-sharing attributes clauses, except for the cases
20418 // listed below. For these exceptions only, listing a predetermined
20419 // variable in a data-sharing attribute clause is allowed and overrides
20420 // the variable's predetermined data-sharing attributes.
20421 // OpenMP [2.14.3.6, Restrictions, p.3]
20422 // Any number of reduction clauses can be specified on the directive,
20423 // but a list item can appear only once in the reduction clauses for that
20424 // directive.
20425 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
20426 if (DVar.CKind == OMPC_reduction) {
20427 S.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced)
20428 << getOpenMPClauseNameForDiag(C: ClauseKind);
20429 if (DVar.RefExpr)
20430 S.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_referenced);
20431 continue;
20432 }
20433 if (DVar.CKind != OMPC_unknown) {
20434 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
20435 << getOpenMPClauseNameForDiag(C: DVar.CKind)
20436 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20437 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20438 continue;
20439 }
20440
20441 // OpenMP [2.14.3.6, Restrictions, p.1]
20442 // A list item that appears in a reduction clause of a worksharing
20443 // construct must be shared in the parallel regions to which any of the
20444 // worksharing regions arising from the worksharing construct bind.
20445
20446 if (S.getLangOpts().OpenMP <= 52 &&
20447 isOpenMPWorksharingDirective(DKind: CurrDir) &&
20448 !isOpenMPParallelDirective(DKind: CurrDir) &&
20449 !isOpenMPTeamsDirective(DKind: CurrDir)) {
20450 DVar = Stack->getImplicitDSA(D, FromParent: true);
20451 if (DVar.CKind != OMPC_shared) {
20452 S.Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
20453 << getOpenMPClauseNameForDiag(C: OMPC_reduction)
20454 << getOpenMPClauseNameForDiag(C: OMPC_shared);
20455 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20456 continue;
20457 }
20458 } else if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
20459 !isOpenMPParallelDirective(DKind: CurrDir) &&
20460 !isOpenMPTeamsDirective(DKind: CurrDir)) {
20461 // OpenMP 6.0 [ 7.6.10 ]
20462 // Support Reduction over private variables with reduction clause.
20463 // A list item in a reduction clause can now be private in the enclosing
20464 // context. For orphaned constructs it is assumed to be shared unless
20465 // the original(private) modifier appears in the clause.
20466 DVar = Stack->getImplicitDSA(D, FromParent: true);
20467 // Determine if the variable should be considered private
20468 IsPrivate = DVar.CKind != OMPC_shared;
20469 bool IsOrphaned = false;
20470 OpenMPDirectiveKind ParentDir = Stack->getParentDirective();
20471 IsOrphaned = ParentDir == OMPD_unknown;
20472 if ((IsOrphaned &&
20473 RD.OrigSharingModifier == OMPC_ORIGINAL_SHARING_private))
20474 IsPrivate = true;
20475 }
20476 } else {
20477 // Threadprivates cannot be shared between threads, so dignose if the base
20478 // is a threadprivate variable.
20479 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
20480 if (DVar.CKind == OMPC_threadprivate) {
20481 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
20482 << getOpenMPClauseNameForDiag(C: DVar.CKind)
20483 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20484 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20485 continue;
20486 }
20487 }
20488
20489 // Try to find 'declare reduction' corresponding construct before using
20490 // builtin/overloaded operators.
20491 CXXCastPath BasePath;
20492 ExprResult DeclareReductionRef = buildDeclareReductionRef(
20493 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
20494 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
20495 if (DeclareReductionRef.isInvalid())
20496 continue;
20497 if (S.CurContext->isDependentContext() &&
20498 (DeclareReductionRef.isUnset() ||
20499 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get()))) {
20500 RD.push(Item: RefExpr, ReductionOp: DeclareReductionRef.get());
20501 continue;
20502 }
20503 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
20504 // Not allowed reduction identifier is found.
20505 if (S.LangOpts.OpenMP > 52)
20506 S.Diag(Loc: ReductionId.getBeginLoc(),
20507 DiagID: diag::err_omp_unknown_reduction_identifier_since_omp_6_0)
20508 << Type << ReductionIdRange;
20509 else
20510 S.Diag(Loc: ReductionId.getBeginLoc(),
20511 DiagID: diag::err_omp_unknown_reduction_identifier_prior_omp_6_0)
20512 << Type << ReductionIdRange;
20513 continue;
20514 }
20515
20516 // OpenMP [2.14.3.6, reduction clause, Restrictions]
20517 // The type of a list item that appears in a reduction clause must be valid
20518 // for the reduction-identifier. For a max or min reduction in C, the type
20519 // of the list item must be an allowed arithmetic data type: char, int,
20520 // float, double, or _Bool, possibly modified with long, short, signed, or
20521 // unsigned. For a max or min reduction in C++, the type of the list item
20522 // must be an allowed arithmetic data type: char, wchar_t, int, float,
20523 // double, or bool, possibly modified with long, short, signed, or unsigned.
20524 if (DeclareReductionRef.isUnset()) {
20525 if ((BOK == BO_GT || BOK == BO_LT) &&
20526 !(Type->isScalarType() ||
20527 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
20528 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_not_arithmetic_type_arg)
20529 << getOpenMPClauseNameForDiag(C: ClauseKind)
20530 << S.getLangOpts().CPlusPlus;
20531 if (!ASE && !OASE) {
20532 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20533 VarDecl::DeclarationOnly;
20534 S.Diag(Loc: D->getLocation(),
20535 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20536 << D;
20537 }
20538 continue;
20539 }
20540 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
20541 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
20542 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_floating_type_arg)
20543 << getOpenMPClauseNameForDiag(C: ClauseKind);
20544 if (!ASE && !OASE) {
20545 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20546 VarDecl::DeclarationOnly;
20547 S.Diag(Loc: D->getLocation(),
20548 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20549 << D;
20550 }
20551 continue;
20552 }
20553 }
20554
20555 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
20556 VarDecl *LHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: ".reduction.lhs",
20557 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20558 VarDecl *RHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: D->getName(),
20559 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20560 QualType PrivateTy = Type;
20561
20562 // Try if we can determine constant lengths for all array sections and avoid
20563 // the VLA.
20564 bool ConstantLengthOASE = false;
20565 if (OASE) {
20566 bool SingleElement;
20567 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
20568 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
20569 Context, OASE, SingleElement, ArraySizes);
20570
20571 // If we don't have a single element, we must emit a constant array type.
20572 if (ConstantLengthOASE && !SingleElement) {
20573 for (llvm::APSInt &Size : ArraySizes)
20574 PrivateTy = Context.getConstantArrayType(EltTy: PrivateTy, ArySize: Size, SizeExpr: nullptr,
20575 ASM: ArraySizeModifier::Normal,
20576 /*IndexTypeQuals=*/0);
20577 }
20578 }
20579
20580 if ((OASE && !ConstantLengthOASE) ||
20581 (!OASE && !ASE &&
20582 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
20583 if (!Context.getTargetInfo().isVLASupported()) {
20584 if (isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective())) {
20585 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
20586 S.Diag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
20587 continue;
20588 } else {
20589 S.targetDiag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
20590 S.targetDiag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
20591 }
20592 }
20593 // For arrays/array sections only:
20594 // Create pseudo array type for private copy. The size for this array will
20595 // be generated during codegen.
20596 // For array subscripts or single variables Private Ty is the same as Type
20597 // (type of the variable or single array element).
20598 PrivateTy = Context.getVariableArrayType(
20599 EltTy: Type,
20600 NumElts: new (Context)
20601 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue),
20602 ASM: ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
20603 } else if (!ASE && !OASE &&
20604 Context.getAsArrayType(T: D->getType().getNonReferenceType())) {
20605 PrivateTy = D->getType().getNonReferenceType();
20606 }
20607 // Private copy.
20608 VarDecl *PrivateVD =
20609 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
20610 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
20611 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
20612 // Add initializer for private variable.
20613 Expr *Init = nullptr;
20614 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, D: LHSVD, Ty: Type, Loc: ELoc);
20615 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, D: RHSVD, Ty: Type, Loc: ELoc);
20616 if (DeclareReductionRef.isUsable()) {
20617 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
20618 auto *DRD = cast<OMPDeclareReductionDecl>(Val: DRDRef->getDecl());
20619 if (DRD->getInitializer()) {
20620 Init = DRDRef;
20621 RHSVD->setInit(DRDRef);
20622 RHSVD->setInitStyle(VarDecl::CallInit);
20623 }
20624 } else {
20625 switch (BOK) {
20626 case BO_Add:
20627 case BO_Xor:
20628 case BO_Or:
20629 case BO_LOr:
20630 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
20631 if (Type->isScalarType() || Type->isAnyComplexType())
20632 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/0).get();
20633 break;
20634 case BO_Mul:
20635 case BO_LAnd:
20636 if (Type->isScalarType() || Type->isAnyComplexType()) {
20637 // '*' and '&&' reduction ops - initializer is '1'.
20638 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/1).get();
20639 }
20640 break;
20641 case BO_And: {
20642 // '&' reduction op - initializer is '~0'.
20643 QualType OrigType = Type;
20644 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
20645 Type = ComplexTy->getElementType();
20646 if (Type->isRealFloatingType()) {
20647 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue(
20648 Semantics: Context.getFloatTypeSemantics(T: Type));
20649 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
20650 Type, L: ELoc);
20651 } else if (Type->isScalarType()) {
20652 uint64_t Size = Context.getTypeSize(T: Type);
20653 QualType IntTy = Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/0);
20654 llvm::APInt InitValue = llvm::APInt::getAllOnes(numBits: Size);
20655 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
20656 }
20657 if (Init && OrigType->isAnyComplexType()) {
20658 // Init = 0xFFFF + 0xFFFFi;
20659 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
20660 Init = S.CreateBuiltinBinOp(OpLoc: ELoc, Opc: BO_Add, LHSExpr: Init, RHSExpr: Im).get();
20661 }
20662 Type = OrigType;
20663 break;
20664 }
20665 case BO_LT:
20666 case BO_GT: {
20667 // 'min' reduction op - initializer is 'Largest representable number in
20668 // the reduction list item type'.
20669 // 'max' reduction op - initializer is 'Least representable number in
20670 // the reduction list item type'.
20671 if (Type->isIntegerType() || Type->isPointerType()) {
20672 bool IsSigned = Type->hasSignedIntegerRepresentation();
20673 uint64_t Size = Context.getTypeSize(T: Type);
20674 QualType IntTy =
20675 Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/IsSigned);
20676 llvm::APInt InitValue =
20677 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(numBits: Size)
20678 : llvm::APInt::getMinValue(numBits: Size)
20679 : IsSigned ? llvm::APInt::getSignedMaxValue(numBits: Size)
20680 : llvm::APInt::getMaxValue(numBits: Size);
20681 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
20682 if (Type->isPointerType()) {
20683 // Cast to pointer type.
20684 ExprResult CastExpr = S.BuildCStyleCastExpr(
20685 LParenLoc: ELoc, Ty: Context.getTrivialTypeSourceInfo(T: Type, Loc: ELoc), RParenLoc: ELoc, Op: Init);
20686 if (CastExpr.isInvalid())
20687 continue;
20688 Init = CastExpr.get();
20689 }
20690 } else if (Type->isRealFloatingType()) {
20691 llvm::APFloat InitValue = llvm::APFloat::getLargest(
20692 Sem: Context.getFloatTypeSemantics(T: Type), Negative: BOK != BO_LT);
20693 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
20694 Type, L: ELoc);
20695 }
20696 break;
20697 }
20698 case BO_PtrMemD:
20699 case BO_PtrMemI:
20700 case BO_MulAssign:
20701 case BO_Div:
20702 case BO_Rem:
20703 case BO_Sub:
20704 case BO_Shl:
20705 case BO_Shr:
20706 case BO_LE:
20707 case BO_GE:
20708 case BO_EQ:
20709 case BO_NE:
20710 case BO_Cmp:
20711 case BO_AndAssign:
20712 case BO_XorAssign:
20713 case BO_OrAssign:
20714 case BO_Assign:
20715 case BO_AddAssign:
20716 case BO_SubAssign:
20717 case BO_DivAssign:
20718 case BO_RemAssign:
20719 case BO_ShlAssign:
20720 case BO_ShrAssign:
20721 case BO_Comma:
20722 llvm_unreachable("Unexpected reduction operation");
20723 }
20724 }
20725 if (Init && DeclareReductionRef.isUnset()) {
20726 S.AddInitializerToDecl(dcl: RHSVD, init: Init, /*DirectInit=*/false);
20727 // Store initializer for single element in private copy. Will be used
20728 // during codegen.
20729 PrivateVD->setInit(RHSVD->getInit());
20730 PrivateVD->setInitStyle(RHSVD->getInitStyle());
20731 } else if (!Init) {
20732 S.ActOnUninitializedDecl(dcl: RHSVD);
20733 // Store initializer for single element in private copy. Will be used
20734 // during codegen.
20735 PrivateVD->setInit(RHSVD->getInit());
20736 PrivateVD->setInitStyle(RHSVD->getInitStyle());
20737 }
20738 if (RHSVD->isInvalidDecl())
20739 continue;
20740 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
20741 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_id_not_compatible)
20742 << Type << ReductionIdRange;
20743 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20744 VarDecl::DeclarationOnly;
20745 S.Diag(Loc: D->getLocation(),
20746 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20747 << D;
20748 continue;
20749 }
20750 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, D: PrivateVD, Ty: PrivateTy, Loc: ELoc);
20751 ExprResult ReductionOp;
20752 if (DeclareReductionRef.isUsable()) {
20753 QualType RedTy = DeclareReductionRef.get()->getType();
20754 QualType PtrRedTy = Context.getPointerType(T: RedTy);
20755 ExprResult LHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: LHSDRE);
20756 ExprResult RHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: RHSDRE);
20757 if (!BasePath.empty()) {
20758 LHS = S.DefaultLvalueConversion(E: LHS.get());
20759 RHS = S.DefaultLvalueConversion(E: RHS.get());
20760 LHS = ImplicitCastExpr::Create(
20761 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: LHS.get(), BasePath: &BasePath,
20762 Cat: LHS.get()->getValueKind(), FPO: FPOptionsOverride());
20763 RHS = ImplicitCastExpr::Create(
20764 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: RHS.get(), BasePath: &BasePath,
20765 Cat: RHS.get()->getValueKind(), FPO: FPOptionsOverride());
20766 }
20767 FunctionProtoType::ExtProtoInfo EPI;
20768 QualType Params[] = {PtrRedTy, PtrRedTy};
20769 QualType FnTy = Context.getFunctionType(ResultTy: Context.VoidTy, Args: Params, EPI);
20770 auto *OVE = new (Context) OpaqueValueExpr(
20771 ELoc, Context.getPointerType(T: FnTy), VK_PRValue, OK_Ordinary,
20772 S.DefaultLvalueConversion(E: DeclareReductionRef.get()).get());
20773 Expr *Args[] = {LHS.get(), RHS.get()};
20774 ReductionOp =
20775 CallExpr::Create(Ctx: Context, Fn: OVE, Args, Ty: Context.VoidTy, VK: VK_PRValue, RParenLoc: ELoc,
20776 FPFeatures: S.CurFPFeatureOverrides());
20777 } else {
20778 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK);
20779 if (Type->isRecordType() && CombBOK != BOK) {
20780 Sema::TentativeAnalysisScope Trap(S);
20781 ReductionOp =
20782 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20783 Opc: CombBOK, LHSExpr: LHSDRE, RHSExpr: RHSDRE);
20784 }
20785 if (!ReductionOp.isUsable()) {
20786 ReductionOp =
20787 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(), Opc: BOK,
20788 LHSExpr: LHSDRE, RHSExpr: RHSDRE);
20789 if (ReductionOp.isUsable()) {
20790 if (BOK != BO_LT && BOK != BO_GT) {
20791 ReductionOp =
20792 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20793 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ReductionOp.get());
20794 } else {
20795 auto *ConditionalOp = new (Context)
20796 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc,
20797 RHSDRE, Type, VK_LValue, OK_Ordinary);
20798 ReductionOp =
20799 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20800 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ConditionalOp);
20801 }
20802 }
20803 }
20804 if (ReductionOp.isUsable())
20805 ReductionOp = S.ActOnFinishFullExpr(Expr: ReductionOp.get(),
20806 /*DiscardedValue=*/false);
20807 if (!ReductionOp.isUsable())
20808 continue;
20809 }
20810
20811 // Add copy operations for inscan reductions.
20812 // LHS = RHS;
20813 ExprResult CopyOpRes, TempArrayRes, TempArrayElem;
20814 if (ClauseKind == OMPC_reduction &&
20815 RD.RedModifier == OMPC_REDUCTION_inscan) {
20816 ExprResult RHS = S.DefaultLvalueConversion(E: RHSDRE);
20817 CopyOpRes = S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: LHSDRE,
20818 RHSExpr: RHS.get());
20819 if (!CopyOpRes.isUsable())
20820 continue;
20821 CopyOpRes =
20822 S.ActOnFinishFullExpr(Expr: CopyOpRes.get(), /*DiscardedValue=*/true);
20823 if (!CopyOpRes.isUsable())
20824 continue;
20825 // For simd directive and simd-based directives in simd mode no need to
20826 // construct temp array, need just a single temp element.
20827 if (Stack->getCurrentDirective() == OMPD_simd ||
20828 (S.getLangOpts().OpenMPSimd &&
20829 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()))) {
20830 VarDecl *TempArrayVD =
20831 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
20832 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20833 // Add a constructor to the temp decl.
20834 S.ActOnUninitializedDecl(dcl: TempArrayVD);
20835 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: PrivateTy, Loc: ELoc);
20836 } else {
20837 // Build temp array for prefix sum.
20838 auto *Dim = new (S.Context)
20839 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
20840 QualType ArrayTy = S.Context.getVariableArrayType(
20841 EltTy: PrivateTy, NumElts: Dim, ASM: ArraySizeModifier::Normal,
20842 /*IndexTypeQuals=*/0);
20843 VarDecl *TempArrayVD =
20844 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: ArrayTy, Name: D->getName(),
20845 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20846 // Add a constructor to the temp decl.
20847 S.ActOnUninitializedDecl(dcl: TempArrayVD);
20848 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: ArrayTy, Loc: ELoc);
20849 TempArrayElem =
20850 S.DefaultFunctionArrayLvalueConversion(E: TempArrayRes.get());
20851 auto *Idx = new (S.Context)
20852 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
20853 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(Base: TempArrayElem.get(),
20854 LLoc: ELoc, Idx, RLoc: ELoc);
20855 }
20856 }
20857
20858 // OpenMP [2.15.4.6, Restrictions, p.2]
20859 // A list item that appears in an in_reduction clause of a task construct
20860 // must appear in a task_reduction clause of a construct associated with a
20861 // taskgroup region that includes the participating task in its taskgroup
20862 // set. The construct associated with the innermost region that meets this
20863 // condition must specify the same reduction-identifier as the in_reduction
20864 // clause.
20865 if (ClauseKind == OMPC_in_reduction) {
20866 SourceRange ParentSR;
20867 BinaryOperatorKind ParentBOK;
20868 const Expr *ParentReductionOp = nullptr;
20869 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr;
20870 DSAStackTy::DSAVarData ParentBOKDSA =
20871 Stack->getTopMostTaskgroupReductionData(D, SR&: ParentSR, BOK&: ParentBOK,
20872 TaskgroupDescriptor&: ParentBOKTD);
20873 DSAStackTy::DSAVarData ParentReductionOpDSA =
20874 Stack->getTopMostTaskgroupReductionData(
20875 D, SR&: ParentSR, ReductionRef&: ParentReductionOp, TaskgroupDescriptor&: ParentReductionOpTD);
20876 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
20877 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
20878 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
20879 (DeclareReductionRef.isUsable() && IsParentBOK) ||
20880 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) {
20881 bool EmitError = true;
20882 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
20883 llvm::FoldingSetNodeID RedId, ParentRedId;
20884 ParentReductionOp->Profile(ID&: ParentRedId, Context, /*Canonical=*/true);
20885 DeclareReductionRef.get()->Profile(ID&: RedId, Context,
20886 /*Canonical=*/true);
20887 EmitError = RedId != ParentRedId;
20888 }
20889 if (EmitError) {
20890 S.Diag(Loc: ReductionId.getBeginLoc(),
20891 DiagID: diag::err_omp_reduction_identifier_mismatch)
20892 << ReductionIdRange << RefExpr->getSourceRange();
20893 S.Diag(Loc: ParentSR.getBegin(),
20894 DiagID: diag::note_omp_previous_reduction_identifier)
20895 << ParentSR
20896 << (IsParentBOK ? ParentBOKDSA.RefExpr
20897 : ParentReductionOpDSA.RefExpr)
20898 ->getSourceRange();
20899 continue;
20900 }
20901 }
20902 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
20903 }
20904
20905 DeclRefExpr *Ref = nullptr;
20906 Expr *VarsExpr = RefExpr->IgnoreParens();
20907 if (!VD && !S.CurContext->isDependentContext()) {
20908 if (ASE || OASE) {
20909 TransformExprToCaptures RebuildToCapture(S, D);
20910 VarsExpr =
20911 RebuildToCapture.TransformExpr(E: RefExpr->IgnoreParens()).get();
20912 Ref = RebuildToCapture.getCapturedExpr();
20913 } else {
20914 VarsExpr = Ref = buildCapture(S, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
20915 }
20916 if (!S.OpenMP().isOpenMPCapturedDecl(D)) {
20917 RD.ExprCaptures.emplace_back(Args: Ref->getDecl());
20918 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
20919 ExprResult RefRes = S.DefaultLvalueConversion(E: Ref);
20920 if (!RefRes.isUsable())
20921 continue;
20922 ExprResult PostUpdateRes =
20923 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: SimpleRefExpr,
20924 RHSExpr: RefRes.get());
20925 if (!PostUpdateRes.isUsable())
20926 continue;
20927 if (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
20928 Stack->getCurrentDirective() == OMPD_taskgroup) {
20929 S.Diag(Loc: RefExpr->getExprLoc(),
20930 DiagID: diag::err_omp_reduction_non_addressable_expression)
20931 << RefExpr->getSourceRange();
20932 continue;
20933 }
20934 RD.ExprPostUpdates.emplace_back(
20935 Args: S.IgnoredValueConversions(E: PostUpdateRes.get()).get());
20936 }
20937 }
20938 }
20939 // All reduction items are still marked as reduction (to do not increase
20940 // code base size).
20941 unsigned Modifier = RD.RedModifier;
20942 // Consider task_reductions as reductions with task modifier. Required for
20943 // correct analysis of in_reduction clauses.
20944 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction)
20945 Modifier = OMPC_REDUCTION_task;
20946 Stack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_reduction, PrivateCopy: Ref, Modifier,
20947 AppliedToPointee: ASE || OASE);
20948 if (Modifier == OMPC_REDUCTION_task &&
20949 (CurrDir == OMPD_taskgroup ||
20950 ((isOpenMPParallelDirective(DKind: CurrDir) ||
20951 isOpenMPWorksharingDirective(DKind: CurrDir)) &&
20952 !isOpenMPSimdDirective(DKind: CurrDir)))) {
20953 if (DeclareReductionRef.isUsable())
20954 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange,
20955 ReductionRef: DeclareReductionRef.get());
20956 else
20957 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange, BOK);
20958 }
20959 RD.push(Item: VarsExpr, Private: PrivateDRE, LHS: LHSDRE, RHS: RHSDRE, ReductionOp: ReductionOp.get(),
20960 TaskgroupDescriptor, CopyOp: CopyOpRes.get(), CopyArrayTemp: TempArrayRes.get(),
20961 CopyArrayElem: TempArrayElem.get(), IsPrivate);
20962 }
20963 return RD.Vars.empty();
20964}
20965
20966OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
20967 ArrayRef<Expr *> VarList,
20968 OpenMPVarListDataTy::OpenMPReductionClauseModifiers Modifiers,
20969 SourceLocation StartLoc, SourceLocation LParenLoc,
20970 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
20971 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20972 ArrayRef<Expr *> UnresolvedReductions) {
20973 OpenMPReductionClauseModifier Modifier =
20974 static_cast<OpenMPReductionClauseModifier>(Modifiers.ExtraModifier);
20975 OpenMPOriginalSharingModifier OriginalSharingModifier =
20976 static_cast<OpenMPOriginalSharingModifier>(
20977 Modifiers.OriginalSharingModifier);
20978 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
20979 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_clause_value)
20980 << getListOfPossibleValues(K: OMPC_reduction, /*First=*/0,
20981 /*Last=*/OMPC_REDUCTION_unknown)
20982 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20983 return nullptr;
20984 }
20985 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
20986 // A reduction clause with the inscan reduction-modifier may only appear on a
20987 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
20988 // construct, a parallel worksharing-loop construct or a parallel
20989 // worksharing-loop SIMD construct.
20990 if (Modifier == OMPC_REDUCTION_inscan &&
20991 (DSAStack->getCurrentDirective() != OMPD_for &&
20992 DSAStack->getCurrentDirective() != OMPD_for_simd &&
20993 DSAStack->getCurrentDirective() != OMPD_simd &&
20994 DSAStack->getCurrentDirective() != OMPD_parallel_for &&
20995 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
20996 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_wrong_inscan_reduction);
20997 return nullptr;
20998 }
20999 ReductionData RD(VarList.size(), Modifier, OriginalSharingModifier);
21000 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_reduction, VarList,
21001 StartLoc, LParenLoc, ColonLoc, EndLoc,
21002 ReductionIdScopeSpec, ReductionId,
21003 UnresolvedReductions, RD))
21004 return nullptr;
21005
21006 return OMPReductionClause::Create(
21007 C: getASTContext(), StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc,
21008 Modifier, VL: RD.Vars,
21009 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
21010 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, CopyOps: RD.InscanCopyOps,
21011 CopyArrayTemps: RD.InscanCopyArrayTemps, CopyArrayElems: RD.InscanCopyArrayElems,
21012 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
21013 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates), IsPrivateVarReduction: RD.IsPrivateVarReduction,
21014 OriginalSharingModifier);
21015}
21016
21017OMPClause *SemaOpenMP::ActOnOpenMPTaskReductionClause(
21018 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
21019 SourceLocation ColonLoc, SourceLocation EndLoc,
21020 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
21021 ArrayRef<Expr *> UnresolvedReductions) {
21022 ReductionData RD(VarList.size());
21023 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_task_reduction,
21024 VarList, StartLoc, LParenLoc, ColonLoc,
21025 EndLoc, ReductionIdScopeSpec, ReductionId,
21026 UnresolvedReductions, RD))
21027 return nullptr;
21028
21029 return OMPTaskReductionClause::Create(
21030 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
21031 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
21032 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps,
21033 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
21034 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
21035}
21036
21037OMPClause *SemaOpenMP::ActOnOpenMPInReductionClause(
21038 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
21039 SourceLocation ColonLoc, SourceLocation EndLoc,
21040 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
21041 ArrayRef<Expr *> UnresolvedReductions) {
21042 ReductionData RD(VarList.size());
21043 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_in_reduction, VarList,
21044 StartLoc, LParenLoc, ColonLoc, EndLoc,
21045 ReductionIdScopeSpec, ReductionId,
21046 UnresolvedReductions, RD))
21047 return nullptr;
21048
21049 return OMPInReductionClause::Create(
21050 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
21051 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
21052 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, TaskgroupDescriptors: RD.TaskgroupDescriptors,
21053 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
21054 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
21055}
21056
21057bool SemaOpenMP::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
21058 SourceLocation LinLoc) {
21059 if ((!getLangOpts().CPlusPlus && LinKind != OMPC_LINEAR_val) ||
21060 LinKind == OMPC_LINEAR_unknown || LinKind == OMPC_LINEAR_step) {
21061 Diag(Loc: LinLoc, DiagID: diag::err_omp_wrong_linear_modifier)
21062 << getLangOpts().CPlusPlus;
21063 return true;
21064 }
21065 return false;
21066}
21067
21068bool SemaOpenMP::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
21069 OpenMPLinearClauseKind LinKind,
21070 QualType Type, bool IsDeclareSimd) {
21071 const auto *VD = dyn_cast_or_null<VarDecl>(Val: D);
21072 // A variable must not have an incomplete type or a reference type.
21073 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
21074 DiagID: diag::err_omp_linear_incomplete_type))
21075 return true;
21076 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
21077 !Type->isReferenceType()) {
21078 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_linear_modifier_non_reference)
21079 << Type << getOpenMPSimpleClauseTypeName(Kind: OMPC_linear, Type: LinKind);
21080 return true;
21081 }
21082 Type = Type.getNonReferenceType();
21083
21084 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
21085 // A variable that is privatized must not have a const-qualified type
21086 // unless it is of class type with a mutable member. This restriction does
21087 // not apply to the firstprivate clause, nor to the linear clause on
21088 // declarative directives (like declare simd).
21089 if (!IsDeclareSimd &&
21090 rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_linear, ELoc))
21091 return true;
21092
21093 // A list item must be of integral or pointer type.
21094 Type = Type.getUnqualifiedType().getCanonicalType();
21095 const auto *Ty = Type.getTypePtrOrNull();
21096 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() &&
21097 !Ty->isIntegralType(Ctx: getASTContext()) && !Ty->isPointerType())) {
21098 Diag(Loc: ELoc, DiagID: diag::err_omp_linear_expected_int_or_ptr) << Type;
21099 if (D) {
21100 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21101 VarDecl::DeclarationOnly;
21102 Diag(Loc: D->getLocation(),
21103 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21104 << D;
21105 }
21106 return true;
21107 }
21108 return false;
21109}
21110
21111OMPClause *SemaOpenMP::ActOnOpenMPLinearClause(
21112 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
21113 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
21114 SourceLocation LinLoc, SourceLocation ColonLoc,
21115 SourceLocation StepModifierLoc, SourceLocation EndLoc) {
21116 SmallVector<Expr *, 8> Vars;
21117 SmallVector<Expr *, 8> Privates;
21118 SmallVector<Expr *, 8> Inits;
21119 SmallVector<Decl *, 4> ExprCaptures;
21120 SmallVector<Expr *, 4> ExprPostUpdates;
21121 // OpenMP 5.2 [Section 5.4.6, linear clause]
21122 // step-simple-modifier is exclusive, can't be used with 'val', 'uval', or
21123 // 'ref'
21124 if (LinLoc.isValid() && StepModifierLoc.isInvalid() && Step &&
21125 getLangOpts().OpenMP >= 52)
21126 Diag(Loc: Step->getBeginLoc(), DiagID: diag::err_omp_step_simple_modifier_exclusive);
21127 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
21128 LinKind = OMPC_LINEAR_val;
21129 for (Expr *RefExpr : VarList) {
21130 assert(RefExpr && "NULL expr in OpenMP linear clause.");
21131 SourceLocation ELoc;
21132 SourceRange ERange;
21133 Expr *SimpleRefExpr = RefExpr;
21134 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21135 if (Res.second) {
21136 // It will be analyzed later.
21137 Vars.push_back(Elt: RefExpr);
21138 Privates.push_back(Elt: nullptr);
21139 Inits.push_back(Elt: nullptr);
21140 }
21141 ValueDecl *D = Res.first;
21142 if (!D)
21143 continue;
21144
21145 QualType Type = D->getType();
21146 auto *VD = dyn_cast<VarDecl>(Val: D);
21147
21148 // OpenMP [2.14.3.7, linear clause]
21149 // A list-item cannot appear in more than one linear clause.
21150 // A list-item that appears in a linear clause cannot appear in any
21151 // other data-sharing attribute clause.
21152 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21153 if (DVar.RefExpr) {
21154 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21155 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21156 << getOpenMPClauseNameForDiag(C: OMPC_linear);
21157 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21158 continue;
21159 }
21160
21161 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
21162 continue;
21163 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
21164
21165 // Build private copy of original var.
21166 VarDecl *Private =
21167 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
21168 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
21169 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
21170 DeclRefExpr *PrivateRef = buildDeclRefExpr(S&: SemaRef, D: Private, Ty: Type, Loc: ELoc);
21171 // Build var to save initial value.
21172 VarDecl *Init = buildVarDecl(SemaRef, Loc: ELoc, Type, Name: ".linear.start");
21173 Expr *InitExpr;
21174 DeclRefExpr *Ref = nullptr;
21175 if (!VD && !SemaRef.CurContext->isDependentContext()) {
21176 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
21177 if (!isOpenMPCapturedDecl(D)) {
21178 ExprCaptures.push_back(Elt: Ref->getDecl());
21179 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
21180 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
21181 if (!RefRes.isUsable())
21182 continue;
21183 ExprResult PostUpdateRes =
21184 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
21185 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
21186 if (!PostUpdateRes.isUsable())
21187 continue;
21188 ExprPostUpdates.push_back(
21189 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
21190 }
21191 }
21192 }
21193 if (LinKind == OMPC_LINEAR_uval)
21194 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
21195 else
21196 InitExpr = VD ? SimpleRefExpr : Ref;
21197 SemaRef.AddInitializerToDecl(
21198 dcl: Init, init: SemaRef.DefaultLvalueConversion(E: InitExpr).get(),
21199 /*DirectInit=*/false);
21200 DeclRefExpr *InitRef = buildDeclRefExpr(S&: SemaRef, D: Init, Ty: Type, Loc: ELoc);
21201
21202 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_linear, PrivateCopy: Ref);
21203 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
21204 ? RefExpr->IgnoreParens()
21205 : Ref);
21206 Privates.push_back(Elt: PrivateRef);
21207 Inits.push_back(Elt: InitRef);
21208 }
21209
21210 if (Vars.empty())
21211 return nullptr;
21212
21213 Expr *StepExpr = Step;
21214 Expr *CalcStepExpr = nullptr;
21215 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
21216 !Step->isInstantiationDependent() &&
21217 !Step->containsUnexpandedParameterPack()) {
21218 SourceLocation StepLoc = Step->getBeginLoc();
21219 ExprResult Val = PerformOpenMPImplicitIntegerConversion(Loc: StepLoc, Op: Step);
21220 if (Val.isInvalid())
21221 return nullptr;
21222 StepExpr = Val.get();
21223
21224 // Build var to save the step value.
21225 VarDecl *SaveVar =
21226 buildVarDecl(SemaRef, Loc: StepLoc, Type: StepExpr->getType(), Name: ".linear.step");
21227 ExprResult SaveRef =
21228 buildDeclRefExpr(S&: SemaRef, D: SaveVar, Ty: StepExpr->getType(), Loc: StepLoc);
21229 ExprResult CalcStep = SemaRef.BuildBinOp(
21230 S: SemaRef.getCurScope(), OpLoc: StepLoc, Opc: BO_Assign, LHSExpr: SaveRef.get(), RHSExpr: StepExpr);
21231 CalcStep =
21232 SemaRef.ActOnFinishFullExpr(Expr: CalcStep.get(), /*DiscardedValue=*/false);
21233
21234 // Warn about zero linear step (it would be probably better specified as
21235 // making corresponding variables 'const').
21236 if (std::optional<llvm::APSInt> Result =
21237 StepExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
21238 if (!Result->isNegative() && !Result->isStrictlyPositive())
21239 Diag(Loc: StepLoc, DiagID: diag::warn_omp_linear_step_zero)
21240 << Vars[0] << (Vars.size() > 1);
21241 } else if (CalcStep.isUsable()) {
21242 // Calculate the step beforehand instead of doing this on each iteration.
21243 // (This is not used if the number of iterations may be kfold-ed).
21244 CalcStepExpr = CalcStep.get();
21245 }
21246 }
21247
21248 return OMPLinearClause::Create(C: getASTContext(), StartLoc, LParenLoc, Modifier: LinKind,
21249 ModifierLoc: LinLoc, ColonLoc, StepModifierLoc, EndLoc,
21250 VL: Vars, PL: Privates, IL: Inits, Step: StepExpr, CalcStep: CalcStepExpr,
21251 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
21252 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
21253}
21254
21255static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
21256 Expr *NumIterations, Sema &SemaRef,
21257 Scope *S, DSAStackTy *Stack) {
21258 // Walk the vars and build update/final expressions for the CodeGen.
21259 SmallVector<Expr *, 8> Updates;
21260 SmallVector<Expr *, 8> Finals;
21261 SmallVector<Expr *, 8> UsedExprs;
21262 Expr *Step = Clause.getStep();
21263 Expr *CalcStep = Clause.getCalcStep();
21264 // OpenMP [2.14.3.7, linear clause]
21265 // If linear-step is not specified it is assumed to be 1.
21266 if (!Step)
21267 Step = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
21268 else if (CalcStep)
21269 Step = cast<BinaryOperator>(Val: CalcStep)->getLHS();
21270 bool HasErrors = false;
21271 auto CurInit = Clause.inits().begin();
21272 auto CurPrivate = Clause.privates().begin();
21273 OpenMPLinearClauseKind LinKind = Clause.getModifier();
21274 for (Expr *RefExpr : Clause.varlist()) {
21275 SourceLocation ELoc;
21276 SourceRange ERange;
21277 Expr *SimpleRefExpr = RefExpr;
21278 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21279 ValueDecl *D = Res.first;
21280 if (Res.second || !D) {
21281 Updates.push_back(Elt: nullptr);
21282 Finals.push_back(Elt: nullptr);
21283 HasErrors = true;
21284 continue;
21285 }
21286 auto &&Info = Stack->isLoopControlVariable(D);
21287 // OpenMP [2.15.11, distribute simd Construct]
21288 // A list item may not appear in a linear clause, unless it is the loop
21289 // iteration variable.
21290 if (isOpenMPDistributeDirective(DKind: Stack->getCurrentDirective()) &&
21291 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()) && !Info.first) {
21292 SemaRef.Diag(Loc: ELoc,
21293 DiagID: diag::err_omp_linear_distribute_var_non_loop_iteration);
21294 Updates.push_back(Elt: nullptr);
21295 Finals.push_back(Elt: nullptr);
21296 HasErrors = true;
21297 continue;
21298 }
21299 Expr *InitExpr = *CurInit;
21300
21301 // Build privatized reference to the current linear var.
21302 auto *DE = cast<DeclRefExpr>(Val: SimpleRefExpr);
21303 Expr *CapturedRef;
21304 if (LinKind == OMPC_LINEAR_uval)
21305 CapturedRef = cast<VarDecl>(Val: DE->getDecl())->getInit();
21306 else
21307 CapturedRef =
21308 buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: DE->getDecl()),
21309 Ty: DE->getType().getUnqualifiedType(), Loc: DE->getExprLoc(),
21310 /*RefersToCapture=*/true);
21311
21312 // Build update: Var = InitExpr + IV * Step
21313 ExprResult Update;
21314 if (!Info.first)
21315 Update = buildCounterUpdate(
21316 SemaRef, S, Loc: RefExpr->getExprLoc(), VarRef: *CurPrivate, Start: InitExpr, Iter: IV, Step,
21317 /*Subtract=*/false, /*IsNonRectangularLB=*/false);
21318 else
21319 Update = *CurPrivate;
21320 Update = SemaRef.ActOnFinishFullExpr(Expr: Update.get(), CC: DE->getBeginLoc(),
21321 /*DiscardedValue=*/false);
21322
21323 // Build final: Var = PrivCopy;
21324 ExprResult Final;
21325 if (!Info.first)
21326 Final = SemaRef.BuildBinOp(
21327 S, OpLoc: RefExpr->getExprLoc(), Opc: BO_Assign, LHSExpr: CapturedRef,
21328 RHSExpr: SemaRef.DefaultLvalueConversion(E: *CurPrivate).get());
21329 else
21330 Final = *CurPrivate;
21331 Final = SemaRef.ActOnFinishFullExpr(Expr: Final.get(), CC: DE->getBeginLoc(),
21332 /*DiscardedValue=*/false);
21333
21334 if (!Update.isUsable() || !Final.isUsable()) {
21335 Updates.push_back(Elt: nullptr);
21336 Finals.push_back(Elt: nullptr);
21337 UsedExprs.push_back(Elt: nullptr);
21338 HasErrors = true;
21339 } else {
21340 Updates.push_back(Elt: Update.get());
21341 Finals.push_back(Elt: Final.get());
21342 if (!Info.first)
21343 UsedExprs.push_back(Elt: SimpleRefExpr);
21344 }
21345 ++CurInit;
21346 ++CurPrivate;
21347 }
21348 if (Expr *S = Clause.getStep())
21349 UsedExprs.push_back(Elt: S);
21350 // Fill the remaining part with the nullptr.
21351 UsedExprs.append(NumInputs: Clause.varlist_size() + 1 - UsedExprs.size(), Elt: nullptr);
21352 Clause.setUpdates(Updates);
21353 Clause.setFinals(Finals);
21354 Clause.setUsedExprs(UsedExprs);
21355 return HasErrors;
21356}
21357
21358OMPClause *SemaOpenMP::ActOnOpenMPAlignedClause(
21359 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
21360 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
21361 SmallVector<Expr *, 8> Vars;
21362 for (Expr *RefExpr : VarList) {
21363 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
21364 SourceLocation ELoc;
21365 SourceRange ERange;
21366 Expr *SimpleRefExpr = RefExpr;
21367 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21368 if (Res.second) {
21369 // It will be analyzed later.
21370 Vars.push_back(Elt: RefExpr);
21371 }
21372 ValueDecl *D = Res.first;
21373 if (!D)
21374 continue;
21375
21376 QualType QType = D->getType();
21377 auto *VD = dyn_cast<VarDecl>(Val: D);
21378
21379 // OpenMP [2.8.1, simd construct, Restrictions]
21380 // The type of list items appearing in the aligned clause must be
21381 // array, pointer, reference to array, or reference to pointer.
21382 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
21383 const Type *Ty = QType.getTypePtrOrNull();
21384 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
21385 Diag(Loc: ELoc, DiagID: diag::err_omp_aligned_expected_array_or_ptr)
21386 << QType << getLangOpts().CPlusPlus << ERange;
21387 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21388 VarDecl::DeclarationOnly;
21389 Diag(Loc: D->getLocation(),
21390 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21391 << D;
21392 continue;
21393 }
21394
21395 // OpenMP [2.8.1, simd construct, Restrictions]
21396 // A list-item cannot appear in more than one aligned clause.
21397 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, NewDE: SimpleRefExpr)) {
21398 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
21399 << 0 << getOpenMPClauseNameForDiag(C: OMPC_aligned) << ERange;
21400 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
21401 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
21402 continue;
21403 }
21404
21405 DeclRefExpr *Ref = nullptr;
21406 if (!VD && isOpenMPCapturedDecl(D))
21407 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
21408 Vars.push_back(Elt: SemaRef
21409 .DefaultFunctionArrayConversion(
21410 E: (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
21411 .get());
21412 }
21413
21414 // OpenMP [2.8.1, simd construct, Description]
21415 // The parameter of the aligned clause, alignment, must be a constant
21416 // positive integer expression.
21417 // If no optional parameter is specified, implementation-defined default
21418 // alignments for SIMD instructions on the target platforms are assumed.
21419 if (Alignment != nullptr) {
21420 ExprResult AlignResult =
21421 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_aligned);
21422 if (AlignResult.isInvalid())
21423 return nullptr;
21424 Alignment = AlignResult.get();
21425 }
21426 if (Vars.empty())
21427 return nullptr;
21428
21429 return OMPAlignedClause::Create(C: getASTContext(), StartLoc, LParenLoc,
21430 ColonLoc, EndLoc, VL: Vars, A: Alignment);
21431}
21432
21433OMPClause *SemaOpenMP::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
21434 SourceLocation StartLoc,
21435 SourceLocation LParenLoc,
21436 SourceLocation EndLoc) {
21437 SmallVector<Expr *, 8> Vars;
21438 SmallVector<Expr *, 8> SrcExprs;
21439 SmallVector<Expr *, 8> DstExprs;
21440 SmallVector<Expr *, 8> AssignmentOps;
21441 for (Expr *RefExpr : VarList) {
21442 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
21443 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21444 // It will be analyzed later.
21445 Vars.push_back(Elt: RefExpr);
21446 SrcExprs.push_back(Elt: nullptr);
21447 DstExprs.push_back(Elt: nullptr);
21448 AssignmentOps.push_back(Elt: nullptr);
21449 continue;
21450 }
21451
21452 SourceLocation ELoc = RefExpr->getExprLoc();
21453 // OpenMP [2.1, C/C++]
21454 // A list item is a variable name.
21455 // OpenMP [2.14.4.1, Restrictions, p.1]
21456 // A list item that appears in a copyin clause must be threadprivate.
21457 auto *DE = dyn_cast<DeclRefExpr>(Val: RefExpr);
21458 if (!DE || !isa<VarDecl>(Val: DE->getDecl())) {
21459 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr)
21460 << 0 << RefExpr->getSourceRange();
21461 continue;
21462 }
21463
21464 Decl *D = DE->getDecl();
21465 auto *VD = cast<VarDecl>(Val: D);
21466
21467 QualType Type = VD->getType();
21468 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
21469 // It will be analyzed later.
21470 Vars.push_back(Elt: DE);
21471 SrcExprs.push_back(Elt: nullptr);
21472 DstExprs.push_back(Elt: nullptr);
21473 AssignmentOps.push_back(Elt: nullptr);
21474 continue;
21475 }
21476
21477 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
21478 // A list item that appears in a copyin clause must be threadprivate.
21479 if (!DSAStack->isThreadPrivate(D: VD)) {
21480 unsigned OMPVersion = getLangOpts().OpenMP;
21481 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21482 << getOpenMPClauseNameForDiag(C: OMPC_copyin)
21483 << getOpenMPDirectiveName(D: OMPD_threadprivate, Ver: OMPVersion);
21484 continue;
21485 }
21486
21487 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
21488 // A variable of class type (or array thereof) that appears in a
21489 // copyin clause requires an accessible, unambiguous copy assignment
21490 // operator for the class type.
21491 QualType ElemType =
21492 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
21493 VarDecl *SrcVD =
21494 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType.getUnqualifiedType(),
21495 Name: ".copyin.src", Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
21496 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
21497 S&: SemaRef, D: SrcVD, Ty: ElemType.getUnqualifiedType(), Loc: DE->getExprLoc());
21498 VarDecl *DstVD =
21499 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType, Name: ".copyin.dst",
21500 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
21501 DeclRefExpr *PseudoDstExpr =
21502 buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: ElemType, Loc: DE->getExprLoc());
21503 // For arrays generate assignment operation for single element and replace
21504 // it by the original array element in CodeGen.
21505 ExprResult AssignmentOp =
21506 SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: DE->getExprLoc(), Opc: BO_Assign,
21507 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
21508 if (AssignmentOp.isInvalid())
21509 continue;
21510 AssignmentOp =
21511 SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: DE->getExprLoc(),
21512 /*DiscardedValue=*/false);
21513 if (AssignmentOp.isInvalid())
21514 continue;
21515
21516 DSAStack->addDSA(D: VD, E: DE, A: OMPC_copyin);
21517 Vars.push_back(Elt: DE);
21518 SrcExprs.push_back(Elt: PseudoSrcExpr);
21519 DstExprs.push_back(Elt: PseudoDstExpr);
21520 AssignmentOps.push_back(Elt: AssignmentOp.get());
21521 }
21522
21523 if (Vars.empty())
21524 return nullptr;
21525
21526 return OMPCopyinClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21527 VL: Vars, SrcExprs, DstExprs, AssignmentOps);
21528}
21529
21530OMPClause *SemaOpenMP::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
21531 SourceLocation StartLoc,
21532 SourceLocation LParenLoc,
21533 SourceLocation EndLoc) {
21534 SmallVector<Expr *, 8> Vars;
21535 SmallVector<Expr *, 8> SrcExprs;
21536 SmallVector<Expr *, 8> DstExprs;
21537 SmallVector<Expr *, 8> AssignmentOps;
21538 for (Expr *RefExpr : VarList) {
21539 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
21540 SourceLocation ELoc;
21541 SourceRange ERange;
21542 Expr *SimpleRefExpr = RefExpr;
21543 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21544 if (Res.second) {
21545 // It will be analyzed later.
21546 Vars.push_back(Elt: RefExpr);
21547 SrcExprs.push_back(Elt: nullptr);
21548 DstExprs.push_back(Elt: nullptr);
21549 AssignmentOps.push_back(Elt: nullptr);
21550 }
21551 ValueDecl *D = Res.first;
21552 if (!D)
21553 continue;
21554
21555 QualType Type = D->getType();
21556 auto *VD = dyn_cast<VarDecl>(Val: D);
21557
21558 // OpenMP [2.14.4.2, Restrictions, p.2]
21559 // A list item that appears in a copyprivate clause may not appear in a
21560 // private or firstprivate clause on the single construct.
21561 if (!VD || !DSAStack->isThreadPrivate(D: VD)) {
21562 DSAStackTy::DSAVarData DVar =
21563 DSAStack->getTopDSA(D, /*FromParent=*/false);
21564 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
21565 DVar.RefExpr) {
21566 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21567 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21568 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate);
21569 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21570 continue;
21571 }
21572
21573 // OpenMP [2.11.4.2, Restrictions, p.1]
21574 // All list items that appear in a copyprivate clause must be either
21575 // threadprivate or private in the enclosing context.
21576 if (DVar.CKind == OMPC_unknown) {
21577 DVar = DSAStack->getImplicitDSA(D, FromParent: false);
21578 if (DVar.CKind == OMPC_shared) {
21579 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21580 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate)
21581 << "threadprivate or private in the enclosing context";
21582 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21583 continue;
21584 }
21585 }
21586 }
21587
21588 // Variably modified types are not supported.
21589 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
21590 unsigned OMPVersion = getLangOpts().OpenMP;
21591 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
21592 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate) << Type
21593 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
21594 Ver: OMPVersion);
21595 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21596 VarDecl::DeclarationOnly;
21597 Diag(Loc: D->getLocation(),
21598 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21599 << D;
21600 continue;
21601 }
21602
21603 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
21604 // A variable of class type (or array thereof) that appears in a
21605 // copyin clause requires an accessible, unambiguous copy assignment
21606 // operator for the class type.
21607 Type = getASTContext()
21608 .getBaseElementType(QT: Type.getNonReferenceType())
21609 .getUnqualifiedType();
21610 VarDecl *SrcVD =
21611 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.src",
21612 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21613 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type, Loc: ELoc);
21614 VarDecl *DstVD =
21615 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.dst",
21616 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21617 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
21618 ExprResult AssignmentOp = SemaRef.BuildBinOp(
21619 DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
21620 if (AssignmentOp.isInvalid())
21621 continue;
21622 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
21623 /*DiscardedValue=*/false);
21624 if (AssignmentOp.isInvalid())
21625 continue;
21626
21627 // No need to mark vars as copyprivate, they are already threadprivate or
21628 // implicitly private.
21629 assert(VD || isOpenMPCapturedDecl(D));
21630 Vars.push_back(
21631 Elt: VD ? RefExpr->IgnoreParens()
21632 : buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false));
21633 SrcExprs.push_back(Elt: PseudoSrcExpr);
21634 DstExprs.push_back(Elt: PseudoDstExpr);
21635 AssignmentOps.push_back(Elt: AssignmentOp.get());
21636 }
21637
21638 if (Vars.empty())
21639 return nullptr;
21640
21641 return OMPCopyprivateClause::Create(C: getASTContext(), StartLoc, LParenLoc,
21642 EndLoc, VL: Vars, SrcExprs, DstExprs,
21643 AssignmentOps);
21644}
21645
21646OMPClause *SemaOpenMP::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
21647 SourceLocation StartLoc,
21648 SourceLocation LParenLoc,
21649 SourceLocation EndLoc) {
21650 if (VarList.empty())
21651 return nullptr;
21652
21653 return OMPFlushClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21654 VL: VarList);
21655}
21656
21657/// Tries to find omp_depend_t. type.
21658static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack,
21659 bool Diagnose = true) {
21660 QualType OMPDependT = Stack->getOMPDependT();
21661 if (!OMPDependT.isNull())
21662 return true;
21663 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_depend_t");
21664 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
21665 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
21666 if (Diagnose)
21667 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_depend_t";
21668 return false;
21669 }
21670 Stack->setOMPDependT(PT.get());
21671 return true;
21672}
21673
21674OMPClause *SemaOpenMP::ActOnOpenMPDepobjClause(Expr *Depobj,
21675 SourceLocation StartLoc,
21676 SourceLocation LParenLoc,
21677 SourceLocation EndLoc) {
21678 if (!Depobj)
21679 return nullptr;
21680
21681 bool OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack);
21682
21683 // OpenMP 5.0, 2.17.10.1 depobj Construct
21684 // depobj is an lvalue expression of type omp_depend_t.
21685 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() &&
21686 !Depobj->isInstantiationDependent() &&
21687 !Depobj->containsUnexpandedParameterPack() &&
21688 (OMPDependTFound && !getASTContext().typesAreCompatible(
21689 DSAStack->getOMPDependT(), T2: Depobj->getType(),
21690 /*CompareUnqualified=*/true))) {
21691 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21692 << 0 << Depobj->getType() << Depobj->getSourceRange();
21693 }
21694
21695 if (!Depobj->isLValue()) {
21696 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21697 << 1 << Depobj->getSourceRange();
21698 }
21699
21700 return OMPDepobjClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21701 Depobj);
21702}
21703
21704namespace {
21705// Utility struct that gathers the related info for doacross clause.
21706struct DoacrossDataInfoTy {
21707 // The list of expressions.
21708 SmallVector<Expr *, 8> Vars;
21709 // The OperatorOffset for doacross loop.
21710 DSAStackTy::OperatorOffsetTy OpsOffs;
21711 // The depended loop count.
21712 llvm::APSInt TotalDepCount;
21713};
21714} // namespace
21715static DoacrossDataInfoTy
21716ProcessOpenMPDoacrossClauseCommon(Sema &SemaRef, bool IsSource,
21717 ArrayRef<Expr *> VarList, DSAStackTy *Stack,
21718 SourceLocation EndLoc) {
21719
21720 SmallVector<Expr *, 8> Vars;
21721 DSAStackTy::OperatorOffsetTy OpsOffs;
21722 llvm::APSInt DepCounter(/*BitWidth=*/32);
21723 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
21724
21725 if (const Expr *OrderedCountExpr =
21726 Stack->getParentOrderedRegionParam().first) {
21727 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Ctx: SemaRef.Context);
21728 TotalDepCount.setIsUnsigned(/*Val=*/true);
21729 }
21730
21731 for (Expr *RefExpr : VarList) {
21732 assert(RefExpr && "NULL expr in OpenMP doacross clause.");
21733 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21734 // It will be analyzed later.
21735 Vars.push_back(Elt: RefExpr);
21736 continue;
21737 }
21738
21739 SourceLocation ELoc = RefExpr->getExprLoc();
21740 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
21741 if (!IsSource) {
21742 if (Stack->getParentOrderedRegionParam().first &&
21743 DepCounter >= TotalDepCount) {
21744 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_unexpected_expr);
21745 continue;
21746 }
21747 ++DepCounter;
21748 // OpenMP [2.13.9, Summary]
21749 // depend(dependence-type : vec), where dependence-type is:
21750 // 'sink' and where vec is the iteration vector, which has the form:
21751 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
21752 // where n is the value specified by the ordered clause in the loop
21753 // directive, xi denotes the loop iteration variable of the i-th nested
21754 // loop associated with the loop directive, and di is a constant
21755 // non-negative integer.
21756 if (SemaRef.CurContext->isDependentContext()) {
21757 // It will be analyzed later.
21758 Vars.push_back(Elt: RefExpr);
21759 continue;
21760 }
21761 SimpleExpr = SimpleExpr->IgnoreImplicit();
21762 OverloadedOperatorKind OOK = OO_None;
21763 SourceLocation OOLoc;
21764 Expr *LHS = SimpleExpr;
21765 Expr *RHS = nullptr;
21766 if (auto *BO = dyn_cast<BinaryOperator>(Val: SimpleExpr)) {
21767 OOK = BinaryOperator::getOverloadedOperator(Opc: BO->getOpcode());
21768 OOLoc = BO->getOperatorLoc();
21769 LHS = BO->getLHS()->IgnoreParenImpCasts();
21770 RHS = BO->getRHS()->IgnoreParenImpCasts();
21771 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: SimpleExpr)) {
21772 OOK = OCE->getOperator();
21773 OOLoc = OCE->getOperatorLoc();
21774 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
21775 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
21776 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Val: SimpleExpr)) {
21777 OOK = MCE->getMethodDecl()
21778 ->getNameInfo()
21779 .getName()
21780 .getCXXOverloadedOperator();
21781 OOLoc = MCE->getCallee()->getExprLoc();
21782 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
21783 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
21784 }
21785 SourceLocation ELoc;
21786 SourceRange ERange;
21787 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: LHS, ELoc, ERange);
21788 if (Res.second) {
21789 // It will be analyzed later.
21790 Vars.push_back(Elt: RefExpr);
21791 }
21792 ValueDecl *D = Res.first;
21793 if (!D)
21794 continue;
21795
21796 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
21797 SemaRef.Diag(Loc: OOLoc, DiagID: diag::err_omp_depend_sink_expected_plus_minus);
21798 continue;
21799 }
21800 if (RHS) {
21801 ExprResult RHSRes =
21802 SemaRef.OpenMP().VerifyPositiveIntegerConstantInClause(
21803 E: RHS, CKind: OMPC_depend, /*StrictlyPositive=*/false);
21804 if (RHSRes.isInvalid())
21805 continue;
21806 }
21807 if (!SemaRef.CurContext->isDependentContext() &&
21808 Stack->getParentOrderedRegionParam().first &&
21809 DepCounter != Stack->isParentLoopControlVariable(D).first) {
21810 const ValueDecl *VD =
21811 Stack->getParentLoopControlVariable(I: DepCounter.getZExtValue());
21812 if (VD)
21813 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21814 << 1 << VD;
21815 else
21816 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21817 << 0;
21818 continue;
21819 }
21820 OpsOffs.emplace_back(Args&: RHS, Args&: OOK);
21821 }
21822 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
21823 }
21824 if (!SemaRef.CurContext->isDependentContext() && !IsSource &&
21825 TotalDepCount > VarList.size() &&
21826 Stack->getParentOrderedRegionParam().first &&
21827 Stack->getParentLoopControlVariable(I: VarList.size() + 1)) {
21828 SemaRef.Diag(Loc: EndLoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21829 << 1 << Stack->getParentLoopControlVariable(I: VarList.size() + 1);
21830 }
21831 return {.Vars: Vars, .OpsOffs: OpsOffs, .TotalDepCount: TotalDepCount};
21832}
21833
21834OMPClause *SemaOpenMP::ActOnOpenMPDependClause(
21835 const OMPDependClause::DependDataTy &Data, Expr *DepModifier,
21836 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
21837 SourceLocation EndLoc) {
21838 OpenMPDependClauseKind DepKind = Data.DepKind;
21839 SourceLocation DepLoc = Data.DepLoc;
21840 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
21841 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
21842 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
21843 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_depend);
21844 return nullptr;
21845 }
21846 if (DSAStack->getCurrentDirective() == OMPD_taskwait &&
21847 DepKind == OMPC_DEPEND_mutexinoutset) {
21848 Diag(Loc: DepLoc, DiagID: diag::err_omp_taskwait_depend_mutexinoutset_not_allowed);
21849 return nullptr;
21850 }
21851 if ((DSAStack->getCurrentDirective() != OMPD_ordered ||
21852 DSAStack->getCurrentDirective() == OMPD_depobj) &&
21853 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
21854 DepKind == OMPC_DEPEND_sink ||
21855 ((getLangOpts().OpenMP < 50 ||
21856 DSAStack->getCurrentDirective() == OMPD_depobj) &&
21857 DepKind == OMPC_DEPEND_depobj))) {
21858 SmallVector<unsigned, 6> Except = {OMPC_DEPEND_source, OMPC_DEPEND_sink,
21859 OMPC_DEPEND_outallmemory,
21860 OMPC_DEPEND_inoutallmemory};
21861 if (getLangOpts().OpenMP < 50 ||
21862 DSAStack->getCurrentDirective() == OMPD_depobj)
21863 Except.push_back(Elt: OMPC_DEPEND_depobj);
21864 if (getLangOpts().OpenMP < 51)
21865 Except.push_back(Elt: OMPC_DEPEND_inoutset);
21866 std::string Expected = (getLangOpts().OpenMP >= 50 && !DepModifier)
21867 ? "depend modifier(iterator) or "
21868 : "";
21869 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
21870 << Expected + getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
21871 /*Last=*/OMPC_DEPEND_unknown,
21872 Exclude: Except)
21873 << getOpenMPClauseNameForDiag(C: OMPC_depend);
21874 return nullptr;
21875 }
21876 if (DepModifier &&
21877 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) {
21878 Diag(Loc: DepModifier->getExprLoc(),
21879 DiagID: diag::err_omp_depend_sink_source_with_modifier);
21880 return nullptr;
21881 }
21882 if (DepModifier &&
21883 !DepModifier->getType()->isSpecificBuiltinType(K: BuiltinType::OMPIterator))
21884 Diag(Loc: DepModifier->getExprLoc(), DiagID: diag::err_omp_depend_modifier_not_iterator);
21885
21886 SmallVector<Expr *, 8> Vars;
21887 DSAStackTy::OperatorOffsetTy OpsOffs;
21888 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
21889
21890 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
21891 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
21892 SemaRef, IsSource: DepKind == OMPC_DEPEND_source, VarList, DSAStack, EndLoc);
21893 Vars = VarOffset.Vars;
21894 OpsOffs = VarOffset.OpsOffs;
21895 TotalDepCount = VarOffset.TotalDepCount;
21896 } else {
21897 for (Expr *RefExpr : VarList) {
21898 assert(RefExpr && "NULL expr in OpenMP depend clause.");
21899 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21900 // It will be analyzed later.
21901 Vars.push_back(Elt: RefExpr);
21902 continue;
21903 }
21904
21905 SourceLocation ELoc = RefExpr->getExprLoc();
21906 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
21907 if (DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) {
21908 bool OMPDependTFound = getLangOpts().OpenMP >= 50;
21909 if (OMPDependTFound)
21910 OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack,
21911 Diagnose: DepKind == OMPC_DEPEND_depobj);
21912 if (DepKind == OMPC_DEPEND_depobj) {
21913 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
21914 // List items used in depend clauses with the depobj dependence type
21915 // must be expressions of the omp_depend_t type.
21916 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
21917 !RefExpr->isInstantiationDependent() &&
21918 !RefExpr->containsUnexpandedParameterPack() &&
21919 (OMPDependTFound &&
21920 !getASTContext().hasSameUnqualifiedType(
21921 DSAStack->getOMPDependT(), T2: RefExpr->getType()))) {
21922 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21923 << 0 << RefExpr->getType() << RefExpr->getSourceRange();
21924 continue;
21925 }
21926 if (!RefExpr->isLValue()) {
21927 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21928 << 1 << RefExpr->getType() << RefExpr->getSourceRange();
21929 continue;
21930 }
21931 } else {
21932 // OpenMP 5.0 [2.17.11, Restrictions]
21933 // List items used in depend clauses cannot be zero-length array
21934 // sections.
21935 QualType ExprTy = RefExpr->getType().getNonReferenceType();
21936 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: SimpleExpr);
21937 if (OASE) {
21938 QualType BaseType =
21939 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
21940 if (BaseType.isNull())
21941 return nullptr;
21942 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
21943 ExprTy = ATy->getElementType();
21944 else
21945 ExprTy = BaseType->getPointeeType();
21946 if (BaseType.isNull() || ExprTy.isNull())
21947 return nullptr;
21948 ExprTy = ExprTy.getNonReferenceType();
21949 const Expr *Length = OASE->getLength();
21950 Expr::EvalResult Result;
21951 if (Length && !Length->isValueDependent() &&
21952 Length->EvaluateAsInt(Result, Ctx: getASTContext()) &&
21953 Result.Val.getInt().isZero()) {
21954 Diag(Loc: ELoc,
21955 DiagID: diag::err_omp_depend_zero_length_array_section_not_allowed)
21956 << SimpleExpr->getSourceRange();
21957 continue;
21958 }
21959 }
21960
21961 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
21962 // List items used in depend clauses with the in, out, inout,
21963 // inoutset, or mutexinoutset dependence types cannot be
21964 // expressions of the omp_depend_t type.
21965 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
21966 !RefExpr->isInstantiationDependent() &&
21967 !RefExpr->containsUnexpandedParameterPack() &&
21968 (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
21969 (OMPDependTFound && DSAStack->getOMPDependT().getTypePtr() ==
21970 ExprTy.getTypePtr()))) {
21971 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21972 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21973 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21974 << RefExpr->getSourceRange();
21975 continue;
21976 }
21977
21978 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: SimpleExpr);
21979 if (ASE && !ASE->getBase()->isTypeDependent() &&
21980 !ASE->getBase()
21981 ->getType()
21982 .getNonReferenceType()
21983 ->isPointerType() &&
21984 !ASE->getBase()->getType().getNonReferenceType()->isArrayType()) {
21985 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21986 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21987 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21988 << RefExpr->getSourceRange();
21989 continue;
21990 }
21991
21992 ExprResult Res;
21993 {
21994 Sema::TentativeAnalysisScope Trap(SemaRef);
21995 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf,
21996 InputExpr: RefExpr->IgnoreParenImpCasts());
21997 }
21998 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
21999 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
22000 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
22001 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
22002 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
22003 << RefExpr->getSourceRange();
22004 continue;
22005 }
22006 }
22007 }
22008 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
22009 }
22010 }
22011
22012 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
22013 DepKind != OMPC_DEPEND_outallmemory &&
22014 DepKind != OMPC_DEPEND_inoutallmemory && Vars.empty())
22015 return nullptr;
22016
22017 auto *C = OMPDependClause::Create(
22018 C: getASTContext(), StartLoc, LParenLoc, EndLoc,
22019 Data: {.DepKind: DepKind, .DepLoc: DepLoc, .ColonLoc: Data.ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc}, DepModifier, VL: Vars,
22020 NumLoops: TotalDepCount.getZExtValue());
22021 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
22022 DSAStack->isParentOrderedRegion())
22023 DSAStack->addDoacrossDependClause(C, OpsOffs);
22024 return C;
22025}
22026
22027OMPClause *SemaOpenMP::ActOnOpenMPDeviceClause(
22028 OpenMPDeviceClauseModifier Modifier, Expr *Device, SourceLocation StartLoc,
22029 SourceLocation LParenLoc, SourceLocation ModifierLoc,
22030 SourceLocation EndLoc) {
22031 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 50) &&
22032 "Unexpected device modifier in OpenMP < 50.");
22033
22034 bool ErrorFound = false;
22035 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) {
22036 std::string Values =
22037 getListOfPossibleValues(K: OMPC_device, /*First=*/0, Last: OMPC_DEVICE_unknown);
22038 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
22039 << Values << getOpenMPClauseNameForDiag(C: OMPC_device);
22040 ErrorFound = true;
22041 }
22042
22043 Expr *ValExpr = Device;
22044 Stmt *HelperValStmt = nullptr;
22045
22046 // OpenMP [2.9.1, Restrictions]
22047 // The device expression must evaluate to a non-negative integer value.
22048 ErrorFound = !isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_device,
22049 /*StrictlyPositive=*/false) ||
22050 ErrorFound;
22051 if (ErrorFound)
22052 return nullptr;
22053
22054 // OpenMP 5.0 [2.12.5, Restrictions]
22055 // In case of ancestor device-modifier, a requires directive with
22056 // the reverse_offload clause must be specified.
22057 if (Modifier == OMPC_DEVICE_ancestor) {
22058 if (!DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>()) {
22059 SemaRef.targetDiag(
22060 Loc: StartLoc,
22061 DiagID: diag::err_omp_device_ancestor_without_requires_reverse_offload);
22062 ErrorFound = true;
22063 }
22064 }
22065
22066 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
22067 OpenMPDirectiveKind CaptureRegion =
22068 getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_device, OpenMPVersion: getLangOpts().OpenMP);
22069 if (CaptureRegion != OMPD_unknown &&
22070 !SemaRef.CurContext->isDependentContext()) {
22071 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
22072 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
22073 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
22074 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
22075 }
22076
22077 return new (getASTContext())
22078 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
22079 LParenLoc, ModifierLoc, EndLoc);
22080}
22081
22082static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
22083 DSAStackTy *Stack, QualType QTy,
22084 bool FullCheck = true) {
22085 if (SemaRef.RequireCompleteType(Loc: SL, T: QTy, DiagID: diag::err_incomplete_type))
22086 return false;
22087 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
22088 !QTy.isTriviallyCopyableType(Context: SemaRef.Context))
22089 SemaRef.Diag(Loc: SL, DiagID: diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
22090 return true;
22091}
22092
22093/// Return true if it can be proven that the provided array expression
22094/// (array section or array subscript) does NOT specify the whole size of the
22095/// array whose base type is \a BaseQTy.
22096static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
22097 const Expr *E,
22098 QualType BaseQTy) {
22099 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
22100
22101 // If this is an array subscript, it refers to the whole size if the size of
22102 // the dimension is constant and equals 1. Also, an array section assumes the
22103 // format of an array subscript if no colon is used.
22104 if (isa<ArraySubscriptExpr>(Val: E) ||
22105 (OASE && OASE->getColonLocFirst().isInvalid())) {
22106 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
22107 return ATy->getSExtSize() != 1;
22108 // Size can't be evaluated statically.
22109 return false;
22110 }
22111
22112 assert(OASE && "Expecting array section if not an array subscript.");
22113 const Expr *LowerBound = OASE->getLowerBound();
22114 const Expr *Length = OASE->getLength();
22115
22116 // If there is a lower bound that does not evaluates to zero, we are not
22117 // covering the whole dimension.
22118 if (LowerBound) {
22119 Expr::EvalResult Result;
22120 if (!LowerBound->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22121 return false; // Can't get the integer value as a constant.
22122
22123 llvm::APSInt ConstLowerBound = Result.Val.getInt();
22124 if (ConstLowerBound.getSExtValue())
22125 return true;
22126 }
22127
22128 // If we don't have a length we covering the whole dimension.
22129 if (!Length)
22130 return false;
22131
22132 // If the base is a pointer, we don't have a way to get the size of the
22133 // pointee.
22134 if (BaseQTy->isPointerType())
22135 return false;
22136
22137 // We can only check if the length is the same as the size of the dimension
22138 // if we have a constant array.
22139 const auto *CATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr());
22140 if (!CATy)
22141 return false;
22142
22143 Expr::EvalResult Result;
22144 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22145 return false; // Can't get the integer value as a constant.
22146
22147 llvm::APSInt ConstLength = Result.Val.getInt();
22148 return CATy->getSExtSize() != ConstLength.getSExtValue();
22149}
22150
22151// Return true if it can be proven that the provided array expression (array
22152// section or array subscript) does NOT specify a single element of the array
22153// whose base type is \a BaseQTy.
22154static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
22155 const Expr *E,
22156 QualType BaseQTy) {
22157 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
22158
22159 // An array subscript always refer to a single element. Also, an array section
22160 // assumes the format of an array subscript if no colon is used.
22161 if (isa<ArraySubscriptExpr>(Val: E) ||
22162 (OASE && OASE->getColonLocFirst().isInvalid()))
22163 return false;
22164
22165 assert(OASE && "Expecting array section if not an array subscript.");
22166 const Expr *Length = OASE->getLength();
22167
22168 // If we don't have a length we have to check if the array has unitary size
22169 // for this dimension. Also, we should always expect a length if the base type
22170 // is pointer.
22171 if (!Length) {
22172 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
22173 return ATy->getSExtSize() != 1;
22174 // We cannot assume anything.
22175 return false;
22176 }
22177
22178 // Check if the length evaluates to 1.
22179 Expr::EvalResult Result;
22180 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22181 return false; // Can't get the integer value as a constant.
22182
22183 llvm::APSInt ConstLength = Result.Val.getInt();
22184 return ConstLength.getSExtValue() != 1;
22185}
22186
22187// The base of elements of list in a map clause have to be either:
22188// - a reference to variable or field.
22189// - a member expression.
22190// - an array expression.
22191//
22192// E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
22193// reference to 'r'.
22194//
22195// If we have:
22196//
22197// struct SS {
22198// Bla S;
22199// foo() {
22200// #pragma omp target map (S.Arr[:12]);
22201// }
22202// }
22203//
22204// We want to retrieve the member expression 'this->S';
22205
22206// OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2]
22207// If a list item is an array section, it must specify contiguous storage.
22208//
22209// For this restriction it is sufficient that we make sure only references
22210// to variables or fields and array expressions, and that no array sections
22211// exist except in the rightmost expression (unless they cover the whole
22212// dimension of the array). E.g. these would be invalid:
22213//
22214// r.ArrS[3:5].Arr[6:7]
22215//
22216// r.ArrS[3:5].x
22217//
22218// but these would be valid:
22219// r.ArrS[3].Arr[6:7]
22220//
22221// r.ArrS[3].x
22222namespace {
22223class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> {
22224 Sema &SemaRef;
22225 OpenMPClauseKind CKind = OMPC_unknown;
22226 OpenMPDirectiveKind DKind = OMPD_unknown;
22227 OMPClauseMappableExprCommon::MappableExprComponentList &Components;
22228 bool IsNonContiguous = false;
22229 bool NoDiagnose = false;
22230 const Expr *RelevantExpr = nullptr;
22231 bool AllowUnitySizeArraySection = true;
22232 bool AllowWholeSizeArraySection = true;
22233 bool AllowAnotherPtr = true;
22234 SourceLocation ELoc;
22235 SourceRange ERange;
22236
22237 void emitErrorMsg() {
22238 // If nothing else worked, this is not a valid map clause expression.
22239 if (SemaRef.getLangOpts().OpenMP < 50) {
22240 SemaRef.Diag(Loc: ELoc,
22241 DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
22242 << ERange;
22243 } else {
22244 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
22245 << getOpenMPClauseNameForDiag(C: CKind) << ERange;
22246 }
22247 }
22248
22249public:
22250 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
22251 if (!isa<VarDecl>(Val: DRE->getDecl())) {
22252 emitErrorMsg();
22253 return false;
22254 }
22255 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22256 RelevantExpr = DRE;
22257 // Record the component.
22258 Components.emplace_back(Args&: DRE, Args: DRE->getDecl(), Args&: IsNonContiguous);
22259 return true;
22260 }
22261
22262 bool VisitMemberExpr(MemberExpr *ME) {
22263 Expr *E = ME;
22264 Expr *BaseE = ME->getBase()->IgnoreParenCasts();
22265
22266 if (isa<CXXThisExpr>(Val: BaseE)) {
22267 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22268 // We found a base expression: this->Val.
22269 RelevantExpr = ME;
22270 } else {
22271 E = BaseE;
22272 }
22273
22274 if (!isa<FieldDecl>(Val: ME->getMemberDecl())) {
22275 if (!NoDiagnose) {
22276 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_access_to_data_field)
22277 << ME->getSourceRange();
22278 return false;
22279 }
22280 if (RelevantExpr)
22281 return false;
22282 return Visit(S: E);
22283 }
22284
22285 auto *FD = cast<FieldDecl>(Val: ME->getMemberDecl());
22286
22287 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
22288 // A bit-field cannot appear in a map clause.
22289 //
22290 if (FD->isBitField()) {
22291 if (!NoDiagnose) {
22292 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_bit_fields_forbidden_in_clause)
22293 << ME->getSourceRange() << getOpenMPClauseNameForDiag(C: CKind);
22294 return false;
22295 }
22296 if (RelevantExpr)
22297 return false;
22298 return Visit(S: E);
22299 }
22300
22301 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22302 // If the type of a list item is a reference to a type T then the type
22303 // will be considered to be T for all purposes of this clause.
22304 QualType CurType = BaseE->getType().getNonReferenceType();
22305
22306 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
22307 // A list item cannot be a variable that is a member of a structure with
22308 // a union type.
22309 //
22310 if (CurType->isUnionType()) {
22311 if (!NoDiagnose) {
22312 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_union_type_not_allowed)
22313 << ME->getSourceRange();
22314 return false;
22315 }
22316 return RelevantExpr || Visit(S: E);
22317 }
22318
22319 // If we got a member expression, we should not expect any array section
22320 // before that:
22321 //
22322 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
22323 // If a list item is an element of a structure, only the rightmost symbol
22324 // of the variable reference can be an array section.
22325 //
22326 AllowUnitySizeArraySection = false;
22327 AllowWholeSizeArraySection = false;
22328
22329 // Record the component.
22330 Components.emplace_back(Args&: ME, Args&: FD, Args&: IsNonContiguous);
22331 return RelevantExpr || Visit(S: E);
22332 }
22333
22334 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) {
22335 Expr *E = AE->getBase()->IgnoreParenImpCasts();
22336
22337 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
22338 if (!NoDiagnose) {
22339 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
22340 << 0 << AE->getSourceRange();
22341 return false;
22342 }
22343 return RelevantExpr || Visit(S: E);
22344 }
22345
22346 // If we got an array subscript that express the whole dimension we
22347 // can have any array expressions before. If it only expressing part of
22348 // the dimension, we can only have unitary-size array expressions.
22349 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: AE, BaseQTy: E->getType()))
22350 AllowWholeSizeArraySection = false;
22351
22352 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E->IgnoreParenCasts())) {
22353 Expr::EvalResult Result;
22354 if (!AE->getIdx()->isValueDependent() &&
22355 AE->getIdx()->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()) &&
22356 !Result.Val.getInt().isZero()) {
22357 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
22358 DiagID: diag::err_omp_invalid_map_this_expr);
22359 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
22360 DiagID: diag::note_omp_invalid_subscript_on_this_ptr_map);
22361 }
22362 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22363 RelevantExpr = TE;
22364 }
22365
22366 // Record the component - we don't have any declaration associated.
22367 Components.emplace_back(Args&: AE, Args: nullptr, Args&: IsNonContiguous);
22368
22369 return RelevantExpr || Visit(S: E);
22370 }
22371
22372 bool VisitArraySectionExpr(ArraySectionExpr *OASE) {
22373 // After OMP 5.0 Array section in reduction clause will be implicitly
22374 // mapped
22375 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) &&
22376 "Array sections cannot be implicitly mapped.");
22377 Expr *E = OASE->getBase()->IgnoreParenImpCasts();
22378 QualType CurType =
22379 ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
22380
22381 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22382 // If the type of a list item is a reference to a type T then the type
22383 // will be considered to be T for all purposes of this clause.
22384 if (CurType->isReferenceType())
22385 CurType = CurType->getPointeeType();
22386
22387 bool IsPointer = CurType->isAnyPointerType();
22388
22389 if (!IsPointer && !CurType->isArrayType()) {
22390 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
22391 << 0 << OASE->getSourceRange();
22392 return false;
22393 }
22394
22395 bool NotWhole =
22396 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: OASE, BaseQTy: CurType);
22397 bool NotUnity =
22398 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, E: OASE, BaseQTy: CurType);
22399
22400 if (AllowWholeSizeArraySection) {
22401 // Any array section is currently allowed. Allowing a whole size array
22402 // section implies allowing a unity array section as well.
22403 //
22404 // If this array section refers to the whole dimension we can still
22405 // accept other array sections before this one, except if the base is a
22406 // pointer. Otherwise, only unitary sections are accepted.
22407 if (NotWhole || IsPointer)
22408 AllowWholeSizeArraySection = false;
22409 } else if (DKind == OMPD_target_update &&
22410 SemaRef.getLangOpts().OpenMP >= 50) {
22411 if (IsPointer && !AllowAnotherPtr)
22412 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_section_length_undefined)
22413 << /*array of unknown bound */ 1;
22414 else
22415 IsNonContiguous = true;
22416 } else if (AllowUnitySizeArraySection && NotUnity) {
22417 // A unity or whole array section is not allowed and that is not
22418 // compatible with the properties of the current array section.
22419 if (NoDiagnose)
22420 return false;
22421 SemaRef.Diag(Loc: ELoc,
22422 DiagID: diag::err_array_section_does_not_specify_contiguous_storage)
22423 << OASE->getSourceRange();
22424 return false;
22425 }
22426
22427 if (IsPointer)
22428 AllowAnotherPtr = false;
22429
22430 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E)) {
22431 Expr::EvalResult ResultR;
22432 Expr::EvalResult ResultL;
22433 if (!OASE->getLength()->isValueDependent() &&
22434 OASE->getLength()->EvaluateAsInt(Result&: ResultR, Ctx: SemaRef.getASTContext()) &&
22435 !ResultR.Val.getInt().isOne()) {
22436 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
22437 DiagID: diag::err_omp_invalid_map_this_expr);
22438 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
22439 DiagID: diag::note_omp_invalid_length_on_this_ptr_mapping);
22440 }
22441 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() &&
22442 OASE->getLowerBound()->EvaluateAsInt(Result&: ResultL,
22443 Ctx: SemaRef.getASTContext()) &&
22444 !ResultL.Val.getInt().isZero()) {
22445 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
22446 DiagID: diag::err_omp_invalid_map_this_expr);
22447 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
22448 DiagID: diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
22449 }
22450 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22451 RelevantExpr = TE;
22452 }
22453
22454 // Record the component - we don't have any declaration associated.
22455 Components.emplace_back(Args&: OASE, Args: nullptr, /*IsNonContiguous=*/Args: false);
22456 return RelevantExpr || Visit(S: E);
22457 }
22458 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
22459 Expr *Base = E->getBase();
22460
22461 // Record the component - we don't have any declaration associated.
22462 Components.emplace_back(Args&: E, Args: nullptr, Args&: IsNonContiguous);
22463
22464 return Visit(S: Base->IgnoreParenImpCasts());
22465 }
22466
22467 bool VisitUnaryOperator(UnaryOperator *UO) {
22468 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() ||
22469 UO->getOpcode() != UO_Deref) {
22470 emitErrorMsg();
22471 return false;
22472 }
22473 if (!RelevantExpr) {
22474 // Record the component if haven't found base decl.
22475 Components.emplace_back(Args&: UO, Args: nullptr, /*IsNonContiguous=*/Args: false);
22476 }
22477 return RelevantExpr || Visit(S: UO->getSubExpr()->IgnoreParenImpCasts());
22478 }
22479 bool VisitBinaryOperator(BinaryOperator *BO) {
22480 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) {
22481 emitErrorMsg();
22482 return false;
22483 }
22484
22485 // Pointer arithmetic is the only thing we expect to happen here so after we
22486 // make sure the binary operator is a pointer type, the only thing we need
22487 // to do is to visit the subtree that has the same type as root (so that we
22488 // know the other subtree is just an offset)
22489 Expr *LE = BO->getLHS()->IgnoreParenImpCasts();
22490 Expr *RE = BO->getRHS()->IgnoreParenImpCasts();
22491 Components.emplace_back(Args&: BO, Args: nullptr, Args: false);
22492 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() ||
22493 RE->getType().getTypePtr() == BO->getType().getTypePtr()) &&
22494 "Either LHS or RHS have base decl inside");
22495 if (BO->getType().getTypePtr() == LE->getType().getTypePtr())
22496 return RelevantExpr || Visit(S: LE);
22497 return RelevantExpr || Visit(S: RE);
22498 }
22499 bool VisitCXXThisExpr(CXXThisExpr *CTE) {
22500 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22501 RelevantExpr = CTE;
22502 Components.emplace_back(Args&: CTE, Args: nullptr, Args&: IsNonContiguous);
22503 return true;
22504 }
22505 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) {
22506 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22507 Components.emplace_back(Args&: COCE, Args: nullptr, Args&: IsNonContiguous);
22508 return true;
22509 }
22510 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) {
22511 Expr *Source = E->getSourceExpr();
22512 if (!Source) {
22513 emitErrorMsg();
22514 return false;
22515 }
22516 return Visit(S: Source);
22517 }
22518 bool VisitStmt(Stmt *) {
22519 emitErrorMsg();
22520 return false;
22521 }
22522 const Expr *getFoundBase() const { return RelevantExpr; }
22523 explicit MapBaseChecker(
22524 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind,
22525 OMPClauseMappableExprCommon::MappableExprComponentList &Components,
22526 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange)
22527 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components),
22528 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {}
22529};
22530} // namespace
22531
22532/// Return the expression of the base of the mappable expression or null if it
22533/// cannot be determined and do all the necessary checks to see if the
22534/// expression is valid as a standalone mappable expression. In the process,
22535/// record all the components of the expression.
22536static const Expr *checkMapClauseExpressionBase(
22537 Sema &SemaRef, Expr *E,
22538 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
22539 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) {
22540 SourceLocation ELoc = E->getExprLoc();
22541 SourceRange ERange = E->getSourceRange();
22542 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc,
22543 ERange);
22544 if (Checker.Visit(S: E->IgnoreParens())) {
22545 // Check if the highest dimension array section has length specified
22546 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() &&
22547 (CKind == OMPC_to || CKind == OMPC_from)) {
22548 auto CI = CurComponents.rbegin();
22549 auto CE = CurComponents.rend();
22550 for (; CI != CE; ++CI) {
22551 const auto *OASE =
22552 dyn_cast<ArraySectionExpr>(Val: CI->getAssociatedExpression());
22553 if (!OASE)
22554 continue;
22555 if (OASE && OASE->getLength())
22556 break;
22557 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_array_section_does_not_specify_length)
22558 << ERange;
22559 }
22560 }
22561 return Checker.getFoundBase();
22562 }
22563 return nullptr;
22564}
22565
22566// Return true if expression E associated with value VD has conflicts with other
22567// map information.
22568static bool checkMapConflicts(
22569 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
22570 bool CurrentRegionOnly,
22571 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
22572 OpenMPClauseKind CKind) {
22573 assert(VD && E);
22574 SourceLocation ELoc = E->getExprLoc();
22575 SourceRange ERange = E->getSourceRange();
22576
22577 // In order to easily check the conflicts we need to match each component of
22578 // the expression under test with the components of the expressions that are
22579 // already in the stack.
22580
22581 assert(!CurComponents.empty() && "Map clause expression with no components!");
22582 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
22583 "Map clause expression with unexpected base!");
22584
22585 // Variables to help detecting enclosing problems in data environment nests.
22586 bool IsEnclosedByDataEnvironmentExpr = false;
22587 const Expr *EnclosingExpr = nullptr;
22588
22589 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
22590 VD, CurrentRegionOnly,
22591 Check: [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
22592 ERange, CKind, &EnclosingExpr,
22593 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
22594 StackComponents,
22595 OpenMPClauseKind Kind) {
22596 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50)
22597 return false;
22598 assert(!StackComponents.empty() &&
22599 "Map clause expression with no components!");
22600 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
22601 "Map clause expression with unexpected base!");
22602 (void)VD;
22603
22604 // The whole expression in the stack.
22605 const Expr *RE = StackComponents.front().getAssociatedExpression();
22606
22607 // Expressions must start from the same base. Here we detect at which
22608 // point both expressions diverge from each other and see if we can
22609 // detect if the memory referred to both expressions is contiguous and
22610 // do not overlap.
22611 auto CI = CurComponents.rbegin();
22612 auto CE = CurComponents.rend();
22613 auto SI = StackComponents.rbegin();
22614 auto SE = StackComponents.rend();
22615 for (; CI != CE && SI != SE; ++CI, ++SI) {
22616
22617 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
22618 // At most one list item can be an array item derived from a given
22619 // variable in map clauses of the same construct.
22620 if (CurrentRegionOnly &&
22621 (isa<ArraySubscriptExpr>(Val: CI->getAssociatedExpression()) ||
22622 isa<ArraySectionExpr>(Val: CI->getAssociatedExpression()) ||
22623 isa<OMPArrayShapingExpr>(Val: CI->getAssociatedExpression())) &&
22624 (isa<ArraySubscriptExpr>(Val: SI->getAssociatedExpression()) ||
22625 isa<ArraySectionExpr>(Val: SI->getAssociatedExpression()) ||
22626 isa<OMPArrayShapingExpr>(Val: SI->getAssociatedExpression()))) {
22627 SemaRef.Diag(Loc: CI->getAssociatedExpression()->getExprLoc(),
22628 DiagID: diag::err_omp_multiple_array_items_in_map_clause)
22629 << CI->getAssociatedExpression()->getSourceRange();
22630 SemaRef.Diag(Loc: SI->getAssociatedExpression()->getExprLoc(),
22631 DiagID: diag::note_used_here)
22632 << SI->getAssociatedExpression()->getSourceRange();
22633 return true;
22634 }
22635
22636 // Do both expressions have the same kind?
22637 if (CI->getAssociatedExpression()->getStmtClass() !=
22638 SI->getAssociatedExpression()->getStmtClass())
22639 break;
22640
22641 // Are we dealing with different variables/fields?
22642 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
22643 break;
22644 }
22645 // Check if the extra components of the expressions in the enclosing
22646 // data environment are redundant for the current base declaration.
22647 // If they are, the maps completely overlap, which is legal.
22648 for (; SI != SE; ++SI) {
22649 QualType Type;
22650 if (const auto *ASE =
22651 dyn_cast<ArraySubscriptExpr>(Val: SI->getAssociatedExpression())) {
22652 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
22653 } else if (const auto *OASE = dyn_cast<ArraySectionExpr>(
22654 Val: SI->getAssociatedExpression())) {
22655 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
22656 Type = ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
22657 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
22658 Val: SI->getAssociatedExpression())) {
22659 Type = OASE->getBase()->getType()->getPointeeType();
22660 }
22661 if (Type.isNull() || Type->isAnyPointerType() ||
22662 checkArrayExpressionDoesNotReferToWholeSize(
22663 SemaRef, E: SI->getAssociatedExpression(), BaseQTy: Type))
22664 break;
22665 }
22666
22667 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
22668 // List items of map clauses in the same construct must not share
22669 // original storage.
22670 //
22671 // If the expressions are exactly the same or one is a subset of the
22672 // other, it means they are sharing storage.
22673 if (CI == CE && SI == SE) {
22674 if (CurrentRegionOnly) {
22675 if (CKind == OMPC_map) {
22676 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
22677 } else {
22678 assert(CKind == OMPC_to || CKind == OMPC_from);
22679 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
22680 << ERange;
22681 }
22682 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22683 << RE->getSourceRange();
22684 return true;
22685 }
22686 // If we find the same expression in the enclosing data environment,
22687 // that is legal.
22688 IsEnclosedByDataEnvironmentExpr = true;
22689 return false;
22690 }
22691
22692 QualType DerivedType =
22693 std::prev(x: CI)->getAssociatedDeclaration()->getType();
22694 SourceLocation DerivedLoc =
22695 std::prev(x: CI)->getAssociatedExpression()->getExprLoc();
22696
22697 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22698 // If the type of a list item is a reference to a type T then the type
22699 // will be considered to be T for all purposes of this clause.
22700 DerivedType = DerivedType.getNonReferenceType();
22701
22702 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
22703 // A variable for which the type is pointer and an array section
22704 // derived from that variable must not appear as list items of map
22705 // clauses of the same construct.
22706 //
22707 // Also, cover one of the cases in:
22708 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
22709 // If any part of the original storage of a list item has corresponding
22710 // storage in the device data environment, all of the original storage
22711 // must have corresponding storage in the device data environment.
22712 //
22713 if (DerivedType->isAnyPointerType()) {
22714 if (CI == CE || SI == SE) {
22715 SemaRef.Diag(
22716 Loc: DerivedLoc,
22717 DiagID: diag::err_omp_pointer_mapped_along_with_derived_section)
22718 << DerivedLoc;
22719 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22720 << RE->getSourceRange();
22721 return true;
22722 }
22723 if (CI->getAssociatedExpression()->getStmtClass() !=
22724 SI->getAssociatedExpression()->getStmtClass() ||
22725 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
22726 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
22727 assert(CI != CE && SI != SE);
22728 SemaRef.Diag(Loc: DerivedLoc, DiagID: diag::err_omp_same_pointer_dereferenced)
22729 << DerivedLoc;
22730 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22731 << RE->getSourceRange();
22732 return true;
22733 }
22734 }
22735
22736 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
22737 // List items of map clauses in the same construct must not share
22738 // original storage.
22739 //
22740 // An expression is a subset of the other.
22741 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
22742 if (CKind == OMPC_map) {
22743 if (CI != CE || SI != SE) {
22744 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
22745 // a pointer.
22746 auto Begin =
22747 CI != CE ? CurComponents.begin() : StackComponents.begin();
22748 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
22749 auto It = Begin;
22750 while (It != End && !It->getAssociatedDeclaration())
22751 std::advance(i&: It, n: 1);
22752 assert(It != End &&
22753 "Expected at least one component with the declaration.");
22754 if (It != Begin && It->getAssociatedDeclaration()
22755 ->getType()
22756 .getCanonicalType()
22757 ->isAnyPointerType()) {
22758 IsEnclosedByDataEnvironmentExpr = false;
22759 EnclosingExpr = nullptr;
22760 return false;
22761 }
22762 }
22763 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
22764 } else {
22765 assert(CKind == OMPC_to || CKind == OMPC_from);
22766 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
22767 << ERange;
22768 }
22769 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22770 << RE->getSourceRange();
22771 return true;
22772 }
22773
22774 // The current expression uses the same base as other expression in the
22775 // data environment but does not contain it completely.
22776 if (!CurrentRegionOnly && SI != SE)
22777 EnclosingExpr = RE;
22778
22779 // The current expression is a subset of the expression in the data
22780 // environment.
22781 IsEnclosedByDataEnvironmentExpr |=
22782 (!CurrentRegionOnly && CI != CE && SI == SE);
22783
22784 return false;
22785 });
22786
22787 if (CurrentRegionOnly)
22788 return FoundError;
22789
22790 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
22791 // If any part of the original storage of a list item has corresponding
22792 // storage in the device data environment, all of the original storage must
22793 // have corresponding storage in the device data environment.
22794 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
22795 // If a list item is an element of a structure, and a different element of
22796 // the structure has a corresponding list item in the device data environment
22797 // prior to a task encountering the construct associated with the map clause,
22798 // then the list item must also have a corresponding list item in the device
22799 // data environment prior to the task encountering the construct.
22800 //
22801 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
22802 SemaRef.Diag(Loc: ELoc,
22803 DiagID: diag::err_omp_original_storage_is_shared_and_does_not_contain)
22804 << ERange;
22805 SemaRef.Diag(Loc: EnclosingExpr->getExprLoc(), DiagID: diag::note_used_here)
22806 << EnclosingExpr->getSourceRange();
22807 return true;
22808 }
22809
22810 return FoundError;
22811}
22812
22813// Look up the user-defined mapper given the mapper name and mapped type, and
22814// build a reference to it.
22815static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
22816 CXXScopeSpec &MapperIdScopeSpec,
22817 const DeclarationNameInfo &MapperId,
22818 QualType Type,
22819 Expr *UnresolvedMapper) {
22820 if (MapperIdScopeSpec.isInvalid())
22821 return ExprError();
22822 // Get the actual type for the array type.
22823 if (Type->isArrayType()) {
22824 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
22825 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
22826 }
22827 // Find all user-defined mappers with the given MapperId.
22828 SmallVector<UnresolvedSet<8>, 4> Lookups;
22829 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
22830 Lookup.suppressDiagnostics();
22831 if (S) {
22832 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
22833 /*ObjectType=*/QualType())) {
22834 NamedDecl *D = Lookup.getRepresentativeDecl();
22835 while (S && !S->isDeclScope(D))
22836 S = S->getParent();
22837 if (S)
22838 S = S->getParent();
22839 Lookups.emplace_back();
22840 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
22841 Lookup.clear();
22842 }
22843 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedMapper)) {
22844 // Extract the user-defined mappers with the given MapperId.
22845 Lookups.push_back(Elt: UnresolvedSet<8>());
22846 for (NamedDecl *D : ULE->decls()) {
22847 auto *DMD = cast<OMPDeclareMapperDecl>(Val: D);
22848 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
22849 Lookups.back().addDecl(D: DMD);
22850 }
22851 }
22852 // Defer the lookup for dependent types. The results will be passed through
22853 // UnresolvedMapper on instantiation.
22854 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
22855 Type->isInstantiationDependentType() ||
22856 Type->containsUnexpandedParameterPack() ||
22857 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
22858 return !D->isInvalidDecl() &&
22859 (D->getType()->isDependentType() ||
22860 D->getType()->isInstantiationDependentType() ||
22861 D->getType()->containsUnexpandedParameterPack());
22862 })) {
22863 UnresolvedSet<8> URS;
22864 for (const UnresolvedSet<8> &Set : Lookups) {
22865 if (Set.empty())
22866 continue;
22867 URS.append(I: Set.begin(), E: Set.end());
22868 }
22869 return UnresolvedLookupExpr::Create(
22870 Context: SemaRef.Context, /*NamingClass=*/nullptr,
22871 QualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: MapperId,
22872 /*ADL=*/RequiresADL: false, Begin: URS.begin(), End: URS.end(), /*KnownDependent=*/false,
22873 /*KnownInstantiationDependent=*/false);
22874 }
22875 SourceLocation Loc = MapperId.getLoc();
22876 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
22877 // The type must be of struct, union or class type in C and C++
22878 if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
22879 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
22880 SemaRef.Diag(Loc, DiagID: diag::err_omp_mapper_wrong_type);
22881 return ExprError();
22882 }
22883 // Perform argument dependent lookup.
22884 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
22885 argumentDependentLookup(SemaRef, Id: MapperId, Loc, Ty: Type, Lookups);
22886 // Return the first user-defined mapper with the desired type.
22887 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22888 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
22889 if (!D->isInvalidDecl() &&
22890 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
22891 return D;
22892 return nullptr;
22893 }))
22894 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
22895 // Find the first user-defined mapper with a type derived from the desired
22896 // type.
22897 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22898 Lookups, Gen: [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
22899 if (!D->isInvalidDecl() &&
22900 SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: D->getType()) &&
22901 !Type.isMoreQualifiedThan(other: D->getType(),
22902 Ctx: SemaRef.getASTContext()))
22903 return D;
22904 return nullptr;
22905 })) {
22906 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
22907 /*DetectVirtual=*/false);
22908 if (SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: VD->getType(), Paths)) {
22909 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
22910 T: VD->getType().getUnqualifiedType()))) {
22911 if (SemaRef.CheckBaseClassAccess(
22912 AccessLoc: Loc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
22913 /*DiagID=*/0) != Sema::AR_inaccessible) {
22914 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
22915 }
22916 }
22917 }
22918 }
22919 // Report error if a mapper is specified, but cannot be found.
22920 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
22921 SemaRef.Diag(Loc, DiagID: diag::err_omp_invalid_mapper)
22922 << Type << MapperId.getName();
22923 return ExprError();
22924 }
22925 return ExprEmpty();
22926}
22927
22928namespace {
22929// Utility struct that gathers all the related lists associated with a mappable
22930// expression.
22931struct MappableVarListInfo {
22932 // The list of expressions.
22933 ArrayRef<Expr *> VarList;
22934 // The list of processed expressions.
22935 SmallVector<Expr *, 16> ProcessedVarList;
22936 // The mappble components for each expression.
22937 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
22938 // The base declaration of the variable.
22939 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
22940 // The reference to the user-defined mapper associated with every expression.
22941 SmallVector<Expr *, 16> UDMapperList;
22942
22943 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
22944 // We have a list of components and base declarations for each entry in the
22945 // variable list.
22946 VarComponents.reserve(N: VarList.size());
22947 VarBaseDeclarations.reserve(N: VarList.size());
22948 }
22949};
22950} // namespace
22951
22952static DeclRefExpr *buildImplicitMap(Sema &S, QualType BaseType,
22953 DSAStackTy *Stack,
22954 SmallVectorImpl<OMPClause *> &Maps) {
22955
22956 const RecordDecl *RD = BaseType->getAsRecordDecl();
22957 SourceRange Range = RD->getSourceRange();
22958 DeclarationNameInfo ImplicitName;
22959 // Dummy variable _s for Mapper.
22960 VarDecl *VD = buildVarDecl(SemaRef&: S, Loc: Range.getEnd(), Type: BaseType, Name: "_s");
22961 DeclRefExpr *MapperVarRef =
22962 buildDeclRefExpr(S, D: VD, Ty: BaseType, Loc: SourceLocation());
22963
22964 // Create implicit map clause for mapper.
22965 SmallVector<Expr *, 4> SExprs;
22966 for (auto *FD : RD->fields()) {
22967 Expr *BE = S.BuildMemberExpr(
22968 Base: MapperVarRef, /*IsArrow=*/false, OpLoc: Range.getBegin(),
22969 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: Range.getBegin(), Member: FD,
22970 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
22971 /*HadMultipleCandidates=*/false,
22972 MemberNameInfo: DeclarationNameInfo(FD->getDeclName(), FD->getSourceRange().getBegin()),
22973 Ty: FD->getType(), VK: VK_LValue, OK: OK_Ordinary);
22974 SExprs.push_back(Elt: BE);
22975 }
22976 CXXScopeSpec MapperIdScopeSpec;
22977 DeclarationNameInfo MapperId;
22978 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
22979
22980 OMPClause *MapClause = S.OpenMP().ActOnOpenMPMapClause(
22981 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(), MapperIdScopeSpec,
22982 MapperId, MapType: DKind == OMPD_target_enter_data ? OMPC_MAP_to : OMPC_MAP_tofrom,
22983 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: SExprs,
22984 Locs: OMPVarListLocTy());
22985 Maps.push_back(Elt: MapClause);
22986 return MapperVarRef;
22987}
22988
22989static ExprResult buildImplicitMapper(Sema &S, QualType BaseType,
22990 DSAStackTy *Stack) {
22991
22992 // Build impilicit map for mapper
22993 SmallVector<OMPClause *, 4> Maps;
22994 DeclRefExpr *MapperVarRef = buildImplicitMap(S, BaseType, Stack, Maps);
22995
22996 const RecordDecl *RD = BaseType->getAsRecordDecl();
22997 // AST context is RD's ParentASTContext().
22998 ASTContext &Ctx = RD->getParentASTContext();
22999 // DeclContext is RD's DeclContext.
23000 DeclContext *DCT = const_cast<DeclContext *>(RD->getDeclContext());
23001
23002 // Create implicit default mapper for "RD".
23003 DeclarationName MapperId;
23004 auto &DeclNames = Ctx.DeclarationNames;
23005 MapperId = DeclNames.getIdentifier(ID: &Ctx.Idents.get(Name: "default"));
23006 auto *DMD = OMPDeclareMapperDecl::Create(C&: Ctx, DC: DCT, L: SourceLocation(), Name: MapperId,
23007 T: BaseType, VarName: MapperId, Clauses: Maps, PrevDeclInScope: nullptr);
23008 Scope *Scope = S.getScopeForContext(Ctx: DCT);
23009 if (Scope)
23010 S.PushOnScopeChains(D: DMD, S: Scope, /*AddToContext=*/false);
23011 DCT->addDecl(D: DMD);
23012 DMD->setAccess(clang::AS_none);
23013 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
23014 VD->setDeclContext(DMD);
23015 VD->setLexicalDeclContext(DMD);
23016 DMD->addDecl(D: VD);
23017 DMD->setMapperVarRef(MapperVarRef);
23018 FieldDecl *FD = *RD->field_begin();
23019 // create mapper refence.
23020 return DeclRefExpr::Create(Context: Ctx, QualifierLoc: NestedNameSpecifierLoc{}, TemplateKWLoc: FD->getLocation(),
23021 D: DMD, RefersToEnclosingVariableOrCapture: false, NameLoc: SourceLocation(), T: BaseType, VK: VK_LValue);
23022}
23023
23024// Look up the user-defined mapper given the mapper name and mapper type,
23025// return true if found one.
23026static bool hasUserDefinedMapper(Sema &SemaRef, Scope *S,
23027 CXXScopeSpec &MapperIdScopeSpec,
23028 const DeclarationNameInfo &MapperId,
23029 QualType Type) {
23030 // Find all user-defined mappers with the given MapperId.
23031 SmallVector<UnresolvedSet<8>, 4> Lookups;
23032 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
23033 Lookup.suppressDiagnostics();
23034 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
23035 /*ObjectType=*/QualType())) {
23036 NamedDecl *D = Lookup.getRepresentativeDecl();
23037 while (S && !S->isDeclScope(D))
23038 S = S->getParent();
23039 if (S)
23040 S = S->getParent();
23041 Lookups.emplace_back();
23042 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
23043 Lookup.clear();
23044 }
23045 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
23046 Type->isInstantiationDependentType() ||
23047 Type->containsUnexpandedParameterPack() ||
23048 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
23049 return !D->isInvalidDecl() &&
23050 (D->getType()->isDependentType() ||
23051 D->getType()->isInstantiationDependentType() ||
23052 D->getType()->containsUnexpandedParameterPack());
23053 }))
23054 return false;
23055 // Perform argument dependent lookup.
23056 SourceLocation Loc = MapperId.getLoc();
23057 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
23058 argumentDependentLookup(SemaRef, Id: MapperId, Loc, Ty: Type, Lookups);
23059 if (filterLookupForUDReductionAndMapper<ValueDecl *>(
23060 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
23061 if (!D->isInvalidDecl() &&
23062 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
23063 return D;
23064 return nullptr;
23065 }))
23066 return true;
23067 // Find the first user-defined mapper with a type derived from the desired
23068 // type.
23069 auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
23070 Lookups, Gen: [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
23071 if (!D->isInvalidDecl() &&
23072 SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: D->getType()) &&
23073 !Type.isMoreQualifiedThan(other: D->getType(), Ctx: SemaRef.getASTContext()))
23074 return D;
23075 return nullptr;
23076 });
23077 if (!VD)
23078 return false;
23079 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
23080 /*DetectVirtual=*/false);
23081 if (SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: VD->getType(), Paths)) {
23082 bool IsAmbiguous = !Paths.isAmbiguous(
23083 BaseType: SemaRef.Context.getCanonicalType(T: VD->getType().getUnqualifiedType()));
23084 if (IsAmbiguous)
23085 return false;
23086 if (SemaRef.CheckBaseClassAccess(AccessLoc: Loc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
23087 /*DiagID=*/0) != Sema::AR_inaccessible)
23088 return true;
23089 }
23090 return false;
23091}
23092
23093static bool isImplicitMapperNeeded(Sema &S, DSAStackTy *Stack,
23094 QualType CanonType, const Expr *E) {
23095
23096 // DFS over data members in structures/classes.
23097 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(1,
23098 {CanonType, nullptr});
23099 llvm::DenseMap<const Type *, bool> Visited;
23100 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(1, {nullptr, 1});
23101 while (!Types.empty()) {
23102 auto [BaseType, CurFD] = Types.pop_back_val();
23103 while (ParentChain.back().second == 0)
23104 ParentChain.pop_back();
23105 --ParentChain.back().second;
23106 if (BaseType.isNull())
23107 continue;
23108 // Only structs/classes are allowed to have mappers.
23109 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
23110 if (!RD)
23111 continue;
23112 auto It = Visited.find(Val: BaseType.getTypePtr());
23113 if (It == Visited.end()) {
23114 // Try to find the associated user-defined mapper.
23115 CXXScopeSpec MapperIdScopeSpec;
23116 DeclarationNameInfo DefaultMapperId;
23117 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
23118 ID: &S.Context.Idents.get(Name: "default")));
23119 DefaultMapperId.setLoc(E->getExprLoc());
23120 bool HasUDMapper =
23121 hasUserDefinedMapper(SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec,
23122 MapperId: DefaultMapperId, Type: BaseType);
23123 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args&: HasUDMapper).first;
23124 }
23125 // Found default mapper.
23126 if (It->second)
23127 return true;
23128 // Check for the "default" mapper for data members.
23129 bool FirstIter = true;
23130 for (FieldDecl *FD : RD->fields()) {
23131 if (!FD)
23132 continue;
23133 QualType FieldTy = FD->getType();
23134 if (FieldTy.isNull() ||
23135 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
23136 continue;
23137 if (FirstIter) {
23138 FirstIter = false;
23139 ParentChain.emplace_back(Args&: CurFD, Args: 1);
23140 } else {
23141 ++ParentChain.back().second;
23142 }
23143 Types.emplace_back(Args&: FieldTy, Args&: FD);
23144 }
23145 }
23146 return false;
23147}
23148
23149// Check the validity of the provided variable list for the provided clause kind
23150// \a CKind. In the check process the valid expressions, mappable expression
23151// components, variables, and user-defined mappers are extracted and used to
23152// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
23153// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
23154// and \a MapperId are expected to be valid if the clause kind is 'map'.
23155static void checkMappableExpressionList(
23156 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
23157 MappableVarListInfo &MVLI, SourceLocation StartLoc,
23158 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
23159 ArrayRef<Expr *> UnresolvedMappers,
23160 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
23161 ArrayRef<OpenMPMapModifierKind> Modifiers = {},
23162 bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
23163 // We only expect mappable expressions in 'to', 'from', 'map', and
23164 // 'use_device_addr' clauses.
23165 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from ||
23166 CKind == OMPC_use_device_addr) &&
23167 "Unexpected clause kind with mappable expressions!");
23168 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
23169
23170 // If the identifier of user-defined mapper is not specified, it is "default".
23171 // We do not change the actual name in this clause to distinguish whether a
23172 // mapper is specified explicitly, i.e., it is not explicitly specified when
23173 // MapperId.getName() is empty.
23174 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
23175 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
23176 MapperId.setName(DeclNames.getIdentifier(
23177 ID: &SemaRef.getASTContext().Idents.get(Name: "default")));
23178 MapperId.setLoc(StartLoc);
23179 }
23180
23181 // Iterators to find the current unresolved mapper expression.
23182 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
23183 bool UpdateUMIt = false;
23184 Expr *UnresolvedMapper = nullptr;
23185
23186 bool HasHoldModifier =
23187 llvm::is_contained(Range&: Modifiers, Element: OMPC_MAP_MODIFIER_ompx_hold);
23188
23189 // Keep track of the mappable components and base declarations in this clause.
23190 // Each entry in the list is going to have a list of components associated. We
23191 // record each set of the components so that we can build the clause later on.
23192 // In the end we should have the same amount of declarations and component
23193 // lists.
23194
23195 for (Expr *RE : MVLI.VarList) {
23196 assert(RE && "Null expr in omp to/from/map clause");
23197 SourceLocation ELoc = RE->getExprLoc();
23198
23199 // Find the current unresolved mapper expression.
23200 if (UpdateUMIt && UMIt != UMEnd) {
23201 UMIt++;
23202 assert(
23203 UMIt != UMEnd &&
23204 "Expect the size of UnresolvedMappers to match with that of VarList");
23205 }
23206 UpdateUMIt = true;
23207 if (UMIt != UMEnd)
23208 UnresolvedMapper = *UMIt;
23209
23210 const Expr *VE = RE->IgnoreParenLValueCasts();
23211
23212 if (VE->isValueDependent() || VE->isTypeDependent() ||
23213 VE->isInstantiationDependent() ||
23214 VE->containsUnexpandedParameterPack()) {
23215 // Try to find the associated user-defined mapper.
23216 ExprResult ER = buildUserDefinedMapperRef(
23217 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23218 Type: VE->getType().getCanonicalType(), UnresolvedMapper);
23219 if (ER.isInvalid())
23220 continue;
23221 MVLI.UDMapperList.push_back(Elt: ER.get());
23222 // We can only analyze this information once the missing information is
23223 // resolved.
23224 MVLI.ProcessedVarList.push_back(Elt: RE);
23225 continue;
23226 }
23227
23228 Expr *SimpleExpr = RE->IgnoreParenCasts();
23229
23230 if (!RE->isLValue()) {
23231 if (SemaRef.getLangOpts().OpenMP < 50) {
23232 SemaRef.Diag(
23233 Loc: ELoc, DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
23234 << RE->getSourceRange();
23235 } else {
23236 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
23237 << getOpenMPClauseNameForDiag(C: CKind) << RE->getSourceRange();
23238 }
23239 continue;
23240 }
23241
23242 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
23243 ValueDecl *CurDeclaration = nullptr;
23244
23245 // Obtain the array or member expression bases if required. Also, fill the
23246 // components array with all the components identified in the process.
23247 const Expr *BE =
23248 checkMapClauseExpressionBase(SemaRef, E: SimpleExpr, CurComponents, CKind,
23249 DKind: DSAS->getCurrentDirective(), NoDiagnose);
23250 if (!BE)
23251 continue;
23252
23253 assert(!CurComponents.empty() &&
23254 "Invalid mappable expression information.");
23255
23256 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: BE)) {
23257 // Add store "this" pointer to class in DSAStackTy for future checking
23258 DSAS->addMappedClassesQualTypes(QT: TE->getType());
23259 // Try to find the associated user-defined mapper.
23260 ExprResult ER = buildUserDefinedMapperRef(
23261 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23262 Type: VE->getType().getCanonicalType(), UnresolvedMapper);
23263 if (ER.isInvalid())
23264 continue;
23265 MVLI.UDMapperList.push_back(Elt: ER.get());
23266 // Skip restriction checking for variable or field declarations
23267 MVLI.ProcessedVarList.push_back(Elt: RE);
23268 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
23269 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
23270 in_end: CurComponents.end());
23271 MVLI.VarBaseDeclarations.push_back(Elt: nullptr);
23272 continue;
23273 }
23274
23275 // For the following checks, we rely on the base declaration which is
23276 // expected to be associated with the last component. The declaration is
23277 // expected to be a variable or a field (if 'this' is being mapped).
23278 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
23279 assert(CurDeclaration && "Null decl on map clause.");
23280 assert(
23281 CurDeclaration->isCanonicalDecl() &&
23282 "Expecting components to have associated only canonical declarations.");
23283
23284 auto *VD = dyn_cast<VarDecl>(Val: CurDeclaration);
23285 const auto *FD = dyn_cast<FieldDecl>(Val: CurDeclaration);
23286
23287 assert((VD || FD) && "Only variables or fields are expected here!");
23288 (void)FD;
23289
23290 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
23291 // threadprivate variables cannot appear in a map clause.
23292 // OpenMP 4.5 [2.10.5, target update Construct]
23293 // threadprivate variables cannot appear in a from clause.
23294 if (VD && DSAS->isThreadPrivate(D: VD)) {
23295 if (NoDiagnose)
23296 continue;
23297 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
23298 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_threadprivate_in_clause)
23299 << getOpenMPClauseNameForDiag(C: CKind);
23300 reportOriginalDsa(SemaRef, Stack: DSAS, D: VD, DVar);
23301 continue;
23302 }
23303
23304 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
23305 // A list item cannot appear in both a map clause and a data-sharing
23306 // attribute clause on the same construct.
23307
23308 // Check conflicts with other map clause expressions. We check the conflicts
23309 // with the current construct separately from the enclosing data
23310 // environment, because the restrictions are different. We only have to
23311 // check conflicts across regions for the map clauses.
23312 if (checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
23313 /*CurrentRegionOnly=*/true, CurComponents, CKind))
23314 break;
23315 if (CKind == OMPC_map &&
23316 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) &&
23317 checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
23318 /*CurrentRegionOnly=*/false, CurComponents, CKind))
23319 break;
23320
23321 // OpenMP 4.5 [2.10.5, target update Construct]
23322 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
23323 // If the type of a list item is a reference to a type T then the type will
23324 // be considered to be T for all purposes of this clause.
23325 auto I = llvm::find_if(
23326 Range&: CurComponents,
23327 P: [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
23328 return MC.getAssociatedDeclaration();
23329 });
23330 assert(I != CurComponents.end() && "Null decl on map clause.");
23331 (void)I;
23332 QualType Type;
23333 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: VE->IgnoreParens());
23334 auto *OASE = dyn_cast<ArraySectionExpr>(Val: VE->IgnoreParens());
23335 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(Val: VE->IgnoreParens());
23336 if (ASE) {
23337 Type = ASE->getType().getNonReferenceType();
23338 } else if (OASE) {
23339 QualType BaseType =
23340 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
23341 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
23342 Type = ATy->getElementType();
23343 else
23344 Type = BaseType->getPointeeType();
23345 Type = Type.getNonReferenceType();
23346 } else if (OAShE) {
23347 Type = OAShE->getBase()->getType()->getPointeeType();
23348 } else {
23349 Type = VE->getType();
23350 }
23351
23352 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
23353 // A list item in a to or from clause must have a mappable type.
23354 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
23355 // A list item must have a mappable type.
23356 if (!checkTypeMappable(SL: VE->getExprLoc(), SR: VE->getSourceRange(), SemaRef,
23357 Stack: DSAS, QTy: Type, /*FullCheck=*/true))
23358 continue;
23359
23360 if (CKind == OMPC_map) {
23361 // target enter data
23362 // OpenMP [2.10.2, Restrictions, p. 99]
23363 // A map-type must be specified in all map clauses and must be either
23364 // to or alloc. Starting with OpenMP 5.2 the default map type is `to` if
23365 // no map type is present.
23366 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
23367 if (DKind == OMPD_target_enter_data &&
23368 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc ||
23369 SemaRef.getLangOpts().OpenMP >= 52)) {
23370 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23371 << (IsMapTypeImplicit ? 1 : 0)
23372 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23373 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23374 continue;
23375 }
23376
23377 // target exit_data
23378 // OpenMP [2.10.3, Restrictions, p. 102]
23379 // A map-type must be specified in all map clauses and must be either
23380 // from, release, or delete. Starting with OpenMP 5.2 the default map
23381 // type is `from` if no map type is present.
23382 if (DKind == OMPD_target_exit_data &&
23383 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
23384 MapType == OMPC_MAP_delete || SemaRef.getLangOpts().OpenMP >= 52)) {
23385 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23386 << (IsMapTypeImplicit ? 1 : 0)
23387 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23388 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23389 continue;
23390 }
23391
23392 // The 'ompx_hold' modifier is specifically intended to be used on a
23393 // 'target' or 'target data' directive to prevent data from being unmapped
23394 // during the associated statement. It is not permitted on a 'target
23395 // enter data' or 'target exit data' directive, which have no associated
23396 // statement.
23397 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) &&
23398 HasHoldModifier) {
23399 SemaRef.Diag(Loc: StartLoc,
23400 DiagID: diag::err_omp_invalid_map_type_modifier_for_directive)
23401 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map,
23402 Type: OMPC_MAP_MODIFIER_ompx_hold)
23403 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23404 continue;
23405 }
23406
23407 // target, target data
23408 // OpenMP 5.0 [2.12.2, Restrictions, p. 163]
23409 // OpenMP 5.0 [2.12.5, Restrictions, p. 174]
23410 // A map-type in a map clause must be to, from, tofrom or alloc
23411 if ((DKind == OMPD_target_data ||
23412 isOpenMPTargetExecutionDirective(DKind)) &&
23413 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from ||
23414 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) {
23415 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23416 << (IsMapTypeImplicit ? 1 : 0)
23417 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23418 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23419 continue;
23420 }
23421
23422 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
23423 // A list item cannot appear in both a map clause and a data-sharing
23424 // attribute clause on the same construct
23425 //
23426 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
23427 // A list item cannot appear in both a map clause and a data-sharing
23428 // attribute clause on the same construct unless the construct is a
23429 // combined construct.
23430 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
23431 isOpenMPTargetExecutionDirective(DKind)) ||
23432 DKind == OMPD_target)) {
23433 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
23434 if (isOpenMPPrivate(Kind: DVar.CKind)) {
23435 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
23436 << getOpenMPClauseNameForDiag(C: DVar.CKind)
23437 << getOpenMPClauseNameForDiag(C: OMPC_map)
23438 << getOpenMPDirectiveName(D: DSAS->getCurrentDirective(),
23439 Ver: OMPVersion);
23440 reportOriginalDsa(SemaRef, Stack: DSAS, D: CurDeclaration, DVar);
23441 continue;
23442 }
23443 }
23444 }
23445
23446 // Try to find the associated user-defined mapper.
23447 ExprResult ER = buildUserDefinedMapperRef(
23448 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23449 Type: Type.getCanonicalType(), UnresolvedMapper);
23450 if (ER.isInvalid())
23451 continue;
23452
23453 // If no user-defined mapper is found, we need to create an implicit one for
23454 // arrays/array-sections on structs that have members that have
23455 // user-defined mappers. This is needed to ensure that the mapper for the
23456 // member is invoked when mapping each element of the array/array-section.
23457 if (!ER.get()) {
23458 QualType BaseType;
23459
23460 if (isa<ArraySectionExpr>(Val: VE)) {
23461 BaseType = VE->getType().getCanonicalType();
23462 if (BaseType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
23463 const auto *OASE = cast<ArraySectionExpr>(Val: VE->IgnoreParenImpCasts());
23464 QualType BType =
23465 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
23466 QualType ElemType;
23467 if (const auto *ATy = BType->getAsArrayTypeUnsafe())
23468 ElemType = ATy->getElementType();
23469 else
23470 ElemType = BType->getPointeeType();
23471 BaseType = ElemType.getCanonicalType();
23472 }
23473 } else if (VE->getType()->isArrayType()) {
23474 const ArrayType *AT = VE->getType()->getAsArrayTypeUnsafe();
23475 const QualType ElemType = AT->getElementType();
23476 BaseType = ElemType.getCanonicalType();
23477 }
23478
23479 if (!BaseType.isNull() && BaseType->getAsRecordDecl() &&
23480 isImplicitMapperNeeded(S&: SemaRef, Stack: DSAS, CanonType: BaseType, E: VE)) {
23481 ER = buildImplicitMapper(S&: SemaRef, BaseType, Stack: DSAS);
23482 }
23483 }
23484 MVLI.UDMapperList.push_back(Elt: ER.get());
23485
23486 // Save the current expression.
23487 MVLI.ProcessedVarList.push_back(Elt: RE);
23488
23489 // Store the components in the stack so that they can be used to check
23490 // against other clauses later on.
23491 DSAS->addMappableExpressionComponents(VD: CurDeclaration, Components: CurComponents,
23492 /*WhereFoundClauseKind=*/OMPC_map);
23493
23494 // Save the components and declaration to create the clause. For purposes of
23495 // the clause creation, any component list that has base 'this' uses
23496 // null as base declaration.
23497 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
23498 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
23499 in_end: CurComponents.end());
23500 MVLI.VarBaseDeclarations.push_back(Elt: isa<MemberExpr>(Val: BE) ? nullptr
23501 : CurDeclaration);
23502 }
23503}
23504
23505OMPClause *SemaOpenMP::ActOnOpenMPMapClause(
23506 Expr *IteratorModifier, ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
23507 ArrayRef<SourceLocation> MapTypeModifiersLoc,
23508 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
23509 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
23510 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
23511 const OMPVarListLocTy &Locs, bool NoDiagnose,
23512 ArrayRef<Expr *> UnresolvedMappers) {
23513 OpenMPMapModifierKind Modifiers[] = {
23514 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23515 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23516 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23517 OMPC_MAP_MODIFIER_unknown};
23518 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
23519
23520 if (IteratorModifier && !IteratorModifier->getType()->isSpecificBuiltinType(
23521 K: BuiltinType::OMPIterator))
23522 Diag(Loc: IteratorModifier->getExprLoc(),
23523 DiagID: diag::err_omp_map_modifier_not_iterator);
23524
23525 // Process map-type-modifiers, flag errors for duplicate modifiers.
23526 unsigned Count = 0;
23527 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
23528 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
23529 llvm::is_contained(Range&: Modifiers, Element: MapTypeModifiers[I])) {
23530 Diag(Loc: MapTypeModifiersLoc[I], DiagID: diag::err_omp_duplicate_map_type_modifier);
23531 continue;
23532 }
23533 assert(Count < NumberOfOMPMapClauseModifiers &&
23534 "Modifiers exceed the allowed number of map type modifiers");
23535 Modifiers[Count] = MapTypeModifiers[I];
23536 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
23537 ++Count;
23538 }
23539
23540 MappableVarListInfo MVLI(VarList);
23541 // Per OpenMP 6.0 p299 lines 3-4, a list item with the const specifier and
23542 // no mutable members is ignored for 'from' clauses. A const-qualified
23543 // variable cannot be modified on the device, so copying back to the host
23544 // is unnecessary and potentially unsafe. Strip the FROM component:
23545 // map(tofrom:) -> map(to:), map(from:) -> map(alloc:).
23546 for (auto *E : VarList) {
23547 if ((MapType == OMPC_MAP_from || MapType == OMPC_MAP_tofrom) &&
23548 hasConstQualifiedMappingType(T: E->getType()))
23549 MapType = (MapType == OMPC_MAP_tofrom) ? OMPC_MAP_to : OMPC_MAP_alloc;
23550 }
23551 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_map, MVLI, StartLoc: Locs.StartLoc,
23552 MapperIdScopeSpec, MapperId, UnresolvedMappers,
23553 MapType, Modifiers, IsMapTypeImplicit,
23554 NoDiagnose);
23555
23556 // We need to produce a map clause even if we don't have variables so that
23557 // other diagnostics related with non-existing map clauses are accurate.
23558 return OMPMapClause::Create(
23559 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
23560 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier, MapModifiers: Modifiers,
23561 MapModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
23562 MapperId, Type: MapType, TypeIsImplicit: IsMapTypeImplicit, TypeLoc: MapLoc);
23563}
23564
23565QualType SemaOpenMP::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
23566 TypeResult ParsedType) {
23567 assert(ParsedType.isUsable());
23568
23569 QualType ReductionType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
23570 if (ReductionType.isNull())
23571 return QualType();
23572
23573 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
23574 // A type name in a declare reduction directive cannot be a function type, an
23575 // array type, a reference type, or a type qualified with const, volatile or
23576 // restrict.
23577 if (ReductionType.hasQualifiers()) {
23578 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 0;
23579 return QualType();
23580 }
23581
23582 if (ReductionType->isFunctionType()) {
23583 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 1;
23584 return QualType();
23585 }
23586 if (ReductionType->isReferenceType()) {
23587 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 2;
23588 return QualType();
23589 }
23590 if (ReductionType->isArrayType()) {
23591 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 3;
23592 return QualType();
23593 }
23594 return ReductionType;
23595}
23596
23597SemaOpenMP::DeclGroupPtrTy
23598SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveStart(
23599 Scope *S, DeclContext *DC, DeclarationName Name,
23600 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
23601 AccessSpecifier AS, Decl *PrevDeclInScope) {
23602 SmallVector<Decl *, 8> Decls;
23603 Decls.reserve(N: ReductionTypes.size());
23604
23605 LookupResult Lookup(SemaRef, Name, SourceLocation(),
23606 Sema::LookupOMPReductionName,
23607 SemaRef.forRedeclarationInCurContext());
23608 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
23609 // A reduction-identifier may not be re-declared in the current scope for the
23610 // same type or for a type that is compatible according to the base language
23611 // rules.
23612 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
23613 OMPDeclareReductionDecl *PrevDRD = nullptr;
23614 bool InCompoundScope = true;
23615 if (S != nullptr) {
23616 // Find previous declaration with the same name not referenced in other
23617 // declarations.
23618 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
23619 InCompoundScope =
23620 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
23621 SemaRef.LookupName(R&: Lookup, S);
23622 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
23623 /*AllowInlineNamespace=*/false);
23624 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
23625 LookupResult::Filter Filter = Lookup.makeFilter();
23626 while (Filter.hasNext()) {
23627 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Val: Filter.next());
23628 if (InCompoundScope) {
23629 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
23630 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
23631 UsedAsPrevious[D] = true;
23632 }
23633 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
23634 PrevDecl->getLocation();
23635 }
23636 Filter.done();
23637 if (InCompoundScope) {
23638 for (const auto &PrevData : UsedAsPrevious) {
23639 if (!PrevData.second) {
23640 PrevDRD = PrevData.first;
23641 break;
23642 }
23643 }
23644 }
23645 } else if (PrevDeclInScope != nullptr) {
23646 auto *PrevDRDInScope = PrevDRD =
23647 cast<OMPDeclareReductionDecl>(Val: PrevDeclInScope);
23648 do {
23649 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
23650 PrevDRDInScope->getLocation();
23651 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
23652 } while (PrevDRDInScope != nullptr);
23653 }
23654 for (const auto &TyData : ReductionTypes) {
23655 const auto I = PreviousRedeclTypes.find(Val: TyData.first.getCanonicalType());
23656 bool Invalid = false;
23657 if (I != PreviousRedeclTypes.end()) {
23658 Diag(Loc: TyData.second, DiagID: diag::err_omp_declare_reduction_redefinition)
23659 << TyData.first;
23660 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
23661 Invalid = true;
23662 }
23663 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
23664 auto *DRD = OMPDeclareReductionDecl::Create(
23665 C&: getASTContext(), DC, L: TyData.second, Name, T: TyData.first, PrevDeclInScope: PrevDRD);
23666 DC->addDecl(D: DRD);
23667 DRD->setAccess(AS);
23668 Decls.push_back(Elt: DRD);
23669 if (Invalid)
23670 DRD->setInvalidDecl();
23671 else
23672 PrevDRD = DRD;
23673 }
23674
23675 return DeclGroupPtrTy::make(
23676 P: DeclGroupRef::Create(C&: getASTContext(), Decls: Decls.begin(), NumDecls: Decls.size()));
23677}
23678
23679void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
23680 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23681
23682 // Enter new function scope.
23683 SemaRef.PushFunctionScope();
23684 SemaRef.setFunctionHasBranchProtectedScope();
23685 SemaRef.getCurFunction()->setHasOMPDeclareReductionCombiner();
23686
23687 if (S != nullptr)
23688 SemaRef.PushDeclContext(S, DC: DRD);
23689 else
23690 SemaRef.CurContext = DRD;
23691
23692 SemaRef.PushExpressionEvaluationContext(
23693 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
23694
23695 QualType ReductionType = DRD->getType();
23696 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
23697 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
23698 // uses semantics of argument handles by value, but it should be passed by
23699 // reference. C lang does not support references, so pass all parameters as
23700 // pointers.
23701 // Create 'T omp_in;' variable.
23702 VarDecl *OmpInParm =
23703 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_in");
23704 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
23705 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
23706 // uses semantics of argument handles by value, but it should be passed by
23707 // reference. C lang does not support references, so pass all parameters as
23708 // pointers.
23709 // Create 'T omp_out;' variable.
23710 VarDecl *OmpOutParm =
23711 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_out");
23712 if (S != nullptr) {
23713 SemaRef.PushOnScopeChains(D: OmpInParm, S);
23714 SemaRef.PushOnScopeChains(D: OmpOutParm, S);
23715 } else {
23716 DRD->addDecl(D: OmpInParm);
23717 DRD->addDecl(D: OmpOutParm);
23718 }
23719 Expr *InE =
23720 ::buildDeclRefExpr(S&: SemaRef, D: OmpInParm, Ty: ReductionType, Loc: D->getLocation());
23721 Expr *OutE =
23722 ::buildDeclRefExpr(S&: SemaRef, D: OmpOutParm, Ty: ReductionType, Loc: D->getLocation());
23723 DRD->setCombinerData(InE, OutE);
23724}
23725
23726void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D,
23727 Expr *Combiner) {
23728 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23729 SemaRef.DiscardCleanupsInEvaluationContext();
23730 SemaRef.PopExpressionEvaluationContext();
23731
23732 SemaRef.PopDeclContext();
23733 SemaRef.PopFunctionScopeInfo();
23734
23735 if (Combiner != nullptr)
23736 DRD->setCombiner(Combiner);
23737 else
23738 DRD->setInvalidDecl();
23739}
23740
23741VarDecl *SemaOpenMP::ActOnOpenMPDeclareReductionInitializerStart(Scope *S,
23742 Decl *D) {
23743 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23744
23745 // Enter new function scope.
23746 SemaRef.PushFunctionScope();
23747 SemaRef.setFunctionHasBranchProtectedScope();
23748
23749 if (S != nullptr)
23750 SemaRef.PushDeclContext(S, DC: DRD);
23751 else
23752 SemaRef.CurContext = DRD;
23753
23754 SemaRef.PushExpressionEvaluationContext(
23755 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
23756
23757 QualType ReductionType = DRD->getType();
23758 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
23759 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
23760 // uses semantics of argument handles by value, but it should be passed by
23761 // reference. C lang does not support references, so pass all parameters as
23762 // pointers.
23763 // Create 'T omp_priv;' variable.
23764 VarDecl *OmpPrivParm =
23765 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_priv");
23766 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
23767 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
23768 // uses semantics of argument handles by value, but it should be passed by
23769 // reference. C lang does not support references, so pass all parameters as
23770 // pointers.
23771 // Create 'T omp_orig;' variable.
23772 VarDecl *OmpOrigParm =
23773 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_orig");
23774 if (S != nullptr) {
23775 SemaRef.PushOnScopeChains(D: OmpPrivParm, S);
23776 SemaRef.PushOnScopeChains(D: OmpOrigParm, S);
23777 } else {
23778 DRD->addDecl(D: OmpPrivParm);
23779 DRD->addDecl(D: OmpOrigParm);
23780 }
23781 Expr *OrigE =
23782 ::buildDeclRefExpr(S&: SemaRef, D: OmpOrigParm, Ty: ReductionType, Loc: D->getLocation());
23783 Expr *PrivE =
23784 ::buildDeclRefExpr(S&: SemaRef, D: OmpPrivParm, Ty: ReductionType, Loc: D->getLocation());
23785 DRD->setInitializerData(OrigE, PrivE);
23786 return OmpPrivParm;
23787}
23788
23789void SemaOpenMP::ActOnOpenMPDeclareReductionInitializerEnd(
23790 Decl *D, Expr *Initializer, VarDecl *OmpPrivParm) {
23791 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23792 SemaRef.DiscardCleanupsInEvaluationContext();
23793 SemaRef.PopExpressionEvaluationContext();
23794
23795 SemaRef.PopDeclContext();
23796 SemaRef.PopFunctionScopeInfo();
23797
23798 if (Initializer != nullptr) {
23799 DRD->setInitializer(E: Initializer, IK: OMPDeclareReductionInitKind::Call);
23800 } else if (OmpPrivParm->hasInit()) {
23801 DRD->setInitializer(E: OmpPrivParm->getInit(),
23802 IK: OmpPrivParm->isDirectInit()
23803 ? OMPDeclareReductionInitKind::Direct
23804 : OMPDeclareReductionInitKind::Copy);
23805 } else {
23806 DRD->setInvalidDecl();
23807 }
23808}
23809
23810SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveEnd(
23811 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
23812 for (Decl *D : DeclReductions.get()) {
23813 if (IsValid) {
23814 if (S)
23815 SemaRef.PushOnScopeChains(D: cast<OMPDeclareReductionDecl>(Val: D), S,
23816 /*AddToContext=*/false);
23817 } else {
23818 D->setInvalidDecl();
23819 }
23820 }
23821 return DeclReductions;
23822}
23823
23824TypeResult SemaOpenMP::ActOnOpenMPDeclareMapperVarDecl(Scope *S,
23825 Declarator &D) {
23826 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
23827 QualType T = TInfo->getType();
23828 if (D.isInvalidType())
23829 return true;
23830
23831 if (getLangOpts().CPlusPlus) {
23832 // Check that there are no default arguments (C++ only).
23833 SemaRef.CheckExtraCXXDefaultArguments(D);
23834 }
23835
23836 return SemaRef.CreateParsedType(T, TInfo);
23837}
23838
23839QualType SemaOpenMP::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
23840 TypeResult ParsedType) {
23841 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
23842
23843 QualType MapperType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
23844 assert(!MapperType.isNull() && "Expect valid mapper type");
23845
23846 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
23847 // The type must be of struct, union or class type in C and C++
23848 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
23849 Diag(Loc: TyLoc, DiagID: diag::err_omp_mapper_wrong_type);
23850 return QualType();
23851 }
23852 return MapperType;
23853}
23854
23855SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareMapperDirective(
23856 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
23857 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
23858 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) {
23859 LookupResult Lookup(SemaRef, Name, SourceLocation(),
23860 Sema::LookupOMPMapperName,
23861 SemaRef.forRedeclarationInCurContext());
23862 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
23863 // A mapper-identifier may not be redeclared in the current scope for the
23864 // same type or for a type that is compatible according to the base language
23865 // rules.
23866 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
23867 OMPDeclareMapperDecl *PrevDMD = nullptr;
23868 bool InCompoundScope = true;
23869 if (S != nullptr) {
23870 // Find previous declaration with the same name not referenced in other
23871 // declarations.
23872 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
23873 InCompoundScope =
23874 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
23875 SemaRef.LookupName(R&: Lookup, S);
23876 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
23877 /*AllowInlineNamespace=*/false);
23878 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
23879 LookupResult::Filter Filter = Lookup.makeFilter();
23880 while (Filter.hasNext()) {
23881 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Val: Filter.next());
23882 if (InCompoundScope) {
23883 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
23884 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
23885 UsedAsPrevious[D] = true;
23886 }
23887 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
23888 PrevDecl->getLocation();
23889 }
23890 Filter.done();
23891 if (InCompoundScope) {
23892 for (const auto &PrevData : UsedAsPrevious) {
23893 if (!PrevData.second) {
23894 PrevDMD = PrevData.first;
23895 break;
23896 }
23897 }
23898 }
23899 } else if (PrevDeclInScope) {
23900 auto *PrevDMDInScope = PrevDMD =
23901 cast<OMPDeclareMapperDecl>(Val: PrevDeclInScope);
23902 do {
23903 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
23904 PrevDMDInScope->getLocation();
23905 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
23906 } while (PrevDMDInScope != nullptr);
23907 }
23908 const auto I = PreviousRedeclTypes.find(Val: MapperType.getCanonicalType());
23909 bool Invalid = false;
23910 if (I != PreviousRedeclTypes.end()) {
23911 Diag(Loc: StartLoc, DiagID: diag::err_omp_declare_mapper_redefinition)
23912 << MapperType << Name;
23913 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
23914 Invalid = true;
23915 }
23916 // Build expressions for implicit maps of data members with 'default'
23917 // mappers.
23918 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses);
23919 if (getLangOpts().OpenMP >= 50)
23920 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
23921 Clauses&: ClausesWithImplicit);
23922 auto *DMD = OMPDeclareMapperDecl::Create(C&: getASTContext(), DC, L: StartLoc, Name,
23923 T: MapperType, VarName: VN, Clauses: ClausesWithImplicit,
23924 PrevDeclInScope: PrevDMD);
23925 if (S)
23926 SemaRef.PushOnScopeChains(D: DMD, S);
23927 else
23928 DC->addDecl(D: DMD);
23929 DMD->setAccess(AS);
23930 if (Invalid)
23931 DMD->setInvalidDecl();
23932
23933 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
23934 VD->setDeclContext(DMD);
23935 VD->setLexicalDeclContext(DMD);
23936 DMD->addDecl(D: VD);
23937 DMD->setMapperVarRef(MapperVarRef);
23938
23939 return DeclGroupPtrTy::make(P: DeclGroupRef(DMD));
23940}
23941
23942ExprResult SemaOpenMP::ActOnOpenMPDeclareMapperDirectiveVarDecl(
23943 Scope *S, QualType MapperType, SourceLocation StartLoc,
23944 DeclarationName VN) {
23945 TypeSourceInfo *TInfo =
23946 getASTContext().getTrivialTypeSourceInfo(T: MapperType, Loc: StartLoc);
23947 auto *VD = VarDecl::Create(
23948 C&: getASTContext(), DC: getASTContext().getTranslationUnitDecl(), StartLoc,
23949 IdLoc: StartLoc, Id: VN.getAsIdentifierInfo(), T: MapperType, TInfo, S: SC_None);
23950 if (S)
23951 SemaRef.PushOnScopeChains(D: VD, S, /*AddToContext=*/false);
23952 Expr *E = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: MapperType, Loc: StartLoc);
23953 DSAStack->addDeclareMapperVarRef(Ref: E);
23954 return E;
23955}
23956
23957void SemaOpenMP::ActOnOpenMPIteratorVarDecl(VarDecl *VD) {
23958 bool IsGlobalVar =
23959 !VD->isLocalVarDecl() && VD->getDeclContext()->isTranslationUnit();
23960 if (DSAStack->getDeclareMapperVarRef()) {
23961 if (IsGlobalVar)
23962 SemaRef.Consumer.HandleTopLevelDecl(D: DeclGroupRef(VD));
23963 DSAStack->addIteratorVarDecl(VD);
23964 } else {
23965 // Currently, only declare mapper handles global-scope iterator vars.
23966 assert(!IsGlobalVar && "Only declare mapper handles TU-scope iterators.");
23967 }
23968}
23969
23970bool SemaOpenMP::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const {
23971 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
23972 const Expr *Ref = DSAStack->getDeclareMapperVarRef();
23973 if (const auto *DRE = cast_or_null<DeclRefExpr>(Val: Ref)) {
23974 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl())
23975 return true;
23976 if (VD->isUsableInConstantExpressions(C: getASTContext()))
23977 return true;
23978 if (getLangOpts().OpenMP >= 52 && DSAStack->isIteratorVarDecl(VD))
23979 return true;
23980 return false;
23981 }
23982 return true;
23983}
23984
23985const ValueDecl *SemaOpenMP::getOpenMPDeclareMapperVarName() const {
23986 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
23987 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl();
23988}
23989
23990OMPClause *SemaOpenMP::ActOnOpenMPNumTeamsClause(ArrayRef<Expr *> VarList,
23991 SourceLocation StartLoc,
23992 SourceLocation LParenLoc,
23993 SourceLocation EndLoc) {
23994 if (VarList.empty())
23995 return nullptr;
23996
23997 for (Expr *ValExpr : VarList) {
23998 // OpenMP [teams Constrcut, Restrictions]
23999 // The num_teams expression must evaluate to a positive integer value.
24000 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_teams,
24001 /*StrictlyPositive=*/true))
24002 return nullptr;
24003 }
24004
24005 // OpenMP 5.2: Validate lower-bound ≤ upper-bound constraint
24006 if (VarList.size() == 2) {
24007 Expr *LowerBound = VarList[0];
24008 Expr *UpperBound = VarList[1];
24009
24010 // Check if both are compile-time constants for validation
24011 if (!LowerBound->isValueDependent() && !UpperBound->isValueDependent() &&
24012 LowerBound->isIntegerConstantExpr(Ctx: getASTContext()) &&
24013 UpperBound->isIntegerConstantExpr(Ctx: getASTContext())) {
24014
24015 // Get the actual constant values
24016 llvm::APSInt LowerVal =
24017 LowerBound->EvaluateKnownConstInt(Ctx: getASTContext());
24018 llvm::APSInt UpperVal =
24019 UpperBound->EvaluateKnownConstInt(Ctx: getASTContext());
24020
24021 if (LowerVal > UpperVal) {
24022 Diag(Loc: LowerBound->getExprLoc(),
24023 DiagID: diag::err_omp_num_teams_lower_bound_larger)
24024 << LowerBound->getSourceRange() << UpperBound->getSourceRange();
24025 return nullptr;
24026 }
24027 }
24028 }
24029
24030 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
24031 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
24032 DKind, CKind: OMPC_num_teams, OpenMPVersion: getLangOpts().OpenMP);
24033 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
24034 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
24035 LParenLoc, EndLoc, VL: VarList,
24036 /*PreInit=*/nullptr);
24037
24038 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
24039 SmallVector<Expr *, 3> Vars;
24040 for (Expr *ValExpr : VarList) {
24041 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
24042 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
24043 Vars.push_back(Elt: ValExpr);
24044 }
24045
24046 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
24047 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
24048 LParenLoc, EndLoc, VL: Vars, PreInit);
24049}
24050
24051OMPClause *SemaOpenMP::ActOnOpenMPThreadLimitClause(ArrayRef<Expr *> VarList,
24052 SourceLocation StartLoc,
24053 SourceLocation LParenLoc,
24054 SourceLocation EndLoc) {
24055 if (VarList.empty())
24056 return nullptr;
24057
24058 for (Expr *ValExpr : VarList) {
24059 // OpenMP [teams Constrcut, Restrictions]
24060 // The thread_limit expression must evaluate to a positive integer value.
24061 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_thread_limit,
24062 /*StrictlyPositive=*/true))
24063 return nullptr;
24064 }
24065
24066 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
24067 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
24068 DKind, CKind: OMPC_thread_limit, OpenMPVersion: getLangOpts().OpenMP);
24069 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
24070 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion,
24071 StartLoc, LParenLoc, EndLoc, VL: VarList,
24072 /*PreInit=*/nullptr);
24073
24074 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
24075 SmallVector<Expr *, 3> Vars;
24076 for (Expr *ValExpr : VarList) {
24077 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
24078 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
24079 Vars.push_back(Elt: ValExpr);
24080 }
24081
24082 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
24083 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
24084 LParenLoc, EndLoc, VL: Vars, PreInit);
24085}
24086
24087OMPClause *SemaOpenMP::ActOnOpenMPPriorityClause(Expr *Priority,
24088 SourceLocation StartLoc,
24089 SourceLocation LParenLoc,
24090 SourceLocation EndLoc) {
24091 Expr *ValExpr = Priority;
24092 Stmt *HelperValStmt = nullptr;
24093 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
24094
24095 // OpenMP [2.9.1, task Constrcut]
24096 // The priority-value is a non-negative numerical scalar expression.
24097 if (!isNonNegativeIntegerValue(
24098 ValExpr, SemaRef, CKind: OMPC_priority,
24099 /*StrictlyPositive=*/false, /*BuildCapture=*/true,
24100 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
24101 return nullptr;
24102
24103 return new (getASTContext()) OMPPriorityClause(
24104 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
24105}
24106
24107OMPClause *SemaOpenMP::ActOnOpenMPGrainsizeClause(
24108 OpenMPGrainsizeClauseModifier Modifier, Expr *Grainsize,
24109 SourceLocation StartLoc, SourceLocation LParenLoc,
24110 SourceLocation ModifierLoc, SourceLocation EndLoc) {
24111 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
24112 "Unexpected grainsize modifier in OpenMP < 51.");
24113
24114 if (ModifierLoc.isValid() && Modifier == OMPC_GRAINSIZE_unknown) {
24115 std::string Values = getListOfPossibleValues(K: OMPC_grainsize, /*First=*/0,
24116 Last: OMPC_GRAINSIZE_unknown);
24117 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
24118 << Values << getOpenMPClauseNameForDiag(C: OMPC_grainsize);
24119 return nullptr;
24120 }
24121
24122 Expr *ValExpr = Grainsize;
24123 Stmt *HelperValStmt = nullptr;
24124 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
24125
24126 // OpenMP [2.9.2, taskloop Constrcut]
24127 // The parameter of the grainsize clause must be a positive integer
24128 // expression.
24129 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_grainsize,
24130 /*StrictlyPositive=*/true,
24131 /*BuildCapture=*/true,
24132 DSAStack->getCurrentDirective(),
24133 CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
24134 return nullptr;
24135
24136 return new (getASTContext())
24137 OMPGrainsizeClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
24138 StartLoc, LParenLoc, ModifierLoc, EndLoc);
24139}
24140
24141OMPClause *SemaOpenMP::ActOnOpenMPNumTasksClause(
24142 OpenMPNumTasksClauseModifier Modifier, Expr *NumTasks,
24143 SourceLocation StartLoc, SourceLocation LParenLoc,
24144 SourceLocation ModifierLoc, SourceLocation EndLoc) {
24145 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
24146 "Unexpected num_tasks modifier in OpenMP < 51.");
24147
24148 if (ModifierLoc.isValid() && Modifier == OMPC_NUMTASKS_unknown) {
24149 std::string Values = getListOfPossibleValues(K: OMPC_num_tasks, /*First=*/0,
24150 Last: OMPC_NUMTASKS_unknown);
24151 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
24152 << Values << getOpenMPClauseNameForDiag(C: OMPC_num_tasks);
24153 return nullptr;
24154 }
24155
24156 Expr *ValExpr = NumTasks;
24157 Stmt *HelperValStmt = nullptr;
24158 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
24159
24160 // OpenMP [2.9.2, taskloop Constrcut]
24161 // The parameter of the num_tasks clause must be a positive integer
24162 // expression.
24163 if (!isNonNegativeIntegerValue(
24164 ValExpr, SemaRef, CKind: OMPC_num_tasks,
24165 /*StrictlyPositive=*/true, /*BuildCapture=*/true,
24166 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
24167 return nullptr;
24168
24169 return new (getASTContext())
24170 OMPNumTasksClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
24171 StartLoc, LParenLoc, ModifierLoc, EndLoc);
24172}
24173
24174OMPClause *SemaOpenMP::ActOnOpenMPHintClause(Expr *Hint,
24175 SourceLocation StartLoc,
24176 SourceLocation LParenLoc,
24177 SourceLocation EndLoc) {
24178 // OpenMP [2.13.2, critical construct, Description]
24179 // ... where hint-expression is an integer constant expression that evaluates
24180 // to a valid lock hint.
24181 ExprResult HintExpr =
24182 VerifyPositiveIntegerConstantInClause(E: Hint, CKind: OMPC_hint, StrictlyPositive: false);
24183 if (HintExpr.isInvalid())
24184 return nullptr;
24185 return new (getASTContext())
24186 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
24187}
24188
24189/// Tries to find omp_event_handle_t type.
24190static bool findOMPEventHandleT(Sema &S, SourceLocation Loc,
24191 DSAStackTy *Stack) {
24192 QualType OMPEventHandleT = Stack->getOMPEventHandleT();
24193 if (!OMPEventHandleT.isNull())
24194 return true;
24195 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_event_handle_t");
24196 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
24197 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
24198 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_event_handle_t";
24199 return false;
24200 }
24201 Stack->setOMPEventHandleT(PT.get());
24202 return true;
24203}
24204
24205OMPClause *SemaOpenMP::ActOnOpenMPDetachClause(Expr *Evt,
24206 SourceLocation StartLoc,
24207 SourceLocation LParenLoc,
24208 SourceLocation EndLoc) {
24209 if (!Evt->isValueDependent() && !Evt->isTypeDependent() &&
24210 !Evt->isInstantiationDependent() &&
24211 !Evt->containsUnexpandedParameterPack()) {
24212 if (!findOMPEventHandleT(S&: SemaRef, Loc: Evt->getExprLoc(), DSAStack))
24213 return nullptr;
24214 // OpenMP 5.0, 2.10.1 task Construct.
24215 // event-handle is a variable of the omp_event_handle_t type.
24216 auto *Ref = dyn_cast<DeclRefExpr>(Val: Evt->IgnoreParenImpCasts());
24217 if (!Ref) {
24218 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24219 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
24220 return nullptr;
24221 }
24222 auto *VD = dyn_cast_or_null<VarDecl>(Val: Ref->getDecl());
24223 if (!VD) {
24224 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24225 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
24226 return nullptr;
24227 }
24228 if (!getASTContext().hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(),
24229 T2: VD->getType()) ||
24230 VD->getType().isConstant(Ctx: getASTContext())) {
24231 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24232 << "omp_event_handle_t" << 1 << VD->getType()
24233 << Evt->getSourceRange();
24234 return nullptr;
24235 }
24236 // OpenMP 5.0, 2.10.1 task Construct
24237 // [detach clause]... The event-handle will be considered as if it was
24238 // specified on a firstprivate clause.
24239 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
24240 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
24241 DVar.RefExpr) {
24242 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
24243 << getOpenMPClauseNameForDiag(C: DVar.CKind)
24244 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
24245 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
24246 return nullptr;
24247 }
24248 }
24249
24250 return new (getASTContext())
24251 OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc);
24252}
24253
24254OMPClause *SemaOpenMP::ActOnOpenMPDistScheduleClause(
24255 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
24256 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
24257 SourceLocation EndLoc) {
24258 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
24259 std::string Values;
24260 Values += "'";
24261 Values += getOpenMPSimpleClauseTypeName(Kind: OMPC_dist_schedule, Type: 0);
24262 Values += "'";
24263 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24264 << Values << getOpenMPClauseNameForDiag(C: OMPC_dist_schedule);
24265 return nullptr;
24266 }
24267 Expr *ValExpr = ChunkSize;
24268 Stmt *HelperValStmt = nullptr;
24269 if (ChunkSize) {
24270 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
24271 !ChunkSize->isInstantiationDependent() &&
24272 !ChunkSize->containsUnexpandedParameterPack()) {
24273 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
24274 ExprResult Val =
24275 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
24276 if (Val.isInvalid())
24277 return nullptr;
24278
24279 ValExpr = Val.get();
24280
24281 // OpenMP [2.7.1, Restrictions]
24282 // chunk_size must be a loop invariant integer expression with a positive
24283 // value.
24284 if (std::optional<llvm::APSInt> Result =
24285 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
24286 if (Result->isSigned() && !Result->isStrictlyPositive()) {
24287 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
24288 << "dist_schedule" << /*strictly positive*/ 1
24289 << ChunkSize->getSourceRange();
24290 return nullptr;
24291 }
24292 } else if (getOpenMPCaptureRegionForClause(
24293 DSAStack->getCurrentDirective(), CKind: OMPC_dist_schedule,
24294 OpenMPVersion: getLangOpts().OpenMP) != OMPD_unknown &&
24295 !SemaRef.CurContext->isDependentContext()) {
24296 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
24297 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
24298 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
24299 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
24300 }
24301 }
24302 }
24303
24304 return new (getASTContext())
24305 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
24306 Kind, ValExpr, HelperValStmt);
24307}
24308
24309OMPClause *SemaOpenMP::ActOnOpenMPDefaultmapClause(
24310 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
24311 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
24312 SourceLocation KindLoc, SourceLocation EndLoc) {
24313 if (getLangOpts().OpenMP < 50) {
24314 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
24315 Kind != OMPC_DEFAULTMAP_scalar) {
24316 std::string Value;
24317 SourceLocation Loc;
24318 Value += "'";
24319 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
24320 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
24321 Type: OMPC_DEFAULTMAP_MODIFIER_tofrom);
24322 Loc = MLoc;
24323 } else {
24324 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
24325 Type: OMPC_DEFAULTMAP_scalar);
24326 Loc = KindLoc;
24327 }
24328 Value += "'";
24329 Diag(Loc, DiagID: diag::err_omp_unexpected_clause_value)
24330 << Value << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24331 return nullptr;
24332 }
24333 } else {
24334 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
24335 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) ||
24336 (getLangOpts().OpenMP >= 50 && KindLoc.isInvalid());
24337 if (!isDefaultmapKind || !isDefaultmapModifier) {
24338 StringRef KindValue = getLangOpts().OpenMP < 52
24339 ? "'scalar', 'aggregate', 'pointer'"
24340 : "'scalar', 'aggregate', 'pointer', 'all'";
24341 if (getLangOpts().OpenMP == 50) {
24342 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
24343 "'firstprivate', 'none', 'default'";
24344 if (!isDefaultmapKind && isDefaultmapModifier) {
24345 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24346 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24347 } else if (isDefaultmapKind && !isDefaultmapModifier) {
24348 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24349 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24350 } else {
24351 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24352 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24353 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24354 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24355 }
24356 } else {
24357 StringRef ModifierValue =
24358 getLangOpts().OpenMP < 60
24359 ? "'alloc', 'from', 'to', 'tofrom', "
24360 "'firstprivate', 'none', 'default', 'present'"
24361 : "'storage', 'from', 'to', 'tofrom', "
24362 "'firstprivate', 'private', 'none', 'default', 'present'";
24363 if (!isDefaultmapKind && isDefaultmapModifier) {
24364 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24365 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24366 } else if (isDefaultmapKind && !isDefaultmapModifier) {
24367 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24368 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24369 } else {
24370 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24371 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24372 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24373 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24374 }
24375 }
24376 return nullptr;
24377 }
24378
24379 // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
24380 // At most one defaultmap clause for each category can appear on the
24381 // directive.
24382 if (DSAStack->checkDefaultmapCategory(VariableCategory: Kind)) {
24383 Diag(Loc: StartLoc, DiagID: diag::err_omp_one_defaultmap_each_category);
24384 return nullptr;
24385 }
24386 }
24387 if (Kind == OMPC_DEFAULTMAP_unknown || Kind == OMPC_DEFAULTMAP_all) {
24388 // Variable category is not specified - mark all categories.
24389 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_aggregate, Loc: StartLoc);
24390 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_scalar, Loc: StartLoc);
24391 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_pointer, Loc: StartLoc);
24392 } else {
24393 DSAStack->setDefaultDMAAttr(M, Kind, Loc: StartLoc);
24394 }
24395
24396 return new (getASTContext())
24397 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
24398}
24399
24400bool SemaOpenMP::ActOnStartOpenMPDeclareTargetContext(
24401 DeclareTargetContextInfo &DTCI) {
24402 DeclContext *CurLexicalContext = SemaRef.getCurLexicalContext();
24403 if (!CurLexicalContext->isFileContext() &&
24404 !CurLexicalContext->isExternCContext() &&
24405 !CurLexicalContext->isExternCXXContext() &&
24406 !isa<CXXRecordDecl>(Val: CurLexicalContext) &&
24407 !isa<ClassTemplateDecl>(Val: CurLexicalContext) &&
24408 !isa<ClassTemplatePartialSpecializationDecl>(Val: CurLexicalContext) &&
24409 !isa<ClassTemplateSpecializationDecl>(Val: CurLexicalContext)) {
24410 Diag(Loc: DTCI.Loc, DiagID: diag::err_omp_region_not_file_context);
24411 return false;
24412 }
24413
24414 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
24415 if (getLangOpts().HIP)
24416 Diag(Loc: DTCI.Loc, DiagID: diag::warn_hip_omp_target_directives);
24417
24418 DeclareTargetNesting.push_back(Elt: DTCI);
24419 return true;
24420}
24421
24422const SemaOpenMP::DeclareTargetContextInfo
24423SemaOpenMP::ActOnOpenMPEndDeclareTargetDirective() {
24424 assert(!DeclareTargetNesting.empty() &&
24425 "check isInOpenMPDeclareTargetContext() first!");
24426 return DeclareTargetNesting.pop_back_val();
24427}
24428
24429void SemaOpenMP::ActOnFinishedOpenMPDeclareTargetContext(
24430 DeclareTargetContextInfo &DTCI) {
24431 for (auto &It : DTCI.ExplicitlyMapped)
24432 ActOnOpenMPDeclareTargetName(ND: It.first, Loc: It.second.Loc, MT: It.second.MT, DTCI);
24433}
24434
24435void SemaOpenMP::DiagnoseUnterminatedOpenMPDeclareTarget() {
24436 if (DeclareTargetNesting.empty())
24437 return;
24438 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
24439 unsigned OMPVersion = getLangOpts().OpenMP;
24440 Diag(Loc: DTCI.Loc, DiagID: diag::warn_omp_unterminated_declare_target)
24441 << getOpenMPDirectiveName(D: DTCI.Kind, Ver: OMPVersion);
24442}
24443
24444NamedDecl *SemaOpenMP::lookupOpenMPDeclareTargetName(
24445 Scope *CurScope, CXXScopeSpec &ScopeSpec, const DeclarationNameInfo &Id) {
24446 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
24447 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
24448 /*ObjectType=*/QualType(),
24449 /*AllowBuiltinCreation=*/true);
24450
24451 if (Lookup.isAmbiguous())
24452 return nullptr;
24453 Lookup.suppressDiagnostics();
24454
24455 if (!Lookup.isSingleResult()) {
24456 VarOrFuncDeclFilterCCC CCC(SemaRef);
24457 if (TypoCorrection Corrected =
24458 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
24459 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
24460 SemaRef.diagnoseTypo(Correction: Corrected,
24461 TypoDiag: SemaRef.PDiag(DiagID: diag::err_undeclared_var_use_suggest)
24462 << Id.getName());
24463 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: Corrected.getCorrectionDecl());
24464 return nullptr;
24465 }
24466
24467 Diag(Loc: Id.getLoc(), DiagID: diag::err_undeclared_var_use) << Id.getName();
24468 return nullptr;
24469 }
24470
24471 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
24472 if (!isa<VarDecl>(Val: ND) && !isa<FunctionDecl>(Val: ND) &&
24473 !isa<FunctionTemplateDecl>(Val: ND)) {
24474 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_invalid_target_decl) << Id.getName();
24475 return nullptr;
24476 }
24477 return ND;
24478}
24479
24480void SemaOpenMP::ActOnOpenMPDeclareTargetName(
24481 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
24482 DeclareTargetContextInfo &DTCI) {
24483 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
24484 isa<FunctionTemplateDecl>(ND)) &&
24485 "Expected variable, function or function template.");
24486
24487 if (auto *VD = dyn_cast<VarDecl>(Val: ND)) {
24488 // Only global variables can be marked as declare target.
24489 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
24490 !VD->isStaticDataMember()) {
24491 Diag(Loc, DiagID: diag::err_omp_declare_target_has_local_vars)
24492 << VD->getNameAsString();
24493 return;
24494 }
24495 }
24496 // Diagnose marking after use as it may lead to incorrect diagnosis and
24497 // codegen.
24498 if (getLangOpts().OpenMP >= 50 &&
24499 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
24500 Diag(Loc, DiagID: diag::warn_omp_declare_target_after_first_use);
24501
24502 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
24503 if (getLangOpts().HIP)
24504 Diag(Loc, DiagID: diag::warn_hip_omp_target_directives);
24505
24506 // 'local' is incompatible with 'device_type(host)' because 'local'
24507 // variables exist only on the device.
24508 if (MT == OMPDeclareTargetDeclAttr::MT_Local &&
24509 DTCI.DT == OMPDeclareTargetDeclAttr::DT_Host) {
24510 Diag(Loc, DiagID: diag::err_omp_declare_target_local_host_only);
24511 return;
24512 }
24513
24514 // Explicit declare target lists have precedence.
24515 const unsigned Level = -1;
24516
24517 auto *VD = cast<ValueDecl>(Val: ND);
24518 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
24519 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
24520 if (ActiveAttr && (*ActiveAttr)->getDevType() != DTCI.DT &&
24521 (*ActiveAttr)->getLevel() == Level) {
24522 Diag(Loc, DiagID: diag::err_omp_device_type_mismatch)
24523 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(Val: DTCI.DT)
24524 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(
24525 Val: (*ActiveAttr)->getDevType());
24526 return;
24527 }
24528 if (ActiveAttr && (*ActiveAttr)->getMapType() != MT &&
24529 (*ActiveAttr)->getLevel() == Level) {
24530 Diag(Loc, DiagID: diag::err_omp_declare_target_var_in_both_clauses)
24531 << ND
24532 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(
24533 Val: (*ActiveAttr)->getMapType())
24534 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(Val: MT);
24535 return;
24536 }
24537
24538 if (ActiveAttr && (*ActiveAttr)->getLevel() == Level)
24539 return;
24540
24541 Expr *IndirectE = nullptr;
24542 bool IsIndirect = false;
24543 if (DTCI.Indirect) {
24544 IndirectE = *DTCI.Indirect;
24545 if (!IndirectE)
24546 IsIndirect = true;
24547 }
24548 // FIXME: 'local' clause is not yet implemented in CodeGen. For now, it is
24549 // treated as 'enter'. For host compilation, 'local' is a no-op.
24550 if (MT == OMPDeclareTargetDeclAttr::MT_Local &&
24551 getLangOpts().OpenMPIsTargetDevice)
24552 Diag(Loc, DiagID: diag::warn_omp_declare_target_local_not_implemented);
24553 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
24554 Ctx&: getASTContext(), MapType: MT, DevType: DTCI.DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
24555 Range: SourceRange(Loc, Loc));
24556 ND->addAttr(A);
24557 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
24558 ML->DeclarationMarkedOpenMPDeclareTarget(D: ND, Attr: A);
24559 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: ND, IdLoc: Loc);
24560 if (auto *VD = dyn_cast<VarDecl>(Val: ND);
24561 getLangOpts().OpenMP && VD && VD->hasAttr<OMPDeclareTargetDeclAttr>() &&
24562 VD->hasGlobalStorage())
24563 ActOnOpenMPDeclareTargetInitializer(D: ND);
24564}
24565
24566static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
24567 Sema &SemaRef, Decl *D) {
24568 if (!D || !isa<VarDecl>(Val: D))
24569 return;
24570 auto *VD = cast<VarDecl>(Val: D);
24571 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
24572 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
24573 if (SemaRef.LangOpts.OpenMP >= 50 &&
24574 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
24575 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
24576 VD->hasGlobalStorage()) {
24577 if (!MapTy || (*MapTy != OMPDeclareTargetDeclAttr::MT_To &&
24578 *MapTy != OMPDeclareTargetDeclAttr::MT_Enter &&
24579 *MapTy != OMPDeclareTargetDeclAttr::MT_Local)) {
24580 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
24581 // If a lambda declaration and definition appears between a
24582 // declare target directive and the matching end declare target
24583 // directive, all variables that are captured by the lambda
24584 // expression must also appear in a to clause.
24585 SemaRef.Diag(Loc: VD->getLocation(),
24586 DiagID: diag::err_omp_lambda_capture_in_declare_target_not_to);
24587 SemaRef.Diag(Loc: SL, DiagID: diag::note_var_explicitly_captured_here)
24588 << VD << 0 << SR;
24589 return;
24590 }
24591 }
24592 if (MapTy)
24593 return;
24594 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::warn_omp_not_in_target_context);
24595 SemaRef.Diag(Loc: SL, DiagID: diag::note_used_here) << SR;
24596}
24597
24598static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
24599 Sema &SemaRef, DSAStackTy *Stack,
24600 ValueDecl *VD) {
24601 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
24602 checkTypeMappable(SL, SR, SemaRef, Stack, QTy: VD->getType(),
24603 /*FullCheck=*/false);
24604}
24605
24606void SemaOpenMP::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
24607 SourceLocation IdLoc) {
24608 if (!D || D->isInvalidDecl())
24609 return;
24610 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
24611 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
24612 if (auto *VD = dyn_cast<VarDecl>(Val: D)) {
24613 // Only global variables can be marked as declare target.
24614 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
24615 !VD->isStaticDataMember())
24616 return;
24617 // 2.10.6: threadprivate variable cannot appear in a declare target
24618 // directive.
24619 if (DSAStack->isThreadPrivate(D: VD)) {
24620 Diag(Loc: SL, DiagID: diag::err_omp_threadprivate_in_target);
24621 reportOriginalDsa(SemaRef, DSAStack, D: VD, DSAStack->getTopDSA(D: VD, FromParent: false));
24622 return;
24623 }
24624 }
24625 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: D))
24626 D = FTD->getTemplatedDecl();
24627 if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
24628 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
24629 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: FD);
24630 if (IdLoc.isValid() && Res &&
24631 (*Res == OMPDeclareTargetDeclAttr::MT_Link ||
24632 *Res == OMPDeclareTargetDeclAttr::MT_Local)) {
24633 Diag(Loc: IdLoc, DiagID: diag::err_omp_function_in_target_clause_list)
24634 << OMPDeclareTargetDeclAttr::ConvertMapTypeTyToStr(Val: *Res);
24635 Diag(Loc: FD->getLocation(), DiagID: diag::note_defined_here) << FD;
24636 return;
24637 }
24638 }
24639 if (auto *VD = dyn_cast<ValueDecl>(Val: D)) {
24640 // Problem if any with var declared with incomplete type will be reported
24641 // as normal, so no need to check it here.
24642 if ((E || !VD->getType()->isIncompleteType()) &&
24643 !checkValueDeclInTarget(SL, SR, SemaRef, DSAStack, VD))
24644 return;
24645 if (!E && isInOpenMPDeclareTargetContext()) {
24646 // Checking declaration inside declare target region.
24647 if (isa<VarDecl>(Val: D) || isa<FunctionDecl>(Val: D) ||
24648 isa<FunctionTemplateDecl>(Val: D)) {
24649 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
24650 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
24651 unsigned Level = DeclareTargetNesting.size();
24652 if (ActiveAttr && (*ActiveAttr)->getLevel() >= Level)
24653 return;
24654 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
24655 Expr *IndirectE = nullptr;
24656 bool IsIndirect = false;
24657 if (DTCI.Indirect) {
24658 IndirectE = *DTCI.Indirect;
24659 if (!IndirectE)
24660 IsIndirect = true;
24661 }
24662 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
24663 Ctx&: getASTContext(),
24664 MapType: getLangOpts().OpenMP >= 52 ? OMPDeclareTargetDeclAttr::MT_Enter
24665 : OMPDeclareTargetDeclAttr::MT_To,
24666 DevType: DTCI.DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
24667 Range: SourceRange(DTCI.Loc, DTCI.Loc));
24668 D->addAttr(A);
24669 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
24670 ML->DeclarationMarkedOpenMPDeclareTarget(D, Attr: A);
24671 }
24672 return;
24673 }
24674 }
24675 if (!E)
24676 return;
24677 checkDeclInTargetContext(SL: E->getExprLoc(), SR: E->getSourceRange(), SemaRef, D);
24678}
24679
24680/// This class visits every VarDecl that the initializer references and adds
24681/// OMPDeclareTargetDeclAttr to each of them.
24682class GlobalDeclRefChecker final : public StmtVisitor<GlobalDeclRefChecker> {
24683 SmallVector<VarDecl *> DeclVector;
24684 Attr *A;
24685
24686public:
24687 /// A StmtVisitor class function that visits all DeclRefExpr and adds
24688 /// OMPDeclareTargetDeclAttr to them.
24689 void VisitDeclRefExpr(DeclRefExpr *Node) {
24690 if (auto *VD = dyn_cast<VarDecl>(Val: Node->getDecl())) {
24691 VD->addAttr(A);
24692 DeclVector.push_back(Elt: VD);
24693 }
24694 }
24695 /// A function that iterates across each of the Expr's children.
24696 void VisitExpr(Expr *Ex) {
24697 for (auto *Child : Ex->children()) {
24698 Visit(S: Child);
24699 }
24700 }
24701 /// A function that keeps a record of all the Decls that are variables, has
24702 /// OMPDeclareTargetDeclAttr, and has global storage in the DeclVector. Pop
24703 /// each Decl one at a time and use the inherited 'visit' functions to look
24704 /// for DeclRefExpr.
24705 void declareTargetInitializer(Decl *TD) {
24706 A = TD->getAttr<OMPDeclareTargetDeclAttr>();
24707 DeclVector.push_back(Elt: cast<VarDecl>(Val: TD));
24708 llvm::SmallDenseSet<Decl *> Visited;
24709 while (!DeclVector.empty()) {
24710 VarDecl *TargetVarDecl = DeclVector.pop_back_val();
24711 if (!Visited.insert(V: TargetVarDecl).second)
24712 continue;
24713
24714 if (TargetVarDecl->hasAttr<OMPDeclareTargetDeclAttr>() &&
24715 TargetVarDecl->hasInit() && TargetVarDecl->hasGlobalStorage()) {
24716 if (Expr *Ex = TargetVarDecl->getInit())
24717 Visit(S: Ex);
24718 }
24719 }
24720 }
24721};
24722
24723/// Adding OMPDeclareTargetDeclAttr to variables with static storage
24724/// duration that are referenced in the initializer expression list of
24725/// variables with static storage duration in declare target directive.
24726void SemaOpenMP::ActOnOpenMPDeclareTargetInitializer(Decl *TargetDecl) {
24727 GlobalDeclRefChecker Checker;
24728 if (isa<VarDecl>(Val: TargetDecl))
24729 Checker.declareTargetInitializer(TD: TargetDecl);
24730}
24731
24732OMPClause *SemaOpenMP::ActOnOpenMPToClause(
24733 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
24734 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
24735 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
24736 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
24737 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
24738 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
24739 OMPC_MOTION_MODIFIER_unknown,
24740 OMPC_MOTION_MODIFIER_unknown};
24741 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
24742
24743 // Process motion-modifiers, flag errors for duplicate modifiers.
24744 unsigned Count = 0;
24745 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
24746 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
24747 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
24748 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
24749 continue;
24750 }
24751 assert(Count < NumberOfOMPMotionModifiers &&
24752 "Modifiers exceed the allowed number of motion modifiers");
24753 Modifiers[Count] = MotionModifiers[I];
24754 ModifiersLoc[Count] = MotionModifiersLoc[I];
24755 ++Count;
24756 }
24757
24758 MappableVarListInfo MVLI(VarList);
24759 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_to, MVLI, StartLoc: Locs.StartLoc,
24760 MapperIdScopeSpec, MapperId, UnresolvedMappers);
24761 if (MVLI.ProcessedVarList.empty())
24762 return nullptr;
24763 if (IteratorExpr)
24764 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
24765 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
24766 DSAStack->addIteratorVarDecl(VD);
24767 return OMPToClause::Create(
24768 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24769 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier: IteratorExpr, MotionModifiers: Modifiers,
24770 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
24771 MapperId);
24772}
24773
24774OMPClause *SemaOpenMP::ActOnOpenMPFromClause(
24775 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
24776 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
24777 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
24778 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
24779 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
24780 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
24781 OMPC_MOTION_MODIFIER_unknown,
24782 OMPC_MOTION_MODIFIER_unknown};
24783 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
24784
24785 // Process motion-modifiers, flag errors for duplicate modifiers.
24786 unsigned Count = 0;
24787 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
24788 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
24789 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
24790 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
24791 continue;
24792 }
24793 assert(Count < NumberOfOMPMotionModifiers &&
24794 "Modifiers exceed the allowed number of motion modifiers");
24795 Modifiers[Count] = MotionModifiers[I];
24796 ModifiersLoc[Count] = MotionModifiersLoc[I];
24797 ++Count;
24798 }
24799
24800 MappableVarListInfo MVLI(VarList);
24801 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_from, MVLI, StartLoc: Locs.StartLoc,
24802 MapperIdScopeSpec, MapperId, UnresolvedMappers);
24803 if (MVLI.ProcessedVarList.empty())
24804 return nullptr;
24805 if (IteratorExpr)
24806 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
24807 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
24808 DSAStack->addIteratorVarDecl(VD);
24809 return OMPFromClause::Create(
24810 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24811 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorExpr, MotionModifiers: Modifiers,
24812 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
24813 MapperId);
24814}
24815
24816OMPClause *SemaOpenMP::ActOnOpenMPUseDevicePtrClause(
24817 ArrayRef<Expr *> VarList, const OMPVarListLocTy &Locs,
24818 OpenMPUseDevicePtrFallbackModifier FallbackModifier,
24819 SourceLocation FallbackModifierLoc) {
24820 MappableVarListInfo MVLI(VarList);
24821 SmallVector<Expr *, 8> PrivateCopies;
24822 SmallVector<Expr *, 8> Inits;
24823
24824 for (Expr *RefExpr : VarList) {
24825 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
24826 SourceLocation ELoc;
24827 SourceRange ERange;
24828 Expr *SimpleRefExpr = RefExpr;
24829 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
24830 if (Res.second) {
24831 // It will be analyzed later.
24832 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24833 PrivateCopies.push_back(Elt: nullptr);
24834 Inits.push_back(Elt: nullptr);
24835 }
24836 ValueDecl *D = Res.first;
24837 if (!D)
24838 continue;
24839
24840 QualType Type = D->getType();
24841 Type = Type.getNonReferenceType().getUnqualifiedType();
24842
24843 auto *VD = dyn_cast<VarDecl>(Val: D);
24844
24845 // Item should be a pointer or reference to pointer.
24846 if (!Type->isPointerType()) {
24847 Diag(Loc: ELoc, DiagID: diag::err_omp_usedeviceptr_not_a_pointer)
24848 << 0 << RefExpr->getSourceRange();
24849 continue;
24850 }
24851
24852 // Build the private variable and the expression that refers to it.
24853 auto VDPrivate =
24854 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
24855 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
24856 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
24857 if (VDPrivate->isInvalidDecl())
24858 continue;
24859
24860 SemaRef.CurContext->addDecl(D: VDPrivate);
24861 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
24862 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
24863
24864 // Add temporary variable to initialize the private copy of the pointer.
24865 VarDecl *VDInit =
24866 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type, Name: ".devptr.temp");
24867 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
24868 S&: SemaRef, D: VDInit, Ty: RefExpr->getType(), Loc: RefExpr->getExprLoc());
24869 SemaRef.AddInitializerToDecl(
24870 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
24871 /*DirectInit=*/false);
24872
24873 // If required, build a capture to implement the privatization initialized
24874 // with the current list item value.
24875 DeclRefExpr *Ref = nullptr;
24876 if (!VD)
24877 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
24878 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
24879 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
24880 Inits.push_back(Elt: VDInitRefExpr);
24881
24882 // We need to add a data sharing attribute for this variable to make sure it
24883 // is correctly captured. A variable that shows up in a use_device_ptr has
24884 // similar properties of a first private variable.
24885 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
24886
24887 // Create a mappable component for the list item. List items in this clause
24888 // only need a component.
24889 MVLI.VarBaseDeclarations.push_back(Elt: D);
24890 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
24891 MVLI.VarComponents.back().emplace_back(Args&: SimpleRefExpr, Args&: D,
24892 /*IsNonContiguous=*/Args: false);
24893 }
24894
24895 if (MVLI.ProcessedVarList.empty())
24896 return nullptr;
24897
24898 return OMPUseDevicePtrClause::Create(
24899 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, PrivateVars: PrivateCopies, Inits,
24900 Declarations: MVLI.VarBaseDeclarations, ComponentLists: MVLI.VarComponents, FallbackModifier,
24901 FallbackModifierLoc);
24902}
24903
24904OMPClause *
24905SemaOpenMP::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
24906 const OMPVarListLocTy &Locs) {
24907 MappableVarListInfo MVLI(VarList);
24908
24909 for (Expr *RefExpr : VarList) {
24910 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause.");
24911 SourceLocation ELoc;
24912 SourceRange ERange;
24913 Expr *SimpleRefExpr = RefExpr;
24914 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
24915 /*AllowArraySection=*/true,
24916 /*AllowAssumedSizeArray=*/true);
24917 if (Res.second) {
24918 // It will be analyzed later.
24919 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24920 }
24921 ValueDecl *D = Res.first;
24922 if (!D)
24923 continue;
24924 auto *VD = dyn_cast<VarDecl>(Val: D);
24925
24926 // If required, build a capture to implement the privatization initialized
24927 // with the current list item value.
24928 DeclRefExpr *Ref = nullptr;
24929 if (!VD)
24930 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
24931 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
24932
24933 // We need to add a data sharing attribute for this variable to make sure it
24934 // is correctly captured. A variable that shows up in a use_device_addr has
24935 // similar properties of a first private variable.
24936 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
24937
24938 // Use the map-like approach to fully populate VarComponents
24939 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
24940
24941 const Expr *BE = checkMapClauseExpressionBase(
24942 SemaRef, E: RefExpr, CurComponents, CKind: OMPC_use_device_addr,
24943 DSAStack->getCurrentDirective(),
24944 /*NoDiagnose=*/false);
24945
24946 if (!BE)
24947 continue;
24948
24949 assert(!CurComponents.empty() &&
24950 "use_device_addr clause expression with no components!");
24951
24952 // OpenMP use_device_addr: If a list item is an array section, the array
24953 // base must be a base language identifier. We caught the cases where
24954 // the array-section has a base-variable in getPrivateItem. e.g.
24955 // struct S {
24956 // int a[10];
24957 // }; S s1;
24958 // ... use_device_addr(s1.a[0]) // not ok, caught already
24959 //
24960 // But we still neeed to verify that the base-pointer is also a
24961 // base-language identifier, and catch cases like:
24962 // int *pa[10]; *p;
24963 // ... use_device_addr(pa[1][2]) // not ok, base-pointer is pa[1]
24964 // ... use_device_addr(p[1]) // ok
24965 // ... use_device_addr(this->p[1]) // ok
24966 auto AttachPtrResult = OMPClauseMappableExprCommon::findAttachPtrExpr(
24967 Components: CurComponents, DSAStack->getCurrentDirective());
24968 const Expr *AttachPtrExpr = AttachPtrResult.first;
24969
24970 if (AttachPtrExpr) {
24971 const Expr *BaseExpr = AttachPtrExpr->IgnoreParenImpCasts();
24972 bool IsValidBase = false;
24973
24974 if (isa<DeclRefExpr>(Val: BaseExpr))
24975 IsValidBase = true;
24976 else if (const auto *ME = dyn_cast<MemberExpr>(Val: BaseExpr);
24977 ME && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
24978 IsValidBase = true;
24979
24980 if (!IsValidBase) {
24981 SemaRef.Diag(Loc: ELoc,
24982 DiagID: diag::err_omp_expected_base_pointer_var_name_member_expr)
24983 << (SemaRef.getCurrentThisType().isNull() ? 0 : 1)
24984 << AttachPtrExpr->getSourceRange();
24985 continue;
24986 }
24987 }
24988
24989 // Get the declaration from the components
24990 ValueDecl *CurDeclaration = CurComponents.back().getAssociatedDeclaration();
24991 assert((isa<CXXThisExpr>(BE) || CurDeclaration) &&
24992 "Unexpected null decl for use_device_addr clause.");
24993
24994 MVLI.VarBaseDeclarations.push_back(Elt: CurDeclaration);
24995 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
24996 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
24997 in_end: CurComponents.end());
24998 }
24999
25000 if (MVLI.ProcessedVarList.empty())
25001 return nullptr;
25002
25003 return OMPUseDeviceAddrClause::Create(
25004 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
25005 ComponentLists: MVLI.VarComponents);
25006}
25007
25008OMPClause *
25009SemaOpenMP::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
25010 const OMPVarListLocTy &Locs) {
25011 MappableVarListInfo MVLI(VarList);
25012 for (Expr *RefExpr : VarList) {
25013 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
25014 SourceLocation ELoc;
25015 SourceRange ERange;
25016 Expr *SimpleRefExpr = RefExpr;
25017 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
25018 if (Res.second) {
25019 // It will be analyzed later.
25020 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
25021 }
25022 ValueDecl *D = Res.first;
25023 if (!D)
25024 continue;
25025
25026 QualType Type = D->getType();
25027 // item should be a pointer or array or reference to pointer or array
25028 if (!Type.getNonReferenceType()->isPointerType() &&
25029 !Type.getNonReferenceType()->isArrayType()) {
25030 Diag(Loc: ELoc, DiagID: diag::err_omp_argument_type_isdeviceptr)
25031 << 0 << RefExpr->getSourceRange();
25032 continue;
25033 }
25034
25035 // Check if the declaration in the clause does not show up in any data
25036 // sharing attribute.
25037 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
25038 if (isOpenMPPrivate(Kind: DVar.CKind)) {
25039 unsigned OMPVersion = getLangOpts().OpenMP;
25040 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
25041 << getOpenMPClauseNameForDiag(C: DVar.CKind)
25042 << getOpenMPClauseNameForDiag(C: OMPC_is_device_ptr)
25043 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
25044 Ver: OMPVersion);
25045 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
25046 continue;
25047 }
25048
25049 const Expr *ConflictExpr;
25050 if (DSAStack->checkMappableExprComponentListsForDecl(
25051 VD: D, /*CurrentRegionOnly=*/true,
25052 Check: [&ConflictExpr](
25053 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
25054 OpenMPClauseKind) -> bool {
25055 ConflictExpr = R.front().getAssociatedExpression();
25056 return true;
25057 })) {
25058 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
25059 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
25060 << ConflictExpr->getSourceRange();
25061 continue;
25062 }
25063
25064 // Store the components in the stack so that they can be used to check
25065 // against other clauses later on.
25066 OMPClauseMappableExprCommon::MappableComponent MC(
25067 SimpleRefExpr, D, /*IsNonContiguous=*/false);
25068 DSAStack->addMappableExpressionComponents(
25069 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
25070
25071 // Record the expression we've just processed.
25072 MVLI.ProcessedVarList.push_back(Elt: SimpleRefExpr);
25073
25074 // Create a mappable component for the list item. List items in this clause
25075 // only need a component. We use a null declaration to signal fields in
25076 // 'this'.
25077 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
25078 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
25079 "Unexpected device pointer expression!");
25080 MVLI.VarBaseDeclarations.push_back(
25081 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
25082 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
25083 MVLI.VarComponents.back().push_back(Elt: MC);
25084 }
25085
25086 if (MVLI.ProcessedVarList.empty())
25087 return nullptr;
25088
25089 return OMPIsDevicePtrClause::Create(
25090 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
25091 ComponentLists: MVLI.VarComponents);
25092}
25093
25094OMPClause *
25095SemaOpenMP::ActOnOpenMPHasDeviceAddrClause(ArrayRef<Expr *> VarList,
25096 const OMPVarListLocTy &Locs) {
25097 MappableVarListInfo MVLI(VarList);
25098 for (Expr *RefExpr : VarList) {
25099 assert(RefExpr && "NULL expr in OpenMP has_device_addr clause.");
25100 SourceLocation ELoc;
25101 SourceRange ERange;
25102 Expr *SimpleRefExpr = RefExpr;
25103 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25104 /*AllowArraySection=*/true);
25105 if (Res.second) {
25106 // It will be analyzed later.
25107 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
25108 }
25109 ValueDecl *D = Res.first;
25110 if (!D)
25111 continue;
25112
25113 // Check if the declaration in the clause does not show up in any data
25114 // sharing attribute.
25115 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
25116 if (isOpenMPPrivate(Kind: DVar.CKind)) {
25117 unsigned OMPVersion = getLangOpts().OpenMP;
25118 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
25119 << getOpenMPClauseNameForDiag(C: DVar.CKind)
25120 << getOpenMPClauseNameForDiag(C: OMPC_has_device_addr)
25121 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
25122 Ver: OMPVersion);
25123 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
25124 continue;
25125 }
25126
25127 const Expr *ConflictExpr;
25128 if (DSAStack->checkMappableExprComponentListsForDecl(
25129 VD: D, /*CurrentRegionOnly=*/true,
25130 Check: [&ConflictExpr](
25131 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
25132 OpenMPClauseKind) -> bool {
25133 ConflictExpr = R.front().getAssociatedExpression();
25134 return true;
25135 })) {
25136 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
25137 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
25138 << ConflictExpr->getSourceRange();
25139 continue;
25140 }
25141
25142 // Store the components in the stack so that they can be used to check
25143 // against other clauses later on.
25144 Expr *Component = SimpleRefExpr;
25145 auto *VD = dyn_cast<VarDecl>(Val: D);
25146 if (VD && (isa<ArraySectionExpr>(Val: RefExpr->IgnoreParenImpCasts()) ||
25147 isa<ArraySubscriptExpr>(Val: RefExpr->IgnoreParenImpCasts())))
25148 Component =
25149 SemaRef.DefaultFunctionArrayLvalueConversion(E: SimpleRefExpr).get();
25150 OMPClauseMappableExprCommon::MappableComponent MC(
25151 Component, D, /*IsNonContiguous=*/false);
25152 DSAStack->addMappableExpressionComponents(
25153 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_has_device_addr);
25154
25155 // Record the expression we've just processed.
25156 if (!VD && !SemaRef.CurContext->isDependentContext()) {
25157 DeclRefExpr *Ref =
25158 buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
25159 assert(Ref && "has_device_addr capture failed");
25160 MVLI.ProcessedVarList.push_back(Elt: Ref);
25161 } else
25162 MVLI.ProcessedVarList.push_back(Elt: RefExpr->IgnoreParens());
25163
25164 // Create a mappable component for the list item. List items in this clause
25165 // only need a component. We use a null declaration to signal fields in
25166 // 'this'.
25167 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
25168 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
25169 "Unexpected device pointer expression!");
25170 MVLI.VarBaseDeclarations.push_back(
25171 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
25172 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
25173 MVLI.VarComponents.back().push_back(Elt: MC);
25174 }
25175
25176 if (MVLI.ProcessedVarList.empty())
25177 return nullptr;
25178
25179 return OMPHasDeviceAddrClause::Create(
25180 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
25181 ComponentLists: MVLI.VarComponents);
25182}
25183
25184OMPClause *SemaOpenMP::ActOnOpenMPAllocateClause(
25185 Expr *Allocator, Expr *Alignment,
25186 OpenMPAllocateClauseModifier FirstAllocateModifier,
25187 SourceLocation FirstAllocateModifierLoc,
25188 OpenMPAllocateClauseModifier SecondAllocateModifier,
25189 SourceLocation SecondAllocateModifierLoc, ArrayRef<Expr *> VarList,
25190 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
25191 SourceLocation EndLoc) {
25192 if (Allocator) {
25193 // Allocator expression is dependent - skip it for now and build the
25194 // allocator when instantiated.
25195 bool AllocDependent =
25196 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
25197 Allocator->isInstantiationDependent() ||
25198 Allocator->containsUnexpandedParameterPack());
25199 if (!AllocDependent) {
25200 // OpenMP [2.11.4 allocate Clause, Description]
25201 // allocator is an expression of omp_allocator_handle_t type.
25202 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: Allocator->getExprLoc(), DSAStack))
25203 return nullptr;
25204
25205 ExprResult AllocatorRes = SemaRef.DefaultLvalueConversion(E: Allocator);
25206 if (AllocatorRes.isInvalid())
25207 return nullptr;
25208 AllocatorRes = SemaRef.PerformImplicitConversion(
25209 From: AllocatorRes.get(), DSAStack->getOMPAllocatorHandleT(),
25210 Action: AssignmentAction::Initializing,
25211 /*AllowExplicit=*/true);
25212 if (AllocatorRes.isInvalid())
25213 return nullptr;
25214 Allocator = AllocatorRes.isUsable() ? AllocatorRes.get() : nullptr;
25215 }
25216 } else {
25217 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
25218 // allocate clauses that appear on a target construct or on constructs in a
25219 // target region must specify an allocator expression unless a requires
25220 // directive with the dynamic_allocators clause is present in the same
25221 // compilation unit.
25222 if (getLangOpts().OpenMPIsTargetDevice &&
25223 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
25224 SemaRef.targetDiag(Loc: StartLoc, DiagID: diag::err_expected_allocator_expression);
25225 }
25226 if (Alignment) {
25227 bool AlignmentDependent = Alignment->isTypeDependent() ||
25228 Alignment->isValueDependent() ||
25229 Alignment->isInstantiationDependent() ||
25230 Alignment->containsUnexpandedParameterPack();
25231 if (!AlignmentDependent) {
25232 ExprResult AlignResult =
25233 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_allocate);
25234 Alignment = AlignResult.isUsable() ? AlignResult.get() : nullptr;
25235 }
25236 }
25237 // Analyze and build list of variables.
25238 SmallVector<Expr *, 8> Vars;
25239 for (Expr *RefExpr : VarList) {
25240 assert(RefExpr && "NULL expr in OpenMP allocate clause.");
25241 SourceLocation ELoc;
25242 SourceRange ERange;
25243 Expr *SimpleRefExpr = RefExpr;
25244 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
25245 if (Res.second) {
25246 // It will be analyzed later.
25247 Vars.push_back(Elt: RefExpr);
25248 }
25249 ValueDecl *D = Res.first;
25250 if (!D)
25251 continue;
25252
25253 auto *VD = dyn_cast<VarDecl>(Val: D);
25254 DeclRefExpr *Ref = nullptr;
25255 if (!VD && !SemaRef.CurContext->isDependentContext())
25256 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
25257 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
25258 ? RefExpr->IgnoreParens()
25259 : Ref);
25260 }
25261
25262 if (Vars.empty())
25263 return nullptr;
25264
25265 if (Allocator)
25266 DSAStack->addInnerAllocatorExpr(E: Allocator);
25267
25268 return OMPAllocateClause::Create(
25269 C: getASTContext(), StartLoc, LParenLoc, Allocator, Alignment, ColonLoc,
25270 Modifier1: FirstAllocateModifier, Modifier1Loc: FirstAllocateModifierLoc, Modifier2: SecondAllocateModifier,
25271 Modifier2Loc: SecondAllocateModifierLoc, EndLoc, VL: Vars);
25272}
25273
25274OMPClause *SemaOpenMP::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList,
25275 SourceLocation StartLoc,
25276 SourceLocation LParenLoc,
25277 SourceLocation EndLoc) {
25278 SmallVector<Expr *, 8> Vars;
25279 for (Expr *RefExpr : VarList) {
25280 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
25281 SourceLocation ELoc;
25282 SourceRange ERange;
25283 Expr *SimpleRefExpr = RefExpr;
25284 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
25285 if (Res.second)
25286 // It will be analyzed later.
25287 Vars.push_back(Elt: RefExpr);
25288 ValueDecl *D = Res.first;
25289 if (!D)
25290 continue;
25291
25292 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions.
25293 // A list-item cannot appear in more than one nontemporal clause.
25294 if (const Expr *PrevRef =
25295 DSAStack->addUniqueNontemporal(D, NewDE: SimpleRefExpr)) {
25296 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
25297 << 0 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal) << ERange;
25298 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
25299 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal);
25300 continue;
25301 }
25302
25303 Vars.push_back(Elt: RefExpr);
25304 }
25305
25306 if (Vars.empty())
25307 return nullptr;
25308
25309 return OMPNontemporalClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25310 EndLoc, VL: Vars);
25311}
25312
25313StmtResult SemaOpenMP::ActOnOpenMPScopeDirective(ArrayRef<OMPClause *> Clauses,
25314 Stmt *AStmt,
25315 SourceLocation StartLoc,
25316 SourceLocation EndLoc) {
25317 if (!AStmt)
25318 return StmtError();
25319
25320 SemaRef.setFunctionHasBranchProtectedScope();
25321
25322 return OMPScopeDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
25323 AssociatedStmt: AStmt);
25324}
25325
25326OMPClause *SemaOpenMP::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
25327 SourceLocation StartLoc,
25328 SourceLocation LParenLoc,
25329 SourceLocation EndLoc) {
25330 SmallVector<Expr *, 8> Vars;
25331 for (Expr *RefExpr : VarList) {
25332 assert(RefExpr && "NULL expr in OpenMP inclusive clause.");
25333 SourceLocation ELoc;
25334 SourceRange ERange;
25335 Expr *SimpleRefExpr = RefExpr;
25336 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25337 /*AllowArraySection=*/true);
25338 if (Res.second)
25339 // It will be analyzed later.
25340 Vars.push_back(Elt: RefExpr);
25341 ValueDecl *D = Res.first;
25342 if (!D)
25343 continue;
25344
25345 const DSAStackTy::DSAVarData DVar =
25346 DSAStack->getTopDSA(D, /*FromParent=*/true);
25347 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
25348 // A list item that appears in the inclusive or exclusive clause must appear
25349 // in a reduction clause with the inscan modifier on the enclosing
25350 // worksharing-loop, worksharing-loop SIMD, or simd construct.
25351 if (DVar.CKind != OMPC_reduction || DVar.Modifier != OMPC_REDUCTION_inscan)
25352 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
25353 << RefExpr->getSourceRange();
25354
25355 if (DSAStack->getParentDirective() != OMPD_unknown)
25356 DSAStack->markDeclAsUsedInScanDirective(D);
25357 Vars.push_back(Elt: RefExpr);
25358 }
25359
25360 if (Vars.empty())
25361 return nullptr;
25362
25363 return OMPInclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25364 EndLoc, VL: Vars);
25365}
25366
25367OMPClause *SemaOpenMP::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
25368 SourceLocation StartLoc,
25369 SourceLocation LParenLoc,
25370 SourceLocation EndLoc) {
25371 SmallVector<Expr *, 8> Vars;
25372 for (Expr *RefExpr : VarList) {
25373 assert(RefExpr && "NULL expr in OpenMP exclusive clause.");
25374 SourceLocation ELoc;
25375 SourceRange ERange;
25376 Expr *SimpleRefExpr = RefExpr;
25377 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25378 /*AllowArraySection=*/true);
25379 if (Res.second)
25380 // It will be analyzed later.
25381 Vars.push_back(Elt: RefExpr);
25382 ValueDecl *D = Res.first;
25383 if (!D)
25384 continue;
25385
25386 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
25387 DSAStackTy::DSAVarData DVar;
25388 if (ParentDirective != OMPD_unknown)
25389 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
25390 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
25391 // A list item that appears in the inclusive or exclusive clause must appear
25392 // in a reduction clause with the inscan modifier on the enclosing
25393 // worksharing-loop, worksharing-loop SIMD, or simd construct.
25394 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
25395 DVar.Modifier != OMPC_REDUCTION_inscan) {
25396 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
25397 << RefExpr->getSourceRange();
25398 } else {
25399 DSAStack->markDeclAsUsedInScanDirective(D);
25400 }
25401 Vars.push_back(Elt: RefExpr);
25402 }
25403
25404 if (Vars.empty())
25405 return nullptr;
25406
25407 return OMPExclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25408 EndLoc, VL: Vars);
25409}
25410
25411/// Tries to find omp_alloctrait_t type.
25412static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) {
25413 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT();
25414 if (!OMPAlloctraitT.isNull())
25415 return true;
25416 IdentifierInfo &II = S.PP.getIdentifierTable().get(Name: "omp_alloctrait_t");
25417 ParsedType PT = S.getTypeName(II, NameLoc: Loc, S: S.getCurScope());
25418 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
25419 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_alloctrait_t";
25420 return false;
25421 }
25422 Stack->setOMPAlloctraitT(PT.get());
25423 return true;
25424}
25425
25426OMPClause *SemaOpenMP::ActOnOpenMPUsesAllocatorClause(
25427 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
25428 ArrayRef<UsesAllocatorsData> Data) {
25429 ASTContext &Context = getASTContext();
25430 // OpenMP [2.12.5, target Construct]
25431 // allocator is an identifier of omp_allocator_handle_t type.
25432 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: StartLoc, DSAStack))
25433 return nullptr;
25434 // OpenMP [2.12.5, target Construct]
25435 // allocator-traits-array is an identifier of const omp_alloctrait_t * type.
25436 if (llvm::any_of(
25437 Range&: Data,
25438 P: [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) &&
25439 !findOMPAlloctraitT(S&: SemaRef, Loc: StartLoc, DSAStack))
25440 return nullptr;
25441 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators;
25442 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
25443 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
25444 StringRef Allocator =
25445 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
25446 DeclarationName AllocatorName = &Context.Idents.get(Name: Allocator);
25447 PredefinedAllocators.insert(Ptr: SemaRef.LookupSingleName(
25448 S: SemaRef.TUScope, Name: AllocatorName, Loc: StartLoc, NameKind: Sema::LookupAnyName));
25449 }
25450
25451 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData;
25452 for (const UsesAllocatorsData &D : Data) {
25453 Expr *AllocatorExpr = nullptr;
25454 // Check allocator expression.
25455 if (D.Allocator->isTypeDependent()) {
25456 AllocatorExpr = D.Allocator;
25457 } else {
25458 // Traits were specified - need to assign new allocator to the specified
25459 // allocator, so it must be an lvalue.
25460 AllocatorExpr = D.Allocator->IgnoreParenImpCasts();
25461 auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorExpr);
25462 bool IsPredefinedAllocator = false;
25463 if (DRE) {
25464 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorTy =
25465 getAllocatorKind(S&: SemaRef, DSAStack, Allocator: AllocatorExpr);
25466 IsPredefinedAllocator =
25467 AllocatorTy !=
25468 OMPAllocateDeclAttr::AllocatorTypeTy::OMPUserDefinedMemAlloc;
25469 }
25470 QualType OMPAllocatorHandleT = DSAStack->getOMPAllocatorHandleT();
25471 QualType AllocatorExprType = AllocatorExpr->getType();
25472 bool IsTypeCompatible = IsPredefinedAllocator;
25473 IsTypeCompatible = IsTypeCompatible ||
25474 Context.hasSameUnqualifiedType(T1: AllocatorExprType,
25475 T2: OMPAllocatorHandleT);
25476 IsTypeCompatible =
25477 IsTypeCompatible ||
25478 Context.typesAreCompatible(T1: AllocatorExprType, T2: OMPAllocatorHandleT);
25479 bool IsNonConstantLValue =
25480 !AllocatorExprType.isConstant(Ctx: Context) && AllocatorExpr->isLValue();
25481 if (!DRE || !IsTypeCompatible ||
25482 (!IsPredefinedAllocator && !IsNonConstantLValue)) {
25483 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_var_expected)
25484 << "omp_allocator_handle_t" << (DRE ? 1 : 0)
25485 << AllocatorExpr->getType() << D.Allocator->getSourceRange();
25486 continue;
25487 }
25488 // OpenMP [2.12.5, target Construct]
25489 // Predefined allocators appearing in a uses_allocators clause cannot have
25490 // traits specified.
25491 if (IsPredefinedAllocator && D.AllocatorTraits) {
25492 Diag(Loc: D.AllocatorTraits->getExprLoc(),
25493 DiagID: diag::err_omp_predefined_allocator_with_traits)
25494 << D.AllocatorTraits->getSourceRange();
25495 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::note_omp_predefined_allocator)
25496 << cast<NamedDecl>(Val: DRE->getDecl())->getName()
25497 << D.Allocator->getSourceRange();
25498 continue;
25499 }
25500 // OpenMP [2.12.5, target Construct]
25501 // Non-predefined allocators appearing in a uses_allocators clause must
25502 // have traits specified.
25503 if (getLangOpts().OpenMP < 52) {
25504 if (!IsPredefinedAllocator && !D.AllocatorTraits) {
25505 Diag(Loc: D.Allocator->getExprLoc(),
25506 DiagID: diag::err_omp_nonpredefined_allocator_without_traits);
25507 continue;
25508 }
25509 }
25510 // No allocator traits - just convert it to rvalue.
25511 if (!D.AllocatorTraits)
25512 AllocatorExpr = SemaRef.DefaultLvalueConversion(E: AllocatorExpr).get();
25513 DSAStack->addUsesAllocatorsDecl(
25514 D: DRE->getDecl(),
25515 Kind: IsPredefinedAllocator
25516 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator
25517 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator);
25518 }
25519 Expr *AllocatorTraitsExpr = nullptr;
25520 if (D.AllocatorTraits) {
25521 if (D.AllocatorTraits->isTypeDependent()) {
25522 AllocatorTraitsExpr = D.AllocatorTraits;
25523 } else {
25524 // OpenMP [2.12.5, target Construct]
25525 // Arrays that contain allocator traits that appear in a uses_allocators
25526 // clause must be constant arrays, have constant values and be defined
25527 // in the same scope as the construct in which the clause appears.
25528 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts();
25529 // Check that traits expr is a constant array.
25530 QualType TraitTy;
25531 if (const ArrayType *Ty =
25532 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe())
25533 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Val: Ty))
25534 TraitTy = ConstArrayTy->getElementType();
25535 if (TraitTy.isNull() ||
25536 !(Context.hasSameUnqualifiedType(T1: TraitTy,
25537 DSAStack->getOMPAlloctraitT()) ||
25538 Context.typesAreCompatible(T1: TraitTy, DSAStack->getOMPAlloctraitT(),
25539 /*CompareUnqualified=*/true))) {
25540 Diag(Loc: D.AllocatorTraits->getExprLoc(),
25541 DiagID: diag::err_omp_expected_array_alloctraits)
25542 << AllocatorTraitsExpr->getType();
25543 continue;
25544 }
25545 // Do not map by default allocator traits if it is a standalone
25546 // variable.
25547 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorTraitsExpr))
25548 DSAStack->addUsesAllocatorsDecl(
25549 D: DRE->getDecl(),
25550 Kind: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait);
25551 }
25552 }
25553 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back();
25554 NewD.Allocator = AllocatorExpr;
25555 NewD.AllocatorTraits = AllocatorTraitsExpr;
25556 NewD.LParenLoc = D.LParenLoc;
25557 NewD.RParenLoc = D.RParenLoc;
25558 }
25559 return OMPUsesAllocatorsClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25560 EndLoc, Data: NewData);
25561}
25562
25563OMPClause *SemaOpenMP::ActOnOpenMPAffinityClause(
25564 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
25565 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) {
25566 SmallVector<Expr *, 8> Vars;
25567 for (Expr *RefExpr : Locators) {
25568 assert(RefExpr && "NULL expr in OpenMP affinity clause.");
25569 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr) || RefExpr->isTypeDependent()) {
25570 // It will be analyzed later.
25571 Vars.push_back(Elt: RefExpr);
25572 continue;
25573 }
25574
25575 SourceLocation ELoc = RefExpr->getExprLoc();
25576 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts();
25577
25578 if (!SimpleExpr->isLValue()) {
25579 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
25580 << 1 << 0 << RefExpr->getSourceRange();
25581 continue;
25582 }
25583
25584 ExprResult Res;
25585 {
25586 Sema::TentativeAnalysisScope Trap(SemaRef);
25587 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: SimpleExpr);
25588 }
25589 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
25590 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
25591 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
25592 << 1 << 0 << RefExpr->getSourceRange();
25593 continue;
25594 }
25595 Vars.push_back(Elt: SimpleExpr);
25596 }
25597
25598 return OMPAffinityClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25599 ColonLoc, EndLoc, Modifier, Locators: Vars);
25600}
25601
25602OMPClause *SemaOpenMP::ActOnOpenMPBindClause(OpenMPBindClauseKind Kind,
25603 SourceLocation KindLoc,
25604 SourceLocation StartLoc,
25605 SourceLocation LParenLoc,
25606 SourceLocation EndLoc) {
25607 if (Kind == OMPC_BIND_unknown) {
25608 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
25609 << getListOfPossibleValues(K: OMPC_bind, /*First=*/0,
25610 /*Last=*/unsigned(OMPC_BIND_unknown))
25611 << getOpenMPClauseNameForDiag(C: OMPC_bind);
25612 return nullptr;
25613 }
25614
25615 return OMPBindClause::Create(C: getASTContext(), K: Kind, KLoc: KindLoc, StartLoc,
25616 LParenLoc, EndLoc);
25617}
25618
25619OMPClause *SemaOpenMP::ActOnOpenMPXDynCGroupMemClause(Expr *Size,
25620 SourceLocation StartLoc,
25621 SourceLocation LParenLoc,
25622 SourceLocation EndLoc) {
25623 Expr *ValExpr = Size;
25624 Stmt *HelperValStmt = nullptr;
25625
25626 // OpenMP [2.5, Restrictions]
25627 // The ompx_dyn_cgroup_mem expression must evaluate to a positive integer
25628 // value.
25629 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_ompx_dyn_cgroup_mem,
25630 /*StrictlyPositive=*/false))
25631 return nullptr;
25632
25633 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
25634 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
25635 DKind, CKind: OMPC_ompx_dyn_cgroup_mem, OpenMPVersion: getLangOpts().OpenMP);
25636 if (CaptureRegion != OMPD_unknown &&
25637 !SemaRef.CurContext->isDependentContext()) {
25638 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
25639 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
25640 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
25641 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
25642 }
25643
25644 return new (getASTContext()) OMPXDynCGroupMemClause(
25645 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
25646}
25647
25648OMPClause *SemaOpenMP::ActOnOpenMPDynGroupprivateClause(
25649 OpenMPDynGroupprivateClauseModifier M1,
25650 OpenMPDynGroupprivateClauseFallbackModifier M2, Expr *Size,
25651 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation M1Loc,
25652 SourceLocation M2Loc, SourceLocation EndLoc) {
25653
25654 if ((M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ||
25655 (M2Loc.isValid() && M2 == OMPC_DYN_GROUPPRIVATE_FALLBACK_unknown)) {
25656 std::string Values = getListOfPossibleValues(
25657 K: OMPC_dyn_groupprivate, /*First=*/0, Last: OMPC_DYN_GROUPPRIVATE_unknown);
25658 Diag(Loc: (M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ? M1Loc
25659 : M2Loc,
25660 DiagID: diag::err_omp_unexpected_clause_value)
25661 << Values << getOpenMPClauseName(C: OMPC_dyn_groupprivate);
25662 return nullptr;
25663 }
25664
25665 Expr *ValExpr = Size;
25666 Stmt *HelperValStmt = nullptr;
25667
25668 // OpenMP [2.5, Restrictions]
25669 // The dyn_groupprivate expression must evaluate to a positive integer
25670 // value.
25671 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_dyn_groupprivate,
25672 /*StrictlyPositive=*/false))
25673 return nullptr;
25674
25675 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
25676 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
25677 DKind, CKind: OMPC_dyn_groupprivate, OpenMPVersion: getLangOpts().OpenMP);
25678 if (CaptureRegion != OMPD_unknown &&
25679 !SemaRef.CurContext->isDependentContext()) {
25680 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
25681 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
25682 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
25683 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
25684 }
25685
25686 return new (getASTContext()) OMPDynGroupprivateClause(
25687 StartLoc, LParenLoc, EndLoc, ValExpr, HelperValStmt, CaptureRegion, M1,
25688 M1Loc, M2, M2Loc);
25689}
25690
25691OMPClause *SemaOpenMP::ActOnOpenMPDoacrossClause(
25692 OpenMPDoacrossClauseModifier DepType, SourceLocation DepLoc,
25693 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
25694 SourceLocation LParenLoc, SourceLocation EndLoc) {
25695
25696 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
25697 DepType != OMPC_DOACROSS_source && DepType != OMPC_DOACROSS_sink &&
25698 DepType != OMPC_DOACROSS_sink_omp_cur_iteration &&
25699 DepType != OMPC_DOACROSS_source_omp_cur_iteration) {
25700 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
25701 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_doacross);
25702 return nullptr;
25703 }
25704
25705 SmallVector<Expr *, 8> Vars;
25706 DSAStackTy::OperatorOffsetTy OpsOffs;
25707 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
25708 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
25709 SemaRef,
25710 IsSource: DepType == OMPC_DOACROSS_source ||
25711 DepType == OMPC_DOACROSS_source_omp_cur_iteration ||
25712 DepType == OMPC_DOACROSS_sink_omp_cur_iteration,
25713 VarList, DSAStack, EndLoc);
25714 Vars = VarOffset.Vars;
25715 OpsOffs = VarOffset.OpsOffs;
25716 TotalDepCount = VarOffset.TotalDepCount;
25717 auto *C = OMPDoacrossClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25718 EndLoc, DepType, DepLoc, ColonLoc, VL: Vars,
25719 NumLoops: TotalDepCount.getZExtValue());
25720 if (DSAStack->isParentOrderedRegion())
25721 DSAStack->addDoacrossDependClause(C, OpsOffs);
25722 return C;
25723}
25724
25725OMPClause *SemaOpenMP::ActOnOpenMPXAttributeClause(ArrayRef<const Attr *> Attrs,
25726 SourceLocation StartLoc,
25727 SourceLocation LParenLoc,
25728 SourceLocation EndLoc) {
25729 return new (getASTContext())
25730 OMPXAttributeClause(Attrs, StartLoc, LParenLoc, EndLoc);
25731}
25732
25733OMPClause *SemaOpenMP::ActOnOpenMPXBareClause(SourceLocation StartLoc,
25734 SourceLocation EndLoc) {
25735 return new (getASTContext()) OMPXBareClause(StartLoc, EndLoc);
25736}
25737
25738OMPClause *SemaOpenMP::ActOnOpenMPHoldsClause(Expr *E, SourceLocation StartLoc,
25739 SourceLocation LParenLoc,
25740 SourceLocation EndLoc) {
25741 return new (getASTContext()) OMPHoldsClause(E, StartLoc, LParenLoc, EndLoc);
25742}
25743
25744OMPClause *SemaOpenMP::ActOnOpenMPDirectivePresenceClause(
25745 OpenMPClauseKind CK, llvm::ArrayRef<OpenMPDirectiveKind> DKVec,
25746 SourceLocation Loc, SourceLocation LLoc, SourceLocation RLoc) {
25747 switch (CK) {
25748 case OMPC_absent:
25749 return OMPAbsentClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
25750 case OMPC_contains:
25751 return OMPContainsClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
25752 default:
25753 llvm_unreachable("Unexpected OpenMP clause");
25754 }
25755}
25756
25757OMPClause *SemaOpenMP::ActOnOpenMPNullaryAssumptionClause(OpenMPClauseKind CK,
25758 SourceLocation Loc,
25759 SourceLocation RLoc) {
25760 switch (CK) {
25761 case OMPC_no_openmp:
25762 return new (getASTContext()) OMPNoOpenMPClause(Loc, RLoc);
25763 case OMPC_no_openmp_routines:
25764 return new (getASTContext()) OMPNoOpenMPRoutinesClause(Loc, RLoc);
25765 case OMPC_no_parallelism:
25766 return new (getASTContext()) OMPNoParallelismClause(Loc, RLoc);
25767 case OMPC_no_openmp_constructs:
25768 return new (getASTContext()) OMPNoOpenMPConstructsClause(Loc, RLoc);
25769 default:
25770 llvm_unreachable("Unexpected OpenMP clause");
25771 }
25772}
25773
25774ExprResult SemaOpenMP::ActOnOMPArraySectionExpr(
25775 Expr *Base, SourceLocation LBLoc, Expr *LowerBound,
25776 SourceLocation ColonLocFirst, SourceLocation ColonLocSecond, Expr *Length,
25777 Expr *Stride, SourceLocation RBLoc) {
25778 ASTContext &Context = getASTContext();
25779 if (Base->hasPlaceholderType() &&
25780 !Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
25781 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
25782 if (Result.isInvalid())
25783 return ExprError();
25784 Base = Result.get();
25785 }
25786 if (LowerBound && LowerBound->getType()->isNonOverloadPlaceholderType()) {
25787 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: LowerBound);
25788 if (Result.isInvalid())
25789 return ExprError();
25790 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25791 if (Result.isInvalid())
25792 return ExprError();
25793 LowerBound = Result.get();
25794 }
25795 if (Length && Length->getType()->isNonOverloadPlaceholderType()) {
25796 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Length);
25797 if (Result.isInvalid())
25798 return ExprError();
25799 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25800 if (Result.isInvalid())
25801 return ExprError();
25802 Length = Result.get();
25803 }
25804 if (Stride && Stride->getType()->isNonOverloadPlaceholderType()) {
25805 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Stride);
25806 if (Result.isInvalid())
25807 return ExprError();
25808 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25809 if (Result.isInvalid())
25810 return ExprError();
25811 Stride = Result.get();
25812 }
25813
25814 // Build an unanalyzed expression if either operand is type-dependent.
25815 if (Base->isTypeDependent() ||
25816 (LowerBound &&
25817 (LowerBound->isTypeDependent() || LowerBound->isValueDependent())) ||
25818 (Length && (Length->isTypeDependent() || Length->isValueDependent())) ||
25819 (Stride && (Stride->isTypeDependent() || Stride->isValueDependent()))) {
25820 return new (Context) ArraySectionExpr(
25821 Base, LowerBound, Length, Stride, Context.DependentTy, VK_LValue,
25822 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
25823 }
25824
25825 // Perform default conversions.
25826 QualType OriginalTy = ArraySectionExpr::getBaseOriginalType(Base);
25827 QualType ResultTy;
25828 if (OriginalTy->isAnyPointerType()) {
25829 ResultTy = OriginalTy->getPointeeType();
25830 } else if (OriginalTy->isArrayType()) {
25831 ResultTy = OriginalTy->getAsArrayTypeUnsafe()->getElementType();
25832 } else {
25833 return ExprError(
25834 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_typecheck_section_value)
25835 << Base->getSourceRange());
25836 }
25837 // C99 6.5.2.1p1
25838 if (LowerBound) {
25839 auto Res = PerformOpenMPImplicitIntegerConversion(Loc: LowerBound->getExprLoc(),
25840 Op: LowerBound);
25841 if (Res.isInvalid())
25842 return ExprError(Diag(Loc: LowerBound->getExprLoc(),
25843 DiagID: diag::err_omp_typecheck_section_not_integer)
25844 << 0 << LowerBound->getSourceRange());
25845 LowerBound = Res.get();
25846
25847 if (LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25848 LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25849 Diag(Loc: LowerBound->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25850 << 0 << LowerBound->getSourceRange();
25851 }
25852 if (Length) {
25853 auto Res =
25854 PerformOpenMPImplicitIntegerConversion(Loc: Length->getExprLoc(), Op: Length);
25855 if (Res.isInvalid())
25856 return ExprError(Diag(Loc: Length->getExprLoc(),
25857 DiagID: diag::err_omp_typecheck_section_not_integer)
25858 << 1 << Length->getSourceRange());
25859 Length = Res.get();
25860
25861 if (Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25862 Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25863 Diag(Loc: Length->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25864 << 1 << Length->getSourceRange();
25865 }
25866 if (Stride) {
25867 ExprResult Res =
25868 PerformOpenMPImplicitIntegerConversion(Loc: Stride->getExprLoc(), Op: Stride);
25869 if (Res.isInvalid())
25870 return ExprError(Diag(Loc: Stride->getExprLoc(),
25871 DiagID: diag::err_omp_typecheck_section_not_integer)
25872 << 1 << Stride->getSourceRange());
25873 Stride = Res.get();
25874
25875 if (Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25876 Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25877 Diag(Loc: Stride->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25878 << 1 << Stride->getSourceRange();
25879 }
25880
25881 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
25882 // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
25883 // type. Note that functions are not objects, and that (in C99 parlance)
25884 // incomplete types are not object types.
25885 if (ResultTy->isFunctionType()) {
25886 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_section_function_type)
25887 << ResultTy << Base->getSourceRange();
25888 return ExprError();
25889 }
25890
25891 if (SemaRef.RequireCompleteType(Loc: Base->getExprLoc(), T: ResultTy,
25892 DiagID: diag::err_omp_section_incomplete_type, Args: Base))
25893 return ExprError();
25894
25895 if (LowerBound && !OriginalTy->isAnyPointerType()) {
25896 Expr::EvalResult Result;
25897 if (LowerBound->EvaluateAsInt(Result, Ctx: Context)) {
25898 // OpenMP 5.0, [2.1.5 Array Sections]
25899 // The array section must be a subset of the original array.
25900 llvm::APSInt LowerBoundValue = Result.Val.getInt();
25901 if (LowerBoundValue.isNegative()) {
25902 Diag(Loc: LowerBound->getExprLoc(),
25903 DiagID: diag::err_omp_section_not_subset_of_array)
25904 << LowerBound->getSourceRange();
25905 return ExprError();
25906 }
25907 }
25908 }
25909
25910 if (Length) {
25911 Expr::EvalResult Result;
25912 if (Length->EvaluateAsInt(Result, Ctx: Context)) {
25913 // OpenMP 5.0, [2.1.5 Array Sections]
25914 // The length must evaluate to non-negative integers.
25915 llvm::APSInt LengthValue = Result.Val.getInt();
25916 if (LengthValue.isNegative()) {
25917 Diag(Loc: Length->getExprLoc(), DiagID: diag::err_omp_section_length_negative)
25918 << toString(I: LengthValue, /*Radix=*/10, /*Signed=*/true)
25919 << Length->getSourceRange();
25920 return ExprError();
25921 }
25922 }
25923 } else if (SemaRef.getLangOpts().OpenMP < 60 && ColonLocFirst.isValid() &&
25924 (OriginalTy.isNull() || (!OriginalTy->isConstantArrayType() &&
25925 !OriginalTy->isVariableArrayType()))) {
25926 // OpenMP 5.0, [2.1.5 Array Sections]
25927 // When the size of the array dimension is not known, the length must be
25928 // specified explicitly.
25929 Diag(Loc: ColonLocFirst, DiagID: diag::err_omp_section_length_undefined)
25930 << (!OriginalTy.isNull() && OriginalTy->isArrayType());
25931 return ExprError();
25932 }
25933
25934 if (Stride) {
25935 Expr::EvalResult Result;
25936 if (Stride->EvaluateAsInt(Result, Ctx: Context)) {
25937 // OpenMP 5.0, [2.1.5 Array Sections]
25938 // The stride must evaluate to a positive integer.
25939 llvm::APSInt StrideValue = Result.Val.getInt();
25940 if (!StrideValue.isStrictlyPositive()) {
25941 Diag(Loc: Stride->getExprLoc(), DiagID: diag::err_omp_section_stride_non_positive)
25942 << toString(I: StrideValue, /*Radix=*/10, /*Signed=*/true)
25943 << Stride->getSourceRange();
25944 return ExprError();
25945 }
25946 }
25947 }
25948
25949 if (!Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
25950 ExprResult Result = SemaRef.DefaultFunctionArrayLvalueConversion(E: Base);
25951 if (Result.isInvalid())
25952 return ExprError();
25953 Base = Result.get();
25954 }
25955 return new (Context) ArraySectionExpr(
25956 Base, LowerBound, Length, Stride, Context.ArraySectionTy, VK_LValue,
25957 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
25958}
25959
25960ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
25961 Expr *Base, SourceLocation LParenLoc, SourceLocation RParenLoc,
25962 ArrayRef<Expr *> Dims, ArrayRef<SourceRange> Brackets) {
25963 ASTContext &Context = getASTContext();
25964 if (Base->hasPlaceholderType()) {
25965 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
25966 if (Result.isInvalid())
25967 return ExprError();
25968 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25969 if (Result.isInvalid())
25970 return ExprError();
25971 Base = Result.get();
25972 }
25973 QualType BaseTy = Base->getType();
25974 // Delay analysis of the types/expressions if instantiation/specialization is
25975 // required.
25976 if (!BaseTy->isPointerType() && Base->isTypeDependent())
25977 return OMPArrayShapingExpr::Create(Context, T: Context.DependentTy, Op: Base,
25978 L: LParenLoc, R: RParenLoc, Dims, BracketRanges: Brackets);
25979 if (!BaseTy->isPointerType() ||
25980 (!Base->isTypeDependent() &&
25981 BaseTy->getPointeeType()->isIncompleteType()))
25982 return ExprError(Diag(Loc: Base->getExprLoc(),
25983 DiagID: diag::err_omp_non_pointer_type_array_shaping_base)
25984 << Base->getSourceRange());
25985
25986 SmallVector<Expr *, 4> NewDims;
25987 bool ErrorFound = false;
25988 for (Expr *Dim : Dims) {
25989 if (Dim->hasPlaceholderType()) {
25990 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Dim);
25991 if (Result.isInvalid()) {
25992 ErrorFound = true;
25993 continue;
25994 }
25995 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25996 if (Result.isInvalid()) {
25997 ErrorFound = true;
25998 continue;
25999 }
26000 Dim = Result.get();
26001 }
26002 if (!Dim->isTypeDependent()) {
26003 ExprResult Result =
26004 PerformOpenMPImplicitIntegerConversion(Loc: Dim->getExprLoc(), Op: Dim);
26005 if (Result.isInvalid()) {
26006 ErrorFound = true;
26007 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_typecheck_shaping_not_integer)
26008 << Dim->getSourceRange();
26009 continue;
26010 }
26011 Dim = Result.get();
26012 Expr::EvalResult EvResult;
26013 if (!Dim->isValueDependent() && Dim->EvaluateAsInt(Result&: EvResult, Ctx: Context)) {
26014 // OpenMP 5.0, [2.1.4 Array Shaping]
26015 // Each si is an integral type expression that must evaluate to a
26016 // positive integer.
26017 llvm::APSInt Value = EvResult.Val.getInt();
26018 if (!Value.isStrictlyPositive()) {
26019 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_shaping_dimension_not_positive)
26020 << toString(I: Value, /*Radix=*/10, /*Signed=*/true)
26021 << Dim->getSourceRange();
26022 ErrorFound = true;
26023 continue;
26024 }
26025 }
26026 }
26027 NewDims.push_back(Elt: Dim);
26028 }
26029 if (ErrorFound)
26030 return ExprError();
26031 return OMPArrayShapingExpr::Create(Context, T: Context.OMPArrayShapingTy, Op: Base,
26032 L: LParenLoc, R: RParenLoc, Dims: NewDims, BracketRanges: Brackets);
26033}
26034
26035ExprResult SemaOpenMP::ActOnOMPIteratorExpr(Scope *S,
26036 SourceLocation IteratorKwLoc,
26037 SourceLocation LLoc,
26038 SourceLocation RLoc,
26039 ArrayRef<OMPIteratorData> Data) {
26040 ASTContext &Context = getASTContext();
26041 SmallVector<OMPIteratorExpr::IteratorDefinition, 4> ID;
26042 bool IsCorrect = true;
26043 for (const OMPIteratorData &D : Data) {
26044 TypeSourceInfo *TInfo = nullptr;
26045 SourceLocation StartLoc;
26046 QualType DeclTy;
26047 if (!D.Type.getAsOpaquePtr()) {
26048 // OpenMP 5.0, 2.1.6 Iterators
26049 // In an iterator-specifier, if the iterator-type is not specified then
26050 // the type of that iterator is of int type.
26051 DeclTy = Context.IntTy;
26052 StartLoc = D.DeclIdentLoc;
26053 } else {
26054 DeclTy = Sema::GetTypeFromParser(Ty: D.Type, TInfo: &TInfo);
26055 StartLoc = TInfo->getTypeLoc().getBeginLoc();
26056 }
26057
26058 bool IsDeclTyDependent = DeclTy->isDependentType() ||
26059 DeclTy->containsUnexpandedParameterPack() ||
26060 DeclTy->isInstantiationDependentType();
26061 if (!IsDeclTyDependent) {
26062 if (!DeclTy->isIntegralType(Ctx: Context) && !DeclTy->isAnyPointerType()) {
26063 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
26064 // The iterator-type must be an integral or pointer type.
26065 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
26066 << DeclTy;
26067 IsCorrect = false;
26068 continue;
26069 }
26070 if (DeclTy.isConstant(Ctx: Context)) {
26071 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
26072 // The iterator-type must not be const qualified.
26073 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
26074 << DeclTy;
26075 IsCorrect = false;
26076 continue;
26077 }
26078 }
26079
26080 // Iterator declaration.
26081 assert(D.DeclIdent && "Identifier expected.");
26082 // Always try to create iterator declarator to avoid extra error messages
26083 // about unknown declarations use.
26084 auto *VD =
26085 VarDecl::Create(C&: Context, DC: SemaRef.CurContext, StartLoc, IdLoc: D.DeclIdentLoc,
26086 Id: D.DeclIdent, T: DeclTy, TInfo, S: SC_None);
26087 VD->setImplicit();
26088 if (S) {
26089 // Check for conflicting previous declaration.
26090 DeclarationNameInfo NameInfo(VD->getDeclName(), D.DeclIdentLoc);
26091 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
26092 RedeclarationKind::ForVisibleRedeclaration);
26093 Previous.suppressDiagnostics();
26094 SemaRef.LookupName(R&: Previous, S);
26095
26096 SemaRef.FilterLookupForScope(R&: Previous, Ctx: SemaRef.CurContext, S,
26097 /*ConsiderLinkage=*/false,
26098 /*AllowInlineNamespace=*/false);
26099 if (!Previous.empty()) {
26100 NamedDecl *Old = Previous.getRepresentativeDecl();
26101 Diag(Loc: D.DeclIdentLoc, DiagID: diag::err_redefinition) << VD->getDeclName();
26102 Diag(Loc: Old->getLocation(), DiagID: diag::note_previous_definition);
26103 } else {
26104 SemaRef.PushOnScopeChains(D: VD, S);
26105 }
26106 } else {
26107 SemaRef.CurContext->addDecl(D: VD);
26108 }
26109
26110 /// Act on the iterator variable declaration.
26111 ActOnOpenMPIteratorVarDecl(VD);
26112
26113 Expr *Begin = D.Range.Begin;
26114 if (!IsDeclTyDependent && Begin && !Begin->isTypeDependent()) {
26115 ExprResult BeginRes = SemaRef.PerformImplicitConversion(
26116 From: Begin, ToType: DeclTy, Action: AssignmentAction::Converting);
26117 Begin = BeginRes.get();
26118 }
26119 Expr *End = D.Range.End;
26120 if (!IsDeclTyDependent && End && !End->isTypeDependent()) {
26121 ExprResult EndRes = SemaRef.PerformImplicitConversion(
26122 From: End, ToType: DeclTy, Action: AssignmentAction::Converting);
26123 End = EndRes.get();
26124 }
26125 Expr *Step = D.Range.Step;
26126 if (!IsDeclTyDependent && Step && !Step->isTypeDependent()) {
26127 if (!Step->getType()->isIntegralType(Ctx: Context)) {
26128 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_not_integral)
26129 << Step << Step->getSourceRange();
26130 IsCorrect = false;
26131 continue;
26132 }
26133 std::optional<llvm::APSInt> Result =
26134 Step->getIntegerConstantExpr(Ctx: Context);
26135 // OpenMP 5.0, 2.1.6 Iterators, Restrictions
26136 // If the step expression of a range-specification equals zero, the
26137 // behavior is unspecified.
26138 if (Result && Result->isZero()) {
26139 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_constant_zero)
26140 << Step << Step->getSourceRange();
26141 IsCorrect = false;
26142 continue;
26143 }
26144 }
26145 if (!Begin || !End || !IsCorrect) {
26146 IsCorrect = false;
26147 continue;
26148 }
26149 OMPIteratorExpr::IteratorDefinition &IDElem = ID.emplace_back();
26150 IDElem.IteratorDecl = VD;
26151 IDElem.AssignmentLoc = D.AssignLoc;
26152 IDElem.Range.Begin = Begin;
26153 IDElem.Range.End = End;
26154 IDElem.Range.Step = Step;
26155 IDElem.ColonLoc = D.ColonLoc;
26156 IDElem.SecondColonLoc = D.SecColonLoc;
26157 }
26158 if (!IsCorrect) {
26159 // Invalidate all created iterator declarations if error is found.
26160 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
26161 if (Decl *ID = D.IteratorDecl)
26162 ID->setInvalidDecl();
26163 }
26164 return ExprError();
26165 }
26166 SmallVector<OMPIteratorHelperData, 4> Helpers;
26167 if (!SemaRef.CurContext->isDependentContext()) {
26168 // Build number of ityeration for each iteration range.
26169 // Ni = ((Stepi > 0) ? ((Endi + Stepi -1 - Begini)/Stepi) :
26170 // ((Begini-Stepi-1-Endi) / -Stepi);
26171 for (OMPIteratorExpr::IteratorDefinition &D : ID) {
26172 // (Endi - Begini)
26173 ExprResult Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Sub,
26174 LHSExpr: D.Range.End, RHSExpr: D.Range.Begin);
26175 if (!Res.isUsable()) {
26176 IsCorrect = false;
26177 continue;
26178 }
26179 ExprResult St, St1;
26180 if (D.Range.Step) {
26181 St = D.Range.Step;
26182 // (Endi - Begini) + Stepi
26183 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res.get(),
26184 RHSExpr: St.get());
26185 if (!Res.isUsable()) {
26186 IsCorrect = false;
26187 continue;
26188 }
26189 // (Endi - Begini) + Stepi - 1
26190 Res = SemaRef.CreateBuiltinBinOp(
26191 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res.get(),
26192 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
26193 if (!Res.isUsable()) {
26194 IsCorrect = false;
26195 continue;
26196 }
26197 // ((Endi - Begini) + Stepi - 1) / Stepi
26198 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res.get(),
26199 RHSExpr: St.get());
26200 if (!Res.isUsable()) {
26201 IsCorrect = false;
26202 continue;
26203 }
26204 St1 = SemaRef.CreateBuiltinUnaryOp(OpLoc: D.AssignmentLoc, Opc: UO_Minus,
26205 InputExpr: D.Range.Step);
26206 // (Begini - Endi)
26207 ExprResult Res1 = SemaRef.CreateBuiltinBinOp(
26208 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: D.Range.Begin, RHSExpr: D.Range.End);
26209 if (!Res1.isUsable()) {
26210 IsCorrect = false;
26211 continue;
26212 }
26213 // (Begini - Endi) - Stepi
26214 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res1.get(),
26215 RHSExpr: St1.get());
26216 if (!Res1.isUsable()) {
26217 IsCorrect = false;
26218 continue;
26219 }
26220 // (Begini - Endi) - Stepi - 1
26221 Res1 = SemaRef.CreateBuiltinBinOp(
26222 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res1.get(),
26223 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
26224 if (!Res1.isUsable()) {
26225 IsCorrect = false;
26226 continue;
26227 }
26228 // ((Begini - Endi) - Stepi - 1) / (-Stepi)
26229 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res1.get(),
26230 RHSExpr: St1.get());
26231 if (!Res1.isUsable()) {
26232 IsCorrect = false;
26233 continue;
26234 }
26235 // Stepi > 0.
26236 ExprResult CmpRes = SemaRef.CreateBuiltinBinOp(
26237 OpLoc: D.AssignmentLoc, Opc: BO_GT, LHSExpr: D.Range.Step,
26238 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 0).get());
26239 if (!CmpRes.isUsable()) {
26240 IsCorrect = false;
26241 continue;
26242 }
26243 Res = SemaRef.ActOnConditionalOp(QuestionLoc: D.AssignmentLoc, ColonLoc: D.AssignmentLoc,
26244 CondExpr: CmpRes.get(), LHSExpr: Res.get(), RHSExpr: Res1.get());
26245 if (!Res.isUsable()) {
26246 IsCorrect = false;
26247 continue;
26248 }
26249 }
26250 Res = SemaRef.ActOnFinishFullExpr(Expr: Res.get(), /*DiscardedValue=*/false);
26251 if (!Res.isUsable()) {
26252 IsCorrect = false;
26253 continue;
26254 }
26255
26256 // Build counter update.
26257 // Build counter.
26258 auto *CounterVD = VarDecl::Create(C&: Context, DC: SemaRef.CurContext,
26259 StartLoc: D.IteratorDecl->getBeginLoc(),
26260 IdLoc: D.IteratorDecl->getBeginLoc(), Id: nullptr,
26261 T: Res.get()->getType(), TInfo: nullptr, S: SC_None);
26262 CounterVD->setImplicit();
26263 ExprResult RefRes =
26264 SemaRef.BuildDeclRefExpr(D: CounterVD, Ty: CounterVD->getType(), VK: VK_LValue,
26265 Loc: D.IteratorDecl->getBeginLoc());
26266 // Build counter update.
26267 // I = Begini + counter * Stepi;
26268 ExprResult UpdateRes;
26269 if (D.Range.Step) {
26270 UpdateRes = SemaRef.CreateBuiltinBinOp(
26271 OpLoc: D.AssignmentLoc, Opc: BO_Mul,
26272 LHSExpr: SemaRef.DefaultLvalueConversion(E: RefRes.get()).get(), RHSExpr: St.get());
26273 } else {
26274 UpdateRes = SemaRef.DefaultLvalueConversion(E: RefRes.get());
26275 }
26276 if (!UpdateRes.isUsable()) {
26277 IsCorrect = false;
26278 continue;
26279 }
26280 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add,
26281 LHSExpr: D.Range.Begin, RHSExpr: UpdateRes.get());
26282 if (!UpdateRes.isUsable()) {
26283 IsCorrect = false;
26284 continue;
26285 }
26286 ExprResult VDRes =
26287 SemaRef.BuildDeclRefExpr(D: cast<VarDecl>(Val: D.IteratorDecl),
26288 Ty: cast<VarDecl>(Val: D.IteratorDecl)->getType(),
26289 VK: VK_LValue, Loc: D.IteratorDecl->getBeginLoc());
26290 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Assign,
26291 LHSExpr: VDRes.get(), RHSExpr: UpdateRes.get());
26292 if (!UpdateRes.isUsable()) {
26293 IsCorrect = false;
26294 continue;
26295 }
26296 UpdateRes =
26297 SemaRef.ActOnFinishFullExpr(Expr: UpdateRes.get(), /*DiscardedValue=*/true);
26298 if (!UpdateRes.isUsable()) {
26299 IsCorrect = false;
26300 continue;
26301 }
26302 ExprResult CounterUpdateRes = SemaRef.CreateBuiltinUnaryOp(
26303 OpLoc: D.AssignmentLoc, Opc: UO_PreInc, InputExpr: RefRes.get());
26304 if (!CounterUpdateRes.isUsable()) {
26305 IsCorrect = false;
26306 continue;
26307 }
26308 CounterUpdateRes = SemaRef.ActOnFinishFullExpr(Expr: CounterUpdateRes.get(),
26309 /*DiscardedValue=*/true);
26310 if (!CounterUpdateRes.isUsable()) {
26311 IsCorrect = false;
26312 continue;
26313 }
26314 OMPIteratorHelperData &HD = Helpers.emplace_back();
26315 HD.CounterVD = CounterVD;
26316 HD.Upper = Res.get();
26317 HD.Update = UpdateRes.get();
26318 HD.CounterUpdate = CounterUpdateRes.get();
26319 }
26320 } else {
26321 Helpers.assign(NumElts: ID.size(), Elt: {});
26322 }
26323 if (!IsCorrect) {
26324 // Invalidate all created iterator declarations if error is found.
26325 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
26326 if (Decl *ID = D.IteratorDecl)
26327 ID->setInvalidDecl();
26328 }
26329 return ExprError();
26330 }
26331 return OMPIteratorExpr::Create(Context, T: Context.OMPIteratorTy, IteratorKwLoc,
26332 L: LLoc, R: RLoc, Data: ID, Helpers);
26333}
26334
26335/// Check if \p AssumptionStr is a known assumption and warn if not.
26336static void checkOMPAssumeAttr(Sema &S, SourceLocation Loc,
26337 StringRef AssumptionStr) {
26338 if (llvm::getKnownAssumptionStrings().count(Key: AssumptionStr))
26339 return;
26340
26341 unsigned BestEditDistance = 3;
26342 StringRef Suggestion;
26343 for (const auto &KnownAssumptionIt : llvm::getKnownAssumptionStrings()) {
26344 unsigned EditDistance =
26345 AssumptionStr.edit_distance(Other: KnownAssumptionIt.getKey());
26346 if (EditDistance < BestEditDistance) {
26347 Suggestion = KnownAssumptionIt.getKey();
26348 BestEditDistance = EditDistance;
26349 }
26350 }
26351
26352 if (!Suggestion.empty())
26353 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown_suggested)
26354 << AssumptionStr << Suggestion;
26355 else
26356 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown)
26357 << AssumptionStr;
26358}
26359
26360void SemaOpenMP::handleOMPAssumeAttr(Decl *D, const ParsedAttr &AL) {
26361 // Handle the case where the attribute has a text message.
26362 StringRef Str;
26363 SourceLocation AttrStrLoc;
26364 if (!SemaRef.checkStringLiteralArgumentAttr(Attr: AL, ArgNum: 0, Str, ArgLocation: &AttrStrLoc))
26365 return;
26366
26367 checkOMPAssumeAttr(S&: SemaRef, Loc: AttrStrLoc, AssumptionStr: Str);
26368
26369 D->addAttr(A: ::new (getASTContext()) OMPAssumeAttr(getASTContext(), AL, Str));
26370}
26371
26372SemaOpenMP::SemaOpenMP(Sema &S)
26373 : SemaBase(S), VarDataSharingAttributesStack(nullptr) {}
26374