1//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements semantic analysis for OpenMP directives and
10/// clauses.
11///
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/SemaOpenMP.h"
15#include "clang/AST/ASTConsumer.h"
16
17#include "TreeTransform.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTMutationListener.h"
20#include "clang/AST/CXXInheritance.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclOpenMP.h"
24#include "clang/AST/DynamicRecursiveASTVisitor.h"
25#include "clang/AST/OpenMPClause.h"
26#include "clang/AST/StmtCXX.h"
27#include "clang/AST/StmtOpenMP.h"
28#include "clang/AST/StmtVisitor.h"
29#include "clang/Basic/DiagnosticSema.h"
30#include "clang/Basic/OpenMPKinds.h"
31#include "clang/Basic/PartialDiagnostic.h"
32#include "clang/Basic/TargetInfo.h"
33#include "clang/Sema/EnterExpressionEvaluationContext.h"
34#include "clang/Sema/Initialization.h"
35#include "clang/Sema/Lookup.h"
36#include "clang/Sema/ParsedAttr.h"
37#include "clang/Sema/Scope.h"
38#include "clang/Sema/ScopeInfo.h"
39#include "clang/Sema/Sema.h"
40#include "llvm/ADT/IndexedMap.h"
41#include "llvm/ADT/PointerEmbeddedInt.h"
42#include "llvm/ADT/STLExtras.h"
43#include "llvm/ADT/Sequence.h"
44#include "llvm/ADT/SetVector.h"
45#include "llvm/ADT/SmallSet.h"
46#include "llvm/ADT/StringExtras.h"
47#include "llvm/Frontend/OpenMP/OMPAssume.h"
48#include "llvm/Frontend/OpenMP/OMPConstants.h"
49#include "llvm/IR/Assumptions.h"
50#include <optional>
51
52using namespace clang;
53using namespace llvm::omp;
54
55//===----------------------------------------------------------------------===//
56// Stack of data-sharing attributes for variables
57//===----------------------------------------------------------------------===//
58
59static const Expr *checkMapClauseExpressionBase(
60 Sema &SemaRef, Expr *E,
61 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
62 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose);
63
64static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C);
65
66namespace {
67/// Default data sharing attributes, which can be applied to directive.
68enum DefaultDataSharingAttributes {
69 DSA_unspecified = 0, /// Data sharing attribute not specified.
70 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
71 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
72 DSA_private = 1 << 2, /// Default data sharing attribute 'private'.
73 DSA_firstprivate = 1 << 3, /// Default data sharing attribute 'firstprivate'.
74};
75
76/// Variable Category attributes to restrict the modifier of the
77/// default clause (DefaultDataSharingAttributes)
78/// Not mentioning any Variable category attribute indicates
79/// the modifier (DefaultDataSharingAttributes) is for all variables.
80enum DefaultDataSharingVCAttributes {
81 DSA_VC_all = 0, /// for all variables.
82 DSA_VC_aggregate, /// for aggregate variables.
83 DSA_VC_pointer, /// for pointer variables.
84 DSA_VC_scalar, /// for scalar variables.
85};
86
87/// Stack for tracking declarations used in OpenMP directives and
88/// clauses and their data-sharing attributes.
89class DSAStackTy {
90public:
91 struct DSAVarData {
92 OpenMPDirectiveKind DKind = OMPD_unknown;
93 OpenMPClauseKind CKind = OMPC_unknown;
94 unsigned Modifier = 0;
95 const Expr *RefExpr = nullptr;
96 DeclRefExpr *PrivateCopy = nullptr;
97 SourceLocation ImplicitDSALoc;
98 bool AppliedToPointee = false;
99 DSAVarData() = default;
100 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
101 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
102 SourceLocation ImplicitDSALoc, unsigned Modifier,
103 bool AppliedToPointee)
104 : DKind(DKind), CKind(CKind), Modifier(Modifier), RefExpr(RefExpr),
105 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc),
106 AppliedToPointee(AppliedToPointee) {}
107 };
108 using OperatorOffsetTy =
109 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
110 using DoacrossClauseMapTy = llvm::DenseMap<OMPClause *, OperatorOffsetTy>;
111 /// Kind of the declaration used in the uses_allocators clauses.
112 enum class UsesAllocatorsDeclKind {
113 /// Predefined allocator
114 PredefinedAllocator,
115 /// User-defined allocator
116 UserDefinedAllocator,
117 /// The declaration that represent allocator trait
118 AllocatorTrait,
119 };
120
121private:
122 struct DSAInfo {
123 OpenMPClauseKind Attributes = OMPC_unknown;
124 unsigned Modifier = 0;
125 /// Pointer to a reference expression and a flag which shows that the
126 /// variable is marked as lastprivate(true) or not (false).
127 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
128 DeclRefExpr *PrivateCopy = nullptr;
129 /// true if the attribute is applied to the pointee, not the variable
130 /// itself.
131 bool AppliedToPointee = false;
132 };
133 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
134 using UsedRefMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
135 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
136 using LoopControlVariablesMapTy =
137 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
138 /// Struct that associates a component with the clause kind where they are
139 /// found.
140 struct MappedExprComponentTy {
141 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
142 OpenMPClauseKind Kind = OMPC_unknown;
143 };
144 using MappedExprComponentsTy =
145 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
146 using CriticalsWithHintsTy =
147 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
148 struct ReductionData {
149 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
150 SourceRange ReductionRange;
151 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
152 ReductionData() = default;
153 void set(BinaryOperatorKind BO, SourceRange RR) {
154 ReductionRange = RR;
155 ReductionOp = BO;
156 }
157 void set(const Expr *RefExpr, SourceRange RR) {
158 ReductionRange = RR;
159 ReductionOp = RefExpr;
160 }
161 };
162 using DeclReductionMapTy =
163 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
164 struct DefaultmapInfo {
165 OpenMPDefaultmapClauseModifier ImplicitBehavior =
166 OMPC_DEFAULTMAP_MODIFIER_unknown;
167 SourceLocation SLoc;
168 DefaultmapInfo() = default;
169 DefaultmapInfo(OpenMPDefaultmapClauseModifier M, SourceLocation Loc)
170 : ImplicitBehavior(M), SLoc(Loc) {}
171 };
172
173 struct SharingMapTy {
174 DeclSAMapTy SharingMap;
175 DeclReductionMapTy ReductionMap;
176 UsedRefMapTy AlignedMap;
177 UsedRefMapTy NontemporalMap;
178 MappedExprComponentsTy MappedExprComponents;
179 LoopControlVariablesMapTy LCVMap;
180 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
181 SourceLocation DefaultAttrLoc;
182 DefaultDataSharingVCAttributes DefaultVCAttr = DSA_VC_all;
183 SourceLocation DefaultAttrVCLoc;
184 DefaultmapInfo DefaultmapMap[OMPC_DEFAULTMAP_unknown + 1];
185 OpenMPDirectiveKind Directive = OMPD_unknown;
186 DeclarationNameInfo DirectiveName;
187 Scope *CurScope = nullptr;
188 DeclContext *Context = nullptr;
189 SourceLocation ConstructLoc;
190 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
191 /// get the data (loop counters etc.) about enclosing loop-based construct.
192 /// This data is required during codegen.
193 DoacrossClauseMapTy DoacrossDepends;
194 /// First argument (Expr *) contains optional argument of the
195 /// 'ordered' clause, the second one is true if the regions has 'ordered'
196 /// clause, false otherwise.
197 std::optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
198 bool RegionHasOrderConcurrent = false;
199 unsigned AssociatedLoops = 1;
200 bool HasMutipleLoops = false;
201 const Decl *PossiblyLoopCounter = nullptr;
202 bool NowaitRegion = false;
203 bool UntiedRegion = false;
204 bool CancelRegion = false;
205 bool LoopStart = false;
206 bool BodyComplete = false;
207 SourceLocation PrevScanLocation;
208 SourceLocation PrevOrderedLocation;
209 SourceLocation InnerTeamsRegionLoc;
210 /// Reference to the taskgroup task_reduction reference expression.
211 Expr *TaskgroupReductionRef = nullptr;
212 llvm::DenseSet<QualType> MappedClassesQualTypes;
213 SmallVector<Expr *, 4> InnerUsedAllocators;
214 llvm::DenseSet<CanonicalDeclPtr<Decl>> ImplicitTaskFirstprivates;
215 /// List of globals marked as declare target link in this target region
216 /// (isOpenMPTargetExecutionDirective(Directive) == true).
217 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
218 /// List of decls used in inclusive/exclusive clauses of the scan directive.
219 llvm::DenseSet<CanonicalDeclPtr<Decl>> UsedInScanDirective;
220 llvm::DenseMap<CanonicalDeclPtr<const Decl>, UsesAllocatorsDeclKind>
221 UsesAllocatorsDecls;
222 /// Data is required on creating capture fields for implicit
223 /// default first|private clause.
224 struct ImplicitDefaultFDInfoTy {
225 /// Field decl.
226 const FieldDecl *FD = nullptr;
227 /// Nesting stack level
228 size_t StackLevel = 0;
229 /// Capture variable decl.
230 VarDecl *VD = nullptr;
231 ImplicitDefaultFDInfoTy(const FieldDecl *FD, size_t StackLevel,
232 VarDecl *VD)
233 : FD(FD), StackLevel(StackLevel), VD(VD) {}
234 };
235 /// List of captured fields
236 llvm::SmallVector<ImplicitDefaultFDInfoTy, 8>
237 ImplicitDefaultFirstprivateFDs;
238 Expr *DeclareMapperVar = nullptr;
239 SmallVector<VarDecl *, 16> IteratorVarDecls;
240 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
241 Scope *CurScope, SourceLocation Loc)
242 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
243 ConstructLoc(Loc) {}
244 SharingMapTy() = default;
245 };
246
247 using StackTy = SmallVector<SharingMapTy, 4>;
248
249 /// Stack of used declaration and their data-sharing attributes.
250 DeclSAMapTy Threadprivates;
251 DeclSAMapTy Groupprivates;
252 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
253 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
254 /// true, if check for DSA must be from parent directive, false, if
255 /// from current directive.
256 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
257 Sema &SemaRef;
258 bool ForceCapturing = false;
259 /// true if all the variables in the target executable directives must be
260 /// captured by reference.
261 bool ForceCaptureByReferenceInTargetExecutable = false;
262 CriticalsWithHintsTy Criticals;
263 unsigned IgnoredStackElements = 0;
264
265 /// Iterators over the stack iterate in order from innermost to outermost
266 /// directive.
267 using const_iterator = StackTy::const_reverse_iterator;
268 const_iterator begin() const {
269 return Stack.empty() ? const_iterator()
270 : Stack.back().first.rbegin() + IgnoredStackElements;
271 }
272 const_iterator end() const {
273 return Stack.empty() ? const_iterator() : Stack.back().first.rend();
274 }
275 using iterator = StackTy::reverse_iterator;
276 iterator begin() {
277 return Stack.empty() ? iterator()
278 : Stack.back().first.rbegin() + IgnoredStackElements;
279 }
280 iterator end() {
281 return Stack.empty() ? iterator() : Stack.back().first.rend();
282 }
283
284 // Convenience operations to get at the elements of the stack.
285
286 bool isStackEmpty() const {
287 return Stack.empty() ||
288 Stack.back().second != CurrentNonCapturingFunctionScope ||
289 Stack.back().first.size() <= IgnoredStackElements;
290 }
291 size_t getStackSize() const {
292 return isStackEmpty() ? 0
293 : Stack.back().first.size() - IgnoredStackElements;
294 }
295
296 SharingMapTy *getTopOfStackOrNull() {
297 size_t Size = getStackSize();
298 if (Size == 0)
299 return nullptr;
300 return &Stack.back().first[Size - 1];
301 }
302 const SharingMapTy *getTopOfStackOrNull() const {
303 return const_cast<DSAStackTy &>(*this).getTopOfStackOrNull();
304 }
305 SharingMapTy &getTopOfStack() {
306 assert(!isStackEmpty() && "no current directive");
307 return *getTopOfStackOrNull();
308 }
309 const SharingMapTy &getTopOfStack() const {
310 return const_cast<DSAStackTy &>(*this).getTopOfStack();
311 }
312
313 SharingMapTy *getSecondOnStackOrNull() {
314 size_t Size = getStackSize();
315 if (Size <= 1)
316 return nullptr;
317 return &Stack.back().first[Size - 2];
318 }
319 const SharingMapTy *getSecondOnStackOrNull() const {
320 return const_cast<DSAStackTy &>(*this).getSecondOnStackOrNull();
321 }
322
323 /// Get the stack element at a certain level (previously returned by
324 /// \c getNestingLevel).
325 ///
326 /// Note that nesting levels count from outermost to innermost, and this is
327 /// the reverse of our iteration order where new inner levels are pushed at
328 /// the front of the stack.
329 SharingMapTy &getStackElemAtLevel(unsigned Level) {
330 assert(Level < getStackSize() && "no such stack element");
331 return Stack.back().first[Level];
332 }
333 const SharingMapTy &getStackElemAtLevel(unsigned Level) const {
334 return const_cast<DSAStackTy &>(*this).getStackElemAtLevel(Level);
335 }
336
337 DSAVarData getDSA(const_iterator &Iter, ValueDecl *D) const;
338
339 /// Checks if the variable is a local for OpenMP region.
340 bool isOpenMPLocal(VarDecl *D, const_iterator Iter) const;
341
342 /// Vector of previously declared requires directives
343 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
344 /// omp_allocator_handle_t type.
345 QualType OMPAllocatorHandleT;
346 /// omp_depend_t type.
347 QualType OMPDependT;
348 /// omp_event_handle_t type.
349 QualType OMPEventHandleT;
350 /// omp_alloctrait_t type.
351 QualType OMPAlloctraitT;
352 /// Expression for the predefined allocators.
353 Expr *OMPPredefinedAllocators[OMPAllocateDeclAttr::OMPUserDefinedMemAlloc] = {
354 nullptr};
355 /// Vector of previously encountered target directives
356 SmallVector<SourceLocation, 2> TargetLocations;
357 SourceLocation AtomicLocation;
358 /// Vector of declare variant construct traits.
359 SmallVector<llvm::omp::TraitProperty, 8> ConstructTraits;
360
361public:
362 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
363
364 /// Sets omp_allocator_handle_t type.
365 void setOMPAllocatorHandleT(QualType Ty) { OMPAllocatorHandleT = Ty; }
366 /// Gets omp_allocator_handle_t type.
367 QualType getOMPAllocatorHandleT() const { return OMPAllocatorHandleT; }
368 /// Sets omp_alloctrait_t type.
369 void setOMPAlloctraitT(QualType Ty) { OMPAlloctraitT = Ty; }
370 /// Gets omp_alloctrait_t type.
371 QualType getOMPAlloctraitT() const { return OMPAlloctraitT; }
372 /// Sets the given default allocator.
373 void setAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
374 Expr *Allocator) {
375 OMPPredefinedAllocators[AllocatorKind] = Allocator;
376 }
377 /// Returns the specified default allocator.
378 Expr *getAllocator(OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind) const {
379 return OMPPredefinedAllocators[AllocatorKind];
380 }
381 /// Sets omp_depend_t type.
382 void setOMPDependT(QualType Ty) { OMPDependT = Ty; }
383 /// Gets omp_depend_t type.
384 QualType getOMPDependT() const { return OMPDependT; }
385
386 /// Sets omp_event_handle_t type.
387 void setOMPEventHandleT(QualType Ty) { OMPEventHandleT = Ty; }
388 /// Gets omp_event_handle_t type.
389 QualType getOMPEventHandleT() const { return OMPEventHandleT; }
390
391 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
392 OpenMPClauseKind getClauseParsingMode() const {
393 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
394 return ClauseKindMode;
395 }
396 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
397
398 bool isBodyComplete() const {
399 const SharingMapTy *Top = getTopOfStackOrNull();
400 return Top && Top->BodyComplete;
401 }
402 void setBodyComplete() { getTopOfStack().BodyComplete = true; }
403
404 bool isForceVarCapturing() const { return ForceCapturing; }
405 void setForceVarCapturing(bool V) { ForceCapturing = V; }
406
407 void setForceCaptureByReferenceInTargetExecutable(bool V) {
408 ForceCaptureByReferenceInTargetExecutable = V;
409 }
410 bool isForceCaptureByReferenceInTargetExecutable() const {
411 return ForceCaptureByReferenceInTargetExecutable;
412 }
413
414 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
415 Scope *CurScope, SourceLocation Loc) {
416 assert(!IgnoredStackElements &&
417 "cannot change stack while ignoring elements");
418 if (Stack.empty() ||
419 Stack.back().second != CurrentNonCapturingFunctionScope)
420 Stack.emplace_back(Args: StackTy(), Args&: CurrentNonCapturingFunctionScope);
421 Stack.back().first.emplace_back(Args&: DKind, Args: DirName, Args&: CurScope, Args&: Loc);
422 Stack.back().first.back().DefaultAttrLoc = Loc;
423 }
424
425 void pop() {
426 assert(!IgnoredStackElements &&
427 "cannot change stack while ignoring elements");
428 assert(!Stack.back().first.empty() &&
429 "Data-sharing attributes stack is empty!");
430 Stack.back().first.pop_back();
431 }
432
433 /// RAII object to temporarily leave the scope of a directive when we want to
434 /// logically operate in its parent.
435 class ParentDirectiveScope {
436 DSAStackTy &Self;
437 bool Active;
438
439 public:
440 ParentDirectiveScope(DSAStackTy &Self, bool Activate)
441 : Self(Self), Active(false) {
442 if (Activate)
443 enable();
444 }
445 ~ParentDirectiveScope() { disable(); }
446 void disable() {
447 if (Active) {
448 --Self.IgnoredStackElements;
449 Active = false;
450 }
451 }
452 void enable() {
453 if (!Active) {
454 ++Self.IgnoredStackElements;
455 Active = true;
456 }
457 }
458 };
459
460 /// Marks that we're started loop parsing.
461 void loopInit() {
462 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
463 "Expected loop-based directive.");
464 getTopOfStack().LoopStart = true;
465 }
466 /// Start capturing of the variables in the loop context.
467 void loopStart() {
468 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
469 "Expected loop-based directive.");
470 getTopOfStack().LoopStart = false;
471 }
472 /// true, if variables are captured, false otherwise.
473 bool isLoopStarted() const {
474 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
475 "Expected loop-based directive.");
476 return !getTopOfStack().LoopStart;
477 }
478 /// Marks (or clears) declaration as possibly loop counter.
479 void resetPossibleLoopCounter(const Decl *D = nullptr) {
480 getTopOfStack().PossiblyLoopCounter = D ? D->getCanonicalDecl() : D;
481 }
482 /// Gets the possible loop counter decl.
483 const Decl *getPossiblyLoopCounter() const {
484 return getTopOfStack().PossiblyLoopCounter;
485 }
486 /// Start new OpenMP region stack in new non-capturing function.
487 void pushFunction() {
488 assert(!IgnoredStackElements &&
489 "cannot change stack while ignoring elements");
490 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
491 assert(!isa<CapturingScopeInfo>(CurFnScope));
492 CurrentNonCapturingFunctionScope = CurFnScope;
493 }
494 /// Pop region stack for non-capturing function.
495 void popFunction(const FunctionScopeInfo *OldFSI) {
496 assert(!IgnoredStackElements &&
497 "cannot change stack while ignoring elements");
498 if (!Stack.empty() && Stack.back().second == OldFSI) {
499 assert(Stack.back().first.empty());
500 Stack.pop_back();
501 }
502 CurrentNonCapturingFunctionScope = nullptr;
503 for (const FunctionScopeInfo *FSI : llvm::reverse(C&: SemaRef.FunctionScopes)) {
504 if (!isa<CapturingScopeInfo>(Val: FSI)) {
505 CurrentNonCapturingFunctionScope = FSI;
506 break;
507 }
508 }
509 }
510
511 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
512 Criticals.try_emplace(Key: D->getDirectiveName().getAsString(), Args&: D, Args&: Hint);
513 }
514 std::pair<const OMPCriticalDirective *, llvm::APSInt>
515 getCriticalWithHint(const DeclarationNameInfo &Name) const {
516 auto I = Criticals.find(Key: Name.getAsString());
517 if (I != Criticals.end())
518 return I->second;
519 return std::make_pair(x: nullptr, y: llvm::APSInt());
520 }
521 /// If 'aligned' declaration for given variable \a D was not seen yet,
522 /// add it and return NULL; otherwise return previous occurrence's expression
523 /// for diagnostics.
524 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
525 /// If 'nontemporal' declaration for given variable \a D was not seen yet,
526 /// add it and return NULL; otherwise return previous occurrence's expression
527 /// for diagnostics.
528 const Expr *addUniqueNontemporal(const ValueDecl *D, const Expr *NewDE);
529
530 /// Register specified variable as loop control variable.
531 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
532 /// Check if the specified variable is a loop control variable for
533 /// current region.
534 /// \return The index of the loop control variable in the list of associated
535 /// for-loops (from outer to inner).
536 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
537 /// Check if the specified variable is a loop control variable for
538 /// parent region.
539 /// \return The index of the loop control variable in the list of associated
540 /// for-loops (from outer to inner).
541 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
542 /// Check if the specified variable is a loop control variable for
543 /// current region.
544 /// \return The index of the loop control variable in the list of associated
545 /// for-loops (from outer to inner).
546 const LCDeclInfo isLoopControlVariable(const ValueDecl *D,
547 unsigned Level) const;
548 /// Get the loop control variable for the I-th loop (or nullptr) in
549 /// parent directive.
550 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
551
552 /// Marks the specified decl \p D as used in scan directive.
553 void markDeclAsUsedInScanDirective(ValueDecl *D) {
554 if (SharingMapTy *Stack = getSecondOnStackOrNull())
555 Stack->UsedInScanDirective.insert(V: D);
556 }
557
558 /// Checks if the specified declaration was used in the inner scan directive.
559 bool isUsedInScanDirective(ValueDecl *D) const {
560 if (const SharingMapTy *Stack = getTopOfStackOrNull())
561 return Stack->UsedInScanDirective.contains(V: D);
562 return false;
563 }
564
565 /// Adds explicit data sharing attribute to the specified declaration.
566 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
567 DeclRefExpr *PrivateCopy = nullptr, unsigned Modifier = 0,
568 bool AppliedToPointee = false);
569
570 /// Adds additional information for the reduction items with the reduction id
571 /// represented as an operator.
572 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
573 BinaryOperatorKind BOK);
574 /// Adds additional information for the reduction items with the reduction id
575 /// represented as reduction identifier.
576 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
577 const Expr *ReductionRef);
578 /// Returns the location and reduction operation from the innermost parent
579 /// region for the given \p D.
580 const DSAVarData
581 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
582 BinaryOperatorKind &BOK,
583 Expr *&TaskgroupDescriptor) const;
584 /// Returns the location and reduction operation from the innermost parent
585 /// region for the given \p D.
586 const DSAVarData
587 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
588 const Expr *&ReductionRef,
589 Expr *&TaskgroupDescriptor) const;
590 /// Return reduction reference expression for the current taskgroup or
591 /// parallel/worksharing directives with task reductions.
592 Expr *getTaskgroupReductionRef() const {
593 assert((getTopOfStack().Directive == OMPD_taskgroup ||
594 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
595 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
596 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
597 "taskgroup reference expression requested for non taskgroup or "
598 "parallel/worksharing directive.");
599 return getTopOfStack().TaskgroupReductionRef;
600 }
601 /// Checks if the given \p VD declaration is actually a taskgroup reduction
602 /// descriptor variable at the \p Level of OpenMP regions.
603 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
604 return getStackElemAtLevel(Level).TaskgroupReductionRef &&
605 cast<DeclRefExpr>(Val: getStackElemAtLevel(Level).TaskgroupReductionRef)
606 ->getDecl() == VD;
607 }
608
609 /// Returns data sharing attributes from top of the stack for the
610 /// specified declaration.
611 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
612 /// Returns data-sharing attributes for the specified declaration.
613 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
614 /// Returns data-sharing attributes for the specified declaration.
615 const DSAVarData getImplicitDSA(ValueDecl *D, unsigned Level) const;
616 /// Checks if the specified variables has data-sharing attributes which
617 /// match specified \a CPred predicate in any directive which matches \a DPred
618 /// predicate.
619 const DSAVarData
620 hasDSA(ValueDecl *D,
621 const llvm::function_ref<bool(OpenMPClauseKind, bool,
622 DefaultDataSharingAttributes)>
623 CPred,
624 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
625 bool FromParent) const;
626 /// Checks if the specified variables has data-sharing attributes which
627 /// match specified \a CPred predicate in any innermost directive which
628 /// matches \a DPred predicate.
629 const DSAVarData
630 hasInnermostDSA(ValueDecl *D,
631 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
632 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
633 bool FromParent) const;
634 /// Checks if the specified variables has explicit data-sharing
635 /// attributes which match specified \a CPred predicate at the specified
636 /// OpenMP region.
637 bool
638 hasExplicitDSA(const ValueDecl *D,
639 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
640 unsigned Level, bool NotLastprivate = false) const;
641
642 /// Returns true if the directive at level \Level matches in the
643 /// specified \a DPred predicate.
644 bool hasExplicitDirective(
645 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
646 unsigned Level) const;
647
648 /// Finds a directive which matches specified \a DPred predicate.
649 bool hasDirective(
650 const llvm::function_ref<bool(
651 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
652 DPred,
653 bool FromParent) const;
654
655 /// Returns currently analyzed directive.
656 OpenMPDirectiveKind getCurrentDirective() const {
657 const SharingMapTy *Top = getTopOfStackOrNull();
658 return Top ? Top->Directive : OMPD_unknown;
659 }
660 /// Returns directive kind at specified level.
661 OpenMPDirectiveKind getDirective(unsigned Level) const {
662 assert(!isStackEmpty() && "No directive at specified level.");
663 return getStackElemAtLevel(Level).Directive;
664 }
665 /// Returns the capture region at the specified level.
666 OpenMPDirectiveKind getCaptureRegion(unsigned Level,
667 unsigned OpenMPCaptureLevel) const {
668 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
669 getOpenMPCaptureRegions(CaptureRegions, DKind: getDirective(Level));
670 return CaptureRegions[OpenMPCaptureLevel];
671 }
672 /// Returns parent directive.
673 OpenMPDirectiveKind getParentDirective() const {
674 const SharingMapTy *Parent = getSecondOnStackOrNull();
675 return Parent ? Parent->Directive : OMPD_unknown;
676 }
677
678 /// Add requires decl to internal vector
679 void addRequiresDecl(OMPRequiresDecl *RD) { RequiresDecls.push_back(Elt: RD); }
680
681 /// Checks if the defined 'requires' directive has specified type of clause.
682 template <typename ClauseType> bool hasRequiresDeclWithClause() const {
683 return llvm::any_of(RequiresDecls, [](const OMPRequiresDecl *D) {
684 return llvm::any_of(D->clauselists(), [](const OMPClause *C) {
685 return isa<ClauseType>(C);
686 });
687 });
688 }
689
690 /// Checks for a duplicate clause amongst previously declared requires
691 /// directives
692 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
693 bool IsDuplicate = false;
694 for (OMPClause *CNew : ClauseList) {
695 for (const OMPRequiresDecl *D : RequiresDecls) {
696 for (const OMPClause *CPrev : D->clauselists()) {
697 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
698 SemaRef.Diag(Loc: CNew->getBeginLoc(),
699 DiagID: diag::err_omp_requires_clause_redeclaration)
700 << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
701 SemaRef.Diag(Loc: CPrev->getBeginLoc(),
702 DiagID: diag::note_omp_requires_previous_clause)
703 << getOpenMPClauseNameForDiag(C: CPrev->getClauseKind());
704 IsDuplicate = true;
705 }
706 }
707 }
708 }
709 return IsDuplicate;
710 }
711
712 /// Add location of previously encountered target to internal vector
713 void addTargetDirLocation(SourceLocation LocStart) {
714 TargetLocations.push_back(Elt: LocStart);
715 }
716
717 /// Add location for the first encountered atomic directive.
718 void addAtomicDirectiveLoc(SourceLocation Loc) {
719 if (AtomicLocation.isInvalid())
720 AtomicLocation = Loc;
721 }
722
723 /// Returns the location of the first encountered atomic directive in the
724 /// module.
725 SourceLocation getAtomicDirectiveLoc() const { return AtomicLocation; }
726
727 // Return previously encountered target region locations.
728 ArrayRef<SourceLocation> getEncounteredTargetLocs() const {
729 return TargetLocations;
730 }
731
732 /// Set default data sharing attribute to none.
733 void setDefaultDSANone(SourceLocation Loc) {
734 getTopOfStack().DefaultAttr = DSA_none;
735 getTopOfStack().DefaultAttrLoc = Loc;
736 }
737 /// Set default data sharing attribute to shared.
738 void setDefaultDSAShared(SourceLocation Loc) {
739 getTopOfStack().DefaultAttr = DSA_shared;
740 getTopOfStack().DefaultAttrLoc = Loc;
741 }
742 /// Set default data sharing attribute to private.
743 void setDefaultDSAPrivate(SourceLocation Loc) {
744 getTopOfStack().DefaultAttr = DSA_private;
745 getTopOfStack().DefaultAttrLoc = Loc;
746 }
747 /// Set default data sharing attribute to firstprivate.
748 void setDefaultDSAFirstPrivate(SourceLocation Loc) {
749 getTopOfStack().DefaultAttr = DSA_firstprivate;
750 getTopOfStack().DefaultAttrLoc = Loc;
751 }
752 /// Set default data sharing variable category attribute to aggregate.
753 void setDefaultDSAVCAggregate(SourceLocation VCLoc) {
754 getTopOfStack().DefaultVCAttr = DSA_VC_aggregate;
755 getTopOfStack().DefaultAttrVCLoc = VCLoc;
756 }
757 /// Set default data sharing variable category attribute to all.
758 void setDefaultDSAVCAll(SourceLocation VCLoc) {
759 getTopOfStack().DefaultVCAttr = DSA_VC_all;
760 getTopOfStack().DefaultAttrVCLoc = VCLoc;
761 }
762 /// Set default data sharing variable category attribute to pointer.
763 void setDefaultDSAVCPointer(SourceLocation VCLoc) {
764 getTopOfStack().DefaultVCAttr = DSA_VC_pointer;
765 getTopOfStack().DefaultAttrVCLoc = VCLoc;
766 }
767 /// Set default data sharing variable category attribute to scalar.
768 void setDefaultDSAVCScalar(SourceLocation VCLoc) {
769 getTopOfStack().DefaultVCAttr = DSA_VC_scalar;
770 getTopOfStack().DefaultAttrVCLoc = VCLoc;
771 }
772 /// Set default data mapping attribute to Modifier:Kind
773 void setDefaultDMAAttr(OpenMPDefaultmapClauseModifier M,
774 OpenMPDefaultmapClauseKind Kind, SourceLocation Loc) {
775 DefaultmapInfo &DMI = getTopOfStack().DefaultmapMap[Kind];
776 DMI.ImplicitBehavior = M;
777 DMI.SLoc = Loc;
778 }
779 /// Check whether the implicit-behavior has been set in defaultmap
780 bool checkDefaultmapCategory(OpenMPDefaultmapClauseKind VariableCategory) {
781 if (VariableCategory == OMPC_DEFAULTMAP_unknown)
782 return getTopOfStack()
783 .DefaultmapMap[OMPC_DEFAULTMAP_aggregate]
784 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
785 getTopOfStack()
786 .DefaultmapMap[OMPC_DEFAULTMAP_scalar]
787 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown ||
788 getTopOfStack()
789 .DefaultmapMap[OMPC_DEFAULTMAP_pointer]
790 .ImplicitBehavior != OMPC_DEFAULTMAP_MODIFIER_unknown;
791 return getTopOfStack().DefaultmapMap[VariableCategory].ImplicitBehavior !=
792 OMPC_DEFAULTMAP_MODIFIER_unknown;
793 }
794
795 ArrayRef<llvm::omp::TraitProperty> getConstructTraits() {
796 return ConstructTraits;
797 }
798 void handleConstructTrait(ArrayRef<llvm::omp::TraitProperty> Traits,
799 bool ScopeEntry) {
800 if (ScopeEntry)
801 ConstructTraits.append(in_start: Traits.begin(), in_end: Traits.end());
802 else
803 for (llvm::omp::TraitProperty Trait : llvm::reverse(C&: Traits)) {
804 llvm::omp::TraitProperty Top = ConstructTraits.pop_back_val();
805 assert(Top == Trait && "Something left a trait on the stack!");
806 (void)Trait;
807 (void)Top;
808 }
809 }
810
811 DefaultDataSharingAttributes getDefaultDSA(unsigned Level) const {
812 return getStackSize() <= Level ? DSA_unspecified
813 : getStackElemAtLevel(Level).DefaultAttr;
814 }
815 DefaultDataSharingAttributes getDefaultDSA() const {
816 return isStackEmpty() ? DSA_unspecified : getTopOfStack().DefaultAttr;
817 }
818 SourceLocation getDefaultDSALocation() const {
819 return isStackEmpty() ? SourceLocation() : getTopOfStack().DefaultAttrLoc;
820 }
821 OpenMPDefaultmapClauseModifier
822 getDefaultmapModifier(OpenMPDefaultmapClauseKind Kind) const {
823 return isStackEmpty()
824 ? OMPC_DEFAULTMAP_MODIFIER_unknown
825 : getTopOfStack().DefaultmapMap[Kind].ImplicitBehavior;
826 }
827 OpenMPDefaultmapClauseModifier
828 getDefaultmapModifierAtLevel(unsigned Level,
829 OpenMPDefaultmapClauseKind Kind) const {
830 return getStackElemAtLevel(Level).DefaultmapMap[Kind].ImplicitBehavior;
831 }
832 bool isDefaultmapCapturedByRef(unsigned Level,
833 OpenMPDefaultmapClauseKind Kind) const {
834 OpenMPDefaultmapClauseModifier M =
835 getDefaultmapModifierAtLevel(Level, Kind);
836 if (Kind == OMPC_DEFAULTMAP_scalar || Kind == OMPC_DEFAULTMAP_pointer) {
837 return (M == OMPC_DEFAULTMAP_MODIFIER_alloc) ||
838 (M == OMPC_DEFAULTMAP_MODIFIER_to) ||
839 (M == OMPC_DEFAULTMAP_MODIFIER_from) ||
840 (M == OMPC_DEFAULTMAP_MODIFIER_tofrom) ||
841 (M == OMPC_DEFAULTMAP_MODIFIER_present) ||
842 (M == OMPC_DEFAULTMAP_MODIFIER_storage);
843 }
844 return true;
845 }
846 static bool mustBeFirstprivateBase(OpenMPDefaultmapClauseModifier M,
847 OpenMPDefaultmapClauseKind Kind) {
848 switch (Kind) {
849 case OMPC_DEFAULTMAP_scalar:
850 case OMPC_DEFAULTMAP_pointer:
851 return (M == OMPC_DEFAULTMAP_MODIFIER_unknown) ||
852 (M == OMPC_DEFAULTMAP_MODIFIER_firstprivate) ||
853 (M == OMPC_DEFAULTMAP_MODIFIER_default);
854 case OMPC_DEFAULTMAP_aggregate:
855 return M == OMPC_DEFAULTMAP_MODIFIER_firstprivate;
856 default:
857 break;
858 }
859 llvm_unreachable("Unexpected OpenMPDefaultmapClauseKind enum");
860 }
861 bool mustBeFirstprivateAtLevel(unsigned Level,
862 OpenMPDefaultmapClauseKind Kind) const {
863 OpenMPDefaultmapClauseModifier M =
864 getDefaultmapModifierAtLevel(Level, Kind);
865 return mustBeFirstprivateBase(M, Kind);
866 }
867 bool mustBeFirstprivate(OpenMPDefaultmapClauseKind Kind) const {
868 OpenMPDefaultmapClauseModifier M = getDefaultmapModifier(Kind);
869 return mustBeFirstprivateBase(M, Kind);
870 }
871
872 /// Checks if the specified variable is a threadprivate.
873 bool isThreadPrivate(VarDecl *D) {
874 const DSAVarData DVar = getTopDSA(D, FromParent: false);
875 return isOpenMPThreadPrivate(Kind: DVar.CKind);
876 }
877
878 /// Marks current region as ordered (it has an 'ordered' clause).
879 void setOrderedRegion(bool IsOrdered, const Expr *Param,
880 OMPOrderedClause *Clause) {
881 if (IsOrdered)
882 getTopOfStack().OrderedRegion.emplace(args&: Param, args&: Clause);
883 else
884 getTopOfStack().OrderedRegion.reset();
885 }
886 /// Returns true, if region is ordered (has associated 'ordered' clause),
887 /// false - otherwise.
888 bool isOrderedRegion() const {
889 if (const SharingMapTy *Top = getTopOfStackOrNull())
890 return Top->OrderedRegion.has_value();
891 return false;
892 }
893 /// Returns optional parameter for the ordered region.
894 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
895 if (const SharingMapTy *Top = getTopOfStackOrNull())
896 if (Top->OrderedRegion)
897 return *Top->OrderedRegion;
898 return std::make_pair(x: nullptr, y: nullptr);
899 }
900 /// Returns true, if parent region is ordered (has associated
901 /// 'ordered' clause), false - otherwise.
902 bool isParentOrderedRegion() const {
903 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
904 return Parent->OrderedRegion.has_value();
905 return false;
906 }
907 /// Returns optional parameter for the ordered region.
908 std::pair<const Expr *, OMPOrderedClause *>
909 getParentOrderedRegionParam() const {
910 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
911 if (Parent->OrderedRegion)
912 return *Parent->OrderedRegion;
913 return std::make_pair(x: nullptr, y: nullptr);
914 }
915 /// Marks current region as having an 'order' clause.
916 void setRegionHasOrderConcurrent(bool HasOrderConcurrent) {
917 getTopOfStack().RegionHasOrderConcurrent = HasOrderConcurrent;
918 }
919 /// Returns true, if parent region is order (has associated
920 /// 'order' clause), false - otherwise.
921 bool isParentOrderConcurrent() const {
922 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
923 return Parent->RegionHasOrderConcurrent;
924 return false;
925 }
926 /// Marks current region as nowait (it has a 'nowait' clause).
927 void setNowaitRegion(bool IsNowait = true) {
928 getTopOfStack().NowaitRegion = IsNowait;
929 }
930 /// Returns true, if parent region is nowait (has associated
931 /// 'nowait' clause), false - otherwise.
932 bool isParentNowaitRegion() const {
933 if (const SharingMapTy *Parent = getSecondOnStackOrNull())
934 return Parent->NowaitRegion;
935 return false;
936 }
937 /// Marks current region as untied (it has a 'untied' clause).
938 void setUntiedRegion(bool IsUntied = true) {
939 getTopOfStack().UntiedRegion = IsUntied;
940 }
941 /// Return true if current region is untied.
942 bool isUntiedRegion() const {
943 const SharingMapTy *Top = getTopOfStackOrNull();
944 return Top ? Top->UntiedRegion : false;
945 }
946 /// Marks parent region as cancel region.
947 void setParentCancelRegion(bool Cancel = true) {
948 if (SharingMapTy *Parent = getSecondOnStackOrNull())
949 Parent->CancelRegion |= Cancel;
950 }
951 /// Return true if current region has inner cancel construct.
952 bool isCancelRegion() const {
953 const SharingMapTy *Top = getTopOfStackOrNull();
954 return Top ? Top->CancelRegion : false;
955 }
956
957 /// Mark that parent region already has scan directive.
958 void setParentHasScanDirective(SourceLocation Loc) {
959 if (SharingMapTy *Parent = getSecondOnStackOrNull())
960 Parent->PrevScanLocation = Loc;
961 }
962 /// Return true if current region has inner cancel construct.
963 bool doesParentHasScanDirective() const {
964 const SharingMapTy *Top = getSecondOnStackOrNull();
965 return Top ? Top->PrevScanLocation.isValid() : false;
966 }
967 /// Return true if current region has inner cancel construct.
968 SourceLocation getParentScanDirectiveLoc() const {
969 const SharingMapTy *Top = getSecondOnStackOrNull();
970 return Top ? Top->PrevScanLocation : SourceLocation();
971 }
972 /// Mark that parent region already has ordered directive.
973 void setParentHasOrderedDirective(SourceLocation Loc) {
974 if (SharingMapTy *Parent = getSecondOnStackOrNull())
975 Parent->PrevOrderedLocation = Loc;
976 }
977 /// Return true if current region has inner ordered construct.
978 bool doesParentHasOrderedDirective() const {
979 const SharingMapTy *Top = getSecondOnStackOrNull();
980 return Top ? Top->PrevOrderedLocation.isValid() : false;
981 }
982 /// Returns the location of the previously specified ordered directive.
983 SourceLocation getParentOrderedDirectiveLoc() const {
984 const SharingMapTy *Top = getSecondOnStackOrNull();
985 return Top ? Top->PrevOrderedLocation : SourceLocation();
986 }
987
988 /// Set collapse value for the region.
989 void setAssociatedLoops(unsigned Val) {
990 getTopOfStack().AssociatedLoops = Val;
991 if (Val > 1)
992 getTopOfStack().HasMutipleLoops = true;
993 }
994 /// Return collapse value for region.
995 unsigned getAssociatedLoops() const {
996 const SharingMapTy *Top = getTopOfStackOrNull();
997 return Top ? Top->AssociatedLoops : 0;
998 }
999 /// Returns true if the construct is associated with multiple loops.
1000 bool hasMutipleLoops() const {
1001 const SharingMapTy *Top = getTopOfStackOrNull();
1002 return Top ? Top->HasMutipleLoops : false;
1003 }
1004
1005 /// Marks current target region as one with closely nested teams
1006 /// region.
1007 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
1008 if (SharingMapTy *Parent = getSecondOnStackOrNull())
1009 Parent->InnerTeamsRegionLoc = TeamsRegionLoc;
1010 }
1011 /// Returns true, if current region has closely nested teams region.
1012 bool hasInnerTeamsRegion() const {
1013 return getInnerTeamsRegionLoc().isValid();
1014 }
1015 /// Returns location of the nested teams region (if any).
1016 SourceLocation getInnerTeamsRegionLoc() const {
1017 const SharingMapTy *Top = getTopOfStackOrNull();
1018 return Top ? Top->InnerTeamsRegionLoc : SourceLocation();
1019 }
1020
1021 Scope *getCurScope() const {
1022 const SharingMapTy *Top = getTopOfStackOrNull();
1023 return Top ? Top->CurScope : nullptr;
1024 }
1025 void setContext(DeclContext *DC) { getTopOfStack().Context = DC; }
1026 SourceLocation getConstructLoc() const {
1027 const SharingMapTy *Top = getTopOfStackOrNull();
1028 return Top ? Top->ConstructLoc : SourceLocation();
1029 }
1030
1031 /// Do the check specified in \a Check to all component lists and return true
1032 /// if any issue is found.
1033 bool checkMappableExprComponentListsForDecl(
1034 const ValueDecl *VD, bool CurrentRegionOnly,
1035 const llvm::function_ref<
1036 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1037 OpenMPClauseKind)>
1038 Check) const {
1039 if (isStackEmpty())
1040 return false;
1041 auto SI = begin();
1042 auto SE = end();
1043
1044 if (SI == SE)
1045 return false;
1046
1047 if (CurrentRegionOnly)
1048 SE = std::next(x: SI);
1049 else
1050 std::advance(i&: SI, n: 1);
1051
1052 for (; SI != SE; ++SI) {
1053 auto MI = SI->MappedExprComponents.find(Val: VD);
1054 if (MI != SI->MappedExprComponents.end())
1055 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1056 MI->second.Components)
1057 if (Check(L, MI->second.Kind))
1058 return true;
1059 }
1060 return false;
1061 }
1062
1063 /// Do the check specified in \a Check to all component lists at a given level
1064 /// and return true if any issue is found.
1065 bool checkMappableExprComponentListsForDeclAtLevel(
1066 const ValueDecl *VD, unsigned Level,
1067 const llvm::function_ref<
1068 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
1069 OpenMPClauseKind)>
1070 Check) const {
1071 if (getStackSize() <= Level)
1072 return false;
1073
1074 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
1075 auto MI = StackElem.MappedExprComponents.find(Val: VD);
1076 if (MI != StackElem.MappedExprComponents.end())
1077 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
1078 MI->second.Components)
1079 if (Check(L, MI->second.Kind))
1080 return true;
1081 return false;
1082 }
1083
1084 /// Create a new mappable expression component list associated with a given
1085 /// declaration and initialize it with the provided list of components.
1086 void addMappableExpressionComponents(
1087 const ValueDecl *VD,
1088 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
1089 OpenMPClauseKind WhereFoundClauseKind) {
1090 MappedExprComponentTy &MEC = getTopOfStack().MappedExprComponents[VD];
1091 // Create new entry and append the new components there.
1092 MEC.Components.resize(N: MEC.Components.size() + 1);
1093 MEC.Components.back().append(in_start: Components.begin(), in_end: Components.end());
1094 MEC.Kind = WhereFoundClauseKind;
1095 }
1096
1097 unsigned getNestingLevel() const {
1098 assert(!isStackEmpty());
1099 return getStackSize() - 1;
1100 }
1101 void addDoacrossDependClause(OMPClause *C, const OperatorOffsetTy &OpsOffs) {
1102 SharingMapTy *Parent = getSecondOnStackOrNull();
1103 assert(Parent && isOpenMPWorksharingDirective(Parent->Directive));
1104 Parent->DoacrossDepends.try_emplace(Key: C, Args: OpsOffs);
1105 }
1106 llvm::iterator_range<DoacrossClauseMapTy::const_iterator>
1107 getDoacrossDependClauses() const {
1108 const SharingMapTy &StackElem = getTopOfStack();
1109 if (isOpenMPWorksharingDirective(DKind: StackElem.Directive)) {
1110 const DoacrossClauseMapTy &Ref = StackElem.DoacrossDepends;
1111 return llvm::make_range(x: Ref.begin(), y: Ref.end());
1112 }
1113 return llvm::make_range(x: StackElem.DoacrossDepends.end(),
1114 y: StackElem.DoacrossDepends.end());
1115 }
1116
1117 // Store types of classes which have been explicitly mapped
1118 void addMappedClassesQualTypes(QualType QT) {
1119 SharingMapTy &StackElem = getTopOfStack();
1120 StackElem.MappedClassesQualTypes.insert(V: QT);
1121 }
1122
1123 // Return set of mapped classes types
1124 bool isClassPreviouslyMapped(QualType QT) const {
1125 const SharingMapTy &StackElem = getTopOfStack();
1126 return StackElem.MappedClassesQualTypes.contains(V: QT);
1127 }
1128
1129 /// Adds global declare target to the parent target region.
1130 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
1131 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1132 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
1133 "Expected declare target link global.");
1134 for (auto &Elem : *this) {
1135 if (isOpenMPTargetExecutionDirective(DKind: Elem.Directive)) {
1136 Elem.DeclareTargetLinkVarDecls.push_back(Elt: E);
1137 return;
1138 }
1139 }
1140 }
1141
1142 /// Returns the list of globals with declare target link if current directive
1143 /// is target.
1144 ArrayRef<DeclRefExpr *> getLinkGlobals() const {
1145 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
1146 "Expected target executable directive.");
1147 return getTopOfStack().DeclareTargetLinkVarDecls;
1148 }
1149
1150 /// Adds list of allocators expressions.
1151 void addInnerAllocatorExpr(Expr *E) {
1152 getTopOfStack().InnerUsedAllocators.push_back(Elt: E);
1153 }
1154 /// Return list of used allocators.
1155 ArrayRef<Expr *> getInnerAllocators() const {
1156 return getTopOfStack().InnerUsedAllocators;
1157 }
1158 /// Marks the declaration as implicitly firstprivate nin the task-based
1159 /// regions.
1160 void addImplicitTaskFirstprivate(unsigned Level, Decl *D) {
1161 getStackElemAtLevel(Level).ImplicitTaskFirstprivates.insert(V: D);
1162 }
1163 /// Checks if the decl is implicitly firstprivate in the task-based region.
1164 bool isImplicitTaskFirstprivate(Decl *D) const {
1165 return getTopOfStack().ImplicitTaskFirstprivates.contains(V: D);
1166 }
1167
1168 /// Marks decl as used in uses_allocators clause as the allocator.
1169 void addUsesAllocatorsDecl(const Decl *D, UsesAllocatorsDeclKind Kind) {
1170 getTopOfStack().UsesAllocatorsDecls.try_emplace(Key: D, Args&: Kind);
1171 }
1172 /// Checks if specified decl is used in uses allocator clause as the
1173 /// allocator.
1174 std::optional<UsesAllocatorsDeclKind>
1175 isUsesAllocatorsDecl(unsigned Level, const Decl *D) const {
1176 const SharingMapTy &StackElem = getTopOfStack();
1177 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1178 if (I == StackElem.UsesAllocatorsDecls.end())
1179 return std::nullopt;
1180 return I->getSecond();
1181 }
1182 std::optional<UsesAllocatorsDeclKind>
1183 isUsesAllocatorsDecl(const Decl *D) const {
1184 const SharingMapTy &StackElem = getTopOfStack();
1185 auto I = StackElem.UsesAllocatorsDecls.find(Val: D);
1186 if (I == StackElem.UsesAllocatorsDecls.end())
1187 return std::nullopt;
1188 return I->getSecond();
1189 }
1190
1191 void addDeclareMapperVarRef(Expr *Ref) {
1192 SharingMapTy &StackElem = getTopOfStack();
1193 StackElem.DeclareMapperVar = Ref;
1194 }
1195 const Expr *getDeclareMapperVarRef() const {
1196 const SharingMapTy *Top = getTopOfStackOrNull();
1197 return Top ? Top->DeclareMapperVar : nullptr;
1198 }
1199
1200 /// Add a new iterator variable.
1201 void addIteratorVarDecl(VarDecl *VD) {
1202 SharingMapTy &StackElem = getTopOfStack();
1203 StackElem.IteratorVarDecls.push_back(Elt: VD->getCanonicalDecl());
1204 }
1205 /// Check if variable declaration is an iterator VarDecl.
1206 bool isIteratorVarDecl(const VarDecl *VD) const {
1207 const SharingMapTy *Top = getTopOfStackOrNull();
1208 if (!Top)
1209 return false;
1210
1211 return llvm::is_contained(Range: Top->IteratorVarDecls, Element: VD->getCanonicalDecl());
1212 }
1213 /// get captured field from ImplicitDefaultFirstprivateFDs
1214 VarDecl *getImplicitFDCapExprDecl(const FieldDecl *FD) const {
1215 const_iterator I = begin();
1216 const_iterator EndI = end();
1217 size_t StackLevel = getStackSize();
1218 for (; I != EndI; ++I) {
1219 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1220 break;
1221 StackLevel--;
1222 }
1223 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1224 if (I == EndI)
1225 return nullptr;
1226 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1227 if (IFD.FD == FD && IFD.StackLevel == StackLevel)
1228 return IFD.VD;
1229 return nullptr;
1230 }
1231 /// Check if capture decl is field captured in ImplicitDefaultFirstprivateFDs
1232 bool isImplicitDefaultFirstprivateFD(VarDecl *VD) const {
1233 const_iterator I = begin();
1234 const_iterator EndI = end();
1235 for (; I != EndI; ++I)
1236 if (I->DefaultAttr == DSA_firstprivate || I->DefaultAttr == DSA_private)
1237 break;
1238 if (I == EndI)
1239 return false;
1240 for (const auto &IFD : I->ImplicitDefaultFirstprivateFDs)
1241 if (IFD.VD == VD)
1242 return true;
1243 return false;
1244 }
1245 /// Store capture FD info in ImplicitDefaultFirstprivateFDs
1246 void addImplicitDefaultFirstprivateFD(const FieldDecl *FD, VarDecl *VD) {
1247 iterator I = begin();
1248 const_iterator EndI = end();
1249 size_t StackLevel = getStackSize();
1250 for (; I != EndI; ++I) {
1251 if (I->DefaultAttr == DSA_private || I->DefaultAttr == DSA_firstprivate) {
1252 I->ImplicitDefaultFirstprivateFDs.emplace_back(Args&: FD, Args&: StackLevel, Args&: VD);
1253 break;
1254 }
1255 StackLevel--;
1256 }
1257 assert((StackLevel > 0 && I != EndI) || (StackLevel == 0 && I == EndI));
1258 }
1259};
1260
1261bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1262 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
1263}
1264
1265bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
1266 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(Kind: DKind) ||
1267 DKind == OMPD_unknown;
1268}
1269
1270} // namespace
1271
1272static const Expr *getExprAsWritten(const Expr *E) {
1273 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
1274 E = FE->getSubExpr();
1275
1276 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Val: E))
1277 E = MTE->getSubExpr();
1278
1279 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(Val: E))
1280 E = Binder->getSubExpr();
1281
1282 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E))
1283 E = ICE->getSubExprAsWritten();
1284 return E->IgnoreParens();
1285}
1286
1287static Expr *getExprAsWritten(Expr *E) {
1288 return const_cast<Expr *>(getExprAsWritten(E: const_cast<const Expr *>(E)));
1289}
1290
1291static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
1292 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: D))
1293 if (const auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
1294 D = ME->getMemberDecl();
1295
1296 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
1297 return D;
1298}
1299
1300static ValueDecl *getCanonicalDecl(ValueDecl *D) {
1301 return const_cast<ValueDecl *>(
1302 getCanonicalDecl(D: const_cast<const ValueDecl *>(D)));
1303}
1304
1305static std::string getOpenMPClauseNameForDiag(OpenMPClauseKind C) {
1306 if (C == OMPC_threadprivate)
1307 return getOpenMPClauseName(C).str() + " or thread local";
1308 return getOpenMPClauseName(C).str();
1309}
1310
1311DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
1312 ValueDecl *D) const {
1313 D = getCanonicalDecl(D);
1314 auto *VD = dyn_cast<VarDecl>(Val: D);
1315 const auto *FD = dyn_cast<FieldDecl>(Val: D);
1316 DSAVarData DVar;
1317 if (Iter == end()) {
1318 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1319 // in a region but not in construct]
1320 // File-scope or namespace-scope variables referenced in called routines
1321 // in the region are shared unless they appear in a threadprivate
1322 // directive.
1323 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(Val: VD))
1324 DVar.CKind = OMPC_shared;
1325
1326 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
1327 // in a region but not in construct]
1328 // Variables with static storage duration that are declared in called
1329 // routines in the region are shared.
1330 if (VD && VD->hasGlobalStorage())
1331 DVar.CKind = OMPC_shared;
1332
1333 // Non-static data members are shared by default.
1334 if (FD)
1335 DVar.CKind = OMPC_shared;
1336
1337 return DVar;
1338 }
1339
1340 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1341 // in a Construct, C/C++, predetermined, p.1]
1342 // Variables with automatic storage duration that are declared in a scope
1343 // inside the construct are private.
1344 if (VD && isOpenMPLocal(D: VD, Iter) && VD->isLocalVarDecl() &&
1345 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
1346 DVar.CKind = OMPC_private;
1347 return DVar;
1348 }
1349
1350 DVar.DKind = Iter->Directive;
1351 // Explicitly specified attributes and local variables with predetermined
1352 // attributes.
1353 if (Iter->SharingMap.count(Val: D)) {
1354 const DSAInfo &Data = Iter->SharingMap.lookup(Val: D);
1355 DVar.RefExpr = Data.RefExpr.getPointer();
1356 DVar.PrivateCopy = Data.PrivateCopy;
1357 DVar.CKind = Data.Attributes;
1358 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1359 DVar.Modifier = Data.Modifier;
1360 DVar.AppliedToPointee = Data.AppliedToPointee;
1361 return DVar;
1362 }
1363
1364 DefaultDataSharingAttributes IterDA = Iter->DefaultAttr;
1365 switch (Iter->DefaultVCAttr) {
1366 case DSA_VC_aggregate:
1367 if (!D->getType()->isAggregateType())
1368 IterDA = DSA_none;
1369 break;
1370 case DSA_VC_pointer:
1371 if (!D->getType()->isPointerType())
1372 IterDA = DSA_none;
1373 break;
1374 case DSA_VC_scalar:
1375 if (!D->getType()->isScalarType())
1376 IterDA = DSA_none;
1377 break;
1378 case DSA_VC_all:
1379 break;
1380 }
1381
1382 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1383 // in a Construct, C/C++, implicitly determined, p.1]
1384 // In a parallel or task construct, the data-sharing attributes of these
1385 // variables are determined by the default clause, if present.
1386 switch (IterDA) {
1387 case DSA_shared:
1388 DVar.CKind = OMPC_shared;
1389 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1390 return DVar;
1391 case DSA_none:
1392 return DVar;
1393 case DSA_firstprivate:
1394 if (VD && VD->getStorageDuration() == SD_Static &&
1395 VD->getDeclContext()->isFileContext()) {
1396 DVar.CKind = OMPC_unknown;
1397 } else {
1398 DVar.CKind = OMPC_firstprivate;
1399 }
1400 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1401 return DVar;
1402 case DSA_private:
1403 // each variable with static storage duration that is declared
1404 // in a namespace or global scope and referenced in the construct,
1405 // and that does not have a predetermined data-sharing attribute
1406 if (VD && VD->getStorageDuration() == SD_Static &&
1407 VD->getDeclContext()->isFileContext()) {
1408 DVar.CKind = OMPC_unknown;
1409 } else {
1410 DVar.CKind = OMPC_private;
1411 }
1412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1413 return DVar;
1414 case DSA_unspecified:
1415 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1416 // in a Construct, implicitly determined, p.2]
1417 // In a parallel construct, if no default clause is present, these
1418 // variables are shared.
1419 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
1420 if ((isOpenMPParallelDirective(DKind: DVar.DKind) &&
1421 !isOpenMPTaskLoopDirective(DKind: DVar.DKind)) ||
1422 isOpenMPTeamsDirective(DKind: DVar.DKind)) {
1423 DVar.CKind = OMPC_shared;
1424 return DVar;
1425 }
1426
1427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1428 // in a Construct, implicitly determined, p.4]
1429 // In a task construct, if no default clause is present, a variable that in
1430 // the enclosing context is determined to be shared by all implicit tasks
1431 // bound to the current team is shared.
1432 if (isOpenMPTaskingDirective(Kind: DVar.DKind)) {
1433 DSAVarData DVarTemp;
1434 const_iterator I = Iter, E = end();
1435 do {
1436 ++I;
1437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
1438 // Referenced in a Construct, implicitly determined, p.6]
1439 // In a task construct, if no default clause is present, a variable
1440 // whose data-sharing attribute is not determined by the rules above is
1441 // firstprivate.
1442 DVarTemp = getDSA(Iter&: I, D);
1443 if (DVarTemp.CKind != OMPC_shared) {
1444 DVar.RefExpr = nullptr;
1445 DVar.CKind = OMPC_firstprivate;
1446 return DVar;
1447 }
1448 } while (I != E && !isImplicitTaskingRegion(DKind: I->Directive));
1449 DVar.CKind =
1450 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
1451 return DVar;
1452 }
1453 }
1454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1455 // in a Construct, implicitly determined, p.3]
1456 // For constructs other than task, if no default clause is present, these
1457 // variables inherit their data-sharing attributes from the enclosing
1458 // context.
1459 return getDSA(Iter&: ++Iter, D);
1460}
1461
1462const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
1463 const Expr *NewDE) {
1464 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1465 D = getCanonicalDecl(D);
1466 SharingMapTy &StackElem = getTopOfStack();
1467 auto [It, Inserted] = StackElem.AlignedMap.try_emplace(Key: D, Args&: NewDE);
1468 if (Inserted) {
1469 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1470 return nullptr;
1471 }
1472 assert(It->second && "Unexpected nullptr expr in the aligned map");
1473 return It->second;
1474}
1475
1476const Expr *DSAStackTy::addUniqueNontemporal(const ValueDecl *D,
1477 const Expr *NewDE) {
1478 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
1479 D = getCanonicalDecl(D);
1480 SharingMapTy &StackElem = getTopOfStack();
1481 auto [It, Inserted] = StackElem.NontemporalMap.try_emplace(Key: D, Args&: NewDE);
1482 if (Inserted) {
1483 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
1484 return nullptr;
1485 }
1486 assert(It->second && "Unexpected nullptr expr in the aligned map");
1487 return It->second;
1488}
1489
1490void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
1491 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1492 D = getCanonicalDecl(D);
1493 SharingMapTy &StackElem = getTopOfStack();
1494 StackElem.LCVMap.try_emplace(
1495 Key: D, Args: LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
1496}
1497
1498const DSAStackTy::LCDeclInfo
1499DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
1500 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1501 D = getCanonicalDecl(D);
1502 const SharingMapTy &StackElem = getTopOfStack();
1503 auto It = StackElem.LCVMap.find(Val: D);
1504 if (It != StackElem.LCVMap.end())
1505 return It->second;
1506 return {0, nullptr};
1507}
1508
1509const DSAStackTy::LCDeclInfo
1510DSAStackTy::isLoopControlVariable(const ValueDecl *D, unsigned Level) const {
1511 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1512 D = getCanonicalDecl(D);
1513 for (unsigned I = Level + 1; I > 0; --I) {
1514 const SharingMapTy &StackElem = getStackElemAtLevel(Level: I - 1);
1515 auto It = StackElem.LCVMap.find(Val: D);
1516 if (It != StackElem.LCVMap.end())
1517 return It->second;
1518 }
1519 return {0, nullptr};
1520}
1521
1522const DSAStackTy::LCDeclInfo
1523DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
1524 const SharingMapTy *Parent = getSecondOnStackOrNull();
1525 assert(Parent && "Data-sharing attributes stack is empty");
1526 D = getCanonicalDecl(D);
1527 auto It = Parent->LCVMap.find(Val: D);
1528 if (It != Parent->LCVMap.end())
1529 return It->second;
1530 return {0, nullptr};
1531}
1532
1533const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
1534 const SharingMapTy *Parent = getSecondOnStackOrNull();
1535 assert(Parent && "Data-sharing attributes stack is empty");
1536 if (Parent->LCVMap.size() < I)
1537 return nullptr;
1538 for (const auto &Pair : Parent->LCVMap)
1539 if (Pair.second.first == I)
1540 return Pair.first;
1541 return nullptr;
1542}
1543
1544void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
1545 DeclRefExpr *PrivateCopy, unsigned Modifier,
1546 bool AppliedToPointee) {
1547 D = getCanonicalDecl(D);
1548 if (A == OMPC_threadprivate) {
1549 DSAInfo &Data = Threadprivates[D];
1550 Data.Attributes = A;
1551 Data.RefExpr.setPointer(E);
1552 Data.PrivateCopy = nullptr;
1553 Data.Modifier = Modifier;
1554 } else if (A == OMPC_groupprivate) {
1555 DSAInfo &Data = Groupprivates[D];
1556 Data.Attributes = A;
1557 Data.RefExpr.setPointer(E);
1558 Data.PrivateCopy = nullptr;
1559 Data.Modifier = Modifier;
1560 } else {
1561 DSAInfo &Data = getTopOfStack().SharingMap[D];
1562 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
1563 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
1564 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
1565 (isLoopControlVariable(D).first && A == OMPC_private));
1566 Data.Modifier = Modifier;
1567 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
1568 Data.RefExpr.setInt(/*IntVal=*/true);
1569 return;
1570 }
1571 const bool IsLastprivate =
1572 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
1573 Data.Attributes = A;
1574 Data.RefExpr.setPointerAndInt(PtrVal: E, IntVal: IsLastprivate);
1575 Data.PrivateCopy = PrivateCopy;
1576 Data.AppliedToPointee = AppliedToPointee;
1577 if (PrivateCopy) {
1578 DSAInfo &Data = getTopOfStack().SharingMap[PrivateCopy->getDecl()];
1579 Data.Modifier = Modifier;
1580 Data.Attributes = A;
1581 Data.RefExpr.setPointerAndInt(PtrVal: PrivateCopy, IntVal: IsLastprivate);
1582 Data.PrivateCopy = nullptr;
1583 Data.AppliedToPointee = AppliedToPointee;
1584 }
1585 }
1586}
1587
1588/// Build a variable declaration for OpenMP loop iteration variable.
1589static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
1590 StringRef Name, const AttrVec *Attrs = nullptr,
1591 DeclRefExpr *OrigRef = nullptr) {
1592 DeclContext *DC = SemaRef.CurContext;
1593 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
1594 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(T: Type, Loc);
1595 auto *Decl =
1596 VarDecl::Create(C&: SemaRef.Context, DC, StartLoc: Loc, IdLoc: Loc, Id: II, T: Type, TInfo, S: SC_None);
1597 if (Attrs) {
1598 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
1599 I != E; ++I)
1600 Decl->addAttr(A: *I);
1601 }
1602 Decl->setImplicit();
1603 if (OrigRef) {
1604 Decl->addAttr(
1605 A: OMPReferencedVarAttr::CreateImplicit(Ctx&: SemaRef.Context, Ref: OrigRef));
1606 }
1607 return Decl;
1608}
1609
1610static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
1611 SourceLocation Loc,
1612 bool RefersToCapture = false) {
1613 D->setReferenced();
1614 D->markUsed(C&: S.Context);
1615 return DeclRefExpr::Create(Context: S.getASTContext(), QualifierLoc: NestedNameSpecifierLoc(),
1616 TemplateKWLoc: SourceLocation(), D, RefersToEnclosingVariableOrCapture: RefersToCapture, NameLoc: Loc, T: Ty,
1617 VK: VK_LValue);
1618}
1619
1620void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1621 BinaryOperatorKind BOK) {
1622 D = getCanonicalDecl(D);
1623 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1624 assert(
1625 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1626 "Additional reduction info may be specified only for reduction items.");
1627 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1628 assert(ReductionData.ReductionRange.isInvalid() &&
1629 (getTopOfStack().Directive == OMPD_taskgroup ||
1630 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1631 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1632 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1633 "Additional reduction info may be specified only once for reduction "
1634 "items.");
1635 ReductionData.set(BO: BOK, RR: SR);
1636 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1637 if (!TaskgroupReductionRef) {
1638 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1639 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1640 TaskgroupReductionRef =
1641 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1642 }
1643}
1644
1645void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
1646 const Expr *ReductionRef) {
1647 D = getCanonicalDecl(D);
1648 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
1649 assert(
1650 getTopOfStack().SharingMap[D].Attributes == OMPC_reduction &&
1651 "Additional reduction info may be specified only for reduction items.");
1652 ReductionData &ReductionData = getTopOfStack().ReductionMap[D];
1653 assert(ReductionData.ReductionRange.isInvalid() &&
1654 (getTopOfStack().Directive == OMPD_taskgroup ||
1655 ((isOpenMPParallelDirective(getTopOfStack().Directive) ||
1656 isOpenMPWorksharingDirective(getTopOfStack().Directive)) &&
1657 !isOpenMPSimdDirective(getTopOfStack().Directive))) &&
1658 "Additional reduction info may be specified only once for reduction "
1659 "items.");
1660 ReductionData.set(RefExpr: ReductionRef, RR: SR);
1661 Expr *&TaskgroupReductionRef = getTopOfStack().TaskgroupReductionRef;
1662 if (!TaskgroupReductionRef) {
1663 VarDecl *VD = buildVarDecl(SemaRef, Loc: SR.getBegin(),
1664 Type: SemaRef.Context.VoidPtrTy, Name: ".task_red.");
1665 TaskgroupReductionRef =
1666 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: SemaRef.Context.VoidPtrTy, Loc: SR.getBegin());
1667 }
1668}
1669
1670const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1671 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1672 Expr *&TaskgroupDescriptor) const {
1673 D = getCanonicalDecl(D);
1674 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1675 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1676 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1677 if (Data.Attributes != OMPC_reduction ||
1678 Data.Modifier != OMPC_REDUCTION_task)
1679 continue;
1680 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1681 if (!ReductionData.ReductionOp ||
1682 isa<const Expr *>(Val: ReductionData.ReductionOp))
1683 return DSAVarData();
1684 SR = ReductionData.ReductionRange;
1685 BOK = cast<ReductionData::BOKPtrType>(Val: ReductionData.ReductionOp);
1686 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1687 "expression for the descriptor is not "
1688 "set.");
1689 TaskgroupDescriptor = I->TaskgroupReductionRef;
1690 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1691 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1692 /*AppliedToPointee=*/false);
1693 }
1694 return DSAVarData();
1695}
1696
1697const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1698 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1699 Expr *&TaskgroupDescriptor) const {
1700 D = getCanonicalDecl(D);
1701 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1702 for (const_iterator I = begin() + 1, E = end(); I != E; ++I) {
1703 const DSAInfo &Data = I->SharingMap.lookup(Val: D);
1704 if (Data.Attributes != OMPC_reduction ||
1705 Data.Modifier != OMPC_REDUCTION_task)
1706 continue;
1707 const ReductionData &ReductionData = I->ReductionMap.lookup(Val: D);
1708 if (!ReductionData.ReductionOp ||
1709 !isa<const Expr *>(Val: ReductionData.ReductionOp))
1710 return DSAVarData();
1711 SR = ReductionData.ReductionRange;
1712 ReductionRef = cast<const Expr *>(Val: ReductionData.ReductionOp);
1713 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1714 "expression for the descriptor is not "
1715 "set.");
1716 TaskgroupDescriptor = I->TaskgroupReductionRef;
1717 return DSAVarData(I->Directive, OMPC_reduction, Data.RefExpr.getPointer(),
1718 Data.PrivateCopy, I->DefaultAttrLoc, OMPC_REDUCTION_task,
1719 /*AppliedToPointee=*/false);
1720 }
1721 return DSAVarData();
1722}
1723
1724bool DSAStackTy::isOpenMPLocal(VarDecl *D, const_iterator I) const {
1725 D = D->getCanonicalDecl();
1726 for (const_iterator E = end(); I != E; ++I) {
1727 if (isImplicitOrExplicitTaskingRegion(DKind: I->Directive) ||
1728 isOpenMPTargetExecutionDirective(DKind: I->Directive)) {
1729 if (I->CurScope) {
1730 Scope *TopScope = I->CurScope->getParent();
1731 Scope *CurScope = getCurScope();
1732 while (CurScope && CurScope != TopScope && !CurScope->isDeclScope(D))
1733 CurScope = CurScope->getParent();
1734 return CurScope != TopScope;
1735 }
1736 for (DeclContext *DC = D->getDeclContext(); DC; DC = DC->getParent())
1737 if (I->Context == DC)
1738 return true;
1739 return false;
1740 }
1741 }
1742 return false;
1743}
1744
1745static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1746 bool AcceptIfMutable = true,
1747 bool *IsClassType = nullptr) {
1748 ASTContext &Context = SemaRef.getASTContext();
1749 Type = Type.getNonReferenceType().getCanonicalType();
1750 bool IsConstant = Type.isConstant(Ctx: Context);
1751 Type = Context.getBaseElementType(QT: Type);
1752 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1753 ? Type->getAsCXXRecordDecl()
1754 : nullptr;
1755 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(Val: RD))
1756 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1757 RD = CTD->getTemplatedDecl();
1758 if (IsClassType)
1759 *IsClassType = RD;
1760 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1761 RD->hasDefinition() && RD->hasMutableFields());
1762}
1763
1764static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1765 QualType Type, OpenMPClauseKind CKind,
1766 SourceLocation ELoc,
1767 bool AcceptIfMutable = true,
1768 bool ListItemNotVar = false) {
1769 ASTContext &Context = SemaRef.getASTContext();
1770 bool IsClassType;
1771 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, IsClassType: &IsClassType)) {
1772 unsigned Diag = ListItemNotVar ? diag::err_omp_const_list_item
1773 : IsClassType ? diag::err_omp_const_not_mutable_variable
1774 : diag::err_omp_const_variable;
1775 SemaRef.Diag(Loc: ELoc, DiagID: Diag) << getOpenMPClauseNameForDiag(C: CKind);
1776 if (!ListItemNotVar && D) {
1777 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
1778 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1779 VarDecl::DeclarationOnly;
1780 SemaRef.Diag(Loc: D->getLocation(),
1781 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1782 << D;
1783 }
1784 return true;
1785 }
1786 return false;
1787}
1788
1789const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1790 bool FromParent) {
1791 D = getCanonicalDecl(D);
1792 DSAVarData DVar;
1793
1794 auto *VD = dyn_cast<VarDecl>(Val: D);
1795 auto TI = Threadprivates.find(Val: D);
1796 if (TI != Threadprivates.end()) {
1797 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
1798 DVar.CKind = OMPC_threadprivate;
1799 DVar.Modifier = TI->getSecond().Modifier;
1800 return DVar;
1801 }
1802 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
1803 DVar.RefExpr = buildDeclRefExpr(
1804 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1805 Loc: VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1806 DVar.CKind = OMPC_threadprivate;
1807 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1808 return DVar;
1809 }
1810 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1811 // in a Construct, C/C++, predetermined, p.1]
1812 // Variables appearing in threadprivate directives are threadprivate.
1813 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1814 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1815 SemaRef.getLangOpts().OpenMPUseTLS &&
1816 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1817 (VD && VD->getStorageClass() == SC_Register &&
1818 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1819 DVar.RefExpr = buildDeclRefExpr(
1820 S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(), Loc: D->getLocation());
1821 DVar.CKind = OMPC_threadprivate;
1822 addDSA(D, E: DVar.RefExpr, A: OMPC_threadprivate);
1823 return DVar;
1824 }
1825 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1826 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1827 !isLoopControlVariable(D).first) {
1828 const_iterator IterTarget =
1829 std::find_if(first: begin(), last: end(), pred: [](const SharingMapTy &Data) {
1830 return isOpenMPTargetExecutionDirective(DKind: Data.Directive);
1831 });
1832 if (IterTarget != end()) {
1833 const_iterator ParentIterTarget = IterTarget + 1;
1834 for (const_iterator Iter = begin(); Iter != ParentIterTarget; ++Iter) {
1835 if (isOpenMPLocal(D: VD, I: Iter)) {
1836 DVar.RefExpr =
1837 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1838 Loc: D->getLocation());
1839 DVar.CKind = OMPC_threadprivate;
1840 return DVar;
1841 }
1842 }
1843 if (!isClauseParsingMode() || IterTarget != begin()) {
1844 auto DSAIter = IterTarget->SharingMap.find(Val: D);
1845 if (DSAIter != IterTarget->SharingMap.end() &&
1846 isOpenMPPrivate(Kind: DSAIter->getSecond().Attributes)) {
1847 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1848 DVar.CKind = OMPC_threadprivate;
1849 return DVar;
1850 }
1851 const_iterator End = end();
1852 if (!SemaRef.OpenMP().isOpenMPCapturedByRef(
1853 D, Level: std::distance(first: ParentIterTarget, last: End),
1854 /*OpenMPCaptureLevel=*/0)) {
1855 DVar.RefExpr =
1856 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: D->getType().getNonReferenceType(),
1857 Loc: IterTarget->ConstructLoc);
1858 DVar.CKind = OMPC_threadprivate;
1859 return DVar;
1860 }
1861 }
1862 }
1863 }
1864
1865 if (isStackEmpty())
1866 // Not in OpenMP execution region and top scope was already checked.
1867 return DVar;
1868
1869 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1870 // in a Construct, C/C++, predetermined, p.4]
1871 // Static data members are shared.
1872 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1873 // in a Construct, C/C++, predetermined, p.7]
1874 // Variables with static storage duration that are declared in a scope
1875 // inside the construct are shared.
1876 if (VD && VD->isStaticDataMember()) {
1877 // Check for explicitly specified attributes.
1878 const_iterator I = begin();
1879 const_iterator EndI = end();
1880 if (FromParent && I != EndI)
1881 ++I;
1882 if (I != EndI) {
1883 auto It = I->SharingMap.find(Val: D);
1884 if (It != I->SharingMap.end()) {
1885 const DSAInfo &Data = It->getSecond();
1886 DVar.RefExpr = Data.RefExpr.getPointer();
1887 DVar.PrivateCopy = Data.PrivateCopy;
1888 DVar.CKind = Data.Attributes;
1889 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1890 DVar.DKind = I->Directive;
1891 DVar.Modifier = Data.Modifier;
1892 DVar.AppliedToPointee = Data.AppliedToPointee;
1893 return DVar;
1894 }
1895 }
1896
1897 DVar.CKind = OMPC_shared;
1898 return DVar;
1899 }
1900
1901 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
1902 // The predetermined shared attribute for const-qualified types having no
1903 // mutable members was removed after OpenMP 3.1.
1904 if (SemaRef.LangOpts.OpenMP <= 31) {
1905 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1906 // in a Construct, C/C++, predetermined, p.6]
1907 // Variables with const qualified type having no mutable member are
1908 // shared.
1909 if (isConstNotMutableType(SemaRef, Type: D->getType())) {
1910 // Variables with const-qualified type having no mutable member may be
1911 // listed in a firstprivate clause, even if they are static data members.
1912 DSAVarData DVarTemp = hasInnermostDSA(
1913 D,
1914 CPred: [](OpenMPClauseKind C, bool) {
1915 return C == OMPC_firstprivate || C == OMPC_shared;
1916 },
1917 DPred: MatchesAlways, FromParent);
1918 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1919 return DVarTemp;
1920
1921 DVar.CKind = OMPC_shared;
1922 return DVar;
1923 }
1924 }
1925
1926 // Explicitly specified attributes and local variables with predetermined
1927 // attributes.
1928 const_iterator I = begin();
1929 const_iterator EndI = end();
1930 if (FromParent && I != EndI)
1931 ++I;
1932 if (I == EndI)
1933 return DVar;
1934 auto It = I->SharingMap.find(Val: D);
1935 if (It != I->SharingMap.end()) {
1936 const DSAInfo &Data = It->getSecond();
1937 DVar.RefExpr = Data.RefExpr.getPointer();
1938 DVar.PrivateCopy = Data.PrivateCopy;
1939 DVar.CKind = Data.Attributes;
1940 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1941 DVar.DKind = I->Directive;
1942 DVar.Modifier = Data.Modifier;
1943 DVar.AppliedToPointee = Data.AppliedToPointee;
1944 }
1945
1946 return DVar;
1947}
1948
1949const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1950 bool FromParent) const {
1951 if (isStackEmpty()) {
1952 const_iterator I;
1953 return getDSA(Iter&: I, D);
1954 }
1955 D = getCanonicalDecl(D);
1956 const_iterator StartI = begin();
1957 const_iterator EndI = end();
1958 if (FromParent && StartI != EndI)
1959 ++StartI;
1960 return getDSA(Iter&: StartI, D);
1961}
1962
1963const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1964 unsigned Level) const {
1965 if (getStackSize() <= Level)
1966 return DSAVarData();
1967 D = getCanonicalDecl(D);
1968 const_iterator StartI = std::next(x: begin(), n: getStackSize() - 1 - Level);
1969 return getDSA(Iter&: StartI, D);
1970}
1971
1972const DSAStackTy::DSAVarData
1973DSAStackTy::hasDSA(ValueDecl *D,
1974 const llvm::function_ref<bool(OpenMPClauseKind, bool,
1975 DefaultDataSharingAttributes)>
1976 CPred,
1977 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1978 bool FromParent) const {
1979 if (isStackEmpty())
1980 return {};
1981 D = getCanonicalDecl(D);
1982 const_iterator I = begin();
1983 const_iterator EndI = end();
1984 if (FromParent && I != EndI)
1985 ++I;
1986 for (; I != EndI; ++I) {
1987 if (!DPred(I->Directive) &&
1988 !isImplicitOrExplicitTaskingRegion(DKind: I->Directive))
1989 continue;
1990 const_iterator NewI = I;
1991 DSAVarData DVar = getDSA(Iter&: NewI, D);
1992 if (I == NewI && CPred(DVar.CKind, DVar.AppliedToPointee, I->DefaultAttr))
1993 return DVar;
1994 }
1995 return {};
1996}
1997
1998const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1999 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2000 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2001 bool FromParent) const {
2002 if (isStackEmpty())
2003 return {};
2004 D = getCanonicalDecl(D);
2005 const_iterator StartI = begin();
2006 const_iterator EndI = end();
2007 if (FromParent && StartI != EndI)
2008 ++StartI;
2009 if (StartI == EndI || !DPred(StartI->Directive))
2010 return {};
2011 const_iterator NewI = StartI;
2012 DSAVarData DVar = getDSA(Iter&: NewI, D);
2013 return (NewI == StartI && CPred(DVar.CKind, DVar.AppliedToPointee))
2014 ? DVar
2015 : DSAVarData();
2016}
2017
2018bool DSAStackTy::hasExplicitDSA(
2019 const ValueDecl *D,
2020 const llvm::function_ref<bool(OpenMPClauseKind, bool)> CPred,
2021 unsigned Level, bool NotLastprivate) const {
2022 if (getStackSize() <= Level)
2023 return false;
2024 D = getCanonicalDecl(D);
2025 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2026 auto I = StackElem.SharingMap.find(Val: D);
2027 if (I != StackElem.SharingMap.end() && I->getSecond().RefExpr.getPointer() &&
2028 CPred(I->getSecond().Attributes, I->getSecond().AppliedToPointee) &&
2029 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
2030 return true;
2031 // Check predetermined rules for the loop control variables.
2032 auto LI = StackElem.LCVMap.find(Val: D);
2033 if (LI != StackElem.LCVMap.end())
2034 return CPred(OMPC_private, /*AppliedToPointee=*/false);
2035 return false;
2036}
2037
2038bool DSAStackTy::hasExplicitDirective(
2039 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
2040 unsigned Level) const {
2041 if (getStackSize() <= Level)
2042 return false;
2043 const SharingMapTy &StackElem = getStackElemAtLevel(Level);
2044 return DPred(StackElem.Directive);
2045}
2046
2047bool DSAStackTy::hasDirective(
2048 const llvm::function_ref<bool(OpenMPDirectiveKind,
2049 const DeclarationNameInfo &, SourceLocation)>
2050 DPred,
2051 bool FromParent) const {
2052 // We look only in the enclosing region.
2053 size_t Skip = FromParent ? 2 : 1;
2054 for (const_iterator I = begin() + std::min(a: Skip, b: getStackSize()), E = end();
2055 I != E; ++I) {
2056 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
2057 return true;
2058 }
2059 return false;
2060}
2061
2062void SemaOpenMP::InitDataSharingAttributesStack() {
2063 VarDataSharingAttributesStack = new DSAStackTy(SemaRef);
2064}
2065
2066#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
2067
2068void SemaOpenMP::pushOpenMPFunctionRegion() { DSAStack->pushFunction(); }
2069
2070void SemaOpenMP::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
2071 DSAStack->popFunction(OldFSI);
2072}
2073
2074static bool isOpenMPDeviceDelayedContext(Sema &S) {
2075 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsTargetDevice &&
2076 "Expected OpenMP device compilation.");
2077 return !S.OpenMP().isInOpenMPTargetExecutionDirective();
2078}
2079
2080namespace {
2081/// Status of the function emission on the host/device.
2082enum class FunctionEmissionStatus {
2083 Emitted,
2084 Discarded,
2085 Unknown,
2086};
2087} // anonymous namespace
2088
2089SemaBase::SemaDiagnosticBuilder
2090SemaOpenMP::diagIfOpenMPDeviceCode(SourceLocation Loc, unsigned DiagID,
2091 const FunctionDecl *FD) {
2092 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice &&
2093 "Expected OpenMP device compilation.");
2094
2095 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2096 if (FD) {
2097 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2098 switch (FES) {
2099 case Sema::FunctionEmissionStatus::Emitted:
2100 Kind = SemaDiagnosticBuilder::K_Immediate;
2101 break;
2102 case Sema::FunctionEmissionStatus::Unknown:
2103 // TODO: We should always delay diagnostics here in case a target
2104 // region is in a function we do not emit. However, as the
2105 // current diagnostics are associated with the function containing
2106 // the target region and we do not emit that one, we would miss out
2107 // on diagnostics for the target region itself. We need to anchor
2108 // the diagnostics with the new generated function *or* ensure we
2109 // emit diagnostics associated with the surrounding function.
2110 Kind = isOpenMPDeviceDelayedContext(S&: SemaRef)
2111 ? SemaDiagnosticBuilder::K_Deferred
2112 : SemaDiagnosticBuilder::K_Immediate;
2113 break;
2114 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2115 case Sema::FunctionEmissionStatus::OMPDiscarded:
2116 Kind = SemaDiagnosticBuilder::K_Nop;
2117 break;
2118 case Sema::FunctionEmissionStatus::CUDADiscarded:
2119 llvm_unreachable("CUDADiscarded unexpected in OpenMP device compilation");
2120 break;
2121 }
2122 }
2123
2124 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2125}
2126
2127SemaBase::SemaDiagnosticBuilder
2128SemaOpenMP::diagIfOpenMPHostCode(SourceLocation Loc, unsigned DiagID,
2129 const FunctionDecl *FD) {
2130 assert(getLangOpts().OpenMP && !getLangOpts().OpenMPIsTargetDevice &&
2131 "Expected OpenMP host compilation.");
2132
2133 SemaDiagnosticBuilder::Kind Kind = SemaDiagnosticBuilder::K_Nop;
2134 if (FD) {
2135 Sema::FunctionEmissionStatus FES = SemaRef.getEmissionStatus(Decl: FD);
2136 switch (FES) {
2137 case Sema::FunctionEmissionStatus::Emitted:
2138 Kind = SemaDiagnosticBuilder::K_Immediate;
2139 break;
2140 case Sema::FunctionEmissionStatus::Unknown:
2141 Kind = SemaDiagnosticBuilder::K_Deferred;
2142 break;
2143 case Sema::FunctionEmissionStatus::TemplateDiscarded:
2144 case Sema::FunctionEmissionStatus::OMPDiscarded:
2145 case Sema::FunctionEmissionStatus::CUDADiscarded:
2146 Kind = SemaDiagnosticBuilder::K_Nop;
2147 break;
2148 }
2149 }
2150
2151 return SemaDiagnosticBuilder(Kind, Loc, DiagID, FD, SemaRef);
2152}
2153
2154static OpenMPDefaultmapClauseKind
2155getVariableCategoryFromDecl(const LangOptions &LO, const ValueDecl *VD) {
2156 if (LO.OpenMP <= 45) {
2157 if (VD->getType().getNonReferenceType()->isScalarType())
2158 return OMPC_DEFAULTMAP_scalar;
2159 return OMPC_DEFAULTMAP_aggregate;
2160 }
2161 if (VD->getType().getNonReferenceType()->isAnyPointerType())
2162 return OMPC_DEFAULTMAP_pointer;
2163 if (VD->getType().getNonReferenceType()->isScalarType())
2164 return OMPC_DEFAULTMAP_scalar;
2165 return OMPC_DEFAULTMAP_aggregate;
2166}
2167
2168bool SemaOpenMP::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
2169 unsigned OpenMPCaptureLevel) const {
2170 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2171
2172 ASTContext &Ctx = getASTContext();
2173 bool IsByRef = true;
2174
2175 // Find the directive that is associated with the provided scope.
2176 D = cast<ValueDecl>(Val: D->getCanonicalDecl());
2177 QualType Ty = D->getType();
2178
2179 bool IsVariableUsedInMapClause = false;
2180 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level)) {
2181 // This table summarizes how a given variable should be passed to the device
2182 // given its type and the clauses where it appears. This table is based on
2183 // the description in OpenMP 4.5 [2.10.4, target Construct] and
2184 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
2185 //
2186 // =========================================================================
2187 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
2188 // | |(tofrom:scalar)| | pvt | |has_dv_adr| |
2189 // =========================================================================
2190 // | scl | | | | - | | bycopy|
2191 // | scl | | - | x | - | - | bycopy|
2192 // | scl | | x | - | - | - | null |
2193 // | scl | x | | | - | | byref |
2194 // | scl | x | - | x | - | - | bycopy|
2195 // | scl | x | x | - | - | - | null |
2196 // | scl | | - | - | - | x | byref |
2197 // | scl | x | - | - | - | x | byref |
2198 //
2199 // | agg | n.a. | | | - | | byref |
2200 // | agg | n.a. | - | x | - | - | byref |
2201 // | agg | n.a. | x | - | - | - | null |
2202 // | agg | n.a. | - | - | - | x | byref |
2203 // | agg | n.a. | - | - | - | x[] | byref |
2204 //
2205 // | ptr | n.a. | | | - | | bycopy|
2206 // | ptr | n.a. | - | x | - | - | bycopy|
2207 // | ptr | n.a. | x | - | - | - | null |
2208 // | ptr | n.a. | - | - | - | x | byref |
2209 // | ptr | n.a. | - | - | - | x, x[] | bycopy|
2210 // | ptr | n.a. | - | - | - | x[] | bycopy|
2211 // | ptr | n.a. | - | - | x | | bycopy|
2212 // | ptr | n.a. | - | - | x | x | bycopy|
2213 // | ptr | n.a. | - | - | x | x[] | bycopy|
2214 // =========================================================================
2215 // Legend:
2216 // scl - scalar
2217 // ptr - pointer
2218 // agg - aggregate
2219 // x - applies
2220 // - - invalid in this combination
2221 // [] - mapped with an array section
2222 // byref - should be mapped by reference
2223 // byval - should be mapped by value
2224 // null - initialize a local variable to null on the device
2225 //
2226 // Observations:
2227 // - All scalar declarations that show up in a map clause have to be passed
2228 // by reference, because they may have been mapped in the enclosing data
2229 // environment.
2230 // - If the scalar value does not fit the size of uintptr, it has to be
2231 // passed by reference, regardless the result in the table above.
2232 // - For pointers mapped by value that have either an implicit map or an
2233 // array section, the runtime library may pass the NULL value to the
2234 // device instead of the value passed to it by the compiler.
2235 // - If both a pointer and a dereference of it are mapped, then the pointer
2236 // should be passed by reference.
2237
2238 if (Ty->isReferenceType())
2239 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
2240
2241 // Locate map clauses and see if the variable being captured is mapped by
2242 // itself, or referred to, in any of those clauses. Here we only care about
2243 // variables, not fields, because fields are part of aggregates.
2244 bool IsVariableAssociatedWithSection = false;
2245 bool IsVariableItselfMapped = false;
2246
2247 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2248 VD: D, Level,
2249 Check: [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection,
2250 &IsVariableItselfMapped,
2251 D](OMPClauseMappableExprCommon::MappableExprComponentListRef
2252 MapExprComponents,
2253 OpenMPClauseKind WhereFoundClauseKind) {
2254 // Both map and has_device_addr clauses information influences how a
2255 // variable is captured. E.g. is_device_ptr does not require changing
2256 // the default behavior.
2257 if (WhereFoundClauseKind != OMPC_map &&
2258 WhereFoundClauseKind != OMPC_has_device_addr)
2259 return false;
2260
2261 auto EI = MapExprComponents.rbegin();
2262 auto EE = MapExprComponents.rend();
2263
2264 assert(EI != EE && "Invalid map expression!");
2265
2266 if (isa<DeclRefExpr>(Val: EI->getAssociatedExpression()) &&
2267 EI->getAssociatedDeclaration() == D) {
2268 IsVariableUsedInMapClause = true;
2269
2270 // If the component list has only one element, it's for mapping the
2271 // variable itself, like map(p). This takes precedence in
2272 // determining how it's captured, so we don't need to look further
2273 // for any other maps that use the variable (like map(p[0]) etc.)
2274 if (MapExprComponents.size() == 1) {
2275 IsVariableItselfMapped = true;
2276 return true;
2277 }
2278 }
2279
2280 ++EI;
2281 if (EI == EE)
2282 return false;
2283 auto Last = std::prev(x: EE);
2284 const auto *UO =
2285 dyn_cast<UnaryOperator>(Val: Last->getAssociatedExpression());
2286 if ((UO && UO->getOpcode() == UO_Deref) ||
2287 isa<ArraySubscriptExpr>(Val: Last->getAssociatedExpression()) ||
2288 isa<ArraySectionExpr>(Val: Last->getAssociatedExpression()) ||
2289 isa<MemberExpr>(Val: EI->getAssociatedExpression()) ||
2290 isa<OMPArrayShapingExpr>(Val: Last->getAssociatedExpression())) {
2291 IsVariableAssociatedWithSection = true;
2292 // We've found a case like map(p[0]) or map(p->a) or map(*p),
2293 // so we are done with this particular map, but we need to keep
2294 // looking in case we find a map(p).
2295 return false;
2296 }
2297
2298 // Keep looking for more map info.
2299 return false;
2300 });
2301
2302 if (IsVariableUsedInMapClause) {
2303 // If variable is identified in a map clause it is always captured by
2304 // reference except if it is a pointer that is dereferenced somehow, but
2305 // not itself mapped.
2306 //
2307 // OpenMP 6.0, 7.1.1: Data sharing attribute rules, variables referenced
2308 // in a construct::
2309 // If a list item in a has_device_addr clause or in a map clause on the
2310 // target construct has a base pointer, and the base pointer is a scalar
2311 // variable *that is not a list item in a map clause on the construct*,
2312 // the base pointer is firstprivate.
2313 //
2314 // OpenMP 4.5, 2.15.1.1: Data-sharing Attribute Rules for Variables
2315 // Referenced in a Construct:
2316 // If an array section is a list item in a map clause on the target
2317 // construct and the array section is derived from a variable for which
2318 // the type is pointer then that variable is firstprivate.
2319 IsByRef = IsVariableItselfMapped ||
2320 !(Ty->isPointerType() && IsVariableAssociatedWithSection);
2321 } else {
2322 // By default, all the data that has a scalar type is mapped by copy
2323 // (except for reduction variables).
2324 // Defaultmap scalar is mutual exclusive to defaultmap pointer
2325 IsByRef = (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
2326 !Ty->isAnyPointerType()) ||
2327 !Ty->isScalarType() ||
2328 DSAStack->isDefaultmapCapturedByRef(
2329 Level, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)) ||
2330 DSAStack->hasExplicitDSA(
2331 D,
2332 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) {
2333 return K == OMPC_reduction && !AppliedToPointee;
2334 },
2335 Level);
2336 }
2337 }
2338
2339 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
2340 IsByRef =
2341 ((IsVariableUsedInMapClause &&
2342 DSAStack->getCaptureRegion(Level, OpenMPCaptureLevel) ==
2343 OMPD_target) ||
2344 !(DSAStack->hasExplicitDSA(
2345 D,
2346 CPred: [](OpenMPClauseKind K, bool AppliedToPointee) -> bool {
2347 return K == OMPC_firstprivate ||
2348 (K == OMPC_reduction && AppliedToPointee);
2349 },
2350 Level, /*NotLastprivate=*/true) ||
2351 DSAStack->isUsesAllocatorsDecl(Level, D))) &&
2352 // If the variable is artificial and must be captured by value - try to
2353 // capture by value.
2354 !(isa<OMPCapturedExprDecl>(Val: D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
2355 !cast<OMPCapturedExprDecl>(Val: D)->getInit()->isGLValue()) &&
2356 // If the variable is implicitly firstprivate and scalar - capture by
2357 // copy
2358 !((DSAStack->getDefaultDSA() == DSA_firstprivate ||
2359 DSAStack->getDefaultDSA() == DSA_private) &&
2360 !DSAStack->hasExplicitDSA(
2361 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_unknown; },
2362 Level) &&
2363 !DSAStack->isLoopControlVariable(D, Level).first);
2364 }
2365
2366 // When passing data by copy, we need to make sure it fits the uintptr size
2367 // and alignment, because the runtime library only deals with uintptr types.
2368 // If it does not fit the uintptr size, we need to pass the data by reference
2369 // instead.
2370 if (!IsByRef && (Ctx.getTypeSizeInChars(T: Ty) >
2371 Ctx.getTypeSizeInChars(T: Ctx.getUIntPtrType()) ||
2372 Ctx.getAlignOfGlobalVarInChars(T: Ty, VD: dyn_cast<VarDecl>(Val: D)) >
2373 Ctx.getTypeAlignInChars(T: Ctx.getUIntPtrType()))) {
2374 IsByRef = true;
2375 }
2376
2377 return IsByRef;
2378}
2379
2380unsigned SemaOpenMP::getOpenMPNestingLevel() const {
2381 assert(getLangOpts().OpenMP);
2382 return DSAStack->getNestingLevel();
2383}
2384
2385bool SemaOpenMP::isInOpenMPTaskUntiedContext() const {
2386 return isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2387 DSAStack->isUntiedRegion();
2388}
2389
2390bool SemaOpenMP::isInOpenMPTargetExecutionDirective() const {
2391 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
2392 !DSAStack->isClauseParsingMode()) ||
2393 DSAStack->hasDirective(
2394 DPred: [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2395 SourceLocation) -> bool {
2396 return isOpenMPTargetExecutionDirective(DKind: K);
2397 },
2398 FromParent: false);
2399}
2400
2401bool SemaOpenMP::isOpenMPRebuildMemberExpr(ValueDecl *D) {
2402 // Only rebuild for Field.
2403 if (!isa<FieldDecl>(Val: D))
2404 return false;
2405 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2406 D,
2407 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2408 DefaultDataSharingAttributes DefaultAttr) {
2409 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2410 (DefaultAttr == DSA_firstprivate || DefaultAttr == DSA_private);
2411 },
2412 DPred: [](OpenMPDirectiveKind) { return true; },
2413 DSAStack->isClauseParsingMode());
2414 if (DVarPrivate.CKind != OMPC_unknown)
2415 return true;
2416 return false;
2417}
2418
2419static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
2420 Expr *CaptureExpr, bool WithInit,
2421 DeclContext *CurContext,
2422 bool AsExpression);
2423
2424VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
2425 unsigned StopAt) {
2426 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2427 D = getCanonicalDecl(D);
2428
2429 auto *VD = dyn_cast<VarDecl>(Val: D);
2430 // Do not capture constexpr variables.
2431 if (VD && VD->isConstexpr())
2432 return nullptr;
2433
2434 // If we want to determine whether the variable should be captured from the
2435 // perspective of the current capturing scope, and we've already left all the
2436 // capturing scopes of the top directive on the stack, check from the
2437 // perspective of its parent directive (if any) instead.
2438 DSAStackTy::ParentDirectiveScope InParentDirectiveRAII(
2439 *DSAStack, CheckScopeInfo && DSAStack->isBodyComplete());
2440
2441 // If we are attempting to capture a global variable in a directive with
2442 // 'target' we return true so that this global is also mapped to the device.
2443 //
2444 if (VD && !VD->hasLocalStorage() &&
2445 (SemaRef.getCurCapturedRegion() || SemaRef.getCurBlock() ||
2446 SemaRef.getCurLambda())) {
2447 if (isInOpenMPTargetExecutionDirective()) {
2448 DSAStackTy::DSAVarData DVarTop =
2449 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2450 if (DVarTop.CKind != OMPC_unknown && DVarTop.RefExpr)
2451 return VD;
2452 // If the declaration is enclosed in a 'declare target' directive,
2453 // then it should not be captured.
2454 //
2455 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2456 return nullptr;
2457 CapturedRegionScopeInfo *CSI = nullptr;
2458 for (FunctionScopeInfo *FSI : llvm::drop_begin(
2459 RangeOrContainer: llvm::reverse(C&: SemaRef.FunctionScopes),
2460 N: CheckScopeInfo ? (SemaRef.FunctionScopes.size() - (StopAt + 1))
2461 : 0)) {
2462 if (!isa<CapturingScopeInfo>(Val: FSI))
2463 return nullptr;
2464 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2465 if (RSI->CapRegionKind == CR_OpenMP) {
2466 CSI = RSI;
2467 break;
2468 }
2469 }
2470 assert(CSI && "Failed to find CapturedRegionScopeInfo");
2471 SmallVector<OpenMPDirectiveKind, 4> Regions;
2472 getOpenMPCaptureRegions(CaptureRegions&: Regions,
2473 DSAStack->getDirective(Level: CSI->OpenMPLevel));
2474 if (Regions[CSI->OpenMPCaptureLevel] != OMPD_task)
2475 return VD;
2476 }
2477 if (isInOpenMPDeclareTargetContext()) {
2478 // Try to mark variable as declare target if it is used in capturing
2479 // regions.
2480 if (getLangOpts().OpenMP <= 45 &&
2481 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
2482 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: VD);
2483 return nullptr;
2484 }
2485 }
2486
2487 if (CheckScopeInfo) {
2488 bool OpenMPFound = false;
2489 for (unsigned I = StopAt + 1; I > 0; --I) {
2490 FunctionScopeInfo *FSI = SemaRef.FunctionScopes[I - 1];
2491 if (!isa<CapturingScopeInfo>(Val: FSI))
2492 return nullptr;
2493 if (auto *RSI = dyn_cast<CapturedRegionScopeInfo>(Val: FSI))
2494 if (RSI->CapRegionKind == CR_OpenMP) {
2495 OpenMPFound = true;
2496 break;
2497 }
2498 }
2499 if (!OpenMPFound)
2500 return nullptr;
2501 }
2502
2503 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2504 (!DSAStack->isClauseParsingMode() ||
2505 DSAStack->getParentDirective() != OMPD_unknown)) {
2506 auto &&Info = DSAStack->isLoopControlVariable(D);
2507 if (Info.first ||
2508 (VD && VD->hasLocalStorage() &&
2509 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
2510 (VD && DSAStack->isForceVarCapturing()))
2511 return VD ? VD : Info.second;
2512 DSAStackTy::DSAVarData DVarTop =
2513 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
2514 if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(Kind: DVarTop.CKind) &&
2515 (!VD || VD->hasLocalStorage() ||
2516 !(DVarTop.AppliedToPointee && DVarTop.CKind != OMPC_reduction)))
2517 return VD ? VD : cast<VarDecl>(Val: DVarTop.PrivateCopy->getDecl());
2518 // Threadprivate variables must not be captured.
2519 if (isOpenMPThreadPrivate(Kind: DVarTop.CKind))
2520 return nullptr;
2521 // The variable is not private or it is the variable in the directive with
2522 // default(none) clause and not used in any clause.
2523 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2524 D,
2525 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
2526 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
2527 },
2528 DPred: [](OpenMPDirectiveKind) { return true; },
2529 DSAStack->isClauseParsingMode());
2530 // Global shared must not be captured.
2531 if (VD && !VD->hasLocalStorage() && DVarPrivate.CKind == OMPC_unknown &&
2532 ((DSAStack->getDefaultDSA() != DSA_none &&
2533 DSAStack->getDefaultDSA() != DSA_private &&
2534 DSAStack->getDefaultDSA() != DSA_firstprivate) ||
2535 DVarTop.CKind == OMPC_shared))
2536 return nullptr;
2537 auto *FD = dyn_cast<FieldDecl>(Val: D);
2538 if (DVarPrivate.CKind != OMPC_unknown && !VD && FD &&
2539 !DVarPrivate.PrivateCopy) {
2540 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2541 D,
2542 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2543 DefaultDataSharingAttributes DefaultAttr) {
2544 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2545 (DefaultAttr == DSA_firstprivate ||
2546 DefaultAttr == DSA_private);
2547 },
2548 DPred: [](OpenMPDirectiveKind) { return true; },
2549 DSAStack->isClauseParsingMode());
2550 if (DVarPrivate.CKind == OMPC_unknown)
2551 return nullptr;
2552
2553 VarDecl *VD = DSAStack->getImplicitFDCapExprDecl(FD);
2554 if (VD)
2555 return VD;
2556 if (SemaRef.getCurrentThisType().isNull())
2557 return nullptr;
2558 Expr *ThisExpr = SemaRef.BuildCXXThisExpr(Loc: SourceLocation(),
2559 Type: SemaRef.getCurrentThisType(),
2560 /*IsImplicit=*/true);
2561 const CXXScopeSpec CS = CXXScopeSpec();
2562 Expr *ME = SemaRef.BuildMemberExpr(
2563 Base: ThisExpr, /*IsArrow=*/true, OpLoc: SourceLocation(),
2564 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: FD,
2565 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
2566 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(), Ty: FD->getType(),
2567 VK: VK_LValue, OK: OK_Ordinary);
2568 OMPCapturedExprDecl *CD = buildCaptureDecl(
2569 S&: SemaRef, Id: FD->getIdentifier(), CaptureExpr: ME, WithInit: DVarPrivate.CKind != OMPC_private,
2570 CurContext: SemaRef.CurContext->getParent(), /*AsExpression=*/false);
2571 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
2572 S&: SemaRef, D: CD, Ty: CD->getType().getNonReferenceType(), Loc: SourceLocation());
2573 VD = cast<VarDecl>(Val: VDPrivateRefExpr->getDecl());
2574 DSAStack->addImplicitDefaultFirstprivateFD(FD, VD);
2575 return VD;
2576 }
2577 if (DVarPrivate.CKind != OMPC_unknown ||
2578 (VD && (DSAStack->getDefaultDSA() == DSA_none ||
2579 DSAStack->getDefaultDSA() == DSA_private ||
2580 DSAStack->getDefaultDSA() == DSA_firstprivate)))
2581 return VD ? VD : cast<VarDecl>(Val: DVarPrivate.PrivateCopy->getDecl());
2582 }
2583 return nullptr;
2584}
2585
2586void SemaOpenMP::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
2587 unsigned Level) const {
2588 FunctionScopesIndex -= getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2589}
2590
2591void SemaOpenMP::startOpenMPLoop() {
2592 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2593 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
2594 DSAStack->loopInit();
2595}
2596
2597void SemaOpenMP::startOpenMPCXXRangeFor() {
2598 assert(getLangOpts().OpenMP && "OpenMP must be enabled.");
2599 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2600 DSAStack->resetPossibleLoopCounter();
2601 DSAStack->loopStart();
2602 }
2603}
2604
2605OpenMPClauseKind SemaOpenMP::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level,
2606 unsigned CapLevel) const {
2607 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2608 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
2609 (!DSAStack->isClauseParsingMode() ||
2610 DSAStack->getParentDirective() != OMPD_unknown)) {
2611 DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
2612 D,
2613 CPred: [](OpenMPClauseKind C, bool AppliedToPointee,
2614 DefaultDataSharingAttributes DefaultAttr) {
2615 return isOpenMPPrivate(Kind: C) && !AppliedToPointee &&
2616 DefaultAttr == DSA_private;
2617 },
2618 DPred: [](OpenMPDirectiveKind) { return true; },
2619 DSAStack->isClauseParsingMode());
2620 if (DVarPrivate.CKind == OMPC_private && isa<OMPCapturedExprDecl>(Val: D) &&
2621 DSAStack->isImplicitDefaultFirstprivateFD(VD: cast<VarDecl>(Val: D)) &&
2622 !DSAStack->isLoopControlVariable(D).first)
2623 return OMPC_private;
2624 }
2625 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTaskingDirective, Level)) {
2626 bool IsTriviallyCopyable =
2627 D->getType().getNonReferenceType().isTriviallyCopyableType(
2628 Context: getASTContext()) &&
2629 !D->getType()
2630 .getNonReferenceType()
2631 .getCanonicalType()
2632 ->getAsCXXRecordDecl();
2633 OpenMPDirectiveKind DKind = DSAStack->getDirective(Level);
2634 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2635 getOpenMPCaptureRegions(CaptureRegions, DKind);
2636 if (isOpenMPTaskingDirective(Kind: CaptureRegions[CapLevel]) &&
2637 (IsTriviallyCopyable ||
2638 !isOpenMPTaskLoopDirective(DKind: CaptureRegions[CapLevel]))) {
2639 if (DSAStack->hasExplicitDSA(
2640 D,
2641 CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_firstprivate; },
2642 Level, /*NotLastprivate=*/true))
2643 return OMPC_firstprivate;
2644 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2645 if (DVar.CKind != OMPC_shared &&
2646 !DSAStack->isLoopControlVariable(D, Level).first && !DVar.RefExpr) {
2647 DSAStack->addImplicitTaskFirstprivate(Level, D);
2648 return OMPC_firstprivate;
2649 }
2650 }
2651 }
2652 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()) &&
2653 !isOpenMPLoopTransformationDirective(DSAStack->getCurrentDirective())) {
2654 if (DSAStack->getAssociatedLoops() > 0 && !DSAStack->isLoopStarted()) {
2655 DSAStack->resetPossibleLoopCounter(D);
2656 DSAStack->loopStart();
2657 return OMPC_private;
2658 }
2659 if ((DSAStack->getPossiblyLoopCounter() == D->getCanonicalDecl() ||
2660 DSAStack->isLoopControlVariable(D).first) &&
2661 !DSAStack->hasExplicitDSA(
2662 D, CPred: [](OpenMPClauseKind K, bool) { return K != OMPC_private; },
2663 Level) &&
2664 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
2665 return OMPC_private;
2666 }
2667 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2668 if (DSAStack->isThreadPrivate(D: const_cast<VarDecl *>(VD)) &&
2669 DSAStack->isForceVarCapturing() &&
2670 !DSAStack->hasExplicitDSA(
2671 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_copyin; },
2672 Level))
2673 return OMPC_private;
2674 }
2675 // User-defined allocators are private since they must be defined in the
2676 // context of target region.
2677 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective, Level) &&
2678 DSAStack->isUsesAllocatorsDecl(Level, D).value_or(
2679 u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
2680 DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator)
2681 return OMPC_private;
2682 return (DSAStack->hasExplicitDSA(
2683 D, CPred: [](OpenMPClauseKind K, bool) { return K == OMPC_private; },
2684 Level) ||
2685 (DSAStack->isClauseParsingMode() &&
2686 DSAStack->getClauseParsingMode() == OMPC_private) ||
2687 // Consider taskgroup reduction descriptor variable a private
2688 // to avoid possible capture in the region.
2689 (DSAStack->hasExplicitDirective(
2690 DPred: [](OpenMPDirectiveKind K) {
2691 return K == OMPD_taskgroup ||
2692 ((isOpenMPParallelDirective(DKind: K) ||
2693 isOpenMPWorksharingDirective(DKind: K)) &&
2694 !isOpenMPSimdDirective(DKind: K));
2695 },
2696 Level) &&
2697 DSAStack->isTaskgroupReductionRef(VD: D, Level)))
2698 ? OMPC_private
2699 : OMPC_unknown;
2700}
2701
2702void SemaOpenMP::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
2703 unsigned Level) {
2704 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2705 D = getCanonicalDecl(D);
2706 OpenMPClauseKind OMPC = OMPC_unknown;
2707 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
2708 const unsigned NewLevel = I - 1;
2709 if (DSAStack->hasExplicitDSA(
2710 D,
2711 CPred: [&OMPC](const OpenMPClauseKind K, bool AppliedToPointee) {
2712 if (isOpenMPPrivate(Kind: K) && !AppliedToPointee) {
2713 OMPC = K;
2714 return true;
2715 }
2716 return false;
2717 },
2718 Level: NewLevel))
2719 break;
2720 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
2721 VD: D, Level: NewLevel,
2722 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2723 OpenMPClauseKind) { return true; })) {
2724 OMPC = OMPC_map;
2725 break;
2726 }
2727 if (DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2728 Level: NewLevel)) {
2729 OMPC = OMPC_map;
2730 if (DSAStack->mustBeFirstprivateAtLevel(
2731 Level: NewLevel, Kind: getVariableCategoryFromDecl(LO: getLangOpts(), VD: D)))
2732 OMPC = OMPC_firstprivate;
2733 break;
2734 }
2735 }
2736 if (OMPC != OMPC_unknown)
2737 FD->addAttr(
2738 A: OMPCaptureKindAttr::CreateImplicit(Ctx&: getASTContext(), CaptureKindVal: unsigned(OMPC)));
2739}
2740
2741bool SemaOpenMP::isOpenMPTargetCapturedDecl(const ValueDecl *D, unsigned Level,
2742 unsigned CaptureLevel) const {
2743 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2744 // Return true if the current level is no longer enclosed in a target region.
2745
2746 SmallVector<OpenMPDirectiveKind, 4> Regions;
2747 getOpenMPCaptureRegions(CaptureRegions&: Regions, DSAStack->getDirective(Level));
2748 const auto *VD = dyn_cast<VarDecl>(Val: D);
2749 return VD && !VD->hasLocalStorage() &&
2750 DSAStack->hasExplicitDirective(DPred: isOpenMPTargetExecutionDirective,
2751 Level) &&
2752 Regions[CaptureLevel] != OMPD_task;
2753}
2754
2755bool SemaOpenMP::isOpenMPGlobalCapturedDecl(ValueDecl *D, unsigned Level,
2756 unsigned CaptureLevel) const {
2757 assert(getLangOpts().OpenMP && "OpenMP is not allowed");
2758 // Return true if the current level is no longer enclosed in a target region.
2759
2760 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
2761 if (!VD->hasLocalStorage()) {
2762 if (isInOpenMPTargetExecutionDirective())
2763 return true;
2764 DSAStackTy::DSAVarData TopDVar =
2765 DSAStack->getTopDSA(D, /*FromParent=*/false);
2766 unsigned NumLevels =
2767 getOpenMPCaptureLevels(DSAStack->getDirective(Level));
2768 if (Level == 0)
2769 // non-file scope static variable with default(firstprivate)
2770 // should be global captured.
2771 return (NumLevels == CaptureLevel + 1 &&
2772 (TopDVar.CKind != OMPC_shared ||
2773 DSAStack->getDefaultDSA() == DSA_firstprivate));
2774 do {
2775 --Level;
2776 DSAStackTy::DSAVarData DVar = DSAStack->getImplicitDSA(D, Level);
2777 if (DVar.CKind != OMPC_shared)
2778 return true;
2779 } while (Level > 0);
2780 }
2781 }
2782 return true;
2783}
2784
2785void SemaOpenMP::DestroyDataSharingAttributesStack() { delete DSAStack; }
2786
2787void SemaOpenMP::ActOnOpenMPBeginDeclareVariant(SourceLocation Loc,
2788 OMPTraitInfo &TI) {
2789 OMPDeclareVariantScopes.push_back(Elt: OMPDeclareVariantScope(TI));
2790}
2791
2792void SemaOpenMP::ActOnOpenMPEndDeclareVariant() {
2793 assert(isInOpenMPDeclareVariantScope() &&
2794 "Not in OpenMP declare variant scope!");
2795
2796 OMPDeclareVariantScopes.pop_back();
2797}
2798
2799void SemaOpenMP::finalizeOpenMPDelayedAnalysis(const FunctionDecl *Caller,
2800 const FunctionDecl *Callee,
2801 SourceLocation Loc) {
2802 assert(getLangOpts().OpenMP && "Expected OpenMP compilation mode.");
2803 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2804 OMPDeclareTargetDeclAttr::getDeviceType(VD: Caller->getMostRecentDecl());
2805 // Ignore host functions during device analysis.
2806 if (getLangOpts().OpenMPIsTargetDevice &&
2807 (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host))
2808 return;
2809 // Ignore nohost functions during host analysis.
2810 if (!getLangOpts().OpenMPIsTargetDevice && DevTy &&
2811 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost)
2812 return;
2813 const FunctionDecl *FD = Callee->getMostRecentDecl();
2814 DevTy = OMPDeclareTargetDeclAttr::getDeviceType(VD: FD);
2815 if (getLangOpts().OpenMPIsTargetDevice && DevTy &&
2816 *DevTy == OMPDeclareTargetDeclAttr::DT_Host) {
2817 // Diagnose host function called during device codegen.
2818 StringRef HostDevTy =
2819 getOpenMPSimpleClauseTypeName(Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_host);
2820 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << HostDevTy << 0;
2821 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2822 DiagID: diag::note_omp_marked_device_type_here)
2823 << HostDevTy;
2824 return;
2825 }
2826 if (!getLangOpts().OpenMPIsTargetDevice &&
2827 !getLangOpts().OpenMPOffloadMandatory && DevTy &&
2828 *DevTy == OMPDeclareTargetDeclAttr::DT_NoHost) {
2829 // In OpenMP 5.2 or later, if the function has a host variant then allow
2830 // that to be called instead
2831 auto &&HasHostAttr = [](const FunctionDecl *Callee) {
2832 for (OMPDeclareVariantAttr *A :
2833 Callee->specific_attrs<OMPDeclareVariantAttr>()) {
2834 auto *DeclRefVariant = cast<DeclRefExpr>(Val: A->getVariantFuncRef());
2835 auto *VariantFD = cast<FunctionDecl>(Val: DeclRefVariant->getDecl());
2836 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
2837 OMPDeclareTargetDeclAttr::getDeviceType(
2838 VD: VariantFD->getMostRecentDecl());
2839 if (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
2840 return true;
2841 }
2842 return false;
2843 };
2844 if (getLangOpts().OpenMP >= 52 &&
2845 Callee->hasAttr<OMPDeclareVariantAttr>() && HasHostAttr(Callee))
2846 return;
2847 // Diagnose nohost function called during host codegen.
2848 StringRef NoHostDevTy = getOpenMPSimpleClauseTypeName(
2849 Kind: OMPC_device_type, Type: OMPC_DEVICE_TYPE_nohost);
2850 Diag(Loc, DiagID: diag::err_omp_wrong_device_function_call) << NoHostDevTy << 1;
2851 Diag(Loc: *OMPDeclareTargetDeclAttr::getLocation(VD: FD),
2852 DiagID: diag::note_omp_marked_device_type_here)
2853 << NoHostDevTy;
2854 }
2855}
2856
2857void SemaOpenMP::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
2858 const DeclarationNameInfo &DirName,
2859 Scope *CurScope, SourceLocation Loc) {
2860 DSAStack->push(DKind, DirName, CurScope, Loc);
2861 SemaRef.PushExpressionEvaluationContext(
2862 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
2863}
2864
2865void SemaOpenMP::StartOpenMPClause(OpenMPClauseKind K) {
2866 DSAStack->setClauseParsingMode(K);
2867}
2868
2869void SemaOpenMP::EndOpenMPClause() {
2870 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
2871 SemaRef.CleanupVarDeclMarking();
2872}
2873
2874static std::pair<ValueDecl *, bool>
2875getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
2876 SourceRange &ERange, bool AllowArraySection = false,
2877 bool AllowAssumedSizeArray = false, StringRef DiagType = "");
2878
2879/// Check consistency of the reduction clauses.
2880static void checkReductionClauses(Sema &S, DSAStackTy *Stack,
2881 ArrayRef<OMPClause *> Clauses) {
2882 bool InscanFound = false;
2883 SourceLocation InscanLoc;
2884 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions.
2885 // A reduction clause without the inscan reduction-modifier may not appear on
2886 // a construct on which a reduction clause with the inscan reduction-modifier
2887 // appears.
2888 for (OMPClause *C : Clauses) {
2889 if (C->getClauseKind() != OMPC_reduction)
2890 continue;
2891 auto *RC = cast<OMPReductionClause>(Val: C);
2892 if (RC->getModifier() == OMPC_REDUCTION_inscan) {
2893 InscanFound = true;
2894 InscanLoc = RC->getModifierLoc();
2895 continue;
2896 }
2897 if (RC->getModifier() == OMPC_REDUCTION_task) {
2898 // OpenMP 5.0, 2.19.5.4 reduction Clause.
2899 // A reduction clause with the task reduction-modifier may only appear on
2900 // a parallel construct, a worksharing construct or a combined or
2901 // composite construct for which any of the aforementioned constructs is a
2902 // constituent construct and simd or loop are not constituent constructs.
2903 OpenMPDirectiveKind CurDir = Stack->getCurrentDirective();
2904 if (!(isOpenMPParallelDirective(DKind: CurDir) ||
2905 isOpenMPWorksharingDirective(DKind: CurDir)) ||
2906 isOpenMPSimdDirective(DKind: CurDir))
2907 S.Diag(Loc: RC->getModifierLoc(),
2908 DiagID: diag::err_omp_reduction_task_not_parallel_or_worksharing);
2909 continue;
2910 }
2911 }
2912 if (InscanFound) {
2913 for (OMPClause *C : Clauses) {
2914 if (C->getClauseKind() != OMPC_reduction)
2915 continue;
2916 auto *RC = cast<OMPReductionClause>(Val: C);
2917 if (RC->getModifier() != OMPC_REDUCTION_inscan) {
2918 S.Diag(Loc: RC->getModifier() == OMPC_REDUCTION_unknown
2919 ? RC->getBeginLoc()
2920 : RC->getModifierLoc(),
2921 DiagID: diag::err_omp_inscan_reduction_expected);
2922 S.Diag(Loc: InscanLoc, DiagID: diag::note_omp_previous_inscan_reduction);
2923 continue;
2924 }
2925 for (Expr *Ref : RC->varlist()) {
2926 assert(Ref && "NULL expr in OpenMP reduction clause.");
2927 SourceLocation ELoc;
2928 SourceRange ERange;
2929 Expr *SimpleRefExpr = Ref;
2930 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
2931 /*AllowArraySection=*/true);
2932 ValueDecl *D = Res.first;
2933 if (!D)
2934 continue;
2935 if (!Stack->isUsedInScanDirective(D: getCanonicalDecl(D))) {
2936 S.Diag(Loc: Ref->getExprLoc(),
2937 DiagID: diag::err_omp_reduction_not_inclusive_exclusive)
2938 << Ref->getSourceRange();
2939 }
2940 }
2941 }
2942 }
2943}
2944
2945static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
2946 ArrayRef<OMPClause *> Clauses);
2947static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2948 bool WithInit);
2949
2950static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2951 const ValueDecl *D,
2952 const DSAStackTy::DSAVarData &DVar,
2953 bool IsLoopIterVar = false);
2954
2955void SemaOpenMP::EndOpenMPDSABlock(Stmt *CurDirective) {
2956 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
2957 // A variable of class type (or array thereof) that appears in a lastprivate
2958 // clause requires an accessible, unambiguous default constructor for the
2959 // class type, unless the list item is also specified in a firstprivate
2960 // clause.
2961
2962 auto FinalizeLastprivate = [&](OMPLastprivateClause *Clause) {
2963 SmallVector<Expr *, 8> PrivateCopies;
2964 for (Expr *DE : Clause->varlist()) {
2965 if (DE->isValueDependent() || DE->isTypeDependent()) {
2966 PrivateCopies.push_back(Elt: nullptr);
2967 continue;
2968 }
2969 auto *DRE = cast<DeclRefExpr>(Val: DE->IgnoreParens());
2970 auto *VD = cast<VarDecl>(Val: DRE->getDecl());
2971 QualType Type = VD->getType().getNonReferenceType();
2972 const DSAStackTy::DSAVarData DVar =
2973 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
2974 if (DVar.CKind != OMPC_lastprivate) {
2975 // The variable is also a firstprivate, so initialization sequence
2976 // for private copy is generated already.
2977 PrivateCopies.push_back(Elt: nullptr);
2978 continue;
2979 }
2980 // Generate helper private variable and initialize it with the
2981 // default value. The address of the original variable is replaced
2982 // by the address of the new private variable in CodeGen. This new
2983 // variable is not added to IdResolver, so the code in the OpenMP
2984 // region uses original variable for proper diagnostics.
2985 VarDecl *VDPrivate = buildVarDecl(
2986 SemaRef, Loc: DE->getExprLoc(), Type: Type.getUnqualifiedType(), Name: VD->getName(),
2987 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr, OrigRef: DRE);
2988 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
2989 if (VDPrivate->isInvalidDecl()) {
2990 PrivateCopies.push_back(Elt: nullptr);
2991 continue;
2992 }
2993 PrivateCopies.push_back(Elt: buildDeclRefExpr(
2994 S&: SemaRef, D: VDPrivate, Ty: DE->getType(), Loc: DE->getExprLoc()));
2995 }
2996 Clause->setPrivateCopies(PrivateCopies);
2997 };
2998
2999 auto FinalizeNontemporal = [&](OMPNontemporalClause *Clause) {
3000 // Finalize nontemporal clause by handling private copies, if any.
3001 SmallVector<Expr *, 8> PrivateRefs;
3002 for (Expr *RefExpr : Clause->varlist()) {
3003 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
3004 SourceLocation ELoc;
3005 SourceRange ERange;
3006 Expr *SimpleRefExpr = RefExpr;
3007 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
3008 if (Res.second)
3009 // It will be analyzed later.
3010 PrivateRefs.push_back(Elt: RefExpr);
3011 ValueDecl *D = Res.first;
3012 if (!D)
3013 continue;
3014
3015 const DSAStackTy::DSAVarData DVar =
3016 DSAStack->getTopDSA(D, /*FromParent=*/false);
3017 PrivateRefs.push_back(Elt: DVar.PrivateCopy ? DVar.PrivateCopy
3018 : SimpleRefExpr);
3019 }
3020 Clause->setPrivateRefs(PrivateRefs);
3021 };
3022
3023 auto FinalizeAllocators = [&](OMPUsesAllocatorsClause *Clause) {
3024 for (unsigned I = 0, E = Clause->getNumberOfAllocators(); I < E; ++I) {
3025 OMPUsesAllocatorsClause::Data D = Clause->getAllocatorData(I);
3026 auto *DRE = dyn_cast<DeclRefExpr>(Val: D.Allocator->IgnoreParenImpCasts());
3027 if (!DRE)
3028 continue;
3029 ValueDecl *VD = DRE->getDecl();
3030 if (!VD || !isa<VarDecl>(Val: VD))
3031 continue;
3032 DSAStackTy::DSAVarData DVar =
3033 DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
3034 // OpenMP [2.12.5, target Construct]
3035 // Memory allocators that appear in a uses_allocators clause cannot
3036 // appear in other data-sharing attribute clauses or data-mapping
3037 // attribute clauses in the same construct.
3038 Expr *MapExpr = nullptr;
3039 if (DVar.RefExpr ||
3040 DSAStack->checkMappableExprComponentListsForDecl(
3041 VD, /*CurrentRegionOnly=*/true,
3042 Check: [VD, &MapExpr](
3043 OMPClauseMappableExprCommon::MappableExprComponentListRef
3044 MapExprComponents,
3045 OpenMPClauseKind C) {
3046 auto MI = MapExprComponents.rbegin();
3047 auto ME = MapExprComponents.rend();
3048 if (MI != ME &&
3049 MI->getAssociatedDeclaration()->getCanonicalDecl() ==
3050 VD->getCanonicalDecl()) {
3051 MapExpr = MI->getAssociatedExpression();
3052 return true;
3053 }
3054 return false;
3055 })) {
3056 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_allocator_used_in_clauses)
3057 << D.Allocator->getSourceRange();
3058 if (DVar.RefExpr)
3059 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
3060 else
3061 Diag(Loc: MapExpr->getExprLoc(), DiagID: diag::note_used_here)
3062 << MapExpr->getSourceRange();
3063 }
3064 }
3065 };
3066
3067 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(Val: CurDirective)) {
3068 for (OMPClause *C : D->clauses()) {
3069 if (auto *Clause = dyn_cast<OMPLastprivateClause>(Val: C)) {
3070 FinalizeLastprivate(Clause);
3071 } else if (auto *Clause = dyn_cast<OMPNontemporalClause>(Val: C)) {
3072 FinalizeNontemporal(Clause);
3073 } else if (auto *Clause = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
3074 FinalizeAllocators(Clause);
3075 }
3076 }
3077 // Check allocate clauses.
3078 if (!SemaRef.CurContext->isDependentContext())
3079 checkAllocateClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3080 checkReductionClauses(S&: SemaRef, DSAStack, Clauses: D->clauses());
3081 }
3082
3083 DSAStack->pop();
3084 SemaRef.DiscardCleanupsInEvaluationContext();
3085 SemaRef.PopExpressionEvaluationContext();
3086}
3087
3088static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
3089 Expr *NumIterations, Sema &SemaRef,
3090 Scope *S, DSAStackTy *Stack);
3091
3092static bool finishLinearClauses(Sema &SemaRef, ArrayRef<OMPClause *> Clauses,
3093 OMPLoopBasedDirective::HelperExprs &B,
3094 DSAStackTy *Stack) {
3095 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
3096 "loop exprs were not built");
3097
3098 if (SemaRef.CurContext->isDependentContext())
3099 return false;
3100
3101 // Finalize the clauses that need pre-built expressions for CodeGen.
3102 for (OMPClause *C : Clauses) {
3103 auto *LC = dyn_cast<OMPLinearClause>(Val: C);
3104 if (!LC)
3105 continue;
3106 if (FinishOpenMPLinearClause(Clause&: *LC, IV: cast<DeclRefExpr>(Val: B.IterationVarRef),
3107 NumIterations: B.NumIterations, SemaRef,
3108 S: SemaRef.getCurScope(), Stack))
3109 return true;
3110 }
3111
3112 return false;
3113}
3114
3115namespace {
3116
3117class VarDeclFilterCCC final : public CorrectionCandidateCallback {
3118private:
3119 Sema &SemaRef;
3120
3121public:
3122 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
3123 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3124 NamedDecl *ND = Candidate.getCorrectionDecl();
3125 if (const auto *VD = dyn_cast_or_null<VarDecl>(Val: ND)) {
3126 return VD->hasGlobalStorage() &&
3127 SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3128 S: SemaRef.getCurScope());
3129 }
3130 return false;
3131 }
3132
3133 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3134 return std::make_unique<VarDeclFilterCCC>(args&: *this);
3135 }
3136};
3137
3138class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
3139private:
3140 Sema &SemaRef;
3141
3142public:
3143 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
3144 bool ValidateCandidate(const TypoCorrection &Candidate) override {
3145 NamedDecl *ND = Candidate.getCorrectionDecl();
3146 if (ND && ((isa<VarDecl>(Val: ND) && ND->getKind() == Decl::Var) ||
3147 isa<FunctionDecl>(Val: ND))) {
3148 return SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(),
3149 S: SemaRef.getCurScope());
3150 }
3151 return false;
3152 }
3153
3154 std::unique_ptr<CorrectionCandidateCallback> clone() override {
3155 return std::make_unique<VarOrFuncDeclFilterCCC>(args&: *this);
3156 }
3157};
3158
3159} // namespace
3160
3161ExprResult SemaOpenMP::ActOnOpenMPIdExpression(Scope *CurScope,
3162 CXXScopeSpec &ScopeSpec,
3163 const DeclarationNameInfo &Id,
3164 OpenMPDirectiveKind Kind) {
3165 ASTContext &Context = getASTContext();
3166 unsigned OMPVersion = getLangOpts().OpenMP;
3167 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
3168 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
3169 /*ObjectType=*/QualType(),
3170 /*AllowBuiltinCreation=*/true);
3171
3172 if (Lookup.isAmbiguous())
3173 return ExprError();
3174
3175 VarDecl *VD;
3176 if (!Lookup.isSingleResult()) {
3177 VarDeclFilterCCC CCC(SemaRef);
3178 if (TypoCorrection Corrected =
3179 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
3180 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
3181 SemaRef.diagnoseTypo(
3182 Correction: Corrected,
3183 TypoDiag: SemaRef.PDiag(DiagID: Lookup.empty() ? diag::err_undeclared_var_use_suggest
3184 : diag::err_omp_expected_var_arg_suggest)
3185 << Id.getName());
3186 VD = Corrected.getCorrectionDeclAs<VarDecl>();
3187 } else {
3188 Diag(Loc: Id.getLoc(), DiagID: Lookup.empty() ? diag::err_undeclared_var_use
3189 : diag::err_omp_expected_var_arg)
3190 << Id.getName();
3191 return ExprError();
3192 }
3193 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
3194 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_expected_var_arg) << Id.getName();
3195 Diag(Loc: Lookup.getFoundDecl()->getLocation(), DiagID: diag::note_declared_at);
3196 return ExprError();
3197 }
3198 Lookup.suppressDiagnostics();
3199
3200 // OpenMP [2.9.2, Syntax, C/C++]
3201 // Variables must be file-scope, namespace-scope, or static block-scope.
3202 if ((Kind == OMPD_threadprivate || Kind == OMPD_groupprivate) &&
3203 !VD->hasGlobalStorage()) {
3204 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_global_var_arg)
3205 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << !VD->isStaticLocal();
3206 bool IsDecl =
3207 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3208 Diag(Loc: VD->getLocation(),
3209 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3210 << VD;
3211 return ExprError();
3212 }
3213
3214 VarDecl *CanonicalVD = VD->getCanonicalDecl();
3215 NamedDecl *ND = CanonicalVD;
3216 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
3217 // A threadprivate or groupprivate directive for file-scope variables must
3218 // appear outside any definition or declaration.
3219 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
3220 !SemaRef.getCurLexicalContext()->isTranslationUnit()) {
3221 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3222 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3223 bool IsDecl =
3224 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3225 Diag(Loc: VD->getLocation(),
3226 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3227 << VD;
3228 return ExprError();
3229 }
3230 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
3231 // A threadprivate or groupprivate directive for static class member
3232 // variables must appear in the class definition, in the same scope in which
3233 // the member variables are declared.
3234 if (CanonicalVD->isStaticDataMember() &&
3235 !CanonicalVD->getDeclContext()->Equals(DC: SemaRef.getCurLexicalContext())) {
3236 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3237 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3238 bool IsDecl =
3239 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3240 Diag(Loc: VD->getLocation(),
3241 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3242 << VD;
3243 return ExprError();
3244 }
3245 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
3246 // A threadprivate or groupprivate directive for namespace-scope variables
3247 // must appear outside any definition or declaration other than the
3248 // namespace definition itself.
3249 if (CanonicalVD->getDeclContext()->isNamespace() &&
3250 (!SemaRef.getCurLexicalContext()->isFileContext() ||
3251 !SemaRef.getCurLexicalContext()->Encloses(
3252 DC: CanonicalVD->getDeclContext()))) {
3253 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3254 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3255 bool IsDecl =
3256 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3257 Diag(Loc: VD->getLocation(),
3258 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3259 << VD;
3260 return ExprError();
3261 }
3262 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
3263 // A threadprivate or groupprivate directive for static block-scope
3264 // variables must appear in the scope of the variable and not in a nested
3265 // scope.
3266 if (CanonicalVD->isLocalVarDecl() && CurScope &&
3267 !SemaRef.isDeclInScope(D: ND, Ctx: SemaRef.getCurLexicalContext(), S: CurScope)) {
3268 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_scope)
3269 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3270 bool IsDecl =
3271 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3272 Diag(Loc: VD->getLocation(),
3273 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3274 << VD;
3275 return ExprError();
3276 }
3277
3278 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
3279 // A threadprivate or groupprivate directive must lexically precede all
3280 // references to any of the variables in its list.
3281 if ((Kind == OMPD_threadprivate && VD->isUsed() &&
3282 !DSAStack->isThreadPrivate(D: VD)) ||
3283 (Kind == OMPD_groupprivate && VD->isUsed())) {
3284 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_var_used)
3285 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion) << VD;
3286 return ExprError();
3287 }
3288
3289 QualType ExprType = VD->getType().getNonReferenceType();
3290 return DeclRefExpr::Create(Context, QualifierLoc: NestedNameSpecifierLoc(),
3291 TemplateKWLoc: SourceLocation(), D: VD,
3292 /*RefersToEnclosingVariableOrCapture=*/false,
3293 NameLoc: Id.getLoc(), T: ExprType, VK: VK_LValue);
3294}
3295
3296SemaOpenMP::DeclGroupPtrTy
3297SemaOpenMP::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
3298 ArrayRef<Expr *> VarList) {
3299 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
3300 SemaRef.CurContext->addDecl(D);
3301 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3302 }
3303 return nullptr;
3304}
3305
3306SemaOpenMP::DeclGroupPtrTy
3307SemaOpenMP::ActOnOpenMPGroupPrivateDirective(SourceLocation Loc,
3308 ArrayRef<Expr *> VarList) {
3309 if (!getLangOpts().OpenMP || getLangOpts().OpenMP < 60) {
3310 Diag(Loc, DiagID: diag::err_omp_unexpected_directive)
3311 << getOpenMPDirectiveName(D: OMPD_groupprivate, Ver: getLangOpts().OpenMP);
3312 return nullptr;
3313 }
3314 if (OMPGroupPrivateDecl *D = CheckOMPGroupPrivateDecl(Loc, VarList)) {
3315 SemaRef.CurContext->addDecl(D);
3316 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3317 }
3318 return nullptr;
3319}
3320
3321namespace {
3322class LocalVarRefChecker final
3323 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
3324 Sema &SemaRef;
3325
3326public:
3327 bool VisitDeclRefExpr(const DeclRefExpr *E) {
3328 if (const auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
3329 if (VD->hasLocalStorage()) {
3330 SemaRef.Diag(Loc: E->getBeginLoc(),
3331 DiagID: diag::err_omp_local_var_in_threadprivate_init)
3332 << E->getSourceRange();
3333 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_defined_here)
3334 << VD << VD->getSourceRange();
3335 return true;
3336 }
3337 }
3338 return false;
3339 }
3340 bool VisitStmt(const Stmt *S) {
3341 for (const Stmt *Child : S->children()) {
3342 if (Child && Visit(S: Child))
3343 return true;
3344 }
3345 return false;
3346 }
3347 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
3348};
3349} // namespace
3350
3351OMPThreadPrivateDecl *
3352SemaOpenMP::CheckOMPThreadPrivateDecl(SourceLocation Loc,
3353 ArrayRef<Expr *> VarList) {
3354 ASTContext &Context = getASTContext();
3355 SmallVector<Expr *, 8> Vars;
3356 for (Expr *RefExpr : VarList) {
3357 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3358 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3359 SourceLocation ILoc = DE->getExprLoc();
3360
3361 // Mark variable as used.
3362 VD->setReferenced();
3363 VD->markUsed(C&: Context);
3364
3365 QualType QType = VD->getType();
3366 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3367 // It will be analyzed later.
3368 Vars.push_back(Elt: DE);
3369 continue;
3370 }
3371
3372 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3373 // A threadprivate variable must not have an incomplete type.
3374 if (SemaRef.RequireCompleteType(
3375 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_threadprivate_incomplete_type)) {
3376 continue;
3377 }
3378
3379 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
3380 // A threadprivate variable must not have a reference type.
3381 if (VD->getType()->isReferenceType()) {
3382 unsigned OMPVersion = getLangOpts().OpenMP;
3383 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3384 << getOpenMPDirectiveName(D: OMPD_threadprivate, Ver: OMPVersion)
3385 << VD->getType();
3386 bool IsDecl =
3387 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3388 Diag(Loc: VD->getLocation(),
3389 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3390 << VD;
3391 continue;
3392 }
3393
3394 // Check if this is a TLS variable. If TLS is not being supported, produce
3395 // the corresponding diagnostic.
3396 if ((VD->getTLSKind() != VarDecl::TLS_None &&
3397 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
3398 getLangOpts().OpenMPUseTLS &&
3399 getASTContext().getTargetInfo().isTLSSupported())) ||
3400 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3401 !VD->isLocalVarDecl())) {
3402 Diag(Loc: ILoc, DiagID: diag::err_omp_var_thread_local)
3403 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
3404 bool IsDecl =
3405 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3406 Diag(Loc: VD->getLocation(),
3407 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3408 << VD;
3409 continue;
3410 }
3411
3412 // Check if initial value of threadprivate variable reference variable with
3413 // local storage (it is not supported by runtime).
3414 if (const Expr *Init = VD->getAnyInitializer()) {
3415 LocalVarRefChecker Checker(SemaRef);
3416 if (Checker.Visit(S: Init))
3417 continue;
3418 }
3419
3420 Vars.push_back(Elt: RefExpr);
3421 DSAStack->addDSA(D: VD, E: DE, A: OMPC_threadprivate);
3422 VD->addAttr(A: OMPThreadPrivateDeclAttr::CreateImplicit(
3423 Ctx&: Context, Range: SourceRange(Loc, Loc)));
3424 if (ASTMutationListener *ML = Context.getASTMutationListener())
3425 ML->DeclarationMarkedOpenMPThreadPrivate(D: VD);
3426 }
3427 OMPThreadPrivateDecl *D = nullptr;
3428 if (!Vars.empty()) {
3429 D = OMPThreadPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3430 L: Loc, VL: Vars);
3431 D->setAccess(AS_public);
3432 }
3433 return D;
3434}
3435
3436OMPGroupPrivateDecl *
3437SemaOpenMP::CheckOMPGroupPrivateDecl(SourceLocation Loc,
3438 ArrayRef<Expr *> VarList) {
3439 ASTContext &Context = getASTContext();
3440 SmallVector<Expr *, 8> Vars;
3441 for (Expr *RefExpr : VarList) {
3442 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3443 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3444 SourceLocation ILoc = DE->getExprLoc();
3445
3446 // Mark variable as used.
3447 VD->setReferenced();
3448 VD->markUsed(C&: Context);
3449
3450 QualType QType = VD->getType();
3451 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3452 // It will be analyzed later.
3453 Vars.push_back(Elt: DE);
3454 continue;
3455 }
3456
3457 // OpenMP groupprivate restrictions:
3458 // A groupprivate variable must not have an incomplete type.
3459 if (SemaRef.RequireCompleteType(
3460 Loc: ILoc, T: VD->getType(), DiagID: diag::err_omp_groupprivate_incomplete_type)) {
3461 continue;
3462 }
3463
3464 // A groupprivate variable must not have a reference type.
3465 if (VD->getType()->isReferenceType()) {
3466 Diag(Loc: ILoc, DiagID: diag::err_omp_ref_type_arg)
3467 << getOpenMPDirectiveName(D: OMPD_groupprivate) << VD->getType();
3468 bool IsDecl =
3469 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3470 Diag(Loc: VD->getLocation(),
3471 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3472 << VD;
3473 continue;
3474 }
3475
3476 // A variable that is declared with an initializer must not appear in a
3477 // groupprivate directive.
3478 if (VD->getAnyInitializer()) {
3479 Diag(Loc: ILoc, DiagID: diag::err_omp_groupprivate_with_initializer)
3480 << VD->getDeclName();
3481 bool IsDecl =
3482 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3483 Diag(Loc: VD->getLocation(),
3484 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3485 << VD;
3486 continue;
3487 }
3488
3489 Vars.push_back(Elt: RefExpr);
3490 DSAStack->addDSA(D: VD, E: DE, A: OMPC_groupprivate);
3491 VD->addAttr(A: OMPGroupPrivateDeclAttr::CreateImplicit(Ctx&: Context,
3492 Range: SourceRange(Loc, Loc)));
3493 if (ASTMutationListener *ML = Context.getASTMutationListener())
3494 ML->DeclarationMarkedOpenMPGroupPrivate(D: VD);
3495 }
3496 OMPGroupPrivateDecl *D = nullptr;
3497 if (!Vars.empty()) {
3498 D = OMPGroupPrivateDecl::Create(C&: Context, DC: SemaRef.getCurLexicalContext(),
3499 L: Loc, VL: Vars);
3500 D->setAccess(AS_public);
3501 }
3502 return D;
3503}
3504
3505static OMPAllocateDeclAttr::AllocatorTypeTy
3506getAllocatorKind(Sema &S, DSAStackTy *Stack, Expr *Allocator) {
3507 if (!Allocator)
3508 return OMPAllocateDeclAttr::OMPNullMemAlloc;
3509 if (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3510 Allocator->isInstantiationDependent() ||
3511 Allocator->containsUnexpandedParameterPack())
3512 return OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3513 auto AllocatorKindRes = OMPAllocateDeclAttr::OMPUserDefinedMemAlloc;
3514 llvm::FoldingSetNodeID AEId;
3515 const Expr *AE = Allocator->IgnoreParenImpCasts();
3516 AE->IgnoreImpCasts()->Profile(ID&: AEId, Context: S.getASTContext(), /*Canonical=*/true);
3517 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
3518 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
3519 const Expr *DefAllocator = Stack->getAllocator(AllocatorKind);
3520 llvm::FoldingSetNodeID DAEId;
3521 DefAllocator->IgnoreImpCasts()->Profile(ID&: DAEId, Context: S.getASTContext(),
3522 /*Canonical=*/true);
3523 if (AEId == DAEId) {
3524 AllocatorKindRes = AllocatorKind;
3525 break;
3526 }
3527 }
3528 return AllocatorKindRes;
3529}
3530
3531static bool checkPreviousOMPAllocateAttribute(
3532 Sema &S, DSAStackTy *Stack, Expr *RefExpr, VarDecl *VD,
3533 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind, Expr *Allocator) {
3534 if (!VD->hasAttr<OMPAllocateDeclAttr>())
3535 return false;
3536 const auto *A = VD->getAttr<OMPAllocateDeclAttr>();
3537 Expr *PrevAllocator = A->getAllocator();
3538 OMPAllocateDeclAttr::AllocatorTypeTy PrevAllocatorKind =
3539 getAllocatorKind(S, Stack, Allocator: PrevAllocator);
3540 bool AllocatorsMatch = AllocatorKind == PrevAllocatorKind;
3541 if (AllocatorsMatch &&
3542 AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc &&
3543 Allocator && PrevAllocator) {
3544 const Expr *AE = Allocator->IgnoreParenImpCasts();
3545 const Expr *PAE = PrevAllocator->IgnoreParenImpCasts();
3546 llvm::FoldingSetNodeID AEId, PAEId;
3547 AE->Profile(ID&: AEId, Context: S.Context, /*Canonical=*/true);
3548 PAE->Profile(ID&: PAEId, Context: S.Context, /*Canonical=*/true);
3549 AllocatorsMatch = AEId == PAEId;
3550 }
3551 if (!AllocatorsMatch) {
3552 SmallString<256> AllocatorBuffer;
3553 llvm::raw_svector_ostream AllocatorStream(AllocatorBuffer);
3554 if (Allocator)
3555 Allocator->printPretty(OS&: AllocatorStream, Helper: nullptr, Policy: S.getPrintingPolicy());
3556 SmallString<256> PrevAllocatorBuffer;
3557 llvm::raw_svector_ostream PrevAllocatorStream(PrevAllocatorBuffer);
3558 if (PrevAllocator)
3559 PrevAllocator->printPretty(OS&: PrevAllocatorStream, Helper: nullptr,
3560 Policy: S.getPrintingPolicy());
3561
3562 SourceLocation AllocatorLoc =
3563 Allocator ? Allocator->getExprLoc() : RefExpr->getExprLoc();
3564 SourceRange AllocatorRange =
3565 Allocator ? Allocator->getSourceRange() : RefExpr->getSourceRange();
3566 SourceLocation PrevAllocatorLoc =
3567 PrevAllocator ? PrevAllocator->getExprLoc() : A->getLocation();
3568 SourceRange PrevAllocatorRange =
3569 PrevAllocator ? PrevAllocator->getSourceRange() : A->getRange();
3570 S.Diag(Loc: AllocatorLoc, DiagID: diag::warn_omp_used_different_allocator)
3571 << (Allocator ? 1 : 0) << AllocatorStream.str()
3572 << (PrevAllocator ? 1 : 0) << PrevAllocatorStream.str()
3573 << AllocatorRange;
3574 S.Diag(Loc: PrevAllocatorLoc, DiagID: diag::note_omp_previous_allocator)
3575 << PrevAllocatorRange;
3576 return true;
3577 }
3578 return false;
3579}
3580
3581static void
3582applyOMPAllocateAttribute(Sema &S, VarDecl *VD,
3583 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind,
3584 Expr *Allocator, Expr *Alignment, SourceRange SR) {
3585 if (VD->hasAttr<OMPAllocateDeclAttr>())
3586 return;
3587 if (Alignment &&
3588 (Alignment->isTypeDependent() || Alignment->isValueDependent() ||
3589 Alignment->isInstantiationDependent() ||
3590 Alignment->containsUnexpandedParameterPack()))
3591 // Apply later when we have a usable value.
3592 return;
3593 if (Allocator &&
3594 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
3595 Allocator->isInstantiationDependent() ||
3596 Allocator->containsUnexpandedParameterPack()))
3597 return;
3598 auto *A = OMPAllocateDeclAttr::CreateImplicit(Ctx&: S.Context, AllocatorType: AllocatorKind,
3599 Allocator, Alignment, Range: SR);
3600 VD->addAttr(A);
3601 if (ASTMutationListener *ML = S.Context.getASTMutationListener())
3602 ML->DeclarationMarkedOpenMPAllocate(D: VD, A);
3603}
3604
3605SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPAllocateDirective(
3606 SourceLocation Loc, ArrayRef<Expr *> VarList, ArrayRef<OMPClause *> Clauses,
3607 DeclContext *Owner) {
3608 assert(Clauses.size() <= 2 && "Expected at most two clauses.");
3609 Expr *Alignment = nullptr;
3610 Expr *Allocator = nullptr;
3611 if (Clauses.empty()) {
3612 // OpenMP 5.0, 2.11.3 allocate Directive, Restrictions.
3613 // allocate directives that appear in a target region must specify an
3614 // allocator clause unless a requires directive with the dynamic_allocators
3615 // clause is present in the same compilation unit.
3616 if (getLangOpts().OpenMPIsTargetDevice &&
3617 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
3618 SemaRef.targetDiag(Loc, DiagID: diag::err_expected_allocator_clause);
3619 } else {
3620 for (const OMPClause *C : Clauses)
3621 if (const auto *AC = dyn_cast<OMPAllocatorClause>(Val: C))
3622 Allocator = AC->getAllocator();
3623 else if (const auto *AC = dyn_cast<OMPAlignClause>(Val: C))
3624 Alignment = AC->getAlignment();
3625 else
3626 llvm_unreachable("Unexpected clause on allocate directive");
3627 }
3628 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
3629 getAllocatorKind(S&: SemaRef, DSAStack, Allocator);
3630 SmallVector<Expr *, 8> Vars;
3631 for (Expr *RefExpr : VarList) {
3632 auto *DE = cast<DeclRefExpr>(Val: RefExpr);
3633 auto *VD = cast<VarDecl>(Val: DE->getDecl());
3634
3635 // Check if this is a TLS variable or global register.
3636 if (VD->getTLSKind() != VarDecl::TLS_None ||
3637 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
3638 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
3639 !VD->isLocalVarDecl()))
3640 continue;
3641
3642 // If the used several times in the allocate directive, the same allocator
3643 // must be used.
3644 if (checkPreviousOMPAllocateAttribute(S&: SemaRef, DSAStack, RefExpr, VD,
3645 AllocatorKind, Allocator))
3646 continue;
3647
3648 // OpenMP, 2.11.3 allocate Directive, Restrictions, C / C++
3649 // If a list item has a static storage type, the allocator expression in the
3650 // allocator clause must be a constant expression that evaluates to one of
3651 // the predefined memory allocator values.
3652 if (Allocator && VD->hasGlobalStorage()) {
3653 if (AllocatorKind == OMPAllocateDeclAttr::OMPUserDefinedMemAlloc) {
3654 Diag(Loc: Allocator->getExprLoc(),
3655 DiagID: diag::err_omp_expected_predefined_allocator)
3656 << Allocator->getSourceRange();
3657 bool IsDecl = VD->isThisDeclarationADefinition(getASTContext()) ==
3658 VarDecl::DeclarationOnly;
3659 Diag(Loc: VD->getLocation(),
3660 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3661 << VD;
3662 continue;
3663 }
3664 }
3665
3666 Vars.push_back(Elt: RefExpr);
3667 applyOMPAllocateAttribute(S&: SemaRef, VD, AllocatorKind, Allocator, Alignment,
3668 SR: DE->getSourceRange());
3669 }
3670 if (Vars.empty())
3671 return nullptr;
3672 if (!Owner)
3673 Owner = SemaRef.getCurLexicalContext();
3674 auto *D = OMPAllocateDecl::Create(C&: getASTContext(), DC: Owner, L: Loc, VL: Vars, CL: Clauses);
3675 D->setAccess(AS_public);
3676 Owner->addDecl(D);
3677 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3678}
3679
3680SemaOpenMP::DeclGroupPtrTy
3681SemaOpenMP::ActOnOpenMPRequiresDirective(SourceLocation Loc,
3682 ArrayRef<OMPClause *> ClauseList) {
3683 OMPRequiresDecl *D = nullptr;
3684 if (!SemaRef.CurContext->isFileContext()) {
3685 Diag(Loc, DiagID: diag::err_omp_invalid_scope) << "requires";
3686 } else {
3687 D = CheckOMPRequiresDecl(Loc, Clauses: ClauseList);
3688 if (D) {
3689 SemaRef.CurContext->addDecl(D);
3690 DSAStack->addRequiresDecl(RD: D);
3691 }
3692 }
3693 return DeclGroupPtrTy::make(P: DeclGroupRef(D));
3694}
3695
3696void SemaOpenMP::ActOnOpenMPAssumesDirective(SourceLocation Loc,
3697 OpenMPDirectiveKind DKind,
3698 ArrayRef<std::string> Assumptions,
3699 bool SkippedClauses) {
3700 if (!SkippedClauses && Assumptions.empty()) {
3701 unsigned OMPVersion = getLangOpts().OpenMP;
3702 Diag(Loc, DiagID: diag::err_omp_no_clause_for_directive)
3703 << llvm::omp::getAllAssumeClauseOptions()
3704 << llvm::omp::getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
3705 }
3706
3707 auto *AA =
3708 OMPAssumeAttr::Create(Ctx&: getASTContext(), Assumption: llvm::join(R&: Assumptions, Separator: ","), Range: Loc);
3709 if (DKind == llvm::omp::Directive::OMPD_begin_assumes) {
3710 OMPAssumeScoped.push_back(Elt: AA);
3711 return;
3712 }
3713
3714 // Global assumes without assumption clauses are ignored.
3715 if (Assumptions.empty())
3716 return;
3717
3718 assert(DKind == llvm::omp::Directive::OMPD_assumes &&
3719 "Unexpected omp assumption directive!");
3720 OMPAssumeGlobal.push_back(Elt: AA);
3721
3722 // The OMPAssumeGlobal scope above will take care of new declarations but
3723 // we also want to apply the assumption to existing ones, e.g., to
3724 // declarations in included headers. To this end, we traverse all existing
3725 // declaration contexts and annotate function declarations here.
3726 SmallVector<DeclContext *, 8> DeclContexts;
3727 auto *Ctx = SemaRef.CurContext;
3728 while (Ctx->getLexicalParent())
3729 Ctx = Ctx->getLexicalParent();
3730 DeclContexts.push_back(Elt: Ctx);
3731 while (!DeclContexts.empty()) {
3732 DeclContext *DC = DeclContexts.pop_back_val();
3733 for (auto *SubDC : DC->decls()) {
3734 if (SubDC->isInvalidDecl())
3735 continue;
3736 if (auto *CTD = dyn_cast<ClassTemplateDecl>(Val: SubDC)) {
3737 DeclContexts.push_back(Elt: CTD->getTemplatedDecl());
3738 llvm::append_range(C&: DeclContexts, R: CTD->specializations());
3739 continue;
3740 }
3741 if (auto *DC = dyn_cast<DeclContext>(Val: SubDC))
3742 DeclContexts.push_back(Elt: DC);
3743 if (auto *F = dyn_cast<FunctionDecl>(Val: SubDC)) {
3744 F->addAttr(A: AA);
3745 continue;
3746 }
3747 }
3748 }
3749}
3750
3751void SemaOpenMP::ActOnOpenMPEndAssumesDirective() {
3752 assert(isInOpenMPAssumeScope() && "Not in OpenMP assumes scope!");
3753 OMPAssumeScoped.pop_back();
3754}
3755
3756StmtResult SemaOpenMP::ActOnOpenMPAssumeDirective(ArrayRef<OMPClause *> Clauses,
3757 Stmt *AStmt,
3758 SourceLocation StartLoc,
3759 SourceLocation EndLoc) {
3760 if (!AStmt)
3761 return StmtError();
3762
3763 return OMPAssumeDirective::Create(Ctx: getASTContext(), StartLoc, EndLoc, Clauses,
3764 AStmt);
3765}
3766
3767OMPRequiresDecl *
3768SemaOpenMP::CheckOMPRequiresDecl(SourceLocation Loc,
3769 ArrayRef<OMPClause *> ClauseList) {
3770 /// For target specific clauses, the requires directive cannot be
3771 /// specified after the handling of any of the target regions in the
3772 /// current compilation unit.
3773 ArrayRef<SourceLocation> TargetLocations =
3774 DSAStack->getEncounteredTargetLocs();
3775 SourceLocation AtomicLoc = DSAStack->getAtomicDirectiveLoc();
3776 if (!TargetLocations.empty() || !AtomicLoc.isInvalid()) {
3777 for (const OMPClause *CNew : ClauseList) {
3778 // Check if any of the requires clauses affect target regions.
3779 if (isa<OMPUnifiedSharedMemoryClause>(Val: CNew) ||
3780 isa<OMPUnifiedAddressClause>(Val: CNew) ||
3781 isa<OMPReverseOffloadClause>(Val: CNew) ||
3782 isa<OMPDynamicAllocatorsClause>(Val: CNew)) {
3783 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3784 << "target" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3785 for (SourceLocation TargetLoc : TargetLocations) {
3786 Diag(Loc: TargetLoc, DiagID: diag::note_omp_requires_encountered_directive)
3787 << "target";
3788 }
3789 } else if (!AtomicLoc.isInvalid() &&
3790 isa<OMPAtomicDefaultMemOrderClause>(Val: CNew)) {
3791 Diag(Loc, DiagID: diag::err_omp_directive_before_requires)
3792 << "atomic" << getOpenMPClauseNameForDiag(C: CNew->getClauseKind());
3793 Diag(Loc: AtomicLoc, DiagID: diag::note_omp_requires_encountered_directive)
3794 << "atomic";
3795 }
3796 }
3797 }
3798
3799 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
3800 return OMPRequiresDecl::Create(
3801 C&: getASTContext(), DC: SemaRef.getCurLexicalContext(), L: Loc, CL: ClauseList);
3802 return nullptr;
3803}
3804
3805static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
3806 const ValueDecl *D,
3807 const DSAStackTy::DSAVarData &DVar,
3808 bool IsLoopIterVar) {
3809 if (DVar.RefExpr) {
3810 SemaRef.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
3811 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3812 return;
3813 }
3814 enum {
3815 PDSA_StaticMemberShared,
3816 PDSA_StaticLocalVarShared,
3817 PDSA_LoopIterVarPrivate,
3818 PDSA_LoopIterVarLinear,
3819 PDSA_LoopIterVarLastprivate,
3820 PDSA_ConstVarShared,
3821 PDSA_GlobalVarShared,
3822 PDSA_TaskVarFirstprivate,
3823 PDSA_LocalVarPrivate,
3824 PDSA_Implicit
3825 } Reason = PDSA_Implicit;
3826 bool ReportHint = false;
3827 auto ReportLoc = D->getLocation();
3828 auto *VD = dyn_cast<VarDecl>(Val: D);
3829 if (IsLoopIterVar) {
3830 if (DVar.CKind == OMPC_private)
3831 Reason = PDSA_LoopIterVarPrivate;
3832 else if (DVar.CKind == OMPC_lastprivate)
3833 Reason = PDSA_LoopIterVarLastprivate;
3834 else
3835 Reason = PDSA_LoopIterVarLinear;
3836 } else if (isOpenMPTaskingDirective(Kind: DVar.DKind) &&
3837 DVar.CKind == OMPC_firstprivate) {
3838 Reason = PDSA_TaskVarFirstprivate;
3839 ReportLoc = DVar.ImplicitDSALoc;
3840 } else if (VD && VD->isStaticLocal())
3841 Reason = PDSA_StaticLocalVarShared;
3842 else if (VD && VD->isStaticDataMember())
3843 Reason = PDSA_StaticMemberShared;
3844 else if (VD && VD->isFileVarDecl())
3845 Reason = PDSA_GlobalVarShared;
3846 else if (D->getType().isConstant(Ctx: SemaRef.getASTContext()))
3847 Reason = PDSA_ConstVarShared;
3848 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
3849 ReportHint = true;
3850 Reason = PDSA_LocalVarPrivate;
3851 }
3852 if (Reason != PDSA_Implicit) {
3853 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
3854 SemaRef.Diag(Loc: ReportLoc, DiagID: diag::note_omp_predetermined_dsa)
3855 << Reason << ReportHint
3856 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), Ver: OMPVersion);
3857 } else if (DVar.ImplicitDSALoc.isValid()) {
3858 SemaRef.Diag(Loc: DVar.ImplicitDSALoc, DiagID: diag::note_omp_implicit_dsa)
3859 << getOpenMPClauseNameForDiag(C: DVar.CKind);
3860 }
3861}
3862
3863static OpenMPMapClauseKind
3864getMapClauseKindFromModifier(OpenMPDefaultmapClauseModifier M,
3865 bool IsAggregateOrDeclareTarget) {
3866 OpenMPMapClauseKind Kind = OMPC_MAP_unknown;
3867 switch (M) {
3868 case OMPC_DEFAULTMAP_MODIFIER_alloc:
3869 case OMPC_DEFAULTMAP_MODIFIER_storage:
3870 Kind = OMPC_MAP_alloc;
3871 break;
3872 case OMPC_DEFAULTMAP_MODIFIER_to:
3873 Kind = OMPC_MAP_to;
3874 break;
3875 case OMPC_DEFAULTMAP_MODIFIER_from:
3876 Kind = OMPC_MAP_from;
3877 break;
3878 case OMPC_DEFAULTMAP_MODIFIER_tofrom:
3879 Kind = OMPC_MAP_tofrom;
3880 break;
3881 case OMPC_DEFAULTMAP_MODIFIER_present:
3882 // OpenMP 5.1 [2.21.7.3] defaultmap clause, Description]
3883 // If implicit-behavior is present, each variable referenced in the
3884 // construct in the category specified by variable-category is treated as if
3885 // it had been listed in a map clause with the map-type of alloc and
3886 // map-type-modifier of present.
3887 Kind = OMPC_MAP_alloc;
3888 break;
3889 case OMPC_DEFAULTMAP_MODIFIER_firstprivate:
3890 case OMPC_DEFAULTMAP_MODIFIER_private:
3891 case OMPC_DEFAULTMAP_MODIFIER_last:
3892 llvm_unreachable("Unexpected defaultmap implicit behavior");
3893 case OMPC_DEFAULTMAP_MODIFIER_none:
3894 case OMPC_DEFAULTMAP_MODIFIER_default:
3895 case OMPC_DEFAULTMAP_MODIFIER_unknown:
3896 // IsAggregateOrDeclareTarget could be true if:
3897 // 1. the implicit behavior for aggregate is tofrom
3898 // 2. it's a declare target link
3899 if (IsAggregateOrDeclareTarget) {
3900 Kind = OMPC_MAP_tofrom;
3901 break;
3902 }
3903 llvm_unreachable("Unexpected defaultmap implicit behavior");
3904 }
3905 assert(Kind != OMPC_MAP_unknown && "Expect map kind to be known");
3906 return Kind;
3907}
3908
3909namespace {
3910struct VariableImplicitInfo {
3911 static const unsigned MapKindNum = OMPC_MAP_unknown;
3912 static const unsigned DefaultmapKindNum = OMPC_DEFAULTMAP_unknown + 1;
3913
3914 llvm::SetVector<Expr *> Privates;
3915 llvm::SetVector<Expr *> Firstprivates;
3916 llvm::SetVector<Expr *> Mappings[DefaultmapKindNum][MapKindNum];
3917 llvm::SmallVector<OpenMPMapModifierKind, NumberOfOMPMapClauseModifiers>
3918 MapModifiers[DefaultmapKindNum];
3919};
3920
3921class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
3922 DSAStackTy *Stack;
3923 Sema &SemaRef;
3924 OpenMPDirectiveKind DKind = OMPD_unknown;
3925 bool ErrorFound = false;
3926 bool TryCaptureCXXThisMembers = false;
3927 CapturedStmt *CS = nullptr;
3928
3929 VariableImplicitInfo ImpInfo;
3930 SemaOpenMP::VarsWithInheritedDSAType VarsWithInheritedDSA;
3931 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
3932
3933 void VisitSubCaptures(OMPExecutableDirective *S) {
3934 // Check implicitly captured variables.
3935 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
3936 return;
3937 if (S->getDirectiveKind() == OMPD_atomic ||
3938 S->getDirectiveKind() == OMPD_critical ||
3939 S->getDirectiveKind() == OMPD_section ||
3940 S->getDirectiveKind() == OMPD_master ||
3941 S->getDirectiveKind() == OMPD_masked ||
3942 S->getDirectiveKind() == OMPD_scope ||
3943 S->getDirectiveKind() == OMPD_assume ||
3944 isOpenMPLoopTransformationDirective(DKind: S->getDirectiveKind())) {
3945 Visit(S: S->getAssociatedStmt());
3946 return;
3947 }
3948 visitSubCaptures(S: S->getInnermostCapturedStmt());
3949 // Try to capture inner this->member references to generate correct mappings
3950 // and diagnostics.
3951 if (TryCaptureCXXThisMembers ||
3952 (isOpenMPTargetExecutionDirective(DKind) &&
3953 llvm::any_of(Range: S->getInnermostCapturedStmt()->captures(),
3954 P: [](const CapturedStmt::Capture &C) {
3955 return C.capturesThis();
3956 }))) {
3957 bool SavedTryCaptureCXXThisMembers = TryCaptureCXXThisMembers;
3958 TryCaptureCXXThisMembers = true;
3959 Visit(S: S->getInnermostCapturedStmt()->getCapturedStmt());
3960 TryCaptureCXXThisMembers = SavedTryCaptureCXXThisMembers;
3961 }
3962 // In tasks firstprivates are not captured anymore, need to analyze them
3963 // explicitly.
3964 if (isOpenMPTaskingDirective(Kind: S->getDirectiveKind()) &&
3965 !isOpenMPTaskLoopDirective(DKind: S->getDirectiveKind())) {
3966 for (OMPClause *C : S->clauses())
3967 if (auto *FC = dyn_cast<OMPFirstprivateClause>(Val: C)) {
3968 for (Expr *Ref : FC->varlist())
3969 Visit(S: Ref);
3970 }
3971 }
3972 }
3973
3974public:
3975 void VisitDeclRefExpr(DeclRefExpr *E) {
3976 if (TryCaptureCXXThisMembers || E->isTypeDependent() ||
3977 E->isValueDependent() || E->containsUnexpandedParameterPack() ||
3978 E->isInstantiationDependent() ||
3979 E->isNonOdrUse() == clang::NOUR_Unevaluated)
3980 return;
3981 if (auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
3982 // Check the datasharing rules for the expressions in the clauses.
3983 if (!CS || (isa<OMPCapturedExprDecl>(Val: VD) && !CS->capturesVariable(Var: VD) &&
3984 !Stack->getTopDSA(D: VD, /*FromParent=*/false).RefExpr &&
3985 !Stack->isImplicitDefaultFirstprivateFD(VD))) {
3986 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: VD))
3987 if (!CED->hasAttr<OMPCaptureNoInitAttr>()) {
3988 Visit(S: CED->getInit());
3989 return;
3990 }
3991 } else if (VD->isImplicit() || isa<OMPCapturedExprDecl>(Val: VD))
3992 // Do not analyze internal variables and do not enclose them into
3993 // implicit clauses.
3994 if (!Stack->isImplicitDefaultFirstprivateFD(VD))
3995 return;
3996 VD = VD->getCanonicalDecl();
3997 // Skip internally declared variables.
3998 if (VD->hasLocalStorage() && CS && !CS->capturesVariable(Var: VD) &&
3999 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4000 !Stack->isImplicitTaskFirstprivate(D: VD))
4001 return;
4002 // Skip allocators in uses_allocators clauses.
4003 if (Stack->isUsesAllocatorsDecl(D: VD))
4004 return;
4005
4006 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
4007 // Check if the variable has explicit DSA set and stop analysis if it so.
4008 if (DVar.RefExpr || !ImplicitDeclarations.insert(V: VD).second)
4009 return;
4010
4011 // Skip internally declared static variables.
4012 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
4013 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
4014 if (VD->hasGlobalStorage() && CS && !CS->capturesVariable(Var: VD) &&
4015 (Stack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
4016 !Res || *Res != OMPDeclareTargetDeclAttr::MT_Link) &&
4017 !Stack->isImplicitDefaultFirstprivateFD(VD) &&
4018 !Stack->isImplicitTaskFirstprivate(D: VD))
4019 return;
4020
4021 SourceLocation ELoc = E->getExprLoc();
4022 // The default(none) clause requires that each variable that is referenced
4023 // in the construct, and does not have a predetermined data-sharing
4024 // attribute, must have its data-sharing attribute explicitly determined
4025 // by being listed in a data-sharing attribute clause.
4026 if (DVar.CKind == OMPC_unknown &&
4027 (Stack->getDefaultDSA() == DSA_none ||
4028 Stack->getDefaultDSA() == DSA_private ||
4029 Stack->getDefaultDSA() == DSA_firstprivate) &&
4030 isImplicitOrExplicitTaskingRegion(DKind) &&
4031 VarsWithInheritedDSA.count(Val: VD) == 0) {
4032 bool InheritedDSA = Stack->getDefaultDSA() == DSA_none;
4033 if (!InheritedDSA && (Stack->getDefaultDSA() == DSA_firstprivate ||
4034 Stack->getDefaultDSA() == DSA_private)) {
4035 DSAStackTy::DSAVarData DVar =
4036 Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4037 InheritedDSA = DVar.CKind == OMPC_unknown;
4038 }
4039 if (InheritedDSA)
4040 VarsWithInheritedDSA[VD] = E;
4041 if (Stack->getDefaultDSA() == DSA_none)
4042 return;
4043 }
4044
4045 // OpenMP 5.0 [2.19.7.2, defaultmap clause, Description]
4046 // If implicit-behavior is none, each variable referenced in the
4047 // construct that does not have a predetermined data-sharing attribute
4048 // and does not appear in a to or link clause on a declare target
4049 // directive must be listed in a data-mapping attribute clause, a
4050 // data-sharing attribute clause (including a data-sharing attribute
4051 // clause on a combined construct where target. is one of the
4052 // constituent constructs), or an is_device_ptr clause.
4053 OpenMPDefaultmapClauseKind ClauseKind =
4054 getVariableCategoryFromDecl(LO: SemaRef.getLangOpts(), VD);
4055 if (SemaRef.getLangOpts().OpenMP >= 50) {
4056 bool IsModifierNone = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4057 OMPC_DEFAULTMAP_MODIFIER_none;
4058 if (DVar.CKind == OMPC_unknown && IsModifierNone &&
4059 VarsWithInheritedDSA.count(Val: VD) == 0 && !Res) {
4060 // Only check for data-mapping attribute and is_device_ptr here
4061 // since we have already make sure that the declaration does not
4062 // have a data-sharing attribute above
4063 if (!Stack->checkMappableExprComponentListsForDecl(
4064 VD, /*CurrentRegionOnly=*/true,
4065 Check: [VD](OMPClauseMappableExprCommon::MappableExprComponentListRef
4066 MapExprComponents,
4067 OpenMPClauseKind) {
4068 auto MI = MapExprComponents.rbegin();
4069 auto ME = MapExprComponents.rend();
4070 return MI != ME && MI->getAssociatedDeclaration() == VD;
4071 })) {
4072 VarsWithInheritedDSA[VD] = E;
4073 return;
4074 }
4075 }
4076 }
4077 if (SemaRef.getLangOpts().OpenMP > 50) {
4078 bool IsModifierPresent = Stack->getDefaultmapModifier(Kind: ClauseKind) ==
4079 OMPC_DEFAULTMAP_MODIFIER_present;
4080 if (IsModifierPresent) {
4081 if (!llvm::is_contained(Range&: ImpInfo.MapModifiers[ClauseKind],
4082 Element: OMPC_MAP_MODIFIER_present)) {
4083 ImpInfo.MapModifiers[ClauseKind].push_back(
4084 Elt: OMPC_MAP_MODIFIER_present);
4085 }
4086 }
4087 }
4088
4089 if (isOpenMPTargetExecutionDirective(DKind) &&
4090 !Stack->isLoopControlVariable(D: VD).first) {
4091 if (!Stack->checkMappableExprComponentListsForDecl(
4092 VD, /*CurrentRegionOnly=*/true,
4093 Check: [this](OMPClauseMappableExprCommon::MappableExprComponentListRef
4094 StackComponents,
4095 OpenMPClauseKind) {
4096 if (SemaRef.LangOpts.OpenMP >= 50)
4097 return !StackComponents.empty();
4098 // Variable is used if it has been marked as an array, array
4099 // section, array shaping or the variable itself.
4100 return StackComponents.size() == 1 ||
4101 llvm::all_of(
4102 Range: llvm::drop_begin(RangeOrContainer: llvm::reverse(C&: StackComponents)),
4103 P: [](const OMPClauseMappableExprCommon::
4104 MappableComponent &MC) {
4105 return MC.getAssociatedDeclaration() ==
4106 nullptr &&
4107 (isa<ArraySectionExpr>(
4108 Val: MC.getAssociatedExpression()) ||
4109 isa<OMPArrayShapingExpr>(
4110 Val: MC.getAssociatedExpression()) ||
4111 isa<ArraySubscriptExpr>(
4112 Val: MC.getAssociatedExpression()));
4113 });
4114 })) {
4115 bool IsFirstprivate = false;
4116 // By default lambdas are captured as firstprivates.
4117 if (const auto *RD =
4118 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
4119 IsFirstprivate = RD->isLambda();
4120 IsFirstprivate =
4121 IsFirstprivate || (Stack->mustBeFirstprivate(Kind: ClauseKind) && !Res);
4122 if (IsFirstprivate) {
4123 ImpInfo.Firstprivates.insert(X: E);
4124 } else {
4125 OpenMPDefaultmapClauseModifier M =
4126 Stack->getDefaultmapModifier(Kind: ClauseKind);
4127 if (M == OMPC_DEFAULTMAP_MODIFIER_private) {
4128 ImpInfo.Privates.insert(X: E);
4129 } else {
4130 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4131 M, IsAggregateOrDeclareTarget: ClauseKind == OMPC_DEFAULTMAP_aggregate || Res);
4132 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4133 }
4134 }
4135 return;
4136 }
4137 }
4138
4139 // OpenMP [2.9.3.6, Restrictions, p.2]
4140 // A list item that appears in a reduction clause of the innermost
4141 // enclosing worksharing or parallel construct may not be accessed in an
4142 // explicit task.
4143 DVar = Stack->hasInnermostDSA(
4144 D: VD,
4145 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4146 return C == OMPC_reduction && !AppliedToPointee;
4147 },
4148 DPred: [](OpenMPDirectiveKind K) {
4149 return isOpenMPParallelDirective(DKind: K) ||
4150 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4151 },
4152 /*FromParent=*/true);
4153 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4154 ErrorFound = true;
4155 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4156 reportOriginalDsa(SemaRef, Stack, D: VD, DVar);
4157 return;
4158 }
4159
4160 // Define implicit data-sharing attributes for task.
4161 DVar = Stack->getImplicitDSA(D: VD, /*FromParent=*/false);
4162 if (((isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared) ||
4163 (((Stack->getDefaultDSA() == DSA_firstprivate &&
4164 DVar.CKind == OMPC_firstprivate) ||
4165 (Stack->getDefaultDSA() == DSA_private &&
4166 DVar.CKind == OMPC_private)) &&
4167 !DVar.RefExpr)) &&
4168 !Stack->isLoopControlVariable(D: VD).first) {
4169 if (Stack->getDefaultDSA() == DSA_private)
4170 ImpInfo.Privates.insert(X: E);
4171 else
4172 ImpInfo.Firstprivates.insert(X: E);
4173 return;
4174 }
4175
4176 // Store implicitly used globals with declare target link for parent
4177 // target.
4178 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
4179 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
4180 Stack->addToParentTargetRegionLinkGlobals(E);
4181 return;
4182 }
4183 }
4184 }
4185 void VisitMemberExpr(MemberExpr *E) {
4186 if (E->isTypeDependent() || E->isValueDependent() ||
4187 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
4188 return;
4189 auto *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
4190 if (auto *TE = dyn_cast<CXXThisExpr>(Val: E->getBase()->IgnoreParenCasts())) {
4191 if (!FD)
4192 return;
4193 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: FD, /*FromParent=*/false);
4194 // Check if the variable has explicit DSA set and stop analysis if it
4195 // so.
4196 if (DVar.RefExpr || !ImplicitDeclarations.insert(V: FD).second)
4197 return;
4198
4199 if (isOpenMPTargetExecutionDirective(DKind) &&
4200 !Stack->isLoopControlVariable(D: FD).first &&
4201 !Stack->checkMappableExprComponentListsForDecl(
4202 VD: FD, /*CurrentRegionOnly=*/true,
4203 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef
4204 StackComponents,
4205 OpenMPClauseKind) {
4206 return isa<CXXThisExpr>(
4207 Val: cast<MemberExpr>(
4208 Val: StackComponents.back().getAssociatedExpression())
4209 ->getBase()
4210 ->IgnoreParens());
4211 })) {
4212 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
4213 // A bit-field cannot appear in a map clause.
4214 //
4215 if (FD->isBitField())
4216 return;
4217
4218 // Check to see if the member expression is referencing a class that
4219 // has already been explicitly mapped
4220 if (Stack->isClassPreviouslyMapped(QT: TE->getType()))
4221 return;
4222
4223 OpenMPDefaultmapClauseModifier Modifier =
4224 Stack->getDefaultmapModifier(Kind: OMPC_DEFAULTMAP_aggregate);
4225 OpenMPDefaultmapClauseKind ClauseKind =
4226 getVariableCategoryFromDecl(LO: SemaRef.getLangOpts(), VD: FD);
4227 OpenMPMapClauseKind Kind = getMapClauseKindFromModifier(
4228 M: Modifier, /*IsAggregateOrDeclareTarget=*/true);
4229 ImpInfo.Mappings[ClauseKind][Kind].insert(X: E);
4230 return;
4231 }
4232
4233 SourceLocation ELoc = E->getExprLoc();
4234 // OpenMP [2.9.3.6, Restrictions, p.2]
4235 // A list item that appears in a reduction clause of the innermost
4236 // enclosing worksharing or parallel construct may not be accessed in
4237 // an explicit task.
4238 DVar = Stack->hasInnermostDSA(
4239 D: FD,
4240 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
4241 return C == OMPC_reduction && !AppliedToPointee;
4242 },
4243 DPred: [](OpenMPDirectiveKind K) {
4244 return isOpenMPParallelDirective(DKind: K) ||
4245 isOpenMPWorksharingDirective(DKind: K) || isOpenMPTeamsDirective(DKind: K);
4246 },
4247 /*FromParent=*/true);
4248 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind == OMPC_reduction) {
4249 ErrorFound = true;
4250 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_in_task);
4251 reportOriginalDsa(SemaRef, Stack, D: FD, DVar);
4252 return;
4253 }
4254
4255 // Define implicit data-sharing attributes for task.
4256 DVar = Stack->getImplicitDSA(D: FD, /*FromParent=*/false);
4257 if (isOpenMPTaskingDirective(Kind: DKind) && DVar.CKind != OMPC_shared &&
4258 !Stack->isLoopControlVariable(D: FD).first) {
4259 // Check if there is a captured expression for the current field in the
4260 // region. Do not mark it as firstprivate unless there is no captured
4261 // expression.
4262 // TODO: try to make it firstprivate.
4263 if (DVar.CKind != OMPC_unknown)
4264 ImpInfo.Firstprivates.insert(X: E);
4265 }
4266 return;
4267 }
4268 if (isOpenMPTargetExecutionDirective(DKind)) {
4269 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
4270 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, CKind: OMPC_map,
4271 DKind, /*NoDiagnose=*/true))
4272 return;
4273 const auto *VD = cast<ValueDecl>(
4274 Val: CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
4275 if (!Stack->checkMappableExprComponentListsForDecl(
4276 VD, /*CurrentRegionOnly=*/true,
4277 Check: [&CurComponents](
4278 OMPClauseMappableExprCommon::MappableExprComponentListRef
4279 StackComponents,
4280 OpenMPClauseKind) {
4281 auto CCI = CurComponents.rbegin();
4282 auto CCE = CurComponents.rend();
4283 for (const auto &SC : llvm::reverse(C&: StackComponents)) {
4284 // Do both expressions have the same kind?
4285 if (CCI->getAssociatedExpression()->getStmtClass() !=
4286 SC.getAssociatedExpression()->getStmtClass())
4287 if (!((isa<ArraySectionExpr>(
4288 Val: SC.getAssociatedExpression()) ||
4289 isa<OMPArrayShapingExpr>(
4290 Val: SC.getAssociatedExpression())) &&
4291 isa<ArraySubscriptExpr>(
4292 Val: CCI->getAssociatedExpression())))
4293 return false;
4294
4295 const Decl *CCD = CCI->getAssociatedDeclaration();
4296 const Decl *SCD = SC.getAssociatedDeclaration();
4297 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
4298 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
4299 if (SCD != CCD)
4300 return false;
4301 std::advance(i&: CCI, n: 1);
4302 if (CCI == CCE)
4303 break;
4304 }
4305 return true;
4306 })) {
4307 Visit(S: E->getBase());
4308 }
4309 } else if (!TryCaptureCXXThisMembers) {
4310 Visit(S: E->getBase());
4311 }
4312 }
4313 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
4314 for (OMPClause *C : S->clauses()) {
4315 // Skip analysis of arguments of private clauses for task|target
4316 // directives.
4317 if (isa_and_nonnull<OMPPrivateClause>(Val: C))
4318 continue;
4319 // Skip analysis of arguments of implicitly defined firstprivate clause
4320 // for task|target directives.
4321 // Skip analysis of arguments of implicitly defined map clause for target
4322 // directives.
4323 if (C && !((isa<OMPFirstprivateClause>(Val: C) || isa<OMPMapClause>(Val: C)) &&
4324 C->isImplicit() && !isOpenMPTaskingDirective(Kind: DKind))) {
4325 for (Stmt *CC : C->children()) {
4326 if (CC)
4327 Visit(S: CC);
4328 }
4329 }
4330 }
4331 // Check implicitly captured variables.
4332 VisitSubCaptures(S);
4333 }
4334
4335 void VisitOMPCanonicalLoopNestTransformationDirective(
4336 OMPCanonicalLoopNestTransformationDirective *S) {
4337 // Loop transformation directives do not introduce data sharing
4338 VisitStmt(S);
4339 }
4340
4341 void VisitCallExpr(CallExpr *S) {
4342 for (Stmt *C : S->arguments()) {
4343 if (C) {
4344 // Check implicitly captured variables in the task-based directives to
4345 // check if they must be firstprivatized.
4346 Visit(S: C);
4347 }
4348 }
4349 if (Expr *Callee = S->getCallee()) {
4350 auto *CI = Callee->IgnoreParenImpCasts();
4351 if (auto *CE = dyn_cast<MemberExpr>(Val: CI))
4352 Visit(S: CE->getBase());
4353 else if (auto *CE = dyn_cast<DeclRefExpr>(Val: CI))
4354 Visit(S: CE);
4355 }
4356 }
4357 void VisitStmt(Stmt *S) {
4358 for (Stmt *C : S->children()) {
4359 if (C) {
4360 // Check implicitly captured variables in the task-based directives to
4361 // check if they must be firstprivatized.
4362 Visit(S: C);
4363 }
4364 }
4365 }
4366
4367 void visitSubCaptures(CapturedStmt *S) {
4368 for (const CapturedStmt::Capture &Cap : S->captures()) {
4369 if (!Cap.capturesVariable() && !Cap.capturesVariableByCopy())
4370 continue;
4371 VarDecl *VD = Cap.getCapturedVar();
4372 // Do not try to map the variable if it or its sub-component was mapped
4373 // already.
4374 if (isOpenMPTargetExecutionDirective(DKind) &&
4375 Stack->checkMappableExprComponentListsForDecl(
4376 VD, /*CurrentRegionOnly=*/true,
4377 Check: [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
4378 OpenMPClauseKind) { return true; }))
4379 continue;
4380 DeclRefExpr *DRE = buildDeclRefExpr(
4381 S&: SemaRef, D: VD, Ty: VD->getType().getNonLValueExprType(Context: SemaRef.Context),
4382 Loc: Cap.getLocation(), /*RefersToCapture=*/true);
4383 Visit(S: DRE);
4384 }
4385 }
4386 bool isErrorFound() const { return ErrorFound; }
4387 const VariableImplicitInfo &getImplicitInfo() const { return ImpInfo; }
4388 const SemaOpenMP::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
4389 return VarsWithInheritedDSA;
4390 }
4391
4392 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
4393 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
4394 DKind = S->getCurrentDirective();
4395 // Process declare target link variables for the target directives.
4396 if (isOpenMPTargetExecutionDirective(DKind)) {
4397 for (DeclRefExpr *E : Stack->getLinkGlobals())
4398 Visit(S: E);
4399 }
4400 }
4401};
4402} // namespace
4403
4404static void handleDeclareVariantConstructTrait(DSAStackTy *Stack,
4405 OpenMPDirectiveKind DKind,
4406 bool ScopeEntry) {
4407 SmallVector<llvm::omp::TraitProperty, 8> Traits;
4408 if (isOpenMPTargetExecutionDirective(DKind))
4409 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_target_target);
4410 if (isOpenMPTeamsDirective(DKind))
4411 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_teams_teams);
4412 if (isOpenMPParallelDirective(DKind))
4413 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_parallel_parallel);
4414 if (isOpenMPWorksharingDirective(DKind))
4415 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_for_for);
4416 if (isOpenMPSimdDirective(DKind))
4417 Traits.emplace_back(Args: llvm::omp::TraitProperty::construct_simd_simd);
4418 Stack->handleConstructTrait(Traits, ScopeEntry);
4419}
4420
4421static SmallVector<SemaOpenMP::CapturedParamNameType>
4422getParallelRegionParams(Sema &SemaRef, bool LoopBoundSharing) {
4423 ASTContext &Context = SemaRef.getASTContext();
4424 QualType KmpInt32Ty =
4425 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4426 QualType KmpInt32PtrTy =
4427 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4428 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4429 std::make_pair(x: ".global_tid.", y&: KmpInt32PtrTy),
4430 std::make_pair(x: ".bound_tid.", y&: KmpInt32PtrTy),
4431 };
4432 if (LoopBoundSharing) {
4433 QualType KmpSizeTy = Context.getSizeType().withConst();
4434 Params.push_back(Elt: std::make_pair(x: ".previous.lb.", y&: KmpSizeTy));
4435 Params.push_back(Elt: std::make_pair(x: ".previous.ub.", y&: KmpSizeTy));
4436 }
4437
4438 // __context with shared vars
4439 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4440 return Params;
4441}
4442
4443static SmallVector<SemaOpenMP::CapturedParamNameType>
4444getTeamsRegionParams(Sema &SemaRef) {
4445 return getParallelRegionParams(SemaRef, /*LoopBoundSharing=*/false);
4446}
4447
4448static SmallVector<SemaOpenMP::CapturedParamNameType>
4449getTaskRegionParams(Sema &SemaRef) {
4450 ASTContext &Context = SemaRef.getASTContext();
4451 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(DestWidth: 32, Signed: 1).withConst();
4452 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4453 QualType KmpInt32PtrTy =
4454 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4455 QualType Args[] = {VoidPtrTy};
4456 FunctionProtoType::ExtProtoInfo EPI;
4457 EPI.Variadic = true;
4458 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4459 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4460 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4461 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4462 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4463 std::make_pair(
4464 x: ".copy_fn.",
4465 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4466 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4467 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4468 };
4469 return Params;
4470}
4471
4472static SmallVector<SemaOpenMP::CapturedParamNameType>
4473getTargetRegionParams(Sema &SemaRef) {
4474 ASTContext &Context = SemaRef.getASTContext();
4475 SmallVector<SemaOpenMP::CapturedParamNameType> Params;
4476 if (SemaRef.getLangOpts().OpenMPIsTargetDevice) {
4477 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4478 Params.push_back(Elt: std::make_pair(x: StringRef("dyn_ptr"), y&: VoidPtrTy));
4479 }
4480 // __context with shared vars
4481 Params.push_back(Elt: std::make_pair(x: StringRef(), y: QualType()));
4482 return Params;
4483}
4484
4485static SmallVector<SemaOpenMP::CapturedParamNameType>
4486getUnknownRegionParams(Sema &SemaRef) {
4487 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4488 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4489 };
4490 return Params;
4491}
4492
4493static SmallVector<SemaOpenMP::CapturedParamNameType>
4494getTaskloopRegionParams(Sema &SemaRef) {
4495 ASTContext &Context = SemaRef.getASTContext();
4496 QualType KmpInt32Ty =
4497 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1).withConst();
4498 QualType KmpUInt64Ty =
4499 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0).withConst();
4500 QualType KmpInt64Ty =
4501 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1).withConst();
4502 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
4503 QualType KmpInt32PtrTy =
4504 Context.getPointerType(T: KmpInt32Ty).withConst().withRestrict();
4505 QualType Args[] = {VoidPtrTy};
4506 FunctionProtoType::ExtProtoInfo EPI;
4507 EPI.Variadic = true;
4508 QualType CopyFnType = Context.getFunctionType(ResultTy: Context.VoidTy, Args, EPI);
4509 SmallVector<SemaOpenMP::CapturedParamNameType> Params{
4510 std::make_pair(x: ".global_tid.", y&: KmpInt32Ty),
4511 std::make_pair(x: ".part_id.", y&: KmpInt32PtrTy),
4512 std::make_pair(x: ".privates.", y&: VoidPtrTy),
4513 std::make_pair(
4514 x: ".copy_fn.",
4515 y: Context.getPointerType(T: CopyFnType).withConst().withRestrict()),
4516 std::make_pair(x: ".task_t.", y: Context.VoidPtrTy.withConst()),
4517 std::make_pair(x: ".lb.", y&: KmpUInt64Ty),
4518 std::make_pair(x: ".ub.", y&: KmpUInt64Ty),
4519 std::make_pair(x: ".st.", y&: KmpInt64Ty),
4520 std::make_pair(x: ".liter.", y&: KmpInt32Ty),
4521 std::make_pair(x: ".reductions.", y&: VoidPtrTy),
4522 std::make_pair(x: StringRef(), y: QualType()) // __context with shared vars
4523 };
4524 return Params;
4525}
4526
4527static void processCapturedRegions(Sema &SemaRef, OpenMPDirectiveKind DKind,
4528 Scope *CurScope, SourceLocation Loc) {
4529 SmallVector<OpenMPDirectiveKind> Regions;
4530 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind);
4531
4532 bool LoopBoundSharing = isOpenMPLoopBoundSharingDirective(Kind: DKind);
4533
4534 auto MarkAsInlined = [&](CapturedRegionScopeInfo *CSI) {
4535 CSI->TheCapturedDecl->addAttr(A: AlwaysInlineAttr::CreateImplicit(
4536 Ctx&: SemaRef.getASTContext(), Range: {}, S: AlwaysInlineAttr::Keyword_forceinline));
4537 };
4538
4539 for (auto [Level, RKind] : llvm::enumerate(First&: Regions)) {
4540 switch (RKind) {
4541 // All region kinds that can be returned from `getOpenMPCaptureRegions`
4542 // are listed here.
4543 case OMPD_parallel:
4544 SemaRef.ActOnCapturedRegionStart(
4545 Loc, CurScope, Kind: CR_OpenMP,
4546 Params: getParallelRegionParams(SemaRef, LoopBoundSharing), OpenMPCaptureLevel: Level);
4547 break;
4548 case OMPD_teams:
4549 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4550 Params: getTeamsRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4551 break;
4552 case OMPD_task:
4553 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4554 Params: getTaskRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4555 // Mark this captured region as inlined, because we don't use outlined
4556 // function directly.
4557 MarkAsInlined(SemaRef.getCurCapturedRegion());
4558 break;
4559 case OMPD_taskloop:
4560 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4561 Params: getTaskloopRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4562 // Mark this captured region as inlined, because we don't use outlined
4563 // function directly.
4564 MarkAsInlined(SemaRef.getCurCapturedRegion());
4565 break;
4566 case OMPD_target:
4567 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4568 Params: getTargetRegionParams(SemaRef), OpenMPCaptureLevel: Level);
4569 break;
4570 case OMPD_unknown:
4571 SemaRef.ActOnCapturedRegionStart(Loc, CurScope, Kind: CR_OpenMP,
4572 Params: getUnknownRegionParams(SemaRef));
4573 break;
4574 case OMPD_metadirective:
4575 case OMPD_nothing:
4576 default:
4577 llvm_unreachable("Unexpected capture region");
4578 }
4579 }
4580}
4581
4582void SemaOpenMP::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind,
4583 Scope *CurScope) {
4584 switch (DKind) {
4585 case OMPD_atomic:
4586 case OMPD_critical:
4587 case OMPD_masked:
4588 case OMPD_master:
4589 case OMPD_section:
4590 case OMPD_tile:
4591 case OMPD_stripe:
4592 case OMPD_unroll:
4593 case OMPD_reverse:
4594 case OMPD_interchange:
4595 case OMPD_fuse:
4596 case OMPD_assume:
4597 break;
4598 default:
4599 processCapturedRegions(SemaRef, DKind, CurScope,
4600 DSAStack->getConstructLoc());
4601 break;
4602 }
4603
4604 DSAStack->setContext(SemaRef.CurContext);
4605 handleDeclareVariantConstructTrait(DSAStack, DKind, /*ScopeEntry=*/true);
4606}
4607
4608int SemaOpenMP::getNumberOfConstructScopes(unsigned Level) const {
4609 return getOpenMPCaptureLevels(DSAStack->getDirective(Level));
4610}
4611
4612int SemaOpenMP::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
4613 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4614 getOpenMPCaptureRegions(CaptureRegions, DKind);
4615 return CaptureRegions.size();
4616}
4617
4618static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
4619 Expr *CaptureExpr, bool WithInit,
4620 DeclContext *CurContext,
4621 bool AsExpression) {
4622 assert(CaptureExpr);
4623 ASTContext &C = S.getASTContext();
4624 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
4625 QualType Ty = Init->getType();
4626 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
4627 if (S.getLangOpts().CPlusPlus) {
4628 Ty = C.getLValueReferenceType(T: Ty);
4629 } else {
4630 Ty = C.getPointerType(T: Ty);
4631 ExprResult Res =
4632 S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_AddrOf, InputExpr: Init);
4633 if (!Res.isUsable())
4634 return nullptr;
4635 Init = Res.get();
4636 }
4637 WithInit = true;
4638 }
4639 auto *CED = OMPCapturedExprDecl::Create(C, DC: CurContext, Id, T: Ty,
4640 StartLoc: CaptureExpr->getBeginLoc());
4641 if (!WithInit)
4642 CED->addAttr(A: OMPCaptureNoInitAttr::CreateImplicit(Ctx&: C));
4643 CurContext->addHiddenDecl(D: CED);
4644 Sema::TentativeAnalysisScope Trap(S);
4645 S.AddInitializerToDecl(dcl: CED, init: Init, /*DirectInit=*/false);
4646 return CED;
4647}
4648
4649static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
4650 bool WithInit) {
4651 OMPCapturedExprDecl *CD;
4652 if (VarDecl *VD = S.OpenMP().isOpenMPCapturedDecl(D))
4653 CD = cast<OMPCapturedExprDecl>(Val: VD);
4654 else
4655 CD = buildCaptureDecl(S, Id: D->getIdentifier(), CaptureExpr, WithInit,
4656 CurContext: S.CurContext,
4657 /*AsExpression=*/false);
4658 return buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
4659 Loc: CaptureExpr->getExprLoc());
4660}
4661
4662static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref,
4663 StringRef Name) {
4664 CaptureExpr = S.DefaultLvalueConversion(E: CaptureExpr).get();
4665 if (!Ref) {
4666 OMPCapturedExprDecl *CD = buildCaptureDecl(
4667 S, Id: &S.getASTContext().Idents.get(Name), CaptureExpr,
4668 /*WithInit=*/true, CurContext: S.CurContext, /*AsExpression=*/true);
4669 Ref = buildDeclRefExpr(S, D: CD, Ty: CD->getType().getNonReferenceType(),
4670 Loc: CaptureExpr->getExprLoc());
4671 }
4672 ExprResult Res = Ref;
4673 if (!S.getLangOpts().CPlusPlus &&
4674 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
4675 Ref->getType()->isPointerType()) {
4676 Res = S.CreateBuiltinUnaryOp(OpLoc: CaptureExpr->getExprLoc(), Opc: UO_Deref, InputExpr: Ref);
4677 if (!Res.isUsable())
4678 return ExprError();
4679 }
4680 return S.DefaultLvalueConversion(E: Res.get());
4681}
4682
4683namespace {
4684// OpenMP directives parsed in this section are represented as a
4685// CapturedStatement with an associated statement. If a syntax error
4686// is detected during the parsing of the associated statement, the
4687// compiler must abort processing and close the CapturedStatement.
4688//
4689// Combined directives such as 'target parallel' have more than one
4690// nested CapturedStatements. This RAII ensures that we unwind out
4691// of all the nested CapturedStatements when an error is found.
4692class CaptureRegionUnwinderRAII {
4693private:
4694 Sema &S;
4695 bool &ErrorFound;
4696 OpenMPDirectiveKind DKind = OMPD_unknown;
4697
4698public:
4699 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
4700 OpenMPDirectiveKind DKind)
4701 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
4702 ~CaptureRegionUnwinderRAII() {
4703 if (ErrorFound) {
4704 int ThisCaptureLevel = S.OpenMP().getOpenMPCaptureLevels(DKind);
4705 while (--ThisCaptureLevel >= 0)
4706 S.ActOnCapturedRegionError();
4707 }
4708 }
4709};
4710} // namespace
4711
4712void SemaOpenMP::tryCaptureOpenMPLambdas(ValueDecl *V) {
4713 // Capture variables captured by reference in lambdas for target-based
4714 // directives.
4715 if (!SemaRef.CurContext->isDependentContext() &&
4716 (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) ||
4717 isOpenMPTargetDataManagementDirective(
4718 DSAStack->getCurrentDirective()))) {
4719 QualType Type = V->getType();
4720 if (const auto *RD = Type.getCanonicalType()
4721 .getNonReferenceType()
4722 ->getAsCXXRecordDecl()) {
4723 bool SavedForceCaptureByReferenceInTargetExecutable =
4724 DSAStack->isForceCaptureByReferenceInTargetExecutable();
4725 DSAStack->setForceCaptureByReferenceInTargetExecutable(
4726 /*V=*/true);
4727 if (RD->isLambda()) {
4728 llvm::DenseMap<const ValueDecl *, FieldDecl *> Captures;
4729 FieldDecl *ThisCapture;
4730 RD->getCaptureFields(Captures, ThisCapture);
4731 for (const LambdaCapture &LC : RD->captures()) {
4732 if (LC.getCaptureKind() == LCK_ByRef) {
4733 VarDecl *VD = cast<VarDecl>(Val: LC.getCapturedVar());
4734 DeclContext *VDC = VD->getDeclContext();
4735 if (!VDC->Encloses(DC: SemaRef.CurContext))
4736 continue;
4737 SemaRef.MarkVariableReferenced(Loc: LC.getLocation(), Var: VD);
4738 } else if (LC.getCaptureKind() == LCK_This) {
4739 QualType ThisTy = SemaRef.getCurrentThisType();
4740 if (!ThisTy.isNull() && getASTContext().typesAreCompatible(
4741 T1: ThisTy, T2: ThisCapture->getType()))
4742 SemaRef.CheckCXXThisCapture(Loc: LC.getLocation());
4743 }
4744 }
4745 }
4746 DSAStack->setForceCaptureByReferenceInTargetExecutable(
4747 SavedForceCaptureByReferenceInTargetExecutable);
4748 }
4749 }
4750}
4751
4752static bool checkOrderedOrderSpecified(Sema &S,
4753 const ArrayRef<OMPClause *> Clauses) {
4754 const OMPOrderedClause *Ordered = nullptr;
4755 const OMPOrderClause *Order = nullptr;
4756
4757 for (const OMPClause *Clause : Clauses) {
4758 if (Clause->getClauseKind() == OMPC_ordered)
4759 Ordered = cast<OMPOrderedClause>(Val: Clause);
4760 else if (Clause->getClauseKind() == OMPC_order) {
4761 Order = cast<OMPOrderClause>(Val: Clause);
4762 if (Order->getKind() != OMPC_ORDER_concurrent)
4763 Order = nullptr;
4764 }
4765 if (Ordered && Order)
4766 break;
4767 }
4768
4769 if (Ordered && Order) {
4770 S.Diag(Loc: Order->getKindKwLoc(),
4771 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
4772 << getOpenMPClauseNameForDiag(C: OMPC_order)
4773 << getOpenMPSimpleClauseTypeName(Kind: OMPC_order, Type: OMPC_ORDER_concurrent)
4774 << SourceRange(Order->getBeginLoc(), Order->getEndLoc());
4775 S.Diag(Loc: Ordered->getBeginLoc(), DiagID: diag::note_omp_ordered_param)
4776 << 0 << SourceRange(Ordered->getBeginLoc(), Ordered->getEndLoc());
4777 return true;
4778 }
4779 return false;
4780}
4781
4782StmtResult SemaOpenMP::ActOnOpenMPRegionEnd(StmtResult S,
4783 ArrayRef<OMPClause *> Clauses) {
4784 handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
4785 /*ScopeEntry=*/false);
4786 if (!isOpenMPCapturingDirective(DSAStack->getCurrentDirective()))
4787 return S;
4788
4789 bool ErrorFound = false;
4790 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
4791 SemaRef, ErrorFound, DSAStack->getCurrentDirective());
4792 if (!S.isUsable()) {
4793 ErrorFound = true;
4794 return StmtError();
4795 }
4796
4797 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
4798 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
4799 OMPOrderedClause *OC = nullptr;
4800 OMPScheduleClause *SC = nullptr;
4801 SmallVector<const OMPLinearClause *, 4> LCs;
4802 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
4803 // This is required for proper codegen.
4804 for (OMPClause *Clause : Clauses) {
4805 if (!getLangOpts().OpenMPSimd &&
4806 (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) ||
4807 DSAStack->getCurrentDirective() == OMPD_target) &&
4808 Clause->getClauseKind() == OMPC_in_reduction) {
4809 // Capture taskgroup task_reduction descriptors inside the tasking regions
4810 // with the corresponding in_reduction items.
4811 auto *IRC = cast<OMPInReductionClause>(Val: Clause);
4812 for (Expr *E : IRC->taskgroup_descriptors())
4813 if (E)
4814 SemaRef.MarkDeclarationsReferencedInExpr(E);
4815 }
4816 if (isOpenMPPrivate(Kind: Clause->getClauseKind()) ||
4817 Clause->getClauseKind() == OMPC_copyprivate ||
4818 (getLangOpts().OpenMPUseTLS &&
4819 getASTContext().getTargetInfo().isTLSSupported() &&
4820 Clause->getClauseKind() == OMPC_copyin)) {
4821 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
4822 // Mark all variables in private list clauses as used in inner region.
4823 for (Stmt *VarRef : Clause->children()) {
4824 if (auto *E = cast_or_null<Expr>(Val: VarRef)) {
4825 SemaRef.MarkDeclarationsReferencedInExpr(E);
4826 }
4827 }
4828 DSAStack->setForceVarCapturing(/*V=*/false);
4829 } else if (CaptureRegions.size() > 1 ||
4830 CaptureRegions.back() != OMPD_unknown) {
4831 if (auto *C = OMPClauseWithPreInit::get(C: Clause))
4832 PICs.push_back(Elt: C);
4833 if (auto *C = OMPClauseWithPostUpdate::get(C: Clause)) {
4834 if (Expr *E = C->getPostUpdateExpr())
4835 SemaRef.MarkDeclarationsReferencedInExpr(E);
4836 }
4837 }
4838 if (Clause->getClauseKind() == OMPC_schedule)
4839 SC = cast<OMPScheduleClause>(Val: Clause);
4840 else if (Clause->getClauseKind() == OMPC_ordered)
4841 OC = cast<OMPOrderedClause>(Val: Clause);
4842 else if (Clause->getClauseKind() == OMPC_linear)
4843 LCs.push_back(Elt: cast<OMPLinearClause>(Val: Clause));
4844 }
4845 // Capture allocator expressions if used.
4846 for (Expr *E : DSAStack->getInnerAllocators())
4847 SemaRef.MarkDeclarationsReferencedInExpr(E);
4848 // OpenMP, 2.7.1 Loop Construct, Restrictions
4849 // The nonmonotonic modifier cannot be specified if an ordered clause is
4850 // specified.
4851 if (SC &&
4852 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
4853 SC->getSecondScheduleModifier() ==
4854 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
4855 OC) {
4856 Diag(Loc: SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
4857 ? SC->getFirstScheduleModifierLoc()
4858 : SC->getSecondScheduleModifierLoc(),
4859 DiagID: diag::err_omp_simple_clause_incompatible_with_ordered)
4860 << getOpenMPClauseNameForDiag(C: OMPC_schedule)
4861 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule,
4862 Type: OMPC_SCHEDULE_MODIFIER_nonmonotonic)
4863 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4864 ErrorFound = true;
4865 }
4866 // OpenMP 5.0, 2.9.2 Worksharing-Loop Construct, Restrictions.
4867 // If an order(concurrent) clause is present, an ordered clause may not appear
4868 // on the same directive.
4869 if (checkOrderedOrderSpecified(S&: SemaRef, Clauses))
4870 ErrorFound = true;
4871 if (!LCs.empty() && OC && OC->getNumForLoops()) {
4872 for (const OMPLinearClause *C : LCs) {
4873 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_linear_ordered)
4874 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
4875 }
4876 ErrorFound = true;
4877 }
4878 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
4879 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
4880 OC->getNumForLoops()) {
4881 unsigned OMPVersion = getLangOpts().OpenMP;
4882 Diag(Loc: OC->getBeginLoc(), DiagID: diag::err_omp_ordered_simd)
4883 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(), Ver: OMPVersion);
4884 ErrorFound = true;
4885 }
4886 if (ErrorFound) {
4887 return StmtError();
4888 }
4889 StmtResult SR = S;
4890 unsigned CompletedRegions = 0;
4891 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(C&: CaptureRegions)) {
4892 // Mark all variables in private list clauses as used in inner region.
4893 // Required for proper codegen of combined directives.
4894 // TODO: add processing for other clauses.
4895 if (ThisCaptureRegion != OMPD_unknown) {
4896 for (const clang::OMPClauseWithPreInit *C : PICs) {
4897 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
4898 // Find the particular capture region for the clause if the
4899 // directive is a combined one with multiple capture regions.
4900 // If the directive is not a combined one, the capture region
4901 // associated with the clause is OMPD_unknown and is generated
4902 // only once.
4903 if (CaptureRegion == ThisCaptureRegion ||
4904 CaptureRegion == OMPD_unknown) {
4905 if (auto *DS = cast_or_null<DeclStmt>(Val: C->getPreInitStmt())) {
4906 for (Decl *D : DS->decls())
4907 SemaRef.MarkVariableReferenced(Loc: D->getLocation(),
4908 Var: cast<VarDecl>(Val: D));
4909 }
4910 }
4911 }
4912 }
4913 if (ThisCaptureRegion == OMPD_target) {
4914 // Capture allocator traits in the target region. They are used implicitly
4915 // and, thus, are not captured by default.
4916 for (OMPClause *C : Clauses) {
4917 if (const auto *UAC = dyn_cast<OMPUsesAllocatorsClause>(Val: C)) {
4918 for (unsigned I = 0, End = UAC->getNumberOfAllocators(); I < End;
4919 ++I) {
4920 OMPUsesAllocatorsClause::Data D = UAC->getAllocatorData(I);
4921 if (Expr *E = D.AllocatorTraits)
4922 SemaRef.MarkDeclarationsReferencedInExpr(E);
4923 }
4924 continue;
4925 }
4926 }
4927 }
4928 if (ThisCaptureRegion == OMPD_parallel) {
4929 // Capture temp arrays for inscan reductions and locals in aligned
4930 // clauses.
4931 for (OMPClause *C : Clauses) {
4932 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C)) {
4933 if (RC->getModifier() != OMPC_REDUCTION_inscan)
4934 continue;
4935 for (Expr *E : RC->copy_array_temps())
4936 if (E)
4937 SemaRef.MarkDeclarationsReferencedInExpr(E);
4938 }
4939 if (auto *AC = dyn_cast<OMPAlignedClause>(Val: C)) {
4940 for (Expr *E : AC->varlist())
4941 SemaRef.MarkDeclarationsReferencedInExpr(E);
4942 }
4943 }
4944 }
4945 if (++CompletedRegions == CaptureRegions.size())
4946 DSAStack->setBodyComplete();
4947 SR = SemaRef.ActOnCapturedRegionEnd(S: SR.get());
4948 }
4949 return SR;
4950}
4951
4952static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
4953 OpenMPDirectiveKind CancelRegion,
4954 SourceLocation StartLoc) {
4955 // CancelRegion is only needed for cancel and cancellation_point.
4956 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
4957 return false;
4958
4959 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
4960 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
4961 return false;
4962
4963 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
4964 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_wrong_cancel_region)
4965 << getOpenMPDirectiveName(D: CancelRegion, Ver: OMPVersion);
4966 return true;
4967}
4968
4969static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
4970 OpenMPDirectiveKind CurrentRegion,
4971 const DeclarationNameInfo &CurrentName,
4972 OpenMPDirectiveKind CancelRegion,
4973 OpenMPBindClauseKind BindKind,
4974 SourceLocation StartLoc) {
4975 if (!Stack->getCurScope())
4976 return false;
4977
4978 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
4979 OpenMPDirectiveKind OffendingRegion = ParentRegion;
4980 bool NestingProhibited = false;
4981 bool CloseNesting = true;
4982 bool OrphanSeen = false;
4983 enum {
4984 NoRecommend,
4985 ShouldBeInParallelRegion,
4986 ShouldBeInOrderedRegion,
4987 ShouldBeInTargetRegion,
4988 ShouldBeInTeamsRegion,
4989 ShouldBeInLoopSimdRegion,
4990 } Recommend = NoRecommend;
4991
4992 SmallVector<OpenMPDirectiveKind, 4> LeafOrComposite;
4993 ArrayRef<OpenMPDirectiveKind> ParentLOC =
4994 getLeafOrCompositeConstructs(D: ParentRegion, Output&: LeafOrComposite);
4995 OpenMPDirectiveKind EnclosingConstruct = ParentLOC.back();
4996 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
4997
4998 if (OMPVersion >= 50 && Stack->isParentOrderConcurrent() &&
4999 !isOpenMPOrderConcurrentNestableDirective(DKind: CurrentRegion,
5000 LangOpts: SemaRef.LangOpts)) {
5001 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_order)
5002 << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5003 return true;
5004 }
5005 if (isOpenMPSimdDirective(DKind: ParentRegion) &&
5006 ((OMPVersion <= 45 && CurrentRegion != OMPD_ordered) ||
5007 (OMPVersion >= 50 && CurrentRegion != OMPD_ordered &&
5008 CurrentRegion != OMPD_simd && CurrentRegion != OMPD_atomic &&
5009 CurrentRegion != OMPD_scan))) {
5010 // OpenMP [2.16, Nesting of Regions]
5011 // OpenMP constructs may not be nested inside a simd region.
5012 // OpenMP [2.8.1,simd Construct, Restrictions]
5013 // An ordered construct with the simd clause is the only OpenMP
5014 // construct that can appear in the simd region.
5015 // Allowing a SIMD construct nested in another SIMD construct is an
5016 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
5017 // message.
5018 // OpenMP 5.0 [2.9.3.1, simd Construct, Restrictions]
5019 // The only OpenMP constructs that can be encountered during execution of
5020 // a simd region are the atomic construct, the loop construct, the simd
5021 // construct and the ordered construct with the simd clause.
5022 SemaRef.Diag(Loc: StartLoc, DiagID: (CurrentRegion != OMPD_simd)
5023 ? diag::err_omp_prohibited_region_simd
5024 : diag::warn_omp_nesting_simd)
5025 << (OMPVersion >= 50 ? 1 : 0);
5026 return CurrentRegion != OMPD_simd;
5027 }
5028 if (EnclosingConstruct == OMPD_atomic) {
5029 // OpenMP [2.16, Nesting of Regions]
5030 // OpenMP constructs may not be nested inside an atomic region.
5031 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_atomic);
5032 return true;
5033 }
5034 if (CurrentRegion == OMPD_section) {
5035 // OpenMP [2.7.2, sections Construct, Restrictions]
5036 // Orphaned section directives are prohibited. That is, the section
5037 // directives must appear within the sections construct and must not be
5038 // encountered elsewhere in the sections region.
5039 if (EnclosingConstruct != OMPD_sections) {
5040 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_section_directive)
5041 << (ParentRegion != OMPD_unknown)
5042 << getOpenMPDirectiveName(D: ParentRegion, Ver: OMPVersion);
5043 return true;
5044 }
5045 return false;
5046 }
5047 // Allow some constructs (except teams and cancellation constructs) to be
5048 // orphaned (they could be used in functions, called from OpenMP regions
5049 // with the required preconditions).
5050 if (ParentRegion == OMPD_unknown &&
5051 !isOpenMPNestingTeamsDirective(DKind: CurrentRegion) &&
5052 CurrentRegion != OMPD_cancellation_point &&
5053 CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
5054 return false;
5055 // Checks needed for mapping "loop" construct. Please check mapLoopConstruct
5056 // for a detailed explanation
5057 if (OMPVersion >= 50 && CurrentRegion == OMPD_loop &&
5058 (BindKind == OMPC_BIND_parallel || BindKind == OMPC_BIND_teams) &&
5059 (isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5060 EnclosingConstruct == OMPD_loop)) {
5061 int ErrorMsgNumber = (BindKind == OMPC_BIND_parallel) ? 1 : 4;
5062 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5063 << true << getOpenMPDirectiveName(D: ParentRegion, Ver: OMPVersion)
5064 << ErrorMsgNumber << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5065 return true;
5066 }
5067 if (CurrentRegion == OMPD_cancellation_point ||
5068 CurrentRegion == OMPD_cancel) {
5069 // OpenMP [2.16, Nesting of Regions]
5070 // A cancellation point construct for which construct-type-clause is
5071 // taskgroup must be nested inside a task construct. A cancellation
5072 // point construct for which construct-type-clause is not taskgroup must
5073 // be closely nested inside an OpenMP construct that matches the type
5074 // specified in construct-type-clause.
5075 // A cancel construct for which construct-type-clause is taskgroup must be
5076 // nested inside a task construct. A cancel construct for which
5077 // construct-type-clause is not taskgroup must be closely nested inside an
5078 // OpenMP construct that matches the type specified in
5079 // construct-type-clause.
5080 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: ParentRegion);
5081 if (CancelRegion == OMPD_taskgroup) {
5082 NestingProhibited =
5083 EnclosingConstruct != OMPD_task &&
5084 (OMPVersion < 50 || EnclosingConstruct != OMPD_taskloop);
5085 } else if (CancelRegion == OMPD_sections) {
5086 NestingProhibited = EnclosingConstruct != OMPD_section &&
5087 EnclosingConstruct != OMPD_sections;
5088 } else {
5089 NestingProhibited = CancelRegion != Leafs.back();
5090 }
5091 OrphanSeen = ParentRegion == OMPD_unknown;
5092 } else if (CurrentRegion == OMPD_master || CurrentRegion == OMPD_masked) {
5093 // OpenMP 5.1 [2.22, Nesting of Regions]
5094 // A masked region may not be closely nested inside a worksharing, loop,
5095 // atomic, task, or taskloop region.
5096 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5097 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5098 isOpenMPTaskingDirective(Kind: ParentRegion);
5099 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
5100 // OpenMP [2.16, Nesting of Regions]
5101 // A critical region may not be nested (closely or otherwise) inside a
5102 // critical region with the same name. Note that this restriction is not
5103 // sufficient to prevent deadlock.
5104 SourceLocation PreviousCriticalLoc;
5105 bool DeadLock = Stack->hasDirective(
5106 DPred: [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
5107 const DeclarationNameInfo &DNI,
5108 SourceLocation Loc) {
5109 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
5110 PreviousCriticalLoc = Loc;
5111 return true;
5112 }
5113 return false;
5114 },
5115 FromParent: false /* skip top directive */);
5116 if (DeadLock) {
5117 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_critical_same_name)
5118 << CurrentName.getName();
5119 if (PreviousCriticalLoc.isValid())
5120 SemaRef.Diag(Loc: PreviousCriticalLoc,
5121 DiagID: diag::note_omp_previous_critical_region);
5122 return true;
5123 }
5124 } else if (CurrentRegion == OMPD_barrier || CurrentRegion == OMPD_scope) {
5125 // OpenMP 5.1 [2.22, Nesting of Regions]
5126 // A scope region may not be closely nested inside a worksharing, loop,
5127 // task, taskloop, critical, ordered, atomic, or masked region.
5128 // OpenMP 5.1 [2.22, Nesting of Regions]
5129 // A barrier region may not be closely nested inside a worksharing, loop,
5130 // task, taskloop, critical, ordered, atomic, or masked region.
5131 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5132 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5133 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5134 llvm::is_contained(Set: {OMPD_masked, OMPD_master,
5135 OMPD_critical, OMPD_ordered},
5136 Element: EnclosingConstruct);
5137 } else if (isOpenMPWorksharingDirective(DKind: CurrentRegion) &&
5138 !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5139 !isOpenMPTeamsDirective(DKind: CurrentRegion)) {
5140 // OpenMP 5.1 [2.22, Nesting of Regions]
5141 // A loop region that binds to a parallel region or a worksharing region
5142 // may not be closely nested inside a worksharing, loop, task, taskloop,
5143 // critical, ordered, atomic, or masked region.
5144 NestingProhibited = isOpenMPWorksharingDirective(DKind: ParentRegion) ||
5145 isOpenMPGenericLoopDirective(DKind: ParentRegion) ||
5146 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5147 llvm::is_contained(Set: {OMPD_masked, OMPD_master,
5148 OMPD_critical, OMPD_ordered},
5149 Element: EnclosingConstruct);
5150 Recommend = ShouldBeInParallelRegion;
5151 } else if (CurrentRegion == OMPD_ordered) {
5152 // OpenMP [2.16, Nesting of Regions]
5153 // An ordered region may not be closely nested inside a critical,
5154 // atomic, or explicit task region.
5155 // An ordered region must be closely nested inside a loop region (or
5156 // parallel loop region) with an ordered clause.
5157 // OpenMP [2.8.1,simd Construct, Restrictions]
5158 // An ordered construct with the simd clause is the only OpenMP construct
5159 // that can appear in the simd region.
5160 NestingProhibited = EnclosingConstruct == OMPD_critical ||
5161 isOpenMPTaskingDirective(Kind: ParentRegion) ||
5162 !(isOpenMPSimdDirective(DKind: ParentRegion) ||
5163 Stack->isParentOrderedRegion());
5164 Recommend = ShouldBeInOrderedRegion;
5165 } else if (isOpenMPNestingTeamsDirective(DKind: CurrentRegion)) {
5166 // OpenMP [2.16, Nesting of Regions]
5167 // If specified, a teams construct must be contained within a target
5168 // construct.
5169 NestingProhibited =
5170 (OMPVersion <= 45 && EnclosingConstruct != OMPD_target) ||
5171 (OMPVersion >= 50 && EnclosingConstruct != OMPD_unknown &&
5172 EnclosingConstruct != OMPD_target);
5173 OrphanSeen = ParentRegion == OMPD_unknown;
5174 Recommend = ShouldBeInTargetRegion;
5175 } else if (CurrentRegion == OMPD_scan) {
5176 if (OMPVersion >= 50) {
5177 // OpenMP spec 5.0 and 5.1 require scan to be directly enclosed by for,
5178 // simd, or for simd. This has to take into account combined directives.
5179 // In 5.2 this seems to be implied by the fact that the specified
5180 // separated constructs are do, for, and simd.
5181 NestingProhibited = !llvm::is_contained(
5182 Set: {OMPD_for, OMPD_simd, OMPD_for_simd}, Element: EnclosingConstruct);
5183 } else {
5184 NestingProhibited = true;
5185 }
5186 OrphanSeen = ParentRegion == OMPD_unknown;
5187 Recommend = ShouldBeInLoopSimdRegion;
5188 }
5189 if (!NestingProhibited && !isOpenMPTargetExecutionDirective(DKind: CurrentRegion) &&
5190 !isOpenMPTargetDataManagementDirective(DKind: CurrentRegion) &&
5191 EnclosingConstruct == OMPD_teams) {
5192 // OpenMP [5.1, 2.22, Nesting of Regions]
5193 // distribute, distribute simd, distribute parallel worksharing-loop,
5194 // distribute parallel worksharing-loop SIMD, loop, parallel regions,
5195 // including any parallel regions arising from combined constructs,
5196 // omp_get_num_teams() regions, and omp_get_team_num() regions are the
5197 // only OpenMP regions that may be strictly nested inside the teams
5198 // region.
5199 //
5200 // As an extension, we permit atomic within teams as well.
5201 NestingProhibited = !isOpenMPParallelDirective(DKind: CurrentRegion) &&
5202 !isOpenMPDistributeDirective(DKind: CurrentRegion) &&
5203 CurrentRegion != OMPD_loop &&
5204 !(SemaRef.getLangOpts().OpenMPExtensions &&
5205 CurrentRegion == OMPD_atomic);
5206 Recommend = ShouldBeInParallelRegion;
5207 }
5208 if (!NestingProhibited && CurrentRegion == OMPD_loop) {
5209 // OpenMP [5.1, 2.11.7, loop Construct, Restrictions]
5210 // If the bind clause is present on the loop construct and binding is
5211 // teams then the corresponding loop region must be strictly nested inside
5212 // a teams region.
5213 NestingProhibited =
5214 BindKind == OMPC_BIND_teams && EnclosingConstruct != OMPD_teams;
5215 Recommend = ShouldBeInTeamsRegion;
5216 }
5217 if (!NestingProhibited && isOpenMPNestingDistributeDirective(DKind: CurrentRegion)) {
5218 // OpenMP 4.5 [2.17 Nesting of Regions]
5219 // The region associated with the distribute construct must be strictly
5220 // nested inside a teams region
5221 NestingProhibited = EnclosingConstruct != OMPD_teams;
5222 Recommend = ShouldBeInTeamsRegion;
5223 }
5224 if (!NestingProhibited &&
5225 (isOpenMPTargetExecutionDirective(DKind: CurrentRegion) ||
5226 isOpenMPTargetDataManagementDirective(DKind: CurrentRegion))) {
5227 // OpenMP 4.5 [2.17 Nesting of Regions]
5228 // If a target, target update, target data, target enter data, or
5229 // target exit data construct is encountered during execution of a
5230 // target region, the behavior is unspecified.
5231 NestingProhibited = Stack->hasDirective(
5232 DPred: [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
5233 SourceLocation) {
5234 if (isOpenMPTargetExecutionDirective(DKind: K)) {
5235 OffendingRegion = K;
5236 return true;
5237 }
5238 return false;
5239 },
5240 FromParent: false /* don't skip top directive */);
5241 CloseNesting = false;
5242 }
5243 if (NestingProhibited) {
5244 if (OrphanSeen) {
5245 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
5246 << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion) << Recommend;
5247 } else {
5248 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region)
5249 << CloseNesting << getOpenMPDirectiveName(D: OffendingRegion, Ver: OMPVersion)
5250 << Recommend << getOpenMPDirectiveName(D: CurrentRegion, Ver: OMPVersion);
5251 }
5252 return true;
5253 }
5254 return false;
5255}
5256
5257struct Kind2Unsigned {
5258 using argument_type = OpenMPDirectiveKind;
5259 unsigned operator()(argument_type DK) { return unsigned(DK); }
5260};
5261static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
5262 ArrayRef<OMPClause *> Clauses,
5263 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
5264 bool ErrorFound = false;
5265 unsigned NamedModifiersNumber = 0;
5266 llvm::IndexedMap<const OMPIfClause *, Kind2Unsigned> FoundNameModifiers;
5267 FoundNameModifiers.resize(S: llvm::omp::Directive_enumSize + 1);
5268 SmallVector<SourceLocation, 4> NameModifierLoc;
5269 unsigned OMPVersion = S.getLangOpts().OpenMP;
5270 for (const OMPClause *C : Clauses) {
5271 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(Val: C)) {
5272 // At most one if clause without a directive-name-modifier can appear on
5273 // the directive.
5274 OpenMPDirectiveKind CurNM = IC->getNameModifier();
5275 auto &FNM = FoundNameModifiers[CurNM];
5276 if (FNM) {
5277 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
5278 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion)
5279 << getOpenMPClauseNameForDiag(C: OMPC_if) << (CurNM != OMPD_unknown)
5280 << getOpenMPDirectiveName(D: CurNM, Ver: OMPVersion);
5281 ErrorFound = true;
5282 } else if (CurNM != OMPD_unknown) {
5283 NameModifierLoc.push_back(Elt: IC->getNameModifierLoc());
5284 ++NamedModifiersNumber;
5285 }
5286 FNM = IC;
5287 if (CurNM == OMPD_unknown)
5288 continue;
5289 // Check if the specified name modifier is allowed for the current
5290 // directive.
5291 // At most one if clause with the particular directive-name-modifier can
5292 // appear on the directive.
5293 if (!llvm::is_contained(Range&: AllowedNameModifiers, Element: CurNM)) {
5294 S.Diag(Loc: IC->getNameModifierLoc(),
5295 DiagID: diag::err_omp_wrong_if_directive_name_modifier)
5296 << getOpenMPDirectiveName(D: CurNM, Ver: OMPVersion)
5297 << getOpenMPDirectiveName(D: Kind, Ver: OMPVersion);
5298 ErrorFound = true;
5299 }
5300 }
5301 }
5302 // If any if clause on the directive includes a directive-name-modifier then
5303 // all if clauses on the directive must include a directive-name-modifier.
5304 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
5305 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
5306 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
5307 DiagID: diag::err_omp_no_more_if_clause);
5308 } else {
5309 std::string Values;
5310 std::string Sep(", ");
5311 unsigned AllowedCnt = 0;
5312 unsigned TotalAllowedNum =
5313 AllowedNameModifiers.size() - NamedModifiersNumber;
5314 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
5315 ++Cnt) {
5316 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
5317 if (!FoundNameModifiers[NM]) {
5318 Values += "'";
5319 Values += getOpenMPDirectiveName(D: NM, Ver: OMPVersion);
5320 Values += "'";
5321 if (AllowedCnt + 2 == TotalAllowedNum)
5322 Values += " or ";
5323 else if (AllowedCnt + 1 != TotalAllowedNum)
5324 Values += Sep;
5325 ++AllowedCnt;
5326 }
5327 }
5328 S.Diag(Loc: FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
5329 DiagID: diag::err_omp_unnamed_if_clause)
5330 << (TotalAllowedNum > 1) << Values;
5331 }
5332 for (SourceLocation Loc : NameModifierLoc) {
5333 S.Diag(Loc, DiagID: diag::note_omp_previous_named_if_clause);
5334 }
5335 ErrorFound = true;
5336 }
5337 return ErrorFound;
5338}
5339
5340static std::pair<ValueDecl *, bool>
5341getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
5342 SourceRange &ERange, bool AllowArraySection,
5343 bool AllowAssumedSizeArray, StringRef DiagType) {
5344 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5345 RefExpr->containsUnexpandedParameterPack())
5346 return std::make_pair(x: nullptr, y: true);
5347
5348 // OpenMP [3.1, C/C++]
5349 // A list item is a variable name.
5350 // OpenMP [2.9.3.3, Restrictions, p.1]
5351 // A variable that is part of another variable (as an array or
5352 // structure element) cannot appear in a private clause.
5353 //
5354 // OpenMP [6.0]
5355 // 5.2.5 Array Sections, p. 166, L28-29
5356 // When the length is absent and the size of the dimension is not known,
5357 // the array section is an assumed-size array.
5358 // 2 Glossary, p. 23, L4-6
5359 // assumed-size array
5360 // For C/C++, an array section for which the length is absent and the
5361 // size of the dimensions is not known.
5362 // 5.2.5 Array Sections, p. 168, L11
5363 // An assumed-size array can appear only in clauses for which it is
5364 // explicitly allowed.
5365 // 7.4 List Item Privatization, Restrictions, p. 222, L15
5366 // Assumed-size arrays must not be privatized.
5367 RefExpr = RefExpr->IgnoreParens();
5368 enum {
5369 NoArrayExpr = -1,
5370 ArraySubscript = 0,
5371 OMPArraySection = 1
5372 } IsArrayExpr = NoArrayExpr;
5373 if (AllowArraySection) {
5374 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(Val: RefExpr)) {
5375 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
5376 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5377 Base = TempASE->getBase()->IgnoreParenImpCasts();
5378 RefExpr = Base;
5379 IsArrayExpr = ArraySubscript;
5380 } else if (auto *OASE = dyn_cast_or_null<ArraySectionExpr>(Val: RefExpr)) {
5381 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
5382 if (S.getLangOpts().OpenMP >= 60 && !AllowAssumedSizeArray &&
5383 OASE->getColonLocFirst().isValid() && !OASE->getLength()) {
5384 QualType BaseType = ArraySectionExpr::getBaseOriginalType(Base);
5385 if (BaseType.isNull() || (!BaseType->isConstantArrayType() &&
5386 !BaseType->isVariableArrayType())) {
5387 S.Diag(Loc: OASE->getColonLocFirst(),
5388 DiagID: diag::err_omp_section_length_undefined)
5389 << (!BaseType.isNull() && BaseType->isArrayType());
5390 return std::make_pair(x: nullptr, y: false);
5391 }
5392 }
5393 while (auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base))
5394 Base = TempOASE->getBase()->IgnoreParenImpCasts();
5395 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base))
5396 Base = TempASE->getBase()->IgnoreParenImpCasts();
5397 RefExpr = Base;
5398 IsArrayExpr = OMPArraySection;
5399 }
5400 }
5401 ELoc = RefExpr->getExprLoc();
5402 ERange = RefExpr->getSourceRange();
5403 RefExpr = RefExpr->IgnoreParenImpCasts();
5404 auto *DE = dyn_cast_or_null<DeclRefExpr>(Val: RefExpr);
5405 auto *ME = dyn_cast_or_null<MemberExpr>(Val: RefExpr);
5406 if ((!DE || !isa<VarDecl>(Val: DE->getDecl())) &&
5407 (S.getCurrentThisType().isNull() || !ME ||
5408 !isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()) ||
5409 !isa<FieldDecl>(Val: ME->getMemberDecl()))) {
5410 if (IsArrayExpr != NoArrayExpr) {
5411 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
5412 << IsArrayExpr << ERange;
5413 } else if (!DiagType.empty()) {
5414 unsigned DiagSelect = S.getLangOpts().CPlusPlus
5415 ? (S.getCurrentThisType().isNull() ? 1 : 2)
5416 : 0;
5417 S.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr_with_type)
5418 << DiagSelect << DiagType << ERange;
5419 } else {
5420 S.Diag(Loc: ELoc,
5421 DiagID: AllowArraySection
5422 ? diag::err_omp_expected_var_name_member_expr_or_array_item
5423 : diag::err_omp_expected_var_name_member_expr)
5424 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
5425 }
5426 return std::make_pair(x: nullptr, y: false);
5427 }
5428 return std::make_pair(
5429 x: getCanonicalDecl(D: DE ? DE->getDecl() : ME->getMemberDecl()), y: false);
5430}
5431
5432namespace {
5433/// Checks if the allocator is used in uses_allocators clause to be allowed in
5434/// target regions.
5435class AllocatorChecker final : public ConstStmtVisitor<AllocatorChecker, bool> {
5436 DSAStackTy *S = nullptr;
5437
5438public:
5439 bool VisitDeclRefExpr(const DeclRefExpr *E) {
5440 return S->isUsesAllocatorsDecl(D: E->getDecl())
5441 .value_or(u: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait) ==
5442 DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait;
5443 }
5444 bool VisitStmt(const Stmt *S) {
5445 for (const Stmt *Child : S->children()) {
5446 if (Child && Visit(S: Child))
5447 return true;
5448 }
5449 return false;
5450 }
5451 explicit AllocatorChecker(DSAStackTy *S) : S(S) {}
5452};
5453} // namespace
5454
5455static void checkAllocateClauses(Sema &S, DSAStackTy *Stack,
5456 ArrayRef<OMPClause *> Clauses) {
5457 assert(!S.CurContext->isDependentContext() &&
5458 "Expected non-dependent context.");
5459 auto AllocateRange =
5460 llvm::make_filter_range(Range&: Clauses, Pred: OMPAllocateClause::classof);
5461 llvm::DenseMap<CanonicalDeclPtr<Decl>, CanonicalDeclPtr<VarDecl>> DeclToCopy;
5462 auto PrivateRange = llvm::make_filter_range(Range&: Clauses, Pred: [](const OMPClause *C) {
5463 return isOpenMPPrivate(Kind: C->getClauseKind());
5464 });
5465 for (OMPClause *Cl : PrivateRange) {
5466 MutableArrayRef<Expr *>::iterator I, It, Et;
5467 if (Cl->getClauseKind() == OMPC_private) {
5468 auto *PC = cast<OMPPrivateClause>(Val: Cl);
5469 I = PC->private_copies().begin();
5470 It = PC->varlist_begin();
5471 Et = PC->varlist_end();
5472 } else if (Cl->getClauseKind() == OMPC_firstprivate) {
5473 auto *PC = cast<OMPFirstprivateClause>(Val: Cl);
5474 I = PC->private_copies().begin();
5475 It = PC->varlist_begin();
5476 Et = PC->varlist_end();
5477 } else if (Cl->getClauseKind() == OMPC_lastprivate) {
5478 auto *PC = cast<OMPLastprivateClause>(Val: Cl);
5479 I = PC->private_copies().begin();
5480 It = PC->varlist_begin();
5481 Et = PC->varlist_end();
5482 } else if (Cl->getClauseKind() == OMPC_linear) {
5483 auto *PC = cast<OMPLinearClause>(Val: Cl);
5484 I = PC->privates().begin();
5485 It = PC->varlist_begin();
5486 Et = PC->varlist_end();
5487 } else if (Cl->getClauseKind() == OMPC_reduction) {
5488 auto *PC = cast<OMPReductionClause>(Val: Cl);
5489 I = PC->privates().begin();
5490 It = PC->varlist_begin();
5491 Et = PC->varlist_end();
5492 } else if (Cl->getClauseKind() == OMPC_task_reduction) {
5493 auto *PC = cast<OMPTaskReductionClause>(Val: Cl);
5494 I = PC->privates().begin();
5495 It = PC->varlist_begin();
5496 Et = PC->varlist_end();
5497 } else if (Cl->getClauseKind() == OMPC_in_reduction) {
5498 auto *PC = cast<OMPInReductionClause>(Val: Cl);
5499 I = PC->privates().begin();
5500 It = PC->varlist_begin();
5501 Et = PC->varlist_end();
5502 } else {
5503 llvm_unreachable("Expected private clause.");
5504 }
5505 for (Expr *E : llvm::make_range(x: It, y: Et)) {
5506 if (!*I) {
5507 ++I;
5508 continue;
5509 }
5510 SourceLocation ELoc;
5511 SourceRange ERange;
5512 Expr *SimpleRefExpr = E;
5513 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
5514 /*AllowArraySection=*/true);
5515 DeclToCopy.try_emplace(Key: Res.first,
5516 Args: cast<VarDecl>(Val: cast<DeclRefExpr>(Val: *I)->getDecl()));
5517 ++I;
5518 }
5519 }
5520 for (OMPClause *C : AllocateRange) {
5521 auto *AC = cast<OMPAllocateClause>(Val: C);
5522 if (S.getLangOpts().OpenMP >= 50 &&
5523 !Stack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>() &&
5524 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()) &&
5525 AC->getAllocator()) {
5526 Expr *Allocator = AC->getAllocator();
5527 // OpenMP, 2.12.5 target Construct
5528 // Memory allocators that do not appear in a uses_allocators clause cannot
5529 // appear as an allocator in an allocate clause or be used in the target
5530 // region unless a requires directive with the dynamic_allocators clause
5531 // is present in the same compilation unit.
5532 AllocatorChecker Checker(Stack);
5533 if (Checker.Visit(S: Allocator))
5534 S.Diag(Loc: Allocator->getExprLoc(),
5535 DiagID: diag::err_omp_allocator_not_in_uses_allocators)
5536 << Allocator->getSourceRange();
5537 }
5538 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorKind =
5539 getAllocatorKind(S, Stack, Allocator: AC->getAllocator());
5540 // OpenMP, 2.11.4 allocate Clause, Restrictions.
5541 // For task, taskloop or target directives, allocation requests to memory
5542 // allocators with the trait access set to thread result in unspecified
5543 // behavior.
5544 if (AllocatorKind == OMPAllocateDeclAttr::OMPThreadMemAlloc &&
5545 (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
5546 isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective()))) {
5547 unsigned OMPVersion = S.getLangOpts().OpenMP;
5548 S.Diag(Loc: AC->getAllocator()->getExprLoc(),
5549 DiagID: diag::warn_omp_allocate_thread_on_task_target_directive)
5550 << getOpenMPDirectiveName(D: Stack->getCurrentDirective(), Ver: OMPVersion);
5551 }
5552 for (Expr *E : AC->varlist()) {
5553 SourceLocation ELoc;
5554 SourceRange ERange;
5555 Expr *SimpleRefExpr = E;
5556 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
5557 ValueDecl *VD = Res.first;
5558 if (!VD)
5559 continue;
5560 DSAStackTy::DSAVarData Data = Stack->getTopDSA(D: VD, /*FromParent=*/false);
5561 if (!isOpenMPPrivate(Kind: Data.CKind)) {
5562 S.Diag(Loc: E->getExprLoc(),
5563 DiagID: diag::err_omp_expected_private_copy_for_allocate);
5564 continue;
5565 }
5566 VarDecl *PrivateVD = DeclToCopy[VD];
5567 if (checkPreviousOMPAllocateAttribute(S, Stack, RefExpr: E, VD: PrivateVD,
5568 AllocatorKind, Allocator: AC->getAllocator()))
5569 continue;
5570 applyOMPAllocateAttribute(S, VD: PrivateVD, AllocatorKind, Allocator: AC->getAllocator(),
5571 Alignment: AC->getAlignment(), SR: E->getSourceRange());
5572 }
5573 }
5574}
5575
5576namespace {
5577/// Rewrite statements and expressions for Sema \p Actions CurContext.
5578///
5579/// Used to wrap already parsed statements/expressions into a new CapturedStmt
5580/// context. DeclRefExpr used inside the new context are changed to refer to the
5581/// captured variable instead.
5582class CaptureVars : public TreeTransform<CaptureVars> {
5583 using BaseTransform = TreeTransform<CaptureVars>;
5584
5585public:
5586 CaptureVars(Sema &Actions) : BaseTransform(Actions) {}
5587
5588 bool AlwaysRebuild() { return true; }
5589};
5590} // namespace
5591
5592static VarDecl *precomputeExpr(Sema &Actions,
5593 SmallVectorImpl<Stmt *> &BodyStmts, Expr *E,
5594 StringRef Name) {
5595 Expr *NewE = AssertSuccess(R: CaptureVars(Actions).TransformExpr(E));
5596 VarDecl *NewVar = buildVarDecl(SemaRef&: Actions, Loc: {}, Type: NewE->getType(), Name, Attrs: nullptr,
5597 OrigRef: dyn_cast<DeclRefExpr>(Val: E->IgnoreImplicit()));
5598 auto *NewDeclStmt = cast<DeclStmt>(Val: AssertSuccess(
5599 R: Actions.ActOnDeclStmt(Decl: Actions.ConvertDeclToDeclGroup(Ptr: NewVar), StartLoc: {}, EndLoc: {})));
5600 Actions.AddInitializerToDecl(dcl: NewDeclStmt->getSingleDecl(), init: NewE, DirectInit: false);
5601 BodyStmts.push_back(Elt: NewDeclStmt);
5602 return NewVar;
5603}
5604
5605/// Create a closure that computes the number of iterations of a loop.
5606///
5607/// \param Actions The Sema object.
5608/// \param LogicalTy Type for the logical iteration number.
5609/// \param Rel Comparison operator of the loop condition.
5610/// \param StartExpr Value of the loop counter at the first iteration.
5611/// \param StopExpr Expression the loop counter is compared against in the loop
5612/// condition. \param StepExpr Amount of increment after each iteration.
5613///
5614/// \return Closure (CapturedStmt) of the distance calculation.
5615static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy,
5616 BinaryOperator::Opcode Rel,
5617 Expr *StartExpr, Expr *StopExpr,
5618 Expr *StepExpr) {
5619 ASTContext &Ctx = Actions.getASTContext();
5620 TypeSourceInfo *LogicalTSI = Ctx.getTrivialTypeSourceInfo(T: LogicalTy);
5621
5622 // Captured regions currently don't support return values, we use an
5623 // out-parameter instead. All inputs are implicit captures.
5624 // TODO: Instead of capturing each DeclRefExpr occurring in
5625 // StartExpr/StopExpr/Step, these could also be passed as a value capture.
5626 QualType ResultTy = Ctx.getLValueReferenceType(T: LogicalTy);
5627 Sema::CapturedParamNameType Params[] = {{"Distance", ResultTy},
5628 {StringRef(), QualType()}};
5629 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
5630
5631 Stmt *Body;
5632 {
5633 Sema::CompoundScopeRAII CompoundScope(Actions);
5634 CapturedDecl *CS = cast<CapturedDecl>(Val: Actions.CurContext);
5635
5636 // Get the LValue expression for the result.
5637 ImplicitParamDecl *DistParam = CS->getParam(i: 0);
5638 DeclRefExpr *DistRef = Actions.BuildDeclRefExpr(
5639 D: DistParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5640
5641 SmallVector<Stmt *, 4> BodyStmts;
5642
5643 // Capture all referenced variable references.
5644 // TODO: Instead of computing NewStart/NewStop/NewStep inside the
5645 // CapturedStmt, we could compute them before and capture the result, to be
5646 // used jointly with the LoopVar function.
5647 VarDecl *NewStart = precomputeExpr(Actions, BodyStmts, E: StartExpr, Name: ".start");
5648 VarDecl *NewStop = precomputeExpr(Actions, BodyStmts, E: StopExpr, Name: ".stop");
5649 VarDecl *NewStep = precomputeExpr(Actions, BodyStmts, E: StepExpr, Name: ".step");
5650 auto BuildVarRef = [&](VarDecl *VD) {
5651 return buildDeclRefExpr(S&: Actions, D: VD, Ty: VD->getType(), Loc: {});
5652 };
5653
5654 IntegerLiteral *Zero = IntegerLiteral::Create(
5655 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 0), type: LogicalTy, l: {});
5656 IntegerLiteral *One = IntegerLiteral::Create(
5657 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
5658 Expr *Dist;
5659 if (Rel == BO_NE) {
5660 // When using a != comparison, the increment can be +1 or -1. This can be
5661 // dynamic at runtime, so we need to check for the direction.
5662 Expr *IsNegStep = AssertSuccess(
5663 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_LT, LHSExpr: BuildVarRef(NewStep), RHSExpr: Zero));
5664
5665 // Positive increment.
5666 Expr *ForwardRange = AssertSuccess(R: Actions.BuildBinOp(
5667 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
5668 ForwardRange = AssertSuccess(
5669 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: ForwardRange));
5670 Expr *ForwardDist = AssertSuccess(R: Actions.BuildBinOp(
5671 S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: ForwardRange, RHSExpr: BuildVarRef(NewStep)));
5672
5673 // Negative increment.
5674 Expr *BackwardRange = AssertSuccess(R: Actions.BuildBinOp(
5675 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5676 BackwardRange = AssertSuccess(
5677 R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: BackwardRange));
5678 Expr *NegIncAmount = AssertSuccess(
5679 R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: BuildVarRef(NewStep)));
5680 Expr *BackwardDist = AssertSuccess(
5681 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: BackwardRange, RHSExpr: NegIncAmount));
5682
5683 // Use the appropriate case.
5684 Dist = AssertSuccess(R: Actions.ActOnConditionalOp(
5685 QuestionLoc: {}, ColonLoc: {}, CondExpr: IsNegStep, LHSExpr: BackwardDist, RHSExpr: ForwardDist));
5686 } else {
5687 assert((Rel == BO_LT || Rel == BO_LE || Rel == BO_GE || Rel == BO_GT) &&
5688 "Expected one of these relational operators");
5689
5690 // We can derive the direction from any other comparison operator. It is
5691 // non well-formed OpenMP if Step increments/decrements in the other
5692 // directions. Whether at least the first iteration passes the loop
5693 // condition.
5694 Expr *HasAnyIteration = AssertSuccess(R: Actions.BuildBinOp(
5695 S: nullptr, OpLoc: {}, Opc: Rel, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5696
5697 // Compute the range between first and last counter value.
5698 Expr *Range;
5699 if (Rel == BO_GE || Rel == BO_GT)
5700 Range = AssertSuccess(R: Actions.BuildBinOp(
5701 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStart), RHSExpr: BuildVarRef(NewStop)));
5702 else
5703 Range = AssertSuccess(R: Actions.BuildBinOp(
5704 S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: BuildVarRef(NewStop), RHSExpr: BuildVarRef(NewStart)));
5705
5706 // Ensure unsigned range space.
5707 Range =
5708 AssertSuccess(R: Actions.BuildCStyleCastExpr(LParenLoc: {}, Ty: LogicalTSI, RParenLoc: {}, Op: Range));
5709
5710 if (Rel == BO_LE || Rel == BO_GE) {
5711 // Add one to the range if the relational operator is inclusive.
5712 Range =
5713 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: One));
5714 }
5715
5716 // Divide by the absolute step amount. If the range is not a multiple of
5717 // the step size, rounding-up the effective upper bound ensures that the
5718 // last iteration is included.
5719 // Note that the rounding-up may cause an overflow in a temporary that
5720 // could be avoided, but would have occurred in a C-style for-loop as
5721 // well.
5722 Expr *Divisor = BuildVarRef(NewStep);
5723 if (Rel == BO_GE || Rel == BO_GT)
5724 Divisor =
5725 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: Divisor));
5726 Expr *DivisorMinusOne =
5727 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Sub, LHSExpr: Divisor, RHSExpr: One));
5728 Expr *RangeRoundUp = AssertSuccess(
5729 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: Range, RHSExpr: DivisorMinusOne));
5730 Dist = AssertSuccess(
5731 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Div, LHSExpr: RangeRoundUp, RHSExpr: Divisor));
5732
5733 // If there is not at least one iteration, the range contains garbage. Fix
5734 // to zero in this case.
5735 Dist = AssertSuccess(
5736 R: Actions.ActOnConditionalOp(QuestionLoc: {}, ColonLoc: {}, CondExpr: HasAnyIteration, LHSExpr: Dist, RHSExpr: Zero));
5737 }
5738
5739 // Assign the result to the out-parameter.
5740 Stmt *ResultAssign = AssertSuccess(R: Actions.BuildBinOp(
5741 S: Actions.getCurScope(), OpLoc: {}, Opc: BO_Assign, LHSExpr: DistRef, RHSExpr: Dist));
5742 BodyStmts.push_back(Elt: ResultAssign);
5743
5744 Body = AssertSuccess(R: Actions.ActOnCompoundStmt(L: {}, R: {}, Elts: BodyStmts, isStmtExpr: false));
5745 }
5746
5747 return cast<CapturedStmt>(
5748 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
5749}
5750
5751/// Create a closure that computes the loop variable from the logical iteration
5752/// number.
5753///
5754/// \param Actions The Sema object.
5755/// \param LoopVarTy Type for the loop variable used for result value.
5756/// \param LogicalTy Type for the logical iteration number.
5757/// \param StartExpr Value of the loop counter at the first iteration.
5758/// \param Step Amount of increment after each iteration.
5759/// \param Deref Whether the loop variable is a dereference of the loop
5760/// counter variable.
5761///
5762/// \return Closure (CapturedStmt) of the loop value calculation.
5763static CapturedStmt *buildLoopVarFunc(Sema &Actions, QualType LoopVarTy,
5764 QualType LogicalTy,
5765 DeclRefExpr *StartExpr, Expr *Step,
5766 bool Deref) {
5767 ASTContext &Ctx = Actions.getASTContext();
5768
5769 // Pass the result as an out-parameter. Passing as return value would require
5770 // the OpenMPIRBuilder to know additional C/C++ semantics, such as how to
5771 // invoke a copy constructor.
5772 QualType TargetParamTy = Ctx.getLValueReferenceType(T: LoopVarTy);
5773 SemaOpenMP::CapturedParamNameType Params[] = {{"LoopVar", TargetParamTy},
5774 {"Logical", LogicalTy},
5775 {StringRef(), QualType()}};
5776 Actions.ActOnCapturedRegionStart(Loc: {}, CurScope: nullptr, Kind: CR_Default, Params);
5777
5778 // Capture the initial iterator which represents the LoopVar value at the
5779 // zero's logical iteration. Since the original ForStmt/CXXForRangeStmt update
5780 // it in every iteration, capture it by value before it is modified.
5781 VarDecl *StartVar = cast<VarDecl>(Val: StartExpr->getDecl());
5782 bool Invalid = Actions.tryCaptureVariable(Var: StartVar, Loc: {},
5783 Kind: TryCaptureKind::ExplicitByVal, EllipsisLoc: {});
5784 (void)Invalid;
5785 assert(!Invalid && "Expecting capture-by-value to work.");
5786
5787 Expr *Body;
5788 {
5789 Sema::CompoundScopeRAII CompoundScope(Actions);
5790 auto *CS = cast<CapturedDecl>(Val: Actions.CurContext);
5791
5792 ImplicitParamDecl *TargetParam = CS->getParam(i: 0);
5793 DeclRefExpr *TargetRef = Actions.BuildDeclRefExpr(
5794 D: TargetParam, Ty: LoopVarTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5795 ImplicitParamDecl *IndvarParam = CS->getParam(i: 1);
5796 DeclRefExpr *LogicalRef = Actions.BuildDeclRefExpr(
5797 D: IndvarParam, Ty: LogicalTy, VK: VK_LValue, NameInfo: {}, SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5798
5799 // Capture the Start expression.
5800 CaptureVars Recap(Actions);
5801 Expr *NewStart = AssertSuccess(R: Recap.TransformExpr(E: StartExpr));
5802 Expr *NewStep = AssertSuccess(R: Recap.TransformExpr(E: Step));
5803
5804 Expr *Skip = AssertSuccess(
5805 R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Mul, LHSExpr: NewStep, RHSExpr: LogicalRef));
5806 // TODO: Explicitly cast to the iterator's difference_type instead of
5807 // relying on implicit conversion.
5808 Expr *Advanced =
5809 AssertSuccess(R: Actions.BuildBinOp(S: nullptr, OpLoc: {}, Opc: BO_Add, LHSExpr: NewStart, RHSExpr: Skip));
5810
5811 if (Deref) {
5812 // For range-based for-loops convert the loop counter value to a concrete
5813 // loop variable value by dereferencing the iterator.
5814 Advanced =
5815 AssertSuccess(R: Actions.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Deref, Input: Advanced));
5816 }
5817
5818 // Assign the result to the output parameter.
5819 Body = AssertSuccess(R: Actions.BuildBinOp(S: Actions.getCurScope(), OpLoc: {},
5820 Opc: BO_Assign, LHSExpr: TargetRef, RHSExpr: Advanced));
5821 }
5822 return cast<CapturedStmt>(
5823 Val: AssertSuccess(R: Actions.ActOnCapturedRegionEnd(S: Body)));
5824}
5825
5826StmtResult SemaOpenMP::ActOnOpenMPCanonicalLoop(Stmt *AStmt) {
5827 ASTContext &Ctx = getASTContext();
5828
5829 // Extract the common elements of ForStmt and CXXForRangeStmt:
5830 // Loop variable, repeat condition, increment
5831 Expr *Cond, *Inc;
5832 VarDecl *LIVDecl, *LUVDecl;
5833 if (auto *For = dyn_cast<ForStmt>(Val: AStmt)) {
5834 Stmt *Init = For->getInit();
5835 if (auto *LCVarDeclStmt = dyn_cast<DeclStmt>(Val: Init)) {
5836 // For statement declares loop variable.
5837 LIVDecl = cast<VarDecl>(Val: LCVarDeclStmt->getSingleDecl());
5838 } else if (auto *LCAssign = dyn_cast<BinaryOperator>(Val: Init)) {
5839 // For statement reuses variable.
5840 assert(LCAssign->getOpcode() == BO_Assign &&
5841 "init part must be a loop variable assignment");
5842 auto *CounterRef = cast<DeclRefExpr>(Val: LCAssign->getLHS());
5843 LIVDecl = cast<VarDecl>(Val: CounterRef->getDecl());
5844 } else
5845 llvm_unreachable("Cannot determine loop variable");
5846 LUVDecl = LIVDecl;
5847
5848 Cond = For->getCond();
5849 Inc = For->getInc();
5850 } else if (auto *RangeFor = dyn_cast<CXXForRangeStmt>(Val: AStmt)) {
5851 DeclStmt *BeginStmt = RangeFor->getBeginStmt();
5852 LIVDecl = cast<VarDecl>(Val: BeginStmt->getSingleDecl());
5853 LUVDecl = RangeFor->getLoopVariable();
5854
5855 Cond = RangeFor->getCond();
5856 Inc = RangeFor->getInc();
5857 } else
5858 llvm_unreachable("unhandled kind of loop");
5859
5860 QualType CounterTy = LIVDecl->getType();
5861 QualType LVTy = LUVDecl->getType();
5862
5863 // Analyze the loop condition.
5864 Expr *LHS, *RHS;
5865 BinaryOperator::Opcode CondRel;
5866 Cond = Cond->IgnoreImplicit();
5867 if (auto *CondBinExpr = dyn_cast<BinaryOperator>(Val: Cond)) {
5868 LHS = CondBinExpr->getLHS();
5869 RHS = CondBinExpr->getRHS();
5870 CondRel = CondBinExpr->getOpcode();
5871 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Cond)) {
5872 assert(CondCXXOp->getNumArgs() == 2 && "Comparison should have 2 operands");
5873 LHS = CondCXXOp->getArg(Arg: 0);
5874 RHS = CondCXXOp->getArg(Arg: 1);
5875 switch (CondCXXOp->getOperator()) {
5876 case OO_ExclaimEqual:
5877 CondRel = BO_NE;
5878 break;
5879 case OO_Less:
5880 CondRel = BO_LT;
5881 break;
5882 case OO_LessEqual:
5883 CondRel = BO_LE;
5884 break;
5885 case OO_Greater:
5886 CondRel = BO_GT;
5887 break;
5888 case OO_GreaterEqual:
5889 CondRel = BO_GE;
5890 break;
5891 default:
5892 llvm_unreachable("unexpected iterator operator");
5893 }
5894 } else
5895 llvm_unreachable("unexpected loop condition");
5896
5897 // Normalize such that the loop counter is on the LHS.
5898 if (!isa<DeclRefExpr>(Val: LHS->IgnoreImplicit()) ||
5899 cast<DeclRefExpr>(Val: LHS->IgnoreImplicit())->getDecl() != LIVDecl) {
5900 std::swap(a&: LHS, b&: RHS);
5901 CondRel = BinaryOperator::reverseComparisonOp(Opc: CondRel);
5902 }
5903 auto *CounterRef = cast<DeclRefExpr>(Val: LHS->IgnoreImplicit());
5904
5905 // Decide the bit width for the logical iteration counter. By default use the
5906 // unsigned ptrdiff_t integer size (for iterators and pointers).
5907 // TODO: For iterators, use iterator::difference_type,
5908 // std::iterator_traits<>::difference_type or decltype(it - end).
5909 QualType LogicalTy = Ctx.getUnsignedPointerDiffType();
5910 if (CounterTy->isIntegerType()) {
5911 unsigned BitWidth = Ctx.getIntWidth(T: CounterTy);
5912 LogicalTy = Ctx.getIntTypeForBitwidth(DestWidth: BitWidth, Signed: false);
5913 }
5914
5915 // Analyze the loop increment.
5916 Expr *Step;
5917 if (auto *IncUn = dyn_cast<UnaryOperator>(Val: Inc)) {
5918 int Direction;
5919 switch (IncUn->getOpcode()) {
5920 case UO_PreInc:
5921 case UO_PostInc:
5922 Direction = 1;
5923 break;
5924 case UO_PreDec:
5925 case UO_PostDec:
5926 Direction = -1;
5927 break;
5928 default:
5929 llvm_unreachable("unhandled unary increment operator");
5930 }
5931 Step = IntegerLiteral::Create(
5932 C: Ctx,
5933 V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), Direction, /*isSigned=*/true),
5934 type: LogicalTy, l: {});
5935 } else if (auto *IncBin = dyn_cast<BinaryOperator>(Val: Inc)) {
5936 if (IncBin->getOpcode() == BO_AddAssign) {
5937 Step = IncBin->getRHS();
5938 } else if (IncBin->getOpcode() == BO_SubAssign) {
5939 Step = AssertSuccess(
5940 R: SemaRef.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: IncBin->getRHS()));
5941 } else
5942 llvm_unreachable("unhandled binary increment operator");
5943 } else if (auto *CondCXXOp = dyn_cast<CXXOperatorCallExpr>(Val: Inc)) {
5944 switch (CondCXXOp->getOperator()) {
5945 case OO_PlusPlus:
5946 Step = IntegerLiteral::Create(
5947 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), 1), type: LogicalTy, l: {});
5948 break;
5949 case OO_MinusMinus:
5950 Step = IntegerLiteral::Create(
5951 C: Ctx, V: llvm::APInt(Ctx.getIntWidth(T: LogicalTy), -1), type: LogicalTy, l: {});
5952 break;
5953 case OO_PlusEqual:
5954 Step = CondCXXOp->getArg(Arg: 1);
5955 break;
5956 case OO_MinusEqual:
5957 Step = AssertSuccess(
5958 R: SemaRef.BuildUnaryOp(S: nullptr, OpLoc: {}, Opc: UO_Minus, Input: CondCXXOp->getArg(Arg: 1)));
5959 break;
5960 default:
5961 llvm_unreachable("unhandled overloaded increment operator");
5962 }
5963 } else
5964 llvm_unreachable("unknown increment expression");
5965
5966 CapturedStmt *DistanceFunc =
5967 buildDistanceFunc(Actions&: SemaRef, LogicalTy, Rel: CondRel, StartExpr: LHS, StopExpr: RHS, StepExpr: Step);
5968 CapturedStmt *LoopVarFunc = buildLoopVarFunc(
5969 Actions&: SemaRef, LoopVarTy: LVTy, LogicalTy, StartExpr: CounterRef, Step, Deref: isa<CXXForRangeStmt>(Val: AStmt));
5970 DeclRefExpr *LVRef =
5971 SemaRef.BuildDeclRefExpr(D: LUVDecl, Ty: LUVDecl->getType(), VK: VK_LValue, NameInfo: {},
5972 SS: nullptr, FoundD: nullptr, TemplateKWLoc: {}, TemplateArgs: nullptr);
5973 return OMPCanonicalLoop::create(Ctx: getASTContext(), LoopStmt: AStmt, DistanceFunc,
5974 LoopVarFunc, LoopVarRef: LVRef);
5975}
5976
5977StmtResult SemaOpenMP::ActOnOpenMPLoopnest(Stmt *AStmt) {
5978 // Handle a literal loop.
5979 if (isa<ForStmt>(Val: AStmt) || isa<CXXForRangeStmt>(Val: AStmt))
5980 return ActOnOpenMPCanonicalLoop(AStmt);
5981
5982 // If not a literal loop, it must be the result of a loop transformation.
5983 OMPExecutableDirective *LoopTransform = cast<OMPExecutableDirective>(Val: AStmt);
5984 assert(
5985 isOpenMPLoopTransformationDirective(LoopTransform->getDirectiveKind()) &&
5986 "Loop transformation directive expected");
5987 return LoopTransform;
5988}
5989
5990static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
5991 CXXScopeSpec &MapperIdScopeSpec,
5992 const DeclarationNameInfo &MapperId,
5993 QualType Type,
5994 Expr *UnresolvedMapper);
5995
5996/// Perform DFS through the structure/class data members trying to find
5997/// member(s) with user-defined 'default' mapper and generate implicit map
5998/// clauses for such members with the found 'default' mapper.
5999static void
6000processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack,
6001 SmallVectorImpl<OMPClause *> &Clauses) {
6002 // Check for the default mapper for data members.
6003 if (S.getLangOpts().OpenMP < 50)
6004 return;
6005 for (int Cnt = 0, EndCnt = Clauses.size(); Cnt < EndCnt; ++Cnt) {
6006 auto *C = dyn_cast<OMPMapClause>(Val: Clauses[Cnt]);
6007 if (!C)
6008 continue;
6009 SmallVector<Expr *, 4> SubExprs;
6010 auto *MI = C->mapperlist_begin();
6011 for (auto I = C->varlist_begin(), End = C->varlist_end(); I != End;
6012 ++I, ++MI) {
6013 // Expression is mapped using mapper - skip it.
6014 if (*MI)
6015 continue;
6016 Expr *E = *I;
6017 // Expression is dependent - skip it, build the mapper when it gets
6018 // instantiated.
6019 if (E->isTypeDependent() || E->isValueDependent() ||
6020 E->containsUnexpandedParameterPack())
6021 continue;
6022 // Array section - need to check for the mapping of the array section
6023 // element.
6024 QualType CanonType = E->getType().getCanonicalType();
6025 if (CanonType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
6026 const auto *OASE = cast<ArraySectionExpr>(Val: E->IgnoreParenImpCasts());
6027 QualType BaseType =
6028 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
6029 QualType ElemType;
6030 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
6031 ElemType = ATy->getElementType();
6032 else
6033 ElemType = BaseType->getPointeeType();
6034 CanonType = ElemType;
6035 }
6036
6037 // DFS over data members in structures/classes.
6038 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(
6039 1, {CanonType, nullptr});
6040 llvm::DenseMap<const Type *, Expr *> Visited;
6041 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(
6042 1, {nullptr, 1});
6043 while (!Types.empty()) {
6044 QualType BaseType;
6045 FieldDecl *CurFD;
6046 std::tie(args&: BaseType, args&: CurFD) = Types.pop_back_val();
6047 while (ParentChain.back().second == 0)
6048 ParentChain.pop_back();
6049 --ParentChain.back().second;
6050 if (BaseType.isNull())
6051 continue;
6052 // Only structs/classes are allowed to have mappers.
6053 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
6054 if (!RD)
6055 continue;
6056 auto It = Visited.find(Val: BaseType.getTypePtr());
6057 if (It == Visited.end()) {
6058 // Try to find the associated user-defined mapper.
6059 CXXScopeSpec MapperIdScopeSpec;
6060 DeclarationNameInfo DefaultMapperId;
6061 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
6062 ID: &S.Context.Idents.get(Name: "default")));
6063 DefaultMapperId.setLoc(E->getExprLoc());
6064 ExprResult ER = buildUserDefinedMapperRef(
6065 SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec, MapperId: DefaultMapperId,
6066 Type: BaseType, /*UnresolvedMapper=*/nullptr);
6067 if (ER.isInvalid())
6068 continue;
6069 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args: ER.get()).first;
6070 }
6071 // Found default mapper.
6072 if (It->second) {
6073 auto *OE = new (S.Context) OpaqueValueExpr(E->getExprLoc(), CanonType,
6074 VK_LValue, OK_Ordinary, E);
6075 OE->setIsUnique(/*V=*/true);
6076 Expr *BaseExpr = OE;
6077 for (const auto &P : ParentChain) {
6078 if (P.first) {
6079 BaseExpr = S.BuildMemberExpr(
6080 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6081 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: P.first,
6082 FoundDecl: DeclAccessPair::make(D: P.first, AS: P.first->getAccess()),
6083 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6084 Ty: P.first->getType(), VK: VK_LValue, OK: OK_Ordinary);
6085 BaseExpr = S.DefaultLvalueConversion(E: BaseExpr).get();
6086 }
6087 }
6088 if (CurFD)
6089 BaseExpr = S.BuildMemberExpr(
6090 Base: BaseExpr, /*IsArrow=*/false, OpLoc: E->getExprLoc(),
6091 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), Member: CurFD,
6092 FoundDecl: DeclAccessPair::make(D: CurFD, AS: CurFD->getAccess()),
6093 /*HadMultipleCandidates=*/false, MemberNameInfo: DeclarationNameInfo(),
6094 Ty: CurFD->getType(), VK: VK_LValue, OK: OK_Ordinary);
6095 SubExprs.push_back(Elt: BaseExpr);
6096 continue;
6097 }
6098 // Check for the "default" mapper for data members.
6099 bool FirstIter = true;
6100 for (FieldDecl *FD : RD->fields()) {
6101 if (!FD)
6102 continue;
6103 QualType FieldTy = FD->getType();
6104 if (FieldTy.isNull() ||
6105 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
6106 continue;
6107 if (FirstIter) {
6108 FirstIter = false;
6109 ParentChain.emplace_back(Args&: CurFD, Args: 1);
6110 } else {
6111 ++ParentChain.back().second;
6112 }
6113 Types.emplace_back(Args&: FieldTy, Args&: FD);
6114 }
6115 }
6116 }
6117 if (SubExprs.empty())
6118 continue;
6119 CXXScopeSpec MapperIdScopeSpec;
6120 DeclarationNameInfo MapperId;
6121 if (OMPClause *NewClause = S.OpenMP().ActOnOpenMPMapClause(
6122 IteratorModifier: nullptr, MapTypeModifiers: C->getMapTypeModifiers(), MapTypeModifiersLoc: C->getMapTypeModifiersLoc(),
6123 MapperIdScopeSpec, MapperId, MapType: C->getMapType(),
6124 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6125 VarList: SubExprs, Locs: OMPVarListLocTy()))
6126 Clauses.push_back(Elt: NewClause);
6127 }
6128}
6129
6130namespace {
6131/// A 'teams loop' with a nested 'loop bind(parallel)' or generic function
6132/// call in the associated loop-nest cannot be a 'parallel for'.
6133class TeamsLoopChecker final : public ConstStmtVisitor<TeamsLoopChecker> {
6134 Sema &SemaRef;
6135
6136public:
6137 bool teamsLoopCanBeParallelFor() const { return TeamsLoopCanBeParallelFor; }
6138
6139 // Is there a nested OpenMP loop bind(parallel)
6140 void VisitOMPExecutableDirective(const OMPExecutableDirective *D) {
6141 if (D->getDirectiveKind() == llvm::omp::Directive::OMPD_loop) {
6142 if (const auto *C = D->getSingleClause<OMPBindClause>())
6143 if (C->getBindKind() == OMPC_BIND_parallel) {
6144 TeamsLoopCanBeParallelFor = false;
6145 // No need to continue visiting any more
6146 return;
6147 }
6148 }
6149 for (const Stmt *Child : D->children())
6150 if (Child)
6151 Visit(S: Child);
6152 }
6153
6154 void VisitCallExpr(const CallExpr *C) {
6155 // Function calls inhibit parallel loop translation of 'target teams loop'
6156 // unless the assume-no-nested-parallelism flag has been specified.
6157 // OpenMP API runtime library calls do not inhibit parallel loop
6158 // translation, regardless of the assume-no-nested-parallelism.
6159 bool IsOpenMPAPI = false;
6160 auto *FD = dyn_cast_or_null<FunctionDecl>(Val: C->getCalleeDecl());
6161 if (FD) {
6162 std::string Name = FD->getNameInfo().getAsString();
6163 IsOpenMPAPI = Name.find(s: "omp_") == 0;
6164 }
6165 TeamsLoopCanBeParallelFor =
6166 IsOpenMPAPI || SemaRef.getLangOpts().OpenMPNoNestedParallelism;
6167 if (!TeamsLoopCanBeParallelFor)
6168 return;
6169
6170 for (const Stmt *Child : C->children())
6171 if (Child)
6172 Visit(S: Child);
6173 }
6174
6175 void VisitCapturedStmt(const CapturedStmt *S) {
6176 if (!S)
6177 return;
6178 Visit(S: S->getCapturedDecl()->getBody());
6179 }
6180
6181 void VisitStmt(const Stmt *S) {
6182 if (!S)
6183 return;
6184 for (const Stmt *Child : S->children())
6185 if (Child)
6186 Visit(S: Child);
6187 }
6188 explicit TeamsLoopChecker(Sema &SemaRef)
6189 : SemaRef(SemaRef), TeamsLoopCanBeParallelFor(true) {}
6190
6191private:
6192 bool TeamsLoopCanBeParallelFor;
6193};
6194} // namespace
6195
6196static bool teamsLoopCanBeParallelFor(Stmt *AStmt, Sema &SemaRef) {
6197 TeamsLoopChecker Checker(SemaRef);
6198 Checker.Visit(S: AStmt);
6199 return Checker.teamsLoopCanBeParallelFor();
6200}
6201
6202StmtResult SemaOpenMP::ActOnOpenMPExecutableDirective(
6203 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
6204 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
6205 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
6206 assert(isOpenMPExecutableDirective(Kind) && "Unexpected directive category");
6207
6208 StmtResult Res = StmtError();
6209 OpenMPBindClauseKind BindKind = OMPC_BIND_unknown;
6210 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
6211
6212 if (const OMPBindClause *BC =
6213 OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses))
6214 BindKind = BC->getBindKind();
6215
6216 if (Kind == OMPD_loop && BindKind == OMPC_BIND_unknown) {
6217 const OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
6218
6219 // Setting the enclosing teams or parallel construct for the loop
6220 // directive without bind clause.
6221 // [5.0:129:25-28] If the bind clause is not present on the construct and
6222 // the loop construct is closely nested inside a teams or parallel
6223 // construct, the binding region is the corresponding teams or parallel
6224 // region. If none of those conditions hold, the binding region is not
6225 // defined.
6226 BindKind = OMPC_BIND_thread; // Default bind(thread) if binding is unknown
6227 ArrayRef<OpenMPDirectiveKind> ParentLeafs =
6228 getLeafConstructsOrSelf(D: ParentDirective);
6229
6230 if (ParentDirective == OMPD_unknown) {
6231 Diag(DSAStack->getDefaultDSALocation(),
6232 DiagID: diag::err_omp_bind_required_on_loop);
6233 } else if (ParentLeafs.back() == OMPD_parallel) {
6234 BindKind = OMPC_BIND_parallel;
6235 } else if (ParentLeafs.back() == OMPD_teams) {
6236 BindKind = OMPC_BIND_teams;
6237 }
6238
6239 assert(BindKind != OMPC_BIND_unknown && "Expecting BindKind");
6240
6241 OMPClause *C =
6242 ActOnOpenMPBindClause(Kind: BindKind, KindLoc: SourceLocation(), StartLoc: SourceLocation(),
6243 LParenLoc: SourceLocation(), EndLoc: SourceLocation());
6244 ClausesWithImplicit.push_back(Elt: C);
6245 }
6246
6247 // Diagnose "loop bind(teams)" with "reduction".
6248 if (Kind == OMPD_loop && BindKind == OMPC_BIND_teams) {
6249 for (OMPClause *C : Clauses) {
6250 if (C->getClauseKind() == OMPC_reduction)
6251 Diag(DSAStack->getDefaultDSALocation(),
6252 DiagID: diag::err_omp_loop_reduction_clause);
6253 }
6254 }
6255
6256 // First check CancelRegion which is then used in checkNestingOfRegions.
6257 if (checkCancelRegion(SemaRef, CurrentRegion: Kind, CancelRegion, StartLoc) ||
6258 checkNestingOfRegions(SemaRef, DSAStack, CurrentRegion: Kind, CurrentName: DirName, CancelRegion,
6259 BindKind, StartLoc)) {
6260 return StmtError();
6261 }
6262
6263 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
6264 if (getLangOpts().HIP && (isOpenMPTargetExecutionDirective(DKind: Kind) ||
6265 isOpenMPTargetDataManagementDirective(DKind: Kind)))
6266 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
6267
6268 VarsWithInheritedDSAType VarsWithInheritedDSA;
6269 bool ErrorFound = false;
6270 ClausesWithImplicit.append(in_start: Clauses.begin(), in_end: Clauses.end());
6271
6272 if (AStmt && !SemaRef.CurContext->isDependentContext() &&
6273 isOpenMPCapturingDirective(DKind: Kind)) {
6274 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6275
6276 // Check default data sharing attributes for referenced variables.
6277 DSAAttrChecker DSAChecker(DSAStack, SemaRef, cast<CapturedStmt>(Val: AStmt));
6278 int ThisCaptureLevel = getOpenMPCaptureLevels(DKind: Kind);
6279 Stmt *S = AStmt;
6280 while (--ThisCaptureLevel >= 0)
6281 S = cast<CapturedStmt>(Val: S)->getCapturedStmt();
6282 DSAChecker.Visit(S);
6283 if (!isOpenMPTargetDataManagementDirective(DKind: Kind) &&
6284 !isOpenMPTaskingDirective(Kind)) {
6285 // Visit subcaptures to generate implicit clauses for captured vars.
6286 auto *CS = cast<CapturedStmt>(Val: AStmt);
6287 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
6288 getOpenMPCaptureRegions(CaptureRegions, DKind: Kind);
6289 // Ignore outer tasking regions for target directives.
6290 if (CaptureRegions.size() > 1 && CaptureRegions.front() == OMPD_task)
6291 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
6292 DSAChecker.visitSubCaptures(S: CS);
6293 }
6294 if (DSAChecker.isErrorFound())
6295 return StmtError();
6296 // Generate list of implicitly defined firstprivate variables.
6297 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
6298 VariableImplicitInfo ImpInfo = DSAChecker.getImplicitInfo();
6299
6300 SmallVector<SourceLocation, NumberOfOMPMapClauseModifiers>
6301 ImplicitMapModifiersLoc[VariableImplicitInfo::DefaultmapKindNum];
6302 // Get the original location of present modifier from Defaultmap clause.
6303 SourceLocation PresentModifierLocs[VariableImplicitInfo::DefaultmapKindNum];
6304 for (OMPClause *C : Clauses) {
6305 if (auto *DMC = dyn_cast<OMPDefaultmapClause>(Val: C))
6306 if (DMC->getDefaultmapModifier() == OMPC_DEFAULTMAP_MODIFIER_present)
6307 PresentModifierLocs[DMC->getDefaultmapKind()] =
6308 DMC->getDefaultmapModifierLoc();
6309 }
6310
6311 for (OpenMPDefaultmapClauseKind K :
6312 llvm::enum_seq_inclusive<OpenMPDefaultmapClauseKind>(
6313 Begin: OpenMPDefaultmapClauseKind(), End: OMPC_DEFAULTMAP_unknown)) {
6314 std::fill_n(first: std::back_inserter(x&: ImplicitMapModifiersLoc[K]),
6315 n: ImpInfo.MapModifiers[K].size(), value: PresentModifierLocs[K]);
6316 }
6317 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
6318 for (OMPClause *C : Clauses) {
6319 if (auto *IRC = dyn_cast<OMPInReductionClause>(Val: C)) {
6320 for (Expr *E : IRC->taskgroup_descriptors())
6321 if (E)
6322 ImpInfo.Firstprivates.insert(X: E);
6323 }
6324 // OpenMP 5.0, 2.10.1 task Construct
6325 // [detach clause]... The event-handle will be considered as if it was
6326 // specified on a firstprivate clause.
6327 if (auto *DC = dyn_cast<OMPDetachClause>(Val: C))
6328 ImpInfo.Firstprivates.insert(X: DC->getEventHandler());
6329 }
6330 if (!ImpInfo.Firstprivates.empty()) {
6331 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
6332 VarList: ImpInfo.Firstprivates.getArrayRef(), StartLoc: SourceLocation(),
6333 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6334 ClausesWithImplicit.push_back(Elt: Implicit);
6335 ErrorFound = cast<OMPFirstprivateClause>(Val: Implicit)->varlist_size() !=
6336 ImpInfo.Firstprivates.size();
6337 } else {
6338 ErrorFound = true;
6339 }
6340 }
6341 if (!ImpInfo.Privates.empty()) {
6342 if (OMPClause *Implicit = ActOnOpenMPPrivateClause(
6343 VarList: ImpInfo.Privates.getArrayRef(), StartLoc: SourceLocation(),
6344 LParenLoc: SourceLocation(), EndLoc: SourceLocation())) {
6345 ClausesWithImplicit.push_back(Elt: Implicit);
6346 ErrorFound = cast<OMPPrivateClause>(Val: Implicit)->varlist_size() !=
6347 ImpInfo.Privates.size();
6348 } else {
6349 ErrorFound = true;
6350 }
6351 }
6352 // OpenMP 5.0 [2.19.7]
6353 // If a list item appears in a reduction, lastprivate or linear
6354 // clause on a combined target construct then it is treated as
6355 // if it also appears in a map clause with a map-type of tofrom
6356 if (getLangOpts().OpenMP >= 50 && Kind != OMPD_target &&
6357 isOpenMPTargetExecutionDirective(DKind: Kind)) {
6358 SmallVector<Expr *, 4> ImplicitExprs;
6359 for (OMPClause *C : Clauses) {
6360 if (auto *RC = dyn_cast<OMPReductionClause>(Val: C))
6361 for (Expr *E : RC->varlist())
6362 if (!isa<DeclRefExpr>(Val: E->IgnoreParenImpCasts()))
6363 ImplicitExprs.emplace_back(Args&: E);
6364 }
6365 if (!ImplicitExprs.empty()) {
6366 ArrayRef<Expr *> Exprs = ImplicitExprs;
6367 CXXScopeSpec MapperIdScopeSpec;
6368 DeclarationNameInfo MapperId;
6369 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6370 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(),
6371 MapperIdScopeSpec, MapperId, MapType: OMPC_MAP_tofrom,
6372 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(),
6373 VarList: Exprs, Locs: OMPVarListLocTy(), /*NoDiagnose=*/true))
6374 ClausesWithImplicit.emplace_back(Args&: Implicit);
6375 }
6376 }
6377 for (unsigned I = 0; I < VariableImplicitInfo::DefaultmapKindNum; ++I) {
6378 int ClauseKindCnt = -1;
6379 for (unsigned J = 0; J < VariableImplicitInfo::MapKindNum; ++J) {
6380 ArrayRef<Expr *> ImplicitMap = ImpInfo.Mappings[I][J].getArrayRef();
6381 ++ClauseKindCnt;
6382 if (ImplicitMap.empty())
6383 continue;
6384 CXXScopeSpec MapperIdScopeSpec;
6385 DeclarationNameInfo MapperId;
6386 auto K = static_cast<OpenMPMapClauseKind>(ClauseKindCnt);
6387 if (OMPClause *Implicit = ActOnOpenMPMapClause(
6388 IteratorModifier: nullptr, MapTypeModifiers: ImpInfo.MapModifiers[I], MapTypeModifiersLoc: ImplicitMapModifiersLoc[I],
6389 MapperIdScopeSpec, MapperId, MapType: K, /*IsMapTypeImplicit=*/true,
6390 MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: ImplicitMap,
6391 Locs: OMPVarListLocTy())) {
6392 ClausesWithImplicit.emplace_back(Args&: Implicit);
6393 ErrorFound |= cast<OMPMapClause>(Val: Implicit)->varlist_size() !=
6394 ImplicitMap.size();
6395 } else {
6396 ErrorFound = true;
6397 }
6398 }
6399 }
6400 // Build expressions for implicit maps of data members with 'default'
6401 // mappers.
6402 if (getLangOpts().OpenMP >= 50)
6403 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
6404 Clauses&: ClausesWithImplicit);
6405 }
6406
6407 switch (Kind) {
6408 case OMPD_parallel:
6409 Res = ActOnOpenMPParallelDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6410 EndLoc);
6411 break;
6412 case OMPD_simd:
6413 Res = ActOnOpenMPSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6414 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6415 break;
6416 case OMPD_tile:
6417 Res =
6418 ActOnOpenMPTileDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6419 break;
6420 case OMPD_stripe:
6421 Res = ActOnOpenMPStripeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6422 EndLoc);
6423 break;
6424 case OMPD_unroll:
6425 Res = ActOnOpenMPUnrollDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6426 EndLoc);
6427 break;
6428 case OMPD_reverse:
6429 assert(ClausesWithImplicit.empty() &&
6430 "reverse directive does not support any clauses");
6431 Res = ActOnOpenMPReverseDirective(AStmt, StartLoc, EndLoc);
6432 break;
6433 case OMPD_interchange:
6434 Res = ActOnOpenMPInterchangeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6435 EndLoc);
6436 break;
6437 case OMPD_fuse:
6438 Res =
6439 ActOnOpenMPFuseDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6440 break;
6441 case OMPD_for:
6442 Res = ActOnOpenMPForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc,
6443 VarsWithImplicitDSA&: VarsWithInheritedDSA);
6444 break;
6445 case OMPD_for_simd:
6446 Res = ActOnOpenMPForSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6447 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6448 break;
6449 case OMPD_sections:
6450 Res = ActOnOpenMPSectionsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6451 EndLoc);
6452 break;
6453 case OMPD_section:
6454 assert(ClausesWithImplicit.empty() &&
6455 "No clauses are allowed for 'omp section' directive");
6456 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
6457 break;
6458 case OMPD_single:
6459 Res = ActOnOpenMPSingleDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6460 EndLoc);
6461 break;
6462 case OMPD_master:
6463 assert(ClausesWithImplicit.empty() &&
6464 "No clauses are allowed for 'omp master' directive");
6465 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
6466 break;
6467 case OMPD_masked:
6468 Res = ActOnOpenMPMaskedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6469 EndLoc);
6470 break;
6471 case OMPD_critical:
6472 Res = ActOnOpenMPCriticalDirective(DirName, Clauses: ClausesWithImplicit, AStmt,
6473 StartLoc, EndLoc);
6474 break;
6475 case OMPD_parallel_for:
6476 Res = ActOnOpenMPParallelForDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6477 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6478 break;
6479 case OMPD_parallel_for_simd:
6480 Res = ActOnOpenMPParallelForSimdDirective(
6481 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6482 break;
6483 case OMPD_scope:
6484 Res =
6485 ActOnOpenMPScopeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6486 break;
6487 case OMPD_parallel_master:
6488 Res = ActOnOpenMPParallelMasterDirective(Clauses: ClausesWithImplicit, AStmt,
6489 StartLoc, EndLoc);
6490 break;
6491 case OMPD_parallel_masked:
6492 Res = ActOnOpenMPParallelMaskedDirective(Clauses: ClausesWithImplicit, AStmt,
6493 StartLoc, EndLoc);
6494 break;
6495 case OMPD_parallel_sections:
6496 Res = ActOnOpenMPParallelSectionsDirective(Clauses: ClausesWithImplicit, AStmt,
6497 StartLoc, EndLoc);
6498 break;
6499 case OMPD_task:
6500 Res =
6501 ActOnOpenMPTaskDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6502 break;
6503 case OMPD_taskyield:
6504 assert(ClausesWithImplicit.empty() &&
6505 "No clauses are allowed for 'omp taskyield' directive");
6506 assert(AStmt == nullptr &&
6507 "No associated statement allowed for 'omp taskyield' directive");
6508 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
6509 break;
6510 case OMPD_error:
6511 assert(AStmt == nullptr &&
6512 "No associated statement allowed for 'omp error' directive");
6513 Res = ActOnOpenMPErrorDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6514 break;
6515 case OMPD_barrier:
6516 assert(ClausesWithImplicit.empty() &&
6517 "No clauses are allowed for 'omp barrier' directive");
6518 assert(AStmt == nullptr &&
6519 "No associated statement allowed for 'omp barrier' directive");
6520 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
6521 break;
6522 case OMPD_taskwait:
6523 assert(AStmt == nullptr &&
6524 "No associated statement allowed for 'omp taskwait' directive");
6525 Res = ActOnOpenMPTaskwaitDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6526 break;
6527 case OMPD_taskgroup:
6528 Res = ActOnOpenMPTaskgroupDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6529 EndLoc);
6530 break;
6531 case OMPD_flush:
6532 assert(AStmt == nullptr &&
6533 "No associated statement allowed for 'omp flush' directive");
6534 Res = ActOnOpenMPFlushDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6535 break;
6536 case OMPD_depobj:
6537 assert(AStmt == nullptr &&
6538 "No associated statement allowed for 'omp depobj' directive");
6539 Res = ActOnOpenMPDepobjDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6540 break;
6541 case OMPD_scan:
6542 assert(AStmt == nullptr &&
6543 "No associated statement allowed for 'omp scan' directive");
6544 Res = ActOnOpenMPScanDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6545 break;
6546 case OMPD_ordered:
6547 Res = ActOnOpenMPOrderedDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6548 EndLoc);
6549 break;
6550 case OMPD_atomic:
6551 Res = ActOnOpenMPAtomicDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6552 EndLoc);
6553 break;
6554 case OMPD_teams:
6555 Res =
6556 ActOnOpenMPTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc);
6557 break;
6558 case OMPD_target:
6559 Res = ActOnOpenMPTargetDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6560 EndLoc);
6561 break;
6562 case OMPD_target_parallel:
6563 Res = ActOnOpenMPTargetParallelDirective(Clauses: ClausesWithImplicit, AStmt,
6564 StartLoc, EndLoc);
6565 break;
6566 case OMPD_target_parallel_for:
6567 Res = ActOnOpenMPTargetParallelForDirective(
6568 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6569 break;
6570 case OMPD_cancellation_point:
6571 assert(ClausesWithImplicit.empty() &&
6572 "No clauses are allowed for 'omp cancellation point' directive");
6573 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
6574 "cancellation point' directive");
6575 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
6576 break;
6577 case OMPD_cancel:
6578 assert(AStmt == nullptr &&
6579 "No associated statement allowed for 'omp cancel' directive");
6580 Res = ActOnOpenMPCancelDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc,
6581 CancelRegion);
6582 break;
6583 case OMPD_target_data:
6584 Res = ActOnOpenMPTargetDataDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6585 EndLoc);
6586 break;
6587 case OMPD_target_enter_data:
6588 Res = ActOnOpenMPTargetEnterDataDirective(Clauses: ClausesWithImplicit, StartLoc,
6589 EndLoc, AStmt);
6590 break;
6591 case OMPD_target_exit_data:
6592 Res = ActOnOpenMPTargetExitDataDirective(Clauses: ClausesWithImplicit, StartLoc,
6593 EndLoc, AStmt);
6594 break;
6595 case OMPD_taskloop:
6596 Res = ActOnOpenMPTaskLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6597 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6598 break;
6599 case OMPD_taskloop_simd:
6600 Res = ActOnOpenMPTaskLoopSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6601 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6602 break;
6603 case OMPD_master_taskloop:
6604 Res = ActOnOpenMPMasterTaskLoopDirective(
6605 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6606 break;
6607 case OMPD_masked_taskloop:
6608 Res = ActOnOpenMPMaskedTaskLoopDirective(
6609 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6610 break;
6611 case OMPD_master_taskloop_simd:
6612 Res = ActOnOpenMPMasterTaskLoopSimdDirective(
6613 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6614 break;
6615 case OMPD_masked_taskloop_simd:
6616 Res = ActOnOpenMPMaskedTaskLoopSimdDirective(
6617 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6618 break;
6619 case OMPD_parallel_master_taskloop:
6620 Res = ActOnOpenMPParallelMasterTaskLoopDirective(
6621 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6622 break;
6623 case OMPD_parallel_masked_taskloop:
6624 Res = ActOnOpenMPParallelMaskedTaskLoopDirective(
6625 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6626 break;
6627 case OMPD_parallel_master_taskloop_simd:
6628 Res = ActOnOpenMPParallelMasterTaskLoopSimdDirective(
6629 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6630 break;
6631 case OMPD_parallel_masked_taskloop_simd:
6632 Res = ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
6633 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6634 break;
6635 case OMPD_distribute:
6636 Res = ActOnOpenMPDistributeDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6637 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6638 break;
6639 case OMPD_target_update:
6640 Res = ActOnOpenMPTargetUpdateDirective(Clauses: ClausesWithImplicit, StartLoc,
6641 EndLoc, AStmt);
6642 break;
6643 case OMPD_distribute_parallel_for:
6644 Res = ActOnOpenMPDistributeParallelForDirective(
6645 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6646 break;
6647 case OMPD_distribute_parallel_for_simd:
6648 Res = ActOnOpenMPDistributeParallelForSimdDirective(
6649 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6650 break;
6651 case OMPD_distribute_simd:
6652 Res = ActOnOpenMPDistributeSimdDirective(
6653 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6654 break;
6655 case OMPD_target_parallel_for_simd:
6656 Res = ActOnOpenMPTargetParallelForSimdDirective(
6657 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6658 break;
6659 case OMPD_target_simd:
6660 Res = ActOnOpenMPTargetSimdDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6661 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6662 break;
6663 case OMPD_teams_distribute:
6664 Res = ActOnOpenMPTeamsDistributeDirective(
6665 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6666 break;
6667 case OMPD_teams_distribute_simd:
6668 Res = ActOnOpenMPTeamsDistributeSimdDirective(
6669 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6670 break;
6671 case OMPD_teams_distribute_parallel_for_simd:
6672 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6673 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6674 break;
6675 case OMPD_teams_distribute_parallel_for:
6676 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
6677 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6678 break;
6679 case OMPD_target_teams:
6680 Res = ActOnOpenMPTargetTeamsDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6681 EndLoc);
6682 break;
6683 case OMPD_target_teams_distribute:
6684 Res = ActOnOpenMPTargetTeamsDistributeDirective(
6685 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6686 break;
6687 case OMPD_target_teams_distribute_parallel_for:
6688 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6689 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6690 break;
6691 case OMPD_target_teams_distribute_parallel_for_simd:
6692 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6693 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6694 break;
6695 case OMPD_target_teams_distribute_simd:
6696 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
6697 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6698 break;
6699 case OMPD_interop:
6700 assert(AStmt == nullptr &&
6701 "No associated statement allowed for 'omp interop' directive");
6702 Res = ActOnOpenMPInteropDirective(Clauses: ClausesWithImplicit, StartLoc, EndLoc);
6703 break;
6704 case OMPD_dispatch:
6705 Res = ActOnOpenMPDispatchDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6706 EndLoc);
6707 break;
6708 case OMPD_loop:
6709 Res = ActOnOpenMPGenericLoopDirective(Clauses: ClausesWithImplicit, AStmt, StartLoc,
6710 EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6711 break;
6712 case OMPD_teams_loop:
6713 Res = ActOnOpenMPTeamsGenericLoopDirective(
6714 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6715 break;
6716 case OMPD_target_teams_loop:
6717 Res = ActOnOpenMPTargetTeamsGenericLoopDirective(
6718 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6719 break;
6720 case OMPD_parallel_loop:
6721 Res = ActOnOpenMPParallelGenericLoopDirective(
6722 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6723 break;
6724 case OMPD_target_parallel_loop:
6725 Res = ActOnOpenMPTargetParallelGenericLoopDirective(
6726 Clauses: ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithImplicitDSA&: VarsWithInheritedDSA);
6727 break;
6728 case OMPD_declare_target:
6729 case OMPD_end_declare_target:
6730 case OMPD_threadprivate:
6731 case OMPD_allocate:
6732 case OMPD_declare_reduction:
6733 case OMPD_declare_mapper:
6734 case OMPD_declare_simd:
6735 case OMPD_requires:
6736 case OMPD_declare_variant:
6737 case OMPD_begin_declare_variant:
6738 case OMPD_end_declare_variant:
6739 llvm_unreachable("OpenMP Directive is not allowed");
6740 case OMPD_unknown:
6741 default:
6742 llvm_unreachable("Unknown OpenMP directive");
6743 }
6744
6745 ErrorFound = Res.isInvalid() || ErrorFound;
6746
6747 // Check variables in the clauses if default(none) or
6748 // default(firstprivate) was specified.
6749 if (DSAStack->getDefaultDSA() == DSA_none ||
6750 DSAStack->getDefaultDSA() == DSA_private ||
6751 DSAStack->getDefaultDSA() == DSA_firstprivate) {
6752 DSAAttrChecker DSAChecker(DSAStack, SemaRef, nullptr);
6753 for (OMPClause *C : Clauses) {
6754 switch (C->getClauseKind()) {
6755 case OMPC_num_threads:
6756 case OMPC_dist_schedule:
6757 // Do not analyze if no parent teams directive.
6758 if (isOpenMPTeamsDirective(DKind: Kind))
6759 break;
6760 continue;
6761 case OMPC_if:
6762 if (isOpenMPTeamsDirective(DKind: Kind) &&
6763 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_target)
6764 break;
6765 if (isOpenMPParallelDirective(DKind: Kind) &&
6766 isOpenMPTaskLoopDirective(DKind: Kind) &&
6767 cast<OMPIfClause>(Val: C)->getNameModifier() != OMPD_parallel)
6768 break;
6769 continue;
6770 case OMPC_schedule:
6771 case OMPC_detach:
6772 break;
6773 case OMPC_grainsize:
6774 case OMPC_num_tasks:
6775 case OMPC_final:
6776 case OMPC_priority:
6777 case OMPC_novariants:
6778 case OMPC_nocontext:
6779 // Do not analyze if no parent parallel directive.
6780 if (isOpenMPParallelDirective(DKind: Kind))
6781 break;
6782 continue;
6783 case OMPC_ordered:
6784 case OMPC_device:
6785 case OMPC_num_teams:
6786 case OMPC_thread_limit:
6787 case OMPC_hint:
6788 case OMPC_collapse:
6789 case OMPC_safelen:
6790 case OMPC_simdlen:
6791 case OMPC_sizes:
6792 case OMPC_default:
6793 case OMPC_proc_bind:
6794 case OMPC_private:
6795 case OMPC_firstprivate:
6796 case OMPC_lastprivate:
6797 case OMPC_shared:
6798 case OMPC_reduction:
6799 case OMPC_task_reduction:
6800 case OMPC_in_reduction:
6801 case OMPC_linear:
6802 case OMPC_aligned:
6803 case OMPC_copyin:
6804 case OMPC_copyprivate:
6805 case OMPC_nowait:
6806 case OMPC_untied:
6807 case OMPC_mergeable:
6808 case OMPC_allocate:
6809 case OMPC_read:
6810 case OMPC_write:
6811 case OMPC_update:
6812 case OMPC_capture:
6813 case OMPC_compare:
6814 case OMPC_seq_cst:
6815 case OMPC_acq_rel:
6816 case OMPC_acquire:
6817 case OMPC_release:
6818 case OMPC_relaxed:
6819 case OMPC_depend:
6820 case OMPC_threads:
6821 case OMPC_simd:
6822 case OMPC_map:
6823 case OMPC_nogroup:
6824 case OMPC_defaultmap:
6825 case OMPC_to:
6826 case OMPC_from:
6827 case OMPC_use_device_ptr:
6828 case OMPC_use_device_addr:
6829 case OMPC_is_device_ptr:
6830 case OMPC_has_device_addr:
6831 case OMPC_nontemporal:
6832 case OMPC_order:
6833 case OMPC_destroy:
6834 case OMPC_inclusive:
6835 case OMPC_exclusive:
6836 case OMPC_uses_allocators:
6837 case OMPC_affinity:
6838 case OMPC_bind:
6839 case OMPC_filter:
6840 case OMPC_severity:
6841 case OMPC_message:
6842 continue;
6843 case OMPC_allocator:
6844 case OMPC_flush:
6845 case OMPC_depobj:
6846 case OMPC_threadprivate:
6847 case OMPC_groupprivate:
6848 case OMPC_uniform:
6849 case OMPC_unknown:
6850 case OMPC_unified_address:
6851 case OMPC_unified_shared_memory:
6852 case OMPC_reverse_offload:
6853 case OMPC_dynamic_allocators:
6854 case OMPC_atomic_default_mem_order:
6855 case OMPC_self_maps:
6856 case OMPC_device_type:
6857 case OMPC_match:
6858 case OMPC_when:
6859 case OMPC_at:
6860 default:
6861 llvm_unreachable("Unexpected clause");
6862 }
6863 for (Stmt *CC : C->children()) {
6864 if (CC)
6865 DSAChecker.Visit(S: CC);
6866 }
6867 }
6868 for (const auto &P : DSAChecker.getVarsWithInheritedDSA())
6869 VarsWithInheritedDSA[P.getFirst()] = P.getSecond();
6870 }
6871 for (const auto &P : VarsWithInheritedDSA) {
6872 if (P.getFirst()->isImplicit() || isa<OMPCapturedExprDecl>(Val: P.getFirst()))
6873 continue;
6874 ErrorFound = true;
6875 if (DSAStack->getDefaultDSA() == DSA_none ||
6876 DSAStack->getDefaultDSA() == DSA_private ||
6877 DSAStack->getDefaultDSA() == DSA_firstprivate) {
6878 Diag(Loc: P.second->getExprLoc(), DiagID: diag::err_omp_no_dsa_for_variable)
6879 << P.first << P.second->getSourceRange();
6880 Diag(DSAStack->getDefaultDSALocation(), DiagID: diag::note_omp_default_dsa_none);
6881 } else if (getLangOpts().OpenMP >= 50) {
6882 Diag(Loc: P.second->getExprLoc(),
6883 DiagID: diag::err_omp_defaultmap_no_attr_for_variable)
6884 << P.first << P.second->getSourceRange();
6885 Diag(DSAStack->getDefaultDSALocation(),
6886 DiagID: diag::note_omp_defaultmap_attr_none);
6887 }
6888 }
6889
6890 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
6891 for (OpenMPDirectiveKind D : getLeafConstructsOrSelf(D: Kind)) {
6892 if (isAllowedClauseForDirective(D, C: OMPC_if, Version: getLangOpts().OpenMP))
6893 AllowedNameModifiers.push_back(Elt: D);
6894 }
6895 if (!AllowedNameModifiers.empty())
6896 ErrorFound = checkIfClauses(S&: SemaRef, Kind, Clauses, AllowedNameModifiers) ||
6897 ErrorFound;
6898
6899 if (ErrorFound)
6900 return StmtError();
6901
6902 if (!SemaRef.CurContext->isDependentContext() &&
6903 isOpenMPTargetExecutionDirective(DKind: Kind) &&
6904 !(DSAStack->hasRequiresDeclWithClause<OMPUnifiedSharedMemoryClause>() ||
6905 DSAStack->hasRequiresDeclWithClause<OMPUnifiedAddressClause>() ||
6906 DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>() ||
6907 DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())) {
6908 // Register target to DSA Stack.
6909 DSAStack->addTargetDirLocation(LocStart: StartLoc);
6910 }
6911
6912 return Res;
6913}
6914
6915SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareSimdDirective(
6916 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
6917 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
6918 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
6919 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
6920 assert(Aligneds.size() == Alignments.size());
6921 assert(Linears.size() == LinModifiers.size());
6922 assert(Linears.size() == Steps.size());
6923 if (!DG || DG.get().isNull())
6924 return DeclGroupPtrTy();
6925
6926 const int SimdId = 0;
6927 if (!DG.get().isSingleDecl()) {
6928 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
6929 << SimdId;
6930 return DG;
6931 }
6932 Decl *ADecl = DG.get().getSingleDecl();
6933 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
6934 ADecl = FTD->getTemplatedDecl();
6935
6936 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
6937 if (!FD) {
6938 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected) << SimdId;
6939 return DeclGroupPtrTy();
6940 }
6941
6942 // OpenMP [2.8.2, declare simd construct, Description]
6943 // The parameter of the simdlen clause must be a constant positive integer
6944 // expression.
6945 ExprResult SL;
6946 if (Simdlen)
6947 SL = VerifyPositiveIntegerConstantInClause(Op: Simdlen, CKind: OMPC_simdlen);
6948 // OpenMP [2.8.2, declare simd construct, Description]
6949 // The special this pointer can be used as if was one of the arguments to the
6950 // function in any of the linear, aligned, or uniform clauses.
6951 // The uniform clause declares one or more arguments to have an invariant
6952 // value for all concurrent invocations of the function in the execution of a
6953 // single SIMD loop.
6954 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
6955 const Expr *UniformedLinearThis = nullptr;
6956 for (const Expr *E : Uniforms) {
6957 E = E->IgnoreParenImpCasts();
6958 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
6959 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl()))
6960 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6961 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
6962 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
6963 UniformedArgs.try_emplace(Key: PVD->getCanonicalDecl(), Args&: E);
6964 continue;
6965 }
6966 if (isa<CXXThisExpr>(Val: E)) {
6967 UniformedLinearThis = E;
6968 continue;
6969 }
6970 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
6971 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
6972 }
6973 // OpenMP [2.8.2, declare simd construct, Description]
6974 // The aligned clause declares that the object to which each list item points
6975 // is aligned to the number of bytes expressed in the optional parameter of
6976 // the aligned clause.
6977 // The special this pointer can be used as if was one of the arguments to the
6978 // function in any of the linear, aligned, or uniform clauses.
6979 // The type of list items appearing in the aligned clause must be array,
6980 // pointer, reference to array, or reference to pointer.
6981 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
6982 const Expr *AlignedThis = nullptr;
6983 for (const Expr *E : Aligneds) {
6984 E = E->IgnoreParenImpCasts();
6985 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
6986 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
6987 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
6988 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
6989 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
6990 ->getCanonicalDecl() == CanonPVD) {
6991 // OpenMP [2.8.1, simd construct, Restrictions]
6992 // A list-item cannot appear in more than one aligned clause.
6993 auto [It, Inserted] = AlignedArgs.try_emplace(Key: CanonPVD, Args&: E);
6994 if (!Inserted) {
6995 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
6996 << 1 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
6997 << E->getSourceRange();
6998 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
6999 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7000 continue;
7001 }
7002 QualType QTy = PVD->getType()
7003 .getNonReferenceType()
7004 .getUnqualifiedType()
7005 .getCanonicalType();
7006 const Type *Ty = QTy.getTypePtrOrNull();
7007 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
7008 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_aligned_expected_array_or_ptr)
7009 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
7010 Diag(Loc: PVD->getLocation(), DiagID: diag::note_previous_decl) << PVD;
7011 }
7012 continue;
7013 }
7014 }
7015 if (isa<CXXThisExpr>(Val: E)) {
7016 if (AlignedThis) {
7017 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_used_in_clause_twice)
7018 << 2 << getOpenMPClauseNameForDiag(C: OMPC_aligned)
7019 << E->getSourceRange();
7020 Diag(Loc: AlignedThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7021 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
7022 }
7023 AlignedThis = E;
7024 continue;
7025 }
7026 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7027 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7028 }
7029 // The optional parameter of the aligned clause, alignment, must be a constant
7030 // positive integer expression. If no optional parameter is specified,
7031 // implementation-defined default alignments for SIMD instructions on the
7032 // target platforms are assumed.
7033 SmallVector<const Expr *, 4> NewAligns;
7034 for (Expr *E : Alignments) {
7035 ExprResult Align;
7036 if (E)
7037 Align = VerifyPositiveIntegerConstantInClause(Op: E, CKind: OMPC_aligned);
7038 NewAligns.push_back(Elt: Align.get());
7039 }
7040 // OpenMP [2.8.2, declare simd construct, Description]
7041 // The linear clause declares one or more list items to be private to a SIMD
7042 // lane and to have a linear relationship with respect to the iteration space
7043 // of a loop.
7044 // The special this pointer can be used as if was one of the arguments to the
7045 // function in any of the linear, aligned, or uniform clauses.
7046 // When a linear-step expression is specified in a linear clause it must be
7047 // either a constant integer expression or an integer-typed parameter that is
7048 // specified in a uniform clause on the directive.
7049 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
7050 const bool IsUniformedThis = UniformedLinearThis != nullptr;
7051 auto MI = LinModifiers.begin();
7052 for (const Expr *E : Linears) {
7053 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
7054 ++MI;
7055 E = E->IgnoreParenImpCasts();
7056 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E))
7057 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7058 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7059 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7060 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7061 ->getCanonicalDecl() == CanonPVD) {
7062 // OpenMP [2.15.3.7, linear Clause, Restrictions]
7063 // A list-item cannot appear in more than one linear clause.
7064 if (auto It = LinearArgs.find(Val: CanonPVD); It != LinearArgs.end()) {
7065 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7066 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7067 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7068 << E->getSourceRange();
7069 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7070 << getOpenMPClauseNameForDiag(C: OMPC_linear);
7071 continue;
7072 }
7073 // Each argument can appear in at most one uniform or linear clause.
7074 if (auto It = UniformedArgs.find(Val: CanonPVD);
7075 It != UniformedArgs.end()) {
7076 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7077 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7078 << getOpenMPClauseNameForDiag(C: OMPC_uniform)
7079 << E->getSourceRange();
7080 Diag(Loc: It->second->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7081 << getOpenMPClauseNameForDiag(C: OMPC_uniform);
7082 continue;
7083 }
7084 LinearArgs[CanonPVD] = E;
7085 if (E->isValueDependent() || E->isTypeDependent() ||
7086 E->isInstantiationDependent() ||
7087 E->containsUnexpandedParameterPack())
7088 continue;
7089 (void)CheckOpenMPLinearDecl(D: CanonPVD, ELoc: E->getExprLoc(), LinKind,
7090 Type: PVD->getOriginalType(),
7091 /*IsDeclareSimd=*/true);
7092 continue;
7093 }
7094 }
7095 if (isa<CXXThisExpr>(Val: E)) {
7096 if (UniformedLinearThis) {
7097 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
7098 << getOpenMPClauseNameForDiag(C: OMPC_linear)
7099 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7100 : OMPC_linear)
7101 << E->getSourceRange();
7102 Diag(Loc: UniformedLinearThis->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
7103 << getOpenMPClauseNameForDiag(C: IsUniformedThis ? OMPC_uniform
7104 : OMPC_linear);
7105 continue;
7106 }
7107 UniformedLinearThis = E;
7108 if (E->isValueDependent() || E->isTypeDependent() ||
7109 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7110 continue;
7111 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, ELoc: E->getExprLoc(), LinKind,
7112 Type: E->getType(), /*IsDeclareSimd=*/true);
7113 continue;
7114 }
7115 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause)
7116 << FD->getDeclName() << (isa<CXXMethodDecl>(Val: ADecl) ? 1 : 0);
7117 }
7118 Expr *Step = nullptr;
7119 Expr *NewStep = nullptr;
7120 SmallVector<Expr *, 4> NewSteps;
7121 for (Expr *E : Steps) {
7122 // Skip the same step expression, it was checked already.
7123 if (Step == E || !E) {
7124 NewSteps.push_back(Elt: E ? NewStep : nullptr);
7125 continue;
7126 }
7127 Step = E;
7128 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: Step))
7129 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7130 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7131 if (UniformedArgs.count(Val: CanonPVD) == 0) {
7132 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_uniform_param)
7133 << Step->getSourceRange();
7134 } else if (E->isValueDependent() || E->isTypeDependent() ||
7135 E->isInstantiationDependent() ||
7136 E->containsUnexpandedParameterPack() ||
7137 CanonPVD->getType()->hasIntegerRepresentation()) {
7138 NewSteps.push_back(Elt: Step);
7139 } else {
7140 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_expected_int_param)
7141 << Step->getSourceRange();
7142 }
7143 continue;
7144 }
7145 NewStep = Step;
7146 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7147 !Step->isInstantiationDependent() &&
7148 !Step->containsUnexpandedParameterPack()) {
7149 NewStep = PerformOpenMPImplicitIntegerConversion(OpLoc: Step->getExprLoc(), Op: Step)
7150 .get();
7151 if (NewStep)
7152 NewStep = SemaRef
7153 .VerifyIntegerConstantExpression(
7154 E: NewStep, /*FIXME*/ CanFold: AllowFoldKind::Allow)
7155 .get();
7156 }
7157 NewSteps.push_back(Elt: NewStep);
7158 }
7159 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
7160 Ctx&: getASTContext(), BranchState: BS, Simdlen: SL.get(), Uniforms: const_cast<Expr **>(Uniforms.data()),
7161 UniformsSize: Uniforms.size(), Aligneds: const_cast<Expr **>(Aligneds.data()), AlignedsSize: Aligneds.size(),
7162 Alignments: const_cast<Expr **>(NewAligns.data()), AlignmentsSize: NewAligns.size(),
7163 Linears: const_cast<Expr **>(Linears.data()), LinearsSize: Linears.size(),
7164 Modifiers: const_cast<unsigned *>(LinModifiers.data()), ModifiersSize: LinModifiers.size(),
7165 Steps: NewSteps.data(), StepsSize: NewSteps.size(), Range: SR);
7166 ADecl->addAttr(A: NewAttr);
7167 return DG;
7168}
7169
7170StmtResult SemaOpenMP::ActOnOpenMPInformationalDirective(
7171 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
7172 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7173 SourceLocation EndLoc) {
7174 assert(isOpenMPInformationalDirective(Kind) &&
7175 "Unexpected directive category");
7176
7177 StmtResult Res = StmtError();
7178
7179 switch (Kind) {
7180 case OMPD_assume:
7181 Res = ActOnOpenMPAssumeDirective(Clauses, AStmt, StartLoc, EndLoc);
7182 break;
7183 default:
7184 llvm_unreachable("Unknown OpenMP directive");
7185 }
7186
7187 return Res;
7188}
7189
7190static void setPrototype(Sema &S, FunctionDecl *FD, FunctionDecl *FDWithProto,
7191 QualType NewType) {
7192 assert(NewType->isFunctionProtoType() &&
7193 "Expected function type with prototype.");
7194 assert(FD->getType()->isFunctionNoProtoType() &&
7195 "Expected function with type with no prototype.");
7196 assert(FDWithProto->getType()->isFunctionProtoType() &&
7197 "Expected function with prototype.");
7198 // Synthesize parameters with the same types.
7199 FD->setType(NewType);
7200 SmallVector<ParmVarDecl *, 16> Params;
7201 for (const ParmVarDecl *P : FDWithProto->parameters()) {
7202 auto *Param = ParmVarDecl::Create(C&: S.getASTContext(), DC: FD, StartLoc: SourceLocation(),
7203 IdLoc: SourceLocation(), Id: nullptr, T: P->getType(),
7204 /*TInfo=*/nullptr, S: SC_None, DefArg: nullptr);
7205 Param->setScopeInfo(scopeDepth: 0, parameterIndex: Params.size());
7206 Param->setImplicit();
7207 Params.push_back(Elt: Param);
7208 }
7209
7210 FD->setParams(Params);
7211}
7212
7213void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D) {
7214 if (D->isInvalidDecl())
7215 return;
7216 FunctionDecl *FD = nullptr;
7217 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7218 FD = UTemplDecl->getTemplatedDecl();
7219 else
7220 FD = cast<FunctionDecl>(Val: D);
7221 assert(FD && "Expected a function declaration!");
7222
7223 // If we are instantiating templates we do *not* apply scoped assumptions but
7224 // only global ones. We apply scoped assumption to the template definition
7225 // though.
7226 if (!SemaRef.inTemplateInstantiation()) {
7227 for (OMPAssumeAttr *AA : OMPAssumeScoped)
7228 FD->addAttr(A: AA);
7229 }
7230 for (OMPAssumeAttr *AA : OMPAssumeGlobal)
7231 FD->addAttr(A: AA);
7232}
7233
7234SemaOpenMP::OMPDeclareVariantScope::OMPDeclareVariantScope(OMPTraitInfo &TI)
7235 : TI(&TI), NameSuffix(TI.getMangledName()) {}
7236
7237void SemaOpenMP::ActOnStartOfFunctionDefinitionInOpenMPDeclareVariantScope(
7238 Scope *S, Declarator &D, MultiTemplateParamsArg TemplateParamLists,
7239 SmallVectorImpl<FunctionDecl *> &Bases) {
7240 if (!D.getIdentifier())
7241 return;
7242
7243 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7244
7245 // Template specialization is an extension, check if we do it.
7246 bool IsTemplated = !TemplateParamLists.empty();
7247 if (IsTemplated &&
7248 !DVScope.TI->isExtensionActive(
7249 TP: llvm::omp::TraitProperty::implementation_extension_allow_templates))
7250 return;
7251
7252 const IdentifierInfo *BaseII = D.getIdentifier();
7253 LookupResult Lookup(SemaRef, DeclarationName(BaseII), D.getIdentifierLoc(),
7254 Sema::LookupOrdinaryName);
7255 SemaRef.LookupParsedName(R&: Lookup, S, SS: &D.getCXXScopeSpec(),
7256 /*ObjectType=*/QualType());
7257
7258 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
7259 QualType FType = TInfo->getType();
7260
7261 bool IsConstexpr =
7262 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Constexpr;
7263 bool IsConsteval =
7264 D.getDeclSpec().getConstexprSpecifier() == ConstexprSpecKind::Consteval;
7265
7266 for (auto *Candidate : Lookup) {
7267 auto *CandidateDecl = Candidate->getUnderlyingDecl();
7268 FunctionDecl *UDecl = nullptr;
7269 if (IsTemplated && isa<FunctionTemplateDecl>(Val: CandidateDecl)) {
7270 auto *FTD = cast<FunctionTemplateDecl>(Val: CandidateDecl);
7271 // FIXME: Should this compare the template parameter lists on all levels?
7272 if (SemaRef.Context.isSameTemplateParameterList(
7273 X: FTD->getTemplateParameters(), Y: TemplateParamLists.back()))
7274 UDecl = FTD->getTemplatedDecl();
7275 } else if (!IsTemplated)
7276 UDecl = dyn_cast<FunctionDecl>(Val: CandidateDecl);
7277 if (!UDecl)
7278 continue;
7279
7280 // Don't specialize constexpr/consteval functions with
7281 // non-constexpr/consteval functions.
7282 if (UDecl->isConstexpr() && !IsConstexpr)
7283 continue;
7284 if (UDecl->isConsteval() && !IsConsteval)
7285 continue;
7286
7287 QualType UDeclTy = UDecl->getType();
7288 if (!UDeclTy->isDependentType()) {
7289 QualType NewType = getASTContext().mergeFunctionTypes(
7290 FType, UDeclTy, /*OfBlockPointer=*/false,
7291 /*Unqualified=*/false, /*AllowCXX=*/true);
7292 if (NewType.isNull())
7293 continue;
7294 }
7295
7296 // Found a base!
7297 Bases.push_back(Elt: UDecl);
7298 }
7299
7300 bool UseImplicitBase = !DVScope.TI->isExtensionActive(
7301 TP: llvm::omp::TraitProperty::implementation_extension_disable_implicit_base);
7302 // If no base was found we create a declaration that we use as base.
7303 if (Bases.empty() && UseImplicitBase) {
7304 D.setFunctionDefinitionKind(FunctionDefinitionKind::Declaration);
7305 Decl *BaseD = SemaRef.HandleDeclarator(S, D, TemplateParameterLists: TemplateParamLists);
7306 BaseD->setImplicit(true);
7307 if (auto *BaseTemplD = dyn_cast<FunctionTemplateDecl>(Val: BaseD))
7308 Bases.push_back(Elt: BaseTemplD->getTemplatedDecl());
7309 else
7310 Bases.push_back(Elt: cast<FunctionDecl>(Val: BaseD));
7311 }
7312
7313 std::string MangledName;
7314 MangledName += D.getIdentifier()->getName();
7315 MangledName += getOpenMPVariantManglingSeparatorStr();
7316 MangledName += DVScope.NameSuffix;
7317 IdentifierInfo &VariantII = getASTContext().Idents.get(Name: MangledName);
7318
7319 VariantII.setMangledOpenMPVariantName(true);
7320 D.SetIdentifier(Id: &VariantII, IdLoc: D.getBeginLoc());
7321}
7322
7323void SemaOpenMP::ActOnFinishedFunctionDefinitionInOpenMPDeclareVariantScope(
7324 Decl *D, SmallVectorImpl<FunctionDecl *> &Bases) {
7325 // Do not mark function as is used to prevent its emission if this is the
7326 // only place where it is used.
7327 EnterExpressionEvaluationContext Unevaluated(
7328 SemaRef, Sema::ExpressionEvaluationContext::Unevaluated);
7329
7330 FunctionDecl *FD = nullptr;
7331 if (auto *UTemplDecl = dyn_cast<FunctionTemplateDecl>(Val: D))
7332 FD = UTemplDecl->getTemplatedDecl();
7333 else
7334 FD = cast<FunctionDecl>(Val: D);
7335 auto *VariantFuncRef = DeclRefExpr::Create(
7336 Context: getASTContext(), QualifierLoc: NestedNameSpecifierLoc(), TemplateKWLoc: SourceLocation(), D: FD,
7337 /*RefersToEnclosingVariableOrCapture=*/false,
7338 /*NameLoc=*/FD->getLocation(), T: FD->getType(), VK: ExprValueKind::VK_PRValue);
7339
7340 OMPDeclareVariantScope &DVScope = OMPDeclareVariantScopes.back();
7341 auto *OMPDeclareVariantA = OMPDeclareVariantAttr::CreateImplicit(
7342 Ctx&: getASTContext(), VariantFuncRef, TraitInfos: DVScope.TI,
7343 /*NothingArgs=*/AdjustArgsNothing: nullptr, /*NothingArgsSize=*/AdjustArgsNothingSize: 0,
7344 /*NeedDevicePtrArgs=*/AdjustArgsNeedDevicePtr: nullptr, /*NeedDevicePtrArgsSize=*/AdjustArgsNeedDevicePtrSize: 0,
7345 /*NeedDeviceAddrArgs=*/AdjustArgsNeedDeviceAddr: nullptr, /*NeedDeviceAddrArgsSize=*/AdjustArgsNeedDeviceAddrSize: 0,
7346 /*AppendArgs=*/nullptr, /*AppendArgsSize=*/0);
7347 for (FunctionDecl *BaseFD : Bases)
7348 BaseFD->addAttr(A: OMPDeclareVariantA);
7349}
7350
7351ExprResult SemaOpenMP::ActOnOpenMPCall(ExprResult Call, Scope *Scope,
7352 SourceLocation LParenLoc,
7353 MultiExprArg ArgExprs,
7354 SourceLocation RParenLoc,
7355 Expr *ExecConfig) {
7356 // The common case is a regular call we do not want to specialize at all. Try
7357 // to make that case fast by bailing early.
7358 CallExpr *CE = dyn_cast<CallExpr>(Val: Call.get());
7359 if (!CE)
7360 return Call;
7361
7362 FunctionDecl *CalleeFnDecl = CE->getDirectCallee();
7363 if (!CalleeFnDecl)
7364 return Call;
7365
7366 if (getLangOpts().OpenMP >= 50 && getLangOpts().OpenMP <= 60 &&
7367 CalleeFnDecl->getIdentifier() &&
7368 CalleeFnDecl->getName().starts_with_insensitive(Prefix: "omp_")) {
7369 // checking for any calls inside an Order region
7370 if (Scope && Scope->isOpenMPOrderClauseScope())
7371 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_call_to_omp_runtime_api);
7372 }
7373
7374 if (!CalleeFnDecl->hasAttr<OMPDeclareVariantAttr>())
7375 return Call;
7376
7377 ASTContext &Context = getASTContext();
7378 std::function<void(StringRef)> DiagUnknownTrait = [this,
7379 CE](StringRef ISATrait) {
7380 // TODO Track the selector locations in a way that is accessible here to
7381 // improve the diagnostic location.
7382 Diag(Loc: CE->getBeginLoc(), DiagID: diag::warn_unknown_declare_variant_isa_trait)
7383 << ISATrait;
7384 };
7385 TargetOMPContext OMPCtx(Context, std::move(DiagUnknownTrait),
7386 SemaRef.getCurFunctionDecl(),
7387 DSAStack->getConstructTraits(), getOpenMPDeviceNum());
7388
7389 QualType CalleeFnType = CalleeFnDecl->getType();
7390
7391 SmallVector<Expr *, 4> Exprs;
7392 SmallVector<VariantMatchInfo, 4> VMIs;
7393 while (CalleeFnDecl) {
7394 for (OMPDeclareVariantAttr *A :
7395 CalleeFnDecl->specific_attrs<OMPDeclareVariantAttr>()) {
7396 Expr *VariantRef = A->getVariantFuncRef();
7397
7398 VariantMatchInfo VMI;
7399 OMPTraitInfo &TI = A->getTraitInfo();
7400 TI.getAsVariantMatchInfo(ASTCtx&: Context, VMI);
7401 if (!isVariantApplicableInContext(VMI, Ctx: OMPCtx,
7402 /*DeviceSetOnly=*/DeviceOrImplementationSetOnly: false))
7403 continue;
7404
7405 VMIs.push_back(Elt: VMI);
7406 Exprs.push_back(Elt: VariantRef);
7407 }
7408
7409 CalleeFnDecl = CalleeFnDecl->getPreviousDecl();
7410 }
7411
7412 ExprResult NewCall;
7413 do {
7414 int BestIdx = getBestVariantMatchForContext(VMIs, Ctx: OMPCtx);
7415 if (BestIdx < 0)
7416 return Call;
7417 Expr *BestExpr = cast<DeclRefExpr>(Val: Exprs[BestIdx]);
7418 Decl *BestDecl = cast<DeclRefExpr>(Val: BestExpr)->getDecl();
7419
7420 {
7421 // Try to build a (member) call expression for the current best applicable
7422 // variant expression. We allow this to fail in which case we continue
7423 // with the next best variant expression. The fail case is part of the
7424 // implementation defined behavior in the OpenMP standard when it talks
7425 // about what differences in the function prototypes: "Any differences
7426 // that the specific OpenMP context requires in the prototype of the
7427 // variant from the base function prototype are implementation defined."
7428 // This wording is there to allow the specialized variant to have a
7429 // different type than the base function. This is intended and OK but if
7430 // we cannot create a call the difference is not in the "implementation
7431 // defined range" we allow.
7432 Sema::TentativeAnalysisScope Trap(SemaRef);
7433
7434 if (auto *SpecializedMethod = dyn_cast<CXXMethodDecl>(Val: BestDecl)) {
7435 auto *MemberCall = dyn_cast<CXXMemberCallExpr>(Val: CE);
7436 BestExpr = MemberExpr::CreateImplicit(
7437 C: Context, Base: MemberCall->getImplicitObjectArgument(),
7438 /*IsArrow=*/false, MemberDecl: SpecializedMethod, T: Context.BoundMemberTy,
7439 VK: MemberCall->getValueKind(), OK: MemberCall->getObjectKind());
7440 }
7441 NewCall = SemaRef.BuildCallExpr(S: Scope, Fn: BestExpr, LParenLoc, ArgExprs,
7442 RParenLoc, ExecConfig);
7443 if (NewCall.isUsable()) {
7444 if (CallExpr *NCE = dyn_cast<CallExpr>(Val: NewCall.get())) {
7445 FunctionDecl *NewCalleeFnDecl = NCE->getDirectCallee();
7446 QualType NewType = getASTContext().mergeFunctionTypes(
7447 CalleeFnType, NewCalleeFnDecl->getType(),
7448 /*OfBlockPointer=*/false,
7449 /*Unqualified=*/false, /*AllowCXX=*/true);
7450 if (!NewType.isNull())
7451 break;
7452 // Don't use the call if the function type was not compatible.
7453 NewCall = nullptr;
7454 }
7455 }
7456 }
7457
7458 VMIs.erase(CI: VMIs.begin() + BestIdx);
7459 Exprs.erase(CI: Exprs.begin() + BestIdx);
7460 } while (!VMIs.empty());
7461
7462 if (!NewCall.isUsable())
7463 return Call;
7464 return PseudoObjectExpr::Create(Context: getASTContext(), syntactic: CE, semantic: {NewCall.get()}, resultIndex: 0);
7465}
7466
7467std::optional<std::pair<FunctionDecl *, Expr *>>
7468SemaOpenMP::checkOpenMPDeclareVariantFunction(SemaOpenMP::DeclGroupPtrTy DG,
7469 Expr *VariantRef,
7470 OMPTraitInfo &TI,
7471 unsigned NumAppendArgs,
7472 SourceRange SR) {
7473 ASTContext &Context = getASTContext();
7474 if (!DG || DG.get().isNull())
7475 return std::nullopt;
7476
7477 const int VariantId = 1;
7478 // Must be applied only to single decl.
7479 if (!DG.get().isSingleDecl()) {
7480 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_single_decl_in_declare_simd_variant)
7481 << VariantId << SR;
7482 return std::nullopt;
7483 }
7484 Decl *ADecl = DG.get().getSingleDecl();
7485 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: ADecl))
7486 ADecl = FTD->getTemplatedDecl();
7487
7488 // Decl must be a function.
7489 auto *FD = dyn_cast<FunctionDecl>(Val: ADecl);
7490 if (!FD) {
7491 Diag(Loc: ADecl->getLocation(), DiagID: diag::err_omp_function_expected)
7492 << VariantId << SR;
7493 return std::nullopt;
7494 }
7495
7496 auto &&HasMultiVersionAttributes = [](const FunctionDecl *FD) {
7497 // The 'target' attribute needs to be separately checked because it does
7498 // not always signify a multiversion function declaration.
7499 return FD->isMultiVersion() || FD->hasAttr<TargetAttr>();
7500 };
7501 // OpenMP is not compatible with multiversion function attributes.
7502 if (HasMultiVersionAttributes(FD)) {
7503 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_incompat_attributes)
7504 << SR;
7505 return std::nullopt;
7506 }
7507
7508 // Allow #pragma omp declare variant only if the function is not used.
7509 if (FD->isUsed(CheckUsedAttr: false))
7510 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_used)
7511 << FD->getLocation();
7512
7513 // Check if the function was emitted already.
7514 const FunctionDecl *Definition;
7515 if (!FD->isThisDeclarationADefinition() && FD->isDefined(Definition) &&
7516 (getLangOpts().EmitAllDecls || Context.DeclMustBeEmitted(D: Definition)))
7517 Diag(Loc: SR.getBegin(), DiagID: diag::warn_omp_declare_variant_after_emitted)
7518 << FD->getLocation();
7519
7520 // The VariantRef must point to function.
7521 if (!VariantRef) {
7522 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_function_expected) << VariantId;
7523 return std::nullopt;
7524 }
7525
7526 auto ShouldDelayChecks = [](Expr *&E, bool) {
7527 return E && (E->isTypeDependent() || E->isValueDependent() ||
7528 E->containsUnexpandedParameterPack() ||
7529 E->isInstantiationDependent());
7530 };
7531 // Do not check templates, wait until instantiation.
7532 if (FD->isDependentContext() || ShouldDelayChecks(VariantRef, false) ||
7533 TI.anyScoreOrCondition(Cond: ShouldDelayChecks))
7534 return std::make_pair(x&: FD, y&: VariantRef);
7535
7536 // Deal with non-constant score and user condition expressions.
7537 auto HandleNonConstantScoresAndConditions = [this](Expr *&E,
7538 bool IsScore) -> bool {
7539 if (!E || E->isIntegerConstantExpr(Ctx: getASTContext()))
7540 return false;
7541
7542 if (IsScore) {
7543 // We warn on non-constant scores and pretend they were not present.
7544 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_declare_variant_score_not_constant)
7545 << E;
7546 E = nullptr;
7547 } else {
7548 // We could replace a non-constant user condition with "false" but we
7549 // will soon need to handle these anyway for the dynamic version of
7550 // OpenMP context selectors.
7551 Diag(Loc: E->getExprLoc(),
7552 DiagID: diag::err_omp_declare_variant_user_condition_not_constant)
7553 << E;
7554 }
7555 return true;
7556 };
7557 if (TI.anyScoreOrCondition(Cond: HandleNonConstantScoresAndConditions))
7558 return std::nullopt;
7559
7560 QualType AdjustedFnType = FD->getType();
7561 if (NumAppendArgs) {
7562 const auto *PTy = AdjustedFnType->getAsAdjusted<FunctionProtoType>();
7563 if (!PTy) {
7564 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_prototype_required)
7565 << SR;
7566 return std::nullopt;
7567 }
7568 // Adjust the function type to account for an extra omp_interop_t for each
7569 // specified in the append_args clause.
7570 const TypeDecl *TD = nullptr;
7571 LookupResult Result(SemaRef, &Context.Idents.get(Name: "omp_interop_t"),
7572 SR.getBegin(), Sema::LookupOrdinaryName);
7573 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
7574 NamedDecl *ND = Result.getFoundDecl();
7575 TD = dyn_cast_or_null<TypeDecl>(Val: ND);
7576 }
7577 if (!TD) {
7578 Diag(Loc: SR.getBegin(), DiagID: diag::err_omp_interop_type_not_found) << SR;
7579 return std::nullopt;
7580 }
7581 QualType InteropType =
7582 Context.getTypeDeclType(Keyword: ElaboratedTypeKeyword::None,
7583 /*Qualifier=*/std::nullopt, Decl: TD);
7584 if (PTy->isVariadic()) {
7585 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_append_args_with_varargs) << SR;
7586 return std::nullopt;
7587 }
7588 llvm::SmallVector<QualType, 8> Params;
7589 Params.append(in_start: PTy->param_type_begin(), in_end: PTy->param_type_end());
7590 Params.insert(I: Params.end(), NumToInsert: NumAppendArgs, Elt: InteropType);
7591 AdjustedFnType = Context.getFunctionType(ResultTy: PTy->getReturnType(), Args: Params,
7592 EPI: PTy->getExtProtoInfo());
7593 }
7594
7595 // Convert VariantRef expression to the type of the original function to
7596 // resolve possible conflicts.
7597 ExprResult VariantRefCast = VariantRef;
7598 if (getLangOpts().CPlusPlus) {
7599 QualType FnPtrType;
7600 auto *Method = dyn_cast<CXXMethodDecl>(Val: FD);
7601 if (Method && !Method->isStatic()) {
7602 FnPtrType = Context.getMemberPointerType(
7603 T: AdjustedFnType, /*Qualifier=*/std::nullopt, Cls: Method->getParent());
7604 ExprResult ER;
7605 {
7606 // Build addr_of unary op to correctly handle type checks for member
7607 // functions.
7608 Sema::TentativeAnalysisScope Trap(SemaRef);
7609 ER = SemaRef.CreateBuiltinUnaryOp(OpLoc: VariantRef->getBeginLoc(), Opc: UO_AddrOf,
7610 InputExpr: VariantRef);
7611 }
7612 if (!ER.isUsable()) {
7613 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7614 << VariantId << VariantRef->getSourceRange();
7615 return std::nullopt;
7616 }
7617 VariantRef = ER.get();
7618 } else {
7619 FnPtrType = Context.getPointerType(T: AdjustedFnType);
7620 }
7621 QualType VarianPtrType = Context.getPointerType(T: VariantRef->getType());
7622 if (VarianPtrType.getUnqualifiedType() != FnPtrType.getUnqualifiedType()) {
7623 ImplicitConversionSequence ICS = SemaRef.TryImplicitConversion(
7624 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
7625 /*SuppressUserConversions=*/false, AllowExplicit: Sema::AllowedExplicit::None,
7626 /*InOverloadResolution=*/false,
7627 /*CStyle=*/false,
7628 /*AllowObjCWritebackConversion=*/false);
7629 if (ICS.isFailure()) {
7630 Diag(Loc: VariantRef->getExprLoc(),
7631 DiagID: diag::err_omp_declare_variant_incompat_types)
7632 << VariantRef->getType()
7633 << ((Method && !Method->isStatic()) ? FnPtrType : FD->getType())
7634 << (NumAppendArgs ? 1 : 0) << VariantRef->getSourceRange();
7635 return std::nullopt;
7636 }
7637 VariantRefCast = SemaRef.PerformImplicitConversion(
7638 From: VariantRef, ToType: FnPtrType.getUnqualifiedType(),
7639 Action: AssignmentAction::Converting);
7640 if (!VariantRefCast.isUsable())
7641 return std::nullopt;
7642 }
7643 // Drop previously built artificial addr_of unary op for member functions.
7644 if (Method && !Method->isStatic()) {
7645 Expr *PossibleAddrOfVariantRef = VariantRefCast.get();
7646 if (auto *UO = dyn_cast<UnaryOperator>(
7647 Val: PossibleAddrOfVariantRef->IgnoreImplicit()))
7648 VariantRefCast = UO->getSubExpr();
7649 }
7650 }
7651
7652 ExprResult ER = SemaRef.CheckPlaceholderExpr(E: VariantRefCast.get());
7653 if (!ER.isUsable() ||
7654 !ER.get()->IgnoreParenImpCasts()->getType()->isFunctionType()) {
7655 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7656 << VariantId << VariantRef->getSourceRange();
7657 return std::nullopt;
7658 }
7659
7660 // The VariantRef must point to function.
7661 auto *DRE = dyn_cast<DeclRefExpr>(Val: ER.get()->IgnoreParenImpCasts());
7662 if (!DRE) {
7663 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7664 << VariantId << VariantRef->getSourceRange();
7665 return std::nullopt;
7666 }
7667 auto *NewFD = dyn_cast_or_null<FunctionDecl>(Val: DRE->getDecl());
7668 if (!NewFD) {
7669 Diag(Loc: VariantRef->getExprLoc(), DiagID: diag::err_omp_function_expected)
7670 << VariantId << VariantRef->getSourceRange();
7671 return std::nullopt;
7672 }
7673
7674 if (FD->getCanonicalDecl() == NewFD->getCanonicalDecl()) {
7675 Diag(Loc: VariantRef->getExprLoc(),
7676 DiagID: diag::err_omp_declare_variant_same_base_function)
7677 << VariantRef->getSourceRange();
7678 return std::nullopt;
7679 }
7680
7681 // Check if function types are compatible in C.
7682 if (!getLangOpts().CPlusPlus) {
7683 QualType NewType =
7684 Context.mergeFunctionTypes(AdjustedFnType, NewFD->getType());
7685 if (NewType.isNull()) {
7686 Diag(Loc: VariantRef->getExprLoc(),
7687 DiagID: diag::err_omp_declare_variant_incompat_types)
7688 << NewFD->getType() << FD->getType() << (NumAppendArgs ? 1 : 0)
7689 << VariantRef->getSourceRange();
7690 return std::nullopt;
7691 }
7692 if (NewType->isFunctionProtoType()) {
7693 if (FD->getType()->isFunctionNoProtoType())
7694 setPrototype(S&: SemaRef, FD, FDWithProto: NewFD, NewType);
7695 else if (NewFD->getType()->isFunctionNoProtoType())
7696 setPrototype(S&: SemaRef, FD: NewFD, FDWithProto: FD, NewType);
7697 }
7698 }
7699
7700 // Check if variant function is not marked with declare variant directive.
7701 if (NewFD->hasAttrs() && NewFD->hasAttr<OMPDeclareVariantAttr>()) {
7702 Diag(Loc: VariantRef->getExprLoc(),
7703 DiagID: diag::warn_omp_declare_variant_marked_as_declare_variant)
7704 << VariantRef->getSourceRange();
7705 SourceRange SR =
7706 NewFD->specific_attr_begin<OMPDeclareVariantAttr>()->getRange();
7707 Diag(Loc: SR.getBegin(), DiagID: diag::note_omp_marked_declare_variant_here) << SR;
7708 return std::nullopt;
7709 }
7710
7711 enum DoesntSupport {
7712 VirtFuncs = 1,
7713 Constructors = 3,
7714 Destructors = 4,
7715 DeletedFuncs = 5,
7716 DefaultedFuncs = 6,
7717 ConstexprFuncs = 7,
7718 ConstevalFuncs = 8,
7719 };
7720 if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(Val: FD)) {
7721 if (CXXFD->isVirtual()) {
7722 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7723 << VirtFuncs;
7724 return std::nullopt;
7725 }
7726
7727 if (isa<CXXConstructorDecl>(Val: FD)) {
7728 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7729 << Constructors;
7730 return std::nullopt;
7731 }
7732
7733 if (isa<CXXDestructorDecl>(Val: FD)) {
7734 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7735 << Destructors;
7736 return std::nullopt;
7737 }
7738 }
7739
7740 if (FD->isDeleted()) {
7741 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7742 << DeletedFuncs;
7743 return std::nullopt;
7744 }
7745
7746 if (FD->isDefaulted()) {
7747 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7748 << DefaultedFuncs;
7749 return std::nullopt;
7750 }
7751
7752 if (FD->isConstexpr()) {
7753 Diag(Loc: FD->getLocation(), DiagID: diag::err_omp_declare_variant_doesnt_support)
7754 << (NewFD->isConsteval() ? ConstevalFuncs : ConstexprFuncs);
7755 return std::nullopt;
7756 }
7757
7758 // Check general compatibility.
7759 if (SemaRef.areMultiversionVariantFunctionsCompatible(
7760 OldFD: FD, NewFD, NoProtoDiagID: PartialDiagnostic::NullDiagnostic(),
7761 NoteCausedDiagIDAt: PartialDiagnosticAt(SourceLocation(),
7762 PartialDiagnostic::NullDiagnostic()),
7763 NoSupportDiagIDAt: PartialDiagnosticAt(
7764 VariantRef->getExprLoc(),
7765 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_doesnt_support)),
7766 DiffDiagIDAt: PartialDiagnosticAt(VariantRef->getExprLoc(),
7767 SemaRef.PDiag(DiagID: diag::err_omp_declare_variant_diff)
7768 << FD->getLocation()),
7769 /*TemplatesSupported=*/true, /*ConstexprSupported=*/false,
7770 /*CLinkageMayDiffer=*/true))
7771 return std::nullopt;
7772 return std::make_pair(x&: FD, y: cast<Expr>(Val: DRE));
7773}
7774
7775void SemaOpenMP::ActOnOpenMPDeclareVariantDirective(
7776 FunctionDecl *FD, Expr *VariantRef, OMPTraitInfo &TI,
7777 ArrayRef<Expr *> AdjustArgsNothing,
7778 ArrayRef<Expr *> AdjustArgsNeedDevicePtr,
7779 ArrayRef<Expr *> AdjustArgsNeedDeviceAddr,
7780 ArrayRef<OMPInteropInfo> AppendArgs, SourceLocation AdjustArgsLoc,
7781 SourceLocation AppendArgsLoc, SourceRange SR) {
7782
7783 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7784 // An adjust_args clause or append_args clause can only be specified if the
7785 // dispatch selector of the construct selector set appears in the match
7786 // clause.
7787
7788 SmallVector<Expr *, 8> AllAdjustArgs;
7789 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNothing);
7790 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNeedDevicePtr);
7791 llvm::append_range(C&: AllAdjustArgs, R&: AdjustArgsNeedDeviceAddr);
7792
7793 if (!AllAdjustArgs.empty() || !AppendArgs.empty()) {
7794 VariantMatchInfo VMI;
7795 TI.getAsVariantMatchInfo(ASTCtx&: getASTContext(), VMI);
7796 if (!llvm::is_contained(
7797 Range&: VMI.ConstructTraits,
7798 Element: llvm::omp::TraitProperty::construct_dispatch_dispatch)) {
7799 if (!AllAdjustArgs.empty())
7800 Diag(Loc: AdjustArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
7801 << getOpenMPClauseNameForDiag(C: OMPC_adjust_args);
7802 if (!AppendArgs.empty())
7803 Diag(Loc: AppendArgsLoc, DiagID: diag::err_omp_clause_requires_dispatch_construct)
7804 << getOpenMPClauseNameForDiag(C: OMPC_append_args);
7805 return;
7806 }
7807 }
7808
7809 // OpenMP 5.1 [2.3.5, declare variant directive, Restrictions]
7810 // Each argument can only appear in a single adjust_args clause for each
7811 // declare variant directive.
7812 llvm::SmallPtrSet<const VarDecl *, 4> AdjustVars;
7813
7814 for (Expr *E : AllAdjustArgs) {
7815 E = E->IgnoreParenImpCasts();
7816 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
7817 if (const auto *PVD = dyn_cast<ParmVarDecl>(Val: DRE->getDecl())) {
7818 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
7819 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
7820 FD->getParamDecl(i: PVD->getFunctionScopeIndex())
7821 ->getCanonicalDecl() == CanonPVD) {
7822 // It's a parameter of the function, check duplicates.
7823 if (!AdjustVars.insert(Ptr: CanonPVD).second) {
7824 Diag(Loc: DRE->getLocation(), DiagID: diag::err_omp_adjust_arg_multiple_clauses)
7825 << PVD;
7826 return;
7827 }
7828 continue;
7829 }
7830 }
7831 }
7832 // Anything that is not a function parameter is an error.
7833 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_param_or_this_in_clause) << FD << 0;
7834 return;
7835 }
7836
7837 // OpenMP 6.0 [9.6.2 (page 332, line 31-33, adjust_args clause, Restrictions]
7838 // If the `need_device_addr` adjust-op modifier is present, each list item
7839 // that appears in the clause must refer to an argument in the declaration of
7840 // the function variant that has a reference type
7841 if (getLangOpts().OpenMP >= 60) {
7842 for (Expr *E : AdjustArgsNeedDeviceAddr) {
7843 E = E->IgnoreParenImpCasts();
7844 if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
7845 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl())) {
7846 if (!VD->getType()->isReferenceType())
7847 Diag(Loc: E->getExprLoc(),
7848 DiagID: diag::err_omp_non_by_ref_need_device_addr_modifier_argument);
7849 }
7850 }
7851 }
7852 }
7853
7854 auto *NewAttr = OMPDeclareVariantAttr::CreateImplicit(
7855 Ctx&: getASTContext(), VariantFuncRef: VariantRef, TraitInfos: &TI,
7856 AdjustArgsNothing: const_cast<Expr **>(AdjustArgsNothing.data()), AdjustArgsNothingSize: AdjustArgsNothing.size(),
7857 AdjustArgsNeedDevicePtr: const_cast<Expr **>(AdjustArgsNeedDevicePtr.data()),
7858 AdjustArgsNeedDevicePtrSize: AdjustArgsNeedDevicePtr.size(),
7859 AdjustArgsNeedDeviceAddr: const_cast<Expr **>(AdjustArgsNeedDeviceAddr.data()),
7860 AdjustArgsNeedDeviceAddrSize: AdjustArgsNeedDeviceAddr.size(),
7861 AppendArgs: const_cast<OMPInteropInfo *>(AppendArgs.data()), AppendArgsSize: AppendArgs.size(), Range: SR);
7862 FD->addAttr(A: NewAttr);
7863}
7864
7865static CapturedStmt *
7866setBranchProtectedScope(Sema &SemaRef, OpenMPDirectiveKind DKind, Stmt *AStmt) {
7867 auto *CS = dyn_cast<CapturedStmt>(Val: AStmt);
7868 assert(CS && "Captured statement expected");
7869 // 1.2.2 OpenMP Language Terminology
7870 // Structured block - An executable statement with a single entry at the
7871 // top and a single exit at the bottom.
7872 // The point of exit cannot be a branch out of the structured block.
7873 // longjmp() and throw() must not violate the entry/exit criteria.
7874 CS->getCapturedDecl()->setNothrow();
7875
7876 for (int ThisCaptureLevel = SemaRef.OpenMP().getOpenMPCaptureLevels(DKind);
7877 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7878 CS = cast<CapturedStmt>(Val: CS->getCapturedStmt());
7879 // 1.2.2 OpenMP Language Terminology
7880 // Structured block - An executable statement with a single entry at the
7881 // top and a single exit at the bottom.
7882 // The point of exit cannot be a branch out of the structured block.
7883 // longjmp() and throw() must not violate the entry/exit criteria.
7884 CS->getCapturedDecl()->setNothrow();
7885 }
7886 SemaRef.setFunctionHasBranchProtectedScope();
7887 return CS;
7888}
7889
7890StmtResult
7891SemaOpenMP::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
7892 Stmt *AStmt, SourceLocation StartLoc,
7893 SourceLocation EndLoc) {
7894 if (!AStmt)
7895 return StmtError();
7896
7897 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel, AStmt);
7898
7899 return OMPParallelDirective::Create(
7900 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
7901 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
7902}
7903
7904namespace {
7905/// Iteration space of a single for loop.
7906struct LoopIterationSpace final {
7907 /// True if the condition operator is the strict compare operator (<, > or
7908 /// !=).
7909 bool IsStrictCompare = false;
7910 /// Condition of the loop.
7911 Expr *PreCond = nullptr;
7912 /// This expression calculates the number of iterations in the loop.
7913 /// It is always possible to calculate it before starting the loop.
7914 Expr *NumIterations = nullptr;
7915 /// The loop counter variable.
7916 Expr *CounterVar = nullptr;
7917 /// Private loop counter variable.
7918 Expr *PrivateCounterVar = nullptr;
7919 /// This is initializer for the initial value of #CounterVar.
7920 Expr *CounterInit = nullptr;
7921 /// This is step for the #CounterVar used to generate its update:
7922 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
7923 Expr *CounterStep = nullptr;
7924 /// Should step be subtracted?
7925 bool Subtract = false;
7926 /// Source range of the loop init.
7927 SourceRange InitSrcRange;
7928 /// Source range of the loop condition.
7929 SourceRange CondSrcRange;
7930 /// Source range of the loop increment.
7931 SourceRange IncSrcRange;
7932 /// Minimum value that can have the loop control variable. Used to support
7933 /// non-rectangular loops. Applied only for LCV with the non-iterator types,
7934 /// since only such variables can be used in non-loop invariant expressions.
7935 Expr *MinValue = nullptr;
7936 /// Maximum value that can have the loop control variable. Used to support
7937 /// non-rectangular loops. Applied only for LCV with the non-iterator type,
7938 /// since only such variables can be used in non-loop invariant expressions.
7939 Expr *MaxValue = nullptr;
7940 /// true, if the lower bound depends on the outer loop control var.
7941 bool IsNonRectangularLB = false;
7942 /// true, if the upper bound depends on the outer loop control var.
7943 bool IsNonRectangularUB = false;
7944 /// Index of the loop this loop depends on and forms non-rectangular loop
7945 /// nest.
7946 unsigned LoopDependentIdx = 0;
7947 /// Final condition for the non-rectangular loop nest support. It is used to
7948 /// check that the number of iterations for this particular counter must be
7949 /// finished.
7950 Expr *FinalCondition = nullptr;
7951};
7952
7953/// Scan an AST subtree, checking that no decls in the CollapsedLoopVarDecls
7954/// set are referenced. Used for verifying loop nest structure before
7955/// performing a loop collapse operation.
7956class ForSubExprChecker : public DynamicRecursiveASTVisitor {
7957 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
7958 VarDecl *ForbiddenVar = nullptr;
7959 SourceRange ErrLoc;
7960
7961public:
7962 explicit ForSubExprChecker(
7963 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls)
7964 : CollapsedLoopVarDecls(CollapsedLoopVarDecls) {
7965 // We want to visit implicit code, i.e. synthetic initialisation statements
7966 // created during range-for lowering.
7967 ShouldVisitImplicitCode = true;
7968 }
7969
7970 bool VisitDeclRefExpr(DeclRefExpr *E) override {
7971 ValueDecl *VD = E->getDecl();
7972 if (!isa<VarDecl, BindingDecl>(Val: VD))
7973 return true;
7974 VarDecl *V = VD->getPotentiallyDecomposedVarDecl();
7975 if (V->getType()->isReferenceType()) {
7976 VarDecl *VD = V->getDefinition();
7977 if (VD->hasInit()) {
7978 Expr *I = VD->getInit();
7979 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: I);
7980 if (!DRE)
7981 return true;
7982 V = DRE->getDecl()->getPotentiallyDecomposedVarDecl();
7983 }
7984 }
7985 Decl *Canon = V->getCanonicalDecl();
7986 if (CollapsedLoopVarDecls.contains(Ptr: Canon)) {
7987 ForbiddenVar = V;
7988 ErrLoc = E->getSourceRange();
7989 return false;
7990 }
7991
7992 return true;
7993 }
7994
7995 VarDecl *getForbiddenVar() const { return ForbiddenVar; }
7996 SourceRange getErrRange() const { return ErrLoc; }
7997};
7998
7999/// Helper class for checking canonical form of the OpenMP loops and
8000/// extracting iteration space of each loop in the loop nest, that will be used
8001/// for IR generation.
8002class OpenMPIterationSpaceChecker {
8003 /// Reference to Sema.
8004 Sema &SemaRef;
8005 /// Does the loop associated directive support non-rectangular loops?
8006 bool SupportsNonRectangular;
8007 /// Data-sharing stack.
8008 DSAStackTy &Stack;
8009 /// A location for diagnostics (when there is no some better location).
8010 SourceLocation DefaultLoc;
8011 /// A location for diagnostics (when increment is not compatible).
8012 SourceLocation ConditionLoc;
8013 /// The set of variables declared within the (to be collapsed) loop nest.
8014 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls;
8015 /// A source location for referring to loop init later.
8016 SourceRange InitSrcRange;
8017 /// A source location for referring to condition later.
8018 SourceRange ConditionSrcRange;
8019 /// A source location for referring to increment later.
8020 SourceRange IncrementSrcRange;
8021 /// Loop variable.
8022 ValueDecl *LCDecl = nullptr;
8023 /// Reference to loop variable.
8024 Expr *LCRef = nullptr;
8025 /// Lower bound (initializer for the var).
8026 Expr *LB = nullptr;
8027 /// Upper bound.
8028 Expr *UB = nullptr;
8029 /// Loop step (increment).
8030 Expr *Step = nullptr;
8031 /// This flag is true when condition is one of:
8032 /// Var < UB
8033 /// Var <= UB
8034 /// UB > Var
8035 /// UB >= Var
8036 /// This will have no value when the condition is !=
8037 std::optional<bool> TestIsLessOp;
8038 /// This flag is true when condition is strict ( < or > ).
8039 bool TestIsStrictOp = false;
8040 /// This flag is true when step is subtracted on each iteration.
8041 bool SubtractStep = false;
8042 /// The outer loop counter this loop depends on (if any).
8043 const ValueDecl *DepDecl = nullptr;
8044 /// Contains number of loop (starts from 1) on which loop counter init
8045 /// expression of this loop depends on.
8046 std::optional<unsigned> InitDependOnLC;
8047 /// Contains number of loop (starts from 1) on which loop counter condition
8048 /// expression of this loop depends on.
8049 std::optional<unsigned> CondDependOnLC;
8050 /// Checks if the provide statement depends on the loop counter.
8051 std::optional<unsigned> doesDependOnLoopCounter(const Stmt *S,
8052 bool IsInitializer);
8053 /// Original condition required for checking of the exit condition for
8054 /// non-rectangular loop.
8055 Expr *Condition = nullptr;
8056
8057public:
8058 OpenMPIterationSpaceChecker(
8059 Sema &SemaRef, bool SupportsNonRectangular, DSAStackTy &Stack,
8060 SourceLocation DefaultLoc,
8061 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopDecls)
8062 : SemaRef(SemaRef), SupportsNonRectangular(SupportsNonRectangular),
8063 Stack(Stack), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
8064 CollapsedLoopVarDecls(CollapsedLoopDecls) {}
8065 /// Check init-expr for canonical loop form and save loop counter
8066 /// variable - #Var and its initialization value - #LB.
8067 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
8068 /// Check test-expr for canonical form, save upper-bound (#UB), flags
8069 /// for less/greater and for strict/non-strict comparison.
8070 bool checkAndSetCond(Expr *S);
8071 /// Check incr-expr for canonical loop form and return true if it
8072 /// does not conform, otherwise save loop step (#Step).
8073 bool checkAndSetInc(Expr *S);
8074 /// Return the loop counter variable.
8075 ValueDecl *getLoopDecl() const { return LCDecl; }
8076 /// Return the reference expression to loop counter variable.
8077 Expr *getLoopDeclRefExpr() const { return LCRef; }
8078 /// Source range of the loop init.
8079 SourceRange getInitSrcRange() const { return InitSrcRange; }
8080 /// Source range of the loop condition.
8081 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
8082 /// Source range of the loop increment.
8083 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
8084 /// True if the step should be subtracted.
8085 bool shouldSubtractStep() const { return SubtractStep; }
8086 /// True, if the compare operator is strict (<, > or !=).
8087 bool isStrictTestOp() const { return TestIsStrictOp; }
8088 /// Build the expression to calculate the number of iterations.
8089 Expr *buildNumIterations(
8090 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8091 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8092 /// Build the precondition expression for the loops.
8093 Expr *
8094 buildPreCond(Scope *S, Expr *Cond,
8095 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8096 /// Build reference expression to the counter be used for codegen.
8097 DeclRefExpr *
8098 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8099 DSAStackTy &DSA) const;
8100 /// Build reference expression to the private counter be used for
8101 /// codegen.
8102 Expr *buildPrivateCounterVar() const;
8103 /// Build initialization of the counter be used for codegen.
8104 Expr *buildCounterInit() const;
8105 /// Build step of the counter be used for codegen.
8106 Expr *buildCounterStep() const;
8107 /// Build loop data with counter value for depend clauses in ordered
8108 /// directives.
8109 Expr *
8110 buildOrderedLoopData(Scope *S, Expr *Counter,
8111 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8112 SourceLocation Loc, Expr *Inc = nullptr,
8113 OverloadedOperatorKind OOK = OO_Amp);
8114 /// Builds the minimum value for the loop counter.
8115 std::pair<Expr *, Expr *> buildMinMaxValues(
8116 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
8117 /// Builds final condition for the non-rectangular loops.
8118 Expr *buildFinalCondition(Scope *S) const;
8119 /// Return true if any expression is dependent.
8120 bool dependent() const;
8121 /// Returns true if the initializer forms non-rectangular loop.
8122 bool doesInitDependOnLC() const { return InitDependOnLC.has_value(); }
8123 /// Returns true if the condition forms non-rectangular loop.
8124 bool doesCondDependOnLC() const { return CondDependOnLC.has_value(); }
8125 /// Returns index of the loop we depend on (starting from 1), or 0 otherwise.
8126 unsigned getLoopDependentIdx() const {
8127 return InitDependOnLC.value_or(u: CondDependOnLC.value_or(u: 0));
8128 }
8129
8130private:
8131 /// Check the right-hand side of an assignment in the increment
8132 /// expression.
8133 bool checkAndSetIncRHS(Expr *RHS);
8134 /// Helper to set loop counter variable and its initializer.
8135 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB,
8136 bool EmitDiags);
8137 /// Helper to set upper bound.
8138 bool setUB(Expr *NewUB, std::optional<bool> LessOp, bool StrictOp,
8139 SourceRange SR, SourceLocation SL);
8140 /// Helper to set loop increment.
8141 bool setStep(Expr *NewStep, bool Subtract);
8142};
8143
8144bool OpenMPIterationSpaceChecker::dependent() const {
8145 if (!LCDecl) {
8146 assert(!LB && !UB && !Step);
8147 return false;
8148 }
8149 return LCDecl->getType()->isDependentType() ||
8150 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
8151 (Step && Step->isValueDependent());
8152}
8153
8154bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
8155 Expr *NewLCRefExpr,
8156 Expr *NewLB, bool EmitDiags) {
8157 // State consistency checking to ensure correct usage.
8158 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
8159 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8160 if (!NewLCDecl || !NewLB || NewLB->containsErrors())
8161 return true;
8162 LCDecl = getCanonicalDecl(D: NewLCDecl);
8163 LCRef = NewLCRefExpr;
8164 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: NewLB))
8165 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
8166 if ((Ctor->isCopyOrMoveConstructor() ||
8167 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
8168 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
8169 NewLB = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
8170 LB = NewLB;
8171 if (EmitDiags)
8172 InitDependOnLC = doesDependOnLoopCounter(S: LB, /*IsInitializer=*/true);
8173 return false;
8174}
8175
8176bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, std::optional<bool> LessOp,
8177 bool StrictOp, SourceRange SR,
8178 SourceLocation SL) {
8179 // State consistency checking to ensure correct usage.
8180 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
8181 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
8182 if (!NewUB || NewUB->containsErrors())
8183 return true;
8184 UB = NewUB;
8185 if (LessOp)
8186 TestIsLessOp = LessOp;
8187 TestIsStrictOp = StrictOp;
8188 ConditionSrcRange = SR;
8189 ConditionLoc = SL;
8190 CondDependOnLC = doesDependOnLoopCounter(S: UB, /*IsInitializer=*/false);
8191 return false;
8192}
8193
8194bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
8195 // State consistency checking to ensure correct usage.
8196 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
8197 if (!NewStep || NewStep->containsErrors())
8198 return true;
8199 if (!NewStep->isValueDependent()) {
8200 // Check that the step is integer expression.
8201 SourceLocation StepLoc = NewStep->getBeginLoc();
8202 ExprResult Val = SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(
8203 OpLoc: StepLoc, Op: getExprAsWritten(E: NewStep));
8204 if (Val.isInvalid())
8205 return true;
8206 NewStep = Val.get();
8207
8208 // OpenMP [2.6, Canonical Loop Form, Restrictions]
8209 // If test-expr is of form var relational-op b and relational-op is < or
8210 // <= then incr-expr must cause var to increase on each iteration of the
8211 // loop. If test-expr is of form var relational-op b and relational-op is
8212 // > or >= then incr-expr must cause var to decrease on each iteration of
8213 // the loop.
8214 // If test-expr is of form b relational-op var and relational-op is < or
8215 // <= then incr-expr must cause var to decrease on each iteration of the
8216 // loop. If test-expr is of form b relational-op var and relational-op is
8217 // > or >= then incr-expr must cause var to increase on each iteration of
8218 // the loop.
8219 std::optional<llvm::APSInt> Result =
8220 NewStep->getIntegerConstantExpr(Ctx: SemaRef.Context);
8221 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
8222 bool IsConstNeg =
8223 Result && Result->isSigned() && (Subtract != Result->isNegative());
8224 bool IsConstPos =
8225 Result && Result->isSigned() && (Subtract == Result->isNegative());
8226 bool IsConstZero = Result && !Result->getBoolValue();
8227
8228 // != with increment is treated as <; != with decrement is treated as >
8229 if (!TestIsLessOp)
8230 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
8231 if (UB && (IsConstZero ||
8232 (*TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
8233 : (IsConstPos || (IsUnsigned && !Subtract))))) {
8234 SemaRef.Diag(Loc: NewStep->getExprLoc(),
8235 DiagID: diag::err_omp_loop_incr_not_compatible)
8236 << LCDecl << *TestIsLessOp << NewStep->getSourceRange();
8237 SemaRef.Diag(Loc: ConditionLoc,
8238 DiagID: diag::note_omp_loop_cond_requires_compatible_incr)
8239 << *TestIsLessOp << ConditionSrcRange;
8240 return true;
8241 }
8242 if (*TestIsLessOp == Subtract) {
8243 NewStep =
8244 SemaRef.CreateBuiltinUnaryOp(OpLoc: NewStep->getExprLoc(), Opc: UO_Minus, InputExpr: NewStep)
8245 .get();
8246 Subtract = !Subtract;
8247 }
8248 }
8249
8250 Step = NewStep;
8251 SubtractStep = Subtract;
8252 return false;
8253}
8254
8255namespace {
8256/// Checker for the non-rectangular loops. Checks if the initializer or
8257/// condition expression references loop counter variable.
8258class LoopCounterRefChecker final
8259 : public ConstStmtVisitor<LoopCounterRefChecker, bool> {
8260 Sema &SemaRef;
8261 DSAStackTy &Stack;
8262 const ValueDecl *CurLCDecl = nullptr;
8263 const ValueDecl *DepDecl = nullptr;
8264 const ValueDecl *PrevDepDecl = nullptr;
8265 bool IsInitializer = true;
8266 bool SupportsNonRectangular;
8267 unsigned BaseLoopId = 0;
8268 bool checkDecl(const Expr *E, const ValueDecl *VD) {
8269 if (getCanonicalDecl(D: VD) == getCanonicalDecl(D: CurLCDecl)) {
8270 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_stmt_depends_on_loop_counter)
8271 << (IsInitializer ? 0 : 1);
8272 return false;
8273 }
8274 const auto &&Data = Stack.isLoopControlVariable(D: VD);
8275 // OpenMP, 2.9.1 Canonical Loop Form, Restrictions.
8276 // The type of the loop iterator on which we depend may not have a random
8277 // access iterator type.
8278 if (Data.first && VD->getType()->isRecordType()) {
8279 SmallString<128> Name;
8280 llvm::raw_svector_ostream OS(Name);
8281 VD->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
8282 /*Qualified=*/true);
8283 SemaRef.Diag(Loc: E->getExprLoc(),
8284 DiagID: diag::err_omp_wrong_dependency_iterator_type)
8285 << OS.str();
8286 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::note_previous_decl) << VD;
8287 return false;
8288 }
8289 if (Data.first && !SupportsNonRectangular) {
8290 SemaRef.Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_invariant_dependency);
8291 return false;
8292 }
8293 if (Data.first &&
8294 (DepDecl || (PrevDepDecl &&
8295 getCanonicalDecl(D: VD) != getCanonicalDecl(D: PrevDepDecl)))) {
8296 if (!DepDecl && PrevDepDecl)
8297 DepDecl = PrevDepDecl;
8298 SmallString<128> Name;
8299 llvm::raw_svector_ostream OS(Name);
8300 DepDecl->getNameForDiagnostic(OS, Policy: SemaRef.getPrintingPolicy(),
8301 /*Qualified=*/true);
8302 SemaRef.Diag(Loc: E->getExprLoc(),
8303 DiagID: diag::err_omp_invariant_or_linear_dependency)
8304 << OS.str();
8305 return false;
8306 }
8307 if (Data.first) {
8308 DepDecl = VD;
8309 BaseLoopId = Data.first;
8310 }
8311 return Data.first;
8312 }
8313
8314public:
8315 bool VisitDeclRefExpr(const DeclRefExpr *E) {
8316 const ValueDecl *VD = E->getDecl();
8317 if (isa<VarDecl>(Val: VD))
8318 return checkDecl(E, VD);
8319 return false;
8320 }
8321 bool VisitMemberExpr(const MemberExpr *E) {
8322 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParens())) {
8323 const ValueDecl *VD = E->getMemberDecl();
8324 if (isa<VarDecl>(Val: VD) || isa<FieldDecl>(Val: VD))
8325 return checkDecl(E, VD);
8326 }
8327 return false;
8328 }
8329 bool VisitStmt(const Stmt *S) {
8330 bool Res = false;
8331 for (const Stmt *Child : S->children())
8332 Res = (Child && Visit(S: Child)) || Res;
8333 return Res;
8334 }
8335 explicit LoopCounterRefChecker(Sema &SemaRef, DSAStackTy &Stack,
8336 const ValueDecl *CurLCDecl, bool IsInitializer,
8337 const ValueDecl *PrevDepDecl = nullptr,
8338 bool SupportsNonRectangular = true)
8339 : SemaRef(SemaRef), Stack(Stack), CurLCDecl(CurLCDecl),
8340 PrevDepDecl(PrevDepDecl), IsInitializer(IsInitializer),
8341 SupportsNonRectangular(SupportsNonRectangular) {}
8342 unsigned getBaseLoopId() const {
8343 assert(CurLCDecl && "Expected loop dependency.");
8344 return BaseLoopId;
8345 }
8346 const ValueDecl *getDepDecl() const {
8347 assert(CurLCDecl && "Expected loop dependency.");
8348 return DepDecl;
8349 }
8350};
8351} // namespace
8352
8353std::optional<unsigned>
8354OpenMPIterationSpaceChecker::doesDependOnLoopCounter(const Stmt *S,
8355 bool IsInitializer) {
8356 // Check for the non-rectangular loops.
8357 LoopCounterRefChecker LoopStmtChecker(SemaRef, Stack, LCDecl, IsInitializer,
8358 DepDecl, SupportsNonRectangular);
8359 if (LoopStmtChecker.Visit(S)) {
8360 DepDecl = LoopStmtChecker.getDepDecl();
8361 return LoopStmtChecker.getBaseLoopId();
8362 }
8363 return std::nullopt;
8364}
8365
8366bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
8367 // Check init-expr for canonical loop form and save loop counter
8368 // variable - #Var and its initialization value - #LB.
8369 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
8370 // var = lb
8371 // integer-type var = lb
8372 // random-access-iterator-type var = lb
8373 // pointer-type var = lb
8374 //
8375 if (!S) {
8376 if (EmitDiags) {
8377 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_init);
8378 }
8379 return true;
8380 }
8381 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
8382 if (!ExprTemp->cleanupsHaveSideEffects())
8383 S = ExprTemp->getSubExpr();
8384
8385 if (!CollapsedLoopVarDecls.empty()) {
8386 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8387 if (!FSEC.TraverseStmt(S)) {
8388 SourceRange Range = FSEC.getErrRange();
8389 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8390 << Range.getEnd() << 0 << FSEC.getForbiddenVar();
8391 return true;
8392 }
8393 }
8394
8395 InitSrcRange = S->getSourceRange();
8396 if (Expr *E = dyn_cast<Expr>(Val: S))
8397 S = E->IgnoreParens();
8398 if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8399 if (BO->getOpcode() == BO_Assign) {
8400 Expr *LHS = BO->getLHS()->IgnoreParens();
8401 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
8402 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
8403 if (auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
8404 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8405 EmitDiags);
8406 return setLCDeclAndLB(NewLCDecl: DRE->getDecl(), NewLCRefExpr: DRE, NewLB: BO->getRHS(), EmitDiags);
8407 }
8408 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
8409 if (ME->isArrow() &&
8410 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8411 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8412 EmitDiags);
8413 }
8414 }
8415 } else if (auto *DS = dyn_cast<DeclStmt>(Val: S)) {
8416 if (DS->isSingleDecl()) {
8417 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: DS->getSingleDecl())) {
8418 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
8419 // Accept non-canonical init form here but emit ext. warning.
8420 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
8421 SemaRef.Diag(Loc: S->getBeginLoc(),
8422 DiagID: diag::ext_omp_loop_not_canonical_init)
8423 << S->getSourceRange();
8424 return setLCDeclAndLB(
8425 NewLCDecl: Var,
8426 NewLCRefExpr: buildDeclRefExpr(S&: SemaRef, D: Var,
8427 Ty: Var->getType().getNonReferenceType(),
8428 Loc: DS->getBeginLoc()),
8429 NewLB: Var->getInit(), EmitDiags);
8430 }
8431 }
8432 }
8433 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8434 if (CE->getOperator() == OO_Equal) {
8435 Expr *LHS = CE->getArg(Arg: 0);
8436 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: LHS)) {
8437 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(Val: DRE->getDecl()))
8438 if (auto *ME = dyn_cast<MemberExpr>(Val: getExprAsWritten(E: CED->getInit())))
8439 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8440 EmitDiags);
8441 return setLCDeclAndLB(NewLCDecl: DRE->getDecl(), NewLCRefExpr: DRE, NewLB: CE->getArg(Arg: 1), EmitDiags);
8442 }
8443 if (auto *ME = dyn_cast<MemberExpr>(Val: LHS)) {
8444 if (ME->isArrow() &&
8445 isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8446 return setLCDeclAndLB(NewLCDecl: ME->getMemberDecl(), NewLCRefExpr: ME, NewLB: BO->getRHS(),
8447 EmitDiags);
8448 }
8449 }
8450 }
8451
8452 if (dependent() || SemaRef.CurContext->isDependentContext())
8453 return false;
8454 if (EmitDiags) {
8455 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_init)
8456 << S->getSourceRange();
8457 }
8458 return true;
8459}
8460
8461/// Ignore parenthesizes, implicit casts, copy constructor and return the
8462/// variable (which may be the loop variable) if possible.
8463static const ValueDecl *getInitLCDecl(const Expr *E) {
8464 if (!E)
8465 return nullptr;
8466 E = getExprAsWritten(E);
8467 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(Val: E))
8468 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
8469 if ((Ctor->isCopyOrMoveConstructor() ||
8470 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
8471 CE->getNumArgs() > 0 && CE->getArg(Arg: 0) != nullptr)
8472 E = CE->getArg(Arg: 0)->IgnoreParenImpCasts();
8473 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(Val: E)) {
8474 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
8475 return getCanonicalDecl(D: VD);
8476 }
8477 if (const auto *ME = dyn_cast_or_null<MemberExpr>(Val: E))
8478 if (ME->isArrow() && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
8479 return getCanonicalDecl(D: ME->getMemberDecl());
8480 return nullptr;
8481}
8482
8483bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
8484 // Check test-expr for canonical form, save upper-bound UB, flags for
8485 // less/greater and for strict/non-strict comparison.
8486 // OpenMP [2.9] Canonical loop form. Test-expr may be one of the following:
8487 // var relational-op b
8488 // b relational-op var
8489 //
8490 bool IneqCondIsCanonical = SemaRef.getLangOpts().OpenMP >= 50;
8491 if (!S) {
8492 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
8493 << (IneqCondIsCanonical ? 1 : 0) << LCDecl;
8494 return true;
8495 }
8496 Condition = S;
8497 S = getExprAsWritten(E: S);
8498
8499 if (!CollapsedLoopVarDecls.empty()) {
8500 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8501 if (!FSEC.TraverseStmt(S)) {
8502 SourceRange Range = FSEC.getErrRange();
8503 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8504 << Range.getEnd() << 1 << FSEC.getForbiddenVar();
8505 return true;
8506 }
8507 }
8508
8509 SourceLocation CondLoc = S->getBeginLoc();
8510 auto &&CheckAndSetCond =
8511 [this, IneqCondIsCanonical](BinaryOperatorKind Opcode, const Expr *LHS,
8512 const Expr *RHS, SourceRange SR,
8513 SourceLocation OpLoc) -> std::optional<bool> {
8514 if (BinaryOperator::isRelationalOp(Opc: Opcode)) {
8515 if (getInitLCDecl(E: LHS) == LCDecl)
8516 return setUB(NewUB: const_cast<Expr *>(RHS),
8517 LessOp: (Opcode == BO_LT || Opcode == BO_LE),
8518 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
8519 if (getInitLCDecl(E: RHS) == LCDecl)
8520 return setUB(NewUB: const_cast<Expr *>(LHS),
8521 LessOp: (Opcode == BO_GT || Opcode == BO_GE),
8522 StrictOp: (Opcode == BO_LT || Opcode == BO_GT), SR, SL: OpLoc);
8523 } else if (IneqCondIsCanonical && Opcode == BO_NE) {
8524 return setUB(NewUB: const_cast<Expr *>(getInitLCDecl(E: LHS) == LCDecl ? RHS : LHS),
8525 /*LessOp=*/std::nullopt,
8526 /*StrictOp=*/true, SR, SL: OpLoc);
8527 }
8528 return std::nullopt;
8529 };
8530 std::optional<bool> Res;
8531 if (auto *RBO = dyn_cast<CXXRewrittenBinaryOperator>(Val: S)) {
8532 CXXRewrittenBinaryOperator::DecomposedForm DF = RBO->getDecomposedForm();
8533 Res = CheckAndSetCond(DF.Opcode, DF.LHS, DF.RHS, RBO->getSourceRange(),
8534 RBO->getOperatorLoc());
8535 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8536 Res = CheckAndSetCond(BO->getOpcode(), BO->getLHS(), BO->getRHS(),
8537 BO->getSourceRange(), BO->getOperatorLoc());
8538 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8539 if (CE->getNumArgs() == 2) {
8540 Res = CheckAndSetCond(
8541 BinaryOperator::getOverloadedOpcode(OO: CE->getOperator()), CE->getArg(Arg: 0),
8542 CE->getArg(Arg: 1), CE->getSourceRange(), CE->getOperatorLoc());
8543 }
8544 }
8545 if (Res)
8546 return *Res;
8547 if (dependent() || SemaRef.CurContext->isDependentContext())
8548 return false;
8549 SemaRef.Diag(Loc: CondLoc, DiagID: diag::err_omp_loop_not_canonical_cond)
8550 << (IneqCondIsCanonical ? 1 : 0) << S->getSourceRange() << LCDecl;
8551 return true;
8552}
8553
8554bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
8555 // RHS of canonical loop form increment can be:
8556 // var + incr
8557 // incr + var
8558 // var - incr
8559 //
8560 RHS = RHS->IgnoreParenImpCasts();
8561 if (auto *BO = dyn_cast<BinaryOperator>(Val: RHS)) {
8562 if (BO->isAdditiveOp()) {
8563 bool IsAdd = BO->getOpcode() == BO_Add;
8564 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8565 return setStep(NewStep: BO->getRHS(), Subtract: !IsAdd);
8566 if (IsAdd && getInitLCDecl(E: BO->getRHS()) == LCDecl)
8567 return setStep(NewStep: BO->getLHS(), /*Subtract=*/false);
8568 }
8569 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: RHS)) {
8570 bool IsAdd = CE->getOperator() == OO_Plus;
8571 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
8572 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8573 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: !IsAdd);
8574 if (IsAdd && getInitLCDecl(E: CE->getArg(Arg: 1)) == LCDecl)
8575 return setStep(NewStep: CE->getArg(Arg: 0), /*Subtract=*/false);
8576 }
8577 }
8578 if (dependent() || SemaRef.CurContext->isDependentContext())
8579 return false;
8580 SemaRef.Diag(Loc: RHS->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
8581 << RHS->getSourceRange() << LCDecl;
8582 return true;
8583}
8584
8585bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
8586 // Check incr-expr for canonical loop form and return true if it
8587 // does not conform.
8588 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
8589 // ++var
8590 // var++
8591 // --var
8592 // var--
8593 // var += incr
8594 // var -= incr
8595 // var = var + incr
8596 // var = incr + var
8597 // var = var - incr
8598 //
8599 if (!S) {
8600 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::err_omp_loop_not_canonical_incr) << LCDecl;
8601 return true;
8602 }
8603 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(Val: S))
8604 if (!ExprTemp->cleanupsHaveSideEffects())
8605 S = ExprTemp->getSubExpr();
8606
8607 if (!CollapsedLoopVarDecls.empty()) {
8608 ForSubExprChecker FSEC{CollapsedLoopVarDecls};
8609 if (!FSEC.TraverseStmt(S)) {
8610 SourceRange Range = FSEC.getErrRange();
8611 SemaRef.Diag(Loc: Range.getBegin(), DiagID: diag::err_omp_loop_bad_collapse_var)
8612 << Range.getEnd() << 2 << FSEC.getForbiddenVar();
8613 return true;
8614 }
8615 }
8616
8617 IncrementSrcRange = S->getSourceRange();
8618 S = S->IgnoreParens();
8619 if (auto *UO = dyn_cast<UnaryOperator>(Val: S)) {
8620 if (UO->isIncrementDecrementOp() &&
8621 getInitLCDecl(E: UO->getSubExpr()) == LCDecl)
8622 return setStep(NewStep: SemaRef
8623 .ActOnIntegerConstant(Loc: UO->getBeginLoc(),
8624 Val: (UO->isDecrementOp() ? -1 : 1))
8625 .get(),
8626 /*Subtract=*/false);
8627 } else if (auto *BO = dyn_cast<BinaryOperator>(Val: S)) {
8628 switch (BO->getOpcode()) {
8629 case BO_AddAssign:
8630 case BO_SubAssign:
8631 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8632 return setStep(NewStep: BO->getRHS(), Subtract: BO->getOpcode() == BO_SubAssign);
8633 break;
8634 case BO_Assign:
8635 if (getInitLCDecl(E: BO->getLHS()) == LCDecl)
8636 return checkAndSetIncRHS(RHS: BO->getRHS());
8637 break;
8638 default:
8639 break;
8640 }
8641 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(Val: S)) {
8642 switch (CE->getOperator()) {
8643 case OO_PlusPlus:
8644 case OO_MinusMinus:
8645 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8646 return setStep(NewStep: SemaRef
8647 .ActOnIntegerConstant(
8648 Loc: CE->getBeginLoc(),
8649 Val: ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
8650 .get(),
8651 /*Subtract=*/false);
8652 break;
8653 case OO_PlusEqual:
8654 case OO_MinusEqual:
8655 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8656 return setStep(NewStep: CE->getArg(Arg: 1), Subtract: CE->getOperator() == OO_MinusEqual);
8657 break;
8658 case OO_Equal:
8659 if (getInitLCDecl(E: CE->getArg(Arg: 0)) == LCDecl)
8660 return checkAndSetIncRHS(RHS: CE->getArg(Arg: 1));
8661 break;
8662 default:
8663 break;
8664 }
8665 }
8666 if (dependent() || SemaRef.CurContext->isDependentContext())
8667 return false;
8668 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_loop_not_canonical_incr)
8669 << S->getSourceRange() << LCDecl;
8670 return true;
8671}
8672
8673static ExprResult
8674tryBuildCapture(Sema &SemaRef, Expr *Capture,
8675 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
8676 StringRef Name = ".capture_expr.") {
8677 if (SemaRef.CurContext->isDependentContext() || Capture->containsErrors())
8678 return Capture;
8679 if (Capture->isEvaluatable(Ctx: SemaRef.Context, AllowSideEffects: Expr::SE_AllowSideEffects))
8680 return SemaRef.PerformImplicitConversion(From: Capture->IgnoreImpCasts(),
8681 ToType: Capture->getType(),
8682 Action: AssignmentAction::Converting,
8683 /*AllowExplicit=*/true);
8684 auto I = Captures.find(Key: Capture);
8685 if (I != Captures.end())
8686 return buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref&: I->second, Name);
8687 DeclRefExpr *Ref = nullptr;
8688 ExprResult Res = buildCapture(S&: SemaRef, CaptureExpr: Capture, Ref, Name);
8689 Captures[Capture] = Ref;
8690 return Res;
8691}
8692
8693/// Calculate number of iterations, transforming to unsigned, if number of
8694/// iterations may be larger than the original type.
8695static Expr *
8696calculateNumIters(Sema &SemaRef, Scope *S, SourceLocation DefaultLoc,
8697 Expr *Lower, Expr *Upper, Expr *Step, QualType LCTy,
8698 bool TestIsStrictOp, bool RoundToStep,
8699 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
8700 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
8701 if (!NewStep.isUsable())
8702 return nullptr;
8703 llvm::APSInt LRes, SRes;
8704 bool IsLowerConst = false, IsStepConst = false;
8705 if (std::optional<llvm::APSInt> Res =
8706 Lower->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8707 LRes = *Res;
8708 IsLowerConst = true;
8709 }
8710 if (std::optional<llvm::APSInt> Res =
8711 Step->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8712 SRes = *Res;
8713 IsStepConst = true;
8714 }
8715 bool NoNeedToConvert = IsLowerConst && !RoundToStep &&
8716 ((!TestIsStrictOp && LRes.isNonNegative()) ||
8717 (TestIsStrictOp && LRes.isStrictlyPositive()));
8718 bool NeedToReorganize = false;
8719 // Check if any subexpressions in Lower -Step [+ 1] lead to overflow.
8720 if (!NoNeedToConvert && IsLowerConst &&
8721 (TestIsStrictOp || (RoundToStep && IsStepConst))) {
8722 NoNeedToConvert = true;
8723 if (RoundToStep) {
8724 unsigned BW = LRes.getBitWidth() > SRes.getBitWidth()
8725 ? LRes.getBitWidth()
8726 : SRes.getBitWidth();
8727 LRes = LRes.extend(width: BW + 1);
8728 LRes.setIsSigned(true);
8729 SRes = SRes.extend(width: BW + 1);
8730 SRes.setIsSigned(true);
8731 LRes -= SRes;
8732 NoNeedToConvert = LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
8733 LRes = LRes.trunc(width: BW);
8734 }
8735 if (TestIsStrictOp) {
8736 unsigned BW = LRes.getBitWidth();
8737 LRes = LRes.extend(width: BW + 1);
8738 LRes.setIsSigned(true);
8739 ++LRes;
8740 NoNeedToConvert =
8741 NoNeedToConvert && LRes.trunc(width: BW).extend(width: BW + 1) == LRes;
8742 // truncate to the original bitwidth.
8743 LRes = LRes.trunc(width: BW);
8744 }
8745 NeedToReorganize = NoNeedToConvert;
8746 }
8747 llvm::APSInt URes;
8748 bool IsUpperConst = false;
8749 if (std::optional<llvm::APSInt> Res =
8750 Upper->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
8751 URes = *Res;
8752 IsUpperConst = true;
8753 }
8754 if (NoNeedToConvert && IsLowerConst && IsUpperConst &&
8755 (!RoundToStep || IsStepConst)) {
8756 unsigned BW = LRes.getBitWidth() > URes.getBitWidth() ? LRes.getBitWidth()
8757 : URes.getBitWidth();
8758 LRes = LRes.extend(width: BW + 1);
8759 LRes.setIsSigned(true);
8760 URes = URes.extend(width: BW + 1);
8761 URes.setIsSigned(true);
8762 URes -= LRes;
8763 NoNeedToConvert = URes.trunc(width: BW).extend(width: BW + 1) == URes;
8764 NeedToReorganize = NoNeedToConvert;
8765 }
8766 // If the boundaries are not constant or (Lower - Step [+ 1]) is not constant
8767 // or less than zero (Upper - (Lower - Step [+ 1]) may overflow) - promote to
8768 // unsigned.
8769 if ((!NoNeedToConvert || (LRes.isNegative() && !IsUpperConst)) &&
8770 !LCTy->isDependentType() && LCTy->isIntegerType()) {
8771 QualType LowerTy = Lower->getType();
8772 QualType UpperTy = Upper->getType();
8773 uint64_t LowerSize = SemaRef.Context.getTypeSize(T: LowerTy);
8774 uint64_t UpperSize = SemaRef.Context.getTypeSize(T: UpperTy);
8775 if ((LowerSize <= UpperSize && UpperTy->hasSignedIntegerRepresentation()) ||
8776 (LowerSize > UpperSize && LowerTy->hasSignedIntegerRepresentation())) {
8777 QualType CastType = SemaRef.Context.getIntTypeForBitwidth(
8778 DestWidth: LowerSize > UpperSize ? LowerSize : UpperSize, /*Signed=*/0);
8779 Upper =
8780 SemaRef
8781 .PerformImplicitConversion(
8782 From: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
8783 ToType: CastType, Action: AssignmentAction::Converting)
8784 .get();
8785 Lower = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get();
8786 NewStep = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: NewStep.get());
8787 }
8788 }
8789 if (!Lower || !Upper || NewStep.isInvalid())
8790 return nullptr;
8791
8792 ExprResult Diff;
8793 // If need to reorganize, then calculate the form as Upper - (Lower - Step [+
8794 // 1]).
8795 if (NeedToReorganize) {
8796 Diff = Lower;
8797
8798 if (RoundToStep) {
8799 // Lower - Step
8800 Diff =
8801 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8802 if (!Diff.isUsable())
8803 return nullptr;
8804 }
8805
8806 // Lower - Step [+ 1]
8807 if (TestIsStrictOp)
8808 Diff = SemaRef.BuildBinOp(
8809 S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(),
8810 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
8811 if (!Diff.isUsable())
8812 return nullptr;
8813
8814 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
8815 if (!Diff.isUsable())
8816 return nullptr;
8817
8818 // Upper - (Lower - Step [+ 1]).
8819 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Diff.get());
8820 if (!Diff.isUsable())
8821 return nullptr;
8822 } else {
8823 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Upper, RHSExpr: Lower);
8824
8825 if (!Diff.isUsable() && LCTy->getAsCXXRecordDecl()) {
8826 // BuildBinOp already emitted error, this one is to point user to upper
8827 // and lower bound, and to tell what is passed to 'operator-'.
8828 SemaRef.Diag(Loc: Upper->getBeginLoc(), DiagID: diag::err_omp_loop_diff_cxx)
8829 << Upper->getSourceRange() << Lower->getSourceRange();
8830 return nullptr;
8831 }
8832
8833 if (!Diff.isUsable())
8834 return nullptr;
8835
8836 // Upper - Lower [- 1]
8837 if (TestIsStrictOp)
8838 Diff = SemaRef.BuildBinOp(
8839 S, OpLoc: DefaultLoc, Opc: BO_Sub, LHSExpr: Diff.get(),
8840 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
8841 if (!Diff.isUsable())
8842 return nullptr;
8843
8844 if (RoundToStep) {
8845 // Upper - Lower [- 1] + Step
8846 Diff =
8847 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Add, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8848 if (!Diff.isUsable())
8849 return nullptr;
8850 }
8851 }
8852
8853 // Parentheses (for dumping/debugging purposes only).
8854 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
8855 if (!Diff.isUsable())
8856 return nullptr;
8857
8858 // (Upper - Lower [- 1] + Step) / Step or (Upper - Lower) / Step
8859 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Div, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
8860 if (!Diff.isUsable())
8861 return nullptr;
8862
8863 return Diff.get();
8864}
8865
8866/// Build the expression to calculate the number of iterations.
8867Expr *OpenMPIterationSpaceChecker::buildNumIterations(
8868 Scope *S, ArrayRef<LoopIterationSpace> ResultIterSpaces, bool LimitedType,
8869 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
8870 QualType VarType = LCDecl->getType().getNonReferenceType();
8871 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
8872 !SemaRef.getLangOpts().CPlusPlus)
8873 return nullptr;
8874 Expr *LBVal = LB;
8875 Expr *UBVal = UB;
8876 // OuterVar = (LB = TestIsLessOp.getValue() ? min(LB(MinVal), LB(MaxVal)) :
8877 // max(LB(MinVal), LB(MaxVal)))
8878 if (InitDependOnLC) {
8879 const LoopIterationSpace &IS = ResultIterSpaces[*InitDependOnLC - 1];
8880 if (!IS.MinValue || !IS.MaxValue)
8881 return nullptr;
8882 // OuterVar = Min
8883 ExprResult MinValue =
8884 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
8885 if (!MinValue.isUsable())
8886 return nullptr;
8887
8888 ExprResult LBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8889 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
8890 if (!LBMinVal.isUsable())
8891 return nullptr;
8892 // OuterVar = Min, LBVal
8893 LBMinVal =
8894 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMinVal.get(), RHSExpr: LBVal);
8895 if (!LBMinVal.isUsable())
8896 return nullptr;
8897 // (OuterVar = Min, LBVal)
8898 LBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMinVal.get());
8899 if (!LBMinVal.isUsable())
8900 return nullptr;
8901
8902 // OuterVar = Max
8903 ExprResult MaxValue =
8904 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
8905 if (!MaxValue.isUsable())
8906 return nullptr;
8907
8908 ExprResult LBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8909 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
8910 if (!LBMaxVal.isUsable())
8911 return nullptr;
8912 // OuterVar = Max, LBVal
8913 LBMaxVal =
8914 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: LBMaxVal.get(), RHSExpr: LBVal);
8915 if (!LBMaxVal.isUsable())
8916 return nullptr;
8917 // (OuterVar = Max, LBVal)
8918 LBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: LBMaxVal.get());
8919 if (!LBMaxVal.isUsable())
8920 return nullptr;
8921
8922 Expr *LBMin =
8923 tryBuildCapture(SemaRef, Capture: LBMinVal.get(), Captures, Name: ".lb_min").get();
8924 Expr *LBMax =
8925 tryBuildCapture(SemaRef, Capture: LBMaxVal.get(), Captures, Name: ".lb_max").get();
8926 if (!LBMin || !LBMax)
8927 return nullptr;
8928 // LB(MinVal) < LB(MaxVal)
8929 ExprResult MinLessMaxRes =
8930 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_LT, LHSExpr: LBMin, RHSExpr: LBMax);
8931 if (!MinLessMaxRes.isUsable())
8932 return nullptr;
8933 Expr *MinLessMax =
8934 tryBuildCapture(SemaRef, Capture: MinLessMaxRes.get(), Captures, Name: ".min_less_max")
8935 .get();
8936 if (!MinLessMax)
8937 return nullptr;
8938 if (*TestIsLessOp) {
8939 // LB(MinVal) < LB(MaxVal) ? LB(MinVal) : LB(MaxVal) - min(LB(MinVal),
8940 // LB(MaxVal))
8941 ExprResult MinLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
8942 CondExpr: MinLessMax, LHSExpr: LBMin, RHSExpr: LBMax);
8943 if (!MinLB.isUsable())
8944 return nullptr;
8945 LBVal = MinLB.get();
8946 } else {
8947 // LB(MinVal) < LB(MaxVal) ? LB(MaxVal) : LB(MinVal) - max(LB(MinVal),
8948 // LB(MaxVal))
8949 ExprResult MaxLB = SemaRef.ActOnConditionalOp(QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc,
8950 CondExpr: MinLessMax, LHSExpr: LBMax, RHSExpr: LBMin);
8951 if (!MaxLB.isUsable())
8952 return nullptr;
8953 LBVal = MaxLB.get();
8954 }
8955 // OuterVar = LB
8956 LBMinVal =
8957 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign, LHSExpr: IS.CounterVar, RHSExpr: LBVal);
8958 if (!LBMinVal.isUsable())
8959 return nullptr;
8960 LBVal = LBMinVal.get();
8961 }
8962 // UB = TestIsLessOp.getValue() ? max(UB(MinVal), UB(MaxVal)) :
8963 // min(UB(MinVal), UB(MaxVal))
8964 if (CondDependOnLC) {
8965 const LoopIterationSpace &IS = ResultIterSpaces[*CondDependOnLC - 1];
8966 if (!IS.MinValue || !IS.MaxValue)
8967 return nullptr;
8968 // OuterVar = Min
8969 ExprResult MinValue =
8970 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MinValue);
8971 if (!MinValue.isUsable())
8972 return nullptr;
8973
8974 ExprResult UBMinVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8975 LHSExpr: IS.CounterVar, RHSExpr: MinValue.get());
8976 if (!UBMinVal.isUsable())
8977 return nullptr;
8978 // OuterVar = Min, UBVal
8979 UBMinVal =
8980 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMinVal.get(), RHSExpr: UBVal);
8981 if (!UBMinVal.isUsable())
8982 return nullptr;
8983 // (OuterVar = Min, UBVal)
8984 UBMinVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMinVal.get());
8985 if (!UBMinVal.isUsable())
8986 return nullptr;
8987
8988 // OuterVar = Max
8989 ExprResult MaxValue =
8990 SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: IS.MaxValue);
8991 if (!MaxValue.isUsable())
8992 return nullptr;
8993
8994 ExprResult UBMaxVal = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Assign,
8995 LHSExpr: IS.CounterVar, RHSExpr: MaxValue.get());
8996 if (!UBMaxVal.isUsable())
8997 return nullptr;
8998 // OuterVar = Max, UBVal
8999 UBMaxVal =
9000 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Comma, LHSExpr: UBMaxVal.get(), RHSExpr: UBVal);
9001 if (!UBMaxVal.isUsable())
9002 return nullptr;
9003 // (OuterVar = Max, UBVal)
9004 UBMaxVal = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: UBMaxVal.get());
9005 if (!UBMaxVal.isUsable())
9006 return nullptr;
9007
9008 Expr *UBMin =
9009 tryBuildCapture(SemaRef, Capture: UBMinVal.get(), Captures, Name: ".ub_min").get();
9010 Expr *UBMax =
9011 tryBuildCapture(SemaRef, Capture: UBMaxVal.get(), Captures, Name: ".ub_max").get();
9012 if (!UBMin || !UBMax)
9013 return nullptr;
9014 // UB(MinVal) > UB(MaxVal)
9015 ExprResult MinGreaterMaxRes =
9016 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_GT, LHSExpr: UBMin, RHSExpr: UBMax);
9017 if (!MinGreaterMaxRes.isUsable())
9018 return nullptr;
9019 Expr *MinGreaterMax = tryBuildCapture(SemaRef, Capture: MinGreaterMaxRes.get(),
9020 Captures, Name: ".min_greater_max")
9021 .get();
9022 if (!MinGreaterMax)
9023 return nullptr;
9024 if (*TestIsLessOp) {
9025 // UB(MinVal) > UB(MaxVal) ? UB(MinVal) : UB(MaxVal) - max(UB(MinVal),
9026 // UB(MaxVal))
9027 ExprResult MaxUB = SemaRef.ActOnConditionalOp(
9028 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMin, RHSExpr: UBMax);
9029 if (!MaxUB.isUsable())
9030 return nullptr;
9031 UBVal = MaxUB.get();
9032 } else {
9033 // UB(MinVal) > UB(MaxVal) ? UB(MaxVal) : UB(MinVal) - min(UB(MinVal),
9034 // UB(MaxVal))
9035 ExprResult MinUB = SemaRef.ActOnConditionalOp(
9036 QuestionLoc: DefaultLoc, ColonLoc: DefaultLoc, CondExpr: MinGreaterMax, LHSExpr: UBMax, RHSExpr: UBMin);
9037 if (!MinUB.isUsable())
9038 return nullptr;
9039 UBVal = MinUB.get();
9040 }
9041 }
9042 Expr *UBExpr = *TestIsLessOp ? UBVal : LBVal;
9043 Expr *LBExpr = *TestIsLessOp ? LBVal : UBVal;
9044 Expr *Upper = tryBuildCapture(SemaRef, Capture: UBExpr, Captures, Name: ".upper").get();
9045 Expr *Lower = tryBuildCapture(SemaRef, Capture: LBExpr, Captures, Name: ".lower").get();
9046 if (!Upper || !Lower)
9047 return nullptr;
9048
9049 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper,
9050 Step, LCTy: VarType, TestIsStrictOp,
9051 /*RoundToStep=*/true, Captures);
9052 if (!Diff.isUsable())
9053 return nullptr;
9054
9055 // OpenMP runtime requires 32-bit or 64-bit loop variables.
9056 QualType Type = Diff.get()->getType();
9057 ASTContext &C = SemaRef.Context;
9058 bool UseVarType = VarType->hasIntegerRepresentation() &&
9059 C.getTypeSize(T: Type) > C.getTypeSize(T: VarType);
9060 if (!Type->isIntegerType() || UseVarType) {
9061 unsigned NewSize =
9062 UseVarType ? C.getTypeSize(T: VarType) : C.getTypeSize(T: Type);
9063 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
9064 : Type->hasSignedIntegerRepresentation();
9065 Type = C.getIntTypeForBitwidth(DestWidth: NewSize, Signed: IsSigned);
9066 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: Type)) {
9067 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: Type,
9068 Action: AssignmentAction::Converting,
9069 /*AllowExplicit=*/true);
9070 if (!Diff.isUsable())
9071 return nullptr;
9072 }
9073 }
9074 if (LimitedType) {
9075 unsigned NewSize = (C.getTypeSize(T: Type) > 32) ? 64 : 32;
9076 if (NewSize != C.getTypeSize(T: Type)) {
9077 if (NewSize < C.getTypeSize(T: Type)) {
9078 assert(NewSize == 64 && "incorrect loop var size");
9079 SemaRef.Diag(Loc: DefaultLoc, DiagID: diag::warn_omp_loop_64_bit_var)
9080 << InitSrcRange << ConditionSrcRange;
9081 }
9082 QualType NewType = C.getIntTypeForBitwidth(
9083 DestWidth: NewSize, Signed: Type->hasSignedIntegerRepresentation() ||
9084 C.getTypeSize(T: Type) < NewSize);
9085 if (!SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: NewType)) {
9086 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: NewType,
9087 Action: AssignmentAction::Converting,
9088 /*AllowExplicit=*/true);
9089 if (!Diff.isUsable())
9090 return nullptr;
9091 }
9092 }
9093 }
9094
9095 return Diff.get();
9096}
9097
9098std::pair<Expr *, Expr *> OpenMPIterationSpaceChecker::buildMinMaxValues(
9099 Scope *S, llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
9100 // Do not build for iterators, they cannot be used in non-rectangular loop
9101 // nests.
9102 if (LCDecl->getType()->isRecordType())
9103 return std::make_pair(x: nullptr, y: nullptr);
9104 // If we subtract, the min is in the condition, otherwise the min is in the
9105 // init value.
9106 Expr *MinExpr = nullptr;
9107 Expr *MaxExpr = nullptr;
9108 Expr *LBExpr = *TestIsLessOp ? LB : UB;
9109 Expr *UBExpr = *TestIsLessOp ? UB : LB;
9110 bool LBNonRect =
9111 *TestIsLessOp ? InitDependOnLC.has_value() : CondDependOnLC.has_value();
9112 bool UBNonRect =
9113 *TestIsLessOp ? CondDependOnLC.has_value() : InitDependOnLC.has_value();
9114 Expr *Lower =
9115 LBNonRect ? LBExpr : tryBuildCapture(SemaRef, Capture: LBExpr, Captures).get();
9116 Expr *Upper =
9117 UBNonRect ? UBExpr : tryBuildCapture(SemaRef, Capture: UBExpr, Captures).get();
9118 if (!Upper || !Lower)
9119 return std::make_pair(x: nullptr, y: nullptr);
9120
9121 if (*TestIsLessOp)
9122 MinExpr = Lower;
9123 else
9124 MaxExpr = Upper;
9125
9126 // Build minimum/maximum value based on number of iterations.
9127 QualType VarType = LCDecl->getType().getNonReferenceType();
9128
9129 ExprResult Diff = calculateNumIters(SemaRef, S, DefaultLoc, Lower, Upper,
9130 Step, LCTy: VarType, TestIsStrictOp,
9131 /*RoundToStep=*/false, Captures);
9132 if (!Diff.isUsable())
9133 return std::make_pair(x: nullptr, y: nullptr);
9134
9135 // ((Upper - Lower [- 1]) / Step) * Step
9136 // Parentheses (for dumping/debugging purposes only).
9137 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9138 if (!Diff.isUsable())
9139 return std::make_pair(x: nullptr, y: nullptr);
9140
9141 ExprResult NewStep = tryBuildCapture(SemaRef, Capture: Step, Captures, Name: ".new_step");
9142 if (!NewStep.isUsable())
9143 return std::make_pair(x: nullptr, y: nullptr);
9144 Diff = SemaRef.BuildBinOp(S, OpLoc: DefaultLoc, Opc: BO_Mul, LHSExpr: Diff.get(), RHSExpr: NewStep.get());
9145 if (!Diff.isUsable())
9146 return std::make_pair(x: nullptr, y: nullptr);
9147
9148 // Parentheses (for dumping/debugging purposes only).
9149 Diff = SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Diff.get());
9150 if (!Diff.isUsable())
9151 return std::make_pair(x: nullptr, y: nullptr);
9152
9153 // Convert to the ptrdiff_t, if original type is pointer.
9154 if (VarType->isAnyPointerType() &&
9155 !SemaRef.Context.hasSameType(
9156 T1: Diff.get()->getType(),
9157 T2: SemaRef.Context.getUnsignedPointerDiffType())) {
9158 Diff = SemaRef.PerformImplicitConversion(
9159 From: Diff.get(), ToType: SemaRef.Context.getUnsignedPointerDiffType(),
9160 Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
9161 }
9162 if (!Diff.isUsable())
9163 return std::make_pair(x: nullptr, y: nullptr);
9164
9165 if (*TestIsLessOp) {
9166 // MinExpr = Lower;
9167 // MaxExpr = Lower + (((Upper - Lower [- 1]) / Step) * Step)
9168 Diff = SemaRef.BuildBinOp(
9169 S, OpLoc: DefaultLoc, Opc: BO_Add,
9170 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Lower).get(),
9171 RHSExpr: Diff.get());
9172 if (!Diff.isUsable())
9173 return std::make_pair(x: nullptr, y: nullptr);
9174 } else {
9175 // MaxExpr = Upper;
9176 // MinExpr = Upper - (((Upper - Lower [- 1]) / Step) * Step)
9177 Diff = SemaRef.BuildBinOp(
9178 S, OpLoc: DefaultLoc, Opc: BO_Sub,
9179 LHSExpr: SemaRef.ActOnParenExpr(L: DefaultLoc, R: DefaultLoc, E: Upper).get(),
9180 RHSExpr: Diff.get());
9181 if (!Diff.isUsable())
9182 return std::make_pair(x: nullptr, y: nullptr);
9183 }
9184
9185 // Convert to the original type.
9186 if (SemaRef.Context.hasSameType(T1: Diff.get()->getType(), T2: VarType))
9187 Diff = SemaRef.PerformImplicitConversion(From: Diff.get(), ToType: VarType,
9188 Action: AssignmentAction::Converting,
9189 /*AllowExplicit=*/true);
9190 if (!Diff.isUsable())
9191 return std::make_pair(x: nullptr, y: nullptr);
9192
9193 Sema::TentativeAnalysisScope Trap(SemaRef);
9194 Diff = SemaRef.ActOnFinishFullExpr(Expr: Diff.get(), /*DiscardedValue=*/false);
9195 if (!Diff.isUsable())
9196 return std::make_pair(x: nullptr, y: nullptr);
9197
9198 if (*TestIsLessOp)
9199 MaxExpr = Diff.get();
9200 else
9201 MinExpr = Diff.get();
9202
9203 return std::make_pair(x&: MinExpr, y&: MaxExpr);
9204}
9205
9206Expr *OpenMPIterationSpaceChecker::buildFinalCondition(Scope *S) const {
9207 if (InitDependOnLC || CondDependOnLC)
9208 return Condition;
9209 return nullptr;
9210}
9211
9212Expr *OpenMPIterationSpaceChecker::buildPreCond(
9213 Scope *S, Expr *Cond,
9214 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
9215 // Do not build a precondition when the condition/initialization is dependent
9216 // to prevent pessimistic early loop exit.
9217 // TODO: this can be improved by calculating min/max values but not sure that
9218 // it will be very effective.
9219 if (CondDependOnLC || InitDependOnLC)
9220 return SemaRef
9221 .PerformImplicitConversion(
9222 From: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get(),
9223 ToType: SemaRef.Context.BoolTy, /*Action=*/AssignmentAction::Casting,
9224 /*AllowExplicit=*/true)
9225 .get();
9226
9227 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
9228 Sema::TentativeAnalysisScope Trap(SemaRef);
9229
9230 ExprResult NewLB = tryBuildCapture(SemaRef, Capture: LB, Captures);
9231 ExprResult NewUB = tryBuildCapture(SemaRef, Capture: UB, Captures);
9232 if (!NewLB.isUsable() || !NewUB.isUsable())
9233 return nullptr;
9234
9235 ExprResult CondExpr =
9236 SemaRef.BuildBinOp(S, OpLoc: DefaultLoc,
9237 Opc: *TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
9238 : (TestIsStrictOp ? BO_GT : BO_GE),
9239 LHSExpr: NewLB.get(), RHSExpr: NewUB.get());
9240 if (CondExpr.isUsable()) {
9241 if (!SemaRef.Context.hasSameUnqualifiedType(T1: CondExpr.get()->getType(),
9242 T2: SemaRef.Context.BoolTy))
9243 CondExpr = SemaRef.PerformImplicitConversion(
9244 From: CondExpr.get(), ToType: SemaRef.Context.BoolTy,
9245 /*Action=*/AssignmentAction::Casting,
9246 /*AllowExplicit=*/true);
9247 }
9248
9249 // Otherwise use original loop condition and evaluate it in runtime.
9250 return CondExpr.isUsable() ? CondExpr.get() : Cond;
9251}
9252
9253/// Build reference expression to the counter be used for codegen.
9254DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
9255 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9256 DSAStackTy &DSA) const {
9257 auto *VD = dyn_cast<VarDecl>(Val: LCDecl);
9258 if (!VD) {
9259 VD = SemaRef.OpenMP().isOpenMPCapturedDecl(D: LCDecl);
9260 DeclRefExpr *Ref = buildDeclRefExpr(
9261 S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(), Loc: DefaultLoc);
9262 const DSAStackTy::DSAVarData Data =
9263 DSA.getTopDSA(D: LCDecl, /*FromParent=*/false);
9264 // If the loop control decl is explicitly marked as private, do not mark it
9265 // as captured again.
9266 if (!isOpenMPPrivate(Kind: Data.CKind) || !Data.RefExpr)
9267 Captures.insert(KV: std::make_pair(x: LCRef, y&: Ref));
9268 return Ref;
9269 }
9270 return cast<DeclRefExpr>(Val: LCRef);
9271}
9272
9273Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
9274 if (LCDecl && !LCDecl->isInvalidDecl()) {
9275 QualType Type = LCDecl->getType().getNonReferenceType();
9276 VarDecl *PrivateVar = buildVarDecl(
9277 SemaRef, Loc: DefaultLoc, Type, Name: LCDecl->getName(),
9278 Attrs: LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
9279 OrigRef: isa<VarDecl>(Val: LCDecl)
9280 ? buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: LCDecl), Ty: Type, Loc: DefaultLoc)
9281 : nullptr);
9282 if (PrivateVar->isInvalidDecl())
9283 return nullptr;
9284 return buildDeclRefExpr(S&: SemaRef, D: PrivateVar, Ty: Type, Loc: DefaultLoc);
9285 }
9286 return nullptr;
9287}
9288
9289/// Build initialization of the counter to be used for codegen.
9290Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
9291
9292/// Build step of the counter be used for codegen.
9293Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
9294
9295Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
9296 Scope *S, Expr *Counter,
9297 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
9298 Expr *Inc, OverloadedOperatorKind OOK) {
9299 Expr *Cnt = SemaRef.DefaultLvalueConversion(E: Counter).get();
9300 if (!Cnt)
9301 return nullptr;
9302 if (Inc) {
9303 assert((OOK == OO_Plus || OOK == OO_Minus) &&
9304 "Expected only + or - operations for depend clauses.");
9305 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
9306 Cnt = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BOK, LHSExpr: Cnt, RHSExpr: Inc).get();
9307 if (!Cnt)
9308 return nullptr;
9309 }
9310 QualType VarType = LCDecl->getType().getNonReferenceType();
9311 if (!VarType->isIntegerType() && !VarType->isPointerType() &&
9312 !SemaRef.getLangOpts().CPlusPlus)
9313 return nullptr;
9314 // Upper - Lower
9315 Expr *Upper =
9316 *TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, Capture: LB, Captures).get();
9317 Expr *Lower =
9318 *TestIsLessOp ? tryBuildCapture(SemaRef, Capture: LB, Captures).get() : Cnt;
9319 if (!Upper || !Lower)
9320 return nullptr;
9321
9322 ExprResult Diff = calculateNumIters(
9323 SemaRef, S, DefaultLoc, Lower, Upper, Step, LCTy: VarType,
9324 /*TestIsStrictOp=*/false, /*RoundToStep=*/false, Captures);
9325 if (!Diff.isUsable())
9326 return nullptr;
9327
9328 return Diff.get();
9329}
9330} // namespace
9331
9332void SemaOpenMP::ActOnOpenMPLoopInitialization(SourceLocation ForLoc,
9333 Stmt *Init) {
9334 assert(getLangOpts().OpenMP && "OpenMP is not active.");
9335 assert(Init && "Expected loop in canonical form.");
9336 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
9337 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9338 if (AssociatedLoops == 0 || !isOpenMPLoopDirective(DKind))
9339 return;
9340
9341 DSAStack->loopStart();
9342 llvm::SmallPtrSet<const Decl *, 1> EmptyDeclSet;
9343 OpenMPIterationSpaceChecker ISC(SemaRef, /*SupportsNonRectangular=*/true,
9344 *DSAStack, ForLoc, EmptyDeclSet);
9345 if (!ISC.checkAndSetInit(S: Init, /*EmitDiags=*/false)) {
9346 if (ValueDecl *D = ISC.getLoopDecl()) {
9347 auto *VD = dyn_cast<VarDecl>(Val: D);
9348 DeclRefExpr *PrivateRef = nullptr;
9349 if (!VD) {
9350 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
9351 VD = Private;
9352 } else {
9353 PrivateRef = buildCapture(S&: SemaRef, D, CaptureExpr: ISC.getLoopDeclRefExpr(),
9354 /*WithInit=*/false);
9355 VD = cast<VarDecl>(Val: PrivateRef->getDecl());
9356 }
9357 }
9358 DSAStack->addLoopControlVariable(D, Capture: VD);
9359 const Decl *LD = DSAStack->getPossiblyLoopCounter();
9360 if (LD != D->getCanonicalDecl()) {
9361 DSAStack->resetPossibleLoopCounter();
9362 if (auto *Var = dyn_cast_or_null<VarDecl>(Val: LD))
9363 SemaRef.MarkDeclarationsReferencedInExpr(E: buildDeclRefExpr(
9364 S&: SemaRef, D: const_cast<VarDecl *>(Var),
9365 Ty: Var->getType().getNonLValueExprType(Context: getASTContext()), Loc: ForLoc,
9366 /*RefersToCapture=*/true));
9367 }
9368 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables
9369 // Referenced in a Construct, C/C++]. The loop iteration variable in the
9370 // associated for-loop of a simd construct with just one associated
9371 // for-loop may be listed in a linear clause with a constant-linear-step
9372 // that is the increment of the associated for-loop. The loop iteration
9373 // variable(s) in the associated for-loop(s) of a for or parallel for
9374 // construct may be listed in a private or lastprivate clause.
9375 DSAStackTy::DSAVarData DVar =
9376 DSAStack->getTopDSA(D, /*FromParent=*/false);
9377 // If LoopVarRefExpr is nullptr it means the corresponding loop variable
9378 // is declared in the loop and it is predetermined as a private.
9379 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
9380 OpenMPClauseKind PredeterminedCKind =
9381 isOpenMPSimdDirective(DKind)
9382 ? (DSAStack->hasMutipleLoops() ? OMPC_lastprivate : OMPC_linear)
9383 : OMPC_private;
9384 auto IsOpenMPTaskloopDirective = [](OpenMPDirectiveKind DK) {
9385 return getLeafConstructsOrSelf(D: DK).back() == OMPD_taskloop;
9386 };
9387 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
9388 DVar.CKind != PredeterminedCKind && DVar.RefExpr &&
9389 (getLangOpts().OpenMP <= 45 ||
9390 (DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_private))) ||
9391 ((isOpenMPWorksharingDirective(DKind) ||
9392 IsOpenMPTaskloopDirective(DKind) ||
9393 isOpenMPDistributeDirective(DKind)) &&
9394 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
9395 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
9396 (DVar.CKind != OMPC_private || DVar.RefExpr)) {
9397 unsigned OMPVersion = getLangOpts().OpenMP;
9398 Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_var_dsa)
9399 << getOpenMPClauseNameForDiag(C: DVar.CKind)
9400 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion)
9401 << getOpenMPClauseNameForDiag(C: PredeterminedCKind);
9402 if (DVar.RefExpr == nullptr)
9403 DVar.CKind = PredeterminedCKind;
9404 reportOriginalDsa(SemaRef, DSAStack, D, DVar, /*IsLoopIterVar=*/true);
9405 } else if (LoopDeclRefExpr) {
9406 // Make the loop iteration variable private (for worksharing
9407 // constructs), linear (for simd directives with the only one
9408 // associated loop) or lastprivate (for simd directives with several
9409 // collapsed or ordered loops).
9410 if (DVar.CKind == OMPC_unknown)
9411 DSAStack->addDSA(D, E: LoopDeclRefExpr, A: PredeterminedCKind, PrivateCopy: PrivateRef);
9412 }
9413 }
9414 }
9415 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
9416}
9417
9418namespace {
9419// Utility for OpenMP doacross clause kind
9420class OMPDoacrossKind {
9421public:
9422 bool isSource(const OMPDoacrossClause *C) {
9423 return C->getDependenceType() == OMPC_DOACROSS_source ||
9424 C->getDependenceType() == OMPC_DOACROSS_source_omp_cur_iteration;
9425 }
9426 bool isSink(const OMPDoacrossClause *C) {
9427 return C->getDependenceType() == OMPC_DOACROSS_sink;
9428 }
9429 bool isSinkIter(const OMPDoacrossClause *C) {
9430 return C->getDependenceType() == OMPC_DOACROSS_sink_omp_cur_iteration;
9431 }
9432};
9433} // namespace
9434/// Called on a for stmt to check and extract its iteration space
9435/// for further processing (such as collapsing).
9436static bool checkOpenMPIterationSpace(
9437 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
9438 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
9439 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
9440 Expr *OrderedLoopCountExpr,
9441 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9442 llvm::MutableArrayRef<LoopIterationSpace> ResultIterSpaces,
9443 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
9444 const llvm::SmallPtrSetImpl<const Decl *> &CollapsedLoopVarDecls) {
9445 bool SupportsNonRectangular = !isOpenMPLoopTransformationDirective(DKind);
9446 // OpenMP [2.9.1, Canonical Loop Form]
9447 // for (init-expr; test-expr; incr-expr) structured-block
9448 // for (range-decl: range-expr) structured-block
9449 if (auto *CanonLoop = dyn_cast_or_null<OMPCanonicalLoop>(Val: S))
9450 S = CanonLoop->getLoopStmt();
9451 auto *For = dyn_cast_or_null<ForStmt>(Val: S);
9452 auto *CXXFor = dyn_cast_or_null<CXXForRangeStmt>(Val: S);
9453 // Ranged for is supported only in OpenMP 5.0.
9454 if (!For && (SemaRef.LangOpts.OpenMP <= 45 || !CXXFor)) {
9455 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
9456 SemaRef.Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_not_for)
9457 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
9458 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion) << TotalNestedLoopCount
9459 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
9460 if (TotalNestedLoopCount > 1) {
9461 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
9462 SemaRef.Diag(Loc: DSA.getConstructLoc(),
9463 DiagID: diag::note_omp_collapse_ordered_expr)
9464 << 2 << CollapseLoopCountExpr->getSourceRange()
9465 << OrderedLoopCountExpr->getSourceRange();
9466 else if (CollapseLoopCountExpr)
9467 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
9468 DiagID: diag::note_omp_collapse_ordered_expr)
9469 << 0 << CollapseLoopCountExpr->getSourceRange();
9470 else if (OrderedLoopCountExpr)
9471 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
9472 DiagID: diag::note_omp_collapse_ordered_expr)
9473 << 1 << OrderedLoopCountExpr->getSourceRange();
9474 }
9475 return true;
9476 }
9477 assert(((For && For->getBody()) || (CXXFor && CXXFor->getBody())) &&
9478 "No loop body.");
9479 // Postpone analysis in dependent contexts for ranged for loops.
9480 if (CXXFor && SemaRef.CurContext->isDependentContext())
9481 return false;
9482
9483 OpenMPIterationSpaceChecker ISC(SemaRef, SupportsNonRectangular, DSA,
9484 For ? For->getForLoc() : CXXFor->getForLoc(),
9485 CollapsedLoopVarDecls);
9486
9487 // Check init.
9488 Stmt *Init = For ? For->getInit() : CXXFor->getBeginStmt();
9489 if (ISC.checkAndSetInit(S: Init))
9490 return true;
9491
9492 bool HasErrors = false;
9493
9494 // Check loop variable's type.
9495 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
9496 // OpenMP [2.6, Canonical Loop Form]
9497 // Var is one of the following:
9498 // A variable of signed or unsigned integer type.
9499 // For C++, a variable of a random access iterator type.
9500 // For C, a variable of a pointer type.
9501 QualType VarType = LCDecl->getType().getNonReferenceType();
9502 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
9503 !VarType->isPointerType() &&
9504 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
9505 SemaRef.Diag(Loc: Init->getBeginLoc(), DiagID: diag::err_omp_loop_variable_type)
9506 << SemaRef.getLangOpts().CPlusPlus;
9507 HasErrors = true;
9508 }
9509
9510 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
9511 // a Construct
9512 // The loop iteration variable(s) in the associated for-loop(s) of a for or
9513 // parallel for construct is (are) private.
9514 // The loop iteration variable in the associated for-loop of a simd
9515 // construct with just one associated for-loop is linear with a
9516 // constant-linear-step that is the increment of the associated for-loop.
9517 // Exclude loop var from the list of variables with implicitly defined data
9518 // sharing attributes.
9519 VarsWithImplicitDSA.erase(Val: LCDecl);
9520
9521 assert((isOpenMPLoopDirective(DKind) ||
9522 isOpenMPCanonicalLoopSequenceTransformationDirective(DKind)) &&
9523 "DSA for non-loop vars");
9524
9525 // Check test-expr.
9526 HasErrors |= ISC.checkAndSetCond(S: For ? For->getCond() : CXXFor->getCond());
9527
9528 // Check incr-expr.
9529 HasErrors |= ISC.checkAndSetInc(S: For ? For->getInc() : CXXFor->getInc());
9530 }
9531
9532 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
9533 return HasErrors;
9534
9535 // Build the loop's iteration space representation.
9536 ResultIterSpaces[CurrentNestedLoopCount].PreCond = ISC.buildPreCond(
9537 S: DSA.getCurScope(), Cond: For ? For->getCond() : CXXFor->getCond(), Captures);
9538 ResultIterSpaces[CurrentNestedLoopCount].NumIterations =
9539 ISC.buildNumIterations(S: DSA.getCurScope(), ResultIterSpaces,
9540 LimitedType: (isOpenMPWorksharingDirective(DKind) ||
9541 isOpenMPGenericLoopDirective(DKind) ||
9542 isOpenMPTaskLoopDirective(DKind) ||
9543 isOpenMPDistributeDirective(DKind) ||
9544 isOpenMPLoopTransformationDirective(DKind)),
9545 Captures);
9546 ResultIterSpaces[CurrentNestedLoopCount].CounterVar =
9547 ISC.buildCounterVar(Captures, DSA);
9548 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar =
9549 ISC.buildPrivateCounterVar();
9550 ResultIterSpaces[CurrentNestedLoopCount].CounterInit = ISC.buildCounterInit();
9551 ResultIterSpaces[CurrentNestedLoopCount].CounterStep = ISC.buildCounterStep();
9552 ResultIterSpaces[CurrentNestedLoopCount].InitSrcRange = ISC.getInitSrcRange();
9553 ResultIterSpaces[CurrentNestedLoopCount].CondSrcRange =
9554 ISC.getConditionSrcRange();
9555 ResultIterSpaces[CurrentNestedLoopCount].IncSrcRange =
9556 ISC.getIncrementSrcRange();
9557 ResultIterSpaces[CurrentNestedLoopCount].Subtract = ISC.shouldSubtractStep();
9558 ResultIterSpaces[CurrentNestedLoopCount].IsStrictCompare =
9559 ISC.isStrictTestOp();
9560 std::tie(args&: ResultIterSpaces[CurrentNestedLoopCount].MinValue,
9561 args&: ResultIterSpaces[CurrentNestedLoopCount].MaxValue) =
9562 ISC.buildMinMaxValues(S: DSA.getCurScope(), Captures);
9563 ResultIterSpaces[CurrentNestedLoopCount].FinalCondition =
9564 ISC.buildFinalCondition(S: DSA.getCurScope());
9565 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularLB =
9566 ISC.doesInitDependOnLC();
9567 ResultIterSpaces[CurrentNestedLoopCount].IsNonRectangularUB =
9568 ISC.doesCondDependOnLC();
9569 ResultIterSpaces[CurrentNestedLoopCount].LoopDependentIdx =
9570 ISC.getLoopDependentIdx();
9571
9572 HasErrors |=
9573 (ResultIterSpaces[CurrentNestedLoopCount].PreCond == nullptr ||
9574 ResultIterSpaces[CurrentNestedLoopCount].NumIterations == nullptr ||
9575 ResultIterSpaces[CurrentNestedLoopCount].CounterVar == nullptr ||
9576 ResultIterSpaces[CurrentNestedLoopCount].PrivateCounterVar == nullptr ||
9577 ResultIterSpaces[CurrentNestedLoopCount].CounterInit == nullptr ||
9578 ResultIterSpaces[CurrentNestedLoopCount].CounterStep == nullptr);
9579 if (!HasErrors && DSA.isOrderedRegion()) {
9580 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
9581 if (CurrentNestedLoopCount <
9582 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
9583 DSA.getOrderedRegionParam().second->setLoopNumIterations(
9584 NumLoop: CurrentNestedLoopCount,
9585 NumIterations: ResultIterSpaces[CurrentNestedLoopCount].NumIterations);
9586 DSA.getOrderedRegionParam().second->setLoopCounter(
9587 NumLoop: CurrentNestedLoopCount,
9588 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar);
9589 }
9590 }
9591 for (auto &Pair : DSA.getDoacrossDependClauses()) {
9592 auto *DependC = dyn_cast<OMPDependClause>(Val: Pair.first);
9593 auto *DoacrossC = dyn_cast<OMPDoacrossClause>(Val: Pair.first);
9594 unsigned NumLoops =
9595 DependC ? DependC->getNumLoops() : DoacrossC->getNumLoops();
9596 if (CurrentNestedLoopCount >= NumLoops) {
9597 // Erroneous case - clause has some problems.
9598 continue;
9599 }
9600 if (DependC && DependC->getDependencyKind() == OMPC_DEPEND_sink &&
9601 Pair.second.size() <= CurrentNestedLoopCount) {
9602 // Erroneous case - clause has some problems.
9603 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
9604 continue;
9605 }
9606 OMPDoacrossKind ODK;
9607 if (DoacrossC && ODK.isSink(C: DoacrossC) &&
9608 Pair.second.size() <= CurrentNestedLoopCount) {
9609 // Erroneous case - clause has some problems.
9610 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: nullptr);
9611 continue;
9612 }
9613 Expr *CntValue;
9614 SourceLocation DepLoc =
9615 DependC ? DependC->getDependencyLoc() : DoacrossC->getDependenceLoc();
9616 if ((DependC && DependC->getDependencyKind() == OMPC_DEPEND_source) ||
9617 (DoacrossC && ODK.isSource(C: DoacrossC)))
9618 CntValue = ISC.buildOrderedLoopData(
9619 S: DSA.getCurScope(),
9620 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9621 Loc: DepLoc);
9622 else if (DoacrossC && ODK.isSinkIter(C: DoacrossC)) {
9623 Expr *Cnt = SemaRef
9624 .DefaultLvalueConversion(
9625 E: ResultIterSpaces[CurrentNestedLoopCount].CounterVar)
9626 .get();
9627 if (!Cnt)
9628 continue;
9629 // build CounterVar - 1
9630 Expr *Inc =
9631 SemaRef.ActOnIntegerConstant(Loc: DoacrossC->getColonLoc(), /*Val=*/1)
9632 .get();
9633 CntValue = ISC.buildOrderedLoopData(
9634 S: DSA.getCurScope(),
9635 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9636 Loc: DepLoc, Inc, OOK: clang::OO_Minus);
9637 } else
9638 CntValue = ISC.buildOrderedLoopData(
9639 S: DSA.getCurScope(),
9640 Counter: ResultIterSpaces[CurrentNestedLoopCount].CounterVar, Captures,
9641 Loc: DepLoc, Inc: Pair.second[CurrentNestedLoopCount].first,
9642 OOK: Pair.second[CurrentNestedLoopCount].second);
9643 if (DependC)
9644 DependC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
9645 else
9646 DoacrossC->setLoopData(NumLoop: CurrentNestedLoopCount, Cnt: CntValue);
9647 }
9648 }
9649
9650 return HasErrors;
9651}
9652
9653/// Build 'VarRef = Start.
9654static ExprResult
9655buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
9656 ExprResult Start, bool IsNonRectangularLB,
9657 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9658 // Build 'VarRef = Start.
9659 ExprResult NewStart = IsNonRectangularLB
9660 ? Start.get()
9661 : tryBuildCapture(SemaRef, Capture: Start.get(), Captures);
9662 if (!NewStart.isUsable())
9663 return ExprError();
9664 if (!SemaRef.Context.hasSameType(T1: NewStart.get()->getType(),
9665 T2: VarRef.get()->getType())) {
9666 NewStart = SemaRef.PerformImplicitConversion(
9667 From: NewStart.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
9668 /*AllowExplicit=*/true);
9669 if (!NewStart.isUsable())
9670 return ExprError();
9671 }
9672
9673 ExprResult Init =
9674 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
9675 return Init;
9676}
9677
9678/// Build 'VarRef = Start + Iter * Step'.
9679static ExprResult buildCounterUpdate(
9680 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
9681 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
9682 bool IsNonRectangularLB,
9683 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
9684 // Add parentheses (for debugging purposes only).
9685 Iter = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Iter.get());
9686 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
9687 !Step.isUsable())
9688 return ExprError();
9689
9690 ExprResult NewStep = Step;
9691 if (Captures)
9692 NewStep = tryBuildCapture(SemaRef, Capture: Step.get(), Captures&: *Captures);
9693 if (NewStep.isInvalid())
9694 return ExprError();
9695 ExprResult Update =
9696 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Mul, LHSExpr: Iter.get(), RHSExpr: NewStep.get());
9697 if (!Update.isUsable())
9698 return ExprError();
9699
9700 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
9701 // 'VarRef = Start (+|-) Iter * Step'.
9702 if (!Start.isUsable())
9703 return ExprError();
9704 ExprResult NewStart = SemaRef.ActOnParenExpr(L: Loc, R: Loc, E: Start.get());
9705 if (!NewStart.isUsable())
9706 return ExprError();
9707 if (Captures && !IsNonRectangularLB)
9708 NewStart = tryBuildCapture(SemaRef, Capture: Start.get(), Captures&: *Captures);
9709 if (NewStart.isInvalid())
9710 return ExprError();
9711
9712 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
9713 ExprResult SavedUpdate = Update;
9714 ExprResult UpdateVal;
9715 if (VarRef.get()->getType()->isOverloadableType() ||
9716 NewStart.get()->getType()->isOverloadableType() ||
9717 Update.get()->getType()->isOverloadableType()) {
9718 Sema::TentativeAnalysisScope Trap(SemaRef);
9719
9720 Update =
9721 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: NewStart.get());
9722 if (Update.isUsable()) {
9723 UpdateVal =
9724 SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_SubAssign : BO_AddAssign,
9725 LHSExpr: VarRef.get(), RHSExpr: SavedUpdate.get());
9726 if (UpdateVal.isUsable()) {
9727 Update = SemaRef.CreateBuiltinBinOp(OpLoc: Loc, Opc: BO_Comma, LHSExpr: Update.get(),
9728 RHSExpr: UpdateVal.get());
9729 }
9730 }
9731 }
9732
9733 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
9734 if (!Update.isUsable() || !UpdateVal.isUsable()) {
9735 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: Subtract ? BO_Sub : BO_Add,
9736 LHSExpr: NewStart.get(), RHSExpr: SavedUpdate.get());
9737 if (!Update.isUsable())
9738 return ExprError();
9739
9740 if (!SemaRef.Context.hasSameType(T1: Update.get()->getType(),
9741 T2: VarRef.get()->getType())) {
9742 Update = SemaRef.PerformImplicitConversion(
9743 From: Update.get(), ToType: VarRef.get()->getType(), Action: AssignmentAction::Converting,
9744 /*AllowExplicit=*/true);
9745 if (!Update.isUsable())
9746 return ExprError();
9747 }
9748
9749 Update = SemaRef.BuildBinOp(S, OpLoc: Loc, Opc: BO_Assign, LHSExpr: VarRef.get(), RHSExpr: Update.get());
9750 }
9751 return Update;
9752}
9753
9754/// Convert integer expression \a E to make it have at least \a Bits
9755/// bits.
9756static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
9757 if (E == nullptr)
9758 return ExprError();
9759 ASTContext &C = SemaRef.Context;
9760 QualType OldType = E->getType();
9761 unsigned HasBits = C.getTypeSize(T: OldType);
9762 if (HasBits >= Bits)
9763 return ExprResult(E);
9764 // OK to convert to signed, because new type has more bits than old.
9765 QualType NewType = C.getIntTypeForBitwidth(DestWidth: Bits, /*Signed=*/true);
9766 return SemaRef.PerformImplicitConversion(
9767 From: E, ToType: NewType, Action: AssignmentAction::Converting, /*AllowExplicit=*/true);
9768}
9769
9770/// Check if the given expression \a E is a constant integer that fits
9771/// into \a Bits bits.
9772static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
9773 if (E == nullptr)
9774 return false;
9775 if (std::optional<llvm::APSInt> Result =
9776 E->getIntegerConstantExpr(Ctx: SemaRef.Context))
9777 return Signed ? Result->isSignedIntN(N: Bits) : Result->isIntN(N: Bits);
9778 return false;
9779}
9780
9781/// Build preinits statement for the given declarations.
9782static Stmt *buildPreInits(ASTContext &Context,
9783 MutableArrayRef<Decl *> PreInits) {
9784 if (!PreInits.empty()) {
9785 return new (Context) DeclStmt(
9786 DeclGroupRef::Create(C&: Context, Decls: PreInits.begin(), NumDecls: PreInits.size()),
9787 SourceLocation(), SourceLocation());
9788 }
9789 return nullptr;
9790}
9791
9792/// Append the \p Item or the content of a CompoundStmt to the list \p
9793/// TargetList.
9794///
9795/// A CompoundStmt is used as container in case multiple statements need to be
9796/// stored in lieu of using an explicit list. Flattening is necessary because
9797/// contained DeclStmts need to be visible after the execution of the list. Used
9798/// for OpenMP pre-init declarations/statements.
9799static void appendFlattenedStmtList(SmallVectorImpl<Stmt *> &TargetList,
9800 Stmt *Item) {
9801 // nullptr represents an empty list.
9802 if (!Item)
9803 return;
9804
9805 if (auto *CS = dyn_cast<CompoundStmt>(Val: Item))
9806 llvm::append_range(C&: TargetList, R: CS->body());
9807 else
9808 TargetList.push_back(Elt: Item);
9809}
9810
9811/// Build preinits statement for the given declarations.
9812static Stmt *
9813buildPreInits(ASTContext &Context,
9814 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
9815 if (!Captures.empty()) {
9816 SmallVector<Decl *, 16> PreInits;
9817 for (const auto &Pair : Captures)
9818 PreInits.push_back(Elt: Pair.second->getDecl());
9819 return buildPreInits(Context, PreInits);
9820 }
9821 return nullptr;
9822}
9823
9824/// Build pre-init statement for the given statements.
9825static Stmt *buildPreInits(ASTContext &Context, ArrayRef<Stmt *> PreInits) {
9826 if (PreInits.empty())
9827 return nullptr;
9828
9829 SmallVector<Stmt *> Stmts;
9830 for (Stmt *S : PreInits)
9831 appendFlattenedStmtList(TargetList&: Stmts, Item: S);
9832 return CompoundStmt::Create(C: Context, Stmts: PreInits, FPFeatures: FPOptionsOverride(), LB: {}, RB: {});
9833}
9834
9835/// Build postupdate expression for the given list of postupdates expressions.
9836static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
9837 Expr *PostUpdate = nullptr;
9838 if (!PostUpdates.empty()) {
9839 for (Expr *E : PostUpdates) {
9840 Expr *ConvE = S.BuildCStyleCastExpr(
9841 LParenLoc: E->getExprLoc(),
9842 Ty: S.Context.getTrivialTypeSourceInfo(T: S.Context.VoidTy),
9843 RParenLoc: E->getExprLoc(), Op: E)
9844 .get();
9845 PostUpdate = PostUpdate
9846 ? S.CreateBuiltinBinOp(OpLoc: ConvE->getExprLoc(), Opc: BO_Comma,
9847 LHSExpr: PostUpdate, RHSExpr: ConvE)
9848 .get()
9849 : ConvE;
9850 }
9851 }
9852 return PostUpdate;
9853}
9854
9855/// Look for variables declared in the body parts of a for-loop nest. Used
9856/// for verifying loop nest structure before performing a loop collapse
9857/// operation.
9858class ForVarDeclFinder : public DynamicRecursiveASTVisitor {
9859 int NestingDepth = 0;
9860 llvm::SmallPtrSetImpl<const Decl *> &VarDecls;
9861
9862public:
9863 explicit ForVarDeclFinder(llvm::SmallPtrSetImpl<const Decl *> &VD)
9864 : VarDecls(VD) {}
9865
9866 bool VisitForStmt(ForStmt *F) override {
9867 ++NestingDepth;
9868 TraverseStmt(S: F->getBody());
9869 --NestingDepth;
9870 return false;
9871 }
9872
9873 bool VisitCXXForRangeStmt(CXXForRangeStmt *RF) override {
9874 ++NestingDepth;
9875 TraverseStmt(S: RF->getBody());
9876 --NestingDepth;
9877 return false;
9878 }
9879
9880 bool VisitVarDecl(VarDecl *D) override {
9881 Decl *C = D->getCanonicalDecl();
9882 if (NestingDepth > 0)
9883 VarDecls.insert(Ptr: C);
9884 return true;
9885 }
9886};
9887
9888/// Called on a for stmt to check itself and nested loops (if any).
9889/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
9890/// number of collapsed loops otherwise.
9891static unsigned
9892checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
9893 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
9894 DSAStackTy &DSA,
9895 SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
9896 OMPLoopBasedDirective::HelperExprs &Built) {
9897 // If either of the loop expressions exist and contain errors, we bail out
9898 // early because diagnostics have already been emitted and we can't reliably
9899 // check more about the loop.
9900 if ((CollapseLoopCountExpr && CollapseLoopCountExpr->containsErrors()) ||
9901 (OrderedLoopCountExpr && OrderedLoopCountExpr->containsErrors()))
9902 return 0;
9903
9904 unsigned NestedLoopCount = 1;
9905 bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
9906 !isOpenMPLoopTransformationDirective(DKind);
9907 llvm::SmallPtrSet<const Decl *, 4> CollapsedLoopVarDecls;
9908
9909 if (CollapseLoopCountExpr) {
9910 // Found 'collapse' clause - calculate collapse number.
9911 Expr::EvalResult Result;
9912 if (!CollapseLoopCountExpr->isValueDependent() &&
9913 CollapseLoopCountExpr->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext())) {
9914 NestedLoopCount = Result.Val.getInt().getLimitedValue();
9915
9916 ForVarDeclFinder FVDF{CollapsedLoopVarDecls};
9917 FVDF.TraverseStmt(S: AStmt);
9918 } else {
9919 Built.clear(/*Size=*/1);
9920 return 1;
9921 }
9922 }
9923 unsigned OrderedLoopCount = 1;
9924 if (OrderedLoopCountExpr) {
9925 // Found 'ordered' clause - calculate collapse number.
9926 Expr::EvalResult EVResult;
9927 if (!OrderedLoopCountExpr->isValueDependent() &&
9928 OrderedLoopCountExpr->EvaluateAsInt(Result&: EVResult,
9929 Ctx: SemaRef.getASTContext())) {
9930 llvm::APSInt Result = EVResult.Val.getInt();
9931 if (Result.getLimitedValue() < NestedLoopCount) {
9932 SemaRef.Diag(Loc: OrderedLoopCountExpr->getExprLoc(),
9933 DiagID: diag::err_omp_wrong_ordered_loop_count)
9934 << OrderedLoopCountExpr->getSourceRange();
9935 SemaRef.Diag(Loc: CollapseLoopCountExpr->getExprLoc(),
9936 DiagID: diag::note_collapse_loop_count)
9937 << CollapseLoopCountExpr->getSourceRange();
9938 }
9939 OrderedLoopCount = Result.getLimitedValue();
9940 } else {
9941 Built.clear(/*Size=*/1);
9942 return 1;
9943 }
9944 }
9945 // This is helper routine for loop directives (e.g., 'for', 'simd',
9946 // 'for simd', etc.).
9947 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
9948 unsigned NumLoops = std::max(a: OrderedLoopCount, b: NestedLoopCount);
9949 SmallVector<LoopIterationSpace, 4> IterSpaces(NumLoops);
9950 if (!OMPLoopBasedDirective::doForAllLoops(
9951 CurStmt: AStmt->IgnoreContainers(
9952 IgnoreCaptured: !isOpenMPCanonicalLoopNestTransformationDirective(DKind)),
9953 TryImperfectlyNestedLoops: SupportsNonPerfectlyNested, NumLoops,
9954 Callback: [DKind, &SemaRef, &DSA, NumLoops, NestedLoopCount,
9955 CollapseLoopCountExpr, OrderedLoopCountExpr, &VarsWithImplicitDSA,
9956 &IterSpaces, &Captures,
9957 &CollapsedLoopVarDecls](unsigned Cnt, Stmt *CurStmt) {
9958 if (checkOpenMPIterationSpace(
9959 DKind, S: CurStmt, SemaRef, DSA, CurrentNestedLoopCount: Cnt, NestedLoopCount,
9960 TotalNestedLoopCount: NumLoops, CollapseLoopCountExpr, OrderedLoopCountExpr,
9961 VarsWithImplicitDSA, ResultIterSpaces: IterSpaces, Captures,
9962 CollapsedLoopVarDecls))
9963 return true;
9964 if (Cnt > 0 && Cnt >= NestedLoopCount &&
9965 IterSpaces[Cnt].CounterVar) {
9966 // Handle initialization of captured loop iterator variables.
9967 auto *DRE = cast<DeclRefExpr>(Val: IterSpaces[Cnt].CounterVar);
9968 if (isa<OMPCapturedExprDecl>(Val: DRE->getDecl())) {
9969 Captures[DRE] = DRE;
9970 }
9971 }
9972 return false;
9973 },
9974 OnTransformationCallback: [&SemaRef, &Captures](OMPLoopTransformationDirective *Transform) {
9975 Stmt *DependentPreInits = Transform->getPreInits();
9976 if (!DependentPreInits)
9977 return;
9978
9979 // Search for pre-init declared variables that need to be captured
9980 // to be referenceable inside the directive.
9981 SmallVector<Stmt *> Constituents;
9982 appendFlattenedStmtList(TargetList&: Constituents, Item: DependentPreInits);
9983 for (Stmt *S : Constituents) {
9984 if (auto *DC = dyn_cast<DeclStmt>(Val: S)) {
9985 for (Decl *C : DC->decls()) {
9986 auto *D = cast<VarDecl>(Val: C);
9987 DeclRefExpr *Ref = buildDeclRefExpr(
9988 S&: SemaRef, D, Ty: D->getType().getNonReferenceType(),
9989 Loc: cast<OMPExecutableDirective>(Val: Transform->getDirective())
9990 ->getBeginLoc());
9991 Captures[Ref] = Ref;
9992 }
9993 }
9994 }
9995 }))
9996 return 0;
9997
9998 Built.clear(/*size=*/Size: NestedLoopCount);
9999
10000 if (SemaRef.CurContext->isDependentContext())
10001 return NestedLoopCount;
10002
10003 // An example of what is generated for the following code:
10004 //
10005 // #pragma omp simd collapse(2) ordered(2)
10006 // for (i = 0; i < NI; ++i)
10007 // for (k = 0; k < NK; ++k)
10008 // for (j = J0; j < NJ; j+=2) {
10009 // <loop body>
10010 // }
10011 //
10012 // We generate the code below.
10013 // Note: the loop body may be outlined in CodeGen.
10014 // Note: some counters may be C++ classes, operator- is used to find number of
10015 // iterations and operator+= to calculate counter value.
10016 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
10017 // or i64 is currently supported).
10018 //
10019 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
10020 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
10021 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
10022 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
10023 // // similar updates for vars in clauses (e.g. 'linear')
10024 // <loop body (using local i and j)>
10025 // }
10026 // i = NI; // assign final values of counters
10027 // j = NJ;
10028 //
10029
10030 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
10031 // the iteration counts of the collapsed for loops.
10032 // Precondition tests if there is at least one iteration (all conditions are
10033 // true).
10034 auto PreCond = ExprResult(IterSpaces[0].PreCond);
10035 Expr *N0 = IterSpaces[0].NumIterations;
10036 ExprResult LastIteration32 = widenIterationCount(
10037 /*Bits=*/32,
10038 E: SemaRef
10039 .PerformImplicitConversion(From: N0->IgnoreImpCasts(), ToType: N0->getType(),
10040 Action: AssignmentAction::Converting,
10041 /*AllowExplicit=*/true)
10042 .get(),
10043 SemaRef);
10044 ExprResult LastIteration64 = widenIterationCount(
10045 /*Bits=*/64,
10046 E: SemaRef
10047 .PerformImplicitConversion(From: N0->IgnoreImpCasts(), ToType: N0->getType(),
10048 Action: AssignmentAction::Converting,
10049 /*AllowExplicit=*/true)
10050 .get(),
10051 SemaRef);
10052
10053 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
10054 return NestedLoopCount;
10055
10056 ASTContext &C = SemaRef.Context;
10057 bool AllCountsNeedLessThan32Bits = C.getTypeSize(T: N0->getType()) < 32;
10058
10059 Scope *CurScope = DSA.getCurScope();
10060 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
10061 if (PreCond.isUsable()) {
10062 PreCond =
10063 SemaRef.BuildBinOp(S: CurScope, OpLoc: PreCond.get()->getExprLoc(), Opc: BO_LAnd,
10064 LHSExpr: PreCond.get(), RHSExpr: IterSpaces[Cnt].PreCond);
10065 }
10066 Expr *N = IterSpaces[Cnt].NumIterations;
10067 SourceLocation Loc = N->getExprLoc();
10068 AllCountsNeedLessThan32Bits &= C.getTypeSize(T: N->getType()) < 32;
10069 if (LastIteration32.isUsable())
10070 LastIteration32 = SemaRef.BuildBinOp(
10071 S: CurScope, OpLoc: Loc, Opc: BO_Mul, LHSExpr: LastIteration32.get(),
10072 RHSExpr: SemaRef
10073 .PerformImplicitConversion(From: N->IgnoreImpCasts(), ToType: N->getType(),
10074 Action: AssignmentAction::Converting,
10075 /*AllowExplicit=*/true)
10076 .get());
10077 if (LastIteration64.isUsable())
10078 LastIteration64 = SemaRef.BuildBinOp(
10079 S: CurScope, OpLoc: Loc, Opc: BO_Mul, LHSExpr: LastIteration64.get(),
10080 RHSExpr: SemaRef
10081 .PerformImplicitConversion(From: N->IgnoreImpCasts(), ToType: N->getType(),
10082 Action: AssignmentAction::Converting,
10083 /*AllowExplicit=*/true)
10084 .get());
10085 }
10086
10087 // Choose either the 32-bit or 64-bit version.
10088 ExprResult LastIteration = LastIteration64;
10089 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
10090 (LastIteration32.isUsable() &&
10091 C.getTypeSize(T: LastIteration32.get()->getType()) == 32 &&
10092 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
10093 fitsInto(
10094 /*Bits=*/32,
10095 Signed: LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
10096 E: LastIteration64.get(), SemaRef))))
10097 LastIteration = LastIteration32;
10098 QualType VType = LastIteration.get()->getType();
10099 QualType RealVType = VType;
10100 QualType StrideVType = VType;
10101 if (isOpenMPTaskLoopDirective(DKind)) {
10102 VType =
10103 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
10104 StrideVType =
10105 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
10106 }
10107
10108 if (!LastIteration.isUsable())
10109 return 0;
10110
10111 // Save the number of iterations.
10112 ExprResult NumIterations = LastIteration;
10113 {
10114 LastIteration = SemaRef.BuildBinOp(
10115 S: CurScope, OpLoc: LastIteration.get()->getExprLoc(), Opc: BO_Sub,
10116 LHSExpr: LastIteration.get(),
10117 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
10118 if (!LastIteration.isUsable())
10119 return 0;
10120 }
10121
10122 // Calculate the last iteration number beforehand instead of doing this on
10123 // each iteration. Do not do this if the number of iterations may be kfold-ed.
10124 bool IsConstant = LastIteration.get()->isIntegerConstantExpr(Ctx: SemaRef.Context);
10125 ExprResult CalcLastIteration;
10126 if (!IsConstant) {
10127 ExprResult SaveRef =
10128 tryBuildCapture(SemaRef, Capture: LastIteration.get(), Captures);
10129 LastIteration = SaveRef;
10130
10131 // Prepare SaveRef + 1.
10132 NumIterations = SemaRef.BuildBinOp(
10133 S: CurScope, OpLoc: SaveRef.get()->getExprLoc(), Opc: BO_Add, LHSExpr: SaveRef.get(),
10134 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get());
10135 if (!NumIterations.isUsable())
10136 return 0;
10137 }
10138
10139 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
10140
10141 // Build variables passed into runtime, necessary for worksharing directives.
10142 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
10143 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
10144 isOpenMPDistributeDirective(DKind) ||
10145 isOpenMPGenericLoopDirective(DKind) ||
10146 isOpenMPLoopTransformationDirective(DKind)) {
10147 // Lower bound variable, initialized with zero.
10148 VarDecl *LBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.lb");
10149 LB = buildDeclRefExpr(S&: SemaRef, D: LBDecl, Ty: VType, Loc: InitLoc);
10150 SemaRef.AddInitializerToDecl(dcl: LBDecl,
10151 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10152 /*DirectInit=*/false);
10153
10154 // Upper bound variable, initialized with last iteration number.
10155 VarDecl *UBDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.ub");
10156 UB = buildDeclRefExpr(S&: SemaRef, D: UBDecl, Ty: VType, Loc: InitLoc);
10157 SemaRef.AddInitializerToDecl(dcl: UBDecl, init: LastIteration.get(),
10158 /*DirectInit=*/false);
10159
10160 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
10161 // This will be used to implement clause 'lastprivate'.
10162 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(DestWidth: 32, Signed: true);
10163 VarDecl *ILDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: Int32Ty, Name: ".omp.is_last");
10164 IL = buildDeclRefExpr(S&: SemaRef, D: ILDecl, Ty: Int32Ty, Loc: InitLoc);
10165 SemaRef.AddInitializerToDecl(dcl: ILDecl,
10166 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10167 /*DirectInit=*/false);
10168
10169 // Stride variable returned by runtime (we initialize it to 1 by default).
10170 VarDecl *STDecl =
10171 buildVarDecl(SemaRef, Loc: InitLoc, Type: StrideVType, Name: ".omp.stride");
10172 ST = buildDeclRefExpr(S&: SemaRef, D: STDecl, Ty: StrideVType, Loc: InitLoc);
10173 SemaRef.AddInitializerToDecl(dcl: STDecl,
10174 init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 1).get(),
10175 /*DirectInit=*/false);
10176
10177 // Build expression: UB = min(UB, LastIteration)
10178 // It is necessary for CodeGen of directives with static scheduling.
10179 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_GT,
10180 LHSExpr: UB.get(), RHSExpr: LastIteration.get());
10181 ExprResult CondOp = SemaRef.ActOnConditionalOp(
10182 QuestionLoc: LastIteration.get()->getExprLoc(), ColonLoc: InitLoc, CondExpr: IsUBGreater.get(),
10183 LHSExpr: LastIteration.get(), RHSExpr: UB.get());
10184 EUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: UB.get(),
10185 RHSExpr: CondOp.get());
10186 EUB = SemaRef.ActOnFinishFullExpr(Expr: EUB.get(), /*DiscardedValue=*/false);
10187
10188 // If we have a combined directive that combines 'distribute', 'for' or
10189 // 'simd' we need to be able to access the bounds of the schedule of the
10190 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
10191 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
10192 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10193 // Lower bound variable, initialized with zero.
10194 VarDecl *CombLBDecl =
10195 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.lb");
10196 CombLB = buildDeclRefExpr(S&: SemaRef, D: CombLBDecl, Ty: VType, Loc: InitLoc);
10197 SemaRef.AddInitializerToDecl(
10198 dcl: CombLBDecl, init: SemaRef.ActOnIntegerConstant(Loc: InitLoc, Val: 0).get(),
10199 /*DirectInit=*/false);
10200
10201 // Upper bound variable, initialized with last iteration number.
10202 VarDecl *CombUBDecl =
10203 buildVarDecl(SemaRef, Loc: InitLoc, Type: VType, Name: ".omp.comb.ub");
10204 CombUB = buildDeclRefExpr(S&: SemaRef, D: CombUBDecl, Ty: VType, Loc: InitLoc);
10205 SemaRef.AddInitializerToDecl(dcl: CombUBDecl, init: LastIteration.get(),
10206 /*DirectInit=*/false);
10207
10208 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
10209 S: CurScope, OpLoc: InitLoc, Opc: BO_GT, LHSExpr: CombUB.get(), RHSExpr: LastIteration.get());
10210 ExprResult CombCondOp =
10211 SemaRef.ActOnConditionalOp(QuestionLoc: InitLoc, ColonLoc: InitLoc, CondExpr: CombIsUBGreater.get(),
10212 LHSExpr: LastIteration.get(), RHSExpr: CombUB.get());
10213 CombEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
10214 RHSExpr: CombCondOp.get());
10215 CombEUB =
10216 SemaRef.ActOnFinishFullExpr(Expr: CombEUB.get(), /*DiscardedValue=*/false);
10217
10218 const CapturedDecl *CD = cast<CapturedStmt>(Val: AStmt)->getCapturedDecl();
10219 // We expect to have at least 2 more parameters than the 'parallel'
10220 // directive does - the lower and upper bounds of the previous schedule.
10221 assert(CD->getNumParams() >= 4 &&
10222 "Unexpected number of parameters in loop combined directive");
10223
10224 // Set the proper type for the bounds given what we learned from the
10225 // enclosed loops.
10226 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/i: 2);
10227 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/i: 3);
10228
10229 // Previous lower and upper bounds are obtained from the region
10230 // parameters.
10231 PrevLB =
10232 buildDeclRefExpr(S&: SemaRef, D: PrevLBDecl, Ty: PrevLBDecl->getType(), Loc: InitLoc);
10233 PrevUB =
10234 buildDeclRefExpr(S&: SemaRef, D: PrevUBDecl, Ty: PrevUBDecl->getType(), Loc: InitLoc);
10235 }
10236 }
10237
10238 // Build the iteration variable and its initialization before loop.
10239 ExprResult IV;
10240 ExprResult Init, CombInit;
10241 {
10242 VarDecl *IVDecl = buildVarDecl(SemaRef, Loc: InitLoc, Type: RealVType, Name: ".omp.iv");
10243 IV = buildDeclRefExpr(S&: SemaRef, D: IVDecl, Ty: RealVType, Loc: InitLoc);
10244 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
10245 isOpenMPGenericLoopDirective(DKind) ||
10246 isOpenMPTaskLoopDirective(DKind) ||
10247 isOpenMPDistributeDirective(DKind) ||
10248 isOpenMPLoopTransformationDirective(DKind))
10249 ? LB.get()
10250 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
10251 Init = SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: RHS);
10252 Init = SemaRef.ActOnFinishFullExpr(Expr: Init.get(), /*DiscardedValue=*/false);
10253
10254 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10255 Expr *CombRHS =
10256 (isOpenMPWorksharingDirective(DKind) ||
10257 isOpenMPGenericLoopDirective(DKind) ||
10258 isOpenMPTaskLoopDirective(DKind) ||
10259 isOpenMPDistributeDirective(DKind))
10260 ? CombLB.get()
10261 : SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 0).get();
10262 CombInit =
10263 SemaRef.BuildBinOp(S: CurScope, OpLoc: InitLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: CombRHS);
10264 CombInit =
10265 SemaRef.ActOnFinishFullExpr(Expr: CombInit.get(), /*DiscardedValue=*/false);
10266 }
10267 }
10268
10269 bool UseStrictCompare =
10270 RealVType->hasUnsignedIntegerRepresentation() &&
10271 llvm::all_of(Range&: IterSpaces, P: [](const LoopIterationSpace &LIS) {
10272 return LIS.IsStrictCompare;
10273 });
10274 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
10275 // unsigned IV)) for worksharing loops.
10276 SourceLocation CondLoc = AStmt->getBeginLoc();
10277 Expr *BoundUB = UB.get();
10278 if (UseStrictCompare) {
10279 BoundUB =
10280 SemaRef
10281 .BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundUB,
10282 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10283 .get();
10284 BoundUB =
10285 SemaRef.ActOnFinishFullExpr(Expr: BoundUB, /*DiscardedValue=*/false).get();
10286 }
10287 ExprResult Cond =
10288 (isOpenMPWorksharingDirective(DKind) ||
10289 isOpenMPGenericLoopDirective(DKind) ||
10290 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) ||
10291 isOpenMPLoopTransformationDirective(DKind))
10292 ? SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc,
10293 Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(),
10294 RHSExpr: BoundUB)
10295 : SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
10296 RHSExpr: NumIterations.get());
10297 ExprResult CombDistCond;
10298 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10299 CombDistCond = SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: BO_LT, LHSExpr: IV.get(),
10300 RHSExpr: NumIterations.get());
10301 }
10302
10303 ExprResult CombCond;
10304 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10305 Expr *BoundCombUB = CombUB.get();
10306 if (UseStrictCompare) {
10307 BoundCombUB =
10308 SemaRef
10309 .BuildBinOp(
10310 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundCombUB,
10311 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10312 .get();
10313 BoundCombUB =
10314 SemaRef.ActOnFinishFullExpr(Expr: BoundCombUB, /*DiscardedValue=*/false)
10315 .get();
10316 }
10317 CombCond =
10318 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
10319 LHSExpr: IV.get(), RHSExpr: BoundCombUB);
10320 }
10321 // Loop increment (IV = IV + 1)
10322 SourceLocation IncLoc = AStmt->getBeginLoc();
10323 ExprResult Inc =
10324 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: IV.get(),
10325 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: IncLoc, Val: 1).get());
10326 if (!Inc.isUsable())
10327 return 0;
10328 Inc = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: IV.get(), RHSExpr: Inc.get());
10329 Inc = SemaRef.ActOnFinishFullExpr(Expr: Inc.get(), /*DiscardedValue=*/false);
10330 if (!Inc.isUsable())
10331 return 0;
10332
10333 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
10334 // Used for directives with static scheduling.
10335 // In combined construct, add combined version that use CombLB and CombUB
10336 // base variables for the update
10337 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
10338 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
10339 isOpenMPGenericLoopDirective(DKind) ||
10340 isOpenMPDistributeDirective(DKind) ||
10341 isOpenMPLoopTransformationDirective(DKind)) {
10342 // LB + ST
10343 NextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: LB.get(), RHSExpr: ST.get());
10344 if (!NextLB.isUsable())
10345 return 0;
10346 // LB = LB + ST
10347 NextLB =
10348 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: LB.get(), RHSExpr: NextLB.get());
10349 NextLB =
10350 SemaRef.ActOnFinishFullExpr(Expr: NextLB.get(), /*DiscardedValue=*/false);
10351 if (!NextLB.isUsable())
10352 return 0;
10353 // UB + ST
10354 NextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: UB.get(), RHSExpr: ST.get());
10355 if (!NextUB.isUsable())
10356 return 0;
10357 // UB = UB + ST
10358 NextUB =
10359 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: UB.get(), RHSExpr: NextUB.get());
10360 NextUB =
10361 SemaRef.ActOnFinishFullExpr(Expr: NextUB.get(), /*DiscardedValue=*/false);
10362 if (!NextUB.isUsable())
10363 return 0;
10364 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10365 CombNextLB =
10366 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombLB.get(), RHSExpr: ST.get());
10367 if (!NextLB.isUsable())
10368 return 0;
10369 // LB = LB + ST
10370 CombNextLB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombLB.get(),
10371 RHSExpr: CombNextLB.get());
10372 CombNextLB = SemaRef.ActOnFinishFullExpr(Expr: CombNextLB.get(),
10373 /*DiscardedValue=*/false);
10374 if (!CombNextLB.isUsable())
10375 return 0;
10376 // UB + ST
10377 CombNextUB =
10378 SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Add, LHSExpr: CombUB.get(), RHSExpr: ST.get());
10379 if (!CombNextUB.isUsable())
10380 return 0;
10381 // UB = UB + ST
10382 CombNextUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: IncLoc, Opc: BO_Assign, LHSExpr: CombUB.get(),
10383 RHSExpr: CombNextUB.get());
10384 CombNextUB = SemaRef.ActOnFinishFullExpr(Expr: CombNextUB.get(),
10385 /*DiscardedValue=*/false);
10386 if (!CombNextUB.isUsable())
10387 return 0;
10388 }
10389 }
10390
10391 // Create increment expression for distribute loop when combined in a same
10392 // directive with for as IV = IV + ST; ensure upper bound expression based
10393 // on PrevUB instead of NumIterations - used to implement 'for' when found
10394 // in combination with 'distribute', like in 'distribute parallel for'
10395 SourceLocation DistIncLoc = AStmt->getBeginLoc();
10396 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
10397 if (isOpenMPLoopBoundSharingDirective(Kind: DKind)) {
10398 DistCond = SemaRef.BuildBinOp(
10399 S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE, LHSExpr: IV.get(), RHSExpr: BoundUB);
10400 assert(DistCond.isUsable() && "distribute cond expr was not built");
10401
10402 DistInc =
10403 SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Add, LHSExpr: IV.get(), RHSExpr: ST.get());
10404 assert(DistInc.isUsable() && "distribute inc expr was not built");
10405 DistInc = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: IV.get(),
10406 RHSExpr: DistInc.get());
10407 DistInc =
10408 SemaRef.ActOnFinishFullExpr(Expr: DistInc.get(), /*DiscardedValue=*/false);
10409 assert(DistInc.isUsable() && "distribute inc expr was not built");
10410
10411 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
10412 // construct
10413 ExprResult NewPrevUB = PrevUB;
10414 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
10415 if (!SemaRef.Context.hasSameType(T1: UB.get()->getType(),
10416 T2: PrevUB.get()->getType())) {
10417 NewPrevUB = SemaRef.BuildCStyleCastExpr(
10418 LParenLoc: DistEUBLoc,
10419 Ty: SemaRef.Context.getTrivialTypeSourceInfo(T: UB.get()->getType()),
10420 RParenLoc: DistEUBLoc, Op: NewPrevUB.get());
10421 if (!NewPrevUB.isUsable())
10422 return 0;
10423 }
10424 ExprResult IsUBGreater = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistEUBLoc, Opc: BO_GT,
10425 LHSExpr: UB.get(), RHSExpr: NewPrevUB.get());
10426 ExprResult CondOp = SemaRef.ActOnConditionalOp(
10427 QuestionLoc: DistEUBLoc, ColonLoc: DistEUBLoc, CondExpr: IsUBGreater.get(), LHSExpr: NewPrevUB.get(), RHSExpr: UB.get());
10428 PrevEUB = SemaRef.BuildBinOp(S: CurScope, OpLoc: DistIncLoc, Opc: BO_Assign, LHSExpr: UB.get(),
10429 RHSExpr: CondOp.get());
10430 PrevEUB =
10431 SemaRef.ActOnFinishFullExpr(Expr: PrevEUB.get(), /*DiscardedValue=*/false);
10432
10433 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
10434 // parallel for is in combination with a distribute directive with
10435 // schedule(static, 1)
10436 Expr *BoundPrevUB = PrevUB.get();
10437 if (UseStrictCompare) {
10438 BoundPrevUB =
10439 SemaRef
10440 .BuildBinOp(
10441 S: CurScope, OpLoc: CondLoc, Opc: BO_Add, LHSExpr: BoundPrevUB,
10442 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get())
10443 .get();
10444 BoundPrevUB =
10445 SemaRef.ActOnFinishFullExpr(Expr: BoundPrevUB, /*DiscardedValue=*/false)
10446 .get();
10447 }
10448 ParForInDistCond =
10449 SemaRef.BuildBinOp(S: CurScope, OpLoc: CondLoc, Opc: UseStrictCompare ? BO_LT : BO_LE,
10450 LHSExpr: IV.get(), RHSExpr: BoundPrevUB);
10451 }
10452
10453 // Build updates and final values of the loop counters.
10454 bool HasErrors = false;
10455 Built.Counters.resize(N: NestedLoopCount);
10456 Built.Inits.resize(N: NestedLoopCount);
10457 Built.Updates.resize(N: NestedLoopCount);
10458 Built.Finals.resize(N: NestedLoopCount);
10459 Built.DependentCounters.resize(N: NestedLoopCount);
10460 Built.DependentInits.resize(N: NestedLoopCount);
10461 Built.FinalsConditions.resize(N: NestedLoopCount);
10462 {
10463 // We implement the following algorithm for obtaining the
10464 // original loop iteration variable values based on the
10465 // value of the collapsed loop iteration variable IV.
10466 //
10467 // Let n+1 be the number of collapsed loops in the nest.
10468 // Iteration variables (I0, I1, .... In)
10469 // Iteration counts (N0, N1, ... Nn)
10470 //
10471 // Acc = IV;
10472 //
10473 // To compute Ik for loop k, 0 <= k <= n, generate:
10474 // Prod = N(k+1) * N(k+2) * ... * Nn;
10475 // Ik = Acc / Prod;
10476 // Acc -= Ik * Prod;
10477 //
10478 ExprResult Acc = IV;
10479 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
10480 LoopIterationSpace &IS = IterSpaces[Cnt];
10481 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
10482 ExprResult Iter;
10483
10484 // Compute prod
10485 ExprResult Prod = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
10486 for (unsigned int K = Cnt + 1; K < NestedLoopCount; ++K)
10487 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Prod.get(),
10488 RHSExpr: IterSpaces[K].NumIterations);
10489
10490 // Iter = Acc / Prod
10491 // If there is at least one more inner loop to avoid
10492 // multiplication by 1.
10493 if (Cnt + 1 < NestedLoopCount)
10494 Iter =
10495 SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Div, LHSExpr: Acc.get(), RHSExpr: Prod.get());
10496 else
10497 Iter = Acc;
10498 if (!Iter.isUsable()) {
10499 HasErrors = true;
10500 break;
10501 }
10502
10503 // Update Acc:
10504 // Acc -= Iter * Prod
10505 // Check if there is at least one more inner loop to avoid
10506 // multiplication by 1.
10507 if (Cnt + 1 < NestedLoopCount)
10508 Prod = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Mul, LHSExpr: Iter.get(),
10509 RHSExpr: Prod.get());
10510 else
10511 Prod = Iter;
10512 Acc = SemaRef.BuildBinOp(S: CurScope, OpLoc: UpdLoc, Opc: BO_Sub, LHSExpr: Acc.get(), RHSExpr: Prod.get());
10513
10514 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
10515 auto *VD = cast<VarDecl>(Val: cast<DeclRefExpr>(Val: IS.CounterVar)->getDecl());
10516 DeclRefExpr *CounterVar = buildDeclRefExpr(
10517 S&: SemaRef, D: VD, Ty: IS.CounterVar->getType(), Loc: IS.CounterVar->getExprLoc(),
10518 /*RefersToCapture=*/true);
10519 ExprResult Init =
10520 buildCounterInit(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
10521 Start: IS.CounterInit, IsNonRectangularLB: IS.IsNonRectangularLB, Captures);
10522 if (!Init.isUsable()) {
10523 HasErrors = true;
10524 break;
10525 }
10526 ExprResult Update = buildCounterUpdate(
10527 SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar, Start: IS.CounterInit, Iter,
10528 Step: IS.CounterStep, Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
10529 if (!Update.isUsable()) {
10530 HasErrors = true;
10531 break;
10532 }
10533
10534 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
10535 ExprResult Final =
10536 buildCounterUpdate(SemaRef, S: CurScope, Loc: UpdLoc, VarRef: CounterVar,
10537 Start: IS.CounterInit, Iter: IS.NumIterations, Step: IS.CounterStep,
10538 Subtract: IS.Subtract, IsNonRectangularLB: IS.IsNonRectangularLB, Captures: &Captures);
10539 if (!Final.isUsable()) {
10540 HasErrors = true;
10541 break;
10542 }
10543
10544 if (!Update.isUsable() || !Final.isUsable()) {
10545 HasErrors = true;
10546 break;
10547 }
10548 // Save results
10549 Built.Counters[Cnt] = IS.CounterVar;
10550 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
10551 Built.Inits[Cnt] = Init.get();
10552 Built.Updates[Cnt] = Update.get();
10553 Built.Finals[Cnt] = Final.get();
10554 Built.DependentCounters[Cnt] = nullptr;
10555 Built.DependentInits[Cnt] = nullptr;
10556 Built.FinalsConditions[Cnt] = nullptr;
10557 if (IS.IsNonRectangularLB || IS.IsNonRectangularUB) {
10558 Built.DependentCounters[Cnt] = Built.Counters[IS.LoopDependentIdx - 1];
10559 Built.DependentInits[Cnt] = Built.Inits[IS.LoopDependentIdx - 1];
10560 Built.FinalsConditions[Cnt] = IS.FinalCondition;
10561 }
10562 }
10563 }
10564
10565 if (HasErrors)
10566 return 0;
10567
10568 // Save results
10569 Built.IterationVarRef = IV.get();
10570 Built.LastIteration = LastIteration.get();
10571 Built.NumIterations = NumIterations.get();
10572 Built.CalcLastIteration = SemaRef
10573 .ActOnFinishFullExpr(Expr: CalcLastIteration.get(),
10574 /*DiscardedValue=*/false)
10575 .get();
10576 Built.PreCond = PreCond.get();
10577 Built.PreInits = buildPreInits(Context&: C, Captures);
10578 Built.Cond = Cond.get();
10579 Built.Init = Init.get();
10580 Built.Inc = Inc.get();
10581 Built.LB = LB.get();
10582 Built.UB = UB.get();
10583 Built.IL = IL.get();
10584 Built.ST = ST.get();
10585 Built.EUB = EUB.get();
10586 Built.NLB = NextLB.get();
10587 Built.NUB = NextUB.get();
10588 Built.PrevLB = PrevLB.get();
10589 Built.PrevUB = PrevUB.get();
10590 Built.DistInc = DistInc.get();
10591 Built.PrevEUB = PrevEUB.get();
10592 Built.DistCombinedFields.LB = CombLB.get();
10593 Built.DistCombinedFields.UB = CombUB.get();
10594 Built.DistCombinedFields.EUB = CombEUB.get();
10595 Built.DistCombinedFields.Init = CombInit.get();
10596 Built.DistCombinedFields.Cond = CombCond.get();
10597 Built.DistCombinedFields.NLB = CombNextLB.get();
10598 Built.DistCombinedFields.NUB = CombNextUB.get();
10599 Built.DistCombinedFields.DistCond = CombDistCond.get();
10600 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
10601
10602 return NestedLoopCount;
10603}
10604
10605static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
10606 auto CollapseClauses =
10607 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
10608 if (CollapseClauses.begin() != CollapseClauses.end())
10609 return (*CollapseClauses.begin())->getNumForLoops();
10610 return nullptr;
10611}
10612
10613static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
10614 auto OrderedClauses =
10615 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
10616 if (OrderedClauses.begin() != OrderedClauses.end())
10617 return (*OrderedClauses.begin())->getNumForLoops();
10618 return nullptr;
10619}
10620
10621static bool checkSimdlenSafelenSpecified(Sema &S,
10622 const ArrayRef<OMPClause *> Clauses) {
10623 const OMPSafelenClause *Safelen = nullptr;
10624 const OMPSimdlenClause *Simdlen = nullptr;
10625
10626 for (const OMPClause *Clause : Clauses) {
10627 if (Clause->getClauseKind() == OMPC_safelen)
10628 Safelen = cast<OMPSafelenClause>(Val: Clause);
10629 else if (Clause->getClauseKind() == OMPC_simdlen)
10630 Simdlen = cast<OMPSimdlenClause>(Val: Clause);
10631 if (Safelen && Simdlen)
10632 break;
10633 }
10634
10635 if (Simdlen && Safelen) {
10636 const Expr *SimdlenLength = Simdlen->getSimdlen();
10637 const Expr *SafelenLength = Safelen->getSafelen();
10638 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
10639 SimdlenLength->isInstantiationDependent() ||
10640 SimdlenLength->containsUnexpandedParameterPack())
10641 return false;
10642 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
10643 SafelenLength->isInstantiationDependent() ||
10644 SafelenLength->containsUnexpandedParameterPack())
10645 return false;
10646 Expr::EvalResult SimdlenResult, SafelenResult;
10647 SimdlenLength->EvaluateAsInt(Result&: SimdlenResult, Ctx: S.Context);
10648 SafelenLength->EvaluateAsInt(Result&: SafelenResult, Ctx: S.Context);
10649 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
10650 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
10651 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
10652 // If both simdlen and safelen clauses are specified, the value of the
10653 // simdlen parameter must be less than or equal to the value of the safelen
10654 // parameter.
10655 if (SimdlenRes > SafelenRes) {
10656 S.Diag(Loc: SimdlenLength->getExprLoc(),
10657 DiagID: diag::err_omp_wrong_simdlen_safelen_values)
10658 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
10659 return true;
10660 }
10661 }
10662 return false;
10663}
10664
10665StmtResult SemaOpenMP::ActOnOpenMPSimdDirective(
10666 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10667 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10668 if (!AStmt)
10669 return StmtError();
10670
10671 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_simd, AStmt);
10672
10673 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10674 OMPLoopBasedDirective::HelperExprs B;
10675 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10676 // define the nested loops number.
10677 unsigned NestedLoopCount = checkOpenMPLoop(
10678 DKind: OMPD_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10679 AStmt: CS, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10680 if (NestedLoopCount == 0)
10681 return StmtError();
10682
10683 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10684 return StmtError();
10685
10686 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
10687 return StmtError();
10688
10689 auto *SimdDirective = OMPSimdDirective::Create(
10690 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10691 return SimdDirective;
10692}
10693
10694StmtResult SemaOpenMP::ActOnOpenMPForDirective(
10695 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10696 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10697 if (!AStmt)
10698 return StmtError();
10699
10700 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10701 OMPLoopBasedDirective::HelperExprs B;
10702 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10703 // define the nested loops number.
10704 unsigned NestedLoopCount = checkOpenMPLoop(
10705 DKind: OMPD_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10706 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10707 if (NestedLoopCount == 0)
10708 return StmtError();
10709
10710 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10711 return StmtError();
10712
10713 auto *ForDirective = OMPForDirective::Create(
10714 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
10715 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10716 return ForDirective;
10717}
10718
10719StmtResult SemaOpenMP::ActOnOpenMPForSimdDirective(
10720 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10721 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10722 if (!AStmt)
10723 return StmtError();
10724
10725 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_for_simd, AStmt);
10726
10727 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10728 OMPLoopBasedDirective::HelperExprs B;
10729 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
10730 // define the nested loops number.
10731 unsigned NestedLoopCount =
10732 checkOpenMPLoop(DKind: OMPD_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
10733 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
10734 VarsWithImplicitDSA, Built&: B);
10735 if (NestedLoopCount == 0)
10736 return StmtError();
10737
10738 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
10739 return StmtError();
10740
10741 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
10742 return StmtError();
10743
10744 return OMPForSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10745 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10746}
10747
10748static bool checkSectionsDirective(Sema &SemaRef, OpenMPDirectiveKind DKind,
10749 Stmt *AStmt, DSAStackTy *Stack) {
10750 if (!AStmt)
10751 return true;
10752
10753 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
10754 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
10755 auto BaseStmt = AStmt;
10756 while (auto *CS = dyn_cast_or_null<CapturedStmt>(Val: BaseStmt))
10757 BaseStmt = CS->getCapturedStmt();
10758 if (auto *C = dyn_cast_or_null<CompoundStmt>(Val: BaseStmt)) {
10759 auto S = C->children();
10760 if (S.begin() == S.end())
10761 return true;
10762 // All associated statements must be '#pragma omp section' except for
10763 // the first one.
10764 for (Stmt *SectionStmt : llvm::drop_begin(RangeOrContainer&: S)) {
10765 if (!SectionStmt || !isa<OMPSectionDirective>(Val: SectionStmt)) {
10766 if (SectionStmt)
10767 SemaRef.Diag(Loc: SectionStmt->getBeginLoc(),
10768 DiagID: diag::err_omp_sections_substmt_not_section)
10769 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
10770 return true;
10771 }
10772 cast<OMPSectionDirective>(Val: SectionStmt)
10773 ->setHasCancel(Stack->isCancelRegion());
10774 }
10775 } else {
10776 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_sections_not_compound_stmt)
10777 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
10778 return true;
10779 }
10780 return false;
10781}
10782
10783StmtResult
10784SemaOpenMP::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
10785 Stmt *AStmt, SourceLocation StartLoc,
10786 SourceLocation EndLoc) {
10787 if (checkSectionsDirective(SemaRef, DKind: OMPD_sections, AStmt, DSAStack))
10788 return StmtError();
10789
10790 SemaRef.setFunctionHasBranchProtectedScope();
10791
10792 return OMPSectionsDirective::Create(
10793 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
10794 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
10795}
10796
10797StmtResult SemaOpenMP::ActOnOpenMPSectionDirective(Stmt *AStmt,
10798 SourceLocation StartLoc,
10799 SourceLocation EndLoc) {
10800 if (!AStmt)
10801 return StmtError();
10802
10803 SemaRef.setFunctionHasBranchProtectedScope();
10804 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
10805
10806 return OMPSectionDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt,
10807 DSAStack->isCancelRegion());
10808}
10809
10810static Expr *getDirectCallExpr(Expr *E) {
10811 E = E->IgnoreParenCasts()->IgnoreImplicit();
10812 if (auto *CE = dyn_cast<CallExpr>(Val: E))
10813 if (CE->getDirectCallee())
10814 return E;
10815 return nullptr;
10816}
10817
10818StmtResult
10819SemaOpenMP::ActOnOpenMPDispatchDirective(ArrayRef<OMPClause *> Clauses,
10820 Stmt *AStmt, SourceLocation StartLoc,
10821 SourceLocation EndLoc) {
10822 if (!AStmt)
10823 return StmtError();
10824
10825 Stmt *S = cast<CapturedStmt>(Val: AStmt)->getCapturedStmt();
10826
10827 // 5.1 OpenMP
10828 // expression-stmt : an expression statement with one of the following forms:
10829 // expression = target-call ( [expression-list] );
10830 // target-call ( [expression-list] );
10831
10832 SourceLocation TargetCallLoc;
10833
10834 if (!SemaRef.CurContext->isDependentContext()) {
10835 Expr *TargetCall = nullptr;
10836
10837 auto *E = dyn_cast<Expr>(Val: S);
10838 if (!E) {
10839 Diag(Loc: S->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
10840 return StmtError();
10841 }
10842
10843 E = E->IgnoreParenCasts()->IgnoreImplicit();
10844
10845 if (auto *BO = dyn_cast<BinaryOperator>(Val: E)) {
10846 if (BO->getOpcode() == BO_Assign)
10847 TargetCall = getDirectCallExpr(E: BO->getRHS());
10848 } else {
10849 if (auto *COCE = dyn_cast<CXXOperatorCallExpr>(Val: E))
10850 if (COCE->getOperator() == OO_Equal)
10851 TargetCall = getDirectCallExpr(E: COCE->getArg(Arg: 1));
10852 if (!TargetCall)
10853 TargetCall = getDirectCallExpr(E);
10854 }
10855 if (!TargetCall) {
10856 Diag(Loc: E->getBeginLoc(), DiagID: diag::err_omp_dispatch_statement_call);
10857 return StmtError();
10858 }
10859 TargetCallLoc = TargetCall->getExprLoc();
10860 }
10861
10862 SemaRef.setFunctionHasBranchProtectedScope();
10863
10864 return OMPDispatchDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10865 Clauses, AssociatedStmt: AStmt, TargetCallLoc);
10866}
10867
10868static bool checkGenericLoopLastprivate(Sema &S, ArrayRef<OMPClause *> Clauses,
10869 OpenMPDirectiveKind K,
10870 DSAStackTy *Stack) {
10871 bool ErrorFound = false;
10872 for (OMPClause *C : Clauses) {
10873 if (auto *LPC = dyn_cast<OMPLastprivateClause>(Val: C)) {
10874 for (Expr *RefExpr : LPC->varlist()) {
10875 SourceLocation ELoc;
10876 SourceRange ERange;
10877 Expr *SimpleRefExpr = RefExpr;
10878 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange);
10879 if (ValueDecl *D = Res.first) {
10880 auto &&Info = Stack->isLoopControlVariable(D);
10881 if (!Info.first) {
10882 unsigned OMPVersion = S.getLangOpts().OpenMP;
10883 S.Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_loop_var_non_loop_iteration)
10884 << getOpenMPDirectiveName(D: K, Ver: OMPVersion);
10885 ErrorFound = true;
10886 }
10887 }
10888 }
10889 }
10890 }
10891 return ErrorFound;
10892}
10893
10894StmtResult SemaOpenMP::ActOnOpenMPGenericLoopDirective(
10895 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10896 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10897 if (!AStmt)
10898 return StmtError();
10899
10900 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
10901 // A list item may not appear in a lastprivate clause unless it is the
10902 // loop iteration variable of a loop that is associated with the construct.
10903 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_loop, DSAStack))
10904 return StmtError();
10905
10906 setBranchProtectedScope(SemaRef, DKind: OMPD_loop, AStmt);
10907
10908 OMPLoopDirective::HelperExprs B;
10909 // In presence of clause 'collapse', it will define the nested loops number.
10910 unsigned NestedLoopCount = checkOpenMPLoop(
10911 DKind: OMPD_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses), OrderedLoopCountExpr: getOrderedNumberExpr(Clauses),
10912 AStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
10913 if (NestedLoopCount == 0)
10914 return StmtError();
10915
10916 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
10917 "omp loop exprs were not built");
10918
10919 return OMPGenericLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
10920 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10921}
10922
10923StmtResult SemaOpenMP::ActOnOpenMPTeamsGenericLoopDirective(
10924 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10925 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10926 if (!AStmt)
10927 return StmtError();
10928
10929 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
10930 // A list item may not appear in a lastprivate clause unless it is the
10931 // loop iteration variable of a loop that is associated with the construct.
10932 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_teams_loop, DSAStack))
10933 return StmtError();
10934
10935 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_teams_loop, AStmt);
10936
10937 OMPLoopDirective::HelperExprs B;
10938 // In presence of clause 'collapse', it will define the nested loops number.
10939 unsigned NestedLoopCount =
10940 checkOpenMPLoop(DKind: OMPD_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
10941 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
10942 VarsWithImplicitDSA, Built&: B);
10943 if (NestedLoopCount == 0)
10944 return StmtError();
10945
10946 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
10947 "omp loop exprs were not built");
10948
10949 DSAStack->setParentTeamsRegionLoc(StartLoc);
10950
10951 return OMPTeamsGenericLoopDirective::Create(
10952 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
10953}
10954
10955StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsGenericLoopDirective(
10956 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10957 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10958 if (!AStmt)
10959 return StmtError();
10960
10961 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
10962 // A list item may not appear in a lastprivate clause unless it is the
10963 // loop iteration variable of a loop that is associated with the construct.
10964 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_target_teams_loop,
10965 DSAStack))
10966 return StmtError();
10967
10968 CapturedStmt *CS =
10969 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_loop, AStmt);
10970
10971 OMPLoopDirective::HelperExprs B;
10972 // In presence of clause 'collapse', it will define the nested loops number.
10973 unsigned NestedLoopCount =
10974 checkOpenMPLoop(DKind: OMPD_target_teams_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
10975 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
10976 VarsWithImplicitDSA, Built&: B);
10977 if (NestedLoopCount == 0)
10978 return StmtError();
10979
10980 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
10981 "omp loop exprs were not built");
10982
10983 return OMPTargetTeamsGenericLoopDirective::Create(
10984 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
10985 CanBeParallelFor: teamsLoopCanBeParallelFor(AStmt, SemaRef));
10986}
10987
10988StmtResult SemaOpenMP::ActOnOpenMPParallelGenericLoopDirective(
10989 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
10990 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
10991 if (!AStmt)
10992 return StmtError();
10993
10994 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
10995 // A list item may not appear in a lastprivate clause unless it is the
10996 // loop iteration variable of a loop that is associated with the construct.
10997 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_parallel_loop,
10998 DSAStack))
10999 return StmtError();
11000
11001 CapturedStmt *CS =
11002 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_loop, AStmt);
11003
11004 OMPLoopDirective::HelperExprs B;
11005 // In presence of clause 'collapse', it will define the nested loops number.
11006 unsigned NestedLoopCount =
11007 checkOpenMPLoop(DKind: OMPD_parallel_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11008 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11009 VarsWithImplicitDSA, Built&: B);
11010 if (NestedLoopCount == 0)
11011 return StmtError();
11012
11013 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11014 "omp loop exprs were not built");
11015
11016 return OMPParallelGenericLoopDirective::Create(
11017 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11018}
11019
11020StmtResult SemaOpenMP::ActOnOpenMPTargetParallelGenericLoopDirective(
11021 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11022 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11023 if (!AStmt)
11024 return StmtError();
11025
11026 // OpenMP 5.1 [2.11.7, loop construct, Restrictions]
11027 // A list item may not appear in a lastprivate clause unless it is the
11028 // loop iteration variable of a loop that is associated with the construct.
11029 if (checkGenericLoopLastprivate(S&: SemaRef, Clauses, K: OMPD_target_parallel_loop,
11030 DSAStack))
11031 return StmtError();
11032
11033 CapturedStmt *CS =
11034 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_loop, AStmt);
11035
11036 OMPLoopDirective::HelperExprs B;
11037 // In presence of clause 'collapse', it will define the nested loops number.
11038 unsigned NestedLoopCount =
11039 checkOpenMPLoop(DKind: OMPD_target_parallel_loop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11040 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
11041 VarsWithImplicitDSA, Built&: B);
11042 if (NestedLoopCount == 0)
11043 return StmtError();
11044
11045 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
11046 "omp loop exprs were not built");
11047
11048 return OMPTargetParallelGenericLoopDirective::Create(
11049 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11050}
11051
11052StmtResult SemaOpenMP::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
11053 Stmt *AStmt,
11054 SourceLocation StartLoc,
11055 SourceLocation EndLoc) {
11056 if (!AStmt)
11057 return StmtError();
11058
11059 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11060
11061 SemaRef.setFunctionHasBranchProtectedScope();
11062
11063 // OpenMP [2.7.3, single Construct, Restrictions]
11064 // The copyprivate clause must not be used with the nowait clause.
11065 const OMPClause *Nowait = nullptr;
11066 const OMPClause *Copyprivate = nullptr;
11067 for (const OMPClause *Clause : Clauses) {
11068 if (Clause->getClauseKind() == OMPC_nowait)
11069 Nowait = Clause;
11070 else if (Clause->getClauseKind() == OMPC_copyprivate)
11071 Copyprivate = Clause;
11072 if (Copyprivate && Nowait) {
11073 Diag(Loc: Copyprivate->getBeginLoc(),
11074 DiagID: diag::err_omp_single_copyprivate_with_nowait);
11075 Diag(Loc: Nowait->getBeginLoc(), DiagID: diag::note_omp_nowait_clause_here);
11076 return StmtError();
11077 }
11078 }
11079
11080 return OMPSingleDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11081 AssociatedStmt: AStmt);
11082}
11083
11084StmtResult SemaOpenMP::ActOnOpenMPMasterDirective(Stmt *AStmt,
11085 SourceLocation StartLoc,
11086 SourceLocation EndLoc) {
11087 if (!AStmt)
11088 return StmtError();
11089
11090 SemaRef.setFunctionHasBranchProtectedScope();
11091
11092 return OMPMasterDirective::Create(C: getASTContext(), StartLoc, EndLoc, AssociatedStmt: AStmt);
11093}
11094
11095StmtResult SemaOpenMP::ActOnOpenMPMaskedDirective(ArrayRef<OMPClause *> Clauses,
11096 Stmt *AStmt,
11097 SourceLocation StartLoc,
11098 SourceLocation EndLoc) {
11099 if (!AStmt)
11100 return StmtError();
11101
11102 SemaRef.setFunctionHasBranchProtectedScope();
11103
11104 return OMPMaskedDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11105 AssociatedStmt: AStmt);
11106}
11107
11108StmtResult SemaOpenMP::ActOnOpenMPCriticalDirective(
11109 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
11110 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
11111 if (!AStmt)
11112 return StmtError();
11113
11114 bool ErrorFound = false;
11115 llvm::APSInt Hint;
11116 SourceLocation HintLoc;
11117 bool DependentHint = false;
11118 for (const OMPClause *C : Clauses) {
11119 if (C->getClauseKind() == OMPC_hint) {
11120 if (!DirName.getName()) {
11121 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_hint_clause_no_name);
11122 ErrorFound = true;
11123 }
11124 Expr *E = cast<OMPHintClause>(Val: C)->getHint();
11125 if (E->isTypeDependent() || E->isValueDependent() ||
11126 E->isInstantiationDependent()) {
11127 DependentHint = true;
11128 } else {
11129 Hint = E->EvaluateKnownConstInt(Ctx: getASTContext());
11130 HintLoc = C->getBeginLoc();
11131 }
11132 }
11133 }
11134 if (ErrorFound)
11135 return StmtError();
11136 const auto Pair = DSAStack->getCriticalWithHint(Name: DirName);
11137 if (Pair.first && DirName.getName() && !DependentHint) {
11138 if (llvm::APSInt::compareValues(I1: Hint, I2: Pair.second) != 0) {
11139 Diag(Loc: StartLoc, DiagID: diag::err_omp_critical_with_hint);
11140 if (HintLoc.isValid())
11141 Diag(Loc: HintLoc, DiagID: diag::note_omp_critical_hint_here)
11142 << 0 << toString(I: Hint, /*Radix=*/10, /*Signed=*/false);
11143 else
11144 Diag(Loc: StartLoc, DiagID: diag::note_omp_critical_no_hint) << 0;
11145 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
11146 Diag(Loc: C->getBeginLoc(), DiagID: diag::note_omp_critical_hint_here)
11147 << 1
11148 << toString(I: C->getHint()->EvaluateKnownConstInt(Ctx: getASTContext()),
11149 /*Radix=*/10, /*Signed=*/false);
11150 } else {
11151 Diag(Loc: Pair.first->getBeginLoc(), DiagID: diag::note_omp_critical_no_hint) << 1;
11152 }
11153 }
11154 }
11155
11156 SemaRef.setFunctionHasBranchProtectedScope();
11157
11158 auto *Dir = OMPCriticalDirective::Create(C: getASTContext(), Name: DirName, StartLoc,
11159 EndLoc, Clauses, AssociatedStmt: AStmt);
11160 if (!Pair.first && DirName.getName() && !DependentHint)
11161 DSAStack->addCriticalWithHint(D: Dir, Hint);
11162 return Dir;
11163}
11164
11165StmtResult SemaOpenMP::ActOnOpenMPParallelForDirective(
11166 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11167 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11168 if (!AStmt)
11169 return StmtError();
11170
11171 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for, AStmt);
11172
11173 OMPLoopBasedDirective::HelperExprs B;
11174 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11175 // define the nested loops number.
11176 unsigned NestedLoopCount =
11177 checkOpenMPLoop(DKind: OMPD_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11178 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt, SemaRef, DSA&: *DSAStack,
11179 VarsWithImplicitDSA, Built&: B);
11180 if (NestedLoopCount == 0)
11181 return StmtError();
11182
11183 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11184 return StmtError();
11185
11186 return OMPParallelForDirective::Create(
11187 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
11188 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11189}
11190
11191StmtResult SemaOpenMP::ActOnOpenMPParallelForSimdDirective(
11192 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11193 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
11194 if (!AStmt)
11195 return StmtError();
11196
11197 CapturedStmt *CS =
11198 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_for_simd, AStmt);
11199
11200 OMPLoopBasedDirective::HelperExprs B;
11201 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
11202 // define the nested loops number.
11203 unsigned NestedLoopCount =
11204 checkOpenMPLoop(DKind: OMPD_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
11205 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
11206 VarsWithImplicitDSA, Built&: B);
11207 if (NestedLoopCount == 0)
11208 return StmtError();
11209
11210 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
11211 return StmtError();
11212
11213 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
11214 return StmtError();
11215
11216 return OMPParallelForSimdDirective::Create(
11217 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
11218}
11219
11220StmtResult SemaOpenMP::ActOnOpenMPParallelMasterDirective(
11221 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11222 SourceLocation EndLoc) {
11223 if (!AStmt)
11224 return StmtError();
11225
11226 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master, AStmt);
11227
11228 return OMPParallelMasterDirective::Create(
11229 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11230 DSAStack->getTaskgroupReductionRef());
11231}
11232
11233StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedDirective(
11234 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11235 SourceLocation EndLoc) {
11236 if (!AStmt)
11237 return StmtError();
11238
11239 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked, AStmt);
11240
11241 return OMPParallelMaskedDirective::Create(
11242 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11243 DSAStack->getTaskgroupReductionRef());
11244}
11245
11246StmtResult SemaOpenMP::ActOnOpenMPParallelSectionsDirective(
11247 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
11248 SourceLocation EndLoc) {
11249 if (checkSectionsDirective(SemaRef, DKind: OMPD_parallel_sections, AStmt, DSAStack))
11250 return StmtError();
11251
11252 SemaRef.setFunctionHasBranchProtectedScope();
11253
11254 return OMPParallelSectionsDirective::Create(
11255 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
11256 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
11257}
11258
11259/// Find and diagnose mutually exclusive clause kinds.
11260static bool checkMutuallyExclusiveClauses(
11261 Sema &S, ArrayRef<OMPClause *> Clauses,
11262 ArrayRef<OpenMPClauseKind> MutuallyExclusiveClauses) {
11263 const OMPClause *PrevClause = nullptr;
11264 bool ErrorFound = false;
11265 for (const OMPClause *C : Clauses) {
11266 if (llvm::is_contained(Range&: MutuallyExclusiveClauses, Element: C->getClauseKind())) {
11267 if (!PrevClause) {
11268 PrevClause = C;
11269 } else if (PrevClause->getClauseKind() != C->getClauseKind()) {
11270 S.Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_clauses_mutually_exclusive)
11271 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
11272 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
11273 S.Diag(Loc: PrevClause->getBeginLoc(), DiagID: diag::note_omp_previous_clause)
11274 << getOpenMPClauseNameForDiag(C: PrevClause->getClauseKind());
11275 ErrorFound = true;
11276 }
11277 }
11278 }
11279 return ErrorFound;
11280}
11281
11282StmtResult SemaOpenMP::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
11283 Stmt *AStmt,
11284 SourceLocation StartLoc,
11285 SourceLocation EndLoc) {
11286 if (!AStmt)
11287 return StmtError();
11288
11289 // OpenMP 5.0, 2.10.1 task Construct
11290 // If a detach clause appears on the directive, then a mergeable clause cannot
11291 // appear on the same directive.
11292 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
11293 MutuallyExclusiveClauses: {OMPC_detach, OMPC_mergeable}))
11294 return StmtError();
11295
11296 setBranchProtectedScope(SemaRef, DKind: OMPD_task, AStmt);
11297
11298 return OMPTaskDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11299 AssociatedStmt: AStmt, DSAStack->isCancelRegion());
11300}
11301
11302StmtResult SemaOpenMP::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
11303 SourceLocation EndLoc) {
11304 return OMPTaskyieldDirective::Create(C: getASTContext(), StartLoc, EndLoc);
11305}
11306
11307StmtResult SemaOpenMP::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
11308 SourceLocation EndLoc) {
11309 return OMPBarrierDirective::Create(C: getASTContext(), StartLoc, EndLoc);
11310}
11311
11312StmtResult SemaOpenMP::ActOnOpenMPErrorDirective(ArrayRef<OMPClause *> Clauses,
11313 SourceLocation StartLoc,
11314 SourceLocation EndLoc,
11315 bool InExContext) {
11316 const OMPAtClause *AtC =
11317 OMPExecutableDirective::getSingleClause<OMPAtClause>(Clauses);
11318
11319 if (AtC && !InExContext && AtC->getAtKind() == OMPC_AT_execution) {
11320 Diag(Loc: AtC->getAtKindKwLoc(), DiagID: diag::err_omp_unexpected_execution_modifier);
11321 return StmtError();
11322 }
11323
11324 if (!AtC || AtC->getAtKind() == OMPC_AT_compilation) {
11325 const OMPSeverityClause *SeverityC =
11326 OMPExecutableDirective::getSingleClause<OMPSeverityClause>(Clauses);
11327 const OMPMessageClause *MessageC =
11328 OMPExecutableDirective::getSingleClause<OMPMessageClause>(Clauses);
11329 std::optional<std::string> SL =
11330 MessageC ? MessageC->tryEvaluateString(Ctx&: getASTContext()) : std::nullopt;
11331
11332 if (MessageC && !SL)
11333 Diag(Loc: MessageC->getMessageString()->getBeginLoc(),
11334 DiagID: diag::warn_clause_expected_string)
11335 << getOpenMPClauseNameForDiag(C: OMPC_message) << 1;
11336 if (SeverityC && SeverityC->getSeverityKind() == OMPC_SEVERITY_warning)
11337 Diag(Loc: SeverityC->getSeverityKindKwLoc(), DiagID: diag::warn_diagnose_if_succeeded)
11338 << SL.value_or(u: "WARNING");
11339 else
11340 Diag(Loc: StartLoc, DiagID: diag::err_diagnose_if_succeeded) << SL.value_or(u: "ERROR");
11341 if (!SeverityC || SeverityC->getSeverityKind() != OMPC_SEVERITY_warning)
11342 return StmtError();
11343 }
11344
11345 return OMPErrorDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11346}
11347
11348StmtResult
11349SemaOpenMP::ActOnOpenMPTaskwaitDirective(ArrayRef<OMPClause *> Clauses,
11350 SourceLocation StartLoc,
11351 SourceLocation EndLoc) {
11352 const OMPNowaitClause *NowaitC =
11353 OMPExecutableDirective::getSingleClause<OMPNowaitClause>(Clauses);
11354 bool HasDependC =
11355 !OMPExecutableDirective::getClausesOfKind<OMPDependClause>(Clauses)
11356 .empty();
11357 if (NowaitC && !HasDependC) {
11358 Diag(Loc: StartLoc, DiagID: diag::err_omp_nowait_clause_without_depend);
11359 return StmtError();
11360 }
11361
11362 return OMPTaskwaitDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11363 Clauses);
11364}
11365
11366StmtResult
11367SemaOpenMP::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
11368 Stmt *AStmt, SourceLocation StartLoc,
11369 SourceLocation EndLoc) {
11370 if (!AStmt)
11371 return StmtError();
11372
11373 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11374
11375 SemaRef.setFunctionHasBranchProtectedScope();
11376
11377 return OMPTaskgroupDirective::Create(C: getASTContext(), StartLoc, EndLoc,
11378 Clauses, AssociatedStmt: AStmt,
11379 DSAStack->getTaskgroupReductionRef());
11380}
11381
11382StmtResult SemaOpenMP::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
11383 SourceLocation StartLoc,
11384 SourceLocation EndLoc) {
11385 OMPFlushClause *FC = nullptr;
11386 OMPClause *OrderClause = nullptr;
11387 for (OMPClause *C : Clauses) {
11388 if (C->getClauseKind() == OMPC_flush)
11389 FC = cast<OMPFlushClause>(Val: C);
11390 else
11391 OrderClause = C;
11392 }
11393 unsigned OMPVersion = getLangOpts().OpenMP;
11394 OpenMPClauseKind MemOrderKind = OMPC_unknown;
11395 SourceLocation MemOrderLoc;
11396 for (const OMPClause *C : Clauses) {
11397 if (C->getClauseKind() == OMPC_acq_rel ||
11398 C->getClauseKind() == OMPC_acquire ||
11399 C->getClauseKind() == OMPC_release ||
11400 C->getClauseKind() == OMPC_seq_cst /*OpenMP 5.1*/) {
11401 if (MemOrderKind != OMPC_unknown) {
11402 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
11403 << getOpenMPDirectiveName(D: OMPD_flush, Ver: OMPVersion) << 1
11404 << SourceRange(C->getBeginLoc(), C->getEndLoc());
11405 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
11406 << getOpenMPClauseNameForDiag(C: MemOrderKind);
11407 } else {
11408 MemOrderKind = C->getClauseKind();
11409 MemOrderLoc = C->getBeginLoc();
11410 }
11411 }
11412 }
11413 if (FC && OrderClause) {
11414 Diag(Loc: FC->getLParenLoc(), DiagID: diag::err_omp_flush_order_clause_and_list)
11415 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
11416 Diag(Loc: OrderClause->getBeginLoc(), DiagID: diag::note_omp_flush_order_clause_here)
11417 << getOpenMPClauseNameForDiag(C: OrderClause->getClauseKind());
11418 return StmtError();
11419 }
11420 return OMPFlushDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11421}
11422
11423StmtResult SemaOpenMP::ActOnOpenMPDepobjDirective(ArrayRef<OMPClause *> Clauses,
11424 SourceLocation StartLoc,
11425 SourceLocation EndLoc) {
11426 if (Clauses.empty()) {
11427 Diag(Loc: StartLoc, DiagID: diag::err_omp_depobj_expected);
11428 return StmtError();
11429 } else if (Clauses[0]->getClauseKind() != OMPC_depobj) {
11430 Diag(Loc: Clauses[0]->getBeginLoc(), DiagID: diag::err_omp_depobj_expected);
11431 return StmtError();
11432 }
11433 // Only depobj expression and another single clause is allowed.
11434 if (Clauses.size() > 2) {
11435 Diag(Loc: Clauses[2]->getBeginLoc(),
11436 DiagID: diag::err_omp_depobj_single_clause_expected);
11437 return StmtError();
11438 } else if (Clauses.size() < 1) {
11439 Diag(Loc: Clauses[0]->getEndLoc(), DiagID: diag::err_omp_depobj_single_clause_expected);
11440 return StmtError();
11441 }
11442 return OMPDepobjDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11443}
11444
11445StmtResult SemaOpenMP::ActOnOpenMPScanDirective(ArrayRef<OMPClause *> Clauses,
11446 SourceLocation StartLoc,
11447 SourceLocation EndLoc) {
11448 // Check that exactly one clause is specified.
11449 if (Clauses.size() != 1) {
11450 Diag(Loc: Clauses.empty() ? EndLoc : Clauses[1]->getBeginLoc(),
11451 DiagID: diag::err_omp_scan_single_clause_expected);
11452 return StmtError();
11453 }
11454 // Check that scan directive is used in the scope of the OpenMP loop body.
11455 if (Scope *S = DSAStack->getCurScope()) {
11456 Scope *ParentS = S->getParent();
11457 if (!ParentS || ParentS->getParent() != ParentS->getBreakParent() ||
11458 !ParentS->getBreakParent()->isOpenMPLoopScope()) {
11459 unsigned OMPVersion = getLangOpts().OpenMP;
11460 return StmtError(Diag(Loc: StartLoc, DiagID: diag::err_omp_orphaned_device_directive)
11461 << getOpenMPDirectiveName(D: OMPD_scan, Ver: OMPVersion) << 5);
11462 }
11463 }
11464 // Check that only one instance of scan directives is used in the same outer
11465 // region.
11466 if (DSAStack->doesParentHasScanDirective()) {
11467 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "scan";
11468 Diag(DSAStack->getParentScanDirectiveLoc(),
11469 DiagID: diag::note_omp_previous_directive)
11470 << "scan";
11471 return StmtError();
11472 }
11473 DSAStack->setParentHasScanDirective(StartLoc);
11474 return OMPScanDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses);
11475}
11476
11477StmtResult
11478SemaOpenMP::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
11479 Stmt *AStmt, SourceLocation StartLoc,
11480 SourceLocation EndLoc) {
11481 const OMPClause *DependFound = nullptr;
11482 const OMPClause *DependSourceClause = nullptr;
11483 const OMPClause *DependSinkClause = nullptr;
11484 const OMPClause *DoacrossFound = nullptr;
11485 const OMPClause *DoacrossSourceClause = nullptr;
11486 const OMPClause *DoacrossSinkClause = nullptr;
11487 bool ErrorFound = false;
11488 const OMPThreadsClause *TC = nullptr;
11489 const OMPSIMDClause *SC = nullptr;
11490 for (const OMPClause *C : Clauses) {
11491 auto DOC = dyn_cast<OMPDoacrossClause>(Val: C);
11492 auto DC = dyn_cast<OMPDependClause>(Val: C);
11493 if (DC || DOC) {
11494 DependFound = DC ? C : nullptr;
11495 DoacrossFound = DOC ? C : nullptr;
11496 OMPDoacrossKind ODK;
11497 if ((DC && DC->getDependencyKind() == OMPC_DEPEND_source) ||
11498 (DOC && (ODK.isSource(C: DOC)))) {
11499 if ((DC && DependSourceClause) || (DOC && DoacrossSourceClause)) {
11500 unsigned OMPVersion = getLangOpts().OpenMP;
11501 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_more_one_clause)
11502 << getOpenMPDirectiveName(D: OMPD_ordered, Ver: OMPVersion)
11503 << getOpenMPClauseNameForDiag(C: DC ? OMPC_depend : OMPC_doacross)
11504 << 2;
11505 ErrorFound = true;
11506 } else {
11507 if (DC)
11508 DependSourceClause = C;
11509 else
11510 DoacrossSourceClause = C;
11511 }
11512 if ((DC && DependSinkClause) || (DOC && DoacrossSinkClause)) {
11513 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
11514 << (DC ? "depend" : "doacross") << 0;
11515 ErrorFound = true;
11516 }
11517 } else if ((DC && DC->getDependencyKind() == OMPC_DEPEND_sink) ||
11518 (DOC && (ODK.isSink(C: DOC) || ODK.isSinkIter(C: DOC)))) {
11519 if (DependSourceClause || DoacrossSourceClause) {
11520 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_sink_and_source_not_allowed)
11521 << (DC ? "depend" : "doacross") << 1;
11522 ErrorFound = true;
11523 }
11524 if (DC)
11525 DependSinkClause = C;
11526 else
11527 DoacrossSinkClause = C;
11528 }
11529 } else if (C->getClauseKind() == OMPC_threads) {
11530 TC = cast<OMPThreadsClause>(Val: C);
11531 } else if (C->getClauseKind() == OMPC_simd) {
11532 SC = cast<OMPSIMDClause>(Val: C);
11533 }
11534 }
11535 if (!ErrorFound && !SC &&
11536 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
11537 // OpenMP [2.8.1,simd Construct, Restrictions]
11538 // An ordered construct with the simd clause is the only OpenMP construct
11539 // that can appear in the simd region.
11540 Diag(Loc: StartLoc, DiagID: diag::err_omp_prohibited_region_simd)
11541 << (getLangOpts().OpenMP >= 50 ? 1 : 0);
11542 ErrorFound = true;
11543 } else if ((DependFound || DoacrossFound) && (TC || SC)) {
11544 SourceLocation Loc =
11545 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
11546 Diag(Loc, DiagID: diag::err_omp_depend_clause_thread_simd)
11547 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend : OMPC_doacross)
11548 << getOpenMPClauseNameForDiag(C: TC ? TC->getClauseKind()
11549 : SC->getClauseKind());
11550 ErrorFound = true;
11551 } else if ((DependFound || DoacrossFound) &&
11552 !DSAStack->getParentOrderedRegionParam().first) {
11553 SourceLocation Loc =
11554 DependFound ? DependFound->getBeginLoc() : DoacrossFound->getBeginLoc();
11555 Diag(Loc, DiagID: diag::err_omp_ordered_directive_without_param)
11556 << getOpenMPClauseNameForDiag(C: DependFound ? OMPC_depend
11557 : OMPC_doacross);
11558 ErrorFound = true;
11559 } else if (TC || Clauses.empty()) {
11560 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
11561 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
11562 Diag(Loc: ErrLoc, DiagID: diag::err_omp_ordered_directive_with_param)
11563 << (TC != nullptr);
11564 Diag(Loc: Param->getBeginLoc(), DiagID: diag::note_omp_ordered_param) << 1;
11565 ErrorFound = true;
11566 }
11567 }
11568 if ((!AStmt && !DependFound && !DoacrossFound) || ErrorFound)
11569 return StmtError();
11570
11571 // OpenMP 5.0, 2.17.9, ordered Construct, Restrictions.
11572 // During execution of an iteration of a worksharing-loop or a loop nest
11573 // within a worksharing-loop, simd, or worksharing-loop SIMD region, a thread
11574 // must not execute more than one ordered region corresponding to an ordered
11575 // construct without a depend clause.
11576 if (!DependFound && !DoacrossFound) {
11577 if (DSAStack->doesParentHasOrderedDirective()) {
11578 Diag(Loc: StartLoc, DiagID: diag::err_omp_several_directives_in_region) << "ordered";
11579 Diag(DSAStack->getParentOrderedDirectiveLoc(),
11580 DiagID: diag::note_omp_previous_directive)
11581 << "ordered";
11582 return StmtError();
11583 }
11584 DSAStack->setParentHasOrderedDirective(StartLoc);
11585 }
11586
11587 if (AStmt) {
11588 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
11589
11590 SemaRef.setFunctionHasBranchProtectedScope();
11591 }
11592
11593 return OMPOrderedDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
11594 AssociatedStmt: AStmt);
11595}
11596
11597namespace {
11598/// Helper class for checking expression in 'omp atomic [update]'
11599/// construct.
11600class OpenMPAtomicUpdateChecker {
11601 /// Error results for atomic update expressions.
11602 enum ExprAnalysisErrorCode {
11603 /// A statement is not an expression statement.
11604 NotAnExpression,
11605 /// Expression is not builtin binary or unary operation.
11606 NotABinaryOrUnaryExpression,
11607 /// Unary operation is not post-/pre- increment/decrement operation.
11608 NotAnUnaryIncDecExpression,
11609 /// An expression is not of scalar type.
11610 NotAScalarType,
11611 /// A binary operation is not an assignment operation.
11612 NotAnAssignmentOp,
11613 /// RHS part of the binary operation is not a binary expression.
11614 NotABinaryExpression,
11615 /// RHS part is not additive/multiplicative/shift/bitwise binary
11616 /// expression.
11617 NotABinaryOperator,
11618 /// RHS binary operation does not have reference to the updated LHS
11619 /// part.
11620 NotAnUpdateExpression,
11621 /// An expression contains semantical error not related to
11622 /// 'omp atomic [update]'
11623 NotAValidExpression,
11624 /// No errors is found.
11625 NoError
11626 };
11627 /// Reference to Sema.
11628 Sema &SemaRef;
11629 /// A location for note diagnostics (when error is found).
11630 SourceLocation NoteLoc;
11631 /// 'x' lvalue part of the source atomic expression.
11632 Expr *X;
11633 /// 'expr' rvalue part of the source atomic expression.
11634 Expr *E;
11635 /// Helper expression of the form
11636 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
11637 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
11638 Expr *UpdateExpr;
11639 /// Is 'x' a LHS in a RHS part of full update expression. It is
11640 /// important for non-associative operations.
11641 bool IsXLHSInRHSPart;
11642 BinaryOperatorKind Op;
11643 SourceLocation OpLoc;
11644 /// true if the source expression is a postfix unary operation, false
11645 /// if it is a prefix unary operation.
11646 bool IsPostfixUpdate;
11647
11648public:
11649 OpenMPAtomicUpdateChecker(Sema &SemaRef)
11650 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
11651 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
11652 /// Check specified statement that it is suitable for 'atomic update'
11653 /// constructs and extract 'x', 'expr' and Operation from the original
11654 /// expression. If DiagId and NoteId == 0, then only check is performed
11655 /// without error notification.
11656 /// \param DiagId Diagnostic which should be emitted if error is found.
11657 /// \param NoteId Diagnostic note for the main error message.
11658 /// \return true if statement is not an update expression, false otherwise.
11659 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
11660 /// Return the 'x' lvalue part of the source atomic expression.
11661 Expr *getX() const { return X; }
11662 /// Return the 'expr' rvalue part of the source atomic expression.
11663 Expr *getExpr() const { return E; }
11664 /// Return the update expression used in calculation of the updated
11665 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
11666 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
11667 Expr *getUpdateExpr() const { return UpdateExpr; }
11668 /// Return true if 'x' is LHS in RHS part of full update expression,
11669 /// false otherwise.
11670 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
11671
11672 /// true if the source expression is a postfix unary operation, false
11673 /// if it is a prefix unary operation.
11674 bool isPostfixUpdate() const { return IsPostfixUpdate; }
11675
11676private:
11677 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
11678 unsigned NoteId = 0);
11679};
11680
11681bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
11682 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
11683 ExprAnalysisErrorCode ErrorFound = NoError;
11684 SourceLocation ErrorLoc, NoteLoc;
11685 SourceRange ErrorRange, NoteRange;
11686 // Allowed constructs are:
11687 // x = x binop expr;
11688 // x = expr binop x;
11689 if (AtomicBinOp->getOpcode() == BO_Assign) {
11690 X = AtomicBinOp->getLHS();
11691 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
11692 Val: AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
11693 if (AtomicInnerBinOp->isMultiplicativeOp() ||
11694 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
11695 AtomicInnerBinOp->isBitwiseOp()) {
11696 Op = AtomicInnerBinOp->getOpcode();
11697 OpLoc = AtomicInnerBinOp->getOperatorLoc();
11698 Expr *LHS = AtomicInnerBinOp->getLHS();
11699 Expr *RHS = AtomicInnerBinOp->getRHS();
11700 llvm::FoldingSetNodeID XId, LHSId, RHSId;
11701 X->IgnoreParenImpCasts()->Profile(ID&: XId, Context: SemaRef.getASTContext(),
11702 /*Canonical=*/true);
11703 LHS->IgnoreParenImpCasts()->Profile(ID&: LHSId, Context: SemaRef.getASTContext(),
11704 /*Canonical=*/true);
11705 RHS->IgnoreParenImpCasts()->Profile(ID&: RHSId, Context: SemaRef.getASTContext(),
11706 /*Canonical=*/true);
11707 if (XId == LHSId) {
11708 E = RHS;
11709 IsXLHSInRHSPart = true;
11710 } else if (XId == RHSId) {
11711 E = LHS;
11712 IsXLHSInRHSPart = false;
11713 } else {
11714 ErrorLoc = AtomicInnerBinOp->getExprLoc();
11715 ErrorRange = AtomicInnerBinOp->getSourceRange();
11716 NoteLoc = X->getExprLoc();
11717 NoteRange = X->getSourceRange();
11718 ErrorFound = NotAnUpdateExpression;
11719 }
11720 } else {
11721 ErrorLoc = AtomicInnerBinOp->getExprLoc();
11722 ErrorRange = AtomicInnerBinOp->getSourceRange();
11723 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
11724 NoteRange = SourceRange(NoteLoc, NoteLoc);
11725 ErrorFound = NotABinaryOperator;
11726 }
11727 } else {
11728 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
11729 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
11730 ErrorFound = NotABinaryExpression;
11731 }
11732 } else {
11733 ErrorLoc = AtomicBinOp->getExprLoc();
11734 ErrorRange = AtomicBinOp->getSourceRange();
11735 NoteLoc = AtomicBinOp->getOperatorLoc();
11736 NoteRange = SourceRange(NoteLoc, NoteLoc);
11737 ErrorFound = NotAnAssignmentOp;
11738 }
11739 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
11740 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
11741 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
11742 return true;
11743 }
11744 if (SemaRef.CurContext->isDependentContext())
11745 E = X = UpdateExpr = nullptr;
11746 return ErrorFound != NoError;
11747}
11748
11749bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
11750 unsigned NoteId) {
11751 ExprAnalysisErrorCode ErrorFound = NoError;
11752 SourceLocation ErrorLoc, NoteLoc;
11753 SourceRange ErrorRange, NoteRange;
11754 // Allowed constructs are:
11755 // x++;
11756 // x--;
11757 // ++x;
11758 // --x;
11759 // x binop= expr;
11760 // x = x binop expr;
11761 // x = expr binop x;
11762 if (auto *AtomicBody = dyn_cast<Expr>(Val: S)) {
11763 AtomicBody = AtomicBody->IgnoreParenImpCasts();
11764 if (AtomicBody->getType()->isScalarType() ||
11765 AtomicBody->isInstantiationDependent()) {
11766 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
11767 Val: AtomicBody->IgnoreParenImpCasts())) {
11768 // Check for Compound Assignment Operation
11769 Op = BinaryOperator::getOpForCompoundAssignment(
11770 Opc: AtomicCompAssignOp->getOpcode());
11771 OpLoc = AtomicCompAssignOp->getOperatorLoc();
11772 E = AtomicCompAssignOp->getRHS();
11773 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
11774 IsXLHSInRHSPart = true;
11775 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
11776 Val: AtomicBody->IgnoreParenImpCasts())) {
11777 // Check for Binary Operation
11778 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
11779 return true;
11780 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
11781 Val: AtomicBody->IgnoreParenImpCasts())) {
11782 // Check for Unary Operation
11783 if (AtomicUnaryOp->isIncrementDecrementOp()) {
11784 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
11785 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
11786 OpLoc = AtomicUnaryOp->getOperatorLoc();
11787 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
11788 E = SemaRef.ActOnIntegerConstant(Loc: OpLoc, /*uint64_t Val=*/Val: 1).get();
11789 IsXLHSInRHSPart = true;
11790 } else {
11791 ErrorFound = NotAnUnaryIncDecExpression;
11792 ErrorLoc = AtomicUnaryOp->getExprLoc();
11793 ErrorRange = AtomicUnaryOp->getSourceRange();
11794 NoteLoc = AtomicUnaryOp->getOperatorLoc();
11795 NoteRange = SourceRange(NoteLoc, NoteLoc);
11796 }
11797 } else if (!AtomicBody->isInstantiationDependent()) {
11798 ErrorFound = NotABinaryOrUnaryExpression;
11799 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
11800 NoteRange = ErrorRange = AtomicBody->getSourceRange();
11801 } else if (AtomicBody->containsErrors()) {
11802 ErrorFound = NotAValidExpression;
11803 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
11804 NoteRange = ErrorRange = AtomicBody->getSourceRange();
11805 }
11806 } else {
11807 ErrorFound = NotAScalarType;
11808 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
11809 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11810 }
11811 } else {
11812 ErrorFound = NotAnExpression;
11813 NoteLoc = ErrorLoc = S->getBeginLoc();
11814 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
11815 }
11816 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
11817 SemaRef.Diag(Loc: ErrorLoc, DiagID: DiagId) << ErrorRange;
11818 SemaRef.Diag(Loc: NoteLoc, DiagID: NoteId) << ErrorFound << NoteRange;
11819 return true;
11820 }
11821 if (SemaRef.CurContext->isDependentContext())
11822 E = X = UpdateExpr = nullptr;
11823 if (ErrorFound == NoError && E && X) {
11824 // Build an update expression of form 'OpaqueValueExpr(x) binop
11825 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
11826 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
11827 auto *OVEX = new (SemaRef.getASTContext())
11828 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_PRValue);
11829 auto *OVEExpr = new (SemaRef.getASTContext())
11830 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_PRValue);
11831 ExprResult Update =
11832 SemaRef.CreateBuiltinBinOp(OpLoc, Opc: Op, LHSExpr: IsXLHSInRHSPart ? OVEX : OVEExpr,
11833 RHSExpr: IsXLHSInRHSPart ? OVEExpr : OVEX);
11834 if (Update.isInvalid())
11835 return true;
11836 Update = SemaRef.PerformImplicitConversion(From: Update.get(), ToType: X->getType(),
11837 Action: AssignmentAction::Casting);
11838 if (Update.isInvalid())
11839 return true;
11840 UpdateExpr = Update.get();
11841 }
11842 return ErrorFound != NoError;
11843}
11844
11845/// Get the node id of the fixed point of an expression \a S.
11846llvm::FoldingSetNodeID getNodeId(ASTContext &Context, const Expr *S) {
11847 llvm::FoldingSetNodeID Id;
11848 S->IgnoreParenImpCasts()->Profile(ID&: Id, Context, Canonical: true);
11849 return Id;
11850}
11851
11852/// Check if two expressions are same.
11853bool checkIfTwoExprsAreSame(ASTContext &Context, const Expr *LHS,
11854 const Expr *RHS) {
11855 return getNodeId(Context, S: LHS) == getNodeId(Context, S: RHS);
11856}
11857
11858class OpenMPAtomicCompareChecker {
11859public:
11860 /// All kinds of errors that can occur in `atomic compare`
11861 enum ErrorTy {
11862 /// Empty compound statement.
11863 NoStmt = 0,
11864 /// More than one statement in a compound statement.
11865 MoreThanOneStmt,
11866 /// Not an assignment binary operator.
11867 NotAnAssignment,
11868 /// Not a conditional operator.
11869 NotCondOp,
11870 /// Wrong false expr. According to the spec, 'x' should be at the false
11871 /// expression of a conditional expression.
11872 WrongFalseExpr,
11873 /// The condition of a conditional expression is not a binary operator.
11874 NotABinaryOp,
11875 /// Invalid binary operator (not <, >, or ==).
11876 InvalidBinaryOp,
11877 /// Invalid comparison (not x == e, e == x, x ordop expr, or expr ordop x).
11878 InvalidComparison,
11879 /// X is not a lvalue.
11880 XNotLValue,
11881 /// Not a scalar.
11882 NotScalar,
11883 /// Not an integer.
11884 NotInteger,
11885 /// 'else' statement is not expected.
11886 UnexpectedElse,
11887 /// Not an equality operator.
11888 NotEQ,
11889 /// Invalid assignment (not v == x).
11890 InvalidAssignment,
11891 /// Not if statement
11892 NotIfStmt,
11893 /// More than two statements in a compound statement.
11894 MoreThanTwoStmts,
11895 /// Not a compound statement.
11896 NotCompoundStmt,
11897 /// No else statement.
11898 NoElse,
11899 /// Not 'if (r)'.
11900 InvalidCondition,
11901 /// No error.
11902 NoError,
11903 };
11904
11905 struct ErrorInfoTy {
11906 ErrorTy Error;
11907 SourceLocation ErrorLoc;
11908 SourceRange ErrorRange;
11909 SourceLocation NoteLoc;
11910 SourceRange NoteRange;
11911 };
11912
11913 OpenMPAtomicCompareChecker(Sema &S) : ContextRef(S.getASTContext()) {}
11914
11915 /// Check if statement \a S is valid for <tt>atomic compare</tt>.
11916 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
11917
11918 Expr *getX() const { return X; }
11919 Expr *getE() const { return E; }
11920 Expr *getD() const { return D; }
11921 Expr *getCond() const { return C; }
11922 bool isXBinopExpr() const { return IsXBinopExpr; }
11923
11924protected:
11925 /// Reference to ASTContext
11926 ASTContext &ContextRef;
11927 /// 'x' lvalue part of the source atomic expression.
11928 Expr *X = nullptr;
11929 /// 'expr' or 'e' rvalue part of the source atomic expression.
11930 Expr *E = nullptr;
11931 /// 'd' rvalue part of the source atomic expression.
11932 Expr *D = nullptr;
11933 /// 'cond' part of the source atomic expression. It is in one of the following
11934 /// forms:
11935 /// expr ordop x
11936 /// x ordop expr
11937 /// x == e
11938 /// e == x
11939 Expr *C = nullptr;
11940 /// True if the cond expr is in the form of 'x ordop expr'.
11941 bool IsXBinopExpr = true;
11942
11943 /// Check if it is a valid conditional update statement (cond-update-stmt).
11944 bool checkCondUpdateStmt(IfStmt *S, ErrorInfoTy &ErrorInfo);
11945
11946 /// Check if it is a valid conditional expression statement (cond-expr-stmt).
11947 bool checkCondExprStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
11948
11949 /// Check if all captured values have right type.
11950 bool checkType(ErrorInfoTy &ErrorInfo) const;
11951
11952 static bool CheckValue(const Expr *E, ErrorInfoTy &ErrorInfo,
11953 bool ShouldBeLValue, bool ShouldBeInteger = false) {
11954 if (E->isInstantiationDependent())
11955 return true;
11956
11957 if (ShouldBeLValue && !E->isLValue()) {
11958 ErrorInfo.Error = ErrorTy::XNotLValue;
11959 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
11960 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
11961 return false;
11962 }
11963
11964 QualType QTy = E->getType();
11965 if (!QTy->isScalarType()) {
11966 ErrorInfo.Error = ErrorTy::NotScalar;
11967 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
11968 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
11969 return false;
11970 }
11971 if (ShouldBeInteger && !QTy->isIntegerType()) {
11972 ErrorInfo.Error = ErrorTy::NotInteger;
11973 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = E->getExprLoc();
11974 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = E->getSourceRange();
11975 return false;
11976 }
11977
11978 return true;
11979 }
11980};
11981
11982bool OpenMPAtomicCompareChecker::checkCondUpdateStmt(IfStmt *S,
11983 ErrorInfoTy &ErrorInfo) {
11984 auto *Then = S->getThen();
11985 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
11986 if (CS->body_empty()) {
11987 ErrorInfo.Error = ErrorTy::NoStmt;
11988 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
11989 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
11990 return false;
11991 }
11992 if (CS->size() > 1) {
11993 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
11994 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
11995 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
11996 return false;
11997 }
11998 Then = CS->body_front();
11999 }
12000
12001 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
12002 if (!BO) {
12003 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12004 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
12005 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
12006 return false;
12007 }
12008 if (BO->getOpcode() != BO_Assign) {
12009 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12010 ErrorInfo.ErrorLoc = BO->getExprLoc();
12011 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12012 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12013 return false;
12014 }
12015
12016 X = BO->getLHS();
12017
12018 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
12019 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
12020 Expr *LHS = nullptr;
12021 Expr *RHS = nullptr;
12022 if (Cond) {
12023 LHS = Cond->getLHS();
12024 RHS = Cond->getRHS();
12025 } else if (Call) {
12026 LHS = Call->getArg(Arg: 0);
12027 RHS = Call->getArg(Arg: 1);
12028 } else {
12029 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12030 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12031 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12032 return false;
12033 }
12034
12035 if ((Cond && Cond->getOpcode() == BO_EQ) ||
12036 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
12037 C = S->getCond();
12038 D = BO->getRHS();
12039 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12040 E = RHS;
12041 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12042 E = LHS;
12043 } else {
12044 ErrorInfo.Error = ErrorTy::InvalidComparison;
12045 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12046 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12047 S->getCond()->getSourceRange();
12048 return false;
12049 }
12050 } else if ((Cond &&
12051 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
12052 (Call &&
12053 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
12054 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
12055 E = BO->getRHS();
12056 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
12057 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
12058 C = S->getCond();
12059 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
12060 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12061 C = S->getCond();
12062 IsXBinopExpr = false;
12063 } else {
12064 ErrorInfo.Error = ErrorTy::InvalidComparison;
12065 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12066 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12067 S->getCond()->getSourceRange();
12068 return false;
12069 }
12070 } else {
12071 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
12072 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12073 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12074 return false;
12075 }
12076
12077 if (S->getElse()) {
12078 ErrorInfo.Error = ErrorTy::UnexpectedElse;
12079 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getElse()->getBeginLoc();
12080 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getElse()->getSourceRange();
12081 return false;
12082 }
12083
12084 return true;
12085}
12086
12087bool OpenMPAtomicCompareChecker::checkCondExprStmt(Stmt *S,
12088 ErrorInfoTy &ErrorInfo) {
12089 auto *BO = dyn_cast<BinaryOperator>(Val: S);
12090 if (!BO) {
12091 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12092 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12093 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12094 return false;
12095 }
12096 if (BO->getOpcode() != BO_Assign) {
12097 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12098 ErrorInfo.ErrorLoc = BO->getExprLoc();
12099 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12100 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12101 return false;
12102 }
12103
12104 X = BO->getLHS();
12105
12106 auto *CO = dyn_cast<ConditionalOperator>(Val: BO->getRHS()->IgnoreParenImpCasts());
12107 if (!CO) {
12108 ErrorInfo.Error = ErrorTy::NotCondOp;
12109 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getRHS()->getExprLoc();
12110 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getRHS()->getSourceRange();
12111 return false;
12112 }
12113
12114 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: CO->getFalseExpr())) {
12115 ErrorInfo.Error = ErrorTy::WrongFalseExpr;
12116 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getFalseExpr()->getExprLoc();
12117 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12118 CO->getFalseExpr()->getSourceRange();
12119 return false;
12120 }
12121
12122 auto *Cond = dyn_cast<BinaryOperator>(Val: CO->getCond());
12123 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: CO->getCond());
12124 Expr *LHS = nullptr;
12125 Expr *RHS = nullptr;
12126 if (Cond) {
12127 LHS = Cond->getLHS();
12128 RHS = Cond->getRHS();
12129 } else if (Call) {
12130 LHS = Call->getArg(Arg: 0);
12131 RHS = Call->getArg(Arg: 1);
12132 } else {
12133 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12134 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12135 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12136 CO->getCond()->getSourceRange();
12137 return false;
12138 }
12139
12140 if ((Cond && Cond->getOpcode() == BO_EQ) ||
12141 (Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
12142 C = CO->getCond();
12143 D = CO->getTrueExpr();
12144 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12145 E = RHS;
12146 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12147 E = LHS;
12148 } else {
12149 ErrorInfo.Error = ErrorTy::InvalidComparison;
12150 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12151 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12152 CO->getCond()->getSourceRange();
12153 return false;
12154 }
12155 } else if ((Cond &&
12156 (Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
12157 (Call &&
12158 (Call->getOperator() == OverloadedOperatorKind::OO_Less ||
12159 Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
12160
12161 E = CO->getTrueExpr();
12162 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS) &&
12163 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS)) {
12164 C = CO->getCond();
12165 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: E, RHS: LHS) &&
12166 checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12167 C = CO->getCond();
12168 IsXBinopExpr = false;
12169 } else {
12170 ErrorInfo.Error = ErrorTy::InvalidComparison;
12171 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12172 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12173 CO->getCond()->getSourceRange();
12174 return false;
12175 }
12176 } else {
12177 ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
12178 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
12179 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12180 CO->getCond()->getSourceRange();
12181 return false;
12182 }
12183
12184 return true;
12185}
12186
12187bool OpenMPAtomicCompareChecker::checkType(ErrorInfoTy &ErrorInfo) const {
12188 // 'x' and 'e' cannot be nullptr
12189 assert(X && E && "X and E cannot be nullptr");
12190
12191 if (!CheckValue(E: X, ErrorInfo, ShouldBeLValue: true))
12192 return false;
12193
12194 if (!CheckValue(E, ErrorInfo, ShouldBeLValue: false))
12195 return false;
12196
12197 if (D && !CheckValue(E: D, ErrorInfo, ShouldBeLValue: false))
12198 return false;
12199
12200 return true;
12201}
12202
12203bool OpenMPAtomicCompareChecker::checkStmt(
12204 Stmt *S, OpenMPAtomicCompareChecker::ErrorInfoTy &ErrorInfo) {
12205 auto *CS = dyn_cast<CompoundStmt>(Val: S);
12206 if (CS) {
12207 if (CS->body_empty()) {
12208 ErrorInfo.Error = ErrorTy::NoStmt;
12209 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12210 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12211 return false;
12212 }
12213
12214 if (CS->size() != 1) {
12215 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12216 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12217 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12218 return false;
12219 }
12220 S = CS->body_front();
12221 }
12222
12223 auto Res = false;
12224
12225 if (auto *IS = dyn_cast<IfStmt>(Val: S)) {
12226 // Check if the statement is in one of the following forms
12227 // (cond-update-stmt):
12228 // if (expr ordop x) { x = expr; }
12229 // if (x ordop expr) { x = expr; }
12230 // if (x == e) { x = d; }
12231 Res = checkCondUpdateStmt(S: IS, ErrorInfo);
12232 } else {
12233 // Check if the statement is in one of the following forms (cond-expr-stmt):
12234 // x = expr ordop x ? expr : x;
12235 // x = x ordop expr ? expr : x;
12236 // x = x == e ? d : x;
12237 Res = checkCondExprStmt(S, ErrorInfo);
12238 }
12239
12240 if (!Res)
12241 return false;
12242
12243 return checkType(ErrorInfo);
12244}
12245
12246class OpenMPAtomicCompareCaptureChecker final
12247 : public OpenMPAtomicCompareChecker {
12248public:
12249 OpenMPAtomicCompareCaptureChecker(Sema &S) : OpenMPAtomicCompareChecker(S) {}
12250
12251 Expr *getV() const { return V; }
12252 Expr *getR() const { return R; }
12253 bool isFailOnly() const { return IsFailOnly; }
12254 bool isPostfixUpdate() const { return IsPostfixUpdate; }
12255
12256 /// Check if statement \a S is valid for <tt>atomic compare capture</tt>.
12257 bool checkStmt(Stmt *S, ErrorInfoTy &ErrorInfo);
12258
12259private:
12260 bool checkType(ErrorInfoTy &ErrorInfo);
12261
12262 // NOTE: Form 3, 4, 5 in the following comments mean the 3rd, 4th, and 5th
12263 // form of 'conditional-update-capture-atomic' structured block on the v5.2
12264 // spec p.p. 82:
12265 // (1) { v = x; cond-update-stmt }
12266 // (2) { cond-update-stmt v = x; }
12267 // (3) if(x == e) { x = d; } else { v = x; }
12268 // (4) { r = x == e; if(r) { x = d; } }
12269 // (5) { r = x == e; if(r) { x = d; } else { v = x; } }
12270
12271 /// Check if it is valid 'if(x == e) { x = d; } else { v = x; }' (form 3)
12272 bool checkForm3(IfStmt *S, ErrorInfoTy &ErrorInfo);
12273
12274 /// Check if it is valid '{ r = x == e; if(r) { x = d; } }',
12275 /// or '{ r = x == e; if(r) { x = d; } else { v = x; } }' (form 4 and 5)
12276 bool checkForm45(Stmt *S, ErrorInfoTy &ErrorInfo);
12277
12278 /// 'v' lvalue part of the source atomic expression.
12279 Expr *V = nullptr;
12280 /// 'r' lvalue part of the source atomic expression.
12281 Expr *R = nullptr;
12282 /// If 'v' is only updated when the comparison fails.
12283 bool IsFailOnly = false;
12284 /// If original value of 'x' must be stored in 'v', not an updated one.
12285 bool IsPostfixUpdate = false;
12286};
12287
12288bool OpenMPAtomicCompareCaptureChecker::checkType(ErrorInfoTy &ErrorInfo) {
12289 if (!OpenMPAtomicCompareChecker::checkType(ErrorInfo))
12290 return false;
12291
12292 if (V && !CheckValue(E: V, ErrorInfo, ShouldBeLValue: true))
12293 return false;
12294
12295 if (R && !CheckValue(E: R, ErrorInfo, ShouldBeLValue: true, ShouldBeInteger: true))
12296 return false;
12297
12298 return true;
12299}
12300
12301bool OpenMPAtomicCompareCaptureChecker::checkForm3(IfStmt *S,
12302 ErrorInfoTy &ErrorInfo) {
12303 IsFailOnly = true;
12304
12305 auto *Then = S->getThen();
12306 if (auto *CS = dyn_cast<CompoundStmt>(Val: Then)) {
12307 if (CS->body_empty()) {
12308 ErrorInfo.Error = ErrorTy::NoStmt;
12309 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12310 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12311 return false;
12312 }
12313 if (CS->size() > 1) {
12314 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12315 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12316 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12317 return false;
12318 }
12319 Then = CS->body_front();
12320 }
12321
12322 auto *BO = dyn_cast<BinaryOperator>(Val: Then);
12323 if (!BO) {
12324 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12325 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Then->getBeginLoc();
12326 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Then->getSourceRange();
12327 return false;
12328 }
12329 if (BO->getOpcode() != BO_Assign) {
12330 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12331 ErrorInfo.ErrorLoc = BO->getExprLoc();
12332 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12333 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12334 return false;
12335 }
12336
12337 X = BO->getLHS();
12338 D = BO->getRHS();
12339
12340 auto *Cond = dyn_cast<BinaryOperator>(Val: S->getCond());
12341 auto *Call = dyn_cast<CXXOperatorCallExpr>(Val: S->getCond());
12342 Expr *LHS = nullptr;
12343 Expr *RHS = nullptr;
12344 if (Cond) {
12345 LHS = Cond->getLHS();
12346 RHS = Cond->getRHS();
12347 } else if (Call) {
12348 LHS = Call->getArg(Arg: 0);
12349 RHS = Call->getArg(Arg: 1);
12350 } else {
12351 ErrorInfo.Error = ErrorTy::NotABinaryOp;
12352 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12353 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12354 return false;
12355 }
12356 if ((Cond && Cond->getOpcode() != BO_EQ) ||
12357 (Call && Call->getOperator() != OverloadedOperatorKind::OO_EqualEqual)) {
12358 ErrorInfo.Error = ErrorTy::NotEQ;
12359 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12360 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12361 return false;
12362 }
12363
12364 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: LHS)) {
12365 E = RHS;
12366 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS)) {
12367 E = LHS;
12368 } else {
12369 ErrorInfo.Error = ErrorTy::InvalidComparison;
12370 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
12371 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
12372 return false;
12373 }
12374
12375 C = S->getCond();
12376
12377 if (!S->getElse()) {
12378 ErrorInfo.Error = ErrorTy::NoElse;
12379 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12380 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12381 return false;
12382 }
12383
12384 auto *Else = S->getElse();
12385 if (auto *CS = dyn_cast<CompoundStmt>(Val: Else)) {
12386 if (CS->body_empty()) {
12387 ErrorInfo.Error = ErrorTy::NoStmt;
12388 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12389 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12390 return false;
12391 }
12392 if (CS->size() > 1) {
12393 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12394 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12395 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12396 return false;
12397 }
12398 Else = CS->body_front();
12399 }
12400
12401 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
12402 if (!ElseBO) {
12403 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12404 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
12405 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
12406 return false;
12407 }
12408 if (ElseBO->getOpcode() != BO_Assign) {
12409 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12410 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
12411 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
12412 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
12413 return false;
12414 }
12415
12416 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
12417 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12418 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseBO->getRHS()->getExprLoc();
12419 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12420 ElseBO->getRHS()->getSourceRange();
12421 return false;
12422 }
12423
12424 V = ElseBO->getLHS();
12425
12426 return checkType(ErrorInfo);
12427}
12428
12429bool OpenMPAtomicCompareCaptureChecker::checkForm45(Stmt *S,
12430 ErrorInfoTy &ErrorInfo) {
12431 // We don't check here as they should be already done before call this
12432 // function.
12433 auto *CS = cast<CompoundStmt>(Val: S);
12434 assert(CS->size() == 2 && "CompoundStmt size is not expected");
12435 auto *S1 = cast<BinaryOperator>(Val: CS->body_front());
12436 auto *S2 = cast<IfStmt>(Val: CS->body_back());
12437 assert(S1->getOpcode() == BO_Assign && "unexpected binary operator");
12438
12439 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: S1->getLHS(), RHS: S2->getCond())) {
12440 ErrorInfo.Error = ErrorTy::InvalidCondition;
12441 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getCond()->getExprLoc();
12442 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S1->getLHS()->getSourceRange();
12443 return false;
12444 }
12445
12446 R = S1->getLHS();
12447
12448 auto *Then = S2->getThen();
12449 if (auto *ThenCS = dyn_cast<CompoundStmt>(Val: Then)) {
12450 if (ThenCS->body_empty()) {
12451 ErrorInfo.Error = ErrorTy::NoStmt;
12452 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
12453 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
12454 return false;
12455 }
12456 if (ThenCS->size() > 1) {
12457 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12458 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ThenCS->getBeginLoc();
12459 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenCS->getSourceRange();
12460 return false;
12461 }
12462 Then = ThenCS->body_front();
12463 }
12464
12465 auto *ThenBO = dyn_cast<BinaryOperator>(Val: Then);
12466 if (!ThenBO) {
12467 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12468 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S2->getBeginLoc();
12469 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S2->getSourceRange();
12470 return false;
12471 }
12472 if (ThenBO->getOpcode() != BO_Assign) {
12473 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12474 ErrorInfo.ErrorLoc = ThenBO->getExprLoc();
12475 ErrorInfo.NoteLoc = ThenBO->getOperatorLoc();
12476 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ThenBO->getSourceRange();
12477 return false;
12478 }
12479
12480 X = ThenBO->getLHS();
12481 D = ThenBO->getRHS();
12482
12483 auto *BO = cast<BinaryOperator>(Val: S1->getRHS()->IgnoreImpCasts());
12484 if (BO->getOpcode() != BO_EQ) {
12485 ErrorInfo.Error = ErrorTy::NotEQ;
12486 ErrorInfo.ErrorLoc = BO->getExprLoc();
12487 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12488 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12489 return false;
12490 }
12491
12492 C = BO;
12493
12494 if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getLHS())) {
12495 E = BO->getRHS();
12496 } else if (checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: BO->getRHS())) {
12497 E = BO->getLHS();
12498 } else {
12499 ErrorInfo.Error = ErrorTy::InvalidComparison;
12500 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = BO->getExprLoc();
12501 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12502 return false;
12503 }
12504
12505 if (S2->getElse()) {
12506 IsFailOnly = true;
12507
12508 auto *Else = S2->getElse();
12509 if (auto *ElseCS = dyn_cast<CompoundStmt>(Val: Else)) {
12510 if (ElseCS->body_empty()) {
12511 ErrorInfo.Error = ErrorTy::NoStmt;
12512 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
12513 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
12514 return false;
12515 }
12516 if (ElseCS->size() > 1) {
12517 ErrorInfo.Error = ErrorTy::MoreThanOneStmt;
12518 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = ElseCS->getBeginLoc();
12519 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseCS->getSourceRange();
12520 return false;
12521 }
12522 Else = ElseCS->body_front();
12523 }
12524
12525 auto *ElseBO = dyn_cast<BinaryOperator>(Val: Else);
12526 if (!ElseBO) {
12527 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12528 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Else->getBeginLoc();
12529 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Else->getSourceRange();
12530 return false;
12531 }
12532 if (ElseBO->getOpcode() != BO_Assign) {
12533 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12534 ErrorInfo.ErrorLoc = ElseBO->getExprLoc();
12535 ErrorInfo.NoteLoc = ElseBO->getOperatorLoc();
12536 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = ElseBO->getSourceRange();
12537 return false;
12538 }
12539 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: X, RHS: ElseBO->getRHS())) {
12540 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12541 ErrorInfo.ErrorLoc = ElseBO->getRHS()->getExprLoc();
12542 ErrorInfo.NoteLoc = X->getExprLoc();
12543 ErrorInfo.ErrorRange = ElseBO->getRHS()->getSourceRange();
12544 ErrorInfo.NoteRange = X->getSourceRange();
12545 return false;
12546 }
12547
12548 V = ElseBO->getLHS();
12549 }
12550
12551 return checkType(ErrorInfo);
12552}
12553
12554bool OpenMPAtomicCompareCaptureChecker::checkStmt(Stmt *S,
12555 ErrorInfoTy &ErrorInfo) {
12556 // if(x == e) { x = d; } else { v = x; }
12557 if (auto *IS = dyn_cast<IfStmt>(Val: S))
12558 return checkForm3(S: IS, ErrorInfo);
12559
12560 auto *CS = dyn_cast<CompoundStmt>(Val: S);
12561 if (!CS) {
12562 ErrorInfo.Error = ErrorTy::NotCompoundStmt;
12563 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getBeginLoc();
12564 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getSourceRange();
12565 return false;
12566 }
12567 if (CS->body_empty()) {
12568 ErrorInfo.Error = ErrorTy::NoStmt;
12569 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12570 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12571 return false;
12572 }
12573
12574 // { if(x == e) { x = d; } else { v = x; } }
12575 if (CS->size() == 1) {
12576 auto *IS = dyn_cast<IfStmt>(Val: CS->body_front());
12577 if (!IS) {
12578 ErrorInfo.Error = ErrorTy::NotIfStmt;
12579 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->body_front()->getBeginLoc();
12580 ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
12581 CS->body_front()->getSourceRange();
12582 return false;
12583 }
12584
12585 return checkForm3(S: IS, ErrorInfo);
12586 } else if (CS->size() == 2) {
12587 auto *S1 = CS->body_front();
12588 auto *S2 = CS->body_back();
12589
12590 Stmt *UpdateStmt = nullptr;
12591 Stmt *CondUpdateStmt = nullptr;
12592 Stmt *CondExprStmt = nullptr;
12593
12594 if (auto *BO = dyn_cast<BinaryOperator>(Val: S1)) {
12595 // It could be one of the following cases:
12596 // { v = x; cond-update-stmt }
12597 // { v = x; cond-expr-stmt }
12598 // { cond-expr-stmt; v = x; }
12599 // form 45
12600 if (isa<BinaryOperator>(Val: BO->getRHS()->IgnoreImpCasts()) ||
12601 isa<ConditionalOperator>(Val: BO->getRHS()->IgnoreImpCasts())) {
12602 // check if form 45
12603 if (isa<IfStmt>(Val: S2))
12604 return checkForm45(S: CS, ErrorInfo);
12605 // { cond-expr-stmt; v = x; }
12606 CondExprStmt = S1;
12607 UpdateStmt = S2;
12608 } else {
12609 IsPostfixUpdate = true;
12610 UpdateStmt = S1;
12611 if (isa<IfStmt>(Val: S2)) {
12612 // { v = x; cond-update-stmt }
12613 CondUpdateStmt = S2;
12614 } else {
12615 // { v = x; cond-expr-stmt }
12616 CondExprStmt = S2;
12617 }
12618 }
12619 } else {
12620 // { cond-update-stmt v = x; }
12621 UpdateStmt = S2;
12622 CondUpdateStmt = S1;
12623 }
12624
12625 auto CheckCondUpdateStmt = [this, &ErrorInfo](Stmt *CUS) {
12626 auto *IS = dyn_cast<IfStmt>(Val: CUS);
12627 if (!IS) {
12628 ErrorInfo.Error = ErrorTy::NotIfStmt;
12629 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CUS->getBeginLoc();
12630 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CUS->getSourceRange();
12631 return false;
12632 }
12633
12634 return checkCondUpdateStmt(S: IS, ErrorInfo);
12635 };
12636
12637 // CheckUpdateStmt has to be called *after* CheckCondUpdateStmt.
12638 auto CheckUpdateStmt = [this, &ErrorInfo](Stmt *US) {
12639 auto *BO = dyn_cast<BinaryOperator>(Val: US);
12640 if (!BO) {
12641 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12642 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = US->getBeginLoc();
12643 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = US->getSourceRange();
12644 return false;
12645 }
12646 if (BO->getOpcode() != BO_Assign) {
12647 ErrorInfo.Error = ErrorTy::NotAnAssignment;
12648 ErrorInfo.ErrorLoc = BO->getExprLoc();
12649 ErrorInfo.NoteLoc = BO->getOperatorLoc();
12650 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = BO->getSourceRange();
12651 return false;
12652 }
12653 if (!checkIfTwoExprsAreSame(Context&: ContextRef, LHS: this->X, RHS: BO->getRHS())) {
12654 ErrorInfo.Error = ErrorTy::InvalidAssignment;
12655 ErrorInfo.ErrorLoc = BO->getRHS()->getExprLoc();
12656 ErrorInfo.NoteLoc = this->X->getExprLoc();
12657 ErrorInfo.ErrorRange = BO->getRHS()->getSourceRange();
12658 ErrorInfo.NoteRange = this->X->getSourceRange();
12659 return false;
12660 }
12661
12662 this->V = BO->getLHS();
12663
12664 return true;
12665 };
12666
12667 if (CondUpdateStmt && !CheckCondUpdateStmt(CondUpdateStmt))
12668 return false;
12669 if (CondExprStmt && !checkCondExprStmt(S: CondExprStmt, ErrorInfo))
12670 return false;
12671 if (!CheckUpdateStmt(UpdateStmt))
12672 return false;
12673 } else {
12674 ErrorInfo.Error = ErrorTy::MoreThanTwoStmts;
12675 ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CS->getBeginLoc();
12676 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = CS->getSourceRange();
12677 return false;
12678 }
12679
12680 return checkType(ErrorInfo);
12681}
12682} // namespace
12683
12684StmtResult SemaOpenMP::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
12685 Stmt *AStmt,
12686 SourceLocation StartLoc,
12687 SourceLocation EndLoc) {
12688 ASTContext &Context = getASTContext();
12689 unsigned OMPVersion = getLangOpts().OpenMP;
12690 // Register location of the first atomic directive.
12691 DSAStack->addAtomicDirectiveLoc(Loc: StartLoc);
12692 if (!AStmt)
12693 return StmtError();
12694
12695 // 1.2.2 OpenMP Language Terminology
12696 // Structured block - An executable statement with a single entry at the
12697 // top and a single exit at the bottom.
12698 // The point of exit cannot be a branch out of the structured block.
12699 // longjmp() and throw() must not violate the entry/exit criteria.
12700 OpenMPClauseKind AtomicKind = OMPC_unknown;
12701 SourceLocation AtomicKindLoc;
12702 OpenMPClauseKind MemOrderKind = OMPC_unknown;
12703 SourceLocation MemOrderLoc;
12704 bool MutexClauseEncountered = false;
12705 llvm::SmallSet<OpenMPClauseKind, 2> EncounteredAtomicKinds;
12706 for (const OMPClause *C : Clauses) {
12707 switch (C->getClauseKind()) {
12708 case OMPC_read:
12709 case OMPC_write:
12710 case OMPC_update:
12711 MutexClauseEncountered = true;
12712 [[fallthrough]];
12713 case OMPC_capture:
12714 case OMPC_compare: {
12715 if (AtomicKind != OMPC_unknown && MutexClauseEncountered) {
12716 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
12717 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12718 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12719 << getOpenMPClauseNameForDiag(C: AtomicKind);
12720 } else {
12721 AtomicKind = C->getClauseKind();
12722 AtomicKindLoc = C->getBeginLoc();
12723 if (!EncounteredAtomicKinds.insert(V: C->getClauseKind()).second) {
12724 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_several_clauses)
12725 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12726 Diag(Loc: AtomicKindLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12727 << getOpenMPClauseNameForDiag(C: AtomicKind);
12728 }
12729 }
12730 break;
12731 }
12732 case OMPC_weak:
12733 case OMPC_fail: {
12734 if (!EncounteredAtomicKinds.contains(V: OMPC_compare)) {
12735 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_atomic_no_compare)
12736 << getOpenMPClauseNameForDiag(C: C->getClauseKind())
12737 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12738 return StmtError();
12739 }
12740 break;
12741 }
12742 case OMPC_seq_cst:
12743 case OMPC_acq_rel:
12744 case OMPC_acquire:
12745 case OMPC_release:
12746 case OMPC_relaxed: {
12747 if (MemOrderKind != OMPC_unknown) {
12748 Diag(Loc: C->getBeginLoc(), DiagID: diag::err_omp_several_mem_order_clauses)
12749 << getOpenMPDirectiveName(D: OMPD_atomic, Ver: OMPVersion) << 0
12750 << SourceRange(C->getBeginLoc(), C->getEndLoc());
12751 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12752 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12753 } else {
12754 MemOrderKind = C->getClauseKind();
12755 MemOrderLoc = C->getBeginLoc();
12756 }
12757 break;
12758 }
12759 // The following clauses are allowed, but we don't need to do anything here.
12760 case OMPC_hint:
12761 break;
12762 default:
12763 llvm_unreachable("unknown clause is encountered");
12764 }
12765 }
12766 bool IsCompareCapture = false;
12767 if (EncounteredAtomicKinds.contains(V: OMPC_compare) &&
12768 EncounteredAtomicKinds.contains(V: OMPC_capture)) {
12769 IsCompareCapture = true;
12770 AtomicKind = OMPC_compare;
12771 }
12772 // OpenMP 5.0, 2.17.7 atomic Construct, Restrictions
12773 // If atomic-clause is read then memory-order-clause must not be acq_rel or
12774 // release.
12775 // If atomic-clause is write then memory-order-clause must not be acq_rel or
12776 // acquire.
12777 // If atomic-clause is update or not present then memory-order-clause must not
12778 // be acq_rel or acquire.
12779 if ((AtomicKind == OMPC_read &&
12780 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_release)) ||
12781 ((AtomicKind == OMPC_write || AtomicKind == OMPC_update ||
12782 AtomicKind == OMPC_unknown) &&
12783 (MemOrderKind == OMPC_acq_rel || MemOrderKind == OMPC_acquire))) {
12784 SourceLocation Loc = AtomicKindLoc;
12785 if (AtomicKind == OMPC_unknown)
12786 Loc = StartLoc;
12787 Diag(Loc, DiagID: diag::err_omp_atomic_incompatible_mem_order_clause)
12788 << getOpenMPClauseNameForDiag(C: AtomicKind)
12789 << (AtomicKind == OMPC_unknown ? 1 : 0)
12790 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12791 Diag(Loc: MemOrderLoc, DiagID: diag::note_omp_previous_mem_order_clause)
12792 << getOpenMPClauseNameForDiag(C: MemOrderKind);
12793 }
12794
12795 Stmt *Body = AStmt;
12796 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Body))
12797 Body = EWC->getSubExpr();
12798
12799 Expr *X = nullptr;
12800 Expr *V = nullptr;
12801 Expr *E = nullptr;
12802 Expr *UE = nullptr;
12803 Expr *D = nullptr;
12804 Expr *CE = nullptr;
12805 Expr *R = nullptr;
12806 bool IsXLHSInRHSPart = false;
12807 bool IsPostfixUpdate = false;
12808 bool IsFailOnly = false;
12809 // OpenMP [2.12.6, atomic Construct]
12810 // In the next expressions:
12811 // * x and v (as applicable) are both l-value expressions with scalar type.
12812 // * During the execution of an atomic region, multiple syntactic
12813 // occurrences of x must designate the same storage location.
12814 // * Neither of v and expr (as applicable) may access the storage location
12815 // designated by x.
12816 // * Neither of x and expr (as applicable) may access the storage location
12817 // designated by v.
12818 // * expr is an expression with scalar type.
12819 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
12820 // * binop, binop=, ++, and -- are not overloaded operators.
12821 // * The expression x binop expr must be numerically equivalent to x binop
12822 // (expr). This requirement is satisfied if the operators in expr have
12823 // precedence greater than binop, or by using parentheses around expr or
12824 // subexpressions of expr.
12825 // * The expression expr binop x must be numerically equivalent to (expr)
12826 // binop x. This requirement is satisfied if the operators in expr have
12827 // precedence equal to or greater than binop, or by using parentheses around
12828 // expr or subexpressions of expr.
12829 // * For forms that allow multiple occurrences of x, the number of times
12830 // that x is evaluated is unspecified.
12831 if (AtomicKind == OMPC_read) {
12832 enum {
12833 NotAnExpression,
12834 NotAnAssignmentOp,
12835 NotAScalarType,
12836 NotAnLValue,
12837 NoError
12838 } ErrorFound = NoError;
12839 SourceLocation ErrorLoc, NoteLoc;
12840 SourceRange ErrorRange, NoteRange;
12841 // If clause is read:
12842 // v = x;
12843 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
12844 const auto *AtomicBinOp =
12845 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
12846 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
12847 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
12848 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
12849 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
12850 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
12851 if (!X->isLValue() || !V->isLValue()) {
12852 const Expr *NotLValueExpr = X->isLValue() ? V : X;
12853 ErrorFound = NotAnLValue;
12854 ErrorLoc = AtomicBinOp->getExprLoc();
12855 ErrorRange = AtomicBinOp->getSourceRange();
12856 NoteLoc = NotLValueExpr->getExprLoc();
12857 NoteRange = NotLValueExpr->getSourceRange();
12858 }
12859 } else if (!X->isInstantiationDependent() ||
12860 !V->isInstantiationDependent()) {
12861 const Expr *NotScalarExpr =
12862 (X->isInstantiationDependent() || X->getType()->isScalarType())
12863 ? V
12864 : X;
12865 ErrorFound = NotAScalarType;
12866 ErrorLoc = AtomicBinOp->getExprLoc();
12867 ErrorRange = AtomicBinOp->getSourceRange();
12868 NoteLoc = NotScalarExpr->getExprLoc();
12869 NoteRange = NotScalarExpr->getSourceRange();
12870 }
12871 } else if (!AtomicBody->isInstantiationDependent()) {
12872 ErrorFound = NotAnAssignmentOp;
12873 ErrorLoc = AtomicBody->getExprLoc();
12874 ErrorRange = AtomicBody->getSourceRange();
12875 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
12876 : AtomicBody->getExprLoc();
12877 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
12878 : AtomicBody->getSourceRange();
12879 }
12880 } else {
12881 ErrorFound = NotAnExpression;
12882 NoteLoc = ErrorLoc = Body->getBeginLoc();
12883 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
12884 }
12885 if (ErrorFound != NoError) {
12886 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_read_not_expression_statement)
12887 << ErrorRange;
12888 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
12889 << ErrorFound << NoteRange;
12890 return StmtError();
12891 }
12892 if (SemaRef.CurContext->isDependentContext())
12893 V = X = nullptr;
12894 } else if (AtomicKind == OMPC_write) {
12895 enum {
12896 NotAnExpression,
12897 NotAnAssignmentOp,
12898 NotAScalarType,
12899 NotAnLValue,
12900 NoError
12901 } ErrorFound = NoError;
12902 SourceLocation ErrorLoc, NoteLoc;
12903 SourceRange ErrorRange, NoteRange;
12904 // If clause is write:
12905 // x = expr;
12906 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
12907 const auto *AtomicBinOp =
12908 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
12909 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
12910 X = AtomicBinOp->getLHS();
12911 E = AtomicBinOp->getRHS();
12912 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
12913 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
12914 if (!X->isLValue()) {
12915 ErrorFound = NotAnLValue;
12916 ErrorLoc = AtomicBinOp->getExprLoc();
12917 ErrorRange = AtomicBinOp->getSourceRange();
12918 NoteLoc = X->getExprLoc();
12919 NoteRange = X->getSourceRange();
12920 }
12921 } else if (!X->isInstantiationDependent() ||
12922 !E->isInstantiationDependent()) {
12923 const Expr *NotScalarExpr =
12924 (X->isInstantiationDependent() || X->getType()->isScalarType())
12925 ? E
12926 : X;
12927 ErrorFound = NotAScalarType;
12928 ErrorLoc = AtomicBinOp->getExprLoc();
12929 ErrorRange = AtomicBinOp->getSourceRange();
12930 NoteLoc = NotScalarExpr->getExprLoc();
12931 NoteRange = NotScalarExpr->getSourceRange();
12932 }
12933 } else if (!AtomicBody->isInstantiationDependent()) {
12934 ErrorFound = NotAnAssignmentOp;
12935 ErrorLoc = AtomicBody->getExprLoc();
12936 ErrorRange = AtomicBody->getSourceRange();
12937 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
12938 : AtomicBody->getExprLoc();
12939 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
12940 : AtomicBody->getSourceRange();
12941 }
12942 } else {
12943 ErrorFound = NotAnExpression;
12944 NoteLoc = ErrorLoc = Body->getBeginLoc();
12945 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
12946 }
12947 if (ErrorFound != NoError) {
12948 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_write_not_expression_statement)
12949 << ErrorRange;
12950 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_read_write)
12951 << ErrorFound << NoteRange;
12952 return StmtError();
12953 }
12954 if (SemaRef.CurContext->isDependentContext())
12955 E = X = nullptr;
12956 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
12957 // If clause is update:
12958 // x++;
12959 // x--;
12960 // ++x;
12961 // --x;
12962 // x binop= expr;
12963 // x = x binop expr;
12964 // x = expr binop x;
12965 OpenMPAtomicUpdateChecker Checker(SemaRef);
12966 if (Checker.checkStatement(
12967 S: Body,
12968 DiagId: (AtomicKind == OMPC_update)
12969 ? diag::err_omp_atomic_update_not_expression_statement
12970 : diag::err_omp_atomic_not_expression_statement,
12971 NoteId: diag::note_omp_atomic_update))
12972 return StmtError();
12973 if (!SemaRef.CurContext->isDependentContext()) {
12974 E = Checker.getExpr();
12975 X = Checker.getX();
12976 UE = Checker.getUpdateExpr();
12977 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
12978 }
12979 } else if (AtomicKind == OMPC_capture) {
12980 enum {
12981 NotAnAssignmentOp,
12982 NotACompoundStatement,
12983 NotTwoSubstatements,
12984 NotASpecificExpression,
12985 NoError
12986 } ErrorFound = NoError;
12987 SourceLocation ErrorLoc, NoteLoc;
12988 SourceRange ErrorRange, NoteRange;
12989 if (const auto *AtomicBody = dyn_cast<Expr>(Val: Body)) {
12990 // If clause is a capture:
12991 // v = x++;
12992 // v = x--;
12993 // v = ++x;
12994 // v = --x;
12995 // v = x binop= expr;
12996 // v = x = x binop expr;
12997 // v = x = expr binop x;
12998 const auto *AtomicBinOp =
12999 dyn_cast<BinaryOperator>(Val: AtomicBody->IgnoreParenImpCasts());
13000 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
13001 V = AtomicBinOp->getLHS();
13002 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
13003 OpenMPAtomicUpdateChecker Checker(SemaRef);
13004 if (Checker.checkStatement(
13005 S: Body, DiagId: diag::err_omp_atomic_capture_not_expression_statement,
13006 NoteId: diag::note_omp_atomic_update))
13007 return StmtError();
13008 E = Checker.getExpr();
13009 X = Checker.getX();
13010 UE = Checker.getUpdateExpr();
13011 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13012 IsPostfixUpdate = Checker.isPostfixUpdate();
13013 } else if (!AtomicBody->isInstantiationDependent()) {
13014 ErrorLoc = AtomicBody->getExprLoc();
13015 ErrorRange = AtomicBody->getSourceRange();
13016 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
13017 : AtomicBody->getExprLoc();
13018 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
13019 : AtomicBody->getSourceRange();
13020 ErrorFound = NotAnAssignmentOp;
13021 }
13022 if (ErrorFound != NoError) {
13023 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_expression_statement)
13024 << ErrorRange;
13025 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
13026 return StmtError();
13027 }
13028 if (SemaRef.CurContext->isDependentContext())
13029 UE = V = E = X = nullptr;
13030 } else {
13031 // If clause is a capture:
13032 // { v = x; x = expr; }
13033 // { v = x; x++; }
13034 // { v = x; x--; }
13035 // { v = x; ++x; }
13036 // { v = x; --x; }
13037 // { v = x; x binop= expr; }
13038 // { v = x; x = x binop expr; }
13039 // { v = x; x = expr binop x; }
13040 // { x++; v = x; }
13041 // { x--; v = x; }
13042 // { ++x; v = x; }
13043 // { --x; v = x; }
13044 // { x binop= expr; v = x; }
13045 // { x = x binop expr; v = x; }
13046 // { x = expr binop x; v = x; }
13047 if (auto *CS = dyn_cast<CompoundStmt>(Val: Body)) {
13048 // Check that this is { expr1; expr2; }
13049 if (CS->size() == 2) {
13050 Stmt *First = CS->body_front();
13051 Stmt *Second = CS->body_back();
13052 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: First))
13053 First = EWC->getSubExpr()->IgnoreParenImpCasts();
13054 if (auto *EWC = dyn_cast<ExprWithCleanups>(Val: Second))
13055 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
13056 // Need to find what subexpression is 'v' and what is 'x'.
13057 OpenMPAtomicUpdateChecker Checker(SemaRef);
13058 bool IsUpdateExprFound = !Checker.checkStatement(S: Second);
13059 BinaryOperator *BinOp = nullptr;
13060 if (IsUpdateExprFound) {
13061 BinOp = dyn_cast<BinaryOperator>(Val: First);
13062 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
13063 }
13064 if (IsUpdateExprFound && !SemaRef.CurContext->isDependentContext()) {
13065 // { v = x; x++; }
13066 // { v = x; x--; }
13067 // { v = x; ++x; }
13068 // { v = x; --x; }
13069 // { v = x; x binop= expr; }
13070 // { v = x; x = x binop expr; }
13071 // { v = x; x = expr binop x; }
13072 // Check that the first expression has form v = x.
13073 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
13074 llvm::FoldingSetNodeID XId, PossibleXId;
13075 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
13076 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
13077 IsUpdateExprFound = XId == PossibleXId;
13078 if (IsUpdateExprFound) {
13079 V = BinOp->getLHS();
13080 X = Checker.getX();
13081 E = Checker.getExpr();
13082 UE = Checker.getUpdateExpr();
13083 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13084 IsPostfixUpdate = true;
13085 }
13086 }
13087 if (!IsUpdateExprFound) {
13088 IsUpdateExprFound = !Checker.checkStatement(S: First);
13089 BinOp = nullptr;
13090 if (IsUpdateExprFound) {
13091 BinOp = dyn_cast<BinaryOperator>(Val: Second);
13092 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
13093 }
13094 if (IsUpdateExprFound &&
13095 !SemaRef.CurContext->isDependentContext()) {
13096 // { x++; v = x; }
13097 // { x--; v = x; }
13098 // { ++x; v = x; }
13099 // { --x; v = x; }
13100 // { x binop= expr; v = x; }
13101 // { x = x binop expr; v = x; }
13102 // { x = expr binop x; v = x; }
13103 // Check that the second expression has form v = x.
13104 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
13105 llvm::FoldingSetNodeID XId, PossibleXId;
13106 Checker.getX()->Profile(ID&: XId, Context, /*Canonical=*/true);
13107 PossibleX->Profile(ID&: PossibleXId, Context, /*Canonical=*/true);
13108 IsUpdateExprFound = XId == PossibleXId;
13109 if (IsUpdateExprFound) {
13110 V = BinOp->getLHS();
13111 X = Checker.getX();
13112 E = Checker.getExpr();
13113 UE = Checker.getUpdateExpr();
13114 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
13115 IsPostfixUpdate = false;
13116 }
13117 }
13118 }
13119 if (!IsUpdateExprFound) {
13120 // { v = x; x = expr; }
13121 auto *FirstExpr = dyn_cast<Expr>(Val: First);
13122 auto *SecondExpr = dyn_cast<Expr>(Val: Second);
13123 if (!FirstExpr || !SecondExpr ||
13124 !(FirstExpr->isInstantiationDependent() ||
13125 SecondExpr->isInstantiationDependent())) {
13126 auto *FirstBinOp = dyn_cast<BinaryOperator>(Val: First);
13127 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
13128 ErrorFound = NotAnAssignmentOp;
13129 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
13130 : First->getBeginLoc();
13131 NoteRange = ErrorRange = FirstBinOp
13132 ? FirstBinOp->getSourceRange()
13133 : SourceRange(ErrorLoc, ErrorLoc);
13134 } else {
13135 auto *SecondBinOp = dyn_cast<BinaryOperator>(Val: Second);
13136 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
13137 ErrorFound = NotAnAssignmentOp;
13138 NoteLoc = ErrorLoc = SecondBinOp
13139 ? SecondBinOp->getOperatorLoc()
13140 : Second->getBeginLoc();
13141 NoteRange = ErrorRange =
13142 SecondBinOp ? SecondBinOp->getSourceRange()
13143 : SourceRange(ErrorLoc, ErrorLoc);
13144 } else {
13145 Expr *PossibleXRHSInFirst =
13146 FirstBinOp->getRHS()->IgnoreParenImpCasts();
13147 Expr *PossibleXLHSInSecond =
13148 SecondBinOp->getLHS()->IgnoreParenImpCasts();
13149 llvm::FoldingSetNodeID X1Id, X2Id;
13150 PossibleXRHSInFirst->Profile(ID&: X1Id, Context,
13151 /*Canonical=*/true);
13152 PossibleXLHSInSecond->Profile(ID&: X2Id, Context,
13153 /*Canonical=*/true);
13154 IsUpdateExprFound = X1Id == X2Id;
13155 if (IsUpdateExprFound) {
13156 V = FirstBinOp->getLHS();
13157 X = SecondBinOp->getLHS();
13158 E = SecondBinOp->getRHS();
13159 UE = nullptr;
13160 IsXLHSInRHSPart = false;
13161 IsPostfixUpdate = true;
13162 } else {
13163 ErrorFound = NotASpecificExpression;
13164 ErrorLoc = FirstBinOp->getExprLoc();
13165 ErrorRange = FirstBinOp->getSourceRange();
13166 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
13167 NoteRange = SecondBinOp->getRHS()->getSourceRange();
13168 }
13169 }
13170 }
13171 }
13172 }
13173 } else {
13174 NoteLoc = ErrorLoc = Body->getBeginLoc();
13175 NoteRange = ErrorRange =
13176 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
13177 ErrorFound = NotTwoSubstatements;
13178 }
13179 } else {
13180 NoteLoc = ErrorLoc = Body->getBeginLoc();
13181 NoteRange = ErrorRange =
13182 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
13183 ErrorFound = NotACompoundStatement;
13184 }
13185 }
13186 if (ErrorFound != NoError) {
13187 Diag(Loc: ErrorLoc, DiagID: diag::err_omp_atomic_capture_not_compound_statement)
13188 << ErrorRange;
13189 Diag(Loc: NoteLoc, DiagID: diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
13190 return StmtError();
13191 }
13192 if (SemaRef.CurContext->isDependentContext())
13193 UE = V = E = X = nullptr;
13194 } else if (AtomicKind == OMPC_compare) {
13195 if (IsCompareCapture) {
13196 OpenMPAtomicCompareCaptureChecker::ErrorInfoTy ErrorInfo;
13197 OpenMPAtomicCompareCaptureChecker Checker(SemaRef);
13198 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
13199 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare_capture)
13200 << ErrorInfo.ErrorRange;
13201 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
13202 << ErrorInfo.Error << ErrorInfo.NoteRange;
13203 return StmtError();
13204 }
13205 X = Checker.getX();
13206 E = Checker.getE();
13207 D = Checker.getD();
13208 CE = Checker.getCond();
13209 V = Checker.getV();
13210 R = Checker.getR();
13211 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
13212 IsXLHSInRHSPart = Checker.isXBinopExpr();
13213 IsFailOnly = Checker.isFailOnly();
13214 IsPostfixUpdate = Checker.isPostfixUpdate();
13215 } else {
13216 OpenMPAtomicCompareChecker::ErrorInfoTy ErrorInfo;
13217 OpenMPAtomicCompareChecker Checker(SemaRef);
13218 if (!Checker.checkStmt(S: Body, ErrorInfo)) {
13219 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_compare)
13220 << ErrorInfo.ErrorRange;
13221 Diag(Loc: ErrorInfo.NoteLoc, DiagID: diag::note_omp_atomic_compare)
13222 << ErrorInfo.Error << ErrorInfo.NoteRange;
13223 return StmtError();
13224 }
13225 X = Checker.getX();
13226 E = Checker.getE();
13227 D = Checker.getD();
13228 CE = Checker.getCond();
13229 // The weak clause may only appear if the resulting atomic operation is
13230 // an atomic conditional update for which the comparison tests for
13231 // equality. It was not possible to do this check in
13232 // OpenMPAtomicCompareChecker::checkStmt() as the check for OMPC_weak
13233 // could not be performed (Clauses are not available).
13234 auto *It = find_if(Range&: Clauses, P: [](OMPClause *C) {
13235 return C->getClauseKind() == llvm::omp::Clause::OMPC_weak;
13236 });
13237 if (It != Clauses.end()) {
13238 auto *Cond = dyn_cast<BinaryOperator>(Val: CE);
13239 if (Cond->getOpcode() != BO_EQ) {
13240 ErrorInfo.Error = Checker.ErrorTy::NotAnAssignment;
13241 ErrorInfo.ErrorLoc = Cond->getExprLoc();
13242 ErrorInfo.NoteLoc = Cond->getOperatorLoc();
13243 ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
13244
13245 Diag(Loc: ErrorInfo.ErrorLoc, DiagID: diag::err_omp_atomic_weak_no_equality)
13246 << ErrorInfo.ErrorRange;
13247 return StmtError();
13248 }
13249 }
13250 // We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
13251 IsXLHSInRHSPart = Checker.isXBinopExpr();
13252 }
13253 }
13254
13255 SemaRef.setFunctionHasBranchProtectedScope();
13256
13257 return OMPAtomicDirective::Create(
13258 C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
13259 Exprs: {.X: X, .V: V, .R: R, .E: E, .UE: UE, .D: D, .Cond: CE, .IsXLHSInRHSPart: IsXLHSInRHSPart, .IsPostfixUpdate: IsPostfixUpdate, .IsFailOnly: IsFailOnly});
13260}
13261
13262StmtResult SemaOpenMP::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
13263 Stmt *AStmt,
13264 SourceLocation StartLoc,
13265 SourceLocation EndLoc) {
13266 if (!AStmt)
13267 return StmtError();
13268
13269 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target, AStmt);
13270
13271 // OpenMP [2.16, Nesting of Regions]
13272 // If specified, a teams construct must be contained within a target
13273 // construct. That target construct must contain no statements or directives
13274 // outside of the teams construct.
13275 if (DSAStack->hasInnerTeamsRegion()) {
13276 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
13277 bool OMPTeamsFound = true;
13278 if (const auto *CS = dyn_cast<CompoundStmt>(Val: S)) {
13279 auto I = CS->body_begin();
13280 while (I != CS->body_end()) {
13281 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: *I);
13282 bool IsTeams = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
13283 if (!IsTeams || I != CS->body_begin()) {
13284 OMPTeamsFound = false;
13285 if (IsTeams && I != CS->body_begin()) {
13286 // This is the two teams case. Since the InnerTeamsRegionLoc will
13287 // point to this second one reset the iterator to the other teams.
13288 --I;
13289 }
13290 break;
13291 }
13292 ++I;
13293 }
13294 assert(I != CS->body_end() && "Not found statement");
13295 S = *I;
13296 } else {
13297 const auto *OED = dyn_cast<OMPExecutableDirective>(Val: S);
13298 OMPTeamsFound = OED && isOpenMPTeamsDirective(DKind: OED->getDirectiveKind());
13299 }
13300 if (!OMPTeamsFound) {
13301 Diag(Loc: StartLoc, DiagID: diag::err_omp_target_contains_not_only_teams);
13302 Diag(DSAStack->getInnerTeamsRegionLoc(),
13303 DiagID: diag::note_omp_nested_teams_construct_here);
13304 Diag(Loc: S->getBeginLoc(), DiagID: diag::note_omp_nested_statement_here)
13305 << isa<OMPExecutableDirective>(Val: S);
13306 return StmtError();
13307 }
13308 }
13309
13310 return OMPTargetDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13311 AssociatedStmt: AStmt);
13312}
13313
13314StmtResult SemaOpenMP::ActOnOpenMPTargetParallelDirective(
13315 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13316 SourceLocation EndLoc) {
13317 if (!AStmt)
13318 return StmtError();
13319
13320 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel, AStmt);
13321
13322 return OMPTargetParallelDirective::Create(
13323 C: getASTContext(), StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
13324 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
13325}
13326
13327StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForDirective(
13328 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13329 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13330 if (!AStmt)
13331 return StmtError();
13332
13333 CapturedStmt *CS =
13334 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for, AStmt);
13335
13336 OMPLoopBasedDirective::HelperExprs B;
13337 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13338 // define the nested loops number.
13339 unsigned NestedLoopCount =
13340 checkOpenMPLoop(DKind: OMPD_target_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13341 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
13342 VarsWithImplicitDSA, Built&: B);
13343 if (NestedLoopCount == 0)
13344 return StmtError();
13345
13346 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13347 return StmtError();
13348
13349 return OMPTargetParallelForDirective::Create(
13350 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13351 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
13352}
13353
13354/// Check for existence of a map clause in the list of clauses.
13355static bool hasClauses(ArrayRef<OMPClause *> Clauses,
13356 const OpenMPClauseKind K) {
13357 return llvm::any_of(
13358 Range&: Clauses, P: [K](const OMPClause *C) { return C->getClauseKind() == K; });
13359}
13360
13361template <typename... Params>
13362static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
13363 const Params... ClauseTypes) {
13364 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
13365}
13366
13367/// Check if the variables in the mapping clause are externally visible.
13368static bool isClauseMappable(ArrayRef<OMPClause *> Clauses) {
13369 for (const OMPClause *C : Clauses) {
13370 if (auto *TC = dyn_cast<OMPToClause>(Val: C))
13371 return llvm::all_of(Range: TC->all_decls(), P: [](ValueDecl *VD) {
13372 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13373 (VD->isExternallyVisible() &&
13374 VD->getVisibility() != HiddenVisibility);
13375 });
13376 else if (auto *FC = dyn_cast<OMPFromClause>(Val: C))
13377 return llvm::all_of(Range: FC->all_decls(), P: [](ValueDecl *VD) {
13378 return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13379 (VD->isExternallyVisible() &&
13380 VD->getVisibility() != HiddenVisibility);
13381 });
13382 }
13383
13384 return true;
13385}
13386
13387StmtResult
13388SemaOpenMP::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
13389 Stmt *AStmt, SourceLocation StartLoc,
13390 SourceLocation EndLoc) {
13391 if (!AStmt)
13392 return StmtError();
13393
13394 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13395
13396 // OpenMP [2.12.2, target data Construct, Restrictions]
13397 // At least one map, use_device_addr or use_device_ptr clause must appear on
13398 // the directive.
13399 if (!hasClauses(Clauses, K: OMPC_map, ClauseTypes: OMPC_use_device_ptr) &&
13400 (getLangOpts().OpenMP < 50 ||
13401 !hasClauses(Clauses, K: OMPC_use_device_addr))) {
13402 StringRef Expected;
13403 if (getLangOpts().OpenMP < 50)
13404 Expected = "'map' or 'use_device_ptr'";
13405 else
13406 Expected = "'map', 'use_device_ptr', or 'use_device_addr'";
13407 unsigned OMPVersion = getLangOpts().OpenMP;
13408 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13409 << Expected << getOpenMPDirectiveName(D: OMPD_target_data, Ver: OMPVersion);
13410 return StmtError();
13411 }
13412
13413 SemaRef.setFunctionHasBranchProtectedScope();
13414
13415 return OMPTargetDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13416 Clauses, AssociatedStmt: AStmt);
13417}
13418
13419StmtResult SemaOpenMP::ActOnOpenMPTargetEnterDataDirective(
13420 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13421 SourceLocation EndLoc, Stmt *AStmt) {
13422 if (!AStmt)
13423 return StmtError();
13424
13425 setBranchProtectedScope(SemaRef, DKind: OMPD_target_enter_data, AStmt);
13426
13427 // OpenMP [2.10.2, Restrictions, p. 99]
13428 // At least one map clause must appear on the directive.
13429 if (!hasClauses(Clauses, K: OMPC_map)) {
13430 unsigned OMPVersion = getLangOpts().OpenMP;
13431 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13432 << "'map'"
13433 << getOpenMPDirectiveName(D: OMPD_target_enter_data, Ver: OMPVersion);
13434 return StmtError();
13435 }
13436
13437 return OMPTargetEnterDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13438 Clauses, AssociatedStmt: AStmt);
13439}
13440
13441StmtResult SemaOpenMP::ActOnOpenMPTargetExitDataDirective(
13442 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13443 SourceLocation EndLoc, Stmt *AStmt) {
13444 if (!AStmt)
13445 return StmtError();
13446
13447 setBranchProtectedScope(SemaRef, DKind: OMPD_target_exit_data, AStmt);
13448
13449 // OpenMP [2.10.3, Restrictions, p. 102]
13450 // At least one map clause must appear on the directive.
13451 if (!hasClauses(Clauses, K: OMPC_map)) {
13452 unsigned OMPVersion = getLangOpts().OpenMP;
13453 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
13454 << "'map'" << getOpenMPDirectiveName(D: OMPD_target_exit_data, Ver: OMPVersion);
13455 return StmtError();
13456 }
13457
13458 return OMPTargetExitDataDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13459 Clauses, AssociatedStmt: AStmt);
13460}
13461
13462StmtResult SemaOpenMP::ActOnOpenMPTargetUpdateDirective(
13463 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13464 SourceLocation EndLoc, Stmt *AStmt) {
13465 if (!AStmt)
13466 return StmtError();
13467
13468 setBranchProtectedScope(SemaRef, DKind: OMPD_target_update, AStmt);
13469
13470 if (!hasClauses(Clauses, K: OMPC_to, ClauseTypes: OMPC_from)) {
13471 Diag(Loc: StartLoc, DiagID: diag::err_omp_at_least_one_motion_clause_required);
13472 return StmtError();
13473 }
13474
13475 if (!isClauseMappable(Clauses)) {
13476 Diag(Loc: StartLoc, DiagID: diag::err_omp_cannot_update_with_internal_linkage);
13477 return StmtError();
13478 }
13479
13480 return OMPTargetUpdateDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13481 Clauses, AssociatedStmt: AStmt);
13482}
13483
13484/// This checks whether a \p ClauseType clause \p C has at most \p Max
13485/// expression. If not, a diag of number \p Diag will be emitted.
13486template <typename ClauseType>
13487static bool checkNumExprsInClause(SemaBase &SemaRef,
13488 ArrayRef<OMPClause *> Clauses,
13489 unsigned MaxNum, unsigned Diag) {
13490 auto ClauseItr = llvm::find_if(Clauses, llvm::IsaPred<ClauseType>);
13491 if (ClauseItr == Clauses.end())
13492 return true;
13493 const auto *C = cast<ClauseType>(*ClauseItr);
13494 auto VarList = C->getVarRefs();
13495 if (VarList.size() > MaxNum) {
13496 SemaRef.Diag(VarList[MaxNum]->getBeginLoc(), Diag)
13497 << getOpenMPClauseNameForDiag(C->getClauseKind());
13498 return false;
13499 }
13500 return true;
13501}
13502
13503StmtResult SemaOpenMP::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
13504 Stmt *AStmt,
13505 SourceLocation StartLoc,
13506 SourceLocation EndLoc) {
13507 if (!AStmt)
13508 return StmtError();
13509
13510 if (!checkNumExprsInClause<OMPNumTeamsClause>(
13511 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed) ||
13512 !checkNumExprsInClause<OMPThreadLimitClause>(
13513 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
13514 return StmtError();
13515
13516 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
13517 if (getLangOpts().HIP && (DSAStack->getParentDirective() == OMPD_target))
13518 Diag(Loc: StartLoc, DiagID: diag::warn_hip_omp_target_directives);
13519
13520 setBranchProtectedScope(SemaRef, DKind: OMPD_teams, AStmt);
13521
13522 DSAStack->setParentTeamsRegionLoc(StartLoc);
13523
13524 return OMPTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13525 AssociatedStmt: AStmt);
13526}
13527
13528StmtResult SemaOpenMP::ActOnOpenMPCancellationPointDirective(
13529 SourceLocation StartLoc, SourceLocation EndLoc,
13530 OpenMPDirectiveKind CancelRegion) {
13531 if (DSAStack->isParentNowaitRegion()) {
13532 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 0;
13533 return StmtError();
13534 }
13535 if (DSAStack->isParentOrderedRegion()) {
13536 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 0;
13537 return StmtError();
13538 }
13539 return OMPCancellationPointDirective::Create(C: getASTContext(), StartLoc,
13540 EndLoc, CancelRegion);
13541}
13542
13543StmtResult SemaOpenMP::ActOnOpenMPCancelDirective(
13544 ArrayRef<OMPClause *> Clauses, SourceLocation StartLoc,
13545 SourceLocation EndLoc, OpenMPDirectiveKind CancelRegion) {
13546 if (DSAStack->isParentNowaitRegion()) {
13547 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_nowait) << 1;
13548 return StmtError();
13549 }
13550 if (DSAStack->isParentOrderedRegion()) {
13551 Diag(Loc: StartLoc, DiagID: diag::err_omp_parent_cancel_region_ordered) << 1;
13552 return StmtError();
13553 }
13554 DSAStack->setParentCancelRegion(/*Cancel=*/true);
13555 return OMPCancelDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
13556 CancelRegion);
13557}
13558
13559static bool checkReductionClauseWithNogroup(Sema &S,
13560 ArrayRef<OMPClause *> Clauses) {
13561 const OMPClause *ReductionClause = nullptr;
13562 const OMPClause *NogroupClause = nullptr;
13563 for (const OMPClause *C : Clauses) {
13564 if (C->getClauseKind() == OMPC_reduction) {
13565 ReductionClause = C;
13566 if (NogroupClause)
13567 break;
13568 continue;
13569 }
13570 if (C->getClauseKind() == OMPC_nogroup) {
13571 NogroupClause = C;
13572 if (ReductionClause)
13573 break;
13574 continue;
13575 }
13576 }
13577 if (ReductionClause && NogroupClause) {
13578 S.Diag(Loc: ReductionClause->getBeginLoc(), DiagID: diag::err_omp_reduction_with_nogroup)
13579 << SourceRange(NogroupClause->getBeginLoc(),
13580 NogroupClause->getEndLoc());
13581 return true;
13582 }
13583 return false;
13584}
13585
13586StmtResult SemaOpenMP::ActOnOpenMPTaskLoopDirective(
13587 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13588 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13589 if (!AStmt)
13590 return StmtError();
13591
13592 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13593 OMPLoopBasedDirective::HelperExprs B;
13594 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13595 // define the nested loops number.
13596 unsigned NestedLoopCount =
13597 checkOpenMPLoop(DKind: OMPD_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13598 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13599 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13600 if (NestedLoopCount == 0)
13601 return StmtError();
13602
13603 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13604 "omp for loop exprs were not built");
13605
13606 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13607 // The grainsize clause and num_tasks clause are mutually exclusive and may
13608 // not appear on the same taskloop directive.
13609 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13610 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13611 return StmtError();
13612 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13613 // If a reduction clause is present on the taskloop directive, the nogroup
13614 // clause must not be specified.
13615 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13616 return StmtError();
13617
13618 SemaRef.setFunctionHasBranchProtectedScope();
13619 return OMPTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13620 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13621 DSAStack->isCancelRegion());
13622}
13623
13624StmtResult SemaOpenMP::ActOnOpenMPTaskLoopSimdDirective(
13625 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13626 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13627 if (!AStmt)
13628 return StmtError();
13629
13630 CapturedStmt *CS =
13631 setBranchProtectedScope(SemaRef, DKind: OMPD_taskloop_simd, AStmt);
13632
13633 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13634 OMPLoopBasedDirective::HelperExprs B;
13635 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13636 // define the nested loops number.
13637 unsigned NestedLoopCount =
13638 checkOpenMPLoop(DKind: OMPD_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13639 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13640 VarsWithImplicitDSA, Built&: B);
13641 if (NestedLoopCount == 0)
13642 return StmtError();
13643
13644 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13645 return StmtError();
13646
13647 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13648 // The grainsize clause and num_tasks clause are mutually exclusive and may
13649 // not appear on the same taskloop directive.
13650 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13651 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13652 return StmtError();
13653 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13654 // If a reduction clause is present on the taskloop directive, the nogroup
13655 // clause must not be specified.
13656 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13657 return StmtError();
13658 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13659 return StmtError();
13660
13661 return OMPTaskLoopSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13662 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13663}
13664
13665StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopDirective(
13666 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13667 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13668 if (!AStmt)
13669 return StmtError();
13670
13671 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13672 OMPLoopBasedDirective::HelperExprs B;
13673 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13674 // define the nested loops number.
13675 unsigned NestedLoopCount =
13676 checkOpenMPLoop(DKind: OMPD_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13677 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13678 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13679 if (NestedLoopCount == 0)
13680 return StmtError();
13681
13682 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13683 "omp for loop exprs were not built");
13684
13685 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13686 // The grainsize clause and num_tasks clause are mutually exclusive and may
13687 // not appear on the same taskloop directive.
13688 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13689 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13690 return StmtError();
13691 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13692 // If a reduction clause is present on the taskloop directive, the nogroup
13693 // clause must not be specified.
13694 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13695 return StmtError();
13696
13697 SemaRef.setFunctionHasBranchProtectedScope();
13698 return OMPMasterTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13699 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13700 DSAStack->isCancelRegion());
13701}
13702
13703StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopDirective(
13704 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13705 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13706 if (!AStmt)
13707 return StmtError();
13708
13709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13710 OMPLoopBasedDirective::HelperExprs B;
13711 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13712 // define the nested loops number.
13713 unsigned NestedLoopCount =
13714 checkOpenMPLoop(DKind: OMPD_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13715 /*OrderedLoopCountExpr=*/nullptr, AStmt, SemaRef,
13716 DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13717 if (NestedLoopCount == 0)
13718 return StmtError();
13719
13720 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13721 "omp for loop exprs were not built");
13722
13723 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13724 // The grainsize clause and num_tasks clause are mutually exclusive and may
13725 // not appear on the same taskloop directive.
13726 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13727 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13728 return StmtError();
13729 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13730 // If a reduction clause is present on the taskloop directive, the nogroup
13731 // clause must not be specified.
13732 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13733 return StmtError();
13734
13735 SemaRef.setFunctionHasBranchProtectedScope();
13736 return OMPMaskedTaskLoopDirective::Create(C: getASTContext(), StartLoc, EndLoc,
13737 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13738 DSAStack->isCancelRegion());
13739}
13740
13741StmtResult SemaOpenMP::ActOnOpenMPMasterTaskLoopSimdDirective(
13742 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13743 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13744 if (!AStmt)
13745 return StmtError();
13746
13747 CapturedStmt *CS =
13748 setBranchProtectedScope(SemaRef, DKind: OMPD_master_taskloop_simd, AStmt);
13749
13750 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13751 OMPLoopBasedDirective::HelperExprs B;
13752 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13753 // define the nested loops number.
13754 unsigned NestedLoopCount =
13755 checkOpenMPLoop(DKind: OMPD_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13756 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13757 VarsWithImplicitDSA, Built&: B);
13758 if (NestedLoopCount == 0)
13759 return StmtError();
13760
13761 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13762 return StmtError();
13763
13764 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13765 // The grainsize clause and num_tasks clause are mutually exclusive and may
13766 // not appear on the same taskloop directive.
13767 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13768 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13769 return StmtError();
13770 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13771 // If a reduction clause is present on the taskloop directive, the nogroup
13772 // clause must not be specified.
13773 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13774 return StmtError();
13775 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13776 return StmtError();
13777
13778 return OMPMasterTaskLoopSimdDirective::Create(
13779 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13780}
13781
13782StmtResult SemaOpenMP::ActOnOpenMPMaskedTaskLoopSimdDirective(
13783 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13784 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13785 if (!AStmt)
13786 return StmtError();
13787
13788 CapturedStmt *CS =
13789 setBranchProtectedScope(SemaRef, DKind: OMPD_masked_taskloop_simd, AStmt);
13790
13791 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13792 OMPLoopBasedDirective::HelperExprs B;
13793 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13794 // define the nested loops number.
13795 unsigned NestedLoopCount =
13796 checkOpenMPLoop(DKind: OMPD_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13797 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13798 VarsWithImplicitDSA, Built&: B);
13799 if (NestedLoopCount == 0)
13800 return StmtError();
13801
13802 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13803 return StmtError();
13804
13805 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13806 // The grainsize clause and num_tasks clause are mutually exclusive and may
13807 // not appear on the same taskloop directive.
13808 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13809 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13810 return StmtError();
13811 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13812 // If a reduction clause is present on the taskloop directive, the nogroup
13813 // clause must not be specified.
13814 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13815 return StmtError();
13816 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13817 return StmtError();
13818
13819 return OMPMaskedTaskLoopSimdDirective::Create(
13820 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13821}
13822
13823StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopDirective(
13824 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13825 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13826 if (!AStmt)
13827 return StmtError();
13828
13829 CapturedStmt *CS =
13830 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_master_taskloop, AStmt);
13831
13832 OMPLoopBasedDirective::HelperExprs B;
13833 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13834 // define the nested loops number.
13835 unsigned NestedLoopCount = checkOpenMPLoop(
13836 DKind: OMPD_parallel_master_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13837 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13838 VarsWithImplicitDSA, Built&: B);
13839 if (NestedLoopCount == 0)
13840 return StmtError();
13841
13842 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13843 "omp for loop exprs were not built");
13844
13845 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13846 // The grainsize clause and num_tasks clause are mutually exclusive and may
13847 // not appear on the same taskloop directive.
13848 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13849 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13850 return StmtError();
13851 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13852 // If a reduction clause is present on the taskloop directive, the nogroup
13853 // clause must not be specified.
13854 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13855 return StmtError();
13856
13857 return OMPParallelMasterTaskLoopDirective::Create(
13858 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13859 DSAStack->isCancelRegion());
13860}
13861
13862StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopDirective(
13863 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13864 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13865 if (!AStmt)
13866 return StmtError();
13867
13868 CapturedStmt *CS =
13869 setBranchProtectedScope(SemaRef, DKind: OMPD_parallel_masked_taskloop, AStmt);
13870
13871 OMPLoopBasedDirective::HelperExprs B;
13872 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13873 // define the nested loops number.
13874 unsigned NestedLoopCount = checkOpenMPLoop(
13875 DKind: OMPD_parallel_masked_taskloop, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13876 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13877 VarsWithImplicitDSA, Built&: B);
13878 if (NestedLoopCount == 0)
13879 return StmtError();
13880
13881 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13882 "omp for loop exprs were not built");
13883
13884 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13885 // The grainsize clause and num_tasks clause are mutually exclusive and may
13886 // not appear on the same taskloop directive.
13887 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13888 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13889 return StmtError();
13890 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13891 // If a reduction clause is present on the taskloop directive, the nogroup
13892 // clause must not be specified.
13893 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13894 return StmtError();
13895
13896 return OMPParallelMaskedTaskLoopDirective::Create(
13897 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
13898 DSAStack->isCancelRegion());
13899}
13900
13901StmtResult SemaOpenMP::ActOnOpenMPParallelMasterTaskLoopSimdDirective(
13902 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13903 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13904 if (!AStmt)
13905 return StmtError();
13906
13907 CapturedStmt *CS = setBranchProtectedScope(
13908 SemaRef, DKind: OMPD_parallel_master_taskloop_simd, AStmt);
13909
13910 OMPLoopBasedDirective::HelperExprs B;
13911 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13912 // define the nested loops number.
13913 unsigned NestedLoopCount = checkOpenMPLoop(
13914 DKind: OMPD_parallel_master_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13915 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13916 VarsWithImplicitDSA, Built&: B);
13917 if (NestedLoopCount == 0)
13918 return StmtError();
13919
13920 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13921 return StmtError();
13922
13923 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13924 // The grainsize clause and num_tasks clause are mutually exclusive and may
13925 // not appear on the same taskloop directive.
13926 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13927 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13928 return StmtError();
13929 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13930 // If a reduction clause is present on the taskloop directive, the nogroup
13931 // clause must not be specified.
13932 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13933 return StmtError();
13934 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13935 return StmtError();
13936
13937 return OMPParallelMasterTaskLoopSimdDirective::Create(
13938 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13939}
13940
13941StmtResult SemaOpenMP::ActOnOpenMPParallelMaskedTaskLoopSimdDirective(
13942 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13943 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13944 if (!AStmt)
13945 return StmtError();
13946
13947 CapturedStmt *CS = setBranchProtectedScope(
13948 SemaRef, DKind: OMPD_parallel_masked_taskloop_simd, AStmt);
13949
13950 OMPLoopBasedDirective::HelperExprs B;
13951 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
13952 // define the nested loops number.
13953 unsigned NestedLoopCount = checkOpenMPLoop(
13954 DKind: OMPD_parallel_masked_taskloop_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13955 /*OrderedLoopCountExpr=*/nullptr, AStmt: CS, SemaRef, DSA&: *DSAStack,
13956 VarsWithImplicitDSA, Built&: B);
13957 if (NestedLoopCount == 0)
13958 return StmtError();
13959
13960 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
13961 return StmtError();
13962
13963 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13964 // The grainsize clause and num_tasks clause are mutually exclusive and may
13965 // not appear on the same taskloop directive.
13966 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
13967 MutuallyExclusiveClauses: {OMPC_grainsize, OMPC_num_tasks}))
13968 return StmtError();
13969 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
13970 // If a reduction clause is present on the taskloop directive, the nogroup
13971 // clause must not be specified.
13972 if (checkReductionClauseWithNogroup(S&: SemaRef, Clauses))
13973 return StmtError();
13974 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
13975 return StmtError();
13976
13977 return OMPParallelMaskedTaskLoopSimdDirective::Create(
13978 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
13979}
13980
13981StmtResult SemaOpenMP::ActOnOpenMPDistributeDirective(
13982 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
13983 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
13984 if (!AStmt)
13985 return StmtError();
13986
13987 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
13988 OMPLoopBasedDirective::HelperExprs B;
13989 // In presence of clause 'collapse' with number of loops, it will
13990 // define the nested loops number.
13991 unsigned NestedLoopCount =
13992 checkOpenMPLoop(DKind: OMPD_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
13993 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt,
13994 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
13995 if (NestedLoopCount == 0)
13996 return StmtError();
13997
13998 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
13999 "omp for loop exprs were not built");
14000
14001 SemaRef.setFunctionHasBranchProtectedScope();
14002 auto *DistributeDirective = OMPDistributeDirective::Create(
14003 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14004 return DistributeDirective;
14005}
14006
14007StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForDirective(
14008 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14009 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14010 if (!AStmt)
14011 return StmtError();
14012
14013 CapturedStmt *CS =
14014 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_parallel_for, AStmt);
14015
14016 OMPLoopBasedDirective::HelperExprs B;
14017 // In presence of clause 'collapse' with number of loops, it will
14018 // define the nested loops number.
14019 unsigned NestedLoopCount = checkOpenMPLoop(
14020 DKind: OMPD_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14021 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14022 VarsWithImplicitDSA, Built&: B);
14023 if (NestedLoopCount == 0)
14024 return StmtError();
14025
14026 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14027 "omp for loop exprs were not built");
14028
14029 return OMPDistributeParallelForDirective::Create(
14030 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14031 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14032}
14033
14034StmtResult SemaOpenMP::ActOnOpenMPDistributeParallelForSimdDirective(
14035 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14036 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14037 if (!AStmt)
14038 return StmtError();
14039
14040 CapturedStmt *CS = setBranchProtectedScope(
14041 SemaRef, DKind: OMPD_distribute_parallel_for_simd, AStmt);
14042
14043 OMPLoopBasedDirective::HelperExprs B;
14044 // In presence of clause 'collapse' with number of loops, it will
14045 // define the nested loops number.
14046 unsigned NestedLoopCount = checkOpenMPLoop(
14047 DKind: OMPD_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14048 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14049 VarsWithImplicitDSA, Built&: B);
14050 if (NestedLoopCount == 0)
14051 return StmtError();
14052
14053 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14054 return StmtError();
14055
14056 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14057 return StmtError();
14058
14059 return OMPDistributeParallelForSimdDirective::Create(
14060 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14061}
14062
14063StmtResult SemaOpenMP::ActOnOpenMPDistributeSimdDirective(
14064 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14065 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14066 if (!AStmt)
14067 return StmtError();
14068
14069 CapturedStmt *CS =
14070 setBranchProtectedScope(SemaRef, DKind: OMPD_distribute_simd, AStmt);
14071
14072 OMPLoopBasedDirective::HelperExprs B;
14073 // In presence of clause 'collapse' with number of loops, it will
14074 // define the nested loops number.
14075 unsigned NestedLoopCount =
14076 checkOpenMPLoop(DKind: OMPD_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14077 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14078 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14079 if (NestedLoopCount == 0)
14080 return StmtError();
14081
14082 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14083 return StmtError();
14084
14085 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14086 return StmtError();
14087
14088 return OMPDistributeSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14089 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14090}
14091
14092StmtResult SemaOpenMP::ActOnOpenMPTargetParallelForSimdDirective(
14093 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14094 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14095 if (!AStmt)
14096 return StmtError();
14097
14098 CapturedStmt *CS =
14099 setBranchProtectedScope(SemaRef, DKind: OMPD_target_parallel_for_simd, AStmt);
14100
14101 OMPLoopBasedDirective::HelperExprs B;
14102 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
14103 // define the nested loops number.
14104 unsigned NestedLoopCount = checkOpenMPLoop(
14105 DKind: OMPD_target_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14106 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), AStmt: CS, SemaRef, DSA&: *DSAStack,
14107 VarsWithImplicitDSA, Built&: B);
14108 if (NestedLoopCount == 0)
14109 return StmtError();
14110
14111 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14112 return StmtError();
14113
14114 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14115 return StmtError();
14116
14117 return OMPTargetParallelForSimdDirective::Create(
14118 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14119}
14120
14121StmtResult SemaOpenMP::ActOnOpenMPTargetSimdDirective(
14122 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14123 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14124 if (!AStmt)
14125 return StmtError();
14126
14127 CapturedStmt *CS = setBranchProtectedScope(SemaRef, DKind: OMPD_target_simd, AStmt);
14128
14129 OMPLoopBasedDirective::HelperExprs B;
14130 // In presence of clause 'collapse' with number of loops, it will define the
14131 // nested loops number.
14132 unsigned NestedLoopCount =
14133 checkOpenMPLoop(DKind: OMPD_target_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14134 OrderedLoopCountExpr: getOrderedNumberExpr(Clauses), 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 OMPTargetSimdDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14146 CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14147}
14148
14149StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeDirective(
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_teams_distribute, 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_teams_distribute, 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 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14169 "omp teams distribute loop exprs were not built");
14170
14171 DSAStack->setParentTeamsRegionLoc(StartLoc);
14172
14173 return OMPTeamsDistributeDirective::Create(
14174 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14175}
14176
14177StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeSimdDirective(
14178 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14179 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14180 if (!AStmt)
14181 return StmtError();
14182
14183 CapturedStmt *CS =
14184 setBranchProtectedScope(SemaRef, DKind: OMPD_teams_distribute_simd, AStmt);
14185
14186 OMPLoopBasedDirective::HelperExprs B;
14187 // In presence of clause 'collapse' with number of loops, it will
14188 // define the nested loops number.
14189 unsigned NestedLoopCount = checkOpenMPLoop(
14190 DKind: OMPD_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14191 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14192 VarsWithImplicitDSA, Built&: B);
14193 if (NestedLoopCount == 0)
14194 return StmtError();
14195
14196 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14197 return StmtError();
14198
14199 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14200 return StmtError();
14201
14202 DSAStack->setParentTeamsRegionLoc(StartLoc);
14203
14204 return OMPTeamsDistributeSimdDirective::Create(
14205 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14206}
14207
14208StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
14209 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14210 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14211 if (!AStmt)
14212 return StmtError();
14213
14214 CapturedStmt *CS = setBranchProtectedScope(
14215 SemaRef, DKind: OMPD_teams_distribute_parallel_for_simd, AStmt);
14216
14217 OMPLoopBasedDirective::HelperExprs B;
14218 // In presence of clause 'collapse' with number of loops, it will
14219 // define the nested loops number.
14220 unsigned NestedLoopCount = checkOpenMPLoop(
14221 DKind: OMPD_teams_distribute_parallel_for_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14222 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14223 VarsWithImplicitDSA, Built&: B);
14224 if (NestedLoopCount == 0)
14225 return StmtError();
14226
14227 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14228 return StmtError();
14229
14230 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14231 return StmtError();
14232
14233 DSAStack->setParentTeamsRegionLoc(StartLoc);
14234
14235 return OMPTeamsDistributeParallelForSimdDirective::Create(
14236 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14237}
14238
14239StmtResult SemaOpenMP::ActOnOpenMPTeamsDistributeParallelForDirective(
14240 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14241 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14242 if (!AStmt)
14243 return StmtError();
14244
14245 CapturedStmt *CS = setBranchProtectedScope(
14246 SemaRef, DKind: OMPD_teams_distribute_parallel_for, AStmt);
14247
14248 OMPLoopBasedDirective::HelperExprs B;
14249 // In presence of clause 'collapse' with number of loops, it will
14250 // define the nested loops number.
14251 unsigned NestedLoopCount = checkOpenMPLoop(
14252 DKind: OMPD_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14253 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14254 VarsWithImplicitDSA, Built&: B);
14255
14256 if (NestedLoopCount == 0)
14257 return StmtError();
14258
14259 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14260 "omp for loop exprs were not built");
14261
14262 DSAStack->setParentTeamsRegionLoc(StartLoc);
14263
14264 return OMPTeamsDistributeParallelForDirective::Create(
14265 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14266 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14267}
14268
14269StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDirective(
14270 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14271 SourceLocation EndLoc) {
14272 if (!AStmt)
14273 return StmtError();
14274
14275 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams, AStmt);
14276
14277 const OMPClause *BareClause = nullptr;
14278 bool HasThreadLimitAndNumTeamsClause = hasClauses(Clauses, K: OMPC_num_teams) &&
14279 hasClauses(Clauses, K: OMPC_thread_limit);
14280 bool HasBareClause = llvm::any_of(Range&: Clauses, P: [&](const OMPClause *C) {
14281 BareClause = C;
14282 return C->getClauseKind() == OMPC_ompx_bare;
14283 });
14284
14285 if (HasBareClause && !HasThreadLimitAndNumTeamsClause) {
14286 Diag(Loc: BareClause->getBeginLoc(), DiagID: diag::err_ompx_bare_no_grid);
14287 return StmtError();
14288 }
14289
14290 unsigned ClauseMaxNumExprs = HasBareClause ? 3 : 1;
14291 unsigned DiagNo = HasBareClause
14292 ? diag::err_ompx_more_than_three_expr_not_allowed
14293 : diag::err_omp_multi_expr_not_allowed;
14294 if (!checkNumExprsInClause<OMPNumTeamsClause>(SemaRef&: *this, Clauses,
14295 MaxNum: ClauseMaxNumExprs, Diag: DiagNo) ||
14296 !checkNumExprsInClause<OMPThreadLimitClause>(SemaRef&: *this, Clauses,
14297 MaxNum: ClauseMaxNumExprs, Diag: DiagNo))
14298 return StmtError();
14299
14300 return OMPTargetTeamsDirective::Create(C: getASTContext(), StartLoc, EndLoc,
14301 Clauses, AssociatedStmt: AStmt);
14302}
14303
14304StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeDirective(
14305 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14306 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14307 if (!AStmt)
14308 return StmtError();
14309
14310 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14311 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed) ||
14312 !checkNumExprsInClause<OMPThreadLimitClause>(
14313 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14314 return StmtError();
14315
14316 CapturedStmt *CS =
14317 setBranchProtectedScope(SemaRef, DKind: OMPD_target_teams_distribute, AStmt);
14318
14319 OMPLoopBasedDirective::HelperExprs B;
14320 // In presence of clause 'collapse' with number of loops, it will
14321 // define the nested loops number.
14322 unsigned NestedLoopCount = checkOpenMPLoop(
14323 DKind: OMPD_target_teams_distribute, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14324 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14325 VarsWithImplicitDSA, Built&: B);
14326 if (NestedLoopCount == 0)
14327 return StmtError();
14328
14329 assert((SemaRef.CurContext->isDependentContext() || B.builtAll()) &&
14330 "omp target teams distribute loop exprs were not built");
14331
14332 return OMPTargetTeamsDistributeDirective::Create(
14333 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14334}
14335
14336StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
14337 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14338 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14339 if (!AStmt)
14340 return StmtError();
14341
14342 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14343 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed) ||
14344 !checkNumExprsInClause<OMPThreadLimitClause>(
14345 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14346 return StmtError();
14347
14348 CapturedStmt *CS = setBranchProtectedScope(
14349 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for, AStmt);
14350
14351 OMPLoopBasedDirective::HelperExprs B;
14352 // In presence of clause 'collapse' with number of loops, it will
14353 // define the nested loops number.
14354 unsigned NestedLoopCount = checkOpenMPLoop(
14355 DKind: OMPD_target_teams_distribute_parallel_for, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14356 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14357 VarsWithImplicitDSA, Built&: B);
14358 if (NestedLoopCount == 0)
14359 return StmtError();
14360
14361 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14362 return StmtError();
14363
14364 return OMPTargetTeamsDistributeParallelForDirective::Create(
14365 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B,
14366 DSAStack->getTaskgroupReductionRef(), DSAStack->isCancelRegion());
14367}
14368
14369StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
14370 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14371 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14372 if (!AStmt)
14373 return StmtError();
14374
14375 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14376 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed) ||
14377 !checkNumExprsInClause<OMPThreadLimitClause>(
14378 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14379 return StmtError();
14380
14381 CapturedStmt *CS = setBranchProtectedScope(
14382 SemaRef, DKind: OMPD_target_teams_distribute_parallel_for_simd, AStmt);
14383
14384 OMPLoopBasedDirective::HelperExprs B;
14385 // In presence of clause 'collapse' with number of loops, it will
14386 // define the nested loops number.
14387 unsigned NestedLoopCount =
14388 checkOpenMPLoop(DKind: OMPD_target_teams_distribute_parallel_for_simd,
14389 CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14390 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS,
14391 SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA, Built&: B);
14392 if (NestedLoopCount == 0)
14393 return StmtError();
14394
14395 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14396 return StmtError();
14397
14398 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14399 return StmtError();
14400
14401 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
14402 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14403}
14404
14405StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeSimdDirective(
14406 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
14407 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
14408 if (!AStmt)
14409 return StmtError();
14410
14411 if (!checkNumExprsInClause<OMPNumTeamsClause>(
14412 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed) ||
14413 !checkNumExprsInClause<OMPThreadLimitClause>(
14414 SemaRef&: *this, Clauses, /*MaxNum=*/1, Diag: diag::err_omp_multi_expr_not_allowed))
14415 return StmtError();
14416
14417 CapturedStmt *CS = setBranchProtectedScope(
14418 SemaRef, DKind: OMPD_target_teams_distribute_simd, AStmt);
14419
14420 OMPLoopBasedDirective::HelperExprs B;
14421 // In presence of clause 'collapse' with number of loops, it will
14422 // define the nested loops number.
14423 unsigned NestedLoopCount = checkOpenMPLoop(
14424 DKind: OMPD_target_teams_distribute_simd, CollapseLoopCountExpr: getCollapseNumberExpr(Clauses),
14425 OrderedLoopCountExpr: nullptr /*ordered not a clause on distribute*/, AStmt: CS, SemaRef, DSA&: *DSAStack,
14426 VarsWithImplicitDSA, Built&: B);
14427 if (NestedLoopCount == 0)
14428 return StmtError();
14429
14430 if (finishLinearClauses(SemaRef, Clauses, B, DSAStack))
14431 return StmtError();
14432
14433 if (checkSimdlenSafelenSpecified(S&: SemaRef, Clauses))
14434 return StmtError();
14435
14436 return OMPTargetTeamsDistributeSimdDirective::Create(
14437 C: getASTContext(), StartLoc, EndLoc, CollapsedNum: NestedLoopCount, Clauses, AssociatedStmt: AStmt, Exprs: B);
14438}
14439
14440/// Updates OriginalInits by checking Transform against loop transformation
14441/// directives and appending their pre-inits if a match is found.
14442static void updatePreInits(OMPLoopTransformationDirective *Transform,
14443 SmallVectorImpl<Stmt *> &PreInits) {
14444 Stmt *Dir = Transform->getDirective();
14445 switch (Dir->getStmtClass()) {
14446#define STMT(CLASS, PARENT)
14447#define ABSTRACT_STMT(CLASS)
14448#define COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT) \
14449 case Stmt::CLASS##Class: \
14450 appendFlattenedStmtList(PreInits, \
14451 static_cast<const CLASS *>(Dir)->getPreInits()); \
14452 break;
14453#define OMPCANONICALLOOPNESTTRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
14454 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
14455#define OMPCANONICALLOOPSEQUENCETRANSFORMATIONDIRECTIVE(CLASS, PARENT) \
14456 COMMON_OMP_LOOP_TRANSFORMATION(CLASS, PARENT)
14457#include "clang/AST/StmtNodes.inc"
14458#undef COMMON_OMP_LOOP_TRANSFORMATION
14459 default:
14460 llvm_unreachable("Not a loop transformation");
14461 }
14462}
14463
14464bool SemaOpenMP::checkTransformableLoopNest(
14465 OpenMPDirectiveKind Kind, Stmt *AStmt, int NumLoops,
14466 SmallVectorImpl<OMPLoopBasedDirective::HelperExprs> &LoopHelpers,
14467 Stmt *&Body, SmallVectorImpl<SmallVector<Stmt *>> &OriginalInits) {
14468 OriginalInits.emplace_back();
14469 bool Result = OMPLoopBasedDirective::doForAllLoops(
14470 CurStmt: AStmt->IgnoreContainers(), /*TryImperfectlyNestedLoops=*/false, NumLoops,
14471 Callback: [this, &LoopHelpers, &Body, &OriginalInits, Kind](unsigned Cnt,
14472 Stmt *CurStmt) {
14473 VarsWithInheritedDSAType TmpDSA;
14474 unsigned SingleNumLoops =
14475 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: CurStmt, SemaRef, DSA&: *DSAStack,
14476 VarsWithImplicitDSA&: TmpDSA, Built&: LoopHelpers[Cnt]);
14477 if (SingleNumLoops == 0)
14478 return true;
14479 assert(SingleNumLoops == 1 && "Expect single loop iteration space");
14480 if (auto *For = dyn_cast<ForStmt>(Val: CurStmt)) {
14481 OriginalInits.back().push_back(Elt: For->getInit());
14482 Body = For->getBody();
14483 } else {
14484 assert(isa<CXXForRangeStmt>(CurStmt) &&
14485 "Expected canonical for or range-based for loops.");
14486 auto *CXXFor = cast<CXXForRangeStmt>(Val: CurStmt);
14487 OriginalInits.back().push_back(Elt: CXXFor->getBeginStmt());
14488 Body = CXXFor->getBody();
14489 }
14490 OriginalInits.emplace_back();
14491 return false;
14492 },
14493 OnTransformationCallback: [&OriginalInits](OMPLoopTransformationDirective *Transform) {
14494 updatePreInits(Transform, PreInits&: OriginalInits.back());
14495 });
14496 assert(OriginalInits.back().empty() && "No preinit after innermost loop");
14497 OriginalInits.pop_back();
14498 return Result;
14499}
14500
14501/// Counts the total number of OpenMP canonical nested loops, including the
14502/// outermost loop (the original loop). PRECONDITION of this visitor is that it
14503/// must be invoked from the original loop to be analyzed. The traversal stops
14504/// for Decl's and Expr's given that they may contain inner loops that must not
14505/// be counted.
14506///
14507/// Example AST structure for the code:
14508///
14509/// int main() {
14510/// #pragma omp fuse
14511/// {
14512/// for (int i = 0; i < 100; i++) { <-- Outer loop
14513/// []() {
14514/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (1)
14515/// };
14516/// for(int j = 0; j < 5; ++j) {} <-- Inner loop
14517/// }
14518/// for (int r = 0; i < 100; i++) { <-- Outer loop
14519/// struct LocalClass {
14520/// void bar() {
14521/// for(int j = 0; j < 100; j++) {} <-- NOT A LOOP (2)
14522/// }
14523/// };
14524/// for(int k = 0; k < 10; ++k) {} <-- Inner loop
14525/// {x = 5; for(k = 0; k < 10; ++k) x += k; x}; <-- NOT A LOOP (3)
14526/// }
14527/// }
14528/// }
14529/// (1) because in a different function (here: a lambda)
14530/// (2) because in a different function (here: class method)
14531/// (3) because considered to be intervening-code of non-perfectly nested loop
14532/// Result: Loop 'i' contains 2 loops, Loop 'r' also contains 2 loops.
14533class NestedLoopCounterVisitor final : public DynamicRecursiveASTVisitor {
14534private:
14535 unsigned NestedLoopCount = 0;
14536
14537public:
14538 explicit NestedLoopCounterVisitor() = default;
14539
14540 unsigned getNestedLoopCount() const { return NestedLoopCount; }
14541
14542 bool VisitForStmt(ForStmt *FS) override {
14543 ++NestedLoopCount;
14544 return true;
14545 }
14546
14547 bool VisitCXXForRangeStmt(CXXForRangeStmt *FRS) override {
14548 ++NestedLoopCount;
14549 return true;
14550 }
14551
14552 bool TraverseStmt(Stmt *S) override {
14553 if (!S)
14554 return true;
14555
14556 // Skip traversal of all expressions, including special cases like
14557 // LambdaExpr, StmtExpr, BlockExpr, and RequiresExpr. These expressions
14558 // may contain inner statements (and even loops), but they are not part
14559 // of the syntactic body of the surrounding loop structure.
14560 // Therefore must not be counted.
14561 if (isa<Expr>(Val: S))
14562 return true;
14563
14564 // Only recurse into CompoundStmt (block {}) and loop bodies.
14565 if (isa<CompoundStmt, ForStmt, CXXForRangeStmt>(Val: S)) {
14566 return DynamicRecursiveASTVisitor::TraverseStmt(S);
14567 }
14568
14569 // Stop traversal of the rest of statements, that break perfect
14570 // loop nesting, such as control flow (IfStmt, SwitchStmt...).
14571 return true;
14572 }
14573
14574 bool TraverseDecl(Decl *D) override {
14575 // Stop in the case of finding a declaration, it is not important
14576 // in order to find nested loops (Possible CXXRecordDecl, RecordDecl,
14577 // FunctionDecl...).
14578 return true;
14579 }
14580};
14581
14582bool SemaOpenMP::analyzeLoopSequence(Stmt *LoopSeqStmt,
14583 LoopSequenceAnalysis &SeqAnalysis,
14584 ASTContext &Context,
14585 OpenMPDirectiveKind Kind) {
14586 VarsWithInheritedDSAType TmpDSA;
14587 // Helper Lambda to handle storing initialization and body statements for
14588 // both ForStmt and CXXForRangeStmt.
14589 auto StoreLoopStatements = [](LoopAnalysis &Analysis, Stmt *LoopStmt) {
14590 if (auto *For = dyn_cast<ForStmt>(Val: LoopStmt)) {
14591 Analysis.OriginalInits.push_back(Elt: For->getInit());
14592 Analysis.TheForStmt = For;
14593 } else {
14594 auto *CXXFor = cast<CXXForRangeStmt>(Val: LoopStmt);
14595 Analysis.OriginalInits.push_back(Elt: CXXFor->getBeginStmt());
14596 Analysis.TheForStmt = CXXFor;
14597 }
14598 };
14599
14600 // Helper lambda functions to encapsulate the processing of different
14601 // derivations of the canonical loop sequence grammar
14602 // Modularized code for handling loop generation and transformations.
14603 auto AnalyzeLoopGeneration = [&](Stmt *Child) {
14604 auto *LoopTransform = cast<OMPLoopTransformationDirective>(Val: Child);
14605 Stmt *TransformedStmt = LoopTransform->getTransformedStmt();
14606 unsigned NumGeneratedTopLevelLoops =
14607 LoopTransform->getNumGeneratedTopLevelLoops();
14608 // Handle the case where transformed statement is not available due to
14609 // dependent contexts
14610 if (!TransformedStmt) {
14611 if (NumGeneratedTopLevelLoops > 0) {
14612 SeqAnalysis.LoopSeqSize += NumGeneratedTopLevelLoops;
14613 return true;
14614 }
14615 // Unroll full (0 loops produced)
14616 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14617 << 0 << getOpenMPDirectiveName(D: Kind);
14618 return false;
14619 }
14620 // Handle loop transformations with multiple loop nests
14621 // Unroll full
14622 if (!NumGeneratedTopLevelLoops) {
14623 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14624 << 0 << getOpenMPDirectiveName(D: Kind);
14625 return false;
14626 }
14627 // Loop transformatons such as split or loopranged fuse
14628 if (NumGeneratedTopLevelLoops > 1) {
14629 // Get the preinits related to this loop sequence generating
14630 // loop transformation (i.e loopranged fuse, split...)
14631 // These preinits differ slightly from regular inits/pre-inits related
14632 // to single loop generating loop transformations (interchange, unroll)
14633 // given that they are not bounded to a particular loop nest
14634 // so they need to be treated independently
14635 updatePreInits(Transform: LoopTransform, PreInits&: SeqAnalysis.LoopSequencePreInits);
14636 return analyzeLoopSequence(LoopSeqStmt: TransformedStmt, SeqAnalysis, Context, Kind);
14637 }
14638 // Vast majority: (Tile, Unroll, Stripe, Reverse, Interchange, Fuse all)
14639 // Process the transformed loop statement
14640 LoopAnalysis &NewTransformedSingleLoop =
14641 SeqAnalysis.Loops.emplace_back(Args&: Child);
14642 unsigned IsCanonical = checkOpenMPLoop(
14643 DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: TransformedStmt, SemaRef, DSA&: *DSAStack, VarsWithImplicitDSA&: TmpDSA,
14644 Built&: NewTransformedSingleLoop.HelperExprs);
14645
14646 if (!IsCanonical)
14647 return false;
14648
14649 StoreLoopStatements(NewTransformedSingleLoop, TransformedStmt);
14650 updatePreInits(Transform: LoopTransform, PreInits&: NewTransformedSingleLoop.TransformsPreInits);
14651
14652 SeqAnalysis.LoopSeqSize++;
14653 return true;
14654 };
14655
14656 // Modularized code for handling regular canonical loops.
14657 auto AnalyzeRegularLoop = [&](Stmt *Child) {
14658 LoopAnalysis &NewRegularLoop = SeqAnalysis.Loops.emplace_back(Args&: Child);
14659 unsigned IsCanonical =
14660 checkOpenMPLoop(DKind: Kind, CollapseLoopCountExpr: nullptr, OrderedLoopCountExpr: nullptr, AStmt: Child, SemaRef, DSA&: *DSAStack,
14661 VarsWithImplicitDSA&: TmpDSA, Built&: NewRegularLoop.HelperExprs);
14662
14663 if (!IsCanonical)
14664 return false;
14665
14666 StoreLoopStatements(NewRegularLoop, Child);
14667 NestedLoopCounterVisitor NLCV;
14668 NLCV.TraverseStmt(S: Child);
14669 return true;
14670 };
14671
14672 // High level grammar validation.
14673 for (Stmt *Child : LoopSeqStmt->children()) {
14674 if (!Child)
14675 continue;
14676 // Skip over non-loop-sequence statements.
14677 if (!LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
14678 Child = Child->IgnoreContainers();
14679 // Ignore empty compound statement.
14680 if (!Child)
14681 continue;
14682 // In the case of a nested loop sequence ignoring containers would not
14683 // be enough, a recurisve transversal of the loop sequence is required.
14684 if (isa<CompoundStmt>(Val: Child)) {
14685 if (!analyzeLoopSequence(LoopSeqStmt: Child, SeqAnalysis, Context, Kind))
14686 return false;
14687 // Already been treated, skip this children
14688 continue;
14689 }
14690 }
14691 // Regular loop sequence handling.
14692 if (LoopSequenceAnalysis::isLoopSequenceDerivation(S: Child)) {
14693 if (LoopAnalysis::isLoopTransformation(S: Child)) {
14694 if (!AnalyzeLoopGeneration(Child))
14695 return false;
14696 // AnalyzeLoopGeneration updates SeqAnalysis.LoopSeqSize accordingly.
14697 } else {
14698 if (!AnalyzeRegularLoop(Child))
14699 return false;
14700 SeqAnalysis.LoopSeqSize++;
14701 }
14702 } else {
14703 // Report error for invalid statement inside canonical loop sequence.
14704 Diag(Loc: Child->getBeginLoc(), DiagID: diag::err_omp_not_for)
14705 << 0 << getOpenMPDirectiveName(D: Kind);
14706 return false;
14707 }
14708 }
14709 return true;
14710}
14711
14712bool SemaOpenMP::checkTransformableLoopSequence(
14713 OpenMPDirectiveKind Kind, Stmt *AStmt, LoopSequenceAnalysis &SeqAnalysis,
14714 ASTContext &Context) {
14715 // Following OpenMP 6.0 API Specification, a Canonical Loop Sequence follows
14716 // the grammar:
14717 //
14718 // canonical-loop-sequence:
14719 // {
14720 // loop-sequence+
14721 // }
14722 // where loop-sequence can be any of the following:
14723 // 1. canonical-loop-sequence
14724 // 2. loop-nest
14725 // 3. loop-sequence-generating-construct (i.e OMPLoopTransformationDirective)
14726 //
14727 // To recognise and traverse this structure the helper function
14728 // analyzeLoopSequence serves as the recurisve entry point
14729 // and tries to match the input AST to the canonical loop sequence grammar
14730 // structure. This function will perform both a semantic and syntactical
14731 // analysis of the given statement according to OpenMP 6.0 definition of
14732 // the aforementioned canonical loop sequence.
14733
14734 // We expect an outer compound statement.
14735 if (!isa<CompoundStmt>(Val: AStmt)) {
14736 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_not_a_loop_sequence)
14737 << getOpenMPDirectiveName(D: Kind);
14738 return false;
14739 }
14740
14741 // Recursive entry point to process the main loop sequence
14742 if (!analyzeLoopSequence(LoopSeqStmt: AStmt, SeqAnalysis, Context, Kind))
14743 return false;
14744
14745 // Diagnose an empty loop sequence.
14746 if (!SeqAnalysis.LoopSeqSize) {
14747 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_empty_loop_sequence)
14748 << getOpenMPDirectiveName(D: Kind);
14749 return false;
14750 }
14751 return true;
14752}
14753
14754/// Add preinit statements that need to be propagated from the selected loop.
14755static void addLoopPreInits(ASTContext &Context,
14756 OMPLoopBasedDirective::HelperExprs &LoopHelper,
14757 Stmt *LoopStmt, ArrayRef<Stmt *> OriginalInit,
14758 SmallVectorImpl<Stmt *> &PreInits) {
14759
14760 // For range-based for-statements, ensure that their syntactic sugar is
14761 // executed by adding them as pre-init statements.
14762 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt)) {
14763 Stmt *RangeInit = CXXRangeFor->getInit();
14764 if (RangeInit)
14765 PreInits.push_back(Elt: RangeInit);
14766
14767 DeclStmt *RangeStmt = CXXRangeFor->getRangeStmt();
14768 PreInits.push_back(Elt: new (Context) DeclStmt(RangeStmt->getDeclGroup(),
14769 RangeStmt->getBeginLoc(),
14770 RangeStmt->getEndLoc()));
14771
14772 DeclStmt *RangeEnd = CXXRangeFor->getEndStmt();
14773 PreInits.push_back(Elt: new (Context) DeclStmt(RangeEnd->getDeclGroup(),
14774 RangeEnd->getBeginLoc(),
14775 RangeEnd->getEndLoc()));
14776 }
14777
14778 llvm::append_range(C&: PreInits, R&: OriginalInit);
14779
14780 // List of OMPCapturedExprDecl, for __begin, __end, and NumIterations
14781 if (auto *PI = cast_or_null<DeclStmt>(Val: LoopHelper.PreInits)) {
14782 PreInits.push_back(Elt: new (Context) DeclStmt(
14783 PI->getDeclGroup(), PI->getBeginLoc(), PI->getEndLoc()));
14784 }
14785
14786 // Gather declarations for the data members used as counters.
14787 for (Expr *CounterRef : LoopHelper.Counters) {
14788 auto *CounterDecl = cast<DeclRefExpr>(Val: CounterRef)->getDecl();
14789 if (isa<OMPCapturedExprDecl>(Val: CounterDecl))
14790 PreInits.push_back(Elt: new (Context) DeclStmt(
14791 DeclGroupRef(CounterDecl), SourceLocation(), SourceLocation()));
14792 }
14793}
14794
14795/// Collect the loop statements (ForStmt or CXXRangeForStmt) of the affected
14796/// loop of a construct.
14797static void collectLoopStmts(Stmt *AStmt, MutableArrayRef<Stmt *> LoopStmts) {
14798 size_t NumLoops = LoopStmts.size();
14799 OMPLoopBasedDirective::doForAllLoops(
14800 CurStmt: AStmt, /*TryImperfectlyNestedLoops=*/false, NumLoops,
14801 Callback: [LoopStmts](unsigned Cnt, Stmt *CurStmt) {
14802 assert(!LoopStmts[Cnt] && "Loop statement must not yet be assigned");
14803 LoopStmts[Cnt] = CurStmt;
14804 return false;
14805 });
14806 assert(!is_contained(LoopStmts, nullptr) &&
14807 "Expecting a loop statement for each affected loop");
14808}
14809
14810/// Build and return a DeclRefExpr for the floor induction variable using the
14811/// SemaRef and the provided parameters.
14812static Expr *makeFloorIVRef(Sema &SemaRef, ArrayRef<VarDecl *> FloorIndVars,
14813 int I, QualType IVTy, DeclRefExpr *OrigCntVar) {
14814 return buildDeclRefExpr(S&: SemaRef, D: FloorIndVars[I], Ty: IVTy,
14815 Loc: OrigCntVar->getExprLoc());
14816}
14817
14818StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
14819 Stmt *AStmt,
14820 SourceLocation StartLoc,
14821 SourceLocation EndLoc) {
14822 ASTContext &Context = getASTContext();
14823 Scope *CurScope = SemaRef.getCurScope();
14824
14825 const auto *SizesClause =
14826 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
14827 if (!SizesClause ||
14828 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](Expr *E) { return !E; }))
14829 return StmtError();
14830 unsigned NumLoops = SizesClause->getNumSizes();
14831
14832 // Empty statement should only be possible if there already was an error.
14833 if (!AStmt)
14834 return StmtError();
14835
14836 // Verify and diagnose loop nest.
14837 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
14838 Stmt *Body = nullptr;
14839 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
14840 if (!checkTransformableLoopNest(Kind: OMPD_tile, AStmt, NumLoops, LoopHelpers, Body,
14841 OriginalInits))
14842 return StmtError();
14843
14844 // Delay tiling to when template is completely instantiated.
14845 if (SemaRef.CurContext->isDependentContext())
14846 return OMPTileDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
14847 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
14848
14849 assert(LoopHelpers.size() == NumLoops &&
14850 "Expecting loop iteration space dimensionality to match number of "
14851 "affected loops");
14852 assert(OriginalInits.size() == NumLoops &&
14853 "Expecting loop iteration space dimensionality to match number of "
14854 "affected loops");
14855
14856 // Collect all affected loop statements.
14857 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
14858 collectLoopStmts(AStmt, LoopStmts);
14859
14860 SmallVector<Stmt *, 4> PreInits;
14861 CaptureVars CopyTransformer(SemaRef);
14862
14863 // Create iteration variables for the generated loops.
14864 SmallVector<VarDecl *, 4> FloorIndVars;
14865 SmallVector<VarDecl *, 4> TileIndVars;
14866 FloorIndVars.resize(N: NumLoops);
14867 TileIndVars.resize(N: NumLoops);
14868 for (unsigned I = 0; I < NumLoops; ++I) {
14869 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
14870
14871 assert(LoopHelper.Counters.size() == 1 &&
14872 "Expect single-dimensional loop iteration space");
14873 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
14874 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
14875 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
14876 QualType CntTy = IterVarRef->getType();
14877
14878 // Iteration variable for the floor (i.e. outer) loop.
14879 {
14880 std::string FloorCntName =
14881 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
14882 VarDecl *FloorCntDecl =
14883 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
14884 FloorIndVars[I] = FloorCntDecl;
14885 }
14886
14887 // Iteration variable for the tile (i.e. inner) loop.
14888 {
14889 std::string TileCntName =
14890 (Twine(".tile_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
14891
14892 // Reuse the iteration variable created by checkOpenMPLoop. It is also
14893 // used by the expressions to derive the original iteration variable's
14894 // value from the logical iteration number.
14895 auto *TileCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
14896 TileCntDecl->setDeclName(
14897 &SemaRef.PP.getIdentifierTable().get(Name: TileCntName));
14898 TileIndVars[I] = TileCntDecl;
14899 }
14900
14901 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
14902 PreInits);
14903 }
14904
14905 // Once the original iteration values are set, append the innermost body.
14906 Stmt *Inner = Body;
14907
14908 auto MakeDimTileSize = [&SemaRef = this->SemaRef, &CopyTransformer, &Context,
14909 SizesClause, CurScope](int I) -> Expr * {
14910 Expr *DimTileSizeExpr = SizesClause->getSizesRefs()[I];
14911
14912 if (DimTileSizeExpr->containsErrors())
14913 return nullptr;
14914
14915 if (isa<ConstantExpr>(Val: DimTileSizeExpr))
14916 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr));
14917
14918 // When the tile size is not a constant but a variable, it is possible to
14919 // pass non-positive numbers. For instance:
14920 // \code{c}
14921 // int a = 0;
14922 // #pragma omp tile sizes(a)
14923 // for (int i = 0; i < 42; ++i)
14924 // body(i);
14925 // \endcode
14926 // Although there is no meaningful interpretation of the tile size, the body
14927 // should still be executed 42 times to avoid surprises. To preserve the
14928 // invariant that every loop iteration is executed exactly once and not
14929 // cause an infinite loop, apply a minimum tile size of one.
14930 // Build expr:
14931 // \code{c}
14932 // (TS <= 0) ? 1 : TS
14933 // \endcode
14934 QualType DimTy = DimTileSizeExpr->getType();
14935 uint64_t DimWidth = Context.getTypeSize(T: DimTy);
14936 IntegerLiteral *Zero = IntegerLiteral::Create(
14937 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
14938 IntegerLiteral *One =
14939 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
14940 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
14941 S: CurScope, OpLoc: {}, Opc: BO_LE,
14942 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), RHSExpr: Zero));
14943 Expr *MinOne = new (Context) ConditionalOperator(
14944 Cond, {}, One, {},
14945 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimTileSizeExpr)), DimTy,
14946 VK_PRValue, OK_Ordinary);
14947 return MinOne;
14948 };
14949
14950 // Create tile loops from the inside to the outside.
14951 for (int I = NumLoops - 1; I >= 0; --I) {
14952 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
14953 Expr *NumIterations = LoopHelper.NumIterations;
14954 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
14955 QualType IVTy = NumIterations->getType();
14956 Stmt *LoopStmt = LoopStmts[I];
14957
14958 // Commonly used variables. One of the constraints of an AST is that every
14959 // node object must appear at most once, hence we define a lambda that
14960 // creates a new AST node at every use.
14961 auto MakeTileIVRef = [&SemaRef = this->SemaRef, &TileIndVars, I, IVTy,
14962 OrigCntVar]() {
14963 return buildDeclRefExpr(S&: SemaRef, D: TileIndVars[I], Ty: IVTy,
14964 Loc: OrigCntVar->getExprLoc());
14965 };
14966
14967 // For init-statement: auto .tile.iv = .floor.iv
14968 SemaRef.AddInitializerToDecl(
14969 dcl: TileIndVars[I],
14970 init: SemaRef
14971 .DefaultLvalueConversion(
14972 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
14973 .get(),
14974 /*DirectInit=*/false);
14975 Decl *CounterDecl = TileIndVars[I];
14976 StmtResult InitStmt = new (Context)
14977 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
14978 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
14979 if (!InitStmt.isUsable())
14980 return StmtError();
14981
14982 // For cond-expression:
14983 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations)
14984 Expr *DimTileSize = MakeDimTileSize(I);
14985 if (!DimTileSize)
14986 return StmtError();
14987 ExprResult EndOfTile = SemaRef.BuildBinOp(
14988 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
14989 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
14990 RHSExpr: DimTileSize);
14991 if (!EndOfTile.isUsable())
14992 return StmtError();
14993 ExprResult IsPartialTile =
14994 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
14995 LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
14996 if (!IsPartialTile.isUsable())
14997 return StmtError();
14998 ExprResult MinTileAndIterSpace = SemaRef.ActOnConditionalOp(
14999 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
15000 CondExpr: IsPartialTile.get(), LHSExpr: NumIterations, RHSExpr: EndOfTile.get());
15001 if (!MinTileAndIterSpace.isUsable())
15002 return StmtError();
15003 ExprResult CondExpr =
15004 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15005 LHSExpr: MakeTileIVRef(), RHSExpr: MinTileAndIterSpace.get());
15006 if (!CondExpr.isUsable())
15007 return StmtError();
15008
15009 // For incr-statement: ++.tile.iv
15010 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15011 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeTileIVRef());
15012 if (!IncrStmt.isUsable())
15013 return StmtError();
15014
15015 // Statements to set the original iteration variable's value from the
15016 // logical iteration number.
15017 // Generated for loop is:
15018 // \code
15019 // Original_for_init;
15020 // for (auto .tile.iv = .floor.iv;
15021 // .tile.iv < min(.floor.iv + DimTileSize, NumIterations);
15022 // ++.tile.iv) {
15023 // Original_Body;
15024 // Original_counter_update;
15025 // }
15026 // \endcode
15027 // FIXME: If the innermost body is an loop itself, inserting these
15028 // statements stops it being recognized as a perfectly nested loop (e.g.
15029 // for applying tiling again). If this is the case, sink the expressions
15030 // further into the inner loop.
15031 SmallVector<Stmt *, 4> BodyParts;
15032 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15033 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15034 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
15035 BodyParts.push_back(Elt: Inner);
15036 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
15037 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
15038 Inner = new (Context)
15039 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15040 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15041 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15042 }
15043
15044 // Create floor loops from the inside to the outside.
15045 for (int I = NumLoops - 1; I >= 0; --I) {
15046 auto &LoopHelper = LoopHelpers[I];
15047 Expr *NumIterations = LoopHelper.NumIterations;
15048 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15049 QualType IVTy = NumIterations->getType();
15050
15051 // For init-statement: auto .floor.iv = 0
15052 SemaRef.AddInitializerToDecl(
15053 dcl: FloorIndVars[I],
15054 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15055 /*DirectInit=*/false);
15056 Decl *CounterDecl = FloorIndVars[I];
15057 StmtResult InitStmt = new (Context)
15058 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15059 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15060 if (!InitStmt.isUsable())
15061 return StmtError();
15062
15063 // For cond-expression: .floor.iv < NumIterations
15064 ExprResult CondExpr = SemaRef.BuildBinOp(
15065 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15066 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15067 RHSExpr: NumIterations);
15068 if (!CondExpr.isUsable())
15069 return StmtError();
15070
15071 // For incr-statement: .floor.iv += DimTileSize
15072 Expr *DimTileSize = MakeDimTileSize(I);
15073 if (!DimTileSize)
15074 return StmtError();
15075 ExprResult IncrStmt = SemaRef.BuildBinOp(
15076 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15077 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15078 RHSExpr: DimTileSize);
15079 if (!IncrStmt.isUsable())
15080 return StmtError();
15081
15082 Inner = new (Context)
15083 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15084 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15085 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15086 }
15087
15088 return OMPTileDirective::Create(C: Context, StartLoc, EndLoc, Clauses, NumLoops,
15089 AssociatedStmt: AStmt, TransformedStmt: Inner,
15090 PreInits: buildPreInits(Context, PreInits));
15091}
15092
15093StmtResult SemaOpenMP::ActOnOpenMPStripeDirective(ArrayRef<OMPClause *> Clauses,
15094 Stmt *AStmt,
15095 SourceLocation StartLoc,
15096 SourceLocation EndLoc) {
15097 ASTContext &Context = getASTContext();
15098 Scope *CurScope = SemaRef.getCurScope();
15099
15100 const auto *SizesClause =
15101 OMPExecutableDirective::getSingleClause<OMPSizesClause>(Clauses);
15102 if (!SizesClause ||
15103 llvm::any_of(Range: SizesClause->getSizesRefs(), P: [](const Expr *SizeExpr) {
15104 return !SizeExpr || SizeExpr->containsErrors();
15105 }))
15106 return StmtError();
15107 unsigned NumLoops = SizesClause->getNumSizes();
15108
15109 // Empty statement should only be possible if there already was an error.
15110 if (!AStmt)
15111 return StmtError();
15112
15113 // Verify and diagnose loop nest.
15114 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
15115 Stmt *Body = nullptr;
15116 SmallVector<SmallVector<Stmt *>, 4> OriginalInits;
15117 if (!checkTransformableLoopNest(Kind: OMPD_stripe, AStmt, NumLoops, LoopHelpers,
15118 Body, OriginalInits))
15119 return StmtError();
15120
15121 // Delay striping to when template is completely instantiated.
15122 if (SemaRef.CurContext->isDependentContext())
15123 return OMPStripeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15124 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
15125
15126 assert(LoopHelpers.size() == NumLoops &&
15127 "Expecting loop iteration space dimensionality to match number of "
15128 "affected loops");
15129 assert(OriginalInits.size() == NumLoops &&
15130 "Expecting loop iteration space dimensionality to match number of "
15131 "affected loops");
15132
15133 // Collect all affected loop statements.
15134 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
15135 collectLoopStmts(AStmt, LoopStmts);
15136
15137 SmallVector<Stmt *, 4> PreInits;
15138 CaptureVars CopyTransformer(SemaRef);
15139
15140 // Create iteration variables for the generated loops.
15141 SmallVector<VarDecl *, 4> FloorIndVars;
15142 SmallVector<VarDecl *, 4> StripeIndVars;
15143 FloorIndVars.resize(N: NumLoops);
15144 StripeIndVars.resize(N: NumLoops);
15145 for (unsigned I : llvm::seq<unsigned>(Size: NumLoops)) {
15146 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15147
15148 assert(LoopHelper.Counters.size() == 1 &&
15149 "Expect single-dimensional loop iteration space");
15150 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15151 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
15152 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15153 QualType CntTy = IterVarRef->getType();
15154
15155 // Iteration variable for the stripe (i.e. outer) loop.
15156 {
15157 std::string FloorCntName =
15158 (Twine(".floor_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
15159 VarDecl *FloorCntDecl =
15160 buildVarDecl(SemaRef, Loc: {}, Type: CntTy, Name: FloorCntName, Attrs: nullptr, OrigRef: OrigCntVar);
15161 FloorIndVars[I] = FloorCntDecl;
15162 }
15163
15164 // Iteration variable for the stripe (i.e. inner) loop.
15165 {
15166 std::string StripeCntName =
15167 (Twine(".stripe_") + llvm::utostr(X: I) + ".iv." + OrigVarName).str();
15168
15169 // Reuse the iteration variable created by checkOpenMPLoop. It is also
15170 // used by the expressions to derive the original iteration variable's
15171 // value from the logical iteration number.
15172 auto *StripeCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
15173 StripeCntDecl->setDeclName(
15174 &SemaRef.PP.getIdentifierTable().get(Name: StripeCntName));
15175 StripeIndVars[I] = StripeCntDecl;
15176 }
15177
15178 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
15179 PreInits);
15180 }
15181
15182 // Once the original iteration values are set, append the innermost body.
15183 Stmt *Inner = Body;
15184
15185 auto MakeDimStripeSize = [&](int I) -> Expr * {
15186 Expr *DimStripeSizeExpr = SizesClause->getSizesRefs()[I];
15187 if (isa<ConstantExpr>(Val: DimStripeSizeExpr))
15188 return AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr));
15189
15190 // When the stripe size is not a constant but a variable, it is possible to
15191 // pass non-positive numbers. For instance:
15192 // \code{c}
15193 // int a = 0;
15194 // #pragma omp stripe sizes(a)
15195 // for (int i = 0; i < 42; ++i)
15196 // body(i);
15197 // \endcode
15198 // Although there is no meaningful interpretation of the stripe size, the
15199 // body should still be executed 42 times to avoid surprises. To preserve
15200 // the invariant that every loop iteration is executed exactly once and not
15201 // cause an infinite loop, apply a minimum stripe size of one.
15202 // Build expr:
15203 // \code{c}
15204 // (TS <= 0) ? 1 : TS
15205 // \endcode
15206 QualType DimTy = DimStripeSizeExpr->getType();
15207 uint64_t DimWidth = Context.getTypeSize(T: DimTy);
15208 IntegerLiteral *Zero = IntegerLiteral::Create(
15209 C: Context, V: llvm::APInt::getZero(numBits: DimWidth), type: DimTy, l: {});
15210 IntegerLiteral *One =
15211 IntegerLiteral::Create(C: Context, V: llvm::APInt(DimWidth, 1), type: DimTy, l: {});
15212 Expr *Cond = AssertSuccess(R: SemaRef.BuildBinOp(
15213 S: CurScope, OpLoc: {}, Opc: BO_LE,
15214 LHSExpr: AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), RHSExpr: Zero));
15215 Expr *MinOne = new (Context) ConditionalOperator(
15216 Cond, {}, One, {},
15217 AssertSuccess(R: CopyTransformer.TransformExpr(E: DimStripeSizeExpr)), DimTy,
15218 VK_PRValue, OK_Ordinary);
15219 return MinOne;
15220 };
15221
15222 // Create stripe loops from the inside to the outside.
15223 for (int I = NumLoops - 1; I >= 0; --I) {
15224 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15225 Expr *NumIterations = LoopHelper.NumIterations;
15226 auto *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15227 QualType IVTy = NumIterations->getType();
15228 Stmt *LoopStmt = LoopStmts[I];
15229
15230 // For init-statement: auto .stripe.iv = .floor.iv
15231 SemaRef.AddInitializerToDecl(
15232 dcl: StripeIndVars[I],
15233 init: SemaRef
15234 .DefaultLvalueConversion(
15235 E: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar))
15236 .get(),
15237 /*DirectInit=*/false);
15238 Decl *CounterDecl = StripeIndVars[I];
15239 StmtResult InitStmt = new (Context)
15240 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15241 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15242 if (!InitStmt.isUsable())
15243 return StmtError();
15244
15245 // For cond-expression:
15246 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations)
15247 ExprResult EndOfStripe = SemaRef.BuildBinOp(
15248 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15249 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15250 RHSExpr: MakeDimStripeSize(I));
15251 if (!EndOfStripe.isUsable())
15252 return StmtError();
15253 ExprResult IsPartialStripe =
15254 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15255 LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
15256 if (!IsPartialStripe.isUsable())
15257 return StmtError();
15258 ExprResult MinStripeAndIterSpace = SemaRef.ActOnConditionalOp(
15259 QuestionLoc: LoopHelper.Cond->getBeginLoc(), ColonLoc: LoopHelper.Cond->getEndLoc(),
15260 CondExpr: IsPartialStripe.get(), LHSExpr: NumIterations, RHSExpr: EndOfStripe.get());
15261 if (!MinStripeAndIterSpace.isUsable())
15262 return StmtError();
15263 ExprResult CondExpr = SemaRef.BuildBinOp(
15264 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15265 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar),
15266 RHSExpr: MinStripeAndIterSpace.get());
15267 if (!CondExpr.isUsable())
15268 return StmtError();
15269
15270 // For incr-statement: ++.stripe.iv
15271 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15272 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc,
15273 Input: makeFloorIVRef(SemaRef, FloorIndVars: StripeIndVars, I, IVTy, OrigCntVar));
15274 if (!IncrStmt.isUsable())
15275 return StmtError();
15276
15277 // Statements to set the original iteration variable's value from the
15278 // logical iteration number.
15279 // Generated for loop is:
15280 // \code
15281 // Original_for_init;
15282 // for (auto .stripe.iv = .floor.iv;
15283 // .stripe.iv < min(.floor.iv + DimStripeSize, NumIterations);
15284 // ++.stripe.iv) {
15285 // Original_Body;
15286 // Original_counter_update;
15287 // }
15288 // \endcode
15289 // FIXME: If the innermost body is a loop itself, inserting these
15290 // statements stops it being recognized as a perfectly nested loop (e.g.
15291 // for applying another loop transformation). If this is the case, sink the
15292 // expressions further into the inner loop.
15293 SmallVector<Stmt *, 4> BodyParts;
15294 BodyParts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15295 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15296 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
15297 BodyParts.push_back(Elt: Inner);
15298 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
15299 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
15300 Inner = new (Context)
15301 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15302 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15303 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15304 }
15305
15306 // Create grid loops from the inside to the outside.
15307 for (int I = NumLoops - 1; I >= 0; --I) {
15308 auto &LoopHelper = LoopHelpers[I];
15309 Expr *NumIterations = LoopHelper.NumIterations;
15310 DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(Val: LoopHelper.Counters[0]);
15311 QualType IVTy = NumIterations->getType();
15312
15313 // For init-statement: auto .grid.iv = 0
15314 SemaRef.AddInitializerToDecl(
15315 dcl: FloorIndVars[I],
15316 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15317 /*DirectInit=*/false);
15318 Decl *CounterDecl = FloorIndVars[I];
15319 StmtResult InitStmt = new (Context)
15320 DeclStmt(DeclGroupRef::Create(C&: Context, Decls: &CounterDecl, NumDecls: 1),
15321 OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15322 if (!InitStmt.isUsable())
15323 return StmtError();
15324
15325 // For cond-expression: .floor.iv < NumIterations
15326 ExprResult CondExpr = SemaRef.BuildBinOp(
15327 S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15328 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15329 RHSExpr: NumIterations);
15330 if (!CondExpr.isUsable())
15331 return StmtError();
15332
15333 // For incr-statement: .floor.iv += DimStripeSize
15334 ExprResult IncrStmt = SemaRef.BuildBinOp(
15335 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15336 LHSExpr: makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
15337 RHSExpr: MakeDimStripeSize(I));
15338 if (!IncrStmt.isUsable())
15339 return StmtError();
15340
15341 Inner = new (Context)
15342 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr,
15343 IncrStmt.get(), Inner, LoopHelper.Init->getBeginLoc(),
15344 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15345 }
15346
15347 return OMPStripeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15348 NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
15349 PreInits: buildPreInits(Context, PreInits));
15350}
15351
15352StmtResult SemaOpenMP::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses,
15353 Stmt *AStmt,
15354 SourceLocation StartLoc,
15355 SourceLocation EndLoc) {
15356 ASTContext &Context = getASTContext();
15357 Scope *CurScope = SemaRef.getCurScope();
15358 // Empty statement should only be possible if there already was an error.
15359 if (!AStmt)
15360 return StmtError();
15361
15362 if (checkMutuallyExclusiveClauses(S&: SemaRef, Clauses,
15363 MutuallyExclusiveClauses: {OMPC_partial, OMPC_full}))
15364 return StmtError();
15365
15366 const OMPFullClause *FullClause =
15367 OMPExecutableDirective::getSingleClause<OMPFullClause>(Clauses);
15368 const OMPPartialClause *PartialClause =
15369 OMPExecutableDirective::getSingleClause<OMPPartialClause>(Clauses);
15370 assert(!(FullClause && PartialClause) &&
15371 "mutual exclusivity must have been checked before");
15372
15373 constexpr unsigned NumLoops = 1;
15374 Stmt *Body = nullptr;
15375 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
15376 NumLoops);
15377 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
15378 if (!checkTransformableLoopNest(Kind: OMPD_unroll, AStmt, NumLoops, LoopHelpers,
15379 Body, OriginalInits))
15380 return StmtError();
15381
15382 unsigned NumGeneratedTopLevelLoops = PartialClause ? 1 : 0;
15383
15384 // Delay unrolling to when template is completely instantiated.
15385 if (SemaRef.CurContext->isDependentContext())
15386 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15387 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
15388 PreInits: nullptr);
15389
15390 assert(LoopHelpers.size() == NumLoops &&
15391 "Expecting a single-dimensional loop iteration space");
15392 assert(OriginalInits.size() == NumLoops &&
15393 "Expecting a single-dimensional loop iteration space");
15394 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
15395
15396 if (FullClause) {
15397 if (!VerifyPositiveIntegerConstantInClause(
15398 Op: LoopHelper.NumIterations, CKind: OMPC_full, /*StrictlyPositive=*/false,
15399 /*SuppressExprDiags=*/true)
15400 .isUsable()) {
15401 Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::err_omp_unroll_full_variable_trip_count);
15402 Diag(Loc: FullClause->getBeginLoc(), DiagID: diag::note_omp_directive_here)
15403 << "#pragma omp unroll full";
15404 return StmtError();
15405 }
15406 }
15407
15408 // The generated loop may only be passed to other loop-associated directive
15409 // when a partial clause is specified. Without the requirement it is
15410 // sufficient to generate loop unroll metadata at code-generation.
15411 if (NumGeneratedTopLevelLoops == 0)
15412 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15413 NumGeneratedTopLevelLoops, TransformedStmt: nullptr,
15414 PreInits: nullptr);
15415
15416 // Otherwise, we need to provide a de-sugared/transformed AST that can be
15417 // associated with another loop directive.
15418 //
15419 // The canonical loop analysis return by checkTransformableLoopNest assumes
15420 // the following structure to be the same loop without transformations or
15421 // directives applied: \code OriginalInits; LoopHelper.PreInits;
15422 // LoopHelper.Counters;
15423 // for (; IV < LoopHelper.NumIterations; ++IV) {
15424 // LoopHelper.Updates;
15425 // Body;
15426 // }
15427 // \endcode
15428 // where IV is a variable declared and initialized to 0 in LoopHelper.PreInits
15429 // and referenced by LoopHelper.IterationVarRef.
15430 //
15431 // The unrolling directive transforms this into the following loop:
15432 // \code
15433 // OriginalInits; \
15434 // LoopHelper.PreInits; > NewPreInits
15435 // LoopHelper.Counters; /
15436 // for (auto UIV = 0; UIV < LoopHelper.NumIterations; UIV+=Factor) {
15437 // #pragma clang loop unroll_count(Factor)
15438 // for (IV = UIV; IV < UIV + Factor && UIV < LoopHelper.NumIterations; ++IV)
15439 // {
15440 // LoopHelper.Updates;
15441 // Body;
15442 // }
15443 // }
15444 // \endcode
15445 // where UIV is a new logical iteration counter. IV must be the same VarDecl
15446 // as the original LoopHelper.IterationVarRef because LoopHelper.Updates
15447 // references it. If the partially unrolled loop is associated with another
15448 // loop directive (like an OMPForDirective), it will use checkOpenMPLoop to
15449 // analyze this loop, i.e. the outer loop must fulfill the constraints of an
15450 // OpenMP canonical loop. The inner loop is not an associable canonical loop
15451 // and only exists to defer its unrolling to LLVM's LoopUnroll instead of
15452 // doing it in the frontend (by adding loop metadata). NewPreInits becomes a
15453 // property of the OMPLoopBasedDirective instead of statements in
15454 // CompoundStatement. This is to allow the loop to become a non-outermost loop
15455 // of a canonical loop nest where these PreInits are emitted before the
15456 // outermost directive.
15457
15458 // Find the loop statement.
15459 Stmt *LoopStmt = nullptr;
15460 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
15461
15462 // Determine the PreInit declarations.
15463 SmallVector<Stmt *, 4> PreInits;
15464 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
15465
15466 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15467 QualType IVTy = IterationVarRef->getType();
15468 assert(LoopHelper.Counters.size() == 1 &&
15469 "Expecting a single-dimensional loop iteration space");
15470 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15471
15472 // Determine the unroll factor.
15473 uint64_t Factor;
15474 SourceLocation FactorLoc;
15475 if (Expr *FactorVal = PartialClause->getFactor();
15476 FactorVal && !FactorVal->containsErrors()) {
15477 Factor = FactorVal->getIntegerConstantExpr(Ctx: Context)->getZExtValue();
15478 FactorLoc = FactorVal->getExprLoc();
15479 } else {
15480 // TODO: Use a better profitability model.
15481 Factor = 2;
15482 }
15483 assert(Factor > 0 && "Expected positive unroll factor");
15484 auto MakeFactorExpr = [this, Factor, IVTy, FactorLoc]() {
15485 return IntegerLiteral::Create(
15486 C: getASTContext(), V: llvm::APInt(getASTContext().getIntWidth(T: IVTy), Factor),
15487 type: IVTy, l: FactorLoc);
15488 };
15489
15490 // Iteration variable SourceLocations.
15491 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
15492 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
15493 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
15494
15495 // Internal variable names.
15496 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
15497 std::string OuterIVName = (Twine(".unrolled.iv.") + OrigVarName).str();
15498 std::string InnerIVName = (Twine(".unroll_inner.iv.") + OrigVarName).str();
15499
15500 // Create the iteration variable for the unrolled loop.
15501 VarDecl *OuterIVDecl =
15502 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: OuterIVName, Attrs: nullptr, OrigRef: OrigVar);
15503 auto MakeOuterRef = [this, OuterIVDecl, IVTy, OrigVarLoc]() {
15504 return buildDeclRefExpr(S&: SemaRef, D: OuterIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15505 };
15506
15507 // Iteration variable for the inner loop: Reuse the iteration variable created
15508 // by checkOpenMPLoop.
15509 auto *InnerIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
15510 InnerIVDecl->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name: InnerIVName));
15511 auto MakeInnerRef = [this, InnerIVDecl, IVTy, OrigVarLoc]() {
15512 return buildDeclRefExpr(S&: SemaRef, D: InnerIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15513 };
15514
15515 // Make a copy of the NumIterations expression for each use: By the AST
15516 // constraints, every expression object in a DeclContext must be unique.
15517 CaptureVars CopyTransformer(SemaRef);
15518 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
15519 return AssertSuccess(
15520 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
15521 };
15522
15523 // Inner For init-statement: auto .unroll_inner.iv = .unrolled.iv
15524 ExprResult LValueConv = SemaRef.DefaultLvalueConversion(E: MakeOuterRef());
15525 SemaRef.AddInitializerToDecl(dcl: InnerIVDecl, init: LValueConv.get(),
15526 /*DirectInit=*/false);
15527 StmtResult InnerInit = new (Context)
15528 DeclStmt(DeclGroupRef(InnerIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15529 if (!InnerInit.isUsable())
15530 return StmtError();
15531
15532 // Inner For cond-expression:
15533 // \code
15534 // .unroll_inner.iv < .unrolled.iv + Factor &&
15535 // .unroll_inner.iv < NumIterations
15536 // \endcode
15537 // This conjunction of two conditions allows ScalarEvolution to derive the
15538 // maximum trip count of the inner loop.
15539 ExprResult EndOfTile =
15540 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_Add,
15541 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
15542 if (!EndOfTile.isUsable())
15543 return StmtError();
15544 ExprResult InnerCond1 =
15545 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15546 LHSExpr: MakeInnerRef(), RHSExpr: EndOfTile.get());
15547 if (!InnerCond1.isUsable())
15548 return StmtError();
15549 ExprResult InnerCond2 =
15550 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15551 LHSExpr: MakeInnerRef(), RHSExpr: MakeNumIterations());
15552 if (!InnerCond2.isUsable())
15553 return StmtError();
15554 ExprResult InnerCond =
15555 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LAnd,
15556 LHSExpr: InnerCond1.get(), RHSExpr: InnerCond2.get());
15557 if (!InnerCond.isUsable())
15558 return StmtError();
15559
15560 // Inner For incr-statement: ++.unroll_inner.iv
15561 ExprResult InnerIncr = SemaRef.BuildUnaryOp(
15562 S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakeInnerRef());
15563 if (!InnerIncr.isUsable())
15564 return StmtError();
15565
15566 // Inner For statement.
15567 SmallVector<Stmt *> InnerBodyStmts;
15568 InnerBodyStmts.append(in_start: LoopHelper.Updates.begin(), in_end: LoopHelper.Updates.end());
15569 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15570 InnerBodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
15571 InnerBodyStmts.push_back(Elt: Body);
15572 CompoundStmt *InnerBody =
15573 CompoundStmt::Create(C: getASTContext(), Stmts: InnerBodyStmts, FPFeatures: FPOptionsOverride(),
15574 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
15575 ForStmt *InnerFor = new (Context)
15576 ForStmt(Context, InnerInit.get(), InnerCond.get(), nullptr,
15577 InnerIncr.get(), InnerBody, LoopHelper.Init->getBeginLoc(),
15578 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15579
15580 // Unroll metadata for the inner loop.
15581 // This needs to take into account the remainder portion of the unrolled loop,
15582 // hence `unroll(full)` does not apply here, even though the LoopUnroll pass
15583 // supports multiple loop exits. Instead, unroll using a factor equivalent to
15584 // the maximum trip count, which will also generate a remainder loop. Just
15585 // `unroll(enable)` (which could have been useful if the user has not
15586 // specified a concrete factor; even though the outer loop cannot be
15587 // influenced anymore, would avoid more code bloat than necessary) will refuse
15588 // the loop because "Won't unroll; remainder loop could not be generated when
15589 // assuming runtime trip count". Even if it did work, it must not choose a
15590 // larger unroll factor than the maximum loop length, or it would always just
15591 // execute the remainder loop.
15592 LoopHintAttr *UnrollHintAttr =
15593 LoopHintAttr::CreateImplicit(Ctx&: Context, Option: LoopHintAttr::UnrollCount,
15594 State: LoopHintAttr::Numeric, Value: MakeFactorExpr());
15595 AttributedStmt *InnerUnrolled = AttributedStmt::Create(
15596 C: getASTContext(), Loc: StartLoc, Attrs: {UnrollHintAttr}, SubStmt: InnerFor);
15597
15598 // Outer For init-statement: auto .unrolled.iv = 0
15599 SemaRef.AddInitializerToDecl(
15600 dcl: OuterIVDecl,
15601 init: SemaRef.ActOnIntegerConstant(Loc: LoopHelper.Init->getExprLoc(), Val: 0).get(),
15602 /*DirectInit=*/false);
15603 StmtResult OuterInit = new (Context)
15604 DeclStmt(DeclGroupRef(OuterIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15605 if (!OuterInit.isUsable())
15606 return StmtError();
15607
15608 // Outer For cond-expression: .unrolled.iv < NumIterations
15609 ExprResult OuterConde =
15610 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15611 LHSExpr: MakeOuterRef(), RHSExpr: MakeNumIterations());
15612 if (!OuterConde.isUsable())
15613 return StmtError();
15614
15615 // Outer For incr-statement: .unrolled.iv += Factor
15616 ExprResult OuterIncr =
15617 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(), Opc: BO_AddAssign,
15618 LHSExpr: MakeOuterRef(), RHSExpr: MakeFactorExpr());
15619 if (!OuterIncr.isUsable())
15620 return StmtError();
15621
15622 // Outer For statement.
15623 ForStmt *OuterFor = new (Context)
15624 ForStmt(Context, OuterInit.get(), OuterConde.get(), nullptr,
15625 OuterIncr.get(), InnerUnrolled, LoopHelper.Init->getBeginLoc(),
15626 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15627
15628 return OMPUnrollDirective::Create(C: Context, StartLoc, EndLoc, Clauses, AssociatedStmt: AStmt,
15629 NumGeneratedTopLevelLoops, TransformedStmt: OuterFor,
15630 PreInits: buildPreInits(Context, PreInits));
15631}
15632
15633StmtResult SemaOpenMP::ActOnOpenMPReverseDirective(Stmt *AStmt,
15634 SourceLocation StartLoc,
15635 SourceLocation EndLoc) {
15636 ASTContext &Context = getASTContext();
15637 Scope *CurScope = SemaRef.getCurScope();
15638
15639 // Empty statement should only be possible if there already was an error.
15640 if (!AStmt)
15641 return StmtError();
15642
15643 constexpr unsigned NumLoops = 1;
15644 Stmt *Body = nullptr;
15645 SmallVector<OMPLoopBasedDirective::HelperExprs, NumLoops> LoopHelpers(
15646 NumLoops);
15647 SmallVector<SmallVector<Stmt *>, NumLoops + 1> OriginalInits;
15648 if (!checkTransformableLoopNest(Kind: OMPD_reverse, AStmt, NumLoops, LoopHelpers,
15649 Body, OriginalInits))
15650 return StmtError();
15651
15652 // Delay applying the transformation to when template is completely
15653 // instantiated.
15654 if (SemaRef.CurContext->isDependentContext())
15655 return OMPReverseDirective::Create(C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt,
15656 NumLoops, TransformedStmt: nullptr, PreInits: nullptr);
15657
15658 assert(LoopHelpers.size() == NumLoops &&
15659 "Expecting a single-dimensional loop iteration space");
15660 assert(OriginalInits.size() == NumLoops &&
15661 "Expecting a single-dimensional loop iteration space");
15662 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers.front();
15663
15664 // Find the loop statement.
15665 Stmt *LoopStmt = nullptr;
15666 collectLoopStmts(AStmt, LoopStmts: {LoopStmt});
15667
15668 // Determine the PreInit declarations.
15669 SmallVector<Stmt *> PreInits;
15670 addLoopPreInits(Context, LoopHelper, LoopStmt, OriginalInit: OriginalInits[0], PreInits);
15671
15672 auto *IterationVarRef = cast<DeclRefExpr>(Val: LoopHelper.IterationVarRef);
15673 QualType IVTy = IterationVarRef->getType();
15674 uint64_t IVWidth = Context.getTypeSize(T: IVTy);
15675 auto *OrigVar = cast<DeclRefExpr>(Val: LoopHelper.Counters.front());
15676
15677 // Iteration variable SourceLocations.
15678 SourceLocation OrigVarLoc = OrigVar->getExprLoc();
15679 SourceLocation OrigVarLocBegin = OrigVar->getBeginLoc();
15680 SourceLocation OrigVarLocEnd = OrigVar->getEndLoc();
15681
15682 // Locations pointing to the transformation.
15683 SourceLocation TransformLoc = StartLoc;
15684 SourceLocation TransformLocBegin = StartLoc;
15685 SourceLocation TransformLocEnd = EndLoc;
15686
15687 // Internal variable names.
15688 std::string OrigVarName = OrigVar->getNameInfo().getAsString();
15689 SmallString<64> ForwardIVName(".forward.iv.");
15690 ForwardIVName += OrigVarName;
15691 SmallString<64> ReversedIVName(".reversed.iv.");
15692 ReversedIVName += OrigVarName;
15693
15694 // LoopHelper.Updates will read the logical iteration number from
15695 // LoopHelper.IterationVarRef, compute the value of the user loop counter of
15696 // that logical iteration from it, then assign it to the user loop counter
15697 // variable. We cannot directly use LoopHelper.IterationVarRef as the
15698 // induction variable of the generated loop because it may cause an underflow:
15699 // \code{.c}
15700 // for (unsigned i = 0; i < n; ++i)
15701 // body(i);
15702 // \endcode
15703 //
15704 // Naive reversal:
15705 // \code{.c}
15706 // for (unsigned i = n-1; i >= 0; --i)
15707 // body(i);
15708 // \endcode
15709 //
15710 // Instead, we introduce a new iteration variable representing the logical
15711 // iteration counter of the original loop, convert it to the logical iteration
15712 // number of the reversed loop, then let LoopHelper.Updates compute the user's
15713 // loop iteration variable from it.
15714 // \code{.cpp}
15715 // for (auto .forward.iv = 0; .forward.iv < n; ++.forward.iv) {
15716 // auto .reversed.iv = n - .forward.iv - 1;
15717 // i = (.reversed.iv + 0) * 1; // LoopHelper.Updates
15718 // body(i); // Body
15719 // }
15720 // \endcode
15721
15722 // Subexpressions with more than one use. One of the constraints of an AST is
15723 // that every node object must appear at most once, hence we define a lambda
15724 // that creates a new AST node at every use.
15725 CaptureVars CopyTransformer(SemaRef);
15726 auto MakeNumIterations = [&CopyTransformer, &LoopHelper]() -> Expr * {
15727 return AssertSuccess(
15728 R: CopyTransformer.TransformExpr(E: LoopHelper.NumIterations));
15729 };
15730
15731 // Create the iteration variable for the forward loop (from 0 to n-1).
15732 VarDecl *ForwardIVDecl =
15733 buildVarDecl(SemaRef, Loc: {}, Type: IVTy, Name: ForwardIVName, Attrs: nullptr, OrigRef: OrigVar);
15734 auto MakeForwardRef = [&SemaRef = this->SemaRef, ForwardIVDecl, IVTy,
15735 OrigVarLoc]() {
15736 return buildDeclRefExpr(S&: SemaRef, D: ForwardIVDecl, Ty: IVTy, Loc: OrigVarLoc);
15737 };
15738
15739 // Iteration variable for the reversed induction variable (from n-1 downto 0):
15740 // Reuse the iteration variable created by checkOpenMPLoop.
15741 auto *ReversedIVDecl = cast<VarDecl>(Val: IterationVarRef->getDecl());
15742 ReversedIVDecl->setDeclName(
15743 &SemaRef.PP.getIdentifierTable().get(Name: ReversedIVName));
15744
15745 // For init-statement:
15746 // \code{.cpp}
15747 // auto .forward.iv = 0;
15748 // \endcode
15749 auto *Zero = IntegerLiteral::Create(C: Context, V: llvm::APInt::getZero(numBits: IVWidth),
15750 type: ForwardIVDecl->getType(), l: OrigVarLoc);
15751 SemaRef.AddInitializerToDecl(dcl: ForwardIVDecl, init: Zero, /*DirectInit=*/false);
15752 StmtResult Init = new (Context)
15753 DeclStmt(DeclGroupRef(ForwardIVDecl), OrigVarLocBegin, OrigVarLocEnd);
15754 if (!Init.isUsable())
15755 return StmtError();
15756
15757 // Forward iv cond-expression:
15758 // \code{.cpp}
15759 // .forward.iv < MakeNumIterations()
15760 // \endcode
15761 ExprResult Cond =
15762 SemaRef.BuildBinOp(S: CurScope, OpLoc: LoopHelper.Cond->getExprLoc(), Opc: BO_LT,
15763 LHSExpr: MakeForwardRef(), RHSExpr: MakeNumIterations());
15764 if (!Cond.isUsable())
15765 return StmtError();
15766
15767 // Forward incr-statement:
15768 // \code{.c}
15769 // ++.forward.iv
15770 // \endcode
15771 ExprResult Incr = SemaRef.BuildUnaryOp(S: CurScope, OpLoc: LoopHelper.Inc->getExprLoc(),
15772 Opc: UO_PreInc, Input: MakeForwardRef());
15773 if (!Incr.isUsable())
15774 return StmtError();
15775
15776 // Reverse the forward-iv:
15777 // \code{.cpp}
15778 // auto .reversed.iv = MakeNumIterations() - 1 - .forward.iv
15779 // \endcode
15780 auto *One = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVWidth, 1), type: IVTy,
15781 l: TransformLoc);
15782 ExprResult Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub,
15783 LHSExpr: MakeNumIterations(), RHSExpr: One);
15784 if (!Minus.isUsable())
15785 return StmtError();
15786 Minus = SemaRef.BuildBinOp(S: CurScope, OpLoc: TransformLoc, Opc: BO_Sub, LHSExpr: Minus.get(),
15787 RHSExpr: MakeForwardRef());
15788 if (!Minus.isUsable())
15789 return StmtError();
15790 StmtResult InitReversed = new (Context) DeclStmt(
15791 DeclGroupRef(ReversedIVDecl), TransformLocBegin, TransformLocEnd);
15792 if (!InitReversed.isUsable())
15793 return StmtError();
15794 SemaRef.AddInitializerToDecl(dcl: ReversedIVDecl, init: Minus.get(),
15795 /*DirectInit=*/false);
15796
15797 // The new loop body.
15798 SmallVector<Stmt *, 4> BodyStmts;
15799 BodyStmts.reserve(N: LoopHelper.Updates.size() + 2 +
15800 (isa<CXXForRangeStmt>(Val: LoopStmt) ? 1 : 0));
15801 BodyStmts.push_back(Elt: InitReversed.get());
15802 llvm::append_range(C&: BodyStmts, R&: LoopHelper.Updates);
15803 if (auto *CXXRangeFor = dyn_cast<CXXForRangeStmt>(Val: LoopStmt))
15804 BodyStmts.push_back(Elt: CXXRangeFor->getLoopVarStmt());
15805 BodyStmts.push_back(Elt: Body);
15806 auto *ReversedBody =
15807 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
15808 LB: Body->getBeginLoc(), RB: Body->getEndLoc());
15809
15810 // Finally create the reversed For-statement.
15811 auto *ReversedFor = new (Context)
15812 ForStmt(Context, Init.get(), Cond.get(), nullptr, Incr.get(),
15813 ReversedBody, LoopHelper.Init->getBeginLoc(),
15814 LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
15815 return OMPReverseDirective::Create(C: Context, StartLoc, EndLoc, AssociatedStmt: AStmt, NumLoops,
15816 TransformedStmt: ReversedFor,
15817 PreInits: buildPreInits(Context, PreInits));
15818}
15819
15820StmtResult SemaOpenMP::ActOnOpenMPInterchangeDirective(
15821 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
15822 SourceLocation EndLoc) {
15823 ASTContext &Context = getASTContext();
15824 DeclContext *CurContext = SemaRef.CurContext;
15825 Scope *CurScope = SemaRef.getCurScope();
15826
15827 // Empty statement should only be possible if there already was an error.
15828 if (!AStmt)
15829 return StmtError();
15830
15831 // interchange without permutation clause swaps two loops.
15832 const OMPPermutationClause *PermutationClause =
15833 OMPExecutableDirective::getSingleClause<OMPPermutationClause>(Clauses);
15834 size_t NumLoops = PermutationClause ? PermutationClause->getNumLoops() : 2;
15835
15836 // Verify and diagnose loop nest.
15837 SmallVector<OMPLoopBasedDirective::HelperExprs, 4> LoopHelpers(NumLoops);
15838 Stmt *Body = nullptr;
15839 SmallVector<SmallVector<Stmt *>, 2> OriginalInits;
15840 if (!checkTransformableLoopNest(Kind: OMPD_interchange, AStmt, NumLoops,
15841 LoopHelpers, Body, OriginalInits))
15842 return StmtError();
15843
15844 // Delay interchange to when template is completely instantiated.
15845 if (CurContext->isDependentContext())
15846 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15847 NumLoops, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
15848
15849 // An invalid expression in the permutation clause is set to nullptr in
15850 // ActOnOpenMPPermutationClause.
15851 if (PermutationClause &&
15852 llvm::is_contained(Range: PermutationClause->getArgsRefs(), Element: nullptr))
15853 return StmtError();
15854
15855 assert(LoopHelpers.size() == NumLoops &&
15856 "Expecting loop iteration space dimensionaly to match number of "
15857 "affected loops");
15858 assert(OriginalInits.size() == NumLoops &&
15859 "Expecting loop iteration space dimensionaly to match number of "
15860 "affected loops");
15861
15862 // Decode the permutation clause.
15863 SmallVector<uint64_t, 2> Permutation;
15864 if (!PermutationClause) {
15865 Permutation = {1, 0};
15866 } else {
15867 ArrayRef<Expr *> PermArgs = PermutationClause->getArgsRefs();
15868 llvm::BitVector Flags(PermArgs.size());
15869 for (Expr *PermArg : PermArgs) {
15870 std::optional<llvm::APSInt> PermCstExpr =
15871 PermArg->getIntegerConstantExpr(Ctx: Context);
15872 if (!PermCstExpr)
15873 continue;
15874 uint64_t PermInt = PermCstExpr->getZExtValue();
15875 assert(1 <= PermInt && PermInt <= NumLoops &&
15876 "Must be a permutation; diagnostic emitted in "
15877 "ActOnOpenMPPermutationClause");
15878 if (Flags[PermInt - 1]) {
15879 SourceRange ExprRange(PermArg->getBeginLoc(), PermArg->getEndLoc());
15880 Diag(Loc: PermArg->getExprLoc(),
15881 DiagID: diag::err_omp_interchange_permutation_value_repeated)
15882 << PermInt << ExprRange;
15883 continue;
15884 }
15885 Flags[PermInt - 1] = true;
15886
15887 Permutation.push_back(Elt: PermInt - 1);
15888 }
15889
15890 if (Permutation.size() != NumLoops)
15891 return StmtError();
15892 }
15893
15894 // Nothing to transform with trivial permutation.
15895 if (NumLoops <= 1 || llvm::all_of(Range: llvm::enumerate(First&: Permutation), P: [](auto P) {
15896 auto [Idx, Arg] = P;
15897 return Idx == Arg;
15898 }))
15899 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
15900 NumLoops, AssociatedStmt: AStmt, TransformedStmt: AStmt, PreInits: nullptr);
15901
15902 // Find the affected loops.
15903 SmallVector<Stmt *> LoopStmts(NumLoops, nullptr);
15904 collectLoopStmts(AStmt, LoopStmts);
15905
15906 // Collect pre-init statements on the order before the permuation.
15907 SmallVector<Stmt *> PreInits;
15908 for (auto I : llvm::seq<int>(Size: NumLoops)) {
15909 OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
15910
15911 assert(LoopHelper.Counters.size() == 1 &&
15912 "Single-dimensional loop iteration space expected");
15913
15914 addLoopPreInits(Context, LoopHelper, LoopStmt: LoopStmts[I], OriginalInit: OriginalInits[I],
15915 PreInits);
15916 }
15917
15918 SmallVector<VarDecl *> PermutedIndVars(NumLoops);
15919 CaptureVars CopyTransformer(SemaRef);
15920
15921 // Create the permuted loops from the inside to the outside of the
15922 // interchanged loop nest. Body of the innermost new loop is the original
15923 // innermost body.
15924 Stmt *Inner = Body;
15925 for (auto TargetIdx : llvm::reverse(C: llvm::seq<int>(Size: NumLoops))) {
15926 // Get the original loop that belongs to this new position.
15927 uint64_t SourceIdx = Permutation[TargetIdx];
15928 OMPLoopBasedDirective::HelperExprs &SourceHelper = LoopHelpers[SourceIdx];
15929 Stmt *SourceLoopStmt = LoopStmts[SourceIdx];
15930 assert(SourceHelper.Counters.size() == 1 &&
15931 "Single-dimensional loop iteration space expected");
15932 auto *OrigCntVar = cast<DeclRefExpr>(Val: SourceHelper.Counters.front());
15933
15934 // Normalized loop counter variable: From 0 to n-1, always an integer type.
15935 DeclRefExpr *IterVarRef = cast<DeclRefExpr>(Val: SourceHelper.IterationVarRef);
15936 QualType IVTy = IterVarRef->getType();
15937 assert(IVTy->isIntegerType() &&
15938 "Expected the logical iteration counter to be an integer");
15939
15940 std::string OrigVarName = OrigCntVar->getNameInfo().getAsString();
15941 SourceLocation OrigVarLoc = IterVarRef->getExprLoc();
15942
15943 // Make a copy of the NumIterations expression for each use: By the AST
15944 // constraints, every expression object in a DeclContext must be unique.
15945 auto MakeNumIterations = [&CopyTransformer, &SourceHelper]() -> Expr * {
15946 return AssertSuccess(
15947 R: CopyTransformer.TransformExpr(E: SourceHelper.NumIterations));
15948 };
15949
15950 // Iteration variable for the permuted loop. Reuse the one from
15951 // checkOpenMPLoop which will also be used to update the original loop
15952 // variable.
15953 SmallString<64> PermutedCntName(".permuted_");
15954 PermutedCntName.append(Refs: {llvm::utostr(X: TargetIdx), ".iv.", OrigVarName});
15955 auto *PermutedCntDecl = cast<VarDecl>(Val: IterVarRef->getDecl());
15956 PermutedCntDecl->setDeclName(
15957 &SemaRef.PP.getIdentifierTable().get(Name: PermutedCntName));
15958 PermutedIndVars[TargetIdx] = PermutedCntDecl;
15959 auto MakePermutedRef = [this, PermutedCntDecl, IVTy, OrigVarLoc]() {
15960 return buildDeclRefExpr(S&: SemaRef, D: PermutedCntDecl, Ty: IVTy, Loc: OrigVarLoc);
15961 };
15962
15963 // For init-statement:
15964 // \code
15965 // auto .permuted_{target}.iv = 0
15966 // \endcode
15967 ExprResult Zero = SemaRef.ActOnIntegerConstant(Loc: OrigVarLoc, Val: 0);
15968 if (!Zero.isUsable())
15969 return StmtError();
15970 SemaRef.AddInitializerToDecl(dcl: PermutedCntDecl, init: Zero.get(),
15971 /*DirectInit=*/false);
15972 StmtResult InitStmt = new (Context)
15973 DeclStmt(DeclGroupRef(PermutedCntDecl), OrigCntVar->getBeginLoc(),
15974 OrigCntVar->getEndLoc());
15975 if (!InitStmt.isUsable())
15976 return StmtError();
15977
15978 // For cond-expression:
15979 // \code
15980 // .permuted_{target}.iv < MakeNumIterations()
15981 // \endcode
15982 ExprResult CondExpr =
15983 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceHelper.Cond->getExprLoc(), Opc: BO_LT,
15984 LHSExpr: MakePermutedRef(), RHSExpr: MakeNumIterations());
15985 if (!CondExpr.isUsable())
15986 return StmtError();
15987
15988 // For incr-statement:
15989 // \code
15990 // ++.tile.iv
15991 // \endcode
15992 ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15993 S: CurScope, OpLoc: SourceHelper.Inc->getExprLoc(), Opc: UO_PreInc, Input: MakePermutedRef());
15994 if (!IncrStmt.isUsable())
15995 return StmtError();
15996
15997 SmallVector<Stmt *, 4> BodyParts(SourceHelper.Updates.begin(),
15998 SourceHelper.Updates.end());
15999 if (auto *SourceCXXFor = dyn_cast<CXXForRangeStmt>(Val: SourceLoopStmt))
16000 BodyParts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16001 BodyParts.push_back(Elt: Inner);
16002 Inner = CompoundStmt::Create(C: Context, Stmts: BodyParts, FPFeatures: FPOptionsOverride(),
16003 LB: Inner->getBeginLoc(), RB: Inner->getEndLoc());
16004 Inner = new (Context) ForStmt(
16005 Context, InitStmt.get(), CondExpr.get(), nullptr, IncrStmt.get(), Inner,
16006 SourceHelper.Init->getBeginLoc(), SourceHelper.Init->getBeginLoc(),
16007 SourceHelper.Inc->getEndLoc());
16008 }
16009
16010 return OMPInterchangeDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16011 NumLoops, AssociatedStmt: AStmt, TransformedStmt: Inner,
16012 PreInits: buildPreInits(Context, PreInits));
16013}
16014
16015StmtResult SemaOpenMP::ActOnOpenMPFuseDirective(ArrayRef<OMPClause *> Clauses,
16016 Stmt *AStmt,
16017 SourceLocation StartLoc,
16018 SourceLocation EndLoc) {
16019
16020 ASTContext &Context = getASTContext();
16021 DeclContext *CurrContext = SemaRef.CurContext;
16022 Scope *CurScope = SemaRef.getCurScope();
16023 CaptureVars CopyTransformer(SemaRef);
16024
16025 // Ensure the structured block is not empty
16026 if (!AStmt)
16027 return StmtError();
16028
16029 // Defer transformation in dependent contexts
16030 // The NumLoopNests argument is set to a placeholder 1 (even though
16031 // using looprange fuse could yield up to 3 top level loop nests)
16032 // because a dependent context could prevent determining its true value
16033 if (CurrContext->isDependentContext())
16034 return OMPFuseDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16035 /* NumLoops */ NumGeneratedTopLevelLoops: 1, AssociatedStmt: AStmt, TransformedStmt: nullptr, PreInits: nullptr);
16036
16037 // Validate that the potential loop sequence is transformable for fusion
16038 // Also collect the HelperExprs, Loop Stmts, Inits, and Number of loops
16039 LoopSequenceAnalysis SeqAnalysis;
16040 if (!checkTransformableLoopSequence(Kind: OMPD_fuse, AStmt, SeqAnalysis, Context))
16041 return StmtError();
16042
16043 // SeqAnalysis.LoopSeqSize exists mostly to handle dependent contexts,
16044 // otherwise it must be the same as SeqAnalysis.Loops.size().
16045 assert(SeqAnalysis.LoopSeqSize == SeqAnalysis.Loops.size() &&
16046 "Inconsistent size of the loop sequence and the number of loops "
16047 "found in the sequence");
16048
16049 // Handle clauses, which can be any of the following: [looprange, apply]
16050 const auto *LRC =
16051 OMPExecutableDirective::getSingleClause<OMPLoopRangeClause>(Clauses);
16052
16053 // The clause arguments are invalidated if any error arises
16054 // such as non-constant or non-positive arguments
16055 if (LRC && (!LRC->getFirst() || !LRC->getCount()))
16056 return StmtError();
16057
16058 // Delayed semantic check of LoopRange constraint
16059 // Evaluates the loop range arguments and returns the first and count values
16060 auto EvaluateLoopRangeArguments = [&Context](Expr *First, Expr *Count,
16061 uint64_t &FirstVal,
16062 uint64_t &CountVal) {
16063 llvm::APSInt FirstInt = First->EvaluateKnownConstInt(Ctx: Context);
16064 llvm::APSInt CountInt = Count->EvaluateKnownConstInt(Ctx: Context);
16065 FirstVal = FirstInt.getZExtValue();
16066 CountVal = CountInt.getZExtValue();
16067 };
16068
16069 // OpenMP [6.0, Restrictions]
16070 // first + count - 1 must not evaluate to a value greater than the
16071 // loop sequence length of the associated canonical loop sequence.
16072 auto ValidLoopRange = [](uint64_t FirstVal, uint64_t CountVal,
16073 unsigned NumLoops) -> bool {
16074 return FirstVal + CountVal - 1 <= NumLoops;
16075 };
16076 uint64_t FirstVal = 1, CountVal = 0, LastVal = SeqAnalysis.LoopSeqSize;
16077
16078 // Validates the loop range after evaluating the semantic information
16079 // and ensures that the range is valid for the given loop sequence size.
16080 // Expressions are evaluated at compile time to obtain constant values.
16081 if (LRC) {
16082 EvaluateLoopRangeArguments(LRC->getFirst(), LRC->getCount(), FirstVal,
16083 CountVal);
16084 if (CountVal == 1)
16085 SemaRef.Diag(Loc: LRC->getCountLoc(), DiagID: diag::warn_omp_redundant_fusion)
16086 << getOpenMPDirectiveName(D: OMPD_fuse);
16087
16088 if (!ValidLoopRange(FirstVal, CountVal, SeqAnalysis.LoopSeqSize)) {
16089 SemaRef.Diag(Loc: LRC->getFirstLoc(), DiagID: diag::err_omp_invalid_looprange)
16090 << getOpenMPDirectiveName(D: OMPD_fuse) << FirstVal
16091 << (FirstVal + CountVal - 1) << SeqAnalysis.LoopSeqSize;
16092 return StmtError();
16093 }
16094
16095 LastVal = FirstVal + CountVal - 1;
16096 }
16097
16098 // Complete fusion generates a single canonical loop nest
16099 // However looprange clause may generate several loop nests
16100 unsigned NumGeneratedTopLevelLoops =
16101 LRC ? SeqAnalysis.LoopSeqSize - CountVal + 1 : 1;
16102
16103 // Emit a warning for redundant loop fusion when the sequence contains only
16104 // one loop.
16105 if (SeqAnalysis.LoopSeqSize == 1)
16106 SemaRef.Diag(Loc: AStmt->getBeginLoc(), DiagID: diag::warn_omp_redundant_fusion)
16107 << getOpenMPDirectiveName(D: OMPD_fuse);
16108
16109 // Select the type with the largest bit width among all induction variables
16110 QualType IVType =
16111 SeqAnalysis.Loops[FirstVal - 1].HelperExprs.IterationVarRef->getType();
16112 for (unsigned I : llvm::seq<unsigned>(Begin: FirstVal, End: LastVal)) {
16113 QualType CurrentIVType =
16114 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef->getType();
16115 if (Context.getTypeSize(T: CurrentIVType) > Context.getTypeSize(T: IVType)) {
16116 IVType = CurrentIVType;
16117 }
16118 }
16119 uint64_t IVBitWidth = Context.getIntWidth(T: IVType);
16120
16121 // Create pre-init declarations for all loops lower bounds, upper bounds,
16122 // strides and num-iterations for every top level loop in the fusion
16123 SmallVector<VarDecl *, 4> LBVarDecls;
16124 SmallVector<VarDecl *, 4> STVarDecls;
16125 SmallVector<VarDecl *, 4> NIVarDecls;
16126 SmallVector<VarDecl *, 4> UBVarDecls;
16127 SmallVector<VarDecl *, 4> IVVarDecls;
16128
16129 // Helper lambda to create variables for bounds, strides, and other
16130 // expressions. Generates both the variable declaration and the corresponding
16131 // initialization statement.
16132 auto CreateHelperVarAndStmt =
16133 [&, &SemaRef = SemaRef](Expr *ExprToCopy, const std::string &BaseName,
16134 unsigned I, bool NeedsNewVD = false) {
16135 Expr *TransformedExpr =
16136 AssertSuccess(R: CopyTransformer.TransformExpr(E: ExprToCopy));
16137 if (!TransformedExpr)
16138 return std::pair<VarDecl *, StmtResult>(nullptr, StmtError());
16139
16140 auto Name = (Twine(".omp.") + BaseName + std::to_string(val: I)).str();
16141
16142 VarDecl *VD;
16143 if (NeedsNewVD) {
16144 VD = buildVarDecl(SemaRef, Loc: SourceLocation(), Type: IVType, Name);
16145 SemaRef.AddInitializerToDecl(dcl: VD, init: TransformedExpr, DirectInit: false);
16146 } else {
16147 // Create a unique variable name
16148 DeclRefExpr *DRE = cast<DeclRefExpr>(Val: TransformedExpr);
16149 VD = cast<VarDecl>(Val: DRE->getDecl());
16150 VD->setDeclName(&SemaRef.PP.getIdentifierTable().get(Name));
16151 }
16152 // Create the corresponding declaration statement
16153 StmtResult DeclStmt = new (Context) class DeclStmt(
16154 DeclGroupRef(VD), SourceLocation(), SourceLocation());
16155 return std::make_pair(x&: VD, y&: DeclStmt);
16156 };
16157
16158 // PreInits hold a sequence of variable declarations that must be executed
16159 // before the fused loop begins. These include bounds, strides, and other
16160 // helper variables required for the transformation. Other loop transforms
16161 // also contain their own preinits
16162 SmallVector<Stmt *> PreInits;
16163
16164 // Update the general preinits using the preinits generated by loop sequence
16165 // generating loop transformations. These preinits differ slightly from
16166 // single-loop transformation preinits, as they can be detached from a
16167 // specific loop inside multiple generated loop nests. This happens
16168 // because certain helper variables, like '.omp.fuse.max', are introduced to
16169 // handle fused iteration spaces and may not be directly tied to a single
16170 // original loop. The preinit structure must ensure that hidden variables
16171 // like '.omp.fuse.max' are still properly handled.
16172 // Transformations that apply this concept: Loopranged Fuse, Split
16173 llvm::append_range(C&: PreInits, R&: SeqAnalysis.LoopSequencePreInits);
16174
16175 // Process each single loop to generate and collect declarations
16176 // and statements for all helper expressions related to
16177 // particular single loop nests
16178
16179 // Also In the case of the fused loops, we keep track of their original
16180 // inits by appending them to their preinits statement, and in the case of
16181 // transformations, also append their preinits (which contain the original
16182 // loop initialization statement or other statements)
16183
16184 // Firstly we need to set TransformIndex to match the begining of the
16185 // looprange section
16186 unsigned int TransformIndex = 0;
16187 for (unsigned I : llvm::seq<unsigned>(Size: FirstVal - 1)) {
16188 if (SeqAnalysis.Loops[I].isLoopTransformation())
16189 ++TransformIndex;
16190 }
16191
16192 for (unsigned int I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16193 if (SeqAnalysis.Loops[I].isRegularLoop()) {
16194 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
16195 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
16196 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
16197 } else if (SeqAnalysis.Loops[I].isLoopTransformation()) {
16198 // For transformed loops, insert both pre-inits and original inits.
16199 // Order matters: pre-inits may define variables used in the original
16200 // inits such as upper bounds...
16201 SmallVector<Stmt *> &TransformPreInit =
16202 SeqAnalysis.Loops[TransformIndex++].TransformsPreInits;
16203 llvm::append_range(C&: PreInits, R&: TransformPreInit);
16204
16205 addLoopPreInits(Context, LoopHelper&: SeqAnalysis.Loops[I].HelperExprs,
16206 LoopStmt: SeqAnalysis.Loops[I].TheForStmt,
16207 OriginalInit: SeqAnalysis.Loops[I].OriginalInits, PreInits);
16208 }
16209 auto [UBVD, UBDStmt] =
16210 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.UB, "ub", J);
16211 auto [LBVD, LBDStmt] =
16212 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.LB, "lb", J);
16213 auto [STVD, STDStmt] =
16214 CreateHelperVarAndStmt(SeqAnalysis.Loops[I].HelperExprs.ST, "st", J);
16215 auto [NIVD, NIDStmt] = CreateHelperVarAndStmt(
16216 SeqAnalysis.Loops[I].HelperExprs.NumIterations, "ni", J, true);
16217 auto [IVVD, IVDStmt] = CreateHelperVarAndStmt(
16218 SeqAnalysis.Loops[I].HelperExprs.IterationVarRef, "iv", J);
16219
16220 assert(LBVD && STVD && NIVD && IVVD &&
16221 "OpenMP Fuse Helper variables creation failed");
16222
16223 UBVarDecls.push_back(Elt: UBVD);
16224 LBVarDecls.push_back(Elt: LBVD);
16225 STVarDecls.push_back(Elt: STVD);
16226 NIVarDecls.push_back(Elt: NIVD);
16227 IVVarDecls.push_back(Elt: IVVD);
16228
16229 PreInits.push_back(Elt: LBDStmt.get());
16230 PreInits.push_back(Elt: STDStmt.get());
16231 PreInits.push_back(Elt: NIDStmt.get());
16232 PreInits.push_back(Elt: IVDStmt.get());
16233 }
16234
16235 auto MakeVarDeclRef = [&SemaRef = this->SemaRef](VarDecl *VD) {
16236 return buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType(), Loc: VD->getLocation(),
16237 RefersToCapture: false);
16238 };
16239
16240 // Following up the creation of the final fused loop will be performed
16241 // which has the following shape (considering the selected loops):
16242 //
16243 // for (fuse.index = 0; fuse.index < max(ni0, ni1..., nik); ++fuse.index) {
16244 // if (fuse.index < ni0){
16245 // iv0 = lb0 + st0 * fuse.index;
16246 // original.index0 = iv0
16247 // body(0);
16248 // }
16249 // if (fuse.index < ni1){
16250 // iv1 = lb1 + st1 * fuse.index;
16251 // original.index1 = iv1
16252 // body(1);
16253 // }
16254 //
16255 // ...
16256 //
16257 // if (fuse.index < nik){
16258 // ivk = lbk + stk * fuse.index;
16259 // original.indexk = ivk
16260 // body(k); Expr *InitVal = IntegerLiteral::Create(Context,
16261 // llvm::APInt(IVWidth, 0),
16262 // }
16263
16264 // 1. Create the initialized fuse index
16265 StringRef IndexName = ".omp.fuse.index";
16266 Expr *InitVal = IntegerLiteral::Create(C: Context, V: llvm::APInt(IVBitWidth, 0),
16267 type: IVType, l: SourceLocation());
16268 VarDecl *IndexDecl =
16269 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: IndexName, Attrs: nullptr, OrigRef: nullptr);
16270 SemaRef.AddInitializerToDecl(dcl: IndexDecl, init: InitVal, DirectInit: false);
16271 StmtResult InitStmt = new (Context)
16272 DeclStmt(DeclGroupRef(IndexDecl), SourceLocation(), SourceLocation());
16273
16274 if (!InitStmt.isUsable())
16275 return StmtError();
16276
16277 auto MakeIVRef = [&SemaRef = this->SemaRef, IndexDecl, IVType,
16278 Loc = InitVal->getExprLoc()]() {
16279 return buildDeclRefExpr(S&: SemaRef, D: IndexDecl, Ty: IVType, Loc, RefersToCapture: false);
16280 };
16281
16282 // 2. Iteratively compute the max number of logical iterations Max(NI_1, NI_2,
16283 // ..., NI_k)
16284 //
16285 // This loop accumulates the maximum value across multiple expressions,
16286 // ensuring each step constructs a unique AST node for correctness. By using
16287 // intermediate temporary variables and conditional operators, we maintain
16288 // distinct nodes and avoid duplicating subtrees, For instance, max(a,b,c):
16289 // omp.temp0 = max(a, b)
16290 // omp.temp1 = max(omp.temp0, c)
16291 // omp.fuse.max = max(omp.temp1, omp.temp0)
16292
16293 ExprResult MaxExpr;
16294 // I is the range of loops in the sequence that we fuse.
16295 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16296 DeclRefExpr *NIRef = MakeVarDeclRef(NIVarDecls[J]);
16297 QualType NITy = NIRef->getType();
16298
16299 if (MaxExpr.isUnset()) {
16300 // Initialize MaxExpr with the first NI expression
16301 MaxExpr = NIRef;
16302 } else {
16303 // Create a new acummulator variable t_i = MaxExpr
16304 std::string TempName = (Twine(".omp.temp.") + Twine(J)).str();
16305 VarDecl *TempDecl =
16306 buildVarDecl(SemaRef, Loc: {}, Type: NITy, Name: TempName, Attrs: nullptr, OrigRef: nullptr);
16307 TempDecl->setInit(MaxExpr.get());
16308 DeclRefExpr *TempRef =
16309 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
16310 DeclRefExpr *TempRef2 =
16311 buildDeclRefExpr(S&: SemaRef, D: TempDecl, Ty: NITy, Loc: SourceLocation(), RefersToCapture: false);
16312 // Add a DeclStmt to PreInits to ensure the variable is declared.
16313 StmtResult TempStmt = new (Context)
16314 DeclStmt(DeclGroupRef(TempDecl), SourceLocation(), SourceLocation());
16315
16316 if (!TempStmt.isUsable())
16317 return StmtError();
16318 PreInits.push_back(Elt: TempStmt.get());
16319
16320 // Build MaxExpr <-(MaxExpr > NIRef ? MaxExpr : NIRef)
16321 ExprResult Comparison =
16322 SemaRef.BuildBinOp(S: nullptr, OpLoc: SourceLocation(), Opc: BO_GT, LHSExpr: TempRef, RHSExpr: NIRef);
16323 // Handle any errors in Comparison creation
16324 if (!Comparison.isUsable())
16325 return StmtError();
16326
16327 DeclRefExpr *NIRef2 = MakeVarDeclRef(NIVarDecls[J]);
16328 // Update MaxExpr using a conditional expression to hold the max value
16329 MaxExpr = new (Context) ConditionalOperator(
16330 Comparison.get(), SourceLocation(), TempRef2, SourceLocation(),
16331 NIRef2->getExprStmt(), NITy, VK_LValue, OK_Ordinary);
16332
16333 if (!MaxExpr.isUsable())
16334 return StmtError();
16335 }
16336 }
16337 if (!MaxExpr.isUsable())
16338 return StmtError();
16339
16340 // 3. Declare the max variable
16341 const std::string MaxName = Twine(".omp.fuse.max").str();
16342 VarDecl *MaxDecl =
16343 buildVarDecl(SemaRef, Loc: {}, Type: IVType, Name: MaxName, Attrs: nullptr, OrigRef: nullptr);
16344 MaxDecl->setInit(MaxExpr.get());
16345 DeclRefExpr *MaxRef = buildDeclRefExpr(S&: SemaRef, D: MaxDecl, Ty: IVType, Loc: {}, RefersToCapture: false);
16346 StmtResult MaxStmt = new (Context)
16347 DeclStmt(DeclGroupRef(MaxDecl), SourceLocation(), SourceLocation());
16348
16349 if (MaxStmt.isInvalid())
16350 return StmtError();
16351 PreInits.push_back(Elt: MaxStmt.get());
16352
16353 // 4. Create condition Expr: index < n_max
16354 ExprResult CondExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT,
16355 LHSExpr: MakeIVRef(), RHSExpr: MaxRef);
16356 if (!CondExpr.isUsable())
16357 return StmtError();
16358
16359 // 5. Increment Expr: ++index
16360 ExprResult IncrExpr =
16361 SemaRef.BuildUnaryOp(S: CurScope, OpLoc: SourceLocation(), Opc: UO_PreInc, Input: MakeIVRef());
16362 if (!IncrExpr.isUsable())
16363 return StmtError();
16364
16365 // 6. Build the Fused Loop Body
16366 // The final fused loop iterates over the maximum logical range. Inside the
16367 // loop, each original loop's index is calculated dynamically, and its body
16368 // is executed conditionally.
16369 //
16370 // Each sub-loop's body is guarded by a conditional statement to ensure
16371 // it executes only within its logical iteration range:
16372 //
16373 // if (fuse.index < ni_k){
16374 // iv_k = lb_k + st_k * fuse.index;
16375 // original.index = iv_k
16376 // body(k);
16377 // }
16378
16379 CompoundStmt *FusedBody = nullptr;
16380 SmallVector<Stmt *, 4> FusedBodyStmts;
16381 for (unsigned I = FirstVal - 1, J = 0; I < LastVal; ++I, ++J) {
16382 // Assingment of the original sub-loop index to compute the logical index
16383 // IV_k = LB_k + omp.fuse.index * ST_k
16384 ExprResult IdxExpr =
16385 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Mul,
16386 LHSExpr: MakeVarDeclRef(STVarDecls[J]), RHSExpr: MakeIVRef());
16387 if (!IdxExpr.isUsable())
16388 return StmtError();
16389 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Add,
16390 LHSExpr: MakeVarDeclRef(LBVarDecls[J]), RHSExpr: IdxExpr.get());
16391
16392 if (!IdxExpr.isUsable())
16393 return StmtError();
16394 IdxExpr = SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_Assign,
16395 LHSExpr: MakeVarDeclRef(IVVarDecls[J]), RHSExpr: IdxExpr.get());
16396 if (!IdxExpr.isUsable())
16397 return StmtError();
16398
16399 // Update the original i_k = IV_k
16400 SmallVector<Stmt *, 4> BodyStmts;
16401 BodyStmts.push_back(Elt: IdxExpr.get());
16402 llvm::append_range(C&: BodyStmts, R&: SeqAnalysis.Loops[I].HelperExprs.Updates);
16403
16404 // If the loop is a CXXForRangeStmt then the iterator variable is needed
16405 if (auto *SourceCXXFor =
16406 dyn_cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
16407 BodyStmts.push_back(Elt: SourceCXXFor->getLoopVarStmt());
16408
16409 Stmt *Body =
16410 (isa<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt))
16411 ? cast<ForStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody()
16412 : cast<CXXForRangeStmt>(Val: SeqAnalysis.Loops[I].TheForStmt)->getBody();
16413 BodyStmts.push_back(Elt: Body);
16414
16415 CompoundStmt *CombinedBody =
16416 CompoundStmt::Create(C: Context, Stmts: BodyStmts, FPFeatures: FPOptionsOverride(),
16417 LB: SourceLocation(), RB: SourceLocation());
16418 ExprResult Condition =
16419 SemaRef.BuildBinOp(S: CurScope, OpLoc: SourceLocation(), Opc: BO_LT, LHSExpr: MakeIVRef(),
16420 RHSExpr: MakeVarDeclRef(NIVarDecls[J]));
16421
16422 if (!Condition.isUsable())
16423 return StmtError();
16424
16425 IfStmt *IfStatement = IfStmt::Create(
16426 Ctx: Context, IL: SourceLocation(), Kind: IfStatementKind::Ordinary, Init: nullptr, Var: nullptr,
16427 Cond: Condition.get(), LPL: SourceLocation(), RPL: SourceLocation(), Then: CombinedBody,
16428 EL: SourceLocation(), Else: nullptr);
16429
16430 FusedBodyStmts.push_back(Elt: IfStatement);
16431 }
16432 FusedBody = CompoundStmt::Create(C: Context, Stmts: FusedBodyStmts, FPFeatures: FPOptionsOverride(),
16433 LB: SourceLocation(), RB: SourceLocation());
16434
16435 // 7. Construct the final fused loop
16436 ForStmt *FusedForStmt = new (Context)
16437 ForStmt(Context, InitStmt.get(), CondExpr.get(), nullptr, IncrExpr.get(),
16438 FusedBody, InitStmt.get()->getBeginLoc(), SourceLocation(),
16439 IncrExpr.get()->getEndLoc());
16440
16441 // In the case of looprange, the result of fuse won't simply
16442 // be a single loop (ForStmt), but rather a loop sequence
16443 // (CompoundStmt) of 3 parts: the pre-fusion loops, the fused loop
16444 // and the post-fusion loops, preserving its original order.
16445 //
16446 // Note: If looprange clause produces a single fused loop nest then
16447 // this compound statement wrapper is unnecessary (Therefore this
16448 // treatment is skipped)
16449
16450 Stmt *FusionStmt = FusedForStmt;
16451 if (LRC && CountVal != SeqAnalysis.LoopSeqSize) {
16452 SmallVector<Stmt *, 4> FinalLoops;
16453
16454 // Reset the transform index
16455 TransformIndex = 0;
16456
16457 // Collect all non-fused loops before and after the fused region.
16458 // Pre-fusion and post-fusion loops are inserted in order exploiting their
16459 // symmetry, along with their corresponding transformation pre-inits if
16460 // needed. The fused loop is added between the two regions.
16461 for (unsigned I : llvm::seq<unsigned>(Size: SeqAnalysis.LoopSeqSize)) {
16462 if (I >= FirstVal - 1 && I < FirstVal + CountVal - 1) {
16463 // Update the Transformation counter to skip already treated
16464 // loop transformations
16465 if (!SeqAnalysis.Loops[I].isLoopTransformation())
16466 ++TransformIndex;
16467 continue;
16468 }
16469
16470 // No need to handle:
16471 // Regular loops: they are kept intact as-is.
16472 // Loop-sequence-generating transformations: already handled earlier.
16473 // Only TransformSingleLoop requires inserting pre-inits here
16474 if (SeqAnalysis.Loops[I].isRegularLoop()) {
16475 const auto &TransformPreInit =
16476 SeqAnalysis.Loops[TransformIndex++].TransformsPreInits;
16477 if (!TransformPreInit.empty())
16478 llvm::append_range(C&: PreInits, R: TransformPreInit);
16479 }
16480
16481 FinalLoops.push_back(Elt: SeqAnalysis.Loops[I].TheForStmt);
16482 }
16483
16484 FinalLoops.insert(I: FinalLoops.begin() + (FirstVal - 1), Elt: FusedForStmt);
16485 FusionStmt = CompoundStmt::Create(C: Context, Stmts: FinalLoops, FPFeatures: FPOptionsOverride(),
16486 LB: SourceLocation(), RB: SourceLocation());
16487 }
16488 return OMPFuseDirective::Create(C: Context, StartLoc, EndLoc, Clauses,
16489 NumGeneratedTopLevelLoops, AssociatedStmt: AStmt, TransformedStmt: FusionStmt,
16490 PreInits: buildPreInits(Context, PreInits));
16491}
16492
16493OMPClause *SemaOpenMP::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
16494 Expr *Expr,
16495 SourceLocation StartLoc,
16496 SourceLocation LParenLoc,
16497 SourceLocation EndLoc) {
16498 OMPClause *Res = nullptr;
16499 switch (Kind) {
16500 case OMPC_final:
16501 Res = ActOnOpenMPFinalClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16502 break;
16503 case OMPC_safelen:
16504 Res = ActOnOpenMPSafelenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
16505 break;
16506 case OMPC_simdlen:
16507 Res = ActOnOpenMPSimdlenClause(Length: Expr, StartLoc, LParenLoc, EndLoc);
16508 break;
16509 case OMPC_allocator:
16510 Res = ActOnOpenMPAllocatorClause(Allocator: Expr, StartLoc, LParenLoc, EndLoc);
16511 break;
16512 case OMPC_collapse:
16513 Res = ActOnOpenMPCollapseClause(NumForLoops: Expr, StartLoc, LParenLoc, EndLoc);
16514 break;
16515 case OMPC_ordered:
16516 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, NumForLoops: Expr);
16517 break;
16518 case OMPC_nowait:
16519 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc, LParenLoc, Condition: Expr);
16520 break;
16521 case OMPC_priority:
16522 Res = ActOnOpenMPPriorityClause(Priority: Expr, StartLoc, LParenLoc, EndLoc);
16523 break;
16524 case OMPC_hint:
16525 Res = ActOnOpenMPHintClause(Hint: Expr, StartLoc, LParenLoc, EndLoc);
16526 break;
16527 case OMPC_depobj:
16528 Res = ActOnOpenMPDepobjClause(Depobj: Expr, StartLoc, LParenLoc, EndLoc);
16529 break;
16530 case OMPC_detach:
16531 Res = ActOnOpenMPDetachClause(Evt: Expr, StartLoc, LParenLoc, EndLoc);
16532 break;
16533 case OMPC_novariants:
16534 Res = ActOnOpenMPNovariantsClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16535 break;
16536 case OMPC_nocontext:
16537 Res = ActOnOpenMPNocontextClause(Condition: Expr, StartLoc, LParenLoc, EndLoc);
16538 break;
16539 case OMPC_filter:
16540 Res = ActOnOpenMPFilterClause(ThreadID: Expr, StartLoc, LParenLoc, EndLoc);
16541 break;
16542 case OMPC_partial:
16543 Res = ActOnOpenMPPartialClause(FactorExpr: Expr, StartLoc, LParenLoc, EndLoc);
16544 break;
16545 case OMPC_message:
16546 Res = ActOnOpenMPMessageClause(MS: Expr, StartLoc, LParenLoc, EndLoc);
16547 break;
16548 case OMPC_align:
16549 Res = ActOnOpenMPAlignClause(Alignment: Expr, StartLoc, LParenLoc, EndLoc);
16550 break;
16551 case OMPC_ompx_dyn_cgroup_mem:
16552 Res = ActOnOpenMPXDynCGroupMemClause(Size: Expr, StartLoc, LParenLoc, EndLoc);
16553 break;
16554 case OMPC_holds:
16555 Res = ActOnOpenMPHoldsClause(E: Expr, StartLoc, LParenLoc, EndLoc);
16556 break;
16557 case OMPC_transparent:
16558 Res = ActOnOpenMPTransparentClause(Transparent: Expr, StartLoc, LParenLoc, EndLoc);
16559 break;
16560 case OMPC_dyn_groupprivate:
16561 case OMPC_grainsize:
16562 case OMPC_num_tasks:
16563 case OMPC_num_threads:
16564 case OMPC_device:
16565 case OMPC_if:
16566 case OMPC_default:
16567 case OMPC_proc_bind:
16568 case OMPC_schedule:
16569 case OMPC_private:
16570 case OMPC_firstprivate:
16571 case OMPC_lastprivate:
16572 case OMPC_shared:
16573 case OMPC_reduction:
16574 case OMPC_task_reduction:
16575 case OMPC_in_reduction:
16576 case OMPC_linear:
16577 case OMPC_aligned:
16578 case OMPC_copyin:
16579 case OMPC_copyprivate:
16580 case OMPC_untied:
16581 case OMPC_mergeable:
16582 case OMPC_threadprivate:
16583 case OMPC_groupprivate:
16584 case OMPC_sizes:
16585 case OMPC_allocate:
16586 case OMPC_flush:
16587 case OMPC_read:
16588 case OMPC_write:
16589 case OMPC_update:
16590 case OMPC_capture:
16591 case OMPC_compare:
16592 case OMPC_seq_cst:
16593 case OMPC_acq_rel:
16594 case OMPC_acquire:
16595 case OMPC_release:
16596 case OMPC_relaxed:
16597 case OMPC_depend:
16598 case OMPC_threads:
16599 case OMPC_simd:
16600 case OMPC_map:
16601 case OMPC_nogroup:
16602 case OMPC_dist_schedule:
16603 case OMPC_defaultmap:
16604 case OMPC_unknown:
16605 case OMPC_uniform:
16606 case OMPC_to:
16607 case OMPC_from:
16608 case OMPC_use_device_ptr:
16609 case OMPC_use_device_addr:
16610 case OMPC_is_device_ptr:
16611 case OMPC_unified_address:
16612 case OMPC_unified_shared_memory:
16613 case OMPC_reverse_offload:
16614 case OMPC_dynamic_allocators:
16615 case OMPC_atomic_default_mem_order:
16616 case OMPC_self_maps:
16617 case OMPC_device_type:
16618 case OMPC_match:
16619 case OMPC_nontemporal:
16620 case OMPC_order:
16621 case OMPC_at:
16622 case OMPC_severity:
16623 case OMPC_destroy:
16624 case OMPC_inclusive:
16625 case OMPC_exclusive:
16626 case OMPC_uses_allocators:
16627 case OMPC_affinity:
16628 case OMPC_when:
16629 case OMPC_bind:
16630 case OMPC_num_teams:
16631 case OMPC_thread_limit:
16632 default:
16633 llvm_unreachable("Clause is not allowed.");
16634 }
16635 return Res;
16636}
16637
16638// An OpenMP directive such as 'target parallel' has two captured regions:
16639// for the 'target' and 'parallel' respectively. This function returns
16640// the region in which to capture expressions associated with a clause.
16641// A return value of OMPD_unknown signifies that the expression should not
16642// be captured.
16643static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
16644 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, unsigned OpenMPVersion,
16645 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
16646 assert(isAllowedClauseForDirective(DKind, CKind, OpenMPVersion) &&
16647 "Invalid directive with CKind-clause");
16648
16649 // Invalid modifier will be diagnosed separately, just return OMPD_unknown.
16650 if (NameModifier != OMPD_unknown &&
16651 !isAllowedClauseForDirective(D: NameModifier, C: CKind, Version: OpenMPVersion))
16652 return OMPD_unknown;
16653
16654 ArrayRef<OpenMPDirectiveKind> Leafs = getLeafConstructsOrSelf(D: DKind);
16655
16656 // [5.2:341:24-30]
16657 // If the clauses have expressions on them, such as for various clauses where
16658 // the argument of the clause is an expression, or lower-bound, length, or
16659 // stride expressions inside array sections (or subscript and stride
16660 // expressions in subscript-triplet for Fortran), or linear-step or alignment
16661 // expressions, the expressions are evaluated immediately before the construct
16662 // to which the clause has been split or duplicated per the above rules
16663 // (therefore inside of the outer leaf constructs). However, the expressions
16664 // inside the num_teams and thread_limit clauses are always evaluated before
16665 // the outermost leaf construct.
16666
16667 // Process special cases first.
16668 switch (CKind) {
16669 case OMPC_if:
16670 switch (DKind) {
16671 case OMPD_teams_loop:
16672 case OMPD_target_teams_loop:
16673 // For [target] teams loop, assume capture region is 'teams' so it's
16674 // available for codegen later to use if/when necessary.
16675 return OMPD_teams;
16676 case OMPD_target_update:
16677 case OMPD_target_enter_data:
16678 case OMPD_target_exit_data:
16679 return OMPD_task;
16680 default:
16681 break;
16682 }
16683 break;
16684 case OMPC_num_teams:
16685 case OMPC_thread_limit:
16686 case OMPC_ompx_dyn_cgroup_mem:
16687 case OMPC_dyn_groupprivate:
16688 // TODO: This may need to consider teams too.
16689 if (Leafs[0] == OMPD_target)
16690 return OMPD_target;
16691 break;
16692 case OMPC_device:
16693 if (Leafs[0] == OMPD_target ||
16694 llvm::is_contained(Set: {OMPD_dispatch, OMPD_target_update,
16695 OMPD_target_enter_data, OMPD_target_exit_data},
16696 Element: DKind))
16697 return OMPD_task;
16698 break;
16699 case OMPC_novariants:
16700 case OMPC_nocontext:
16701 if (DKind == OMPD_dispatch)
16702 return OMPD_task;
16703 break;
16704 case OMPC_when:
16705 if (DKind == OMPD_metadirective)
16706 return OMPD_metadirective;
16707 break;
16708 case OMPC_filter:
16709 return OMPD_unknown;
16710 default:
16711 break;
16712 }
16713
16714 // If none of the special cases above applied, and DKind is a capturing
16715 // directive, find the innermost enclosing leaf construct that allows the
16716 // clause, and returns the corresponding capture region.
16717
16718 auto GetEnclosingRegion = [&](int EndIdx, OpenMPClauseKind Clause) {
16719 // Find the index in "Leafs" of the last leaf that allows the given
16720 // clause. The search will only include indexes [0, EndIdx).
16721 // EndIdx may be set to the index of the NameModifier, if present.
16722 int InnermostIdx = [&]() {
16723 for (int I = EndIdx - 1; I >= 0; --I) {
16724 if (isAllowedClauseForDirective(D: Leafs[I], C: Clause, Version: OpenMPVersion))
16725 return I;
16726 }
16727 return -1;
16728 }();
16729
16730 // Find the nearest enclosing capture region.
16731 SmallVector<OpenMPDirectiveKind, 2> Regions;
16732 for (int I = InnermostIdx - 1; I >= 0; --I) {
16733 if (!isOpenMPCapturingDirective(DKind: Leafs[I]))
16734 continue;
16735 Regions.clear();
16736 getOpenMPCaptureRegions(CaptureRegions&: Regions, DKind: Leafs[I]);
16737 if (Regions[0] != OMPD_unknown)
16738 return Regions.back();
16739 }
16740 return OMPD_unknown;
16741 };
16742
16743 if (isOpenMPCapturingDirective(DKind)) {
16744 auto GetLeafIndex = [&](OpenMPDirectiveKind Dir) {
16745 for (int I = 0, E = Leafs.size(); I != E; ++I) {
16746 if (Leafs[I] == Dir)
16747 return I + 1;
16748 }
16749 return 0;
16750 };
16751
16752 int End = NameModifier == OMPD_unknown ? Leafs.size()
16753 : GetLeafIndex(NameModifier);
16754 return GetEnclosingRegion(End, CKind);
16755 }
16756
16757 return OMPD_unknown;
16758}
16759
16760OMPClause *SemaOpenMP::ActOnOpenMPIfClause(
16761 OpenMPDirectiveKind NameModifier, Expr *Condition, SourceLocation StartLoc,
16762 SourceLocation LParenLoc, SourceLocation NameModifierLoc,
16763 SourceLocation ColonLoc, SourceLocation EndLoc) {
16764 Expr *ValExpr = Condition;
16765 Stmt *HelperValStmt = nullptr;
16766 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16767 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
16768 !Condition->isInstantiationDependent() &&
16769 !Condition->containsUnexpandedParameterPack()) {
16770 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
16771 if (Val.isInvalid())
16772 return nullptr;
16773
16774 ValExpr = Val.get();
16775
16776 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16777 CaptureRegion = getOpenMPCaptureRegionForClause(
16778 DKind, CKind: OMPC_if, OpenMPVersion: getLangOpts().OpenMP, NameModifier);
16779 if (CaptureRegion != OMPD_unknown &&
16780 !SemaRef.CurContext->isDependentContext()) {
16781 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16782 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16783 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16784 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
16785 }
16786 }
16787
16788 return new (getASTContext())
16789 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
16790 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
16791}
16792
16793OMPClause *SemaOpenMP::ActOnOpenMPFinalClause(Expr *Condition,
16794 SourceLocation StartLoc,
16795 SourceLocation LParenLoc,
16796 SourceLocation EndLoc) {
16797 Expr *ValExpr = Condition;
16798 Stmt *HelperValStmt = nullptr;
16799 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
16800 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
16801 !Condition->isInstantiationDependent() &&
16802 !Condition->containsUnexpandedParameterPack()) {
16803 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
16804 if (Val.isInvalid())
16805 return nullptr;
16806
16807 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
16808
16809 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16810 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_final,
16811 OpenMPVersion: getLangOpts().OpenMP);
16812 if (CaptureRegion != OMPD_unknown &&
16813 !SemaRef.CurContext->isDependentContext()) {
16814 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16815 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16816 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16817 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
16818 }
16819 }
16820
16821 return new (getASTContext()) OMPFinalClause(
16822 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
16823}
16824
16825ExprResult
16826SemaOpenMP::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
16827 Expr *Op) {
16828 if (!Op)
16829 return ExprError();
16830
16831 class IntConvertDiagnoser : public Sema::ICEConvertDiagnoser {
16832 public:
16833 IntConvertDiagnoser()
16834 : ICEConvertDiagnoser(/*AllowScopedEnumerations=*/false, false, true) {}
16835 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
16836 QualType T) override {
16837 return S.Diag(Loc, DiagID: diag::err_omp_not_integral) << T;
16838 }
16839 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
16840 QualType T) override {
16841 return S.Diag(Loc, DiagID: diag::err_omp_incomplete_type) << T;
16842 }
16843 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
16844 QualType T,
16845 QualType ConvTy) override {
16846 return S.Diag(Loc, DiagID: diag::err_omp_explicit_conversion) << T << ConvTy;
16847 }
16848 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
16849 QualType ConvTy) override {
16850 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
16851 << ConvTy->isEnumeralType() << ConvTy;
16852 }
16853 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
16854 QualType T) override {
16855 return S.Diag(Loc, DiagID: diag::err_omp_ambiguous_conversion) << T;
16856 }
16857 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
16858 QualType ConvTy) override {
16859 return S.Diag(Loc: Conv->getLocation(), DiagID: diag::note_omp_conversion_here)
16860 << ConvTy->isEnumeralType() << ConvTy;
16861 }
16862 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
16863 QualType) override {
16864 llvm_unreachable("conversion functions are permitted");
16865 }
16866 } ConvertDiagnoser;
16867 return SemaRef.PerformContextualImplicitConversion(Loc, FromE: Op, Converter&: ConvertDiagnoser);
16868}
16869
16870static bool
16871isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
16872 bool StrictlyPositive, bool BuildCapture = false,
16873 OpenMPDirectiveKind DKind = OMPD_unknown,
16874 OpenMPDirectiveKind *CaptureRegion = nullptr,
16875 Stmt **HelperValStmt = nullptr) {
16876 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
16877 !ValExpr->isInstantiationDependent()) {
16878 SourceLocation Loc = ValExpr->getExprLoc();
16879 ExprResult Value =
16880 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc, Op: ValExpr);
16881 if (Value.isInvalid())
16882 return false;
16883
16884 ValExpr = Value.get();
16885 // The expression must evaluate to a non-negative integer value.
16886 if (std::optional<llvm::APSInt> Result =
16887 ValExpr->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
16888 if (Result->isSigned() &&
16889 !((!StrictlyPositive && Result->isNonNegative()) ||
16890 (StrictlyPositive && Result->isStrictlyPositive()))) {
16891 SemaRef.Diag(Loc, DiagID: diag::err_omp_negative_expression_in_clause)
16892 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
16893 << ValExpr->getSourceRange();
16894 return false;
16895 }
16896 }
16897 if (!BuildCapture)
16898 return true;
16899 *CaptureRegion =
16900 getOpenMPCaptureRegionForClause(DKind, CKind, OpenMPVersion: SemaRef.LangOpts.OpenMP);
16901 if (*CaptureRegion != OMPD_unknown &&
16902 !SemaRef.CurContext->isDependentContext()) {
16903 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16904 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16905 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16906 *HelperValStmt = buildPreInits(Context&: SemaRef.Context, Captures);
16907 }
16908 }
16909 return true;
16910}
16911
16912static std::string getListOfPossibleValues(OpenMPClauseKind K, unsigned First,
16913 unsigned Last,
16914 ArrayRef<unsigned> Exclude = {}) {
16915 SmallString<256> Buffer;
16916 llvm::raw_svector_ostream Out(Buffer);
16917 unsigned Skipped = Exclude.size();
16918 for (unsigned I = First; I < Last; ++I) {
16919 if (llvm::is_contained(Range&: Exclude, Element: I)) {
16920 --Skipped;
16921 continue;
16922 }
16923 Out << "'" << getOpenMPSimpleClauseTypeName(Kind: K, Type: I) << "'";
16924 if (I + Skipped + 2 == Last)
16925 Out << " or ";
16926 else if (I + Skipped + 1 != Last)
16927 Out << ", ";
16928 }
16929 return std::string(Out.str());
16930}
16931
16932OMPClause *SemaOpenMP::ActOnOpenMPNumThreadsClause(
16933 OpenMPNumThreadsClauseModifier Modifier, Expr *NumThreads,
16934 SourceLocation StartLoc, SourceLocation LParenLoc,
16935 SourceLocation ModifierLoc, SourceLocation EndLoc) {
16936 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 60) &&
16937 "Unexpected num_threads modifier in OpenMP < 60.");
16938
16939 if (ModifierLoc.isValid() && Modifier == OMPC_NUMTHREADS_unknown) {
16940 std::string Values = getListOfPossibleValues(K: OMPC_num_threads, /*First=*/0,
16941 Last: OMPC_NUMTHREADS_unknown);
16942 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
16943 << Values << getOpenMPClauseNameForDiag(C: OMPC_num_threads);
16944 return nullptr;
16945 }
16946
16947 Expr *ValExpr = NumThreads;
16948 Stmt *HelperValStmt = nullptr;
16949
16950 // OpenMP [2.5, Restrictions]
16951 // The num_threads expression must evaluate to a positive integer value.
16952 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_threads,
16953 /*StrictlyPositive=*/true))
16954 return nullptr;
16955
16956 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16957 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
16958 DKind, CKind: OMPC_num_threads, OpenMPVersion: getLangOpts().OpenMP);
16959 if (CaptureRegion != OMPD_unknown &&
16960 !SemaRef.CurContext->isDependentContext()) {
16961 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
16962 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16963 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
16964 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
16965 }
16966
16967 return new (getASTContext())
16968 OMPNumThreadsClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
16969 StartLoc, LParenLoc, ModifierLoc, EndLoc);
16970}
16971
16972ExprResult SemaOpenMP::VerifyPositiveIntegerConstantInClause(
16973 Expr *E, OpenMPClauseKind CKind, bool StrictlyPositive,
16974 bool SuppressExprDiags) {
16975 if (!E)
16976 return ExprError();
16977 if (E->isValueDependent() || E->isTypeDependent() ||
16978 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
16979 return E;
16980
16981 llvm::APSInt Result;
16982 ExprResult ICE;
16983 if (SuppressExprDiags) {
16984 // Use a custom diagnoser that suppresses 'note' diagnostics about the
16985 // expression.
16986 struct SuppressedDiagnoser : public Sema::VerifyICEDiagnoser {
16987 SuppressedDiagnoser() : VerifyICEDiagnoser(/*Suppress=*/true) {}
16988 SemaBase::SemaDiagnosticBuilder
16989 diagnoseNotICE(Sema &S, SourceLocation Loc) override {
16990 llvm_unreachable("Diagnostic suppressed");
16991 }
16992 } Diagnoser;
16993 ICE = SemaRef.VerifyIntegerConstantExpression(E, Result: &Result, Diagnoser,
16994 CanFold: AllowFoldKind::Allow);
16995 } else {
16996 ICE =
16997 SemaRef.VerifyIntegerConstantExpression(E, Result: &Result,
16998 /*FIXME*/ CanFold: AllowFoldKind::Allow);
16999 }
17000 if (ICE.isInvalid())
17001 return ExprError();
17002
17003 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
17004 (!StrictlyPositive && !Result.isNonNegative())) {
17005 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_negative_expression_in_clause)
17006 << getOpenMPClauseNameForDiag(C: CKind) << (StrictlyPositive ? 1 : 0)
17007 << E->getSourceRange();
17008 return ExprError();
17009 }
17010 if ((CKind == OMPC_aligned || CKind == OMPC_align ||
17011 CKind == OMPC_allocate) &&
17012 !Result.isPowerOf2()) {
17013 Diag(Loc: E->getExprLoc(), DiagID: diag::warn_omp_alignment_not_power_of_two)
17014 << E->getSourceRange();
17015 return ExprError();
17016 }
17017
17018 if (!Result.isRepresentableByInt64()) {
17019 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_large_expression_in_clause)
17020 << getOpenMPClauseNameForDiag(C: CKind) << E->getSourceRange();
17021 return ExprError();
17022 }
17023
17024 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
17025 DSAStack->setAssociatedLoops(Result.getExtValue());
17026 else if (CKind == OMPC_ordered)
17027 DSAStack->setAssociatedLoops(Result.getExtValue());
17028 return ICE;
17029}
17030
17031void SemaOpenMP::setOpenMPDeviceNum(int Num) { DeviceNum = Num; }
17032
17033void SemaOpenMP::setOpenMPDeviceNumID(StringRef ID) { DeviceNumID = ID; }
17034
17035int SemaOpenMP::getOpenMPDeviceNum() const { return DeviceNum; }
17036
17037void SemaOpenMP::ActOnOpenMPDeviceNum(Expr *DeviceNumExpr) {
17038 llvm::APSInt Result;
17039 Expr::EvalResult EvalResult;
17040 // Evaluate the expression to an integer value
17041 if (!DeviceNumExpr->isValueDependent() &&
17042 DeviceNumExpr->EvaluateAsInt(Result&: EvalResult, Ctx: SemaRef.Context)) {
17043 // The device expression must evaluate to a non-negative integer value.
17044 Result = EvalResult.Val.getInt();
17045 if (Result.isNonNegative()) {
17046 setOpenMPDeviceNum(Result.getZExtValue());
17047 } else {
17048 Diag(Loc: DeviceNumExpr->getExprLoc(),
17049 DiagID: diag::err_omp_negative_expression_in_clause)
17050 << "device_num" << 0 << DeviceNumExpr->getSourceRange();
17051 }
17052 } else if (auto *DeclRef = dyn_cast<DeclRefExpr>(Val: DeviceNumExpr)) {
17053 // Check if the expression is an identifier
17054 IdentifierInfo *IdInfo = DeclRef->getDecl()->getIdentifier();
17055 if (IdInfo) {
17056 setOpenMPDeviceNumID(IdInfo->getName());
17057 }
17058 } else {
17059 Diag(Loc: DeviceNumExpr->getExprLoc(), DiagID: diag::err_expected_expression);
17060 }
17061}
17062
17063OMPClause *SemaOpenMP::ActOnOpenMPSafelenClause(Expr *Len,
17064 SourceLocation StartLoc,
17065 SourceLocation LParenLoc,
17066 SourceLocation EndLoc) {
17067 // OpenMP [2.8.1, simd construct, Description]
17068 // The parameter of the safelen clause must be a constant
17069 // positive integer expression.
17070 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_safelen);
17071 if (Safelen.isInvalid())
17072 return nullptr;
17073 return new (getASTContext())
17074 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
17075}
17076
17077OMPClause *SemaOpenMP::ActOnOpenMPSimdlenClause(Expr *Len,
17078 SourceLocation StartLoc,
17079 SourceLocation LParenLoc,
17080 SourceLocation EndLoc) {
17081 // OpenMP [2.8.1, simd construct, Description]
17082 // The parameter of the simdlen clause must be a constant
17083 // positive integer expression.
17084 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(E: Len, CKind: OMPC_simdlen);
17085 if (Simdlen.isInvalid())
17086 return nullptr;
17087 return new (getASTContext())
17088 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
17089}
17090
17091/// Tries to find omp_allocator_handle_t type.
17092static bool findOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
17093 DSAStackTy *Stack) {
17094 if (!Stack->getOMPAllocatorHandleT().isNull())
17095 return true;
17096
17097 // Set the allocator handle type.
17098 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_allocator_handle_t");
17099 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
17100 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
17101 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
17102 << "omp_allocator_handle_t";
17103 return false;
17104 }
17105 QualType AllocatorHandleEnumTy = PT.get();
17106 AllocatorHandleEnumTy.addConst();
17107 Stack->setOMPAllocatorHandleT(AllocatorHandleEnumTy);
17108
17109 // Fill the predefined allocator map.
17110 bool ErrorFound = false;
17111 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
17112 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
17113 StringRef Allocator =
17114 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
17115 DeclarationName AllocatorName = &S.getASTContext().Idents.get(Name: Allocator);
17116 auto *VD = dyn_cast_or_null<ValueDecl>(
17117 Val: S.LookupSingleName(S: S.TUScope, Name: AllocatorName, Loc, NameKind: Sema::LookupAnyName));
17118 if (!VD) {
17119 ErrorFound = true;
17120 break;
17121 }
17122 QualType AllocatorType =
17123 VD->getType().getNonLValueExprType(Context: S.getASTContext());
17124 ExprResult Res = S.BuildDeclRefExpr(D: VD, Ty: AllocatorType, VK: VK_LValue, Loc);
17125 if (!Res.isUsable()) {
17126 ErrorFound = true;
17127 break;
17128 }
17129 Res = S.PerformImplicitConversion(From: Res.get(), ToType: AllocatorHandleEnumTy,
17130 Action: AssignmentAction::Initializing,
17131 /*AllowExplicit=*/true);
17132 if (!Res.isUsable()) {
17133 ErrorFound = true;
17134 break;
17135 }
17136 Stack->setAllocator(AllocatorKind, Allocator: Res.get());
17137 }
17138 if (ErrorFound) {
17139 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found)
17140 << "omp_allocator_handle_t";
17141 return false;
17142 }
17143
17144 return true;
17145}
17146
17147OMPClause *SemaOpenMP::ActOnOpenMPAllocatorClause(Expr *A,
17148 SourceLocation StartLoc,
17149 SourceLocation LParenLoc,
17150 SourceLocation EndLoc) {
17151 // OpenMP [2.11.3, allocate Directive, Description]
17152 // allocator is an expression of omp_allocator_handle_t type.
17153 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: A->getExprLoc(), DSAStack))
17154 return nullptr;
17155
17156 ExprResult Allocator = SemaRef.DefaultLvalueConversion(E: A);
17157 if (Allocator.isInvalid())
17158 return nullptr;
17159 Allocator = SemaRef.PerformImplicitConversion(
17160 From: Allocator.get(), DSAStack->getOMPAllocatorHandleT(),
17161 Action: AssignmentAction::Initializing,
17162 /*AllowExplicit=*/true);
17163 if (Allocator.isInvalid())
17164 return nullptr;
17165 return new (getASTContext())
17166 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
17167}
17168
17169OMPClause *SemaOpenMP::ActOnOpenMPCollapseClause(Expr *NumForLoops,
17170 SourceLocation StartLoc,
17171 SourceLocation LParenLoc,
17172 SourceLocation EndLoc) {
17173 // OpenMP [2.7.1, loop construct, Description]
17174 // OpenMP [2.8.1, simd construct, Description]
17175 // OpenMP [2.9.6, distribute construct, Description]
17176 // The parameter of the collapse clause must be a constant
17177 // positive integer expression.
17178 ExprResult NumForLoopsResult =
17179 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_collapse);
17180 if (NumForLoopsResult.isInvalid())
17181 return nullptr;
17182 return new (getASTContext())
17183 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
17184}
17185
17186OMPClause *SemaOpenMP::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
17187 SourceLocation EndLoc,
17188 SourceLocation LParenLoc,
17189 Expr *NumForLoops) {
17190 // OpenMP [2.7.1, loop construct, Description]
17191 // OpenMP [2.8.1, simd construct, Description]
17192 // OpenMP [2.9.6, distribute construct, Description]
17193 // The parameter of the ordered clause must be a constant
17194 // positive integer expression if any.
17195 if (NumForLoops && LParenLoc.isValid()) {
17196 ExprResult NumForLoopsResult =
17197 VerifyPositiveIntegerConstantInClause(E: NumForLoops, CKind: OMPC_ordered);
17198 if (NumForLoopsResult.isInvalid())
17199 return nullptr;
17200 NumForLoops = NumForLoopsResult.get();
17201 } else {
17202 NumForLoops = nullptr;
17203 }
17204 auto *Clause =
17205 OMPOrderedClause::Create(C: getASTContext(), Num: NumForLoops,
17206 NumLoops: NumForLoops ? DSAStack->getAssociatedLoops() : 0,
17207 StartLoc, LParenLoc, EndLoc);
17208 DSAStack->setOrderedRegion(/*IsOrdered=*/true, Param: NumForLoops, Clause);
17209 return Clause;
17210}
17211
17212OMPClause *SemaOpenMP::ActOnOpenMPSimpleClause(
17213 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
17214 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17215 OMPClause *Res = nullptr;
17216 switch (Kind) {
17217 case OMPC_proc_bind:
17218 Res = ActOnOpenMPProcBindClause(Kind: static_cast<ProcBindKind>(Argument),
17219 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17220 break;
17221 case OMPC_atomic_default_mem_order:
17222 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
17223 Kind: static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
17224 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17225 break;
17226 case OMPC_fail:
17227 Res = ActOnOpenMPFailClause(Kind: static_cast<OpenMPClauseKind>(Argument),
17228 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17229 break;
17230 case OMPC_update:
17231 Res = ActOnOpenMPUpdateClause(Kind: static_cast<OpenMPDependClauseKind>(Argument),
17232 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17233 break;
17234 case OMPC_bind:
17235 Res = ActOnOpenMPBindClause(Kind: static_cast<OpenMPBindClauseKind>(Argument),
17236 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17237 break;
17238 case OMPC_at:
17239 Res = ActOnOpenMPAtClause(Kind: static_cast<OpenMPAtClauseKind>(Argument),
17240 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17241 break;
17242 case OMPC_severity:
17243 Res = ActOnOpenMPSeverityClause(
17244 Kind: static_cast<OpenMPSeverityClauseKind>(Argument), KindLoc: ArgumentLoc, StartLoc,
17245 LParenLoc, EndLoc);
17246 break;
17247 case OMPC_threadset:
17248 Res = ActOnOpenMPThreadsetClause(Kind: static_cast<OpenMPThreadsetKind>(Argument),
17249 KindLoc: ArgumentLoc, StartLoc, LParenLoc, EndLoc);
17250 break;
17251 case OMPC_if:
17252 case OMPC_final:
17253 case OMPC_num_threads:
17254 case OMPC_safelen:
17255 case OMPC_simdlen:
17256 case OMPC_sizes:
17257 case OMPC_allocator:
17258 case OMPC_collapse:
17259 case OMPC_schedule:
17260 case OMPC_private:
17261 case OMPC_firstprivate:
17262 case OMPC_lastprivate:
17263 case OMPC_shared:
17264 case OMPC_reduction:
17265 case OMPC_task_reduction:
17266 case OMPC_in_reduction:
17267 case OMPC_linear:
17268 case OMPC_aligned:
17269 case OMPC_copyin:
17270 case OMPC_copyprivate:
17271 case OMPC_ordered:
17272 case OMPC_nowait:
17273 case OMPC_untied:
17274 case OMPC_mergeable:
17275 case OMPC_threadprivate:
17276 case OMPC_groupprivate:
17277 case OMPC_allocate:
17278 case OMPC_flush:
17279 case OMPC_depobj:
17280 case OMPC_read:
17281 case OMPC_write:
17282 case OMPC_capture:
17283 case OMPC_compare:
17284 case OMPC_seq_cst:
17285 case OMPC_acq_rel:
17286 case OMPC_acquire:
17287 case OMPC_release:
17288 case OMPC_relaxed:
17289 case OMPC_depend:
17290 case OMPC_device:
17291 case OMPC_threads:
17292 case OMPC_simd:
17293 case OMPC_map:
17294 case OMPC_num_teams:
17295 case OMPC_thread_limit:
17296 case OMPC_priority:
17297 case OMPC_grainsize:
17298 case OMPC_nogroup:
17299 case OMPC_num_tasks:
17300 case OMPC_hint:
17301 case OMPC_dist_schedule:
17302 case OMPC_default:
17303 case OMPC_defaultmap:
17304 case OMPC_unknown:
17305 case OMPC_uniform:
17306 case OMPC_to:
17307 case OMPC_from:
17308 case OMPC_use_device_ptr:
17309 case OMPC_use_device_addr:
17310 case OMPC_is_device_ptr:
17311 case OMPC_has_device_addr:
17312 case OMPC_unified_address:
17313 case OMPC_unified_shared_memory:
17314 case OMPC_reverse_offload:
17315 case OMPC_dynamic_allocators:
17316 case OMPC_self_maps:
17317 case OMPC_device_type:
17318 case OMPC_match:
17319 case OMPC_nontemporal:
17320 case OMPC_destroy:
17321 case OMPC_novariants:
17322 case OMPC_nocontext:
17323 case OMPC_detach:
17324 case OMPC_inclusive:
17325 case OMPC_exclusive:
17326 case OMPC_uses_allocators:
17327 case OMPC_affinity:
17328 case OMPC_when:
17329 case OMPC_message:
17330 default:
17331 llvm_unreachable("Clause is not allowed.");
17332 }
17333 return Res;
17334}
17335
17336OMPClause *SemaOpenMP::ActOnOpenMPDefaultClause(
17337 llvm::omp::DefaultKind M, SourceLocation MLoc,
17338 OpenMPDefaultClauseVariableCategory VCKind, SourceLocation VCKindLoc,
17339 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17340 if (M == OMP_DEFAULT_unknown) {
17341 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
17342 << getListOfPossibleValues(K: OMPC_default, /*First=*/0,
17343 /*Last=*/unsigned(OMP_DEFAULT_unknown))
17344 << getOpenMPClauseNameForDiag(C: OMPC_default);
17345 return nullptr;
17346 }
17347 if (VCKind == OMPC_DEFAULT_VC_unknown) {
17348 Diag(Loc: VCKindLoc, DiagID: diag::err_omp_default_vc)
17349 << getOpenMPSimpleClauseTypeName(Kind: OMPC_default, Type: unsigned(M));
17350 return nullptr;
17351 }
17352
17353 bool IsTargetDefault =
17354 getLangOpts().OpenMP >= 60 &&
17355 isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective());
17356
17357 // OpenMP 6.0, page 224, lines 3-4 default Clause, Semantics
17358 // If data-sharing-attribute is shared then the clause has no effect
17359 // on a target construct;
17360 if (IsTargetDefault && M == OMP_DEFAULT_shared)
17361 return nullptr;
17362
17363 auto SetDefaultClauseAttrs = [&](llvm::omp::DefaultKind M,
17364 OpenMPDefaultClauseVariableCategory VCKind) {
17365 OpenMPDefaultmapClauseModifier DefMapMod;
17366 OpenMPDefaultmapClauseKind DefMapKind;
17367 // default data-sharing-attribute
17368 switch (M) {
17369 case OMP_DEFAULT_none:
17370 if (IsTargetDefault)
17371 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_none;
17372 else
17373 DSAStack->setDefaultDSANone(MLoc);
17374 break;
17375 case OMP_DEFAULT_firstprivate:
17376 if (IsTargetDefault)
17377 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_firstprivate;
17378 else
17379 DSAStack->setDefaultDSAFirstPrivate(MLoc);
17380 break;
17381 case OMP_DEFAULT_private:
17382 if (IsTargetDefault)
17383 DefMapMod = OMPC_DEFAULTMAP_MODIFIER_private;
17384 else
17385 DSAStack->setDefaultDSAPrivate(MLoc);
17386 break;
17387 case OMP_DEFAULT_shared:
17388 assert(!IsTargetDefault && "DSA shared invalid with target directive");
17389 DSAStack->setDefaultDSAShared(MLoc);
17390 break;
17391 default:
17392 llvm_unreachable("unexpected DSA in OpenMP default clause");
17393 }
17394 // default variable-category
17395 switch (VCKind) {
17396 case OMPC_DEFAULT_VC_aggregate:
17397 if (IsTargetDefault)
17398 DefMapKind = OMPC_DEFAULTMAP_aggregate;
17399 else
17400 DSAStack->setDefaultDSAVCAggregate(VCKindLoc);
17401 break;
17402 case OMPC_DEFAULT_VC_pointer:
17403 if (IsTargetDefault)
17404 DefMapKind = OMPC_DEFAULTMAP_pointer;
17405 else
17406 DSAStack->setDefaultDSAVCPointer(VCKindLoc);
17407 break;
17408 case OMPC_DEFAULT_VC_scalar:
17409 if (IsTargetDefault)
17410 DefMapKind = OMPC_DEFAULTMAP_scalar;
17411 else
17412 DSAStack->setDefaultDSAVCScalar(VCKindLoc);
17413 break;
17414 case OMPC_DEFAULT_VC_all:
17415 if (IsTargetDefault)
17416 DefMapKind = OMPC_DEFAULTMAP_all;
17417 else
17418 DSAStack->setDefaultDSAVCAll(VCKindLoc);
17419 break;
17420 default:
17421 llvm_unreachable("unexpected variable category in OpenMP default clause");
17422 }
17423 // OpenMP 6.0, page 224, lines 4-5 default Clause, Semantics
17424 // otherwise, its effect on a target construct is equivalent to
17425 // specifying the defaultmap clause with the same data-sharing-attribute
17426 // and variable-category.
17427 //
17428 // If earlier than OpenMP 6.0, or not a target directive, the default DSA
17429 // is/was set as before.
17430 if (IsTargetDefault) {
17431 if (DefMapKind == OMPC_DEFAULTMAP_all) {
17432 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_aggregate, Loc: MLoc);
17433 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_scalar, Loc: MLoc);
17434 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: OMPC_DEFAULTMAP_pointer, Loc: MLoc);
17435 } else {
17436 DSAStack->setDefaultDMAAttr(M: DefMapMod, Kind: DefMapKind, Loc: MLoc);
17437 }
17438 }
17439 };
17440
17441 SetDefaultClauseAttrs(M, VCKind);
17442 return new (getASTContext())
17443 OMPDefaultClause(M, MLoc, VCKind, VCKindLoc, StartLoc, LParenLoc, EndLoc);
17444}
17445
17446OMPClause *SemaOpenMP::ActOnOpenMPThreadsetClause(OpenMPThreadsetKind Kind,
17447 SourceLocation KindLoc,
17448 SourceLocation StartLoc,
17449 SourceLocation LParenLoc,
17450 SourceLocation EndLoc) {
17451 if (Kind == OMPC_THREADSET_unknown) {
17452 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
17453 << getListOfPossibleValues(K: OMPC_threadset, /*First=*/0,
17454 /*Last=*/unsigned(OMPC_THREADSET_unknown))
17455 << getOpenMPClauseName(C: OMPC_threadset);
17456 return nullptr;
17457 }
17458
17459 return new (getASTContext())
17460 OMPThreadsetClause(Kind, KindLoc, StartLoc, LParenLoc, EndLoc);
17461}
17462
17463static OMPClause *createTransparentClause(Sema &SemaRef, ASTContext &Ctx,
17464 Expr *ImpexTypeArg,
17465 SourceLocation StartLoc,
17466 SourceLocation LParenLoc,
17467 SourceLocation EndLoc) {
17468 ExprResult ER = SemaRef.DefaultLvalueConversion(E: ImpexTypeArg);
17469 if (ER.isInvalid())
17470 return nullptr;
17471
17472 return new (Ctx) OMPTransparentClause(ER.get(), StartLoc, LParenLoc, EndLoc);
17473}
17474
17475OMPClause *SemaOpenMP::ActOnOpenMPTransparentClause(Expr *ImpexTypeArg,
17476 SourceLocation StartLoc,
17477 SourceLocation LParenLoc,
17478 SourceLocation EndLoc) {
17479 if (!ImpexTypeArg) {
17480 return new (getASTContext())
17481 OMPTransparentClause(ImpexTypeArg, StartLoc, LParenLoc, EndLoc);
17482 }
17483 QualType Ty = ImpexTypeArg->getType();
17484
17485 if (const auto *TT = Ty->getAs<TypedefType>()) {
17486 const TypedefNameDecl *TypedefDecl = TT->getDecl();
17487 llvm::StringRef TypedefName = TypedefDecl->getName();
17488 IdentifierInfo &II = SemaRef.PP.getIdentifierTable().get(Name: TypedefName);
17489 ParsedType ImpexTy =
17490 SemaRef.getTypeName(II, NameLoc: StartLoc, S: SemaRef.getCurScope());
17491 if (!ImpexTy.getAsOpaquePtr() || ImpexTy.get().isNull()) {
17492 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_implied_type_not_found)
17493 << TypedefName;
17494 return nullptr;
17495 }
17496 return createTransparentClause(SemaRef, Ctx&: getASTContext(), ImpexTypeArg,
17497 StartLoc, LParenLoc, EndLoc);
17498 }
17499
17500 if (Ty->isEnumeralType())
17501 return createTransparentClause(SemaRef, Ctx&: getASTContext(), ImpexTypeArg,
17502 StartLoc, LParenLoc, EndLoc);
17503
17504 if (Ty->isIntegerType()) {
17505 if (isNonNegativeIntegerValue(ValExpr&: ImpexTypeArg, SemaRef, CKind: OMPC_transparent,
17506 /*StrictlyPositive=*/false)) {
17507 ExprResult Value =
17508 SemaRef.OpenMP().PerformOpenMPImplicitIntegerConversion(Loc: StartLoc,
17509 Op: ImpexTypeArg);
17510 if (std::optional<llvm::APSInt> Result =
17511 Value.get()->getIntegerConstantExpr(Ctx: SemaRef.Context)) {
17512 if (Result->isNegative() ||
17513 Result >
17514 static_cast<int64_t>(SemaOpenMP::OpenMPImpexType::OMP_Export))
17515 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_transparent_invalid_value);
17516 }
17517 return createTransparentClause(SemaRef, Ctx&: getASTContext(), ImpexTypeArg,
17518 StartLoc, LParenLoc, EndLoc);
17519 }
17520 }
17521 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_transparent_invalid_type) << Ty;
17522 return nullptr;
17523}
17524
17525OMPClause *SemaOpenMP::ActOnOpenMPProcBindClause(ProcBindKind Kind,
17526 SourceLocation KindKwLoc,
17527 SourceLocation StartLoc,
17528 SourceLocation LParenLoc,
17529 SourceLocation EndLoc) {
17530 if (Kind == OMP_PROC_BIND_unknown) {
17531 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17532 << getListOfPossibleValues(K: OMPC_proc_bind,
17533 /*First=*/unsigned(OMP_PROC_BIND_master),
17534 /*Last=*/
17535 unsigned(getLangOpts().OpenMP > 50
17536 ? OMP_PROC_BIND_primary
17537 : OMP_PROC_BIND_spread) +
17538 1)
17539 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
17540 return nullptr;
17541 }
17542 if (Kind == OMP_PROC_BIND_primary && getLangOpts().OpenMP < 51)
17543 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17544 << getListOfPossibleValues(K: OMPC_proc_bind,
17545 /*First=*/unsigned(OMP_PROC_BIND_master),
17546 /*Last=*/
17547 unsigned(OMP_PROC_BIND_spread) + 1)
17548 << getOpenMPClauseNameForDiag(C: OMPC_proc_bind);
17549 return new (getASTContext())
17550 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17551}
17552
17553OMPClause *SemaOpenMP::ActOnOpenMPAtomicDefaultMemOrderClause(
17554 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
17555 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
17556 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
17557 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17558 << getListOfPossibleValues(
17559 K: OMPC_atomic_default_mem_order, /*First=*/0,
17560 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
17561 << getOpenMPClauseNameForDiag(C: OMPC_atomic_default_mem_order);
17562 return nullptr;
17563 }
17564 return new (getASTContext()) OMPAtomicDefaultMemOrderClause(
17565 Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17566}
17567
17568OMPClause *SemaOpenMP::ActOnOpenMPAtClause(OpenMPAtClauseKind Kind,
17569 SourceLocation KindKwLoc,
17570 SourceLocation StartLoc,
17571 SourceLocation LParenLoc,
17572 SourceLocation EndLoc) {
17573 if (Kind == OMPC_AT_unknown) {
17574 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17575 << getListOfPossibleValues(K: OMPC_at, /*First=*/0,
17576 /*Last=*/OMPC_AT_unknown)
17577 << getOpenMPClauseNameForDiag(C: OMPC_at);
17578 return nullptr;
17579 }
17580 return new (getASTContext())
17581 OMPAtClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17582}
17583
17584OMPClause *SemaOpenMP::ActOnOpenMPSeverityClause(OpenMPSeverityClauseKind Kind,
17585 SourceLocation KindKwLoc,
17586 SourceLocation StartLoc,
17587 SourceLocation LParenLoc,
17588 SourceLocation EndLoc) {
17589 if (Kind == OMPC_SEVERITY_unknown) {
17590 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17591 << getListOfPossibleValues(K: OMPC_severity, /*First=*/0,
17592 /*Last=*/OMPC_SEVERITY_unknown)
17593 << getOpenMPClauseNameForDiag(C: OMPC_severity);
17594 return nullptr;
17595 }
17596 return new (getASTContext())
17597 OMPSeverityClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
17598}
17599
17600OMPClause *SemaOpenMP::ActOnOpenMPMessageClause(Expr *ME,
17601 SourceLocation StartLoc,
17602 SourceLocation LParenLoc,
17603 SourceLocation EndLoc) {
17604 assert(ME && "NULL expr in Message clause");
17605 QualType Type = ME->getType();
17606 if ((!Type->isPointerType() && !Type->isArrayType()) ||
17607 !Type->getPointeeOrArrayElementType()->isAnyCharacterType()) {
17608 Diag(Loc: ME->getBeginLoc(), DiagID: diag::warn_clause_expected_string)
17609 << getOpenMPClauseNameForDiag(C: OMPC_message) << 0;
17610 return nullptr;
17611 }
17612
17613 Stmt *HelperValStmt = nullptr;
17614
17615 // Depending on whether this clause appears in an executable context or not,
17616 // we may or may not build a capture.
17617 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
17618 OpenMPDirectiveKind CaptureRegion =
17619 DKind == OMPD_unknown ? OMPD_unknown
17620 : getOpenMPCaptureRegionForClause(
17621 DKind, CKind: OMPC_message, OpenMPVersion: getLangOpts().OpenMP);
17622 if (CaptureRegion != OMPD_unknown &&
17623 !SemaRef.CurContext->isDependentContext()) {
17624 ME = SemaRef.MakeFullExpr(Arg: ME).get();
17625 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
17626 ME = tryBuildCapture(SemaRef, Capture: ME, Captures).get();
17627 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
17628 }
17629
17630 // Convert array type to pointer type if needed.
17631 ME = SemaRef.DefaultFunctionArrayLvalueConversion(E: ME).get();
17632
17633 return new (getASTContext()) OMPMessageClause(
17634 ME, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
17635}
17636
17637OMPClause *SemaOpenMP::ActOnOpenMPOrderClause(
17638 OpenMPOrderClauseModifier Modifier, OpenMPOrderClauseKind Kind,
17639 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
17640 SourceLocation KindLoc, SourceLocation EndLoc) {
17641 if (Kind != OMPC_ORDER_concurrent ||
17642 (getLangOpts().OpenMP < 51 && MLoc.isValid())) {
17643 // Kind should be concurrent,
17644 // Modifiers introduced in OpenMP 5.1
17645 static_assert(OMPC_ORDER_unknown > 0,
17646 "OMPC_ORDER_unknown not greater than 0");
17647
17648 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
17649 << getListOfPossibleValues(K: OMPC_order,
17650 /*First=*/0,
17651 /*Last=*/OMPC_ORDER_unknown)
17652 << getOpenMPClauseNameForDiag(C: OMPC_order);
17653 return nullptr;
17654 }
17655 if (getLangOpts().OpenMP >= 51 && Modifier == OMPC_ORDER_MODIFIER_unknown &&
17656 MLoc.isValid()) {
17657 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
17658 << getListOfPossibleValues(K: OMPC_order,
17659 /*First=*/OMPC_ORDER_MODIFIER_unknown + 1,
17660 /*Last=*/OMPC_ORDER_MODIFIER_last)
17661 << getOpenMPClauseNameForDiag(C: OMPC_order);
17662 } else if (getLangOpts().OpenMP >= 50) {
17663 DSAStack->setRegionHasOrderConcurrent(/*HasOrderConcurrent=*/true);
17664 if (DSAStack->getCurScope()) {
17665 // mark the current scope with 'order' flag
17666 unsigned existingFlags = DSAStack->getCurScope()->getFlags();
17667 DSAStack->getCurScope()->setFlags(existingFlags |
17668 Scope::OpenMPOrderClauseScope);
17669 }
17670 }
17671 return new (getASTContext()) OMPOrderClause(
17672 Kind, KindLoc, StartLoc, LParenLoc, EndLoc, Modifier, MLoc);
17673}
17674
17675OMPClause *SemaOpenMP::ActOnOpenMPUpdateClause(OpenMPDependClauseKind Kind,
17676 SourceLocation KindKwLoc,
17677 SourceLocation StartLoc,
17678 SourceLocation LParenLoc,
17679 SourceLocation EndLoc) {
17680 if (Kind == OMPC_DEPEND_unknown || Kind == OMPC_DEPEND_source ||
17681 Kind == OMPC_DEPEND_sink || Kind == OMPC_DEPEND_depobj) {
17682 SmallVector<unsigned> Except = {
17683 OMPC_DEPEND_source, OMPC_DEPEND_sink, OMPC_DEPEND_depobj,
17684 OMPC_DEPEND_outallmemory, OMPC_DEPEND_inoutallmemory};
17685 if (getLangOpts().OpenMP < 51)
17686 Except.push_back(Elt: OMPC_DEPEND_inoutset);
17687 Diag(Loc: KindKwLoc, DiagID: diag::err_omp_unexpected_clause_value)
17688 << getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
17689 /*Last=*/OMPC_DEPEND_unknown, Exclude: Except)
17690 << getOpenMPClauseNameForDiag(C: OMPC_update);
17691 return nullptr;
17692 }
17693 return OMPUpdateClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17694 ArgumentLoc: KindKwLoc, DK: Kind, EndLoc);
17695}
17696
17697OMPClause *SemaOpenMP::ActOnOpenMPSizesClause(ArrayRef<Expr *> SizeExprs,
17698 SourceLocation StartLoc,
17699 SourceLocation LParenLoc,
17700 SourceLocation EndLoc) {
17701 SmallVector<Expr *> SanitizedSizeExprs(SizeExprs);
17702
17703 for (Expr *&SizeExpr : SanitizedSizeExprs) {
17704 // Skip if already sanitized, e.g. during a partial template instantiation.
17705 if (!SizeExpr)
17706 continue;
17707
17708 bool IsValid = isNonNegativeIntegerValue(ValExpr&: SizeExpr, SemaRef, CKind: OMPC_sizes,
17709 /*StrictlyPositive=*/true);
17710
17711 // isNonNegativeIntegerValue returns true for non-integral types (but still
17712 // emits error diagnostic), so check for the expected type explicitly.
17713 QualType SizeTy = SizeExpr->getType();
17714 if (!SizeTy->isIntegerType())
17715 IsValid = false;
17716
17717 // Handling in templates is tricky. There are four possibilities to
17718 // consider:
17719 //
17720 // 1a. The expression is valid and we are in a instantiated template or not
17721 // in a template:
17722 // Pass valid expression to be further analysed later in Sema.
17723 // 1b. The expression is valid and we are in a template (including partial
17724 // instantiation):
17725 // isNonNegativeIntegerValue skipped any checks so there is no
17726 // guarantee it will be correct after instantiation.
17727 // ActOnOpenMPSizesClause will be called again at instantiation when
17728 // it is not in a dependent context anymore. This may cause warnings
17729 // to be emitted multiple times.
17730 // 2a. The expression is invalid and we are in an instantiated template or
17731 // not in a template:
17732 // Invalidate the expression with a clearly wrong value (nullptr) so
17733 // later in Sema we do not have to do the same validity analysis again
17734 // or crash from unexpected data. Error diagnostics have already been
17735 // emitted.
17736 // 2b. The expression is invalid and we are in a template (including partial
17737 // instantiation):
17738 // Pass the invalid expression as-is, template instantiation may
17739 // replace unexpected types/values with valid ones. The directives
17740 // with this clause must not try to use these expressions in dependent
17741 // contexts, but delay analysis until full instantiation.
17742 if (!SizeExpr->isInstantiationDependent() && !IsValid)
17743 SizeExpr = nullptr;
17744 }
17745
17746 return OMPSizesClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
17747 Sizes: SanitizedSizeExprs);
17748}
17749
17750OMPClause *SemaOpenMP::ActOnOpenMPPermutationClause(ArrayRef<Expr *> PermExprs,
17751 SourceLocation StartLoc,
17752 SourceLocation LParenLoc,
17753 SourceLocation EndLoc) {
17754 size_t NumLoops = PermExprs.size();
17755 SmallVector<Expr *> SanitizedPermExprs;
17756 llvm::append_range(C&: SanitizedPermExprs, R&: PermExprs);
17757
17758 for (Expr *&PermExpr : SanitizedPermExprs) {
17759 // Skip if template-dependent or already sanitized, e.g. during a partial
17760 // template instantiation.
17761 if (!PermExpr || PermExpr->isInstantiationDependent())
17762 continue;
17763
17764 llvm::APSInt PermVal;
17765 ExprResult PermEvalExpr = SemaRef.VerifyIntegerConstantExpression(
17766 E: PermExpr, Result: &PermVal, CanFold: AllowFoldKind::Allow);
17767 bool IsValid = PermEvalExpr.isUsable();
17768 if (IsValid)
17769 PermExpr = PermEvalExpr.get();
17770
17771 if (IsValid && (PermVal < 1 || NumLoops < PermVal)) {
17772 SourceRange ExprRange(PermEvalExpr.get()->getBeginLoc(),
17773 PermEvalExpr.get()->getEndLoc());
17774 Diag(Loc: PermEvalExpr.get()->getExprLoc(),
17775 DiagID: diag::err_omp_interchange_permutation_value_range)
17776 << NumLoops << ExprRange;
17777 IsValid = false;
17778 }
17779
17780 if (!PermExpr->isInstantiationDependent() && !IsValid)
17781 PermExpr = nullptr;
17782 }
17783
17784 return OMPPermutationClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17785 EndLoc, Args: SanitizedPermExprs);
17786}
17787
17788OMPClause *SemaOpenMP::ActOnOpenMPFullClause(SourceLocation StartLoc,
17789 SourceLocation EndLoc) {
17790 return OMPFullClause::Create(C: getASTContext(), StartLoc, EndLoc);
17791}
17792
17793OMPClause *SemaOpenMP::ActOnOpenMPPartialClause(Expr *FactorExpr,
17794 SourceLocation StartLoc,
17795 SourceLocation LParenLoc,
17796 SourceLocation EndLoc) {
17797 if (FactorExpr) {
17798 // If an argument is specified, it must be a constant (or an unevaluated
17799 // template expression).
17800 ExprResult FactorResult = VerifyPositiveIntegerConstantInClause(
17801 E: FactorExpr, CKind: OMPC_partial, /*StrictlyPositive=*/true);
17802 if (FactorResult.isInvalid())
17803 return nullptr;
17804 FactorExpr = FactorResult.get();
17805 }
17806
17807 return OMPPartialClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
17808 Factor: FactorExpr);
17809}
17810
17811OMPClause *SemaOpenMP::ActOnOpenMPLoopRangeClause(
17812 Expr *First, Expr *Count, SourceLocation StartLoc, SourceLocation LParenLoc,
17813 SourceLocation FirstLoc, SourceLocation CountLoc, SourceLocation EndLoc) {
17814
17815 // OpenMP [6.0, Restrictions]
17816 // First and Count must be integer expressions with positive value
17817 ExprResult FirstVal =
17818 VerifyPositiveIntegerConstantInClause(E: First, CKind: OMPC_looprange);
17819 if (FirstVal.isInvalid())
17820 First = nullptr;
17821
17822 ExprResult CountVal =
17823 VerifyPositiveIntegerConstantInClause(E: Count, CKind: OMPC_looprange);
17824 if (CountVal.isInvalid())
17825 Count = nullptr;
17826
17827 // OpenMP [6.0, Restrictions]
17828 // first + count - 1 must not evaluate to a value greater than the
17829 // loop sequence length of the associated canonical loop sequence.
17830 // This check must be performed afterwards due to the delayed
17831 // parsing and computation of the associated loop sequence
17832 return OMPLoopRangeClause::Create(C: getASTContext(), StartLoc, LParenLoc,
17833 FirstLoc, CountLoc, EndLoc, First, Count);
17834}
17835
17836OMPClause *SemaOpenMP::ActOnOpenMPAlignClause(Expr *A, SourceLocation StartLoc,
17837 SourceLocation LParenLoc,
17838 SourceLocation EndLoc) {
17839 ExprResult AlignVal;
17840 AlignVal = VerifyPositiveIntegerConstantInClause(E: A, CKind: OMPC_align);
17841 if (AlignVal.isInvalid())
17842 return nullptr;
17843 return OMPAlignClause::Create(C: getASTContext(), A: AlignVal.get(), StartLoc,
17844 LParenLoc, EndLoc);
17845}
17846
17847OMPClause *SemaOpenMP::ActOnOpenMPSingleExprWithArgClause(
17848 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
17849 SourceLocation StartLoc, SourceLocation LParenLoc,
17850 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
17851 SourceLocation EndLoc) {
17852 OMPClause *Res = nullptr;
17853 switch (Kind) {
17854 case OMPC_schedule: {
17855 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
17856 assert(Argument.size() == NumberOfElements &&
17857 ArgumentLoc.size() == NumberOfElements);
17858 Res = ActOnOpenMPScheduleClause(
17859 M1: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
17860 M2: static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
17861 Kind: static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), ChunkSize: Expr,
17862 StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1], M2Loc: ArgumentLoc[Modifier2],
17863 KindLoc: ArgumentLoc[ScheduleKind], CommaLoc: DelimLoc, EndLoc);
17864 break;
17865 }
17866 case OMPC_if:
17867 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
17868 Res = ActOnOpenMPIfClause(NameModifier: static_cast<OpenMPDirectiveKind>(Argument.back()),
17869 Condition: Expr, StartLoc, LParenLoc, NameModifierLoc: ArgumentLoc.back(),
17870 ColonLoc: DelimLoc, EndLoc);
17871 break;
17872 case OMPC_dist_schedule:
17873 Res = ActOnOpenMPDistScheduleClause(
17874 Kind: static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), ChunkSize: Expr,
17875 StartLoc, LParenLoc, KindLoc: ArgumentLoc.back(), CommaLoc: DelimLoc, EndLoc);
17876 break;
17877 case OMPC_default:
17878 enum { DefaultModifier, DefaultVarCategory };
17879 Res = ActOnOpenMPDefaultClause(
17880 M: static_cast<llvm::omp::DefaultKind>(Argument[DefaultModifier]),
17881 MLoc: ArgumentLoc[DefaultModifier],
17882 VCKind: static_cast<OpenMPDefaultClauseVariableCategory>(
17883 Argument[DefaultVarCategory]),
17884 VCKindLoc: ArgumentLoc[DefaultVarCategory], StartLoc, LParenLoc, EndLoc);
17885 break;
17886 case OMPC_defaultmap:
17887 enum { Modifier, DefaultmapKind };
17888 Res = ActOnOpenMPDefaultmapClause(
17889 M: static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
17890 Kind: static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
17891 StartLoc, LParenLoc, MLoc: ArgumentLoc[Modifier], KindLoc: ArgumentLoc[DefaultmapKind],
17892 EndLoc);
17893 break;
17894 case OMPC_order:
17895 enum { OrderModifier, OrderKind };
17896 Res = ActOnOpenMPOrderClause(
17897 Modifier: static_cast<OpenMPOrderClauseModifier>(Argument[OrderModifier]),
17898 Kind: static_cast<OpenMPOrderClauseKind>(Argument[OrderKind]), StartLoc,
17899 LParenLoc, MLoc: ArgumentLoc[OrderModifier], KindLoc: ArgumentLoc[OrderKind], EndLoc);
17900 break;
17901 case OMPC_device:
17902 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
17903 Res = ActOnOpenMPDeviceClause(
17904 Modifier: static_cast<OpenMPDeviceClauseModifier>(Argument.back()), Device: Expr,
17905 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
17906 break;
17907 case OMPC_grainsize:
17908 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
17909 "Modifier for grainsize clause and its location are expected.");
17910 Res = ActOnOpenMPGrainsizeClause(
17911 Modifier: static_cast<OpenMPGrainsizeClauseModifier>(Argument.back()), Size: Expr,
17912 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
17913 break;
17914 case OMPC_num_tasks:
17915 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
17916 "Modifier for num_tasks clause and its location are expected.");
17917 Res = ActOnOpenMPNumTasksClause(
17918 Modifier: static_cast<OpenMPNumTasksClauseModifier>(Argument.back()), NumTasks: Expr,
17919 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
17920 break;
17921 case OMPC_dyn_groupprivate: {
17922 enum { Modifier1, Modifier2, NumberOfElements };
17923 assert(Argument.size() == NumberOfElements &&
17924 ArgumentLoc.size() == NumberOfElements &&
17925 "Modifiers for dyn_groupprivate clause and their locations are "
17926 "expected.");
17927 Res = ActOnOpenMPDynGroupprivateClause(
17928 M1: static_cast<OpenMPDynGroupprivateClauseModifier>(Argument[Modifier1]),
17929 M2: static_cast<OpenMPDynGroupprivateClauseFallbackModifier>(
17930 Argument[Modifier2]),
17931 Size: Expr, StartLoc, LParenLoc, M1Loc: ArgumentLoc[Modifier1],
17932 M2Loc: ArgumentLoc[Modifier2], EndLoc);
17933 break;
17934 }
17935 case OMPC_num_threads:
17936 assert(Argument.size() == 1 && ArgumentLoc.size() == 1 &&
17937 "Modifier for num_threads clause and its location are expected.");
17938 Res = ActOnOpenMPNumThreadsClause(
17939 Modifier: static_cast<OpenMPNumThreadsClauseModifier>(Argument.back()), NumThreads: Expr,
17940 StartLoc, LParenLoc, ModifierLoc: ArgumentLoc.back(), EndLoc);
17941 break;
17942 case OMPC_final:
17943 case OMPC_safelen:
17944 case OMPC_simdlen:
17945 case OMPC_sizes:
17946 case OMPC_allocator:
17947 case OMPC_collapse:
17948 case OMPC_proc_bind:
17949 case OMPC_private:
17950 case OMPC_firstprivate:
17951 case OMPC_lastprivate:
17952 case OMPC_shared:
17953 case OMPC_reduction:
17954 case OMPC_task_reduction:
17955 case OMPC_in_reduction:
17956 case OMPC_linear:
17957 case OMPC_aligned:
17958 case OMPC_copyin:
17959 case OMPC_copyprivate:
17960 case OMPC_ordered:
17961 case OMPC_nowait:
17962 case OMPC_untied:
17963 case OMPC_mergeable:
17964 case OMPC_threadprivate:
17965 case OMPC_groupprivate:
17966 case OMPC_allocate:
17967 case OMPC_flush:
17968 case OMPC_depobj:
17969 case OMPC_read:
17970 case OMPC_write:
17971 case OMPC_update:
17972 case OMPC_capture:
17973 case OMPC_compare:
17974 case OMPC_seq_cst:
17975 case OMPC_acq_rel:
17976 case OMPC_acquire:
17977 case OMPC_release:
17978 case OMPC_relaxed:
17979 case OMPC_depend:
17980 case OMPC_threads:
17981 case OMPC_simd:
17982 case OMPC_map:
17983 case OMPC_num_teams:
17984 case OMPC_thread_limit:
17985 case OMPC_priority:
17986 case OMPC_nogroup:
17987 case OMPC_hint:
17988 case OMPC_unknown:
17989 case OMPC_uniform:
17990 case OMPC_to:
17991 case OMPC_from:
17992 case OMPC_use_device_ptr:
17993 case OMPC_use_device_addr:
17994 case OMPC_is_device_ptr:
17995 case OMPC_has_device_addr:
17996 case OMPC_unified_address:
17997 case OMPC_unified_shared_memory:
17998 case OMPC_reverse_offload:
17999 case OMPC_dynamic_allocators:
18000 case OMPC_atomic_default_mem_order:
18001 case OMPC_self_maps:
18002 case OMPC_device_type:
18003 case OMPC_match:
18004 case OMPC_nontemporal:
18005 case OMPC_at:
18006 case OMPC_severity:
18007 case OMPC_message:
18008 case OMPC_destroy:
18009 case OMPC_novariants:
18010 case OMPC_nocontext:
18011 case OMPC_detach:
18012 case OMPC_inclusive:
18013 case OMPC_exclusive:
18014 case OMPC_uses_allocators:
18015 case OMPC_affinity:
18016 case OMPC_when:
18017 case OMPC_bind:
18018 default:
18019 llvm_unreachable("Clause is not allowed.");
18020 }
18021 return Res;
18022}
18023
18024static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
18025 OpenMPScheduleClauseModifier M2,
18026 SourceLocation M1Loc, SourceLocation M2Loc) {
18027 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
18028 SmallVector<unsigned, 2> Excluded;
18029 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
18030 Excluded.push_back(Elt: M2);
18031 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
18032 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_monotonic);
18033 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
18034 Excluded.push_back(Elt: OMPC_SCHEDULE_MODIFIER_nonmonotonic);
18035 S.Diag(Loc: M1Loc, DiagID: diag::err_omp_unexpected_clause_value)
18036 << getListOfPossibleValues(K: OMPC_schedule,
18037 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
18038 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
18039 Exclude: Excluded)
18040 << getOpenMPClauseNameForDiag(C: OMPC_schedule);
18041 return true;
18042 }
18043 return false;
18044}
18045
18046OMPClause *SemaOpenMP::ActOnOpenMPScheduleClause(
18047 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
18048 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
18049 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
18050 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
18051 if (checkScheduleModifiers(S&: SemaRef, M1, M2, M1Loc, M2Loc) ||
18052 checkScheduleModifiers(S&: SemaRef, M1: M2, M2: M1, M1Loc: M2Loc, M2Loc: M1Loc))
18053 return nullptr;
18054 // OpenMP, 2.7.1, Loop Construct, Restrictions
18055 // Either the monotonic modifier or the nonmonotonic modifier can be specified
18056 // but not both.
18057 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
18058 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
18059 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
18060 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
18061 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
18062 Diag(Loc: M2Loc, DiagID: diag::err_omp_unexpected_schedule_modifier)
18063 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M2)
18064 << getOpenMPSimpleClauseTypeName(Kind: OMPC_schedule, Type: M1);
18065 return nullptr;
18066 }
18067 if (Kind == OMPC_SCHEDULE_unknown) {
18068 std::string Values;
18069 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
18070 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
18071 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
18072 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
18073 Exclude);
18074 } else {
18075 Values = getListOfPossibleValues(K: OMPC_schedule, /*First=*/0,
18076 /*Last=*/OMPC_SCHEDULE_unknown);
18077 }
18078 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
18079 << Values << getOpenMPClauseNameForDiag(C: OMPC_schedule);
18080 return nullptr;
18081 }
18082 // OpenMP, 2.7.1, Loop Construct, Restrictions
18083 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
18084 // schedule(guided).
18085 // OpenMP 5.0 does not have this restriction.
18086 if (getLangOpts().OpenMP < 50 &&
18087 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
18088 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
18089 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
18090 Diag(Loc: M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
18091 DiagID: diag::err_omp_schedule_nonmonotonic_static);
18092 return nullptr;
18093 }
18094 Expr *ValExpr = ChunkSize;
18095 Stmt *HelperValStmt = nullptr;
18096 if (ChunkSize) {
18097 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
18098 !ChunkSize->isInstantiationDependent() &&
18099 !ChunkSize->containsUnexpandedParameterPack()) {
18100 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
18101 ExprResult Val =
18102 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
18103 if (Val.isInvalid())
18104 return nullptr;
18105
18106 ValExpr = Val.get();
18107
18108 // OpenMP [2.7.1, Restrictions]
18109 // chunk_size must be a loop invariant integer expression with a positive
18110 // value.
18111 if (std::optional<llvm::APSInt> Result =
18112 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
18113 if (Result->isSigned() && !Result->isStrictlyPositive()) {
18114 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
18115 << "schedule" << 1 << ChunkSize->getSourceRange();
18116 return nullptr;
18117 }
18118 } else if (getOpenMPCaptureRegionForClause(
18119 DSAStack->getCurrentDirective(), CKind: OMPC_schedule,
18120 OpenMPVersion: getLangOpts().OpenMP) != OMPD_unknown &&
18121 !SemaRef.CurContext->isDependentContext()) {
18122 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18123 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18124 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18125 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18126 }
18127 }
18128 }
18129
18130 return new (getASTContext())
18131 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
18132 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
18133}
18134
18135OMPClause *SemaOpenMP::ActOnOpenMPClause(OpenMPClauseKind Kind,
18136 SourceLocation StartLoc,
18137 SourceLocation EndLoc) {
18138 OMPClause *Res = nullptr;
18139 switch (Kind) {
18140 case OMPC_ordered:
18141 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
18142 break;
18143 case OMPC_nowait:
18144 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc,
18145 /*LParenLoc=*/SourceLocation(),
18146 /*Condition=*/nullptr);
18147 break;
18148 case OMPC_untied:
18149 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
18150 break;
18151 case OMPC_mergeable:
18152 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
18153 break;
18154 case OMPC_read:
18155 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
18156 break;
18157 case OMPC_write:
18158 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
18159 break;
18160 case OMPC_update:
18161 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
18162 break;
18163 case OMPC_capture:
18164 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
18165 break;
18166 case OMPC_compare:
18167 Res = ActOnOpenMPCompareClause(StartLoc, EndLoc);
18168 break;
18169 case OMPC_fail:
18170 Res = ActOnOpenMPFailClause(StartLoc, EndLoc);
18171 break;
18172 case OMPC_seq_cst:
18173 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
18174 break;
18175 case OMPC_acq_rel:
18176 Res = ActOnOpenMPAcqRelClause(StartLoc, EndLoc);
18177 break;
18178 case OMPC_acquire:
18179 Res = ActOnOpenMPAcquireClause(StartLoc, EndLoc);
18180 break;
18181 case OMPC_release:
18182 Res = ActOnOpenMPReleaseClause(StartLoc, EndLoc);
18183 break;
18184 case OMPC_relaxed:
18185 Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
18186 break;
18187 case OMPC_weak:
18188 Res = ActOnOpenMPWeakClause(StartLoc, EndLoc);
18189 break;
18190 case OMPC_threads:
18191 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
18192 break;
18193 case OMPC_simd:
18194 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
18195 break;
18196 case OMPC_nogroup:
18197 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
18198 break;
18199 case OMPC_unified_address:
18200 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
18201 break;
18202 case OMPC_unified_shared_memory:
18203 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
18204 break;
18205 case OMPC_reverse_offload:
18206 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
18207 break;
18208 case OMPC_dynamic_allocators:
18209 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
18210 break;
18211 case OMPC_self_maps:
18212 Res = ActOnOpenMPSelfMapsClause(StartLoc, EndLoc);
18213 break;
18214 case OMPC_destroy:
18215 Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc,
18216 /*LParenLoc=*/SourceLocation(),
18217 /*VarLoc=*/SourceLocation(), EndLoc);
18218 break;
18219 case OMPC_full:
18220 Res = ActOnOpenMPFullClause(StartLoc, EndLoc);
18221 break;
18222 case OMPC_partial:
18223 Res = ActOnOpenMPPartialClause(FactorExpr: nullptr, StartLoc, /*LParenLoc=*/{}, EndLoc);
18224 break;
18225 case OMPC_ompx_bare:
18226 Res = ActOnOpenMPXBareClause(StartLoc, EndLoc);
18227 break;
18228 case OMPC_if:
18229 case OMPC_final:
18230 case OMPC_num_threads:
18231 case OMPC_safelen:
18232 case OMPC_simdlen:
18233 case OMPC_sizes:
18234 case OMPC_allocator:
18235 case OMPC_collapse:
18236 case OMPC_schedule:
18237 case OMPC_private:
18238 case OMPC_firstprivate:
18239 case OMPC_lastprivate:
18240 case OMPC_shared:
18241 case OMPC_reduction:
18242 case OMPC_task_reduction:
18243 case OMPC_in_reduction:
18244 case OMPC_linear:
18245 case OMPC_aligned:
18246 case OMPC_copyin:
18247 case OMPC_copyprivate:
18248 case OMPC_default:
18249 case OMPC_proc_bind:
18250 case OMPC_threadprivate:
18251 case OMPC_groupprivate:
18252 case OMPC_allocate:
18253 case OMPC_flush:
18254 case OMPC_depobj:
18255 case OMPC_depend:
18256 case OMPC_device:
18257 case OMPC_map:
18258 case OMPC_num_teams:
18259 case OMPC_thread_limit:
18260 case OMPC_priority:
18261 case OMPC_grainsize:
18262 case OMPC_num_tasks:
18263 case OMPC_hint:
18264 case OMPC_dist_schedule:
18265 case OMPC_defaultmap:
18266 case OMPC_unknown:
18267 case OMPC_uniform:
18268 case OMPC_to:
18269 case OMPC_from:
18270 case OMPC_use_device_ptr:
18271 case OMPC_use_device_addr:
18272 case OMPC_is_device_ptr:
18273 case OMPC_has_device_addr:
18274 case OMPC_atomic_default_mem_order:
18275 case OMPC_device_type:
18276 case OMPC_match:
18277 case OMPC_nontemporal:
18278 case OMPC_order:
18279 case OMPC_at:
18280 case OMPC_severity:
18281 case OMPC_message:
18282 case OMPC_novariants:
18283 case OMPC_nocontext:
18284 case OMPC_detach:
18285 case OMPC_inclusive:
18286 case OMPC_exclusive:
18287 case OMPC_uses_allocators:
18288 case OMPC_affinity:
18289 case OMPC_when:
18290 case OMPC_ompx_dyn_cgroup_mem:
18291 case OMPC_dyn_groupprivate:
18292 default:
18293 llvm_unreachable("Clause is not allowed.");
18294 }
18295 return Res;
18296}
18297
18298OMPClause *SemaOpenMP::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
18299 SourceLocation EndLoc,
18300 SourceLocation LParenLoc,
18301 Expr *Condition) {
18302 Expr *ValExpr = Condition;
18303 if (Condition && LParenLoc.isValid()) {
18304 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18305 !Condition->isInstantiationDependent() &&
18306 !Condition->containsUnexpandedParameterPack()) {
18307 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18308 if (Val.isInvalid())
18309 return nullptr;
18310
18311 ValExpr = Val.get();
18312 }
18313 }
18314 DSAStack->setNowaitRegion();
18315 return new (getASTContext())
18316 OMPNowaitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
18317}
18318
18319OMPClause *SemaOpenMP::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
18320 SourceLocation EndLoc) {
18321 DSAStack->setUntiedRegion();
18322 return new (getASTContext()) OMPUntiedClause(StartLoc, EndLoc);
18323}
18324
18325OMPClause *SemaOpenMP::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
18326 SourceLocation EndLoc) {
18327 return new (getASTContext()) OMPMergeableClause(StartLoc, EndLoc);
18328}
18329
18330OMPClause *SemaOpenMP::ActOnOpenMPReadClause(SourceLocation StartLoc,
18331 SourceLocation EndLoc) {
18332 return new (getASTContext()) OMPReadClause(StartLoc, EndLoc);
18333}
18334
18335OMPClause *SemaOpenMP::ActOnOpenMPWriteClause(SourceLocation StartLoc,
18336 SourceLocation EndLoc) {
18337 return new (getASTContext()) OMPWriteClause(StartLoc, EndLoc);
18338}
18339
18340OMPClause *SemaOpenMP::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
18341 SourceLocation EndLoc) {
18342 return OMPUpdateClause::Create(C: getASTContext(), StartLoc, EndLoc);
18343}
18344
18345OMPClause *SemaOpenMP::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
18346 SourceLocation EndLoc) {
18347 return new (getASTContext()) OMPCaptureClause(StartLoc, EndLoc);
18348}
18349
18350OMPClause *SemaOpenMP::ActOnOpenMPCompareClause(SourceLocation StartLoc,
18351 SourceLocation EndLoc) {
18352 return new (getASTContext()) OMPCompareClause(StartLoc, EndLoc);
18353}
18354
18355OMPClause *SemaOpenMP::ActOnOpenMPFailClause(SourceLocation StartLoc,
18356 SourceLocation EndLoc) {
18357 return new (getASTContext()) OMPFailClause(StartLoc, EndLoc);
18358}
18359
18360OMPClause *SemaOpenMP::ActOnOpenMPFailClause(OpenMPClauseKind Parameter,
18361 SourceLocation KindLoc,
18362 SourceLocation StartLoc,
18363 SourceLocation LParenLoc,
18364 SourceLocation EndLoc) {
18365
18366 if (!checkFailClauseParameter(FailClauseParameter: Parameter)) {
18367 Diag(Loc: KindLoc, DiagID: diag::err_omp_atomic_fail_wrong_or_no_clauses);
18368 return nullptr;
18369 }
18370 return new (getASTContext())
18371 OMPFailClause(Parameter, KindLoc, StartLoc, LParenLoc, EndLoc);
18372}
18373
18374OMPClause *SemaOpenMP::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
18375 SourceLocation EndLoc) {
18376 return new (getASTContext()) OMPSeqCstClause(StartLoc, EndLoc);
18377}
18378
18379OMPClause *SemaOpenMP::ActOnOpenMPAcqRelClause(SourceLocation StartLoc,
18380 SourceLocation EndLoc) {
18381 return new (getASTContext()) OMPAcqRelClause(StartLoc, EndLoc);
18382}
18383
18384OMPClause *SemaOpenMP::ActOnOpenMPAcquireClause(SourceLocation StartLoc,
18385 SourceLocation EndLoc) {
18386 return new (getASTContext()) OMPAcquireClause(StartLoc, EndLoc);
18387}
18388
18389OMPClause *SemaOpenMP::ActOnOpenMPReleaseClause(SourceLocation StartLoc,
18390 SourceLocation EndLoc) {
18391 return new (getASTContext()) OMPReleaseClause(StartLoc, EndLoc);
18392}
18393
18394OMPClause *SemaOpenMP::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
18395 SourceLocation EndLoc) {
18396 return new (getASTContext()) OMPRelaxedClause(StartLoc, EndLoc);
18397}
18398
18399OMPClause *SemaOpenMP::ActOnOpenMPWeakClause(SourceLocation StartLoc,
18400 SourceLocation EndLoc) {
18401 return new (getASTContext()) OMPWeakClause(StartLoc, EndLoc);
18402}
18403
18404OMPClause *SemaOpenMP::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
18405 SourceLocation EndLoc) {
18406 return new (getASTContext()) OMPThreadsClause(StartLoc, EndLoc);
18407}
18408
18409OMPClause *SemaOpenMP::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
18410 SourceLocation EndLoc) {
18411 return new (getASTContext()) OMPSIMDClause(StartLoc, EndLoc);
18412}
18413
18414OMPClause *SemaOpenMP::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
18415 SourceLocation EndLoc) {
18416 return new (getASTContext()) OMPNogroupClause(StartLoc, EndLoc);
18417}
18418
18419OMPClause *SemaOpenMP::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
18420 SourceLocation EndLoc) {
18421 return new (getASTContext()) OMPUnifiedAddressClause(StartLoc, EndLoc);
18422}
18423
18424OMPClause *
18425SemaOpenMP::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
18426 SourceLocation EndLoc) {
18427 return new (getASTContext()) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
18428}
18429
18430OMPClause *SemaOpenMP::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
18431 SourceLocation EndLoc) {
18432 return new (getASTContext()) OMPReverseOffloadClause(StartLoc, EndLoc);
18433}
18434
18435OMPClause *
18436SemaOpenMP::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
18437 SourceLocation EndLoc) {
18438 return new (getASTContext()) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
18439}
18440
18441OMPClause *SemaOpenMP::ActOnOpenMPSelfMapsClause(SourceLocation StartLoc,
18442 SourceLocation EndLoc) {
18443 return new (getASTContext()) OMPSelfMapsClause(StartLoc, EndLoc);
18444}
18445
18446StmtResult
18447SemaOpenMP::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses,
18448 SourceLocation StartLoc,
18449 SourceLocation EndLoc) {
18450
18451 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18452 // At least one action-clause must appear on a directive.
18453 if (!hasClauses(Clauses, K: OMPC_init, ClauseTypes: OMPC_use, ClauseTypes: OMPC_destroy, ClauseTypes: OMPC_nowait)) {
18454 unsigned OMPVersion = getLangOpts().OpenMP;
18455 StringRef Expected = "'init', 'use', 'destroy', or 'nowait'";
18456 Diag(Loc: StartLoc, DiagID: diag::err_omp_no_clause_for_directive)
18457 << Expected << getOpenMPDirectiveName(D: OMPD_interop, Ver: OMPVersion);
18458 return StmtError();
18459 }
18460
18461 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18462 // A depend clause can only appear on the directive if a targetsync
18463 // interop-type is present or the interop-var was initialized with
18464 // the targetsync interop-type.
18465
18466 // If there is any 'init' clause diagnose if there is no 'init' clause with
18467 // interop-type of 'targetsync'. Cases involving other directives cannot be
18468 // diagnosed.
18469 const OMPDependClause *DependClause = nullptr;
18470 bool HasInitClause = false;
18471 bool IsTargetSync = false;
18472 for (const OMPClause *C : Clauses) {
18473 if (IsTargetSync)
18474 break;
18475 if (const auto *InitClause = dyn_cast<OMPInitClause>(Val: C)) {
18476 HasInitClause = true;
18477 if (InitClause->getIsTargetSync())
18478 IsTargetSync = true;
18479 } else if (const auto *DC = dyn_cast<OMPDependClause>(Val: C)) {
18480 DependClause = DC;
18481 }
18482 }
18483 if (DependClause && HasInitClause && !IsTargetSync) {
18484 Diag(Loc: DependClause->getBeginLoc(), DiagID: diag::err_omp_interop_bad_depend_clause);
18485 return StmtError();
18486 }
18487
18488 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18489 // Each interop-var may be specified for at most one action-clause of each
18490 // interop construct.
18491 llvm::SmallPtrSet<const ValueDecl *, 4> InteropVars;
18492 for (OMPClause *C : Clauses) {
18493 OpenMPClauseKind ClauseKind = C->getClauseKind();
18494 std::pair<ValueDecl *, bool> DeclResult;
18495 SourceLocation ELoc;
18496 SourceRange ERange;
18497
18498 if (ClauseKind == OMPC_init) {
18499 auto *E = cast<OMPInitClause>(Val: C)->getInteropVar();
18500 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18501 } else if (ClauseKind == OMPC_use) {
18502 auto *E = cast<OMPUseClause>(Val: C)->getInteropVar();
18503 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18504 } else if (ClauseKind == OMPC_destroy) {
18505 auto *E = cast<OMPDestroyClause>(Val: C)->getInteropVar();
18506 DeclResult = getPrivateItem(S&: SemaRef, RefExpr&: E, ELoc, ERange);
18507 }
18508
18509 if (DeclResult.first) {
18510 if (!InteropVars.insert(Ptr: DeclResult.first).second) {
18511 Diag(Loc: ELoc, DiagID: diag::err_omp_interop_var_multiple_actions)
18512 << DeclResult.first;
18513 return StmtError();
18514 }
18515 }
18516 }
18517
18518 return OMPInteropDirective::Create(C: getASTContext(), StartLoc, EndLoc,
18519 Clauses);
18520}
18521
18522static bool isValidInteropVariable(Sema &SemaRef, Expr *InteropVarExpr,
18523 SourceLocation VarLoc,
18524 OpenMPClauseKind Kind) {
18525 SourceLocation ELoc;
18526 SourceRange ERange;
18527 Expr *RefExpr = InteropVarExpr;
18528 auto Res = getPrivateItem(S&: SemaRef, RefExpr, ELoc, ERange,
18529 /*AllowArraySection=*/false,
18530 /*AllowAssumedSizeArray=*/false,
18531 /*DiagType=*/"omp_interop_t");
18532
18533 if (Res.second) {
18534 // It will be analyzed later.
18535 return true;
18536 }
18537
18538 if (!Res.first)
18539 return false;
18540
18541 // Interop variable should be of type omp_interop_t.
18542 bool HasError = false;
18543 QualType InteropType;
18544 LookupResult Result(SemaRef, &SemaRef.Context.Idents.get(Name: "omp_interop_t"),
18545 VarLoc, Sema::LookupOrdinaryName);
18546 if (SemaRef.LookupName(R&: Result, S: SemaRef.getCurScope())) {
18547 NamedDecl *ND = Result.getFoundDecl();
18548 if (const auto *TD = dyn_cast<TypeDecl>(Val: ND)) {
18549 InteropType = QualType(TD->getTypeForDecl(), 0);
18550 } else {
18551 HasError = true;
18552 }
18553 } else {
18554 HasError = true;
18555 }
18556
18557 if (HasError) {
18558 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_implied_type_not_found)
18559 << "omp_interop_t";
18560 return false;
18561 }
18562
18563 QualType VarType = InteropVarExpr->getType().getUnqualifiedType();
18564 if (!SemaRef.Context.hasSameType(T1: InteropType, T2: VarType)) {
18565 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_wrong_type);
18566 return false;
18567 }
18568
18569 // OpenMP 5.1 [2.15.1, interop Construct, Restrictions]
18570 // The interop-var passed to init or destroy must be non-const.
18571 if ((Kind == OMPC_init || Kind == OMPC_destroy) &&
18572 isConstNotMutableType(SemaRef, Type: InteropVarExpr->getType())) {
18573 SemaRef.Diag(Loc: VarLoc, DiagID: diag::err_omp_interop_variable_expected)
18574 << /*non-const*/ 1;
18575 return false;
18576 }
18577 return true;
18578}
18579
18580OMPClause *SemaOpenMP::ActOnOpenMPInitClause(
18581 Expr *InteropVar, OMPInteropInfo &InteropInfo, SourceLocation StartLoc,
18582 SourceLocation LParenLoc, SourceLocation VarLoc, SourceLocation EndLoc) {
18583
18584 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_init))
18585 return nullptr;
18586
18587 // Check prefer_type values. These foreign-runtime-id values are either
18588 // string literals or constant integral expressions.
18589 for (const Expr *E : InteropInfo.PreferTypes) {
18590 if (E->isValueDependent() || E->isTypeDependent() ||
18591 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
18592 continue;
18593 if (E->isIntegerConstantExpr(Ctx: getASTContext()))
18594 continue;
18595 if (isa<StringLiteral>(Val: E))
18596 continue;
18597 Diag(Loc: E->getExprLoc(), DiagID: diag::err_omp_interop_prefer_type);
18598 return nullptr;
18599 }
18600
18601 return OMPInitClause::Create(C: getASTContext(), InteropVar, InteropInfo,
18602 StartLoc, LParenLoc, VarLoc, EndLoc);
18603}
18604
18605OMPClause *SemaOpenMP::ActOnOpenMPUseClause(Expr *InteropVar,
18606 SourceLocation StartLoc,
18607 SourceLocation LParenLoc,
18608 SourceLocation VarLoc,
18609 SourceLocation EndLoc) {
18610
18611 if (!isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_use))
18612 return nullptr;
18613
18614 return new (getASTContext())
18615 OMPUseClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
18616}
18617
18618OMPClause *SemaOpenMP::ActOnOpenMPDestroyClause(Expr *InteropVar,
18619 SourceLocation StartLoc,
18620 SourceLocation LParenLoc,
18621 SourceLocation VarLoc,
18622 SourceLocation EndLoc) {
18623 if (!InteropVar && getLangOpts().OpenMP >= 52 &&
18624 DSAStack->getCurrentDirective() == OMPD_depobj) {
18625 unsigned OMPVersion = getLangOpts().OpenMP;
18626 Diag(Loc: StartLoc, DiagID: diag::err_omp_expected_clause_argument)
18627 << getOpenMPClauseNameForDiag(C: OMPC_destroy)
18628 << getOpenMPDirectiveName(D: OMPD_depobj, Ver: OMPVersion);
18629 return nullptr;
18630 }
18631 if (InteropVar &&
18632 !isValidInteropVariable(SemaRef, InteropVarExpr: InteropVar, VarLoc, Kind: OMPC_destroy))
18633 return nullptr;
18634
18635 return new (getASTContext())
18636 OMPDestroyClause(InteropVar, StartLoc, LParenLoc, VarLoc, EndLoc);
18637}
18638
18639OMPClause *SemaOpenMP::ActOnOpenMPNovariantsClause(Expr *Condition,
18640 SourceLocation StartLoc,
18641 SourceLocation LParenLoc,
18642 SourceLocation EndLoc) {
18643 Expr *ValExpr = Condition;
18644 Stmt *HelperValStmt = nullptr;
18645 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18646 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18647 !Condition->isInstantiationDependent() &&
18648 !Condition->containsUnexpandedParameterPack()) {
18649 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18650 if (Val.isInvalid())
18651 return nullptr;
18652
18653 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
18654
18655 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18656 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_novariants,
18657 OpenMPVersion: getLangOpts().OpenMP);
18658 if (CaptureRegion != OMPD_unknown &&
18659 !SemaRef.CurContext->isDependentContext()) {
18660 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18661 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18662 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18663 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18664 }
18665 }
18666
18667 return new (getASTContext()) OMPNovariantsClause(
18668 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18669}
18670
18671OMPClause *SemaOpenMP::ActOnOpenMPNocontextClause(Expr *Condition,
18672 SourceLocation StartLoc,
18673 SourceLocation LParenLoc,
18674 SourceLocation EndLoc) {
18675 Expr *ValExpr = Condition;
18676 Stmt *HelperValStmt = nullptr;
18677 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
18678 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
18679 !Condition->isInstantiationDependent() &&
18680 !Condition->containsUnexpandedParameterPack()) {
18681 ExprResult Val = SemaRef.CheckBooleanCondition(Loc: StartLoc, E: Condition);
18682 if (Val.isInvalid())
18683 return nullptr;
18684
18685 ValExpr = SemaRef.MakeFullExpr(Arg: Val.get()).get();
18686
18687 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18688 CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_nocontext,
18689 OpenMPVersion: getLangOpts().OpenMP);
18690 if (CaptureRegion != OMPD_unknown &&
18691 !SemaRef.CurContext->isDependentContext()) {
18692 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18693 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18694 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18695 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18696 }
18697 }
18698
18699 return new (getASTContext()) OMPNocontextClause(
18700 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18701}
18702
18703OMPClause *SemaOpenMP::ActOnOpenMPFilterClause(Expr *ThreadID,
18704 SourceLocation StartLoc,
18705 SourceLocation LParenLoc,
18706 SourceLocation EndLoc) {
18707 Expr *ValExpr = ThreadID;
18708 Stmt *HelperValStmt = nullptr;
18709
18710 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
18711 OpenMPDirectiveKind CaptureRegion =
18712 getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_filter, OpenMPVersion: getLangOpts().OpenMP);
18713 if (CaptureRegion != OMPD_unknown &&
18714 !SemaRef.CurContext->isDependentContext()) {
18715 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
18716 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
18717 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
18718 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
18719 }
18720
18721 return new (getASTContext()) OMPFilterClause(
18722 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
18723}
18724
18725OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
18726 ArrayRef<Expr *> VarList,
18727 const OMPVarListLocTy &Locs,
18728 OpenMPVarListDataTy &Data) {
18729 SourceLocation StartLoc = Locs.StartLoc;
18730 SourceLocation LParenLoc = Locs.LParenLoc;
18731 SourceLocation EndLoc = Locs.EndLoc;
18732 OMPClause *Res = nullptr;
18733 int ExtraModifier = Data.ExtraModifier;
18734 int OriginalSharingModifier = Data.OriginalSharingModifier;
18735 SourceLocation ExtraModifierLoc = Data.ExtraModifierLoc;
18736 SourceLocation ColonLoc = Data.ColonLoc;
18737 switch (Kind) {
18738 case OMPC_private:
18739 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18740 break;
18741 case OMPC_firstprivate:
18742 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18743 break;
18744 case OMPC_lastprivate:
18745 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LASTPRIVATE_unknown &&
18746 "Unexpected lastprivate modifier.");
18747 Res = ActOnOpenMPLastprivateClause(
18748 VarList, LPKind: static_cast<OpenMPLastprivateModifier>(ExtraModifier),
18749 LPKindLoc: ExtraModifierLoc, ColonLoc, StartLoc, LParenLoc, EndLoc);
18750 break;
18751 case OMPC_shared:
18752 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
18753 break;
18754 case OMPC_reduction:
18755 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_REDUCTION_unknown &&
18756 "Unexpected lastprivate modifier.");
18757 Res = ActOnOpenMPReductionClause(
18758 VarList,
18759 Modifiers: OpenMPVarListDataTy::OpenMPReductionClauseModifiers(
18760 ExtraModifier, OriginalSharingModifier),
18761 StartLoc, LParenLoc, ModifierLoc: ExtraModifierLoc, ColonLoc, EndLoc,
18762 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18763 break;
18764 case OMPC_task_reduction:
18765 Res = ActOnOpenMPTaskReductionClause(
18766 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
18767 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18768 break;
18769 case OMPC_in_reduction:
18770 Res = ActOnOpenMPInReductionClause(
18771 VarList, StartLoc, LParenLoc, ColonLoc, EndLoc,
18772 ReductionIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, ReductionId: Data.ReductionOrMapperId);
18773 break;
18774 case OMPC_linear:
18775 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_LINEAR_unknown &&
18776 "Unexpected linear modifier.");
18777 Res = ActOnOpenMPLinearClause(
18778 VarList, Step: Data.DepModOrTailExpr, StartLoc, LParenLoc,
18779 LinKind: static_cast<OpenMPLinearClauseKind>(ExtraModifier), LinLoc: ExtraModifierLoc,
18780 ColonLoc, StepModifierLoc: Data.StepModifierLoc, EndLoc);
18781 break;
18782 case OMPC_aligned:
18783 Res = ActOnOpenMPAlignedClause(VarList, Alignment: Data.DepModOrTailExpr, StartLoc,
18784 LParenLoc, ColonLoc, EndLoc);
18785 break;
18786 case OMPC_copyin:
18787 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
18788 break;
18789 case OMPC_copyprivate:
18790 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
18791 break;
18792 case OMPC_flush:
18793 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
18794 break;
18795 case OMPC_depend:
18796 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_DEPEND_unknown &&
18797 "Unexpected depend modifier.");
18798 Res = ActOnOpenMPDependClause(
18799 Data: {.DepKind: static_cast<OpenMPDependClauseKind>(ExtraModifier), .DepLoc: ExtraModifierLoc,
18800 .ColonLoc: ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc},
18801 DepModifier: Data.DepModOrTailExpr, VarList, StartLoc, LParenLoc, EndLoc);
18802 break;
18803 case OMPC_map:
18804 assert(0 <= ExtraModifier && ExtraModifier <= OMPC_MAP_unknown &&
18805 "Unexpected map modifier.");
18806 Res = ActOnOpenMPMapClause(
18807 IteratorModifier: Data.IteratorExpr, MapTypeModifiers: Data.MapTypeModifiers, MapTypeModifiersLoc: Data.MapTypeModifiersLoc,
18808 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId,
18809 MapType: static_cast<OpenMPMapClauseKind>(ExtraModifier), IsMapTypeImplicit: Data.IsMapTypeImplicit,
18810 MapLoc: ExtraModifierLoc, ColonLoc, VarList, Locs);
18811 break;
18812 case OMPC_to:
18813 Res = ActOnOpenMPToClause(
18814 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
18815 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
18816 VarList, Locs);
18817 break;
18818 case OMPC_from:
18819 Res = ActOnOpenMPFromClause(
18820 MotionModifiers: Data.MotionModifiers, MotionModifiersLoc: Data.MotionModifiersLoc, IteratorModifier: Data.IteratorExpr,
18821 MapperIdScopeSpec&: Data.ReductionOrMapperIdScopeSpec, MapperId&: Data.ReductionOrMapperId, ColonLoc,
18822 VarList, Locs);
18823 break;
18824 case OMPC_use_device_ptr:
18825 assert(0 <= Data.ExtraModifier &&
18826 Data.ExtraModifier <= OMPC_USE_DEVICE_PTR_FALLBACK_unknown &&
18827 "Unexpected use_device_ptr fallback modifier.");
18828 Res = ActOnOpenMPUseDevicePtrClause(
18829 VarList, Locs,
18830 FallbackModifier: static_cast<OpenMPUseDevicePtrFallbackModifier>(Data.ExtraModifier),
18831 FallbackModifierLoc: Data.ExtraModifierLoc);
18832 break;
18833 case OMPC_use_device_addr:
18834 Res = ActOnOpenMPUseDeviceAddrClause(VarList, Locs);
18835 break;
18836 case OMPC_is_device_ptr:
18837 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
18838 break;
18839 case OMPC_has_device_addr:
18840 Res = ActOnOpenMPHasDeviceAddrClause(VarList, Locs);
18841 break;
18842 case OMPC_allocate: {
18843 OpenMPAllocateClauseModifier Modifier1 = OMPC_ALLOCATE_unknown;
18844 OpenMPAllocateClauseModifier Modifier2 = OMPC_ALLOCATE_unknown;
18845 SourceLocation Modifier1Loc, Modifier2Loc;
18846 if (!Data.AllocClauseModifiers.empty()) {
18847 assert(Data.AllocClauseModifiers.size() <= 2 &&
18848 "More allocate modifiers than expected");
18849 Modifier1 = Data.AllocClauseModifiers[0];
18850 Modifier1Loc = Data.AllocClauseModifiersLoc[0];
18851 if (Data.AllocClauseModifiers.size() == 2) {
18852 Modifier2 = Data.AllocClauseModifiers[1];
18853 Modifier2Loc = Data.AllocClauseModifiersLoc[1];
18854 }
18855 }
18856 Res = ActOnOpenMPAllocateClause(
18857 Allocator: Data.DepModOrTailExpr, Alignment: Data.AllocateAlignment, FirstModifier: Modifier1, FirstModifierLoc: Modifier1Loc,
18858 SecondModifier: Modifier2, SecondModifierLoc: Modifier2Loc, VarList, StartLoc, ColonLoc: LParenLoc, LParenLoc: ColonLoc,
18859 EndLoc);
18860 break;
18861 }
18862 case OMPC_nontemporal:
18863 Res = ActOnOpenMPNontemporalClause(VarList, StartLoc, LParenLoc, EndLoc);
18864 break;
18865 case OMPC_inclusive:
18866 Res = ActOnOpenMPInclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
18867 break;
18868 case OMPC_exclusive:
18869 Res = ActOnOpenMPExclusiveClause(VarList, StartLoc, LParenLoc, EndLoc);
18870 break;
18871 case OMPC_affinity:
18872 Res = ActOnOpenMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc,
18873 Modifier: Data.DepModOrTailExpr, Locators: VarList);
18874 break;
18875 case OMPC_doacross:
18876 Res = ActOnOpenMPDoacrossClause(
18877 DepType: static_cast<OpenMPDoacrossClauseModifier>(ExtraModifier),
18878 DepLoc: ExtraModifierLoc, ColonLoc, VarList, StartLoc, LParenLoc, EndLoc);
18879 break;
18880 case OMPC_num_teams:
18881 Res = ActOnOpenMPNumTeamsClause(VarList, StartLoc, LParenLoc, EndLoc);
18882 break;
18883 case OMPC_thread_limit:
18884 Res = ActOnOpenMPThreadLimitClause(VarList, StartLoc, LParenLoc, EndLoc);
18885 break;
18886 case OMPC_if:
18887 case OMPC_depobj:
18888 case OMPC_final:
18889 case OMPC_num_threads:
18890 case OMPC_safelen:
18891 case OMPC_simdlen:
18892 case OMPC_sizes:
18893 case OMPC_allocator:
18894 case OMPC_collapse:
18895 case OMPC_default:
18896 case OMPC_proc_bind:
18897 case OMPC_schedule:
18898 case OMPC_ordered:
18899 case OMPC_nowait:
18900 case OMPC_untied:
18901 case OMPC_mergeable:
18902 case OMPC_threadprivate:
18903 case OMPC_groupprivate:
18904 case OMPC_read:
18905 case OMPC_write:
18906 case OMPC_update:
18907 case OMPC_capture:
18908 case OMPC_compare:
18909 case OMPC_seq_cst:
18910 case OMPC_acq_rel:
18911 case OMPC_acquire:
18912 case OMPC_release:
18913 case OMPC_relaxed:
18914 case OMPC_device:
18915 case OMPC_threads:
18916 case OMPC_simd:
18917 case OMPC_priority:
18918 case OMPC_grainsize:
18919 case OMPC_nogroup:
18920 case OMPC_num_tasks:
18921 case OMPC_hint:
18922 case OMPC_dist_schedule:
18923 case OMPC_defaultmap:
18924 case OMPC_unknown:
18925 case OMPC_uniform:
18926 case OMPC_unified_address:
18927 case OMPC_unified_shared_memory:
18928 case OMPC_reverse_offload:
18929 case OMPC_dynamic_allocators:
18930 case OMPC_atomic_default_mem_order:
18931 case OMPC_self_maps:
18932 case OMPC_device_type:
18933 case OMPC_match:
18934 case OMPC_order:
18935 case OMPC_at:
18936 case OMPC_severity:
18937 case OMPC_message:
18938 case OMPC_destroy:
18939 case OMPC_novariants:
18940 case OMPC_nocontext:
18941 case OMPC_detach:
18942 case OMPC_uses_allocators:
18943 case OMPC_when:
18944 case OMPC_bind:
18945 default:
18946 llvm_unreachable("Clause is not allowed.");
18947 }
18948 return Res;
18949}
18950
18951ExprResult SemaOpenMP::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
18952 ExprObjectKind OK,
18953 SourceLocation Loc) {
18954 ExprResult Res = SemaRef.BuildDeclRefExpr(
18955 D: Capture, Ty: Capture->getType().getNonReferenceType(), VK: VK_LValue, Loc);
18956 if (!Res.isUsable())
18957 return ExprError();
18958 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
18959 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: Loc, Opc: UO_Deref, InputExpr: Res.get());
18960 if (!Res.isUsable())
18961 return ExprError();
18962 }
18963 if (VK != VK_LValue && Res.get()->isGLValue()) {
18964 Res = SemaRef.DefaultLvalueConversion(E: Res.get());
18965 if (!Res.isUsable())
18966 return ExprError();
18967 }
18968 return Res;
18969}
18970
18971OMPClause *SemaOpenMP::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
18972 SourceLocation StartLoc,
18973 SourceLocation LParenLoc,
18974 SourceLocation EndLoc) {
18975 SmallVector<Expr *, 8> Vars;
18976 SmallVector<Expr *, 8> PrivateCopies;
18977 unsigned OMPVersion = getLangOpts().OpenMP;
18978 bool IsImplicitClause =
18979 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
18980 for (Expr *RefExpr : VarList) {
18981 assert(RefExpr && "NULL expr in OpenMP private clause.");
18982 SourceLocation ELoc;
18983 SourceRange ERange;
18984 Expr *SimpleRefExpr = RefExpr;
18985 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
18986 if (Res.second) {
18987 // It will be analyzed later.
18988 Vars.push_back(Elt: RefExpr);
18989 PrivateCopies.push_back(Elt: nullptr);
18990 }
18991 ValueDecl *D = Res.first;
18992 if (!D)
18993 continue;
18994
18995 QualType Type = D->getType();
18996 auto *VD = dyn_cast<VarDecl>(Val: D);
18997
18998 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
18999 // A variable that appears in a private clause must not have an incomplete
19000 // type or a reference type.
19001 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19002 DiagID: diag::err_omp_private_incomplete_type))
19003 continue;
19004 Type = Type.getNonReferenceType();
19005
19006 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
19007 // A variable that is privatized must not have a const-qualified type
19008 // unless it is of class type with a mutable member. This restriction does
19009 // not apply to the firstprivate clause.
19010 //
19011 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
19012 // A variable that appears in a private clause must not have a
19013 // const-qualified type unless it is of class type with a mutable member.
19014 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_private, ELoc))
19015 continue;
19016
19017 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19018 // in a Construct]
19019 // Variables with the predetermined data-sharing attributes may not be
19020 // listed in data-sharing attributes clauses, except for the cases
19021 // listed below. For these exceptions only, listing a predetermined
19022 // variable in a data-sharing attribute clause is allowed and overrides
19023 // the variable's predetermined data-sharing attributes.
19024 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19025 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
19026 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19027 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19028 << getOpenMPClauseNameForDiag(C: OMPC_private);
19029 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19030 continue;
19031 }
19032
19033 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19034 // Variably modified types are not supported for tasks.
19035 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
19036 isOpenMPTaskingDirective(Kind: CurrDir)) {
19037 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
19038 << getOpenMPClauseNameForDiag(C: OMPC_private) << Type
19039 << getOpenMPDirectiveName(D: CurrDir, Ver: OMPVersion);
19040 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19041 VarDecl::DeclarationOnly;
19042 Diag(Loc: D->getLocation(),
19043 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19044 << D;
19045 continue;
19046 }
19047
19048 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19049 // A list item cannot appear in both a map clause and a data-sharing
19050 // attribute clause on the same construct
19051 //
19052 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19053 // A list item cannot appear in both a map clause and a data-sharing
19054 // attribute clause on the same construct unless the construct is a
19055 // combined construct.
19056 if ((getLangOpts().OpenMP <= 45 &&
19057 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
19058 CurrDir == OMPD_target) {
19059 OpenMPClauseKind ConflictKind;
19060 if (DSAStack->checkMappableExprComponentListsForDecl(
19061 VD, /*CurrentRegionOnly=*/true,
19062 Check: [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
19063 OpenMPClauseKind WhereFoundClauseKind) -> bool {
19064 ConflictKind = WhereFoundClauseKind;
19065 return true;
19066 })) {
19067 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
19068 << getOpenMPClauseNameForDiag(C: OMPC_private)
19069 << getOpenMPClauseNameForDiag(C: ConflictKind)
19070 << getOpenMPDirectiveName(D: CurrDir, Ver: OMPVersion);
19071 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19072 continue;
19073 }
19074 }
19075
19076 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
19077 // A variable of class type (or array thereof) that appears in a private
19078 // clause requires an accessible, unambiguous default constructor for the
19079 // class type.
19080 // Generate helper private variable and initialize it with the default
19081 // value. The address of the original variable is replaced by the address of
19082 // the new private variable in CodeGen. This new variable is not added to
19083 // IdResolver, so the code in the OpenMP region uses original variable for
19084 // proper diagnostics.
19085 Type = Type.getUnqualifiedType();
19086 VarDecl *VDPrivate =
19087 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
19088 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
19089 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
19090 SemaRef.ActOnUninitializedDecl(dcl: VDPrivate);
19091 if (VDPrivate->isInvalidDecl())
19092 continue;
19093 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
19094 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
19095
19096 DeclRefExpr *Ref = nullptr;
19097 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19098 auto *FD = dyn_cast<FieldDecl>(Val: D);
19099 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
19100 if (VD)
19101 Ref = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
19102 Loc: RefExpr->getExprLoc());
19103 else
19104 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
19105 }
19106 if (!IsImplicitClause)
19107 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_private, PrivateCopy: Ref);
19108 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19109 ? RefExpr->IgnoreParens()
19110 : Ref);
19111 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
19112 }
19113
19114 if (Vars.empty())
19115 return nullptr;
19116
19117 return OMPPrivateClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
19118 VL: Vars, PrivateVL: PrivateCopies);
19119}
19120
19121OMPClause *SemaOpenMP::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
19122 SourceLocation StartLoc,
19123 SourceLocation LParenLoc,
19124 SourceLocation EndLoc) {
19125 SmallVector<Expr *, 8> Vars;
19126 SmallVector<Expr *, 8> PrivateCopies;
19127 SmallVector<Expr *, 8> Inits;
19128 SmallVector<Decl *, 4> ExprCaptures;
19129 bool IsImplicitClause =
19130 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
19131 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
19132 unsigned OMPVersion = getLangOpts().OpenMP;
19133
19134 for (Expr *RefExpr : VarList) {
19135 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
19136 SourceLocation ELoc;
19137 SourceRange ERange;
19138 Expr *SimpleRefExpr = RefExpr;
19139 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19140 if (Res.second) {
19141 // It will be analyzed later.
19142 Vars.push_back(Elt: RefExpr);
19143 PrivateCopies.push_back(Elt: nullptr);
19144 Inits.push_back(Elt: nullptr);
19145 }
19146 ValueDecl *D = Res.first;
19147 if (!D)
19148 continue;
19149
19150 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
19151 QualType Type = D->getType();
19152 auto *VD = dyn_cast<VarDecl>(Val: D);
19153
19154 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
19155 // A variable that appears in a private clause must not have an incomplete
19156 // type or a reference type.
19157 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19158 DiagID: diag::err_omp_firstprivate_incomplete_type))
19159 continue;
19160 Type = Type.getNonReferenceType();
19161
19162 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
19163 // A variable of class type (or array thereof) that appears in a private
19164 // clause requires an accessible, unambiguous copy constructor for the
19165 // class type.
19166 QualType ElemType =
19167 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
19168
19169 // If an implicit firstprivate variable found it was checked already.
19170 DSAStackTy::DSAVarData TopDVar;
19171 if (!IsImplicitClause) {
19172 DSAStackTy::DSAVarData DVar =
19173 DSAStack->getTopDSA(D, /*FromParent=*/false);
19174 TopDVar = DVar;
19175 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19176 bool IsConstant = ElemType.isConstant(Ctx: getASTContext());
19177 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
19178 // A list item that specifies a given variable may not appear in more
19179 // than one clause on the same directive, except that a variable may be
19180 // specified in both firstprivate and lastprivate clauses.
19181 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
19182 // A list item may appear in a firstprivate or lastprivate clause but not
19183 // both.
19184 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
19185 (isOpenMPDistributeDirective(DKind: CurrDir) ||
19186 DVar.CKind != OMPC_lastprivate) &&
19187 DVar.RefExpr) {
19188 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19189 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19190 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
19191 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19192 continue;
19193 }
19194
19195 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19196 // in a Construct]
19197 // Variables with the predetermined data-sharing attributes may not be
19198 // listed in data-sharing attributes clauses, except for the cases
19199 // listed below. For these exceptions only, listing a predetermined
19200 // variable in a data-sharing attribute clause is allowed and overrides
19201 // the variable's predetermined data-sharing attributes.
19202 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19203 // in a Construct, C/C++, p.2]
19204 // Variables with const-qualified type having no mutable member may be
19205 // listed in a firstprivate clause, even if they are static data members.
19206 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
19207 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
19208 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19209 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19210 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
19211 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19212 continue;
19213 }
19214
19215 // OpenMP [2.9.3.4, Restrictions, p.2]
19216 // A list item that is private within a parallel region must not appear
19217 // in a firstprivate clause on a worksharing construct if any of the
19218 // worksharing regions arising from the worksharing construct ever bind
19219 // to any of the parallel regions arising from the parallel construct.
19220 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
19221 // A list item that is private within a teams region must not appear in a
19222 // firstprivate clause on a distribute construct if any of the distribute
19223 // regions arising from the distribute construct ever bind to any of the
19224 // teams regions arising from the teams construct.
19225 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
19226 // A list item that appears in a reduction clause of a teams construct
19227 // must not appear in a firstprivate clause on a distribute construct if
19228 // any of the distribute regions arising from the distribute construct
19229 // ever bind to any of the teams regions arising from the teams construct.
19230 if ((isOpenMPWorksharingDirective(DKind: CurrDir) ||
19231 isOpenMPDistributeDirective(DKind: CurrDir)) &&
19232 !isOpenMPParallelDirective(DKind: CurrDir) &&
19233 !isOpenMPTeamsDirective(DKind: CurrDir)) {
19234 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
19235 if (DVar.CKind != OMPC_shared &&
19236 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
19237 isOpenMPTeamsDirective(DKind: DVar.DKind) ||
19238 DVar.DKind == OMPD_unknown)) {
19239 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
19240 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
19241 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19242 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19243 continue;
19244 }
19245 }
19246 // OpenMP [2.9.3.4, Restrictions, p.3]
19247 // A list item that appears in a reduction clause of a parallel construct
19248 // must not appear in a firstprivate clause on a worksharing or task
19249 // construct if any of the worksharing or task regions arising from the
19250 // worksharing or task construct ever bind to any of the parallel regions
19251 // arising from the parallel construct.
19252 // OpenMP [2.9.3.4, Restrictions, p.4]
19253 // A list item that appears in a reduction clause in worksharing
19254 // construct must not appear in a firstprivate clause in a task construct
19255 // encountered during execution of any of the worksharing regions arising
19256 // from the worksharing construct.
19257 if (isOpenMPTaskingDirective(Kind: CurrDir)) {
19258 DVar = DSAStack->hasInnermostDSA(
19259 D,
19260 CPred: [](OpenMPClauseKind C, bool AppliedToPointee) {
19261 return C == OMPC_reduction && !AppliedToPointee;
19262 },
19263 DPred: [](OpenMPDirectiveKind K) {
19264 return isOpenMPParallelDirective(DKind: K) ||
19265 isOpenMPWorksharingDirective(DKind: K) ||
19266 isOpenMPTeamsDirective(DKind: K);
19267 },
19268 /*FromParent=*/true);
19269 if (DVar.CKind == OMPC_reduction &&
19270 (isOpenMPParallelDirective(DKind: DVar.DKind) ||
19271 isOpenMPWorksharingDirective(DKind: DVar.DKind) ||
19272 isOpenMPTeamsDirective(DKind: DVar.DKind))) {
19273 Diag(Loc: ELoc, DiagID: diag::err_omp_parallel_reduction_in_task_firstprivate)
19274 << getOpenMPDirectiveName(D: DVar.DKind, Ver: OMPVersion);
19275 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19276 continue;
19277 }
19278 }
19279
19280 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
19281 // A list item cannot appear in both a map clause and a data-sharing
19282 // attribute clause on the same construct
19283 //
19284 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
19285 // A list item cannot appear in both a map clause and a data-sharing
19286 // attribute clause on the same construct unless the construct is a
19287 // combined construct.
19288 if ((getLangOpts().OpenMP <= 45 &&
19289 isOpenMPTargetExecutionDirective(DKind: CurrDir)) ||
19290 CurrDir == OMPD_target) {
19291 OpenMPClauseKind ConflictKind;
19292 if (DSAStack->checkMappableExprComponentListsForDecl(
19293 VD, /*CurrentRegionOnly=*/true,
19294 Check: [&ConflictKind](
19295 OMPClauseMappableExprCommon::MappableExprComponentListRef,
19296 OpenMPClauseKind WhereFoundClauseKind) {
19297 ConflictKind = WhereFoundClauseKind;
19298 return true;
19299 })) {
19300 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
19301 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate)
19302 << getOpenMPClauseNameForDiag(C: ConflictKind)
19303 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
19304 Ver: OMPVersion);
19305 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19306 continue;
19307 }
19308 }
19309 }
19310
19311 // Variably modified types are not supported for tasks.
19312 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
19313 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
19314 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
19315 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate) << Type
19316 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
19317 Ver: OMPVersion);
19318 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19319 VarDecl::DeclarationOnly;
19320 Diag(Loc: D->getLocation(),
19321 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19322 << D;
19323 continue;
19324 }
19325
19326 Type = Type.getUnqualifiedType();
19327 VarDecl *VDPrivate =
19328 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
19329 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
19330 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
19331 // Generate helper private variable and initialize it with the value of the
19332 // original variable. The address of the original variable is replaced by
19333 // the address of the new private variable in the CodeGen. This new variable
19334 // is not added to IdResolver, so the code in the OpenMP region uses
19335 // original variable for proper diagnostics and variable capturing.
19336 Expr *VDInitRefExpr = nullptr;
19337 // For arrays generate initializer for single element and replace it by the
19338 // original array element in CodeGen.
19339 if (Type->isArrayType()) {
19340 VarDecl *VDInit =
19341 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type: ElemType, Name: D->getName());
19342 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: ElemType, Loc: ELoc);
19343 Expr *Init = SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get();
19344 ElemType = ElemType.getUnqualifiedType();
19345 VarDecl *VDInitTemp = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(),
19346 Type: ElemType, Name: ".firstprivate.temp");
19347 InitializedEntity Entity =
19348 InitializedEntity::InitializeVariable(Var: VDInitTemp);
19349 InitializationKind Kind = InitializationKind::CreateCopy(InitLoc: ELoc, EqualLoc: ELoc);
19350
19351 InitializationSequence InitSeq(SemaRef, Entity, Kind, Init);
19352 ExprResult Result = InitSeq.Perform(S&: SemaRef, Entity, Kind, Args: Init);
19353 if (Result.isInvalid())
19354 VDPrivate->setInvalidDecl();
19355 else
19356 VDPrivate->setInit(Result.getAs<Expr>());
19357 // Remove temp variable declaration.
19358 getASTContext().Deallocate(Ptr: VDInitTemp);
19359 } else {
19360 VarDecl *VDInit = buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type,
19361 Name: ".firstprivate.temp");
19362 VDInitRefExpr = buildDeclRefExpr(S&: SemaRef, D: VDInit, Ty: RefExpr->getType(),
19363 Loc: RefExpr->getExprLoc());
19364 SemaRef.AddInitializerToDecl(
19365 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
19366 /*DirectInit=*/false);
19367 }
19368 if (VDPrivate->isInvalidDecl()) {
19369 if (IsImplicitClause) {
19370 Diag(Loc: RefExpr->getExprLoc(),
19371 DiagID: diag::note_omp_task_predetermined_firstprivate_here);
19372 }
19373 continue;
19374 }
19375 SemaRef.CurContext->addDecl(D: VDPrivate);
19376 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
19377 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(),
19378 Loc: RefExpr->getExprLoc());
19379 DeclRefExpr *Ref = nullptr;
19380 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19381 if (TopDVar.CKind == OMPC_lastprivate) {
19382 Ref = TopDVar.PrivateCopy;
19383 } else {
19384 auto *FD = dyn_cast<FieldDecl>(Val: D);
19385 VarDecl *VD = FD ? DSAStack->getImplicitFDCapExprDecl(FD) : nullptr;
19386 if (VD)
19387 Ref =
19388 buildDeclRefExpr(S&: SemaRef, D: VD, Ty: VD->getType().getNonReferenceType(),
19389 Loc: RefExpr->getExprLoc());
19390 else
19391 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
19392 if (VD || !isOpenMPCapturedDecl(D))
19393 ExprCaptures.push_back(Elt: Ref->getDecl());
19394 }
19395 }
19396 if (!IsImplicitClause)
19397 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
19398 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19399 ? RefExpr->IgnoreParens()
19400 : Ref);
19401 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
19402 Inits.push_back(Elt: VDInitRefExpr);
19403 }
19404
19405 if (Vars.empty())
19406 return nullptr;
19407
19408 return OMPFirstprivateClause::Create(
19409 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, PrivateVL: PrivateCopies, InitVL: Inits,
19410 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures));
19411}
19412
19413OMPClause *SemaOpenMP::ActOnOpenMPLastprivateClause(
19414 ArrayRef<Expr *> VarList, OpenMPLastprivateModifier LPKind,
19415 SourceLocation LPKindLoc, SourceLocation ColonLoc, SourceLocation StartLoc,
19416 SourceLocation LParenLoc, SourceLocation EndLoc) {
19417 if (LPKind == OMPC_LASTPRIVATE_unknown && LPKindLoc.isValid()) {
19418 assert(ColonLoc.isValid() && "Colon location must be valid.");
19419 Diag(Loc: LPKindLoc, DiagID: diag::err_omp_unexpected_clause_value)
19420 << getListOfPossibleValues(K: OMPC_lastprivate, /*First=*/0,
19421 /*Last=*/OMPC_LASTPRIVATE_unknown)
19422 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
19423 return nullptr;
19424 }
19425
19426 SmallVector<Expr *, 8> Vars;
19427 SmallVector<Expr *, 8> SrcExprs;
19428 SmallVector<Expr *, 8> DstExprs;
19429 SmallVector<Expr *, 8> AssignmentOps;
19430 SmallVector<Decl *, 4> ExprCaptures;
19431 SmallVector<Expr *, 4> ExprPostUpdates;
19432 for (Expr *RefExpr : VarList) {
19433 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
19434 SourceLocation ELoc;
19435 SourceRange ERange;
19436 Expr *SimpleRefExpr = RefExpr;
19437 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19438 if (Res.second) {
19439 // It will be analyzed later.
19440 Vars.push_back(Elt: RefExpr);
19441 SrcExprs.push_back(Elt: nullptr);
19442 DstExprs.push_back(Elt: nullptr);
19443 AssignmentOps.push_back(Elt: nullptr);
19444 }
19445 ValueDecl *D = Res.first;
19446 if (!D)
19447 continue;
19448
19449 QualType Type = D->getType();
19450 auto *VD = dyn_cast<VarDecl>(Val: D);
19451
19452 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
19453 // A variable that appears in a lastprivate clause must not have an
19454 // incomplete type or a reference type.
19455 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
19456 DiagID: diag::err_omp_lastprivate_incomplete_type))
19457 continue;
19458 Type = Type.getNonReferenceType();
19459
19460 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
19461 // A variable that is privatized must not have a const-qualified type
19462 // unless it is of class type with a mutable member. This restriction does
19463 // not apply to the firstprivate clause.
19464 //
19465 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
19466 // A variable that appears in a lastprivate clause must not have a
19467 // const-qualified type unless it is of class type with a mutable member.
19468 if (rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_lastprivate, ELoc))
19469 continue;
19470
19471 // OpenMP 5.0 [2.19.4.5 lastprivate Clause, Restrictions]
19472 // A list item that appears in a lastprivate clause with the conditional
19473 // modifier must be a scalar variable.
19474 if (LPKind == OMPC_LASTPRIVATE_conditional && !Type->isScalarType()) {
19475 Diag(Loc: ELoc, DiagID: diag::err_omp_lastprivate_conditional_non_scalar);
19476 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
19477 VarDecl::DeclarationOnly;
19478 Diag(Loc: D->getLocation(),
19479 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
19480 << D;
19481 continue;
19482 }
19483
19484 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
19485 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
19486 // in a Construct]
19487 // Variables with the predetermined data-sharing attributes may not be
19488 // listed in data-sharing attributes clauses, except for the cases
19489 // listed below.
19490 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
19491 // A list item may appear in a firstprivate or lastprivate clause but not
19492 // both.
19493 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19494 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
19495 (isOpenMPDistributeDirective(DKind: CurrDir) ||
19496 DVar.CKind != OMPC_firstprivate) &&
19497 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
19498 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19499 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19500 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate);
19501 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19502 continue;
19503 }
19504
19505 // OpenMP [2.14.3.5, Restrictions, p.2]
19506 // A list item that is private within a parallel region, or that appears in
19507 // the reduction clause of a parallel construct, must not appear in a
19508 // lastprivate clause on a worksharing construct if any of the corresponding
19509 // worksharing regions ever binds to any of the corresponding parallel
19510 // regions.
19511 DSAStackTy::DSAVarData TopDVar = DVar;
19512 if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
19513 !isOpenMPParallelDirective(DKind: CurrDir) &&
19514 !isOpenMPTeamsDirective(DKind: CurrDir)) {
19515 DVar = DSAStack->getImplicitDSA(D, FromParent: true);
19516 if (DVar.CKind != OMPC_shared) {
19517 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
19518 << getOpenMPClauseNameForDiag(C: OMPC_lastprivate)
19519 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19520 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19521 continue;
19522 }
19523 }
19524
19525 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
19526 // A variable of class type (or array thereof) that appears in a
19527 // lastprivate clause requires an accessible, unambiguous default
19528 // constructor for the class type, unless the list item is also specified
19529 // in a firstprivate clause.
19530 // A variable of class type (or array thereof) that appears in a
19531 // lastprivate clause requires an accessible, unambiguous copy assignment
19532 // operator for the class type.
19533 Type = getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
19534 VarDecl *SrcVD = buildVarDecl(SemaRef, Loc: ERange.getBegin(),
19535 Type: Type.getUnqualifiedType(), Name: ".lastprivate.src",
19536 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
19537 DeclRefExpr *PseudoSrcExpr =
19538 buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type.getUnqualifiedType(), Loc: ELoc);
19539 VarDecl *DstVD =
19540 buildVarDecl(SemaRef, Loc: ERange.getBegin(), Type, Name: ".lastprivate.dst",
19541 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
19542 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
19543 // For arrays generate assignment operation for single element and replace
19544 // it by the original array element in CodeGen.
19545 ExprResult AssignmentOp = SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: ELoc, Opc: BO_Assign,
19546 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
19547 if (AssignmentOp.isInvalid())
19548 continue;
19549 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
19550 /*DiscardedValue=*/false);
19551 if (AssignmentOp.isInvalid())
19552 continue;
19553
19554 DeclRefExpr *Ref = nullptr;
19555 if (!VD && !SemaRef.CurContext->isDependentContext()) {
19556 if (TopDVar.CKind == OMPC_firstprivate) {
19557 Ref = TopDVar.PrivateCopy;
19558 } else {
19559 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
19560 if (!isOpenMPCapturedDecl(D))
19561 ExprCaptures.push_back(Elt: Ref->getDecl());
19562 }
19563 if ((TopDVar.CKind == OMPC_firstprivate && !TopDVar.PrivateCopy) ||
19564 (!isOpenMPCapturedDecl(D) &&
19565 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
19566 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
19567 if (!RefRes.isUsable())
19568 continue;
19569 ExprResult PostUpdateRes =
19570 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
19571 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
19572 if (!PostUpdateRes.isUsable())
19573 continue;
19574 ExprPostUpdates.push_back(
19575 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
19576 }
19577 }
19578 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_lastprivate, PrivateCopy: Ref);
19579 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
19580 ? RefExpr->IgnoreParens()
19581 : Ref);
19582 SrcExprs.push_back(Elt: PseudoSrcExpr);
19583 DstExprs.push_back(Elt: PseudoDstExpr);
19584 AssignmentOps.push_back(Elt: AssignmentOp.get());
19585 }
19586
19587 if (Vars.empty())
19588 return nullptr;
19589
19590 return OMPLastprivateClause::Create(
19591 C: getASTContext(), StartLoc, LParenLoc, EndLoc, VL: Vars, SrcExprs, DstExprs,
19592 AssignmentOps, LPKind, LPKindLoc, ColonLoc,
19593 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
19594 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
19595}
19596
19597OMPClause *SemaOpenMP::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
19598 SourceLocation StartLoc,
19599 SourceLocation LParenLoc,
19600 SourceLocation EndLoc) {
19601 SmallVector<Expr *, 8> Vars;
19602 for (Expr *RefExpr : VarList) {
19603 assert(RefExpr && "NULL expr in OpenMP shared clause.");
19604 SourceLocation ELoc;
19605 SourceRange ERange;
19606 Expr *SimpleRefExpr = RefExpr;
19607 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
19608 if (Res.second) {
19609 // It will be analyzed later.
19610 Vars.push_back(Elt: RefExpr);
19611 }
19612 ValueDecl *D = Res.first;
19613 if (!D)
19614 continue;
19615
19616 auto *VD = dyn_cast<VarDecl>(Val: D);
19617 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
19618 // in a Construct]
19619 // Variables with the predetermined data-sharing attributes may not be
19620 // listed in data-sharing attributes clauses, except for the cases
19621 // listed below. For these exceptions only, listing a predetermined
19622 // variable in a data-sharing attribute clause is allowed and overrides
19623 // the variable's predetermined data-sharing attributes.
19624 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
19625 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
19626 DVar.RefExpr) {
19627 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
19628 << getOpenMPClauseNameForDiag(C: DVar.CKind)
19629 << getOpenMPClauseNameForDiag(C: OMPC_shared);
19630 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
19631 continue;
19632 }
19633
19634 DeclRefExpr *Ref = nullptr;
19635 if (!VD && isOpenMPCapturedDecl(D) &&
19636 !SemaRef.CurContext->isDependentContext())
19637 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
19638 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_shared, PrivateCopy: Ref);
19639 Vars.push_back(Elt: (VD || !Ref || SemaRef.CurContext->isDependentContext())
19640 ? RefExpr->IgnoreParens()
19641 : Ref);
19642 }
19643
19644 if (Vars.empty())
19645 return nullptr;
19646
19647 return OMPSharedClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
19648 VL: Vars);
19649}
19650
19651namespace {
19652class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
19653 DSAStackTy *Stack;
19654
19655public:
19656 bool VisitDeclRefExpr(DeclRefExpr *E) {
19657 if (auto *VD = dyn_cast<VarDecl>(Val: E->getDecl())) {
19658 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D: VD, /*FromParent=*/false);
19659 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
19660 return false;
19661 if (DVar.CKind != OMPC_unknown)
19662 return true;
19663 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
19664 D: VD,
19665 CPred: [](OpenMPClauseKind C, bool AppliedToPointee, bool) {
19666 return isOpenMPPrivate(Kind: C) && !AppliedToPointee;
19667 },
19668 DPred: [](OpenMPDirectiveKind) { return true; },
19669 /*FromParent=*/true);
19670 return DVarPrivate.CKind != OMPC_unknown;
19671 }
19672 return false;
19673 }
19674 bool VisitStmt(Stmt *S) {
19675 for (Stmt *Child : S->children()) {
19676 if (Child && Visit(S: Child))
19677 return true;
19678 }
19679 return false;
19680 }
19681 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
19682};
19683} // namespace
19684
19685namespace {
19686// Transform MemberExpression for specified FieldDecl of current class to
19687// DeclRefExpr to specified OMPCapturedExprDecl.
19688class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
19689 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
19690 ValueDecl *Field = nullptr;
19691 DeclRefExpr *CapturedExpr = nullptr;
19692
19693public:
19694 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
19695 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
19696
19697 ExprResult TransformMemberExpr(MemberExpr *E) {
19698 if (isa<CXXThisExpr>(Val: E->getBase()->IgnoreParenImpCasts()) &&
19699 E->getMemberDecl() == Field) {
19700 CapturedExpr = buildCapture(S&: SemaRef, D: Field, CaptureExpr: E, /*WithInit=*/false);
19701 return CapturedExpr;
19702 }
19703 return BaseTransform::TransformMemberExpr(E);
19704 }
19705 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
19706};
19707} // namespace
19708
19709template <typename T, typename U>
19710static T filterLookupForUDReductionAndMapper(
19711 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
19712 for (U &Set : Lookups) {
19713 for (auto *D : Set) {
19714 if (T Res = Gen(cast<ValueDecl>(D)))
19715 return Res;
19716 }
19717 }
19718 return T();
19719}
19720
19721static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
19722 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
19723
19724 for (auto *RD : D->redecls()) {
19725 // Don't bother with extra checks if we already know this one isn't visible.
19726 if (RD == D)
19727 continue;
19728
19729 auto ND = cast<NamedDecl>(Val: RD);
19730 if (LookupResult::isVisible(SemaRef, D: ND))
19731 return ND;
19732 }
19733
19734 return nullptr;
19735}
19736
19737static void
19738argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
19739 SourceLocation Loc, QualType Ty,
19740 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
19741 // Find all of the associated namespaces and classes based on the
19742 // arguments we have.
19743 Sema::AssociatedNamespaceSet AssociatedNamespaces;
19744 Sema::AssociatedClassSet AssociatedClasses;
19745 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
19746 SemaRef.FindAssociatedClassesAndNamespaces(InstantiationLoc: Loc, Args: &OVE, AssociatedNamespaces,
19747 AssociatedClasses);
19748
19749 // C++ [basic.lookup.argdep]p3:
19750 // Let X be the lookup set produced by unqualified lookup (3.4.1)
19751 // and let Y be the lookup set produced by argument dependent
19752 // lookup (defined as follows). If X contains [...] then Y is
19753 // empty. Otherwise Y is the set of declarations found in the
19754 // namespaces associated with the argument types as described
19755 // below. The set of declarations found by the lookup of the name
19756 // is the union of X and Y.
19757 //
19758 // Here, we compute Y and add its members to the overloaded
19759 // candidate set.
19760 for (auto *NS : AssociatedNamespaces) {
19761 // When considering an associated namespace, the lookup is the
19762 // same as the lookup performed when the associated namespace is
19763 // used as a qualifier (3.4.3.2) except that:
19764 //
19765 // -- Any using-directives in the associated namespace are
19766 // ignored.
19767 //
19768 // -- Any namespace-scope friend functions declared in
19769 // associated classes are visible within their respective
19770 // namespaces even if they are not visible during an ordinary
19771 // lookup (11.4).
19772 DeclContext::lookup_result R = NS->lookup(Name: Id.getName());
19773 for (auto *D : R) {
19774 auto *Underlying = D;
19775 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
19776 Underlying = USD->getTargetDecl();
19777
19778 if (!isa<OMPDeclareReductionDecl>(Val: Underlying) &&
19779 !isa<OMPDeclareMapperDecl>(Val: Underlying))
19780 continue;
19781
19782 if (!SemaRef.isVisible(D)) {
19783 D = findAcceptableDecl(SemaRef, D);
19784 if (!D)
19785 continue;
19786 if (auto *USD = dyn_cast<UsingShadowDecl>(Val: D))
19787 Underlying = USD->getTargetDecl();
19788 }
19789 Lookups.emplace_back();
19790 Lookups.back().addDecl(D: Underlying);
19791 }
19792 }
19793}
19794
19795static ExprResult
19796buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
19797 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
19798 const DeclarationNameInfo &ReductionId, QualType Ty,
19799 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
19800 if (ReductionIdScopeSpec.isInvalid())
19801 return ExprError();
19802 SmallVector<UnresolvedSet<8>, 4> Lookups;
19803 if (S) {
19804 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
19805 Lookup.suppressDiagnostics();
19806 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &ReductionIdScopeSpec,
19807 /*ObjectType=*/QualType())) {
19808 NamedDecl *D = Lookup.getRepresentativeDecl();
19809 do {
19810 S = S->getParent();
19811 } while (S && !S->isDeclScope(D));
19812 if (S)
19813 S = S->getParent();
19814 Lookups.emplace_back();
19815 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
19816 Lookup.clear();
19817 }
19818 } else if (auto *ULE =
19819 cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedReduction)) {
19820 Lookups.push_back(Elt: UnresolvedSet<8>());
19821 Decl *PrevD = nullptr;
19822 for (NamedDecl *D : ULE->decls()) {
19823 if (D == PrevD)
19824 Lookups.push_back(Elt: UnresolvedSet<8>());
19825 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Val: D))
19826 Lookups.back().addDecl(D: DRD);
19827 PrevD = D;
19828 }
19829 }
19830 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
19831 Ty->isInstantiationDependentType() ||
19832 Ty->containsUnexpandedParameterPack() ||
19833 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
19834 return !D->isInvalidDecl() &&
19835 (D->getType()->isDependentType() ||
19836 D->getType()->isInstantiationDependentType() ||
19837 D->getType()->containsUnexpandedParameterPack());
19838 })) {
19839 UnresolvedSet<8> ResSet;
19840 for (const UnresolvedSet<8> &Set : Lookups) {
19841 if (Set.empty())
19842 continue;
19843 ResSet.append(I: Set.begin(), E: Set.end());
19844 // The last item marks the end of all declarations at the specified scope.
19845 ResSet.addDecl(D: Set[Set.size() - 1]);
19846 }
19847 return UnresolvedLookupExpr::Create(
19848 Context: SemaRef.Context, /*NamingClass=*/nullptr,
19849 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: ReductionId,
19850 /*ADL=*/RequiresADL: true, Begin: ResSet.begin(), End: ResSet.end(), /*KnownDependent=*/false,
19851 /*KnownInstantiationDependent=*/false);
19852 }
19853 // Lookup inside the classes.
19854 // C++ [over.match.oper]p3:
19855 // For a unary operator @ with an operand of a type whose
19856 // cv-unqualified version is T1, and for a binary operator @ with
19857 // a left operand of a type whose cv-unqualified version is T1 and
19858 // a right operand of a type whose cv-unqualified version is T2,
19859 // three sets of candidate functions, designated member
19860 // candidates, non-member candidates and built-in candidates, are
19861 // constructed as follows:
19862 // -- If T1 is a complete class type or a class currently being
19863 // defined, the set of member candidates is the result of the
19864 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
19865 // the set of member candidates is empty.
19866 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
19867 Lookup.suppressDiagnostics();
19868 if (Ty->isRecordType()) {
19869 // Complete the type if it can be completed.
19870 // If the type is neither complete nor being defined, bail out now.
19871 bool IsComplete = SemaRef.isCompleteType(Loc, T: Ty);
19872 auto *RD = Ty->castAsRecordDecl();
19873 if (IsComplete || RD->isBeingDefined()) {
19874 Lookup.clear();
19875 SemaRef.LookupQualifiedName(R&: Lookup, LookupCtx: RD);
19876 if (Lookup.empty()) {
19877 Lookups.emplace_back();
19878 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
19879 }
19880 }
19881 }
19882 // Perform ADL.
19883 if (SemaRef.getLangOpts().CPlusPlus)
19884 argumentDependentLookup(SemaRef, Id: ReductionId, Loc, Ty, Lookups);
19885 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19886 Lookups, Gen: [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
19887 if (!D->isInvalidDecl() &&
19888 SemaRef.Context.hasSameType(T1: D->getType(), T2: Ty))
19889 return D;
19890 return nullptr;
19891 }))
19892 return SemaRef.BuildDeclRefExpr(D: VD, Ty: VD->getType().getNonReferenceType(),
19893 VK: VK_LValue, Loc);
19894 if (SemaRef.getLangOpts().CPlusPlus) {
19895 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
19896 Lookups, Gen: [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
19897 if (!D->isInvalidDecl() &&
19898 SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: D->getType()) &&
19899 !Ty.isMoreQualifiedThan(other: D->getType(),
19900 Ctx: SemaRef.getASTContext()))
19901 return D;
19902 return nullptr;
19903 })) {
19904 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
19905 /*DetectVirtual=*/false);
19906 if (SemaRef.IsDerivedFrom(Loc, Derived: Ty, Base: VD->getType(), Paths)) {
19907 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
19908 T: VD->getType().getUnqualifiedType()))) {
19909 if (SemaRef.CheckBaseClassAccess(
19910 AccessLoc: Loc, Base: VD->getType(), Derived: Ty, Path: Paths.front(),
19911 /*DiagID=*/0) != Sema::AR_inaccessible) {
19912 SemaRef.BuildBasePathArray(Paths, BasePath);
19913 return SemaRef.BuildDeclRefExpr(
19914 D: VD, Ty: VD->getType().getNonReferenceType(), VK: VK_LValue, Loc);
19915 }
19916 }
19917 }
19918 }
19919 }
19920 if (ReductionIdScopeSpec.isSet()) {
19921 SemaRef.Diag(Loc, DiagID: diag::err_omp_not_resolved_reduction_identifier)
19922 << Ty << Range;
19923 return ExprError();
19924 }
19925 return ExprEmpty();
19926}
19927
19928namespace {
19929/// Data for the reduction-based clauses.
19930struct ReductionData {
19931 /// List of original reduction items.
19932 SmallVector<Expr *, 8> Vars;
19933 /// List of private copies of the reduction items.
19934 SmallVector<Expr *, 8> Privates;
19935 /// LHS expressions for the reduction_op expressions.
19936 SmallVector<Expr *, 8> LHSs;
19937 /// RHS expressions for the reduction_op expressions.
19938 SmallVector<Expr *, 8> RHSs;
19939 /// Reduction operation expression.
19940 SmallVector<Expr *, 8> ReductionOps;
19941 /// inscan copy operation expressions.
19942 SmallVector<Expr *, 8> InscanCopyOps;
19943 /// inscan copy temp array expressions for prefix sums.
19944 SmallVector<Expr *, 8> InscanCopyArrayTemps;
19945 /// inscan copy temp array element expressions for prefix sums.
19946 SmallVector<Expr *, 8> InscanCopyArrayElems;
19947 /// Taskgroup descriptors for the corresponding reduction items in
19948 /// in_reduction clauses.
19949 SmallVector<Expr *, 8> TaskgroupDescriptors;
19950 /// List of captures for clause.
19951 SmallVector<Decl *, 4> ExprCaptures;
19952 /// List of postupdate expressions.
19953 SmallVector<Expr *, 4> ExprPostUpdates;
19954 /// Reduction modifier.
19955 unsigned RedModifier = 0;
19956 /// Original modifier.
19957 unsigned OrigSharingModifier = 0;
19958 /// Private Variable Reduction
19959 SmallVector<bool, 8> IsPrivateVarReduction;
19960 ReductionData() = delete;
19961 /// Reserves required memory for the reduction data.
19962 ReductionData(unsigned Size, unsigned Modifier = 0, unsigned OrgModifier = 0)
19963 : RedModifier(Modifier), OrigSharingModifier(OrgModifier) {
19964 Vars.reserve(N: Size);
19965 Privates.reserve(N: Size);
19966 LHSs.reserve(N: Size);
19967 RHSs.reserve(N: Size);
19968 ReductionOps.reserve(N: Size);
19969 IsPrivateVarReduction.reserve(N: Size);
19970 if (RedModifier == OMPC_REDUCTION_inscan) {
19971 InscanCopyOps.reserve(N: Size);
19972 InscanCopyArrayTemps.reserve(N: Size);
19973 InscanCopyArrayElems.reserve(N: Size);
19974 }
19975 TaskgroupDescriptors.reserve(N: Size);
19976 ExprCaptures.reserve(N: Size);
19977 ExprPostUpdates.reserve(N: Size);
19978 }
19979 /// Stores reduction item and reduction operation only (required for dependent
19980 /// reduction item).
19981 void push(Expr *Item, Expr *ReductionOp) {
19982 Vars.emplace_back(Args&: Item);
19983 Privates.emplace_back(Args: nullptr);
19984 LHSs.emplace_back(Args: nullptr);
19985 RHSs.emplace_back(Args: nullptr);
19986 ReductionOps.emplace_back(Args&: ReductionOp);
19987 IsPrivateVarReduction.emplace_back(Args: false);
19988 TaskgroupDescriptors.emplace_back(Args: nullptr);
19989 if (RedModifier == OMPC_REDUCTION_inscan) {
19990 InscanCopyOps.push_back(Elt: nullptr);
19991 InscanCopyArrayTemps.push_back(Elt: nullptr);
19992 InscanCopyArrayElems.push_back(Elt: nullptr);
19993 }
19994 }
19995 /// Stores reduction data.
19996 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
19997 Expr *TaskgroupDescriptor, Expr *CopyOp, Expr *CopyArrayTemp,
19998 Expr *CopyArrayElem, bool IsPrivate) {
19999 Vars.emplace_back(Args&: Item);
20000 Privates.emplace_back(Args&: Private);
20001 LHSs.emplace_back(Args&: LHS);
20002 RHSs.emplace_back(Args&: RHS);
20003 ReductionOps.emplace_back(Args&: ReductionOp);
20004 TaskgroupDescriptors.emplace_back(Args&: TaskgroupDescriptor);
20005 if (RedModifier == OMPC_REDUCTION_inscan) {
20006 InscanCopyOps.push_back(Elt: CopyOp);
20007 InscanCopyArrayTemps.push_back(Elt: CopyArrayTemp);
20008 InscanCopyArrayElems.push_back(Elt: CopyArrayElem);
20009 } else {
20010 assert(CopyOp == nullptr && CopyArrayTemp == nullptr &&
20011 CopyArrayElem == nullptr &&
20012 "Copy operation must be used for inscan reductions only.");
20013 }
20014 IsPrivateVarReduction.emplace_back(Args&: IsPrivate);
20015 }
20016};
20017} // namespace
20018
20019static bool checkOMPArraySectionConstantForReduction(
20020 ASTContext &Context, const ArraySectionExpr *OASE, bool &SingleElement,
20021 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
20022 const Expr *Length = OASE->getLength();
20023 if (Length == nullptr) {
20024 // For array sections of the form [1:] or [:], we would need to analyze
20025 // the lower bound...
20026 if (OASE->getColonLocFirst().isValid())
20027 return false;
20028
20029 // This is an array subscript which has implicit length 1!
20030 SingleElement = true;
20031 ArraySizes.push_back(Elt: llvm::APSInt::get(X: 1));
20032 } else {
20033 Expr::EvalResult Result;
20034 if (!Length->EvaluateAsInt(Result, Ctx: Context))
20035 return false;
20036
20037 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
20038 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
20039 ArraySizes.push_back(Elt: ConstantLengthValue);
20040 }
20041
20042 // Get the base of this array section and walk up from there.
20043 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
20044
20045 // We require length = 1 for all array sections except the right-most to
20046 // guarantee that the memory region is contiguous and has no holes in it.
20047 while (const auto *TempOASE = dyn_cast<ArraySectionExpr>(Val: Base)) {
20048 Length = TempOASE->getLength();
20049 if (Length == nullptr) {
20050 // For array sections of the form [1:] or [:], we would need to analyze
20051 // the lower bound...
20052 if (OASE->getColonLocFirst().isValid())
20053 return false;
20054
20055 // This is an array subscript which has implicit length 1!
20056 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
20057 ArraySizes.push_back(Elt: ConstantOne);
20058 } else {
20059 Expr::EvalResult Result;
20060 if (!Length->EvaluateAsInt(Result, Ctx: Context))
20061 return false;
20062
20063 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
20064 if (ConstantLengthValue.getSExtValue() != 1)
20065 return false;
20066
20067 ArraySizes.push_back(Elt: ConstantLengthValue);
20068 }
20069 Base = TempOASE->getBase()->IgnoreParenImpCasts();
20070 }
20071
20072 // If we have a single element, we don't need to add the implicit lengths.
20073 if (!SingleElement) {
20074 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Val: Base)) {
20075 // Has implicit length 1!
20076 llvm::APSInt ConstantOne = llvm::APSInt::get(X: 1);
20077 ArraySizes.push_back(Elt: ConstantOne);
20078 Base = TempASE->getBase()->IgnoreParenImpCasts();
20079 }
20080 }
20081
20082 // This array section can be privatized as a single value or as a constant
20083 // sized array.
20084 return true;
20085}
20086
20087static BinaryOperatorKind
20088getRelatedCompoundReductionOp(BinaryOperatorKind BOK) {
20089 if (BOK == BO_Add)
20090 return BO_AddAssign;
20091 if (BOK == BO_Mul)
20092 return BO_MulAssign;
20093 if (BOK == BO_And)
20094 return BO_AndAssign;
20095 if (BOK == BO_Or)
20096 return BO_OrAssign;
20097 if (BOK == BO_Xor)
20098 return BO_XorAssign;
20099 return BOK;
20100}
20101
20102static bool actOnOMPReductionKindClause(
20103 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
20104 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
20105 SourceLocation ColonLoc, SourceLocation EndLoc,
20106 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20107 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
20108 DeclarationName DN = ReductionId.getName();
20109 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
20110 BinaryOperatorKind BOK = BO_Comma;
20111
20112 ASTContext &Context = S.Context;
20113 // OpenMP [2.14.3.6, reduction clause]
20114 // C
20115 // reduction-identifier is either an identifier or one of the following
20116 // operators: +, -, *, &, |, ^, && and ||
20117 // C++
20118 // reduction-identifier is either an id-expression or one of the following
20119 // operators: +, -, *, &, |, ^, && and ||
20120 switch (OOK) {
20121 case OO_Plus:
20122 BOK = BO_Add;
20123 break;
20124 case OO_Minus:
20125 // Minus(-) operator is not supported in TR11 (OpenMP 6.0). Setting BOK to
20126 // BO_Comma will automatically diagnose it for OpenMP > 52 as not allowed
20127 // reduction identifier.
20128 if (S.LangOpts.OpenMP > 52)
20129 BOK = BO_Comma;
20130 else
20131 BOK = BO_Add;
20132 break;
20133 case OO_Star:
20134 BOK = BO_Mul;
20135 break;
20136 case OO_Amp:
20137 BOK = BO_And;
20138 break;
20139 case OO_Pipe:
20140 BOK = BO_Or;
20141 break;
20142 case OO_Caret:
20143 BOK = BO_Xor;
20144 break;
20145 case OO_AmpAmp:
20146 BOK = BO_LAnd;
20147 break;
20148 case OO_PipePipe:
20149 BOK = BO_LOr;
20150 break;
20151 case OO_New:
20152 case OO_Delete:
20153 case OO_Array_New:
20154 case OO_Array_Delete:
20155 case OO_Slash:
20156 case OO_Percent:
20157 case OO_Tilde:
20158 case OO_Exclaim:
20159 case OO_Equal:
20160 case OO_Less:
20161 case OO_Greater:
20162 case OO_LessEqual:
20163 case OO_GreaterEqual:
20164 case OO_PlusEqual:
20165 case OO_MinusEqual:
20166 case OO_StarEqual:
20167 case OO_SlashEqual:
20168 case OO_PercentEqual:
20169 case OO_CaretEqual:
20170 case OO_AmpEqual:
20171 case OO_PipeEqual:
20172 case OO_LessLess:
20173 case OO_GreaterGreater:
20174 case OO_LessLessEqual:
20175 case OO_GreaterGreaterEqual:
20176 case OO_EqualEqual:
20177 case OO_ExclaimEqual:
20178 case OO_Spaceship:
20179 case OO_PlusPlus:
20180 case OO_MinusMinus:
20181 case OO_Comma:
20182 case OO_ArrowStar:
20183 case OO_Arrow:
20184 case OO_Call:
20185 case OO_Subscript:
20186 case OO_Conditional:
20187 case OO_Coawait:
20188 case NUM_OVERLOADED_OPERATORS:
20189 llvm_unreachable("Unexpected reduction identifier");
20190 case OO_None:
20191 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
20192 if (II->isStr(Str: "max"))
20193 BOK = BO_GT;
20194 else if (II->isStr(Str: "min"))
20195 BOK = BO_LT;
20196 }
20197 break;
20198 }
20199
20200 // OpenMP 5.2, 5.5.5 (see page 627, line 18) reduction Clause, Restrictions
20201 // A reduction clause with the minus (-) operator was deprecated
20202 if (OOK == OO_Minus && S.LangOpts.OpenMP == 52)
20203 S.Diag(Loc: ReductionId.getLoc(), DiagID: diag::warn_omp_minus_in_reduction_deprecated);
20204
20205 SourceRange ReductionIdRange;
20206 if (ReductionIdScopeSpec.isValid())
20207 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
20208 else
20209 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
20210 ReductionIdRange.setEnd(ReductionId.getEndLoc());
20211
20212 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
20213 bool FirstIter = true;
20214 for (Expr *RefExpr : VarList) {
20215 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
20216 // OpenMP [2.1, C/C++]
20217 // A list item is a variable or array section, subject to the restrictions
20218 // specified in Section 2.4 on page 42 and in each of the sections
20219 // describing clauses and directives for which a list appears.
20220 // OpenMP [2.14.3.3, Restrictions, p.1]
20221 // A variable that is part of another variable (as an array or
20222 // structure element) cannot appear in a private clause.
20223 if (!FirstIter && IR != ER)
20224 ++IR;
20225 FirstIter = false;
20226 SourceLocation ELoc;
20227 SourceRange ERange;
20228 bool IsPrivate = false;
20229 Expr *SimpleRefExpr = RefExpr;
20230 auto Res = getPrivateItem(S, RefExpr&: SimpleRefExpr, ELoc, ERange,
20231 /*AllowArraySection=*/true);
20232 if (Res.second) {
20233 // Try to find 'declare reduction' corresponding construct before using
20234 // builtin/overloaded operators.
20235 QualType Type = Context.DependentTy;
20236 CXXCastPath BasePath;
20237 ExprResult DeclareReductionRef = buildDeclareReductionRef(
20238 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
20239 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
20240 Expr *ReductionOp = nullptr;
20241 if (S.CurContext->isDependentContext() &&
20242 (DeclareReductionRef.isUnset() ||
20243 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get())))
20244 ReductionOp = DeclareReductionRef.get();
20245 // It will be analyzed later.
20246 RD.push(Item: RefExpr, ReductionOp);
20247 }
20248 ValueDecl *D = Res.first;
20249 if (!D)
20250 continue;
20251
20252 Expr *TaskgroupDescriptor = nullptr;
20253 QualType Type;
20254 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: RefExpr->IgnoreParens());
20255 auto *OASE = dyn_cast<ArraySectionExpr>(Val: RefExpr->IgnoreParens());
20256 if (ASE) {
20257 Type = ASE->getType().getNonReferenceType();
20258 } else if (OASE) {
20259 QualType BaseType =
20260 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
20261 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
20262 Type = ATy->getElementType();
20263 else
20264 Type = BaseType->getPointeeType();
20265 Type = Type.getNonReferenceType();
20266 } else {
20267 Type = Context.getBaseElementType(QT: D->getType().getNonReferenceType());
20268 }
20269 auto *VD = dyn_cast<VarDecl>(Val: D);
20270
20271 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
20272 // A variable that appears in a private clause must not have an incomplete
20273 // type or a reference type.
20274 if (S.RequireCompleteType(Loc: ELoc, T: D->getType(),
20275 DiagID: diag::err_omp_reduction_incomplete_type))
20276 continue;
20277 // OpenMP [2.14.3.6, reduction clause, Restrictions]
20278 // A list item that appears in a reduction clause must not be
20279 // const-qualified.
20280 if (rejectConstNotMutableType(SemaRef&: S, D, Type, CKind: ClauseKind, ELoc,
20281 /*AcceptIfMutable=*/false, ListItemNotVar: ASE || OASE))
20282 continue;
20283
20284 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
20285 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
20286 // If a list-item is a reference type then it must bind to the same object
20287 // for all threads of the team.
20288 if (!ASE && !OASE) {
20289 if (VD) {
20290 VarDecl *VDDef = VD->getDefinition();
20291 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
20292 DSARefChecker Check(Stack);
20293 if (Check.Visit(S: VDDef->getInit())) {
20294 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_ref_type_arg)
20295 << getOpenMPClauseNameForDiag(C: ClauseKind) << ERange;
20296 S.Diag(Loc: VDDef->getLocation(), DiagID: diag::note_defined_here) << VDDef;
20297 continue;
20298 }
20299 }
20300 }
20301
20302 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
20303 // in a Construct]
20304 // Variables with the predetermined data-sharing attributes may not be
20305 // listed in data-sharing attributes clauses, except for the cases
20306 // listed below. For these exceptions only, listing a predetermined
20307 // variable in a data-sharing attribute clause is allowed and overrides
20308 // the variable's predetermined data-sharing attributes.
20309 // OpenMP [2.14.3.6, Restrictions, p.3]
20310 // Any number of reduction clauses can be specified on the directive,
20311 // but a list item can appear only once in the reduction clauses for that
20312 // directive.
20313 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
20314 if (DVar.CKind == OMPC_reduction) {
20315 S.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced)
20316 << getOpenMPClauseNameForDiag(C: ClauseKind);
20317 if (DVar.RefExpr)
20318 S.Diag(Loc: DVar.RefExpr->getExprLoc(), DiagID: diag::note_omp_referenced);
20319 continue;
20320 }
20321 if (DVar.CKind != OMPC_unknown) {
20322 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
20323 << getOpenMPClauseNameForDiag(C: DVar.CKind)
20324 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20325 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20326 continue;
20327 }
20328
20329 // OpenMP [2.14.3.6, Restrictions, p.1]
20330 // A list item that appears in a reduction clause of a worksharing
20331 // construct must be shared in the parallel regions to which any of the
20332 // worksharing regions arising from the worksharing construct bind.
20333
20334 if (S.getLangOpts().OpenMP <= 52 &&
20335 isOpenMPWorksharingDirective(DKind: CurrDir) &&
20336 !isOpenMPParallelDirective(DKind: CurrDir) &&
20337 !isOpenMPTeamsDirective(DKind: CurrDir)) {
20338 DVar = Stack->getImplicitDSA(D, FromParent: true);
20339 if (DVar.CKind != OMPC_shared) {
20340 S.Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
20341 << getOpenMPClauseNameForDiag(C: OMPC_reduction)
20342 << getOpenMPClauseNameForDiag(C: OMPC_shared);
20343 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20344 continue;
20345 }
20346 } else if (isOpenMPWorksharingDirective(DKind: CurrDir) &&
20347 !isOpenMPParallelDirective(DKind: CurrDir) &&
20348 !isOpenMPTeamsDirective(DKind: CurrDir)) {
20349 // OpenMP 6.0 [ 7.6.10 ]
20350 // Support Reduction over private variables with reduction clause.
20351 // A list item in a reduction clause can now be private in the enclosing
20352 // context. For orphaned constructs it is assumed to be shared unless
20353 // the original(private) modifier appears in the clause.
20354 DVar = Stack->getImplicitDSA(D, FromParent: true);
20355 // Determine if the variable should be considered private
20356 IsPrivate = DVar.CKind != OMPC_shared;
20357 bool IsOrphaned = false;
20358 OpenMPDirectiveKind ParentDir = Stack->getParentDirective();
20359 IsOrphaned = ParentDir == OMPD_unknown;
20360 if ((IsOrphaned &&
20361 RD.OrigSharingModifier == OMPC_ORIGINAL_SHARING_private))
20362 IsPrivate = true;
20363 }
20364 } else {
20365 // Threadprivates cannot be shared between threads, so dignose if the base
20366 // is a threadprivate variable.
20367 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
20368 if (DVar.CKind == OMPC_threadprivate) {
20369 S.Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
20370 << getOpenMPClauseNameForDiag(C: DVar.CKind)
20371 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20372 reportOriginalDsa(SemaRef&: S, Stack, D, DVar);
20373 continue;
20374 }
20375 }
20376
20377 // Try to find 'declare reduction' corresponding construct before using
20378 // builtin/overloaded operators.
20379 CXXCastPath BasePath;
20380 ExprResult DeclareReductionRef = buildDeclareReductionRef(
20381 SemaRef&: S, Loc: ELoc, Range: ERange, S: Stack->getCurScope(), ReductionIdScopeSpec,
20382 ReductionId, Ty: Type, BasePath, UnresolvedReduction: IR == ER ? nullptr : *IR);
20383 if (DeclareReductionRef.isInvalid())
20384 continue;
20385 if (S.CurContext->isDependentContext() &&
20386 (DeclareReductionRef.isUnset() ||
20387 isa<UnresolvedLookupExpr>(Val: DeclareReductionRef.get()))) {
20388 RD.push(Item: RefExpr, ReductionOp: DeclareReductionRef.get());
20389 continue;
20390 }
20391 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
20392 // Not allowed reduction identifier is found.
20393 if (S.LangOpts.OpenMP > 52)
20394 S.Diag(Loc: ReductionId.getBeginLoc(),
20395 DiagID: diag::err_omp_unknown_reduction_identifier_since_omp_6_0)
20396 << Type << ReductionIdRange;
20397 else
20398 S.Diag(Loc: ReductionId.getBeginLoc(),
20399 DiagID: diag::err_omp_unknown_reduction_identifier_prior_omp_6_0)
20400 << Type << ReductionIdRange;
20401 continue;
20402 }
20403
20404 // OpenMP [2.14.3.6, reduction clause, Restrictions]
20405 // The type of a list item that appears in a reduction clause must be valid
20406 // for the reduction-identifier. For a max or min reduction in C, the type
20407 // of the list item must be an allowed arithmetic data type: char, int,
20408 // float, double, or _Bool, possibly modified with long, short, signed, or
20409 // unsigned. For a max or min reduction in C++, the type of the list item
20410 // must be an allowed arithmetic data type: char, wchar_t, int, float,
20411 // double, or bool, possibly modified with long, short, signed, or unsigned.
20412 if (DeclareReductionRef.isUnset()) {
20413 if ((BOK == BO_GT || BOK == BO_LT) &&
20414 !(Type->isScalarType() ||
20415 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
20416 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_not_arithmetic_type_arg)
20417 << getOpenMPClauseNameForDiag(C: ClauseKind)
20418 << S.getLangOpts().CPlusPlus;
20419 if (!ASE && !OASE) {
20420 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20421 VarDecl::DeclarationOnly;
20422 S.Diag(Loc: D->getLocation(),
20423 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20424 << D;
20425 }
20426 continue;
20427 }
20428 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
20429 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
20430 S.Diag(Loc: ELoc, DiagID: diag::err_omp_clause_floating_type_arg)
20431 << getOpenMPClauseNameForDiag(C: ClauseKind);
20432 if (!ASE && !OASE) {
20433 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20434 VarDecl::DeclarationOnly;
20435 S.Diag(Loc: D->getLocation(),
20436 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20437 << D;
20438 }
20439 continue;
20440 }
20441 }
20442
20443 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
20444 VarDecl *LHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: ".reduction.lhs",
20445 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20446 VarDecl *RHSVD = buildVarDecl(SemaRef&: S, Loc: ELoc, Type, Name: D->getName(),
20447 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20448 QualType PrivateTy = Type;
20449
20450 // Try if we can determine constant lengths for all array sections and avoid
20451 // the VLA.
20452 bool ConstantLengthOASE = false;
20453 if (OASE) {
20454 bool SingleElement;
20455 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
20456 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
20457 Context, OASE, SingleElement, ArraySizes);
20458
20459 // If we don't have a single element, we must emit a constant array type.
20460 if (ConstantLengthOASE && !SingleElement) {
20461 for (llvm::APSInt &Size : ArraySizes)
20462 PrivateTy = Context.getConstantArrayType(EltTy: PrivateTy, ArySize: Size, SizeExpr: nullptr,
20463 ASM: ArraySizeModifier::Normal,
20464 /*IndexTypeQuals=*/0);
20465 }
20466 }
20467
20468 if ((OASE && !ConstantLengthOASE) ||
20469 (!OASE && !ASE &&
20470 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
20471 if (!Context.getTargetInfo().isVLASupported()) {
20472 if (isOpenMPTargetExecutionDirective(DKind: Stack->getCurrentDirective())) {
20473 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
20474 S.Diag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
20475 continue;
20476 } else {
20477 S.targetDiag(Loc: ELoc, DiagID: diag::err_omp_reduction_vla_unsupported) << !!OASE;
20478 S.targetDiag(Loc: ELoc, DiagID: diag::note_vla_unsupported);
20479 }
20480 }
20481 // For arrays/array sections only:
20482 // Create pseudo array type for private copy. The size for this array will
20483 // be generated during codegen.
20484 // For array subscripts or single variables Private Ty is the same as Type
20485 // (type of the variable or single array element).
20486 PrivateTy = Context.getVariableArrayType(
20487 EltTy: Type,
20488 NumElts: new (Context)
20489 OpaqueValueExpr(ELoc, Context.getSizeType(), VK_PRValue),
20490 ASM: ArraySizeModifier::Normal, /*IndexTypeQuals=*/0);
20491 } else if (!ASE && !OASE &&
20492 Context.getAsArrayType(T: D->getType().getNonReferenceType())) {
20493 PrivateTy = D->getType().getNonReferenceType();
20494 }
20495 // Private copy.
20496 VarDecl *PrivateVD =
20497 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
20498 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
20499 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
20500 // Add initializer for private variable.
20501 Expr *Init = nullptr;
20502 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, D: LHSVD, Ty: Type, Loc: ELoc);
20503 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, D: RHSVD, Ty: Type, Loc: ELoc);
20504 if (DeclareReductionRef.isUsable()) {
20505 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
20506 auto *DRD = cast<OMPDeclareReductionDecl>(Val: DRDRef->getDecl());
20507 if (DRD->getInitializer()) {
20508 Init = DRDRef;
20509 RHSVD->setInit(DRDRef);
20510 RHSVD->setInitStyle(VarDecl::CallInit);
20511 }
20512 } else {
20513 switch (BOK) {
20514 case BO_Add:
20515 case BO_Xor:
20516 case BO_Or:
20517 case BO_LOr:
20518 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
20519 if (Type->isScalarType() || Type->isAnyComplexType())
20520 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/0).get();
20521 break;
20522 case BO_Mul:
20523 case BO_LAnd:
20524 if (Type->isScalarType() || Type->isAnyComplexType()) {
20525 // '*' and '&&' reduction ops - initializer is '1'.
20526 Init = S.ActOnIntegerConstant(Loc: ELoc, /*Val=*/1).get();
20527 }
20528 break;
20529 case BO_And: {
20530 // '&' reduction op - initializer is '~0'.
20531 QualType OrigType = Type;
20532 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
20533 Type = ComplexTy->getElementType();
20534 if (Type->isRealFloatingType()) {
20535 llvm::APFloat InitValue = llvm::APFloat::getAllOnesValue(
20536 Semantics: Context.getFloatTypeSemantics(T: Type));
20537 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
20538 Type, L: ELoc);
20539 } else if (Type->isScalarType()) {
20540 uint64_t Size = Context.getTypeSize(T: Type);
20541 QualType IntTy = Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/0);
20542 llvm::APInt InitValue = llvm::APInt::getAllOnes(numBits: Size);
20543 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
20544 }
20545 if (Init && OrigType->isAnyComplexType()) {
20546 // Init = 0xFFFF + 0xFFFFi;
20547 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
20548 Init = S.CreateBuiltinBinOp(OpLoc: ELoc, Opc: BO_Add, LHSExpr: Init, RHSExpr: Im).get();
20549 }
20550 Type = OrigType;
20551 break;
20552 }
20553 case BO_LT:
20554 case BO_GT: {
20555 // 'min' reduction op - initializer is 'Largest representable number in
20556 // the reduction list item type'.
20557 // 'max' reduction op - initializer is 'Least representable number in
20558 // the reduction list item type'.
20559 if (Type->isIntegerType() || Type->isPointerType()) {
20560 bool IsSigned = Type->hasSignedIntegerRepresentation();
20561 uint64_t Size = Context.getTypeSize(T: Type);
20562 QualType IntTy =
20563 Context.getIntTypeForBitwidth(DestWidth: Size, /*Signed=*/IsSigned);
20564 llvm::APInt InitValue =
20565 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(numBits: Size)
20566 : llvm::APInt::getMinValue(numBits: Size)
20567 : IsSigned ? llvm::APInt::getSignedMaxValue(numBits: Size)
20568 : llvm::APInt::getMaxValue(numBits: Size);
20569 Init = IntegerLiteral::Create(C: Context, V: InitValue, type: IntTy, l: ELoc);
20570 if (Type->isPointerType()) {
20571 // Cast to pointer type.
20572 ExprResult CastExpr = S.BuildCStyleCastExpr(
20573 LParenLoc: ELoc, Ty: Context.getTrivialTypeSourceInfo(T: Type, Loc: ELoc), RParenLoc: ELoc, Op: Init);
20574 if (CastExpr.isInvalid())
20575 continue;
20576 Init = CastExpr.get();
20577 }
20578 } else if (Type->isRealFloatingType()) {
20579 llvm::APFloat InitValue = llvm::APFloat::getLargest(
20580 Sem: Context.getFloatTypeSemantics(T: Type), Negative: BOK != BO_LT);
20581 Init = FloatingLiteral::Create(C: Context, V: InitValue, /*isexact=*/true,
20582 Type, L: ELoc);
20583 }
20584 break;
20585 }
20586 case BO_PtrMemD:
20587 case BO_PtrMemI:
20588 case BO_MulAssign:
20589 case BO_Div:
20590 case BO_Rem:
20591 case BO_Sub:
20592 case BO_Shl:
20593 case BO_Shr:
20594 case BO_LE:
20595 case BO_GE:
20596 case BO_EQ:
20597 case BO_NE:
20598 case BO_Cmp:
20599 case BO_AndAssign:
20600 case BO_XorAssign:
20601 case BO_OrAssign:
20602 case BO_Assign:
20603 case BO_AddAssign:
20604 case BO_SubAssign:
20605 case BO_DivAssign:
20606 case BO_RemAssign:
20607 case BO_ShlAssign:
20608 case BO_ShrAssign:
20609 case BO_Comma:
20610 llvm_unreachable("Unexpected reduction operation");
20611 }
20612 }
20613 if (Init && DeclareReductionRef.isUnset()) {
20614 S.AddInitializerToDecl(dcl: RHSVD, init: Init, /*DirectInit=*/false);
20615 // Store initializer for single element in private copy. Will be used
20616 // during codegen.
20617 PrivateVD->setInit(RHSVD->getInit());
20618 PrivateVD->setInitStyle(RHSVD->getInitStyle());
20619 } else if (!Init) {
20620 S.ActOnUninitializedDecl(dcl: RHSVD);
20621 // Store initializer for single element in private copy. Will be used
20622 // during codegen.
20623 PrivateVD->setInit(RHSVD->getInit());
20624 PrivateVD->setInitStyle(RHSVD->getInitStyle());
20625 }
20626 if (RHSVD->isInvalidDecl())
20627 continue;
20628 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
20629 S.Diag(Loc: ELoc, DiagID: diag::err_omp_reduction_id_not_compatible)
20630 << Type << ReductionIdRange;
20631 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
20632 VarDecl::DeclarationOnly;
20633 S.Diag(Loc: D->getLocation(),
20634 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20635 << D;
20636 continue;
20637 }
20638 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, D: PrivateVD, Ty: PrivateTy, Loc: ELoc);
20639 ExprResult ReductionOp;
20640 if (DeclareReductionRef.isUsable()) {
20641 QualType RedTy = DeclareReductionRef.get()->getType();
20642 QualType PtrRedTy = Context.getPointerType(T: RedTy);
20643 ExprResult LHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: LHSDRE);
20644 ExprResult RHS = S.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: RHSDRE);
20645 if (!BasePath.empty()) {
20646 LHS = S.DefaultLvalueConversion(E: LHS.get());
20647 RHS = S.DefaultLvalueConversion(E: RHS.get());
20648 LHS = ImplicitCastExpr::Create(
20649 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: LHS.get(), BasePath: &BasePath,
20650 Cat: LHS.get()->getValueKind(), FPO: FPOptionsOverride());
20651 RHS = ImplicitCastExpr::Create(
20652 Context, T: PtrRedTy, Kind: CK_UncheckedDerivedToBase, Operand: RHS.get(), BasePath: &BasePath,
20653 Cat: RHS.get()->getValueKind(), FPO: FPOptionsOverride());
20654 }
20655 FunctionProtoType::ExtProtoInfo EPI;
20656 QualType Params[] = {PtrRedTy, PtrRedTy};
20657 QualType FnTy = Context.getFunctionType(ResultTy: Context.VoidTy, Args: Params, EPI);
20658 auto *OVE = new (Context) OpaqueValueExpr(
20659 ELoc, Context.getPointerType(T: FnTy), VK_PRValue, OK_Ordinary,
20660 S.DefaultLvalueConversion(E: DeclareReductionRef.get()).get());
20661 Expr *Args[] = {LHS.get(), RHS.get()};
20662 ReductionOp =
20663 CallExpr::Create(Ctx: Context, Fn: OVE, Args, Ty: Context.VoidTy, VK: VK_PRValue, RParenLoc: ELoc,
20664 FPFeatures: S.CurFPFeatureOverrides());
20665 } else {
20666 BinaryOperatorKind CombBOK = getRelatedCompoundReductionOp(BOK);
20667 if (Type->isRecordType() && CombBOK != BOK) {
20668 Sema::TentativeAnalysisScope Trap(S);
20669 ReductionOp =
20670 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20671 Opc: CombBOK, LHSExpr: LHSDRE, RHSExpr: RHSDRE);
20672 }
20673 if (!ReductionOp.isUsable()) {
20674 ReductionOp =
20675 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(), Opc: BOK,
20676 LHSExpr: LHSDRE, RHSExpr: RHSDRE);
20677 if (ReductionOp.isUsable()) {
20678 if (BOK != BO_LT && BOK != BO_GT) {
20679 ReductionOp =
20680 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20681 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ReductionOp.get());
20682 } else {
20683 auto *ConditionalOp = new (Context)
20684 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc,
20685 RHSDRE, Type, VK_LValue, OK_Ordinary);
20686 ReductionOp =
20687 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ReductionId.getBeginLoc(),
20688 Opc: BO_Assign, LHSExpr: LHSDRE, RHSExpr: ConditionalOp);
20689 }
20690 }
20691 }
20692 if (ReductionOp.isUsable())
20693 ReductionOp = S.ActOnFinishFullExpr(Expr: ReductionOp.get(),
20694 /*DiscardedValue=*/false);
20695 if (!ReductionOp.isUsable())
20696 continue;
20697 }
20698
20699 // Add copy operations for inscan reductions.
20700 // LHS = RHS;
20701 ExprResult CopyOpRes, TempArrayRes, TempArrayElem;
20702 if (ClauseKind == OMPC_reduction &&
20703 RD.RedModifier == OMPC_REDUCTION_inscan) {
20704 ExprResult RHS = S.DefaultLvalueConversion(E: RHSDRE);
20705 CopyOpRes = S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: LHSDRE,
20706 RHSExpr: RHS.get());
20707 if (!CopyOpRes.isUsable())
20708 continue;
20709 CopyOpRes =
20710 S.ActOnFinishFullExpr(Expr: CopyOpRes.get(), /*DiscardedValue=*/true);
20711 if (!CopyOpRes.isUsable())
20712 continue;
20713 // For simd directive and simd-based directives in simd mode no need to
20714 // construct temp array, need just a single temp element.
20715 if (Stack->getCurrentDirective() == OMPD_simd ||
20716 (S.getLangOpts().OpenMPSimd &&
20717 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()))) {
20718 VarDecl *TempArrayVD =
20719 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: PrivateTy, Name: D->getName(),
20720 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20721 // Add a constructor to the temp decl.
20722 S.ActOnUninitializedDecl(dcl: TempArrayVD);
20723 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: PrivateTy, Loc: ELoc);
20724 } else {
20725 // Build temp array for prefix sum.
20726 auto *Dim = new (S.Context)
20727 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
20728 QualType ArrayTy = S.Context.getVariableArrayType(
20729 EltTy: PrivateTy, NumElts: Dim, ASM: ArraySizeModifier::Normal,
20730 /*IndexTypeQuals=*/0);
20731 VarDecl *TempArrayVD =
20732 buildVarDecl(SemaRef&: S, Loc: ELoc, Type: ArrayTy, Name: D->getName(),
20733 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
20734 // Add a constructor to the temp decl.
20735 S.ActOnUninitializedDecl(dcl: TempArrayVD);
20736 TempArrayRes = buildDeclRefExpr(S, D: TempArrayVD, Ty: ArrayTy, Loc: ELoc);
20737 TempArrayElem =
20738 S.DefaultFunctionArrayLvalueConversion(E: TempArrayRes.get());
20739 auto *Idx = new (S.Context)
20740 OpaqueValueExpr(ELoc, S.Context.getSizeType(), VK_PRValue);
20741 TempArrayElem = S.CreateBuiltinArraySubscriptExpr(Base: TempArrayElem.get(),
20742 LLoc: ELoc, Idx, RLoc: ELoc);
20743 }
20744 }
20745
20746 // OpenMP [2.15.4.6, Restrictions, p.2]
20747 // A list item that appears in an in_reduction clause of a task construct
20748 // must appear in a task_reduction clause of a construct associated with a
20749 // taskgroup region that includes the participating task in its taskgroup
20750 // set. The construct associated with the innermost region that meets this
20751 // condition must specify the same reduction-identifier as the in_reduction
20752 // clause.
20753 if (ClauseKind == OMPC_in_reduction) {
20754 SourceRange ParentSR;
20755 BinaryOperatorKind ParentBOK;
20756 const Expr *ParentReductionOp = nullptr;
20757 Expr *ParentBOKTD = nullptr, *ParentReductionOpTD = nullptr;
20758 DSAStackTy::DSAVarData ParentBOKDSA =
20759 Stack->getTopMostTaskgroupReductionData(D, SR&: ParentSR, BOK&: ParentBOK,
20760 TaskgroupDescriptor&: ParentBOKTD);
20761 DSAStackTy::DSAVarData ParentReductionOpDSA =
20762 Stack->getTopMostTaskgroupReductionData(
20763 D, SR&: ParentSR, ReductionRef&: ParentReductionOp, TaskgroupDescriptor&: ParentReductionOpTD);
20764 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
20765 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
20766 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
20767 (DeclareReductionRef.isUsable() && IsParentBOK) ||
20768 (IsParentBOK && BOK != ParentBOK) || IsParentReductionOp) {
20769 bool EmitError = true;
20770 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
20771 llvm::FoldingSetNodeID RedId, ParentRedId;
20772 ParentReductionOp->Profile(ID&: ParentRedId, Context, /*Canonical=*/true);
20773 DeclareReductionRef.get()->Profile(ID&: RedId, Context,
20774 /*Canonical=*/true);
20775 EmitError = RedId != ParentRedId;
20776 }
20777 if (EmitError) {
20778 S.Diag(Loc: ReductionId.getBeginLoc(),
20779 DiagID: diag::err_omp_reduction_identifier_mismatch)
20780 << ReductionIdRange << RefExpr->getSourceRange();
20781 S.Diag(Loc: ParentSR.getBegin(),
20782 DiagID: diag::note_omp_previous_reduction_identifier)
20783 << ParentSR
20784 << (IsParentBOK ? ParentBOKDSA.RefExpr
20785 : ParentReductionOpDSA.RefExpr)
20786 ->getSourceRange();
20787 continue;
20788 }
20789 }
20790 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
20791 }
20792
20793 DeclRefExpr *Ref = nullptr;
20794 Expr *VarsExpr = RefExpr->IgnoreParens();
20795 if (!VD && !S.CurContext->isDependentContext()) {
20796 if (ASE || OASE) {
20797 TransformExprToCaptures RebuildToCapture(S, D);
20798 VarsExpr =
20799 RebuildToCapture.TransformExpr(E: RefExpr->IgnoreParens()).get();
20800 Ref = RebuildToCapture.getCapturedExpr();
20801 } else {
20802 VarsExpr = Ref = buildCapture(S, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
20803 }
20804 if (!S.OpenMP().isOpenMPCapturedDecl(D)) {
20805 RD.ExprCaptures.emplace_back(Args: Ref->getDecl());
20806 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
20807 ExprResult RefRes = S.DefaultLvalueConversion(E: Ref);
20808 if (!RefRes.isUsable())
20809 continue;
20810 ExprResult PostUpdateRes =
20811 S.BuildBinOp(S: Stack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: SimpleRefExpr,
20812 RHSExpr: RefRes.get());
20813 if (!PostUpdateRes.isUsable())
20814 continue;
20815 if (isOpenMPTaskingDirective(Kind: Stack->getCurrentDirective()) ||
20816 Stack->getCurrentDirective() == OMPD_taskgroup) {
20817 S.Diag(Loc: RefExpr->getExprLoc(),
20818 DiagID: diag::err_omp_reduction_non_addressable_expression)
20819 << RefExpr->getSourceRange();
20820 continue;
20821 }
20822 RD.ExprPostUpdates.emplace_back(
20823 Args: S.IgnoredValueConversions(E: PostUpdateRes.get()).get());
20824 }
20825 }
20826 }
20827 // All reduction items are still marked as reduction (to do not increase
20828 // code base size).
20829 unsigned Modifier = RD.RedModifier;
20830 // Consider task_reductions as reductions with task modifier. Required for
20831 // correct analysis of in_reduction clauses.
20832 if (CurrDir == OMPD_taskgroup && ClauseKind == OMPC_task_reduction)
20833 Modifier = OMPC_REDUCTION_task;
20834 Stack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_reduction, PrivateCopy: Ref, Modifier,
20835 AppliedToPointee: ASE || OASE);
20836 if (Modifier == OMPC_REDUCTION_task &&
20837 (CurrDir == OMPD_taskgroup ||
20838 ((isOpenMPParallelDirective(DKind: CurrDir) ||
20839 isOpenMPWorksharingDirective(DKind: CurrDir)) &&
20840 !isOpenMPSimdDirective(DKind: CurrDir)))) {
20841 if (DeclareReductionRef.isUsable())
20842 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange,
20843 ReductionRef: DeclareReductionRef.get());
20844 else
20845 Stack->addTaskgroupReductionData(D, SR: ReductionIdRange, BOK);
20846 }
20847 RD.push(Item: VarsExpr, Private: PrivateDRE, LHS: LHSDRE, RHS: RHSDRE, ReductionOp: ReductionOp.get(),
20848 TaskgroupDescriptor, CopyOp: CopyOpRes.get(), CopyArrayTemp: TempArrayRes.get(),
20849 CopyArrayElem: TempArrayElem.get(), IsPrivate);
20850 }
20851 return RD.Vars.empty();
20852}
20853
20854OMPClause *SemaOpenMP::ActOnOpenMPReductionClause(
20855 ArrayRef<Expr *> VarList,
20856 OpenMPVarListDataTy::OpenMPReductionClauseModifiers Modifiers,
20857 SourceLocation StartLoc, SourceLocation LParenLoc,
20858 SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc,
20859 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20860 ArrayRef<Expr *> UnresolvedReductions) {
20861 OpenMPReductionClauseModifier Modifier =
20862 static_cast<OpenMPReductionClauseModifier>(Modifiers.ExtraModifier);
20863 OpenMPOriginalSharingModifier OriginalSharingModifier =
20864 static_cast<OpenMPOriginalSharingModifier>(
20865 Modifiers.OriginalSharingModifier);
20866 if (ModifierLoc.isValid() && Modifier == OMPC_REDUCTION_unknown) {
20867 Diag(Loc: LParenLoc, DiagID: diag::err_omp_unexpected_clause_value)
20868 << getListOfPossibleValues(K: OMPC_reduction, /*First=*/0,
20869 /*Last=*/OMPC_REDUCTION_unknown)
20870 << getOpenMPClauseNameForDiag(C: OMPC_reduction);
20871 return nullptr;
20872 }
20873 // OpenMP 5.0, 2.19.5.4 reduction Clause, Restrictions
20874 // A reduction clause with the inscan reduction-modifier may only appear on a
20875 // worksharing-loop construct, a worksharing-loop SIMD construct, a simd
20876 // construct, a parallel worksharing-loop construct or a parallel
20877 // worksharing-loop SIMD construct.
20878 if (Modifier == OMPC_REDUCTION_inscan &&
20879 (DSAStack->getCurrentDirective() != OMPD_for &&
20880 DSAStack->getCurrentDirective() != OMPD_for_simd &&
20881 DSAStack->getCurrentDirective() != OMPD_simd &&
20882 DSAStack->getCurrentDirective() != OMPD_parallel_for &&
20883 DSAStack->getCurrentDirective() != OMPD_parallel_for_simd)) {
20884 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_wrong_inscan_reduction);
20885 return nullptr;
20886 }
20887 ReductionData RD(VarList.size(), Modifier, OriginalSharingModifier);
20888 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_reduction, VarList,
20889 StartLoc, LParenLoc, ColonLoc, EndLoc,
20890 ReductionIdScopeSpec, ReductionId,
20891 UnresolvedReductions, RD))
20892 return nullptr;
20893
20894 return OMPReductionClause::Create(
20895 C: getASTContext(), StartLoc, LParenLoc, ModifierLoc, ColonLoc, EndLoc,
20896 Modifier, VL: RD.Vars,
20897 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
20898 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, CopyOps: RD.InscanCopyOps,
20899 CopyArrayTemps: RD.InscanCopyArrayTemps, CopyArrayElems: RD.InscanCopyArrayElems,
20900 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
20901 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates), IsPrivateVarReduction: RD.IsPrivateVarReduction,
20902 OriginalSharingModifier);
20903}
20904
20905OMPClause *SemaOpenMP::ActOnOpenMPTaskReductionClause(
20906 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
20907 SourceLocation ColonLoc, SourceLocation EndLoc,
20908 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20909 ArrayRef<Expr *> UnresolvedReductions) {
20910 ReductionData RD(VarList.size());
20911 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_task_reduction,
20912 VarList, StartLoc, LParenLoc, ColonLoc,
20913 EndLoc, ReductionIdScopeSpec, ReductionId,
20914 UnresolvedReductions, RD))
20915 return nullptr;
20916
20917 return OMPTaskReductionClause::Create(
20918 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
20919 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
20920 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps,
20921 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
20922 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
20923}
20924
20925OMPClause *SemaOpenMP::ActOnOpenMPInReductionClause(
20926 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
20927 SourceLocation ColonLoc, SourceLocation EndLoc,
20928 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
20929 ArrayRef<Expr *> UnresolvedReductions) {
20930 ReductionData RD(VarList.size());
20931 if (actOnOMPReductionKindClause(S&: SemaRef, DSAStack, ClauseKind: OMPC_in_reduction, VarList,
20932 StartLoc, LParenLoc, ColonLoc, EndLoc,
20933 ReductionIdScopeSpec, ReductionId,
20934 UnresolvedReductions, RD))
20935 return nullptr;
20936
20937 return OMPInReductionClause::Create(
20938 C: getASTContext(), StartLoc, LParenLoc, ColonLoc, EndLoc, VL: RD.Vars,
20939 QualifierLoc: ReductionIdScopeSpec.getWithLocInContext(Context&: getASTContext()), NameInfo: ReductionId,
20940 Privates: RD.Privates, LHSExprs: RD.LHSs, RHSExprs: RD.RHSs, ReductionOps: RD.ReductionOps, TaskgroupDescriptors: RD.TaskgroupDescriptors,
20941 PreInit: buildPreInits(Context&: getASTContext(), PreInits: RD.ExprCaptures),
20942 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: RD.ExprPostUpdates));
20943}
20944
20945bool SemaOpenMP::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
20946 SourceLocation LinLoc) {
20947 if ((!getLangOpts().CPlusPlus && LinKind != OMPC_LINEAR_val) ||
20948 LinKind == OMPC_LINEAR_unknown || LinKind == OMPC_LINEAR_step) {
20949 Diag(Loc: LinLoc, DiagID: diag::err_omp_wrong_linear_modifier)
20950 << getLangOpts().CPlusPlus;
20951 return true;
20952 }
20953 return false;
20954}
20955
20956bool SemaOpenMP::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
20957 OpenMPLinearClauseKind LinKind,
20958 QualType Type, bool IsDeclareSimd) {
20959 const auto *VD = dyn_cast_or_null<VarDecl>(Val: D);
20960 // A variable must not have an incomplete type or a reference type.
20961 if (SemaRef.RequireCompleteType(Loc: ELoc, T: Type,
20962 DiagID: diag::err_omp_linear_incomplete_type))
20963 return true;
20964 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
20965 !Type->isReferenceType()) {
20966 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_linear_modifier_non_reference)
20967 << Type << getOpenMPSimpleClauseTypeName(Kind: OMPC_linear, Type: LinKind);
20968 return true;
20969 }
20970 Type = Type.getNonReferenceType();
20971
20972 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
20973 // A variable that is privatized must not have a const-qualified type
20974 // unless it is of class type with a mutable member. This restriction does
20975 // not apply to the firstprivate clause, nor to the linear clause on
20976 // declarative directives (like declare simd).
20977 if (!IsDeclareSimd &&
20978 rejectConstNotMutableType(SemaRef, D, Type, CKind: OMPC_linear, ELoc))
20979 return true;
20980
20981 // A list item must be of integral or pointer type.
20982 Type = Type.getUnqualifiedType().getCanonicalType();
20983 const auto *Ty = Type.getTypePtrOrNull();
20984 if (!Ty || (LinKind != OMPC_LINEAR_ref && !Ty->isDependentType() &&
20985 !Ty->isIntegralType(Ctx: getASTContext()) && !Ty->isPointerType())) {
20986 Diag(Loc: ELoc, DiagID: diag::err_omp_linear_expected_int_or_ptr) << Type;
20987 if (D) {
20988 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
20989 VarDecl::DeclarationOnly;
20990 Diag(Loc: D->getLocation(),
20991 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
20992 << D;
20993 }
20994 return true;
20995 }
20996 return false;
20997}
20998
20999OMPClause *SemaOpenMP::ActOnOpenMPLinearClause(
21000 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
21001 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
21002 SourceLocation LinLoc, SourceLocation ColonLoc,
21003 SourceLocation StepModifierLoc, SourceLocation EndLoc) {
21004 SmallVector<Expr *, 8> Vars;
21005 SmallVector<Expr *, 8> Privates;
21006 SmallVector<Expr *, 8> Inits;
21007 SmallVector<Decl *, 4> ExprCaptures;
21008 SmallVector<Expr *, 4> ExprPostUpdates;
21009 // OpenMP 5.2 [Section 5.4.6, linear clause]
21010 // step-simple-modifier is exclusive, can't be used with 'val', 'uval', or
21011 // 'ref'
21012 if (LinLoc.isValid() && StepModifierLoc.isInvalid() && Step &&
21013 getLangOpts().OpenMP >= 52)
21014 Diag(Loc: Step->getBeginLoc(), DiagID: diag::err_omp_step_simple_modifier_exclusive);
21015 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
21016 LinKind = OMPC_LINEAR_val;
21017 for (Expr *RefExpr : VarList) {
21018 assert(RefExpr && "NULL expr in OpenMP linear clause.");
21019 SourceLocation ELoc;
21020 SourceRange ERange;
21021 Expr *SimpleRefExpr = RefExpr;
21022 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21023 if (Res.second) {
21024 // It will be analyzed later.
21025 Vars.push_back(Elt: RefExpr);
21026 Privates.push_back(Elt: nullptr);
21027 Inits.push_back(Elt: nullptr);
21028 }
21029 ValueDecl *D = Res.first;
21030 if (!D)
21031 continue;
21032
21033 QualType Type = D->getType();
21034 auto *VD = dyn_cast<VarDecl>(Val: D);
21035
21036 // OpenMP [2.14.3.7, linear clause]
21037 // A list-item cannot appear in more than one linear clause.
21038 // A list-item that appears in a linear clause cannot appear in any
21039 // other data-sharing attribute clause.
21040 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
21041 if (DVar.RefExpr) {
21042 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21043 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21044 << getOpenMPClauseNameForDiag(C: OMPC_linear);
21045 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21046 continue;
21047 }
21048
21049 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
21050 continue;
21051 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
21052
21053 // Build private copy of original var.
21054 VarDecl *Private =
21055 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
21056 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
21057 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
21058 DeclRefExpr *PrivateRef = buildDeclRefExpr(S&: SemaRef, D: Private, Ty: Type, Loc: ELoc);
21059 // Build var to save initial value.
21060 VarDecl *Init = buildVarDecl(SemaRef, Loc: ELoc, Type, Name: ".linear.start");
21061 Expr *InitExpr;
21062 DeclRefExpr *Ref = nullptr;
21063 if (!VD && !SemaRef.CurContext->isDependentContext()) {
21064 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
21065 if (!isOpenMPCapturedDecl(D)) {
21066 ExprCaptures.push_back(Elt: Ref->getDecl());
21067 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
21068 ExprResult RefRes = SemaRef.DefaultLvalueConversion(E: Ref);
21069 if (!RefRes.isUsable())
21070 continue;
21071 ExprResult PostUpdateRes =
21072 SemaRef.BuildBinOp(DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign,
21073 LHSExpr: SimpleRefExpr, RHSExpr: RefRes.get());
21074 if (!PostUpdateRes.isUsable())
21075 continue;
21076 ExprPostUpdates.push_back(
21077 Elt: SemaRef.IgnoredValueConversions(E: PostUpdateRes.get()).get());
21078 }
21079 }
21080 }
21081 if (LinKind == OMPC_LINEAR_uval)
21082 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
21083 else
21084 InitExpr = VD ? SimpleRefExpr : Ref;
21085 SemaRef.AddInitializerToDecl(
21086 dcl: Init, init: SemaRef.DefaultLvalueConversion(E: InitExpr).get(),
21087 /*DirectInit=*/false);
21088 DeclRefExpr *InitRef = buildDeclRefExpr(S&: SemaRef, D: Init, Ty: Type, Loc: ELoc);
21089
21090 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_linear, PrivateCopy: Ref);
21091 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
21092 ? RefExpr->IgnoreParens()
21093 : Ref);
21094 Privates.push_back(Elt: PrivateRef);
21095 Inits.push_back(Elt: InitRef);
21096 }
21097
21098 if (Vars.empty())
21099 return nullptr;
21100
21101 Expr *StepExpr = Step;
21102 Expr *CalcStepExpr = nullptr;
21103 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
21104 !Step->isInstantiationDependent() &&
21105 !Step->containsUnexpandedParameterPack()) {
21106 SourceLocation StepLoc = Step->getBeginLoc();
21107 ExprResult Val = PerformOpenMPImplicitIntegerConversion(Loc: StepLoc, Op: Step);
21108 if (Val.isInvalid())
21109 return nullptr;
21110 StepExpr = Val.get();
21111
21112 // Build var to save the step value.
21113 VarDecl *SaveVar =
21114 buildVarDecl(SemaRef, Loc: StepLoc, Type: StepExpr->getType(), Name: ".linear.step");
21115 ExprResult SaveRef =
21116 buildDeclRefExpr(S&: SemaRef, D: SaveVar, Ty: StepExpr->getType(), Loc: StepLoc);
21117 ExprResult CalcStep = SemaRef.BuildBinOp(
21118 S: SemaRef.getCurScope(), OpLoc: StepLoc, Opc: BO_Assign, LHSExpr: SaveRef.get(), RHSExpr: StepExpr);
21119 CalcStep =
21120 SemaRef.ActOnFinishFullExpr(Expr: CalcStep.get(), /*DiscardedValue=*/false);
21121
21122 // Warn about zero linear step (it would be probably better specified as
21123 // making corresponding variables 'const').
21124 if (std::optional<llvm::APSInt> Result =
21125 StepExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
21126 if (!Result->isNegative() && !Result->isStrictlyPositive())
21127 Diag(Loc: StepLoc, DiagID: diag::warn_omp_linear_step_zero)
21128 << Vars[0] << (Vars.size() > 1);
21129 } else if (CalcStep.isUsable()) {
21130 // Calculate the step beforehand instead of doing this on each iteration.
21131 // (This is not used if the number of iterations may be kfold-ed).
21132 CalcStepExpr = CalcStep.get();
21133 }
21134 }
21135
21136 return OMPLinearClause::Create(C: getASTContext(), StartLoc, LParenLoc, Modifier: LinKind,
21137 ModifierLoc: LinLoc, ColonLoc, StepModifierLoc, EndLoc,
21138 VL: Vars, PL: Privates, IL: Inits, Step: StepExpr, CalcStep: CalcStepExpr,
21139 PreInit: buildPreInits(Context&: getASTContext(), PreInits: ExprCaptures),
21140 PostUpdate: buildPostUpdate(S&: SemaRef, PostUpdates: ExprPostUpdates));
21141}
21142
21143static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
21144 Expr *NumIterations, Sema &SemaRef,
21145 Scope *S, DSAStackTy *Stack) {
21146 // Walk the vars and build update/final expressions for the CodeGen.
21147 SmallVector<Expr *, 8> Updates;
21148 SmallVector<Expr *, 8> Finals;
21149 SmallVector<Expr *, 8> UsedExprs;
21150 Expr *Step = Clause.getStep();
21151 Expr *CalcStep = Clause.getCalcStep();
21152 // OpenMP [2.14.3.7, linear clause]
21153 // If linear-step is not specified it is assumed to be 1.
21154 if (!Step)
21155 Step = SemaRef.ActOnIntegerConstant(Loc: SourceLocation(), Val: 1).get();
21156 else if (CalcStep)
21157 Step = cast<BinaryOperator>(Val: CalcStep)->getLHS();
21158 bool HasErrors = false;
21159 auto CurInit = Clause.inits().begin();
21160 auto CurPrivate = Clause.privates().begin();
21161 OpenMPLinearClauseKind LinKind = Clause.getModifier();
21162 for (Expr *RefExpr : Clause.varlist()) {
21163 SourceLocation ELoc;
21164 SourceRange ERange;
21165 Expr *SimpleRefExpr = RefExpr;
21166 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21167 ValueDecl *D = Res.first;
21168 if (Res.second || !D) {
21169 Updates.push_back(Elt: nullptr);
21170 Finals.push_back(Elt: nullptr);
21171 HasErrors = true;
21172 continue;
21173 }
21174 auto &&Info = Stack->isLoopControlVariable(D);
21175 // OpenMP [2.15.11, distribute simd Construct]
21176 // A list item may not appear in a linear clause, unless it is the loop
21177 // iteration variable.
21178 if (isOpenMPDistributeDirective(DKind: Stack->getCurrentDirective()) &&
21179 isOpenMPSimdDirective(DKind: Stack->getCurrentDirective()) && !Info.first) {
21180 SemaRef.Diag(Loc: ELoc,
21181 DiagID: diag::err_omp_linear_distribute_var_non_loop_iteration);
21182 Updates.push_back(Elt: nullptr);
21183 Finals.push_back(Elt: nullptr);
21184 HasErrors = true;
21185 continue;
21186 }
21187 Expr *InitExpr = *CurInit;
21188
21189 // Build privatized reference to the current linear var.
21190 auto *DE = cast<DeclRefExpr>(Val: SimpleRefExpr);
21191 Expr *CapturedRef;
21192 if (LinKind == OMPC_LINEAR_uval)
21193 CapturedRef = cast<VarDecl>(Val: DE->getDecl())->getInit();
21194 else
21195 CapturedRef =
21196 buildDeclRefExpr(S&: SemaRef, D: cast<VarDecl>(Val: DE->getDecl()),
21197 Ty: DE->getType().getUnqualifiedType(), Loc: DE->getExprLoc(),
21198 /*RefersToCapture=*/true);
21199
21200 // Build update: Var = InitExpr + IV * Step
21201 ExprResult Update;
21202 if (!Info.first)
21203 Update = buildCounterUpdate(
21204 SemaRef, S, Loc: RefExpr->getExprLoc(), VarRef: *CurPrivate, Start: InitExpr, Iter: IV, Step,
21205 /*Subtract=*/false, /*IsNonRectangularLB=*/false);
21206 else
21207 Update = *CurPrivate;
21208 Update = SemaRef.ActOnFinishFullExpr(Expr: Update.get(), CC: DE->getBeginLoc(),
21209 /*DiscardedValue=*/false);
21210
21211 // Build final: Var = PrivCopy;
21212 ExprResult Final;
21213 if (!Info.first)
21214 Final = SemaRef.BuildBinOp(
21215 S, OpLoc: RefExpr->getExprLoc(), Opc: BO_Assign, LHSExpr: CapturedRef,
21216 RHSExpr: SemaRef.DefaultLvalueConversion(E: *CurPrivate).get());
21217 else
21218 Final = *CurPrivate;
21219 Final = SemaRef.ActOnFinishFullExpr(Expr: Final.get(), CC: DE->getBeginLoc(),
21220 /*DiscardedValue=*/false);
21221
21222 if (!Update.isUsable() || !Final.isUsable()) {
21223 Updates.push_back(Elt: nullptr);
21224 Finals.push_back(Elt: nullptr);
21225 UsedExprs.push_back(Elt: nullptr);
21226 HasErrors = true;
21227 } else {
21228 Updates.push_back(Elt: Update.get());
21229 Finals.push_back(Elt: Final.get());
21230 if (!Info.first)
21231 UsedExprs.push_back(Elt: SimpleRefExpr);
21232 }
21233 ++CurInit;
21234 ++CurPrivate;
21235 }
21236 if (Expr *S = Clause.getStep())
21237 UsedExprs.push_back(Elt: S);
21238 // Fill the remaining part with the nullptr.
21239 UsedExprs.append(NumInputs: Clause.varlist_size() + 1 - UsedExprs.size(), Elt: nullptr);
21240 Clause.setUpdates(Updates);
21241 Clause.setFinals(Finals);
21242 Clause.setUsedExprs(UsedExprs);
21243 return HasErrors;
21244}
21245
21246OMPClause *SemaOpenMP::ActOnOpenMPAlignedClause(
21247 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
21248 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
21249 SmallVector<Expr *, 8> Vars;
21250 for (Expr *RefExpr : VarList) {
21251 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
21252 SourceLocation ELoc;
21253 SourceRange ERange;
21254 Expr *SimpleRefExpr = RefExpr;
21255 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21256 if (Res.second) {
21257 // It will be analyzed later.
21258 Vars.push_back(Elt: RefExpr);
21259 }
21260 ValueDecl *D = Res.first;
21261 if (!D)
21262 continue;
21263
21264 QualType QType = D->getType();
21265 auto *VD = dyn_cast<VarDecl>(Val: D);
21266
21267 // OpenMP [2.8.1, simd construct, Restrictions]
21268 // The type of list items appearing in the aligned clause must be
21269 // array, pointer, reference to array, or reference to pointer.
21270 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
21271 const Type *Ty = QType.getTypePtrOrNull();
21272 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
21273 Diag(Loc: ELoc, DiagID: diag::err_omp_aligned_expected_array_or_ptr)
21274 << QType << getLangOpts().CPlusPlus << ERange;
21275 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21276 VarDecl::DeclarationOnly;
21277 Diag(Loc: D->getLocation(),
21278 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21279 << D;
21280 continue;
21281 }
21282
21283 // OpenMP [2.8.1, simd construct, Restrictions]
21284 // A list-item cannot appear in more than one aligned clause.
21285 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, NewDE: SimpleRefExpr)) {
21286 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
21287 << 0 << getOpenMPClauseNameForDiag(C: OMPC_aligned) << ERange;
21288 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
21289 << getOpenMPClauseNameForDiag(C: OMPC_aligned);
21290 continue;
21291 }
21292
21293 DeclRefExpr *Ref = nullptr;
21294 if (!VD && isOpenMPCapturedDecl(D))
21295 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
21296 Vars.push_back(Elt: SemaRef
21297 .DefaultFunctionArrayConversion(
21298 E: (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
21299 .get());
21300 }
21301
21302 // OpenMP [2.8.1, simd construct, Description]
21303 // The parameter of the aligned clause, alignment, must be a constant
21304 // positive integer expression.
21305 // If no optional parameter is specified, implementation-defined default
21306 // alignments for SIMD instructions on the target platforms are assumed.
21307 if (Alignment != nullptr) {
21308 ExprResult AlignResult =
21309 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_aligned);
21310 if (AlignResult.isInvalid())
21311 return nullptr;
21312 Alignment = AlignResult.get();
21313 }
21314 if (Vars.empty())
21315 return nullptr;
21316
21317 return OMPAlignedClause::Create(C: getASTContext(), StartLoc, LParenLoc,
21318 ColonLoc, EndLoc, VL: Vars, A: Alignment);
21319}
21320
21321OMPClause *SemaOpenMP::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
21322 SourceLocation StartLoc,
21323 SourceLocation LParenLoc,
21324 SourceLocation EndLoc) {
21325 SmallVector<Expr *, 8> Vars;
21326 SmallVector<Expr *, 8> SrcExprs;
21327 SmallVector<Expr *, 8> DstExprs;
21328 SmallVector<Expr *, 8> AssignmentOps;
21329 for (Expr *RefExpr : VarList) {
21330 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
21331 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21332 // It will be analyzed later.
21333 Vars.push_back(Elt: RefExpr);
21334 SrcExprs.push_back(Elt: nullptr);
21335 DstExprs.push_back(Elt: nullptr);
21336 AssignmentOps.push_back(Elt: nullptr);
21337 continue;
21338 }
21339
21340 SourceLocation ELoc = RefExpr->getExprLoc();
21341 // OpenMP [2.1, C/C++]
21342 // A list item is a variable name.
21343 // OpenMP [2.14.4.1, Restrictions, p.1]
21344 // A list item that appears in a copyin clause must be threadprivate.
21345 auto *DE = dyn_cast<DeclRefExpr>(Val: RefExpr);
21346 if (!DE || !isa<VarDecl>(Val: DE->getDecl())) {
21347 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_var_name_member_expr)
21348 << 0 << RefExpr->getSourceRange();
21349 continue;
21350 }
21351
21352 Decl *D = DE->getDecl();
21353 auto *VD = cast<VarDecl>(Val: D);
21354
21355 QualType Type = VD->getType();
21356 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
21357 // It will be analyzed later.
21358 Vars.push_back(Elt: DE);
21359 SrcExprs.push_back(Elt: nullptr);
21360 DstExprs.push_back(Elt: nullptr);
21361 AssignmentOps.push_back(Elt: nullptr);
21362 continue;
21363 }
21364
21365 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
21366 // A list item that appears in a copyin clause must be threadprivate.
21367 if (!DSAStack->isThreadPrivate(D: VD)) {
21368 unsigned OMPVersion = getLangOpts().OpenMP;
21369 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21370 << getOpenMPClauseNameForDiag(C: OMPC_copyin)
21371 << getOpenMPDirectiveName(D: OMPD_threadprivate, Ver: OMPVersion);
21372 continue;
21373 }
21374
21375 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
21376 // A variable of class type (or array thereof) that appears in a
21377 // copyin clause requires an accessible, unambiguous copy assignment
21378 // operator for the class type.
21379 QualType ElemType =
21380 getASTContext().getBaseElementType(QT: Type).getNonReferenceType();
21381 VarDecl *SrcVD =
21382 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType.getUnqualifiedType(),
21383 Name: ".copyin.src", Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
21384 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
21385 S&: SemaRef, D: SrcVD, Ty: ElemType.getUnqualifiedType(), Loc: DE->getExprLoc());
21386 VarDecl *DstVD =
21387 buildVarDecl(SemaRef, Loc: DE->getBeginLoc(), Type: ElemType, Name: ".copyin.dst",
21388 Attrs: VD->hasAttrs() ? &VD->getAttrs() : nullptr);
21389 DeclRefExpr *PseudoDstExpr =
21390 buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: ElemType, Loc: DE->getExprLoc());
21391 // For arrays generate assignment operation for single element and replace
21392 // it by the original array element in CodeGen.
21393 ExprResult AssignmentOp =
21394 SemaRef.BuildBinOp(/*S=*/nullptr, OpLoc: DE->getExprLoc(), Opc: BO_Assign,
21395 LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
21396 if (AssignmentOp.isInvalid())
21397 continue;
21398 AssignmentOp =
21399 SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: DE->getExprLoc(),
21400 /*DiscardedValue=*/false);
21401 if (AssignmentOp.isInvalid())
21402 continue;
21403
21404 DSAStack->addDSA(D: VD, E: DE, A: OMPC_copyin);
21405 Vars.push_back(Elt: DE);
21406 SrcExprs.push_back(Elt: PseudoSrcExpr);
21407 DstExprs.push_back(Elt: PseudoDstExpr);
21408 AssignmentOps.push_back(Elt: AssignmentOp.get());
21409 }
21410
21411 if (Vars.empty())
21412 return nullptr;
21413
21414 return OMPCopyinClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21415 VL: Vars, SrcExprs, DstExprs, AssignmentOps);
21416}
21417
21418OMPClause *SemaOpenMP::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
21419 SourceLocation StartLoc,
21420 SourceLocation LParenLoc,
21421 SourceLocation EndLoc) {
21422 SmallVector<Expr *, 8> Vars;
21423 SmallVector<Expr *, 8> SrcExprs;
21424 SmallVector<Expr *, 8> DstExprs;
21425 SmallVector<Expr *, 8> AssignmentOps;
21426 for (Expr *RefExpr : VarList) {
21427 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
21428 SourceLocation ELoc;
21429 SourceRange ERange;
21430 Expr *SimpleRefExpr = RefExpr;
21431 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
21432 if (Res.second) {
21433 // It will be analyzed later.
21434 Vars.push_back(Elt: RefExpr);
21435 SrcExprs.push_back(Elt: nullptr);
21436 DstExprs.push_back(Elt: nullptr);
21437 AssignmentOps.push_back(Elt: nullptr);
21438 }
21439 ValueDecl *D = Res.first;
21440 if (!D)
21441 continue;
21442
21443 QualType Type = D->getType();
21444 auto *VD = dyn_cast<VarDecl>(Val: D);
21445
21446 // OpenMP [2.14.4.2, Restrictions, p.2]
21447 // A list item that appears in a copyprivate clause may not appear in a
21448 // private or firstprivate clause on the single construct.
21449 if (!VD || !DSAStack->isThreadPrivate(D: VD)) {
21450 DSAStackTy::DSAVarData DVar =
21451 DSAStack->getTopDSA(D, /*FromParent=*/false);
21452 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
21453 DVar.RefExpr) {
21454 Diag(Loc: ELoc, DiagID: diag::err_omp_wrong_dsa)
21455 << getOpenMPClauseNameForDiag(C: DVar.CKind)
21456 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate);
21457 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21458 continue;
21459 }
21460
21461 // OpenMP [2.11.4.2, Restrictions, p.1]
21462 // All list items that appear in a copyprivate clause must be either
21463 // threadprivate or private in the enclosing context.
21464 if (DVar.CKind == OMPC_unknown) {
21465 DVar = DSAStack->getImplicitDSA(D, FromParent: false);
21466 if (DVar.CKind == OMPC_shared) {
21467 Diag(Loc: ELoc, DiagID: diag::err_omp_required_access)
21468 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate)
21469 << "threadprivate or private in the enclosing context";
21470 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
21471 continue;
21472 }
21473 }
21474 }
21475
21476 // Variably modified types are not supported.
21477 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
21478 unsigned OMPVersion = getLangOpts().OpenMP;
21479 Diag(Loc: ELoc, DiagID: diag::err_omp_variably_modified_type_not_supported)
21480 << getOpenMPClauseNameForDiag(C: OMPC_copyprivate) << Type
21481 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
21482 Ver: OMPVersion);
21483 bool IsDecl = !VD || VD->isThisDeclarationADefinition(getASTContext()) ==
21484 VarDecl::DeclarationOnly;
21485 Diag(Loc: D->getLocation(),
21486 DiagID: IsDecl ? diag::note_previous_decl : diag::note_defined_here)
21487 << D;
21488 continue;
21489 }
21490
21491 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
21492 // A variable of class type (or array thereof) that appears in a
21493 // copyin clause requires an accessible, unambiguous copy assignment
21494 // operator for the class type.
21495 Type = getASTContext()
21496 .getBaseElementType(QT: Type.getNonReferenceType())
21497 .getUnqualifiedType();
21498 VarDecl *SrcVD =
21499 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.src",
21500 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21501 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(S&: SemaRef, D: SrcVD, Ty: Type, Loc: ELoc);
21502 VarDecl *DstVD =
21503 buildVarDecl(SemaRef, Loc: RefExpr->getBeginLoc(), Type, Name: ".copyprivate.dst",
21504 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr);
21505 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(S&: SemaRef, D: DstVD, Ty: Type, Loc: ELoc);
21506 ExprResult AssignmentOp = SemaRef.BuildBinOp(
21507 DSAStack->getCurScope(), OpLoc: ELoc, Opc: BO_Assign, LHSExpr: PseudoDstExpr, RHSExpr: PseudoSrcExpr);
21508 if (AssignmentOp.isInvalid())
21509 continue;
21510 AssignmentOp = SemaRef.ActOnFinishFullExpr(Expr: AssignmentOp.get(), CC: ELoc,
21511 /*DiscardedValue=*/false);
21512 if (AssignmentOp.isInvalid())
21513 continue;
21514
21515 // No need to mark vars as copyprivate, they are already threadprivate or
21516 // implicitly private.
21517 assert(VD || isOpenMPCapturedDecl(D));
21518 Vars.push_back(
21519 Elt: VD ? RefExpr->IgnoreParens()
21520 : buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false));
21521 SrcExprs.push_back(Elt: PseudoSrcExpr);
21522 DstExprs.push_back(Elt: PseudoDstExpr);
21523 AssignmentOps.push_back(Elt: AssignmentOp.get());
21524 }
21525
21526 if (Vars.empty())
21527 return nullptr;
21528
21529 return OMPCopyprivateClause::Create(C: getASTContext(), StartLoc, LParenLoc,
21530 EndLoc, VL: Vars, SrcExprs, DstExprs,
21531 AssignmentOps);
21532}
21533
21534OMPClause *SemaOpenMP::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
21535 SourceLocation StartLoc,
21536 SourceLocation LParenLoc,
21537 SourceLocation EndLoc) {
21538 if (VarList.empty())
21539 return nullptr;
21540
21541 return OMPFlushClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21542 VL: VarList);
21543}
21544
21545/// Tries to find omp_depend_t. type.
21546static bool findOMPDependT(Sema &S, SourceLocation Loc, DSAStackTy *Stack,
21547 bool Diagnose = true) {
21548 QualType OMPDependT = Stack->getOMPDependT();
21549 if (!OMPDependT.isNull())
21550 return true;
21551 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_depend_t");
21552 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
21553 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
21554 if (Diagnose)
21555 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_depend_t";
21556 return false;
21557 }
21558 Stack->setOMPDependT(PT.get());
21559 return true;
21560}
21561
21562OMPClause *SemaOpenMP::ActOnOpenMPDepobjClause(Expr *Depobj,
21563 SourceLocation StartLoc,
21564 SourceLocation LParenLoc,
21565 SourceLocation EndLoc) {
21566 if (!Depobj)
21567 return nullptr;
21568
21569 bool OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack);
21570
21571 // OpenMP 5.0, 2.17.10.1 depobj Construct
21572 // depobj is an lvalue expression of type omp_depend_t.
21573 if (!Depobj->isTypeDependent() && !Depobj->isValueDependent() &&
21574 !Depobj->isInstantiationDependent() &&
21575 !Depobj->containsUnexpandedParameterPack() &&
21576 (OMPDependTFound && !getASTContext().typesAreCompatible(
21577 DSAStack->getOMPDependT(), T2: Depobj->getType(),
21578 /*CompareUnqualified=*/true))) {
21579 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21580 << 0 << Depobj->getType() << Depobj->getSourceRange();
21581 }
21582
21583 if (!Depobj->isLValue()) {
21584 Diag(Loc: Depobj->getExprLoc(), DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21585 << 1 << Depobj->getSourceRange();
21586 }
21587
21588 return OMPDepobjClause::Create(C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21589 Depobj);
21590}
21591
21592namespace {
21593// Utility struct that gathers the related info for doacross clause.
21594struct DoacrossDataInfoTy {
21595 // The list of expressions.
21596 SmallVector<Expr *, 8> Vars;
21597 // The OperatorOffset for doacross loop.
21598 DSAStackTy::OperatorOffsetTy OpsOffs;
21599 // The depended loop count.
21600 llvm::APSInt TotalDepCount;
21601};
21602} // namespace
21603static DoacrossDataInfoTy
21604ProcessOpenMPDoacrossClauseCommon(Sema &SemaRef, bool IsSource,
21605 ArrayRef<Expr *> VarList, DSAStackTy *Stack,
21606 SourceLocation EndLoc) {
21607
21608 SmallVector<Expr *, 8> Vars;
21609 DSAStackTy::OperatorOffsetTy OpsOffs;
21610 llvm::APSInt DepCounter(/*BitWidth=*/32);
21611 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
21612
21613 if (const Expr *OrderedCountExpr =
21614 Stack->getParentOrderedRegionParam().first) {
21615 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Ctx: SemaRef.Context);
21616 TotalDepCount.setIsUnsigned(/*Val=*/true);
21617 }
21618
21619 for (Expr *RefExpr : VarList) {
21620 assert(RefExpr && "NULL expr in OpenMP doacross clause.");
21621 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21622 // It will be analyzed later.
21623 Vars.push_back(Elt: RefExpr);
21624 continue;
21625 }
21626
21627 SourceLocation ELoc = RefExpr->getExprLoc();
21628 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
21629 if (!IsSource) {
21630 if (Stack->getParentOrderedRegionParam().first &&
21631 DepCounter >= TotalDepCount) {
21632 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_unexpected_expr);
21633 continue;
21634 }
21635 ++DepCounter;
21636 // OpenMP [2.13.9, Summary]
21637 // depend(dependence-type : vec), where dependence-type is:
21638 // 'sink' and where vec is the iteration vector, which has the form:
21639 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
21640 // where n is the value specified by the ordered clause in the loop
21641 // directive, xi denotes the loop iteration variable of the i-th nested
21642 // loop associated with the loop directive, and di is a constant
21643 // non-negative integer.
21644 if (SemaRef.CurContext->isDependentContext()) {
21645 // It will be analyzed later.
21646 Vars.push_back(Elt: RefExpr);
21647 continue;
21648 }
21649 SimpleExpr = SimpleExpr->IgnoreImplicit();
21650 OverloadedOperatorKind OOK = OO_None;
21651 SourceLocation OOLoc;
21652 Expr *LHS = SimpleExpr;
21653 Expr *RHS = nullptr;
21654 if (auto *BO = dyn_cast<BinaryOperator>(Val: SimpleExpr)) {
21655 OOK = BinaryOperator::getOverloadedOperator(Opc: BO->getOpcode());
21656 OOLoc = BO->getOperatorLoc();
21657 LHS = BO->getLHS()->IgnoreParenImpCasts();
21658 RHS = BO->getRHS()->IgnoreParenImpCasts();
21659 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: SimpleExpr)) {
21660 OOK = OCE->getOperator();
21661 OOLoc = OCE->getOperatorLoc();
21662 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
21663 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
21664 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Val: SimpleExpr)) {
21665 OOK = MCE->getMethodDecl()
21666 ->getNameInfo()
21667 .getName()
21668 .getCXXOverloadedOperator();
21669 OOLoc = MCE->getCallee()->getExprLoc();
21670 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
21671 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
21672 }
21673 SourceLocation ELoc;
21674 SourceRange ERange;
21675 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: LHS, ELoc, ERange);
21676 if (Res.second) {
21677 // It will be analyzed later.
21678 Vars.push_back(Elt: RefExpr);
21679 }
21680 ValueDecl *D = Res.first;
21681 if (!D)
21682 continue;
21683
21684 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
21685 SemaRef.Diag(Loc: OOLoc, DiagID: diag::err_omp_depend_sink_expected_plus_minus);
21686 continue;
21687 }
21688 if (RHS) {
21689 ExprResult RHSRes =
21690 SemaRef.OpenMP().VerifyPositiveIntegerConstantInClause(
21691 E: RHS, CKind: OMPC_depend, /*StrictlyPositive=*/false);
21692 if (RHSRes.isInvalid())
21693 continue;
21694 }
21695 if (!SemaRef.CurContext->isDependentContext() &&
21696 Stack->getParentOrderedRegionParam().first &&
21697 DepCounter != Stack->isParentLoopControlVariable(D).first) {
21698 const ValueDecl *VD =
21699 Stack->getParentLoopControlVariable(I: DepCounter.getZExtValue());
21700 if (VD)
21701 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21702 << 1 << VD;
21703 else
21704 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21705 << 0;
21706 continue;
21707 }
21708 OpsOffs.emplace_back(Args&: RHS, Args&: OOK);
21709 }
21710 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
21711 }
21712 if (!SemaRef.CurContext->isDependentContext() && !IsSource &&
21713 TotalDepCount > VarList.size() &&
21714 Stack->getParentOrderedRegionParam().first &&
21715 Stack->getParentLoopControlVariable(I: VarList.size() + 1)) {
21716 SemaRef.Diag(Loc: EndLoc, DiagID: diag::err_omp_depend_sink_expected_loop_iteration)
21717 << 1 << Stack->getParentLoopControlVariable(I: VarList.size() + 1);
21718 }
21719 return {.Vars: Vars, .OpsOffs: OpsOffs, .TotalDepCount: TotalDepCount};
21720}
21721
21722OMPClause *SemaOpenMP::ActOnOpenMPDependClause(
21723 const OMPDependClause::DependDataTy &Data, Expr *DepModifier,
21724 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
21725 SourceLocation EndLoc) {
21726 OpenMPDependClauseKind DepKind = Data.DepKind;
21727 SourceLocation DepLoc = Data.DepLoc;
21728 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
21729 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
21730 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
21731 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_depend);
21732 return nullptr;
21733 }
21734 if (DSAStack->getCurrentDirective() == OMPD_taskwait &&
21735 DepKind == OMPC_DEPEND_mutexinoutset) {
21736 Diag(Loc: DepLoc, DiagID: diag::err_omp_taskwait_depend_mutexinoutset_not_allowed);
21737 return nullptr;
21738 }
21739 if ((DSAStack->getCurrentDirective() != OMPD_ordered ||
21740 DSAStack->getCurrentDirective() == OMPD_depobj) &&
21741 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
21742 DepKind == OMPC_DEPEND_sink ||
21743 ((getLangOpts().OpenMP < 50 ||
21744 DSAStack->getCurrentDirective() == OMPD_depobj) &&
21745 DepKind == OMPC_DEPEND_depobj))) {
21746 SmallVector<unsigned, 6> Except = {OMPC_DEPEND_source, OMPC_DEPEND_sink,
21747 OMPC_DEPEND_outallmemory,
21748 OMPC_DEPEND_inoutallmemory};
21749 if (getLangOpts().OpenMP < 50 ||
21750 DSAStack->getCurrentDirective() == OMPD_depobj)
21751 Except.push_back(Elt: OMPC_DEPEND_depobj);
21752 if (getLangOpts().OpenMP < 51)
21753 Except.push_back(Elt: OMPC_DEPEND_inoutset);
21754 std::string Expected = (getLangOpts().OpenMP >= 50 && !DepModifier)
21755 ? "depend modifier(iterator) or "
21756 : "";
21757 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
21758 << Expected + getListOfPossibleValues(K: OMPC_depend, /*First=*/0,
21759 /*Last=*/OMPC_DEPEND_unknown,
21760 Exclude: Except)
21761 << getOpenMPClauseNameForDiag(C: OMPC_depend);
21762 return nullptr;
21763 }
21764 if (DepModifier &&
21765 (DepKind == OMPC_DEPEND_source || DepKind == OMPC_DEPEND_sink)) {
21766 Diag(Loc: DepModifier->getExprLoc(),
21767 DiagID: diag::err_omp_depend_sink_source_with_modifier);
21768 return nullptr;
21769 }
21770 if (DepModifier &&
21771 !DepModifier->getType()->isSpecificBuiltinType(K: BuiltinType::OMPIterator))
21772 Diag(Loc: DepModifier->getExprLoc(), DiagID: diag::err_omp_depend_modifier_not_iterator);
21773
21774 SmallVector<Expr *, 8> Vars;
21775 DSAStackTy::OperatorOffsetTy OpsOffs;
21776 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
21777
21778 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
21779 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
21780 SemaRef, IsSource: DepKind == OMPC_DEPEND_source, VarList, DSAStack, EndLoc);
21781 Vars = VarOffset.Vars;
21782 OpsOffs = VarOffset.OpsOffs;
21783 TotalDepCount = VarOffset.TotalDepCount;
21784 } else {
21785 for (Expr *RefExpr : VarList) {
21786 assert(RefExpr && "NULL expr in OpenMP depend clause.");
21787 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr)) {
21788 // It will be analyzed later.
21789 Vars.push_back(Elt: RefExpr);
21790 continue;
21791 }
21792
21793 SourceLocation ELoc = RefExpr->getExprLoc();
21794 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
21795 if (DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) {
21796 bool OMPDependTFound = getLangOpts().OpenMP >= 50;
21797 if (OMPDependTFound)
21798 OMPDependTFound = findOMPDependT(S&: SemaRef, Loc: StartLoc, DSAStack,
21799 Diagnose: DepKind == OMPC_DEPEND_depobj);
21800 if (DepKind == OMPC_DEPEND_depobj) {
21801 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
21802 // List items used in depend clauses with the depobj dependence type
21803 // must be expressions of the omp_depend_t type.
21804 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
21805 !RefExpr->isInstantiationDependent() &&
21806 !RefExpr->containsUnexpandedParameterPack() &&
21807 (OMPDependTFound &&
21808 !getASTContext().hasSameUnqualifiedType(
21809 DSAStack->getOMPDependT(), T2: RefExpr->getType()))) {
21810 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21811 << 0 << RefExpr->getType() << RefExpr->getSourceRange();
21812 continue;
21813 }
21814 if (!RefExpr->isLValue()) {
21815 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_omp_depend_t_lvalue)
21816 << 1 << RefExpr->getType() << RefExpr->getSourceRange();
21817 continue;
21818 }
21819 } else {
21820 // OpenMP 5.0 [2.17.11, Restrictions]
21821 // List items used in depend clauses cannot be zero-length array
21822 // sections.
21823 QualType ExprTy = RefExpr->getType().getNonReferenceType();
21824 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: SimpleExpr);
21825 if (OASE) {
21826 QualType BaseType =
21827 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
21828 if (BaseType.isNull())
21829 return nullptr;
21830 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
21831 ExprTy = ATy->getElementType();
21832 else
21833 ExprTy = BaseType->getPointeeType();
21834 if (BaseType.isNull() || ExprTy.isNull())
21835 return nullptr;
21836 ExprTy = ExprTy.getNonReferenceType();
21837 const Expr *Length = OASE->getLength();
21838 Expr::EvalResult Result;
21839 if (Length && !Length->isValueDependent() &&
21840 Length->EvaluateAsInt(Result, Ctx: getASTContext()) &&
21841 Result.Val.getInt().isZero()) {
21842 Diag(Loc: ELoc,
21843 DiagID: diag::err_omp_depend_zero_length_array_section_not_allowed)
21844 << SimpleExpr->getSourceRange();
21845 continue;
21846 }
21847 }
21848
21849 // OpenMP 5.0, 2.17.11 depend Clause, Restrictions, C/C++
21850 // List items used in depend clauses with the in, out, inout,
21851 // inoutset, or mutexinoutset dependence types cannot be
21852 // expressions of the omp_depend_t type.
21853 if (!RefExpr->isValueDependent() && !RefExpr->isTypeDependent() &&
21854 !RefExpr->isInstantiationDependent() &&
21855 !RefExpr->containsUnexpandedParameterPack() &&
21856 (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
21857 (OMPDependTFound && DSAStack->getOMPDependT().getTypePtr() ==
21858 ExprTy.getTypePtr()))) {
21859 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21860 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21861 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21862 << RefExpr->getSourceRange();
21863 continue;
21864 }
21865
21866 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: SimpleExpr);
21867 if (ASE && !ASE->getBase()->isTypeDependent() &&
21868 !ASE->getBase()
21869 ->getType()
21870 .getNonReferenceType()
21871 ->isPointerType() &&
21872 !ASE->getBase()->getType().getNonReferenceType()->isArrayType()) {
21873 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21874 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21875 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21876 << RefExpr->getSourceRange();
21877 continue;
21878 }
21879
21880 ExprResult Res;
21881 {
21882 Sema::TentativeAnalysisScope Trap(SemaRef);
21883 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf,
21884 InputExpr: RefExpr->IgnoreParenImpCasts());
21885 }
21886 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
21887 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
21888 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
21889 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21890 << (getLangOpts().OpenMP >= 50 ? 1 : 0)
21891 << RefExpr->getSourceRange();
21892 continue;
21893 }
21894 }
21895 }
21896 Vars.push_back(Elt: RefExpr->IgnoreParenImpCasts());
21897 }
21898 }
21899
21900 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
21901 DepKind != OMPC_DEPEND_outallmemory &&
21902 DepKind != OMPC_DEPEND_inoutallmemory && Vars.empty())
21903 return nullptr;
21904
21905 auto *C = OMPDependClause::Create(
21906 C: getASTContext(), StartLoc, LParenLoc, EndLoc,
21907 Data: {.DepKind: DepKind, .DepLoc: DepLoc, .ColonLoc: Data.ColonLoc, .OmpAllMemoryLoc: Data.OmpAllMemoryLoc}, DepModifier, VL: Vars,
21908 NumLoops: TotalDepCount.getZExtValue());
21909 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
21910 DSAStack->isParentOrderedRegion())
21911 DSAStack->addDoacrossDependClause(C, OpsOffs);
21912 return C;
21913}
21914
21915OMPClause *SemaOpenMP::ActOnOpenMPDeviceClause(
21916 OpenMPDeviceClauseModifier Modifier, Expr *Device, SourceLocation StartLoc,
21917 SourceLocation LParenLoc, SourceLocation ModifierLoc,
21918 SourceLocation EndLoc) {
21919 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 50) &&
21920 "Unexpected device modifier in OpenMP < 50.");
21921
21922 bool ErrorFound = false;
21923 if (ModifierLoc.isValid() && Modifier == OMPC_DEVICE_unknown) {
21924 std::string Values =
21925 getListOfPossibleValues(K: OMPC_device, /*First=*/0, Last: OMPC_DEVICE_unknown);
21926 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
21927 << Values << getOpenMPClauseNameForDiag(C: OMPC_device);
21928 ErrorFound = true;
21929 }
21930
21931 Expr *ValExpr = Device;
21932 Stmt *HelperValStmt = nullptr;
21933
21934 // OpenMP [2.9.1, Restrictions]
21935 // The device expression must evaluate to a non-negative integer value.
21936 ErrorFound = !isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_device,
21937 /*StrictlyPositive=*/false) ||
21938 ErrorFound;
21939 if (ErrorFound)
21940 return nullptr;
21941
21942 // OpenMP 5.0 [2.12.5, Restrictions]
21943 // In case of ancestor device-modifier, a requires directive with
21944 // the reverse_offload clause must be specified.
21945 if (Modifier == OMPC_DEVICE_ancestor) {
21946 if (!DSAStack->hasRequiresDeclWithClause<OMPReverseOffloadClause>()) {
21947 SemaRef.targetDiag(
21948 Loc: StartLoc,
21949 DiagID: diag::err_omp_device_ancestor_without_requires_reverse_offload);
21950 ErrorFound = true;
21951 }
21952 }
21953
21954 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
21955 OpenMPDirectiveKind CaptureRegion =
21956 getOpenMPCaptureRegionForClause(DKind, CKind: OMPC_device, OpenMPVersion: getLangOpts().OpenMP);
21957 if (CaptureRegion != OMPD_unknown &&
21958 !SemaRef.CurContext->isDependentContext()) {
21959 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
21960 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
21961 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
21962 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
21963 }
21964
21965 return new (getASTContext())
21966 OMPDeviceClause(Modifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
21967 LParenLoc, ModifierLoc, EndLoc);
21968}
21969
21970static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
21971 DSAStackTy *Stack, QualType QTy,
21972 bool FullCheck = true) {
21973 if (SemaRef.RequireCompleteType(Loc: SL, T: QTy, DiagID: diag::err_incomplete_type))
21974 return false;
21975 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
21976 !QTy.isTriviallyCopyableType(Context: SemaRef.Context))
21977 SemaRef.Diag(Loc: SL, DiagID: diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
21978 return true;
21979}
21980
21981/// Return true if it can be proven that the provided array expression
21982/// (array section or array subscript) does NOT specify the whole size of the
21983/// array whose base type is \a BaseQTy.
21984static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
21985 const Expr *E,
21986 QualType BaseQTy) {
21987 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
21988
21989 // If this is an array subscript, it refers to the whole size if the size of
21990 // the dimension is constant and equals 1. Also, an array section assumes the
21991 // format of an array subscript if no colon is used.
21992 if (isa<ArraySubscriptExpr>(Val: E) ||
21993 (OASE && OASE->getColonLocFirst().isInvalid())) {
21994 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
21995 return ATy->getSExtSize() != 1;
21996 // Size can't be evaluated statically.
21997 return false;
21998 }
21999
22000 assert(OASE && "Expecting array section if not an array subscript.");
22001 const Expr *LowerBound = OASE->getLowerBound();
22002 const Expr *Length = OASE->getLength();
22003
22004 // If there is a lower bound that does not evaluates to zero, we are not
22005 // covering the whole dimension.
22006 if (LowerBound) {
22007 Expr::EvalResult Result;
22008 if (!LowerBound->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22009 return false; // Can't get the integer value as a constant.
22010
22011 llvm::APSInt ConstLowerBound = Result.Val.getInt();
22012 if (ConstLowerBound.getSExtValue())
22013 return true;
22014 }
22015
22016 // If we don't have a length we covering the whole dimension.
22017 if (!Length)
22018 return false;
22019
22020 // If the base is a pointer, we don't have a way to get the size of the
22021 // pointee.
22022 if (BaseQTy->isPointerType())
22023 return false;
22024
22025 // We can only check if the length is the same as the size of the dimension
22026 // if we have a constant array.
22027 const auto *CATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr());
22028 if (!CATy)
22029 return false;
22030
22031 Expr::EvalResult Result;
22032 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22033 return false; // Can't get the integer value as a constant.
22034
22035 llvm::APSInt ConstLength = Result.Val.getInt();
22036 return CATy->getSExtSize() != ConstLength.getSExtValue();
22037}
22038
22039// Return true if it can be proven that the provided array expression (array
22040// section or array subscript) does NOT specify a single element of the array
22041// whose base type is \a BaseQTy.
22042static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
22043 const Expr *E,
22044 QualType BaseQTy) {
22045 const auto *OASE = dyn_cast<ArraySectionExpr>(Val: E);
22046
22047 // An array subscript always refer to a single element. Also, an array section
22048 // assumes the format of an array subscript if no colon is used.
22049 if (isa<ArraySubscriptExpr>(Val: E) ||
22050 (OASE && OASE->getColonLocFirst().isInvalid()))
22051 return false;
22052
22053 assert(OASE && "Expecting array section if not an array subscript.");
22054 const Expr *Length = OASE->getLength();
22055
22056 // If we don't have a length we have to check if the array has unitary size
22057 // for this dimension. Also, we should always expect a length if the base type
22058 // is pointer.
22059 if (!Length) {
22060 if (const auto *ATy = dyn_cast<ConstantArrayType>(Val: BaseQTy.getTypePtr()))
22061 return ATy->getSExtSize() != 1;
22062 // We cannot assume anything.
22063 return false;
22064 }
22065
22066 // Check if the length evaluates to 1.
22067 Expr::EvalResult Result;
22068 if (!Length->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()))
22069 return false; // Can't get the integer value as a constant.
22070
22071 llvm::APSInt ConstLength = Result.Val.getInt();
22072 return ConstLength.getSExtValue() != 1;
22073}
22074
22075// The base of elements of list in a map clause have to be either:
22076// - a reference to variable or field.
22077// - a member expression.
22078// - an array expression.
22079//
22080// E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
22081// reference to 'r'.
22082//
22083// If we have:
22084//
22085// struct SS {
22086// Bla S;
22087// foo() {
22088// #pragma omp target map (S.Arr[:12]);
22089// }
22090// }
22091//
22092// We want to retrieve the member expression 'this->S';
22093
22094// OpenMP 5.0 [2.19.7.1, map Clause, Restrictions, p.2]
22095// If a list item is an array section, it must specify contiguous storage.
22096//
22097// For this restriction it is sufficient that we make sure only references
22098// to variables or fields and array expressions, and that no array sections
22099// exist except in the rightmost expression (unless they cover the whole
22100// dimension of the array). E.g. these would be invalid:
22101//
22102// r.ArrS[3:5].Arr[6:7]
22103//
22104// r.ArrS[3:5].x
22105//
22106// but these would be valid:
22107// r.ArrS[3].Arr[6:7]
22108//
22109// r.ArrS[3].x
22110namespace {
22111class MapBaseChecker final : public StmtVisitor<MapBaseChecker, bool> {
22112 Sema &SemaRef;
22113 OpenMPClauseKind CKind = OMPC_unknown;
22114 OpenMPDirectiveKind DKind = OMPD_unknown;
22115 OMPClauseMappableExprCommon::MappableExprComponentList &Components;
22116 bool IsNonContiguous = false;
22117 bool NoDiagnose = false;
22118 const Expr *RelevantExpr = nullptr;
22119 bool AllowUnitySizeArraySection = true;
22120 bool AllowWholeSizeArraySection = true;
22121 bool AllowAnotherPtr = true;
22122 SourceLocation ELoc;
22123 SourceRange ERange;
22124
22125 void emitErrorMsg() {
22126 // If nothing else worked, this is not a valid map clause expression.
22127 if (SemaRef.getLangOpts().OpenMP < 50) {
22128 SemaRef.Diag(Loc: ELoc,
22129 DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
22130 << ERange;
22131 } else {
22132 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
22133 << getOpenMPClauseNameForDiag(C: CKind) << ERange;
22134 }
22135 }
22136
22137public:
22138 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
22139 if (!isa<VarDecl>(Val: DRE->getDecl())) {
22140 emitErrorMsg();
22141 return false;
22142 }
22143 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22144 RelevantExpr = DRE;
22145 // Record the component.
22146 Components.emplace_back(Args&: DRE, Args: DRE->getDecl(), Args&: IsNonContiguous);
22147 return true;
22148 }
22149
22150 bool VisitMemberExpr(MemberExpr *ME) {
22151 Expr *E = ME;
22152 Expr *BaseE = ME->getBase()->IgnoreParenCasts();
22153
22154 if (isa<CXXThisExpr>(Val: BaseE)) {
22155 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22156 // We found a base expression: this->Val.
22157 RelevantExpr = ME;
22158 } else {
22159 E = BaseE;
22160 }
22161
22162 if (!isa<FieldDecl>(Val: ME->getMemberDecl())) {
22163 if (!NoDiagnose) {
22164 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_access_to_data_field)
22165 << ME->getSourceRange();
22166 return false;
22167 }
22168 if (RelevantExpr)
22169 return false;
22170 return Visit(S: E);
22171 }
22172
22173 auto *FD = cast<FieldDecl>(Val: ME->getMemberDecl());
22174
22175 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
22176 // A bit-field cannot appear in a map clause.
22177 //
22178 if (FD->isBitField()) {
22179 if (!NoDiagnose) {
22180 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_bit_fields_forbidden_in_clause)
22181 << ME->getSourceRange() << getOpenMPClauseNameForDiag(C: CKind);
22182 return false;
22183 }
22184 if (RelevantExpr)
22185 return false;
22186 return Visit(S: E);
22187 }
22188
22189 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22190 // If the type of a list item is a reference to a type T then the type
22191 // will be considered to be T for all purposes of this clause.
22192 QualType CurType = BaseE->getType().getNonReferenceType();
22193
22194 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
22195 // A list item cannot be a variable that is a member of a structure with
22196 // a union type.
22197 //
22198 if (CurType->isUnionType()) {
22199 if (!NoDiagnose) {
22200 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_union_type_not_allowed)
22201 << ME->getSourceRange();
22202 return false;
22203 }
22204 return RelevantExpr || Visit(S: E);
22205 }
22206
22207 // If we got a member expression, we should not expect any array section
22208 // before that:
22209 //
22210 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
22211 // If a list item is an element of a structure, only the rightmost symbol
22212 // of the variable reference can be an array section.
22213 //
22214 AllowUnitySizeArraySection = false;
22215 AllowWholeSizeArraySection = false;
22216
22217 // Record the component.
22218 Components.emplace_back(Args&: ME, Args&: FD, Args&: IsNonContiguous);
22219 return RelevantExpr || Visit(S: E);
22220 }
22221
22222 bool VisitArraySubscriptExpr(ArraySubscriptExpr *AE) {
22223 Expr *E = AE->getBase()->IgnoreParenImpCasts();
22224
22225 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
22226 if (!NoDiagnose) {
22227 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
22228 << 0 << AE->getSourceRange();
22229 return false;
22230 }
22231 return RelevantExpr || Visit(S: E);
22232 }
22233
22234 // If we got an array subscript that express the whole dimension we
22235 // can have any array expressions before. If it only expressing part of
22236 // the dimension, we can only have unitary-size array expressions.
22237 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: AE, BaseQTy: E->getType()))
22238 AllowWholeSizeArraySection = false;
22239
22240 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E->IgnoreParenCasts())) {
22241 Expr::EvalResult Result;
22242 if (!AE->getIdx()->isValueDependent() &&
22243 AE->getIdx()->EvaluateAsInt(Result, Ctx: SemaRef.getASTContext()) &&
22244 !Result.Val.getInt().isZero()) {
22245 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
22246 DiagID: diag::err_omp_invalid_map_this_expr);
22247 SemaRef.Diag(Loc: AE->getIdx()->getExprLoc(),
22248 DiagID: diag::note_omp_invalid_subscript_on_this_ptr_map);
22249 }
22250 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22251 RelevantExpr = TE;
22252 }
22253
22254 // Record the component - we don't have any declaration associated.
22255 Components.emplace_back(Args&: AE, Args: nullptr, Args&: IsNonContiguous);
22256
22257 return RelevantExpr || Visit(S: E);
22258 }
22259
22260 bool VisitArraySectionExpr(ArraySectionExpr *OASE) {
22261 // After OMP 5.0 Array section in reduction clause will be implicitly
22262 // mapped
22263 assert(!(SemaRef.getLangOpts().OpenMP < 50 && NoDiagnose) &&
22264 "Array sections cannot be implicitly mapped.");
22265 Expr *E = OASE->getBase()->IgnoreParenImpCasts();
22266 QualType CurType =
22267 ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
22268
22269 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22270 // If the type of a list item is a reference to a type T then the type
22271 // will be considered to be T for all purposes of this clause.
22272 if (CurType->isReferenceType())
22273 CurType = CurType->getPointeeType();
22274
22275 bool IsPointer = CurType->isAnyPointerType();
22276
22277 if (!IsPointer && !CurType->isArrayType()) {
22278 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_expected_base_var_name)
22279 << 0 << OASE->getSourceRange();
22280 return false;
22281 }
22282
22283 bool NotWhole =
22284 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, E: OASE, BaseQTy: CurType);
22285 bool NotUnity =
22286 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, E: OASE, BaseQTy: CurType);
22287
22288 if (AllowWholeSizeArraySection) {
22289 // Any array section is currently allowed. Allowing a whole size array
22290 // section implies allowing a unity array section as well.
22291 //
22292 // If this array section refers to the whole dimension we can still
22293 // accept other array sections before this one, except if the base is a
22294 // pointer. Otherwise, only unitary sections are accepted.
22295 if (NotWhole || IsPointer)
22296 AllowWholeSizeArraySection = false;
22297 } else if (DKind == OMPD_target_update &&
22298 SemaRef.getLangOpts().OpenMP >= 50) {
22299 if (IsPointer && !AllowAnotherPtr)
22300 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_section_length_undefined)
22301 << /*array of unknown bound */ 1;
22302 else
22303 IsNonContiguous = true;
22304 } else if (AllowUnitySizeArraySection && NotUnity) {
22305 // A unity or whole array section is not allowed and that is not
22306 // compatible with the properties of the current array section.
22307 if (NoDiagnose)
22308 return false;
22309 SemaRef.Diag(Loc: ELoc,
22310 DiagID: diag::err_array_section_does_not_specify_contiguous_storage)
22311 << OASE->getSourceRange();
22312 return false;
22313 }
22314
22315 if (IsPointer)
22316 AllowAnotherPtr = false;
22317
22318 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: E)) {
22319 Expr::EvalResult ResultR;
22320 Expr::EvalResult ResultL;
22321 if (!OASE->getLength()->isValueDependent() &&
22322 OASE->getLength()->EvaluateAsInt(Result&: ResultR, Ctx: SemaRef.getASTContext()) &&
22323 !ResultR.Val.getInt().isOne()) {
22324 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
22325 DiagID: diag::err_omp_invalid_map_this_expr);
22326 SemaRef.Diag(Loc: OASE->getLength()->getExprLoc(),
22327 DiagID: diag::note_omp_invalid_length_on_this_ptr_mapping);
22328 }
22329 if (OASE->getLowerBound() && !OASE->getLowerBound()->isValueDependent() &&
22330 OASE->getLowerBound()->EvaluateAsInt(Result&: ResultL,
22331 Ctx: SemaRef.getASTContext()) &&
22332 !ResultL.Val.getInt().isZero()) {
22333 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
22334 DiagID: diag::err_omp_invalid_map_this_expr);
22335 SemaRef.Diag(Loc: OASE->getLowerBound()->getExprLoc(),
22336 DiagID: diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
22337 }
22338 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22339 RelevantExpr = TE;
22340 }
22341
22342 // Record the component - we don't have any declaration associated.
22343 Components.emplace_back(Args&: OASE, Args: nullptr, /*IsNonContiguous=*/Args: false);
22344 return RelevantExpr || Visit(S: E);
22345 }
22346 bool VisitOMPArrayShapingExpr(OMPArrayShapingExpr *E) {
22347 Expr *Base = E->getBase();
22348
22349 // Record the component - we don't have any declaration associated.
22350 Components.emplace_back(Args&: E, Args: nullptr, Args&: IsNonContiguous);
22351
22352 return Visit(S: Base->IgnoreParenImpCasts());
22353 }
22354
22355 bool VisitUnaryOperator(UnaryOperator *UO) {
22356 if (SemaRef.getLangOpts().OpenMP < 50 || !UO->isLValue() ||
22357 UO->getOpcode() != UO_Deref) {
22358 emitErrorMsg();
22359 return false;
22360 }
22361 if (!RelevantExpr) {
22362 // Record the component if haven't found base decl.
22363 Components.emplace_back(Args&: UO, Args: nullptr, /*IsNonContiguous=*/Args: false);
22364 }
22365 return RelevantExpr || Visit(S: UO->getSubExpr()->IgnoreParenImpCasts());
22366 }
22367 bool VisitBinaryOperator(BinaryOperator *BO) {
22368 if (SemaRef.getLangOpts().OpenMP < 50 || !BO->getType()->isPointerType()) {
22369 emitErrorMsg();
22370 return false;
22371 }
22372
22373 // Pointer arithmetic is the only thing we expect to happen here so after we
22374 // make sure the binary operator is a pointer type, the only thing we need
22375 // to do is to visit the subtree that has the same type as root (so that we
22376 // know the other subtree is just an offset)
22377 Expr *LE = BO->getLHS()->IgnoreParenImpCasts();
22378 Expr *RE = BO->getRHS()->IgnoreParenImpCasts();
22379 Components.emplace_back(Args&: BO, Args: nullptr, Args: false);
22380 assert((LE->getType().getTypePtr() == BO->getType().getTypePtr() ||
22381 RE->getType().getTypePtr() == BO->getType().getTypePtr()) &&
22382 "Either LHS or RHS have base decl inside");
22383 if (BO->getType().getTypePtr() == LE->getType().getTypePtr())
22384 return RelevantExpr || Visit(S: LE);
22385 return RelevantExpr || Visit(S: RE);
22386 }
22387 bool VisitCXXThisExpr(CXXThisExpr *CTE) {
22388 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22389 RelevantExpr = CTE;
22390 Components.emplace_back(Args&: CTE, Args: nullptr, Args&: IsNonContiguous);
22391 return true;
22392 }
22393 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *COCE) {
22394 assert(!RelevantExpr && "RelevantExpr is expected to be nullptr");
22395 Components.emplace_back(Args&: COCE, Args: nullptr, Args&: IsNonContiguous);
22396 return true;
22397 }
22398 bool VisitOpaqueValueExpr(OpaqueValueExpr *E) {
22399 Expr *Source = E->getSourceExpr();
22400 if (!Source) {
22401 emitErrorMsg();
22402 return false;
22403 }
22404 return Visit(S: Source);
22405 }
22406 bool VisitStmt(Stmt *) {
22407 emitErrorMsg();
22408 return false;
22409 }
22410 const Expr *getFoundBase() const { return RelevantExpr; }
22411 explicit MapBaseChecker(
22412 Sema &SemaRef, OpenMPClauseKind CKind, OpenMPDirectiveKind DKind,
22413 OMPClauseMappableExprCommon::MappableExprComponentList &Components,
22414 bool NoDiagnose, SourceLocation &ELoc, SourceRange &ERange)
22415 : SemaRef(SemaRef), CKind(CKind), DKind(DKind), Components(Components),
22416 NoDiagnose(NoDiagnose), ELoc(ELoc), ERange(ERange) {}
22417};
22418} // namespace
22419
22420/// Return the expression of the base of the mappable expression or null if it
22421/// cannot be determined and do all the necessary checks to see if the
22422/// expression is valid as a standalone mappable expression. In the process,
22423/// record all the components of the expression.
22424static const Expr *checkMapClauseExpressionBase(
22425 Sema &SemaRef, Expr *E,
22426 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
22427 OpenMPClauseKind CKind, OpenMPDirectiveKind DKind, bool NoDiagnose) {
22428 SourceLocation ELoc = E->getExprLoc();
22429 SourceRange ERange = E->getSourceRange();
22430 MapBaseChecker Checker(SemaRef, CKind, DKind, CurComponents, NoDiagnose, ELoc,
22431 ERange);
22432 if (Checker.Visit(S: E->IgnoreParens())) {
22433 // Check if the highest dimension array section has length specified
22434 if (SemaRef.getLangOpts().OpenMP >= 50 && !CurComponents.empty() &&
22435 (CKind == OMPC_to || CKind == OMPC_from)) {
22436 auto CI = CurComponents.rbegin();
22437 auto CE = CurComponents.rend();
22438 for (; CI != CE; ++CI) {
22439 const auto *OASE =
22440 dyn_cast<ArraySectionExpr>(Val: CI->getAssociatedExpression());
22441 if (!OASE)
22442 continue;
22443 if (OASE && OASE->getLength())
22444 break;
22445 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_array_section_does_not_specify_length)
22446 << ERange;
22447 }
22448 }
22449 return Checker.getFoundBase();
22450 }
22451 return nullptr;
22452}
22453
22454// Return true if expression E associated with value VD has conflicts with other
22455// map information.
22456static bool checkMapConflicts(
22457 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
22458 bool CurrentRegionOnly,
22459 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
22460 OpenMPClauseKind CKind) {
22461 assert(VD && E);
22462 SourceLocation ELoc = E->getExprLoc();
22463 SourceRange ERange = E->getSourceRange();
22464
22465 // In order to easily check the conflicts we need to match each component of
22466 // the expression under test with the components of the expressions that are
22467 // already in the stack.
22468
22469 assert(!CurComponents.empty() && "Map clause expression with no components!");
22470 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
22471 "Map clause expression with unexpected base!");
22472
22473 // Variables to help detecting enclosing problems in data environment nests.
22474 bool IsEnclosedByDataEnvironmentExpr = false;
22475 const Expr *EnclosingExpr = nullptr;
22476
22477 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
22478 VD, CurrentRegionOnly,
22479 Check: [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
22480 ERange, CKind, &EnclosingExpr,
22481 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
22482 StackComponents,
22483 OpenMPClauseKind Kind) {
22484 if (CKind == Kind && SemaRef.LangOpts.OpenMP >= 50)
22485 return false;
22486 assert(!StackComponents.empty() &&
22487 "Map clause expression with no components!");
22488 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
22489 "Map clause expression with unexpected base!");
22490 (void)VD;
22491
22492 // The whole expression in the stack.
22493 const Expr *RE = StackComponents.front().getAssociatedExpression();
22494
22495 // Expressions must start from the same base. Here we detect at which
22496 // point both expressions diverge from each other and see if we can
22497 // detect if the memory referred to both expressions is contiguous and
22498 // do not overlap.
22499 auto CI = CurComponents.rbegin();
22500 auto CE = CurComponents.rend();
22501 auto SI = StackComponents.rbegin();
22502 auto SE = StackComponents.rend();
22503 for (; CI != CE && SI != SE; ++CI, ++SI) {
22504
22505 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
22506 // At most one list item can be an array item derived from a given
22507 // variable in map clauses of the same construct.
22508 if (CurrentRegionOnly &&
22509 (isa<ArraySubscriptExpr>(Val: CI->getAssociatedExpression()) ||
22510 isa<ArraySectionExpr>(Val: CI->getAssociatedExpression()) ||
22511 isa<OMPArrayShapingExpr>(Val: CI->getAssociatedExpression())) &&
22512 (isa<ArraySubscriptExpr>(Val: SI->getAssociatedExpression()) ||
22513 isa<ArraySectionExpr>(Val: SI->getAssociatedExpression()) ||
22514 isa<OMPArrayShapingExpr>(Val: SI->getAssociatedExpression()))) {
22515 SemaRef.Diag(Loc: CI->getAssociatedExpression()->getExprLoc(),
22516 DiagID: diag::err_omp_multiple_array_items_in_map_clause)
22517 << CI->getAssociatedExpression()->getSourceRange();
22518 SemaRef.Diag(Loc: SI->getAssociatedExpression()->getExprLoc(),
22519 DiagID: diag::note_used_here)
22520 << SI->getAssociatedExpression()->getSourceRange();
22521 return true;
22522 }
22523
22524 // Do both expressions have the same kind?
22525 if (CI->getAssociatedExpression()->getStmtClass() !=
22526 SI->getAssociatedExpression()->getStmtClass())
22527 break;
22528
22529 // Are we dealing with different variables/fields?
22530 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
22531 break;
22532 }
22533 // Check if the extra components of the expressions in the enclosing
22534 // data environment are redundant for the current base declaration.
22535 // If they are, the maps completely overlap, which is legal.
22536 for (; SI != SE; ++SI) {
22537 QualType Type;
22538 if (const auto *ASE =
22539 dyn_cast<ArraySubscriptExpr>(Val: SI->getAssociatedExpression())) {
22540 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
22541 } else if (const auto *OASE = dyn_cast<ArraySectionExpr>(
22542 Val: SI->getAssociatedExpression())) {
22543 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
22544 Type = ArraySectionExpr::getBaseOriginalType(Base: E).getCanonicalType();
22545 } else if (const auto *OASE = dyn_cast<OMPArrayShapingExpr>(
22546 Val: SI->getAssociatedExpression())) {
22547 Type = OASE->getBase()->getType()->getPointeeType();
22548 }
22549 if (Type.isNull() || Type->isAnyPointerType() ||
22550 checkArrayExpressionDoesNotReferToWholeSize(
22551 SemaRef, E: SI->getAssociatedExpression(), BaseQTy: Type))
22552 break;
22553 }
22554
22555 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
22556 // List items of map clauses in the same construct must not share
22557 // original storage.
22558 //
22559 // If the expressions are exactly the same or one is a subset of the
22560 // other, it means they are sharing storage.
22561 if (CI == CE && SI == SE) {
22562 if (CurrentRegionOnly) {
22563 if (CKind == OMPC_map) {
22564 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
22565 } else {
22566 assert(CKind == OMPC_to || CKind == OMPC_from);
22567 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
22568 << ERange;
22569 }
22570 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22571 << RE->getSourceRange();
22572 return true;
22573 }
22574 // If we find the same expression in the enclosing data environment,
22575 // that is legal.
22576 IsEnclosedByDataEnvironmentExpr = true;
22577 return false;
22578 }
22579
22580 QualType DerivedType =
22581 std::prev(x: CI)->getAssociatedDeclaration()->getType();
22582 SourceLocation DerivedLoc =
22583 std::prev(x: CI)->getAssociatedExpression()->getExprLoc();
22584
22585 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
22586 // If the type of a list item is a reference to a type T then the type
22587 // will be considered to be T for all purposes of this clause.
22588 DerivedType = DerivedType.getNonReferenceType();
22589
22590 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
22591 // A variable for which the type is pointer and an array section
22592 // derived from that variable must not appear as list items of map
22593 // clauses of the same construct.
22594 //
22595 // Also, cover one of the cases in:
22596 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
22597 // If any part of the original storage of a list item has corresponding
22598 // storage in the device data environment, all of the original storage
22599 // must have corresponding storage in the device data environment.
22600 //
22601 if (DerivedType->isAnyPointerType()) {
22602 if (CI == CE || SI == SE) {
22603 SemaRef.Diag(
22604 Loc: DerivedLoc,
22605 DiagID: diag::err_omp_pointer_mapped_along_with_derived_section)
22606 << DerivedLoc;
22607 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22608 << RE->getSourceRange();
22609 return true;
22610 }
22611 if (CI->getAssociatedExpression()->getStmtClass() !=
22612 SI->getAssociatedExpression()->getStmtClass() ||
22613 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
22614 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
22615 assert(CI != CE && SI != SE);
22616 SemaRef.Diag(Loc: DerivedLoc, DiagID: diag::err_omp_same_pointer_dereferenced)
22617 << DerivedLoc;
22618 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22619 << RE->getSourceRange();
22620 return true;
22621 }
22622 }
22623
22624 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
22625 // List items of map clauses in the same construct must not share
22626 // original storage.
22627 //
22628 // An expression is a subset of the other.
22629 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
22630 if (CKind == OMPC_map) {
22631 if (CI != CE || SI != SE) {
22632 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
22633 // a pointer.
22634 auto Begin =
22635 CI != CE ? CurComponents.begin() : StackComponents.begin();
22636 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
22637 auto It = Begin;
22638 while (It != End && !It->getAssociatedDeclaration())
22639 std::advance(i&: It, n: 1);
22640 assert(It != End &&
22641 "Expected at least one component with the declaration.");
22642 if (It != Begin && It->getAssociatedDeclaration()
22643 ->getType()
22644 .getCanonicalType()
22645 ->isAnyPointerType()) {
22646 IsEnclosedByDataEnvironmentExpr = false;
22647 EnclosingExpr = nullptr;
22648 return false;
22649 }
22650 }
22651 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << ERange;
22652 } else {
22653 assert(CKind == OMPC_to || CKind == OMPC_from);
22654 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_once_referenced_in_target_update)
22655 << ERange;
22656 }
22657 SemaRef.Diag(Loc: RE->getExprLoc(), DiagID: diag::note_used_here)
22658 << RE->getSourceRange();
22659 return true;
22660 }
22661
22662 // The current expression uses the same base as other expression in the
22663 // data environment but does not contain it completely.
22664 if (!CurrentRegionOnly && SI != SE)
22665 EnclosingExpr = RE;
22666
22667 // The current expression is a subset of the expression in the data
22668 // environment.
22669 IsEnclosedByDataEnvironmentExpr |=
22670 (!CurrentRegionOnly && CI != CE && SI == SE);
22671
22672 return false;
22673 });
22674
22675 if (CurrentRegionOnly)
22676 return FoundError;
22677
22678 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
22679 // If any part of the original storage of a list item has corresponding
22680 // storage in the device data environment, all of the original storage must
22681 // have corresponding storage in the device data environment.
22682 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
22683 // If a list item is an element of a structure, and a different element of
22684 // the structure has a corresponding list item in the device data environment
22685 // prior to a task encountering the construct associated with the map clause,
22686 // then the list item must also have a corresponding list item in the device
22687 // data environment prior to the task encountering the construct.
22688 //
22689 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
22690 SemaRef.Diag(Loc: ELoc,
22691 DiagID: diag::err_omp_original_storage_is_shared_and_does_not_contain)
22692 << ERange;
22693 SemaRef.Diag(Loc: EnclosingExpr->getExprLoc(), DiagID: diag::note_used_here)
22694 << EnclosingExpr->getSourceRange();
22695 return true;
22696 }
22697
22698 return FoundError;
22699}
22700
22701// Look up the user-defined mapper given the mapper name and mapped type, and
22702// build a reference to it.
22703static ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
22704 CXXScopeSpec &MapperIdScopeSpec,
22705 const DeclarationNameInfo &MapperId,
22706 QualType Type,
22707 Expr *UnresolvedMapper) {
22708 if (MapperIdScopeSpec.isInvalid())
22709 return ExprError();
22710 // Get the actual type for the array type.
22711 if (Type->isArrayType()) {
22712 assert(Type->getAsArrayTypeUnsafe() && "Expect to get a valid array type");
22713 Type = Type->getAsArrayTypeUnsafe()->getElementType().getCanonicalType();
22714 }
22715 // Find all user-defined mappers with the given MapperId.
22716 SmallVector<UnresolvedSet<8>, 4> Lookups;
22717 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
22718 Lookup.suppressDiagnostics();
22719 if (S) {
22720 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
22721 /*ObjectType=*/QualType())) {
22722 NamedDecl *D = Lookup.getRepresentativeDecl();
22723 while (S && !S->isDeclScope(D))
22724 S = S->getParent();
22725 if (S)
22726 S = S->getParent();
22727 Lookups.emplace_back();
22728 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
22729 Lookup.clear();
22730 }
22731 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(Val: UnresolvedMapper)) {
22732 // Extract the user-defined mappers with the given MapperId.
22733 Lookups.push_back(Elt: UnresolvedSet<8>());
22734 for (NamedDecl *D : ULE->decls()) {
22735 auto *DMD = cast<OMPDeclareMapperDecl>(Val: D);
22736 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
22737 Lookups.back().addDecl(D: DMD);
22738 }
22739 }
22740 // Defer the lookup for dependent types. The results will be passed through
22741 // UnresolvedMapper on instantiation.
22742 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
22743 Type->isInstantiationDependentType() ||
22744 Type->containsUnexpandedParameterPack() ||
22745 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
22746 return !D->isInvalidDecl() &&
22747 (D->getType()->isDependentType() ||
22748 D->getType()->isInstantiationDependentType() ||
22749 D->getType()->containsUnexpandedParameterPack());
22750 })) {
22751 UnresolvedSet<8> URS;
22752 for (const UnresolvedSet<8> &Set : Lookups) {
22753 if (Set.empty())
22754 continue;
22755 URS.append(I: Set.begin(), E: Set.end());
22756 }
22757 return UnresolvedLookupExpr::Create(
22758 Context: SemaRef.Context, /*NamingClass=*/nullptr,
22759 QualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: SemaRef.Context), NameInfo: MapperId,
22760 /*ADL=*/RequiresADL: false, Begin: URS.begin(), End: URS.end(), /*KnownDependent=*/false,
22761 /*KnownInstantiationDependent=*/false);
22762 }
22763 SourceLocation Loc = MapperId.getLoc();
22764 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
22765 // The type must be of struct, union or class type in C and C++
22766 if (!Type->isStructureOrClassType() && !Type->isUnionType() &&
22767 (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default")) {
22768 SemaRef.Diag(Loc, DiagID: diag::err_omp_mapper_wrong_type);
22769 return ExprError();
22770 }
22771 // Perform argument dependent lookup.
22772 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
22773 argumentDependentLookup(SemaRef, Id: MapperId, Loc, Ty: Type, Lookups);
22774 // Return the first user-defined mapper with the desired type.
22775 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22776 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
22777 if (!D->isInvalidDecl() &&
22778 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
22779 return D;
22780 return nullptr;
22781 }))
22782 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
22783 // Find the first user-defined mapper with a type derived from the desired
22784 // type.
22785 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22786 Lookups, Gen: [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
22787 if (!D->isInvalidDecl() &&
22788 SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: D->getType()) &&
22789 !Type.isMoreQualifiedThan(other: D->getType(),
22790 Ctx: SemaRef.getASTContext()))
22791 return D;
22792 return nullptr;
22793 })) {
22794 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
22795 /*DetectVirtual=*/false);
22796 if (SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: VD->getType(), Paths)) {
22797 if (!Paths.isAmbiguous(BaseType: SemaRef.Context.getCanonicalType(
22798 T: VD->getType().getUnqualifiedType()))) {
22799 if (SemaRef.CheckBaseClassAccess(
22800 AccessLoc: Loc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
22801 /*DiagID=*/0) != Sema::AR_inaccessible) {
22802 return SemaRef.BuildDeclRefExpr(D: VD, Ty: Type, VK: VK_LValue, Loc);
22803 }
22804 }
22805 }
22806 }
22807 // Report error if a mapper is specified, but cannot be found.
22808 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
22809 SemaRef.Diag(Loc, DiagID: diag::err_omp_invalid_mapper)
22810 << Type << MapperId.getName();
22811 return ExprError();
22812 }
22813 return ExprEmpty();
22814}
22815
22816namespace {
22817// Utility struct that gathers all the related lists associated with a mappable
22818// expression.
22819struct MappableVarListInfo {
22820 // The list of expressions.
22821 ArrayRef<Expr *> VarList;
22822 // The list of processed expressions.
22823 SmallVector<Expr *, 16> ProcessedVarList;
22824 // The mappble components for each expression.
22825 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
22826 // The base declaration of the variable.
22827 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
22828 // The reference to the user-defined mapper associated with every expression.
22829 SmallVector<Expr *, 16> UDMapperList;
22830
22831 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
22832 // We have a list of components and base declarations for each entry in the
22833 // variable list.
22834 VarComponents.reserve(N: VarList.size());
22835 VarBaseDeclarations.reserve(N: VarList.size());
22836 }
22837};
22838} // namespace
22839
22840static DeclRefExpr *buildImplicitMap(Sema &S, QualType BaseType,
22841 DSAStackTy *Stack,
22842 SmallVectorImpl<OMPClause *> &Maps) {
22843
22844 const RecordDecl *RD = BaseType->getAsRecordDecl();
22845 SourceRange Range = RD->getSourceRange();
22846 DeclarationNameInfo ImplicitName;
22847 // Dummy variable _s for Mapper.
22848 VarDecl *VD = buildVarDecl(SemaRef&: S, Loc: Range.getEnd(), Type: BaseType, Name: "_s");
22849 DeclRefExpr *MapperVarRef =
22850 buildDeclRefExpr(S, D: VD, Ty: BaseType, Loc: SourceLocation());
22851
22852 // Create implicit map clause for mapper.
22853 SmallVector<Expr *, 4> SExprs;
22854 for (auto *FD : RD->fields()) {
22855 Expr *BE = S.BuildMemberExpr(
22856 Base: MapperVarRef, /*IsArrow=*/false, OpLoc: Range.getBegin(),
22857 NNS: NestedNameSpecifierLoc(), TemplateKWLoc: Range.getBegin(), Member: FD,
22858 FoundDecl: DeclAccessPair::make(D: FD, AS: FD->getAccess()),
22859 /*HadMultipleCandidates=*/false,
22860 MemberNameInfo: DeclarationNameInfo(FD->getDeclName(), FD->getSourceRange().getBegin()),
22861 Ty: FD->getType(), VK: VK_LValue, OK: OK_Ordinary);
22862 SExprs.push_back(Elt: BE);
22863 }
22864 CXXScopeSpec MapperIdScopeSpec;
22865 DeclarationNameInfo MapperId;
22866 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
22867
22868 OMPClause *MapClause = S.OpenMP().ActOnOpenMPMapClause(
22869 IteratorModifier: nullptr, MapTypeModifiers: OMPC_MAP_MODIFIER_unknown, MapTypeModifiersLoc: SourceLocation(), MapperIdScopeSpec,
22870 MapperId, MapType: DKind == OMPD_target_enter_data ? OMPC_MAP_to : OMPC_MAP_tofrom,
22871 /*IsMapTypeImplicit=*/true, MapLoc: SourceLocation(), ColonLoc: SourceLocation(), VarList: SExprs,
22872 Locs: OMPVarListLocTy());
22873 Maps.push_back(Elt: MapClause);
22874 return MapperVarRef;
22875}
22876
22877static ExprResult buildImplicitMapper(Sema &S, QualType BaseType,
22878 DSAStackTy *Stack) {
22879
22880 // Build impilicit map for mapper
22881 SmallVector<OMPClause *, 4> Maps;
22882 DeclRefExpr *MapperVarRef = buildImplicitMap(S, BaseType, Stack, Maps);
22883
22884 const RecordDecl *RD = BaseType->getAsRecordDecl();
22885 // AST context is RD's ParentASTContext().
22886 ASTContext &Ctx = RD->getParentASTContext();
22887 // DeclContext is RD's DeclContext.
22888 DeclContext *DCT = const_cast<DeclContext *>(RD->getDeclContext());
22889
22890 // Create implicit default mapper for "RD".
22891 DeclarationName MapperId;
22892 auto &DeclNames = Ctx.DeclarationNames;
22893 MapperId = DeclNames.getIdentifier(ID: &Ctx.Idents.get(Name: "default"));
22894 auto *DMD = OMPDeclareMapperDecl::Create(C&: Ctx, DC: DCT, L: SourceLocation(), Name: MapperId,
22895 T: BaseType, VarName: MapperId, Clauses: Maps, PrevDeclInScope: nullptr);
22896 Scope *Scope = S.getScopeForContext(Ctx: DCT);
22897 if (Scope)
22898 S.PushOnScopeChains(D: DMD, S: Scope, /*AddToContext=*/false);
22899 DCT->addDecl(D: DMD);
22900 DMD->setAccess(clang::AS_none);
22901 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
22902 VD->setDeclContext(DMD);
22903 VD->setLexicalDeclContext(DMD);
22904 DMD->addDecl(D: VD);
22905 DMD->setMapperVarRef(MapperVarRef);
22906 FieldDecl *FD = *RD->field_begin();
22907 // create mapper refence.
22908 return DeclRefExpr::Create(Context: Ctx, QualifierLoc: NestedNameSpecifierLoc{}, TemplateKWLoc: FD->getLocation(),
22909 D: DMD, RefersToEnclosingVariableOrCapture: false, NameLoc: SourceLocation(), T: BaseType, VK: VK_LValue);
22910}
22911
22912// Look up the user-defined mapper given the mapper name and mapper type,
22913// return true if found one.
22914static bool hasUserDefinedMapper(Sema &SemaRef, Scope *S,
22915 CXXScopeSpec &MapperIdScopeSpec,
22916 const DeclarationNameInfo &MapperId,
22917 QualType Type) {
22918 // Find all user-defined mappers with the given MapperId.
22919 SmallVector<UnresolvedSet<8>, 4> Lookups;
22920 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
22921 Lookup.suppressDiagnostics();
22922 while (S && SemaRef.LookupParsedName(R&: Lookup, S, SS: &MapperIdScopeSpec,
22923 /*ObjectType=*/QualType())) {
22924 NamedDecl *D = Lookup.getRepresentativeDecl();
22925 while (S && !S->isDeclScope(D))
22926 S = S->getParent();
22927 if (S)
22928 S = S->getParent();
22929 Lookups.emplace_back();
22930 Lookups.back().append(I: Lookup.begin(), E: Lookup.end());
22931 Lookup.clear();
22932 }
22933 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
22934 Type->isInstantiationDependentType() ||
22935 Type->containsUnexpandedParameterPack() ||
22936 filterLookupForUDReductionAndMapper<bool>(Lookups, Gen: [](ValueDecl *D) {
22937 return !D->isInvalidDecl() &&
22938 (D->getType()->isDependentType() ||
22939 D->getType()->isInstantiationDependentType() ||
22940 D->getType()->containsUnexpandedParameterPack());
22941 }))
22942 return false;
22943 // Perform argument dependent lookup.
22944 SourceLocation Loc = MapperId.getLoc();
22945 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
22946 argumentDependentLookup(SemaRef, Id: MapperId, Loc, Ty: Type, Lookups);
22947 if (filterLookupForUDReductionAndMapper<ValueDecl *>(
22948 Lookups, Gen: [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
22949 if (!D->isInvalidDecl() &&
22950 SemaRef.Context.hasSameType(T1: D->getType(), T2: Type))
22951 return D;
22952 return nullptr;
22953 }))
22954 return true;
22955 // Find the first user-defined mapper with a type derived from the desired
22956 // type.
22957 auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
22958 Lookups, Gen: [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
22959 if (!D->isInvalidDecl() &&
22960 SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: D->getType()) &&
22961 !Type.isMoreQualifiedThan(other: D->getType(), Ctx: SemaRef.getASTContext()))
22962 return D;
22963 return nullptr;
22964 });
22965 if (!VD)
22966 return false;
22967 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
22968 /*DetectVirtual=*/false);
22969 if (SemaRef.IsDerivedFrom(Loc, Derived: Type, Base: VD->getType(), Paths)) {
22970 bool IsAmbiguous = !Paths.isAmbiguous(
22971 BaseType: SemaRef.Context.getCanonicalType(T: VD->getType().getUnqualifiedType()));
22972 if (IsAmbiguous)
22973 return false;
22974 if (SemaRef.CheckBaseClassAccess(AccessLoc: Loc, Base: VD->getType(), Derived: Type, Path: Paths.front(),
22975 /*DiagID=*/0) != Sema::AR_inaccessible)
22976 return true;
22977 }
22978 return false;
22979}
22980
22981static bool isImplicitMapperNeeded(Sema &S, DSAStackTy *Stack,
22982 QualType CanonType, const Expr *E) {
22983
22984 // DFS over data members in structures/classes.
22985 SmallVector<std::pair<QualType, FieldDecl *>, 4> Types(1,
22986 {CanonType, nullptr});
22987 llvm::DenseMap<const Type *, bool> Visited;
22988 SmallVector<std::pair<FieldDecl *, unsigned>, 4> ParentChain(1, {nullptr, 1});
22989 while (!Types.empty()) {
22990 auto [BaseType, CurFD] = Types.pop_back_val();
22991 while (ParentChain.back().second == 0)
22992 ParentChain.pop_back();
22993 --ParentChain.back().second;
22994 if (BaseType.isNull())
22995 continue;
22996 // Only structs/classes are allowed to have mappers.
22997 const RecordDecl *RD = BaseType.getCanonicalType()->getAsRecordDecl();
22998 if (!RD)
22999 continue;
23000 auto It = Visited.find(Val: BaseType.getTypePtr());
23001 if (It == Visited.end()) {
23002 // Try to find the associated user-defined mapper.
23003 CXXScopeSpec MapperIdScopeSpec;
23004 DeclarationNameInfo DefaultMapperId;
23005 DefaultMapperId.setName(S.Context.DeclarationNames.getIdentifier(
23006 ID: &S.Context.Idents.get(Name: "default")));
23007 DefaultMapperId.setLoc(E->getExprLoc());
23008 bool HasUDMapper =
23009 hasUserDefinedMapper(SemaRef&: S, S: Stack->getCurScope(), MapperIdScopeSpec,
23010 MapperId: DefaultMapperId, Type: BaseType);
23011 It = Visited.try_emplace(Key: BaseType.getTypePtr(), Args&: HasUDMapper).first;
23012 }
23013 // Found default mapper.
23014 if (It->second)
23015 return true;
23016 // Check for the "default" mapper for data members.
23017 bool FirstIter = true;
23018 for (FieldDecl *FD : RD->fields()) {
23019 if (!FD)
23020 continue;
23021 QualType FieldTy = FD->getType();
23022 if (FieldTy.isNull() ||
23023 !(FieldTy->isStructureOrClassType() || FieldTy->isUnionType()))
23024 continue;
23025 if (FirstIter) {
23026 FirstIter = false;
23027 ParentChain.emplace_back(Args&: CurFD, Args: 1);
23028 } else {
23029 ++ParentChain.back().second;
23030 }
23031 Types.emplace_back(Args&: FieldTy, Args&: FD);
23032 }
23033 }
23034 return false;
23035}
23036
23037// Check the validity of the provided variable list for the provided clause kind
23038// \a CKind. In the check process the valid expressions, mappable expression
23039// components, variables, and user-defined mappers are extracted and used to
23040// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
23041// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
23042// and \a MapperId are expected to be valid if the clause kind is 'map'.
23043static void checkMappableExpressionList(
23044 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
23045 MappableVarListInfo &MVLI, SourceLocation StartLoc,
23046 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
23047 ArrayRef<Expr *> UnresolvedMappers,
23048 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
23049 ArrayRef<OpenMPMapModifierKind> Modifiers = {},
23050 bool IsMapTypeImplicit = false, bool NoDiagnose = false) {
23051 // We only expect mappable expressions in 'to', 'from', 'map', and
23052 // 'use_device_addr' clauses.
23053 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from ||
23054 CKind == OMPC_use_device_addr) &&
23055 "Unexpected clause kind with mappable expressions!");
23056 unsigned OMPVersion = SemaRef.getLangOpts().OpenMP;
23057
23058 // If the identifier of user-defined mapper is not specified, it is "default".
23059 // We do not change the actual name in this clause to distinguish whether a
23060 // mapper is specified explicitly, i.e., it is not explicitly specified when
23061 // MapperId.getName() is empty.
23062 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
23063 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
23064 MapperId.setName(DeclNames.getIdentifier(
23065 ID: &SemaRef.getASTContext().Idents.get(Name: "default")));
23066 MapperId.setLoc(StartLoc);
23067 }
23068
23069 // Iterators to find the current unresolved mapper expression.
23070 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
23071 bool UpdateUMIt = false;
23072 Expr *UnresolvedMapper = nullptr;
23073
23074 bool HasHoldModifier =
23075 llvm::is_contained(Range&: Modifiers, Element: OMPC_MAP_MODIFIER_ompx_hold);
23076
23077 // Keep track of the mappable components and base declarations in this clause.
23078 // Each entry in the list is going to have a list of components associated. We
23079 // record each set of the components so that we can build the clause later on.
23080 // In the end we should have the same amount of declarations and component
23081 // lists.
23082
23083 for (Expr *RE : MVLI.VarList) {
23084 assert(RE && "Null expr in omp to/from/map clause");
23085 SourceLocation ELoc = RE->getExprLoc();
23086
23087 // Find the current unresolved mapper expression.
23088 if (UpdateUMIt && UMIt != UMEnd) {
23089 UMIt++;
23090 assert(
23091 UMIt != UMEnd &&
23092 "Expect the size of UnresolvedMappers to match with that of VarList");
23093 }
23094 UpdateUMIt = true;
23095 if (UMIt != UMEnd)
23096 UnresolvedMapper = *UMIt;
23097
23098 const Expr *VE = RE->IgnoreParenLValueCasts();
23099
23100 if (VE->isValueDependent() || VE->isTypeDependent() ||
23101 VE->isInstantiationDependent() ||
23102 VE->containsUnexpandedParameterPack()) {
23103 // Try to find the associated user-defined mapper.
23104 ExprResult ER = buildUserDefinedMapperRef(
23105 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23106 Type: VE->getType().getCanonicalType(), UnresolvedMapper);
23107 if (ER.isInvalid())
23108 continue;
23109 MVLI.UDMapperList.push_back(Elt: ER.get());
23110 // We can only analyze this information once the missing information is
23111 // resolved.
23112 MVLI.ProcessedVarList.push_back(Elt: RE);
23113 continue;
23114 }
23115
23116 Expr *SimpleExpr = RE->IgnoreParenCasts();
23117
23118 if (!RE->isLValue()) {
23119 if (SemaRef.getLangOpts().OpenMP < 50) {
23120 SemaRef.Diag(
23121 Loc: ELoc, DiagID: diag::err_omp_expected_named_var_member_or_array_expression)
23122 << RE->getSourceRange();
23123 } else {
23124 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_non_lvalue_in_map_or_motion_clauses)
23125 << getOpenMPClauseNameForDiag(C: CKind) << RE->getSourceRange();
23126 }
23127 continue;
23128 }
23129
23130 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
23131 ValueDecl *CurDeclaration = nullptr;
23132
23133 // Obtain the array or member expression bases if required. Also, fill the
23134 // components array with all the components identified in the process.
23135 const Expr *BE =
23136 checkMapClauseExpressionBase(SemaRef, E: SimpleExpr, CurComponents, CKind,
23137 DKind: DSAS->getCurrentDirective(), NoDiagnose);
23138 if (!BE)
23139 continue;
23140
23141 assert(!CurComponents.empty() &&
23142 "Invalid mappable expression information.");
23143
23144 if (const auto *TE = dyn_cast<CXXThisExpr>(Val: BE)) {
23145 // Add store "this" pointer to class in DSAStackTy for future checking
23146 DSAS->addMappedClassesQualTypes(QT: TE->getType());
23147 // Try to find the associated user-defined mapper.
23148 ExprResult ER = buildUserDefinedMapperRef(
23149 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23150 Type: VE->getType().getCanonicalType(), UnresolvedMapper);
23151 if (ER.isInvalid())
23152 continue;
23153 MVLI.UDMapperList.push_back(Elt: ER.get());
23154 // Skip restriction checking for variable or field declarations
23155 MVLI.ProcessedVarList.push_back(Elt: RE);
23156 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
23157 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
23158 in_end: CurComponents.end());
23159 MVLI.VarBaseDeclarations.push_back(Elt: nullptr);
23160 continue;
23161 }
23162
23163 // For the following checks, we rely on the base declaration which is
23164 // expected to be associated with the last component. The declaration is
23165 // expected to be a variable or a field (if 'this' is being mapped).
23166 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
23167 assert(CurDeclaration && "Null decl on map clause.");
23168 assert(
23169 CurDeclaration->isCanonicalDecl() &&
23170 "Expecting components to have associated only canonical declarations.");
23171
23172 auto *VD = dyn_cast<VarDecl>(Val: CurDeclaration);
23173 const auto *FD = dyn_cast<FieldDecl>(Val: CurDeclaration);
23174
23175 assert((VD || FD) && "Only variables or fields are expected here!");
23176 (void)FD;
23177
23178 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
23179 // threadprivate variables cannot appear in a map clause.
23180 // OpenMP 4.5 [2.10.5, target update Construct]
23181 // threadprivate variables cannot appear in a from clause.
23182 if (VD && DSAS->isThreadPrivate(D: VD)) {
23183 if (NoDiagnose)
23184 continue;
23185 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
23186 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_threadprivate_in_clause)
23187 << getOpenMPClauseNameForDiag(C: CKind);
23188 reportOriginalDsa(SemaRef, Stack: DSAS, D: VD, DVar);
23189 continue;
23190 }
23191
23192 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
23193 // A list item cannot appear in both a map clause and a data-sharing
23194 // attribute clause on the same construct.
23195
23196 // Check conflicts with other map clause expressions. We check the conflicts
23197 // with the current construct separately from the enclosing data
23198 // environment, because the restrictions are different. We only have to
23199 // check conflicts across regions for the map clauses.
23200 if (checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
23201 /*CurrentRegionOnly=*/true, CurComponents, CKind))
23202 break;
23203 if (CKind == OMPC_map &&
23204 (SemaRef.getLangOpts().OpenMP <= 45 || StartLoc.isValid()) &&
23205 checkMapConflicts(SemaRef, DSAS, VD: CurDeclaration, E: SimpleExpr,
23206 /*CurrentRegionOnly=*/false, CurComponents, CKind))
23207 break;
23208
23209 // OpenMP 4.5 [2.10.5, target update Construct]
23210 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
23211 // If the type of a list item is a reference to a type T then the type will
23212 // be considered to be T for all purposes of this clause.
23213 auto I = llvm::find_if(
23214 Range&: CurComponents,
23215 P: [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
23216 return MC.getAssociatedDeclaration();
23217 });
23218 assert(I != CurComponents.end() && "Null decl on map clause.");
23219 (void)I;
23220 QualType Type;
23221 auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: VE->IgnoreParens());
23222 auto *OASE = dyn_cast<ArraySectionExpr>(Val: VE->IgnoreParens());
23223 auto *OAShE = dyn_cast<OMPArrayShapingExpr>(Val: VE->IgnoreParens());
23224 if (ASE) {
23225 Type = ASE->getType().getNonReferenceType();
23226 } else if (OASE) {
23227 QualType BaseType =
23228 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
23229 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
23230 Type = ATy->getElementType();
23231 else
23232 Type = BaseType->getPointeeType();
23233 Type = Type.getNonReferenceType();
23234 } else if (OAShE) {
23235 Type = OAShE->getBase()->getType()->getPointeeType();
23236 } else {
23237 Type = VE->getType();
23238 }
23239
23240 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
23241 // A list item in a to or from clause must have a mappable type.
23242 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
23243 // A list item must have a mappable type.
23244 if (!checkTypeMappable(SL: VE->getExprLoc(), SR: VE->getSourceRange(), SemaRef,
23245 Stack: DSAS, QTy: Type, /*FullCheck=*/true))
23246 continue;
23247
23248 if (CKind == OMPC_map) {
23249 // target enter data
23250 // OpenMP [2.10.2, Restrictions, p. 99]
23251 // A map-type must be specified in all map clauses and must be either
23252 // to or alloc. Starting with OpenMP 5.2 the default map type is `to` if
23253 // no map type is present.
23254 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
23255 if (DKind == OMPD_target_enter_data &&
23256 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc ||
23257 SemaRef.getLangOpts().OpenMP >= 52)) {
23258 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23259 << (IsMapTypeImplicit ? 1 : 0)
23260 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23261 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23262 continue;
23263 }
23264
23265 // target exit_data
23266 // OpenMP [2.10.3, Restrictions, p. 102]
23267 // A map-type must be specified in all map clauses and must be either
23268 // from, release, or delete. Starting with OpenMP 5.2 the default map
23269 // type is `from` if no map type is present.
23270 if (DKind == OMPD_target_exit_data &&
23271 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
23272 MapType == OMPC_MAP_delete || SemaRef.getLangOpts().OpenMP >= 52)) {
23273 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23274 << (IsMapTypeImplicit ? 1 : 0)
23275 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23276 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23277 continue;
23278 }
23279
23280 // The 'ompx_hold' modifier is specifically intended to be used on a
23281 // 'target' or 'target data' directive to prevent data from being unmapped
23282 // during the associated statement. It is not permitted on a 'target
23283 // enter data' or 'target exit data' directive, which have no associated
23284 // statement.
23285 if ((DKind == OMPD_target_enter_data || DKind == OMPD_target_exit_data) &&
23286 HasHoldModifier) {
23287 SemaRef.Diag(Loc: StartLoc,
23288 DiagID: diag::err_omp_invalid_map_type_modifier_for_directive)
23289 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map,
23290 Type: OMPC_MAP_MODIFIER_ompx_hold)
23291 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23292 continue;
23293 }
23294
23295 // target, target data
23296 // OpenMP 5.0 [2.12.2, Restrictions, p. 163]
23297 // OpenMP 5.0 [2.12.5, Restrictions, p. 174]
23298 // A map-type in a map clause must be to, from, tofrom or alloc
23299 if ((DKind == OMPD_target_data ||
23300 isOpenMPTargetExecutionDirective(DKind)) &&
23301 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_from ||
23302 MapType == OMPC_MAP_tofrom || MapType == OMPC_MAP_alloc)) {
23303 SemaRef.Diag(Loc: StartLoc, DiagID: diag::err_omp_invalid_map_type_for_directive)
23304 << (IsMapTypeImplicit ? 1 : 0)
23305 << getOpenMPSimpleClauseTypeName(Kind: OMPC_map, Type: MapType)
23306 << getOpenMPDirectiveName(D: DKind, Ver: OMPVersion);
23307 continue;
23308 }
23309
23310 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
23311 // A list item cannot appear in both a map clause and a data-sharing
23312 // attribute clause on the same construct
23313 //
23314 // OpenMP 5.0 [2.19.7.1, Restrictions, p.7]
23315 // A list item cannot appear in both a map clause and a data-sharing
23316 // attribute clause on the same construct unless the construct is a
23317 // combined construct.
23318 if (VD && ((SemaRef.LangOpts.OpenMP <= 45 &&
23319 isOpenMPTargetExecutionDirective(DKind)) ||
23320 DKind == OMPD_target)) {
23321 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(D: VD, /*FromParent=*/false);
23322 if (isOpenMPPrivate(Kind: DVar.CKind)) {
23323 SemaRef.Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
23324 << getOpenMPClauseNameForDiag(C: DVar.CKind)
23325 << getOpenMPClauseNameForDiag(C: OMPC_map)
23326 << getOpenMPDirectiveName(D: DSAS->getCurrentDirective(),
23327 Ver: OMPVersion);
23328 reportOriginalDsa(SemaRef, Stack: DSAS, D: CurDeclaration, DVar);
23329 continue;
23330 }
23331 }
23332 }
23333
23334 // Try to find the associated user-defined mapper.
23335 ExprResult ER = buildUserDefinedMapperRef(
23336 SemaRef, S: DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
23337 Type: Type.getCanonicalType(), UnresolvedMapper);
23338 if (ER.isInvalid())
23339 continue;
23340
23341 // If no user-defined mapper is found, we need to create an implicit one for
23342 // arrays/array-sections on structs that have members that have
23343 // user-defined mappers. This is needed to ensure that the mapper for the
23344 // member is invoked when mapping each element of the array/array-section.
23345 if (!ER.get()) {
23346 QualType BaseType;
23347
23348 if (isa<ArraySectionExpr>(Val: VE)) {
23349 BaseType = VE->getType().getCanonicalType();
23350 if (BaseType->isSpecificBuiltinType(K: BuiltinType::ArraySection)) {
23351 const auto *OASE = cast<ArraySectionExpr>(Val: VE->IgnoreParenImpCasts());
23352 QualType BType =
23353 ArraySectionExpr::getBaseOriginalType(Base: OASE->getBase());
23354 QualType ElemType;
23355 if (const auto *ATy = BType->getAsArrayTypeUnsafe())
23356 ElemType = ATy->getElementType();
23357 else
23358 ElemType = BType->getPointeeType();
23359 BaseType = ElemType.getCanonicalType();
23360 }
23361 } else if (VE->getType()->isArrayType()) {
23362 const ArrayType *AT = VE->getType()->getAsArrayTypeUnsafe();
23363 const QualType ElemType = AT->getElementType();
23364 BaseType = ElemType.getCanonicalType();
23365 }
23366
23367 if (!BaseType.isNull() && BaseType->getAsRecordDecl() &&
23368 isImplicitMapperNeeded(S&: SemaRef, Stack: DSAS, CanonType: BaseType, E: VE)) {
23369 ER = buildImplicitMapper(S&: SemaRef, BaseType, Stack: DSAS);
23370 }
23371 }
23372 MVLI.UDMapperList.push_back(Elt: ER.get());
23373
23374 // Save the current expression.
23375 MVLI.ProcessedVarList.push_back(Elt: RE);
23376
23377 // Store the components in the stack so that they can be used to check
23378 // against other clauses later on.
23379 DSAS->addMappableExpressionComponents(VD: CurDeclaration, Components: CurComponents,
23380 /*WhereFoundClauseKind=*/OMPC_map);
23381
23382 // Save the components and declaration to create the clause. For purposes of
23383 // the clause creation, any component list that has base 'this' uses
23384 // null as base declaration.
23385 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
23386 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
23387 in_end: CurComponents.end());
23388 MVLI.VarBaseDeclarations.push_back(Elt: isa<MemberExpr>(Val: BE) ? nullptr
23389 : CurDeclaration);
23390 }
23391}
23392
23393OMPClause *SemaOpenMP::ActOnOpenMPMapClause(
23394 Expr *IteratorModifier, ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
23395 ArrayRef<SourceLocation> MapTypeModifiersLoc,
23396 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
23397 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
23398 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
23399 const OMPVarListLocTy &Locs, bool NoDiagnose,
23400 ArrayRef<Expr *> UnresolvedMappers) {
23401 OpenMPMapModifierKind Modifiers[] = {
23402 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23403 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23404 OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
23405 OMPC_MAP_MODIFIER_unknown};
23406 SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
23407
23408 if (IteratorModifier && !IteratorModifier->getType()->isSpecificBuiltinType(
23409 K: BuiltinType::OMPIterator))
23410 Diag(Loc: IteratorModifier->getExprLoc(),
23411 DiagID: diag::err_omp_map_modifier_not_iterator);
23412
23413 // Process map-type-modifiers, flag errors for duplicate modifiers.
23414 unsigned Count = 0;
23415 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
23416 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
23417 llvm::is_contained(Range&: Modifiers, Element: MapTypeModifiers[I])) {
23418 Diag(Loc: MapTypeModifiersLoc[I], DiagID: diag::err_omp_duplicate_map_type_modifier);
23419 continue;
23420 }
23421 assert(Count < NumberOfOMPMapClauseModifiers &&
23422 "Modifiers exceed the allowed number of map type modifiers");
23423 Modifiers[Count] = MapTypeModifiers[I];
23424 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
23425 ++Count;
23426 }
23427
23428 MappableVarListInfo MVLI(VarList);
23429 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_map, MVLI, StartLoc: Locs.StartLoc,
23430 MapperIdScopeSpec, MapperId, UnresolvedMappers,
23431 MapType, Modifiers, IsMapTypeImplicit,
23432 NoDiagnose);
23433
23434 // We need to produce a map clause even if we don't have variables so that
23435 // other diagnostics related with non-existing map clauses are accurate.
23436 return OMPMapClause::Create(
23437 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
23438 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier, MapModifiers: Modifiers,
23439 MapModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
23440 MapperId, Type: MapType, TypeIsImplicit: IsMapTypeImplicit, TypeLoc: MapLoc);
23441}
23442
23443QualType SemaOpenMP::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
23444 TypeResult ParsedType) {
23445 assert(ParsedType.isUsable());
23446
23447 QualType ReductionType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
23448 if (ReductionType.isNull())
23449 return QualType();
23450
23451 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
23452 // A type name in a declare reduction directive cannot be a function type, an
23453 // array type, a reference type, or a type qualified with const, volatile or
23454 // restrict.
23455 if (ReductionType.hasQualifiers()) {
23456 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 0;
23457 return QualType();
23458 }
23459
23460 if (ReductionType->isFunctionType()) {
23461 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 1;
23462 return QualType();
23463 }
23464 if (ReductionType->isReferenceType()) {
23465 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 2;
23466 return QualType();
23467 }
23468 if (ReductionType->isArrayType()) {
23469 Diag(Loc: TyLoc, DiagID: diag::err_omp_reduction_wrong_type) << 3;
23470 return QualType();
23471 }
23472 return ReductionType;
23473}
23474
23475SemaOpenMP::DeclGroupPtrTy
23476SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveStart(
23477 Scope *S, DeclContext *DC, DeclarationName Name,
23478 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
23479 AccessSpecifier AS, Decl *PrevDeclInScope) {
23480 SmallVector<Decl *, 8> Decls;
23481 Decls.reserve(N: ReductionTypes.size());
23482
23483 LookupResult Lookup(SemaRef, Name, SourceLocation(),
23484 Sema::LookupOMPReductionName,
23485 SemaRef.forRedeclarationInCurContext());
23486 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
23487 // A reduction-identifier may not be re-declared in the current scope for the
23488 // same type or for a type that is compatible according to the base language
23489 // rules.
23490 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
23491 OMPDeclareReductionDecl *PrevDRD = nullptr;
23492 bool InCompoundScope = true;
23493 if (S != nullptr) {
23494 // Find previous declaration with the same name not referenced in other
23495 // declarations.
23496 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
23497 InCompoundScope =
23498 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
23499 SemaRef.LookupName(R&: Lookup, S);
23500 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
23501 /*AllowInlineNamespace=*/false);
23502 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
23503 LookupResult::Filter Filter = Lookup.makeFilter();
23504 while (Filter.hasNext()) {
23505 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Val: Filter.next());
23506 if (InCompoundScope) {
23507 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
23508 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
23509 UsedAsPrevious[D] = true;
23510 }
23511 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
23512 PrevDecl->getLocation();
23513 }
23514 Filter.done();
23515 if (InCompoundScope) {
23516 for (const auto &PrevData : UsedAsPrevious) {
23517 if (!PrevData.second) {
23518 PrevDRD = PrevData.first;
23519 break;
23520 }
23521 }
23522 }
23523 } else if (PrevDeclInScope != nullptr) {
23524 auto *PrevDRDInScope = PrevDRD =
23525 cast<OMPDeclareReductionDecl>(Val: PrevDeclInScope);
23526 do {
23527 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
23528 PrevDRDInScope->getLocation();
23529 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
23530 } while (PrevDRDInScope != nullptr);
23531 }
23532 for (const auto &TyData : ReductionTypes) {
23533 const auto I = PreviousRedeclTypes.find(Val: TyData.first.getCanonicalType());
23534 bool Invalid = false;
23535 if (I != PreviousRedeclTypes.end()) {
23536 Diag(Loc: TyData.second, DiagID: diag::err_omp_declare_reduction_redefinition)
23537 << TyData.first;
23538 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
23539 Invalid = true;
23540 }
23541 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
23542 auto *DRD = OMPDeclareReductionDecl::Create(
23543 C&: getASTContext(), DC, L: TyData.second, Name, T: TyData.first, PrevDeclInScope: PrevDRD);
23544 DC->addDecl(D: DRD);
23545 DRD->setAccess(AS);
23546 Decls.push_back(Elt: DRD);
23547 if (Invalid)
23548 DRD->setInvalidDecl();
23549 else
23550 PrevDRD = DRD;
23551 }
23552
23553 return DeclGroupPtrTy::make(
23554 P: DeclGroupRef::Create(C&: getASTContext(), Decls: Decls.begin(), NumDecls: Decls.size()));
23555}
23556
23557void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
23558 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23559
23560 // Enter new function scope.
23561 SemaRef.PushFunctionScope();
23562 SemaRef.setFunctionHasBranchProtectedScope();
23563 SemaRef.getCurFunction()->setHasOMPDeclareReductionCombiner();
23564
23565 if (S != nullptr)
23566 SemaRef.PushDeclContext(S, DC: DRD);
23567 else
23568 SemaRef.CurContext = DRD;
23569
23570 SemaRef.PushExpressionEvaluationContext(
23571 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
23572
23573 QualType ReductionType = DRD->getType();
23574 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
23575 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
23576 // uses semantics of argument handles by value, but it should be passed by
23577 // reference. C lang does not support references, so pass all parameters as
23578 // pointers.
23579 // Create 'T omp_in;' variable.
23580 VarDecl *OmpInParm =
23581 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_in");
23582 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
23583 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
23584 // uses semantics of argument handles by value, but it should be passed by
23585 // reference. C lang does not support references, so pass all parameters as
23586 // pointers.
23587 // Create 'T omp_out;' variable.
23588 VarDecl *OmpOutParm =
23589 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_out");
23590 if (S != nullptr) {
23591 SemaRef.PushOnScopeChains(D: OmpInParm, S);
23592 SemaRef.PushOnScopeChains(D: OmpOutParm, S);
23593 } else {
23594 DRD->addDecl(D: OmpInParm);
23595 DRD->addDecl(D: OmpOutParm);
23596 }
23597 Expr *InE =
23598 ::buildDeclRefExpr(S&: SemaRef, D: OmpInParm, Ty: ReductionType, Loc: D->getLocation());
23599 Expr *OutE =
23600 ::buildDeclRefExpr(S&: SemaRef, D: OmpOutParm, Ty: ReductionType, Loc: D->getLocation());
23601 DRD->setCombinerData(InE, OutE);
23602}
23603
23604void SemaOpenMP::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D,
23605 Expr *Combiner) {
23606 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23607 SemaRef.DiscardCleanupsInEvaluationContext();
23608 SemaRef.PopExpressionEvaluationContext();
23609
23610 SemaRef.PopDeclContext();
23611 SemaRef.PopFunctionScopeInfo();
23612
23613 if (Combiner != nullptr)
23614 DRD->setCombiner(Combiner);
23615 else
23616 DRD->setInvalidDecl();
23617}
23618
23619VarDecl *SemaOpenMP::ActOnOpenMPDeclareReductionInitializerStart(Scope *S,
23620 Decl *D) {
23621 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23622
23623 // Enter new function scope.
23624 SemaRef.PushFunctionScope();
23625 SemaRef.setFunctionHasBranchProtectedScope();
23626
23627 if (S != nullptr)
23628 SemaRef.PushDeclContext(S, DC: DRD);
23629 else
23630 SemaRef.CurContext = DRD;
23631
23632 SemaRef.PushExpressionEvaluationContext(
23633 NewContext: Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
23634
23635 QualType ReductionType = DRD->getType();
23636 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
23637 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
23638 // uses semantics of argument handles by value, but it should be passed by
23639 // reference. C lang does not support references, so pass all parameters as
23640 // pointers.
23641 // Create 'T omp_priv;' variable.
23642 VarDecl *OmpPrivParm =
23643 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_priv");
23644 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
23645 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
23646 // uses semantics of argument handles by value, but it should be passed by
23647 // reference. C lang does not support references, so pass all parameters as
23648 // pointers.
23649 // Create 'T omp_orig;' variable.
23650 VarDecl *OmpOrigParm =
23651 buildVarDecl(SemaRef, Loc: D->getLocation(), Type: ReductionType, Name: "omp_orig");
23652 if (S != nullptr) {
23653 SemaRef.PushOnScopeChains(D: OmpPrivParm, S);
23654 SemaRef.PushOnScopeChains(D: OmpOrigParm, S);
23655 } else {
23656 DRD->addDecl(D: OmpPrivParm);
23657 DRD->addDecl(D: OmpOrigParm);
23658 }
23659 Expr *OrigE =
23660 ::buildDeclRefExpr(S&: SemaRef, D: OmpOrigParm, Ty: ReductionType, Loc: D->getLocation());
23661 Expr *PrivE =
23662 ::buildDeclRefExpr(S&: SemaRef, D: OmpPrivParm, Ty: ReductionType, Loc: D->getLocation());
23663 DRD->setInitializerData(OrigE, PrivE);
23664 return OmpPrivParm;
23665}
23666
23667void SemaOpenMP::ActOnOpenMPDeclareReductionInitializerEnd(
23668 Decl *D, Expr *Initializer, VarDecl *OmpPrivParm) {
23669 auto *DRD = cast<OMPDeclareReductionDecl>(Val: D);
23670 SemaRef.DiscardCleanupsInEvaluationContext();
23671 SemaRef.PopExpressionEvaluationContext();
23672
23673 SemaRef.PopDeclContext();
23674 SemaRef.PopFunctionScopeInfo();
23675
23676 if (Initializer != nullptr) {
23677 DRD->setInitializer(E: Initializer, IK: OMPDeclareReductionInitKind::Call);
23678 } else if (OmpPrivParm->hasInit()) {
23679 DRD->setInitializer(E: OmpPrivParm->getInit(),
23680 IK: OmpPrivParm->isDirectInit()
23681 ? OMPDeclareReductionInitKind::Direct
23682 : OMPDeclareReductionInitKind::Copy);
23683 } else {
23684 DRD->setInvalidDecl();
23685 }
23686}
23687
23688SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareReductionDirectiveEnd(
23689 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
23690 for (Decl *D : DeclReductions.get()) {
23691 if (IsValid) {
23692 if (S)
23693 SemaRef.PushOnScopeChains(D: cast<OMPDeclareReductionDecl>(Val: D), S,
23694 /*AddToContext=*/false);
23695 } else {
23696 D->setInvalidDecl();
23697 }
23698 }
23699 return DeclReductions;
23700}
23701
23702TypeResult SemaOpenMP::ActOnOpenMPDeclareMapperVarDecl(Scope *S,
23703 Declarator &D) {
23704 TypeSourceInfo *TInfo = SemaRef.GetTypeForDeclarator(D);
23705 QualType T = TInfo->getType();
23706 if (D.isInvalidType())
23707 return true;
23708
23709 if (getLangOpts().CPlusPlus) {
23710 // Check that there are no default arguments (C++ only).
23711 SemaRef.CheckExtraCXXDefaultArguments(D);
23712 }
23713
23714 return SemaRef.CreateParsedType(T, TInfo);
23715}
23716
23717QualType SemaOpenMP::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
23718 TypeResult ParsedType) {
23719 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
23720
23721 QualType MapperType = SemaRef.GetTypeFromParser(Ty: ParsedType.get());
23722 assert(!MapperType.isNull() && "Expect valid mapper type");
23723
23724 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
23725 // The type must be of struct, union or class type in C and C++
23726 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
23727 Diag(Loc: TyLoc, DiagID: diag::err_omp_mapper_wrong_type);
23728 return QualType();
23729 }
23730 return MapperType;
23731}
23732
23733SemaOpenMP::DeclGroupPtrTy SemaOpenMP::ActOnOpenMPDeclareMapperDirective(
23734 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
23735 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
23736 Expr *MapperVarRef, ArrayRef<OMPClause *> Clauses, Decl *PrevDeclInScope) {
23737 LookupResult Lookup(SemaRef, Name, SourceLocation(),
23738 Sema::LookupOMPMapperName,
23739 SemaRef.forRedeclarationInCurContext());
23740 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
23741 // A mapper-identifier may not be redeclared in the current scope for the
23742 // same type or for a type that is compatible according to the base language
23743 // rules.
23744 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
23745 OMPDeclareMapperDecl *PrevDMD = nullptr;
23746 bool InCompoundScope = true;
23747 if (S != nullptr) {
23748 // Find previous declaration with the same name not referenced in other
23749 // declarations.
23750 FunctionScopeInfo *ParentFn = SemaRef.getEnclosingFunction();
23751 InCompoundScope =
23752 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
23753 SemaRef.LookupName(R&: Lookup, S);
23754 SemaRef.FilterLookupForScope(R&: Lookup, Ctx: DC, S, /*ConsiderLinkage=*/false,
23755 /*AllowInlineNamespace=*/false);
23756 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
23757 LookupResult::Filter Filter = Lookup.makeFilter();
23758 while (Filter.hasNext()) {
23759 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Val: Filter.next());
23760 if (InCompoundScope) {
23761 UsedAsPrevious.try_emplace(Key: PrevDecl, Args: false);
23762 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
23763 UsedAsPrevious[D] = true;
23764 }
23765 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
23766 PrevDecl->getLocation();
23767 }
23768 Filter.done();
23769 if (InCompoundScope) {
23770 for (const auto &PrevData : UsedAsPrevious) {
23771 if (!PrevData.second) {
23772 PrevDMD = PrevData.first;
23773 break;
23774 }
23775 }
23776 }
23777 } else if (PrevDeclInScope) {
23778 auto *PrevDMDInScope = PrevDMD =
23779 cast<OMPDeclareMapperDecl>(Val: PrevDeclInScope);
23780 do {
23781 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
23782 PrevDMDInScope->getLocation();
23783 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
23784 } while (PrevDMDInScope != nullptr);
23785 }
23786 const auto I = PreviousRedeclTypes.find(Val: MapperType.getCanonicalType());
23787 bool Invalid = false;
23788 if (I != PreviousRedeclTypes.end()) {
23789 Diag(Loc: StartLoc, DiagID: diag::err_omp_declare_mapper_redefinition)
23790 << MapperType << Name;
23791 Diag(Loc: I->second, DiagID: diag::note_previous_definition);
23792 Invalid = true;
23793 }
23794 // Build expressions for implicit maps of data members with 'default'
23795 // mappers.
23796 SmallVector<OMPClause *, 4> ClausesWithImplicit(Clauses);
23797 if (getLangOpts().OpenMP >= 50)
23798 processImplicitMapsWithDefaultMappers(S&: SemaRef, DSAStack,
23799 Clauses&: ClausesWithImplicit);
23800 auto *DMD = OMPDeclareMapperDecl::Create(C&: getASTContext(), DC, L: StartLoc, Name,
23801 T: MapperType, VarName: VN, Clauses: ClausesWithImplicit,
23802 PrevDeclInScope: PrevDMD);
23803 if (S)
23804 SemaRef.PushOnScopeChains(D: DMD, S);
23805 else
23806 DC->addDecl(D: DMD);
23807 DMD->setAccess(AS);
23808 if (Invalid)
23809 DMD->setInvalidDecl();
23810
23811 auto *VD = cast<DeclRefExpr>(Val: MapperVarRef)->getDecl();
23812 VD->setDeclContext(DMD);
23813 VD->setLexicalDeclContext(DMD);
23814 DMD->addDecl(D: VD);
23815 DMD->setMapperVarRef(MapperVarRef);
23816
23817 return DeclGroupPtrTy::make(P: DeclGroupRef(DMD));
23818}
23819
23820ExprResult SemaOpenMP::ActOnOpenMPDeclareMapperDirectiveVarDecl(
23821 Scope *S, QualType MapperType, SourceLocation StartLoc,
23822 DeclarationName VN) {
23823 TypeSourceInfo *TInfo =
23824 getASTContext().getTrivialTypeSourceInfo(T: MapperType, Loc: StartLoc);
23825 auto *VD = VarDecl::Create(
23826 C&: getASTContext(), DC: getASTContext().getTranslationUnitDecl(), StartLoc,
23827 IdLoc: StartLoc, Id: VN.getAsIdentifierInfo(), T: MapperType, TInfo, S: SC_None);
23828 if (S)
23829 SemaRef.PushOnScopeChains(D: VD, S, /*AddToContext=*/false);
23830 Expr *E = buildDeclRefExpr(S&: SemaRef, D: VD, Ty: MapperType, Loc: StartLoc);
23831 DSAStack->addDeclareMapperVarRef(Ref: E);
23832 return E;
23833}
23834
23835void SemaOpenMP::ActOnOpenMPIteratorVarDecl(VarDecl *VD) {
23836 bool IsGlobalVar =
23837 !VD->isLocalVarDecl() && VD->getDeclContext()->isTranslationUnit();
23838 if (DSAStack->getDeclareMapperVarRef()) {
23839 if (IsGlobalVar)
23840 SemaRef.Consumer.HandleTopLevelDecl(D: DeclGroupRef(VD));
23841 DSAStack->addIteratorVarDecl(VD);
23842 } else {
23843 // Currently, only declare mapper handles global-scope iterator vars.
23844 assert(!IsGlobalVar && "Only declare mapper handles TU-scope iterators.");
23845 }
23846}
23847
23848bool SemaOpenMP::isOpenMPDeclareMapperVarDeclAllowed(const VarDecl *VD) const {
23849 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
23850 const Expr *Ref = DSAStack->getDeclareMapperVarRef();
23851 if (const auto *DRE = cast_or_null<DeclRefExpr>(Val: Ref)) {
23852 if (VD->getCanonicalDecl() == DRE->getDecl()->getCanonicalDecl())
23853 return true;
23854 if (VD->isUsableInConstantExpressions(C: getASTContext()))
23855 return true;
23856 if (getLangOpts().OpenMP >= 52 && DSAStack->isIteratorVarDecl(VD))
23857 return true;
23858 return false;
23859 }
23860 return true;
23861}
23862
23863const ValueDecl *SemaOpenMP::getOpenMPDeclareMapperVarName() const {
23864 assert(getLangOpts().OpenMP && "Expected OpenMP mode.");
23865 return cast<DeclRefExpr>(DSAStack->getDeclareMapperVarRef())->getDecl();
23866}
23867
23868OMPClause *SemaOpenMP::ActOnOpenMPNumTeamsClause(ArrayRef<Expr *> VarList,
23869 SourceLocation StartLoc,
23870 SourceLocation LParenLoc,
23871 SourceLocation EndLoc) {
23872 if (VarList.empty())
23873 return nullptr;
23874
23875 for (Expr *ValExpr : VarList) {
23876 // OpenMP [teams Constrcut, Restrictions]
23877 // The num_teams expression must evaluate to a positive integer value.
23878 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_num_teams,
23879 /*StrictlyPositive=*/true))
23880 return nullptr;
23881 }
23882
23883 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
23884 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
23885 DKind, CKind: OMPC_num_teams, OpenMPVersion: getLangOpts().OpenMP);
23886 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
23887 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
23888 LParenLoc, EndLoc, VL: VarList,
23889 /*PreInit=*/nullptr);
23890
23891 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
23892 SmallVector<Expr *, 3> Vars;
23893 for (Expr *ValExpr : VarList) {
23894 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
23895 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
23896 Vars.push_back(Elt: ValExpr);
23897 }
23898
23899 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
23900 return OMPNumTeamsClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
23901 LParenLoc, EndLoc, VL: Vars, PreInit);
23902}
23903
23904OMPClause *SemaOpenMP::ActOnOpenMPThreadLimitClause(ArrayRef<Expr *> VarList,
23905 SourceLocation StartLoc,
23906 SourceLocation LParenLoc,
23907 SourceLocation EndLoc) {
23908 if (VarList.empty())
23909 return nullptr;
23910
23911 for (Expr *ValExpr : VarList) {
23912 // OpenMP [teams Constrcut, Restrictions]
23913 // The thread_limit expression must evaluate to a positive integer value.
23914 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_thread_limit,
23915 /*StrictlyPositive=*/true))
23916 return nullptr;
23917 }
23918
23919 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
23920 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
23921 DKind, CKind: OMPC_thread_limit, OpenMPVersion: getLangOpts().OpenMP);
23922 if (CaptureRegion == OMPD_unknown || SemaRef.CurContext->isDependentContext())
23923 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion,
23924 StartLoc, LParenLoc, EndLoc, VL: VarList,
23925 /*PreInit=*/nullptr);
23926
23927 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
23928 SmallVector<Expr *, 3> Vars;
23929 for (Expr *ValExpr : VarList) {
23930 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
23931 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
23932 Vars.push_back(Elt: ValExpr);
23933 }
23934
23935 Stmt *PreInit = buildPreInits(Context&: getASTContext(), Captures);
23936 return OMPThreadLimitClause::Create(C: getASTContext(), CaptureRegion, StartLoc,
23937 LParenLoc, EndLoc, VL: Vars, PreInit);
23938}
23939
23940OMPClause *SemaOpenMP::ActOnOpenMPPriorityClause(Expr *Priority,
23941 SourceLocation StartLoc,
23942 SourceLocation LParenLoc,
23943 SourceLocation EndLoc) {
23944 Expr *ValExpr = Priority;
23945 Stmt *HelperValStmt = nullptr;
23946 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
23947
23948 // OpenMP [2.9.1, task Constrcut]
23949 // The priority-value is a non-negative numerical scalar expression.
23950 if (!isNonNegativeIntegerValue(
23951 ValExpr, SemaRef, CKind: OMPC_priority,
23952 /*StrictlyPositive=*/false, /*BuildCapture=*/true,
23953 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
23954 return nullptr;
23955
23956 return new (getASTContext()) OMPPriorityClause(
23957 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
23958}
23959
23960OMPClause *SemaOpenMP::ActOnOpenMPGrainsizeClause(
23961 OpenMPGrainsizeClauseModifier Modifier, Expr *Grainsize,
23962 SourceLocation StartLoc, SourceLocation LParenLoc,
23963 SourceLocation ModifierLoc, SourceLocation EndLoc) {
23964 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
23965 "Unexpected grainsize modifier in OpenMP < 51.");
23966
23967 if (ModifierLoc.isValid() && Modifier == OMPC_GRAINSIZE_unknown) {
23968 std::string Values = getListOfPossibleValues(K: OMPC_grainsize, /*First=*/0,
23969 Last: OMPC_GRAINSIZE_unknown);
23970 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
23971 << Values << getOpenMPClauseNameForDiag(C: OMPC_grainsize);
23972 return nullptr;
23973 }
23974
23975 Expr *ValExpr = Grainsize;
23976 Stmt *HelperValStmt = nullptr;
23977 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
23978
23979 // OpenMP [2.9.2, taskloop Constrcut]
23980 // The parameter of the grainsize clause must be a positive integer
23981 // expression.
23982 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_grainsize,
23983 /*StrictlyPositive=*/true,
23984 /*BuildCapture=*/true,
23985 DSAStack->getCurrentDirective(),
23986 CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
23987 return nullptr;
23988
23989 return new (getASTContext())
23990 OMPGrainsizeClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
23991 StartLoc, LParenLoc, ModifierLoc, EndLoc);
23992}
23993
23994OMPClause *SemaOpenMP::ActOnOpenMPNumTasksClause(
23995 OpenMPNumTasksClauseModifier Modifier, Expr *NumTasks,
23996 SourceLocation StartLoc, SourceLocation LParenLoc,
23997 SourceLocation ModifierLoc, SourceLocation EndLoc) {
23998 assert((ModifierLoc.isInvalid() || getLangOpts().OpenMP >= 51) &&
23999 "Unexpected num_tasks modifier in OpenMP < 51.");
24000
24001 if (ModifierLoc.isValid() && Modifier == OMPC_NUMTASKS_unknown) {
24002 std::string Values = getListOfPossibleValues(K: OMPC_num_tasks, /*First=*/0,
24003 Last: OMPC_NUMTASKS_unknown);
24004 Diag(Loc: ModifierLoc, DiagID: diag::err_omp_unexpected_clause_value)
24005 << Values << getOpenMPClauseNameForDiag(C: OMPC_num_tasks);
24006 return nullptr;
24007 }
24008
24009 Expr *ValExpr = NumTasks;
24010 Stmt *HelperValStmt = nullptr;
24011 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
24012
24013 // OpenMP [2.9.2, taskloop Constrcut]
24014 // The parameter of the num_tasks clause must be a positive integer
24015 // expression.
24016 if (!isNonNegativeIntegerValue(
24017 ValExpr, SemaRef, CKind: OMPC_num_tasks,
24018 /*StrictlyPositive=*/true, /*BuildCapture=*/true,
24019 DSAStack->getCurrentDirective(), CaptureRegion: &CaptureRegion, HelperValStmt: &HelperValStmt))
24020 return nullptr;
24021
24022 return new (getASTContext())
24023 OMPNumTasksClause(Modifier, ValExpr, HelperValStmt, CaptureRegion,
24024 StartLoc, LParenLoc, ModifierLoc, EndLoc);
24025}
24026
24027OMPClause *SemaOpenMP::ActOnOpenMPHintClause(Expr *Hint,
24028 SourceLocation StartLoc,
24029 SourceLocation LParenLoc,
24030 SourceLocation EndLoc) {
24031 // OpenMP [2.13.2, critical construct, Description]
24032 // ... where hint-expression is an integer constant expression that evaluates
24033 // to a valid lock hint.
24034 ExprResult HintExpr =
24035 VerifyPositiveIntegerConstantInClause(E: Hint, CKind: OMPC_hint, StrictlyPositive: false);
24036 if (HintExpr.isInvalid())
24037 return nullptr;
24038 return new (getASTContext())
24039 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
24040}
24041
24042/// Tries to find omp_event_handle_t type.
24043static bool findOMPEventHandleT(Sema &S, SourceLocation Loc,
24044 DSAStackTy *Stack) {
24045 QualType OMPEventHandleT = Stack->getOMPEventHandleT();
24046 if (!OMPEventHandleT.isNull())
24047 return true;
24048 IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name: "omp_event_handle_t");
24049 ParsedType PT = S.getTypeName(II: *II, NameLoc: Loc, S: S.getCurScope());
24050 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
24051 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_event_handle_t";
24052 return false;
24053 }
24054 Stack->setOMPEventHandleT(PT.get());
24055 return true;
24056}
24057
24058OMPClause *SemaOpenMP::ActOnOpenMPDetachClause(Expr *Evt,
24059 SourceLocation StartLoc,
24060 SourceLocation LParenLoc,
24061 SourceLocation EndLoc) {
24062 if (!Evt->isValueDependent() && !Evt->isTypeDependent() &&
24063 !Evt->isInstantiationDependent() &&
24064 !Evt->containsUnexpandedParameterPack()) {
24065 if (!findOMPEventHandleT(S&: SemaRef, Loc: Evt->getExprLoc(), DSAStack))
24066 return nullptr;
24067 // OpenMP 5.0, 2.10.1 task Construct.
24068 // event-handle is a variable of the omp_event_handle_t type.
24069 auto *Ref = dyn_cast<DeclRefExpr>(Val: Evt->IgnoreParenImpCasts());
24070 if (!Ref) {
24071 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24072 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
24073 return nullptr;
24074 }
24075 auto *VD = dyn_cast_or_null<VarDecl>(Val: Ref->getDecl());
24076 if (!VD) {
24077 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24078 << "omp_event_handle_t" << 0 << Evt->getSourceRange();
24079 return nullptr;
24080 }
24081 if (!getASTContext().hasSameUnqualifiedType(DSAStack->getOMPEventHandleT(),
24082 T2: VD->getType()) ||
24083 VD->getType().isConstant(Ctx: getASTContext())) {
24084 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_var_expected)
24085 << "omp_event_handle_t" << 1 << VD->getType()
24086 << Evt->getSourceRange();
24087 return nullptr;
24088 }
24089 // OpenMP 5.0, 2.10.1 task Construct
24090 // [detach clause]... The event-handle will be considered as if it was
24091 // specified on a firstprivate clause.
24092 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D: VD, /*FromParent=*/false);
24093 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
24094 DVar.RefExpr) {
24095 Diag(Loc: Evt->getExprLoc(), DiagID: diag::err_omp_wrong_dsa)
24096 << getOpenMPClauseNameForDiag(C: DVar.CKind)
24097 << getOpenMPClauseNameForDiag(C: OMPC_firstprivate);
24098 reportOriginalDsa(SemaRef, DSAStack, D: VD, DVar);
24099 return nullptr;
24100 }
24101 }
24102
24103 return new (getASTContext())
24104 OMPDetachClause(Evt, StartLoc, LParenLoc, EndLoc);
24105}
24106
24107OMPClause *SemaOpenMP::ActOnOpenMPDistScheduleClause(
24108 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
24109 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
24110 SourceLocation EndLoc) {
24111 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
24112 std::string Values;
24113 Values += "'";
24114 Values += getOpenMPSimpleClauseTypeName(Kind: OMPC_dist_schedule, Type: 0);
24115 Values += "'";
24116 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24117 << Values << getOpenMPClauseNameForDiag(C: OMPC_dist_schedule);
24118 return nullptr;
24119 }
24120 Expr *ValExpr = ChunkSize;
24121 Stmt *HelperValStmt = nullptr;
24122 if (ChunkSize) {
24123 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
24124 !ChunkSize->isInstantiationDependent() &&
24125 !ChunkSize->containsUnexpandedParameterPack()) {
24126 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
24127 ExprResult Val =
24128 PerformOpenMPImplicitIntegerConversion(Loc: ChunkSizeLoc, Op: ChunkSize);
24129 if (Val.isInvalid())
24130 return nullptr;
24131
24132 ValExpr = Val.get();
24133
24134 // OpenMP [2.7.1, Restrictions]
24135 // chunk_size must be a loop invariant integer expression with a positive
24136 // value.
24137 if (std::optional<llvm::APSInt> Result =
24138 ValExpr->getIntegerConstantExpr(Ctx: getASTContext())) {
24139 if (Result->isSigned() && !Result->isStrictlyPositive()) {
24140 Diag(Loc: ChunkSizeLoc, DiagID: diag::err_omp_negative_expression_in_clause)
24141 << "dist_schedule" << /*strictly positive*/ 1
24142 << ChunkSize->getSourceRange();
24143 return nullptr;
24144 }
24145 } else if (getOpenMPCaptureRegionForClause(
24146 DSAStack->getCurrentDirective(), CKind: OMPC_dist_schedule,
24147 OpenMPVersion: getLangOpts().OpenMP) != OMPD_unknown &&
24148 !SemaRef.CurContext->isDependentContext()) {
24149 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
24150 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
24151 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
24152 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
24153 }
24154 }
24155 }
24156
24157 return new (getASTContext())
24158 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
24159 Kind, ValExpr, HelperValStmt);
24160}
24161
24162OMPClause *SemaOpenMP::ActOnOpenMPDefaultmapClause(
24163 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
24164 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
24165 SourceLocation KindLoc, SourceLocation EndLoc) {
24166 if (getLangOpts().OpenMP < 50) {
24167 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
24168 Kind != OMPC_DEFAULTMAP_scalar) {
24169 std::string Value;
24170 SourceLocation Loc;
24171 Value += "'";
24172 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
24173 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
24174 Type: OMPC_DEFAULTMAP_MODIFIER_tofrom);
24175 Loc = MLoc;
24176 } else {
24177 Value += getOpenMPSimpleClauseTypeName(Kind: OMPC_defaultmap,
24178 Type: OMPC_DEFAULTMAP_scalar);
24179 Loc = KindLoc;
24180 }
24181 Value += "'";
24182 Diag(Loc, DiagID: diag::err_omp_unexpected_clause_value)
24183 << Value << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24184 return nullptr;
24185 }
24186 } else {
24187 bool isDefaultmapModifier = (M != OMPC_DEFAULTMAP_MODIFIER_unknown);
24188 bool isDefaultmapKind = (Kind != OMPC_DEFAULTMAP_unknown) ||
24189 (getLangOpts().OpenMP >= 50 && KindLoc.isInvalid());
24190 if (!isDefaultmapKind || !isDefaultmapModifier) {
24191 StringRef KindValue = getLangOpts().OpenMP < 52
24192 ? "'scalar', 'aggregate', 'pointer'"
24193 : "'scalar', 'aggregate', 'pointer', 'all'";
24194 if (getLangOpts().OpenMP == 50) {
24195 StringRef ModifierValue = "'alloc', 'from', 'to', 'tofrom', "
24196 "'firstprivate', 'none', 'default'";
24197 if (!isDefaultmapKind && isDefaultmapModifier) {
24198 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24199 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24200 } else if (isDefaultmapKind && !isDefaultmapModifier) {
24201 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24202 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24203 } else {
24204 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24205 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24206 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24207 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24208 }
24209 } else {
24210 StringRef ModifierValue =
24211 getLangOpts().OpenMP < 60
24212 ? "'alloc', 'from', 'to', 'tofrom', "
24213 "'firstprivate', 'none', 'default', 'present'"
24214 : "'storage', 'from', 'to', 'tofrom', "
24215 "'firstprivate', 'private', 'none', 'default', 'present'";
24216 if (!isDefaultmapKind && isDefaultmapModifier) {
24217 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24218 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24219 } else if (isDefaultmapKind && !isDefaultmapModifier) {
24220 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24221 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24222 } else {
24223 Diag(Loc: MLoc, DiagID: diag::err_omp_unexpected_clause_value)
24224 << ModifierValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24225 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
24226 << KindValue << getOpenMPClauseNameForDiag(C: OMPC_defaultmap);
24227 }
24228 }
24229 return nullptr;
24230 }
24231
24232 // OpenMP [5.0, 2.12.5, Restrictions, p. 174]
24233 // At most one defaultmap clause for each category can appear on the
24234 // directive.
24235 if (DSAStack->checkDefaultmapCategory(VariableCategory: Kind)) {
24236 Diag(Loc: StartLoc, DiagID: diag::err_omp_one_defaultmap_each_category);
24237 return nullptr;
24238 }
24239 }
24240 if (Kind == OMPC_DEFAULTMAP_unknown || Kind == OMPC_DEFAULTMAP_all) {
24241 // Variable category is not specified - mark all categories.
24242 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_aggregate, Loc: StartLoc);
24243 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_scalar, Loc: StartLoc);
24244 DSAStack->setDefaultDMAAttr(M, Kind: OMPC_DEFAULTMAP_pointer, Loc: StartLoc);
24245 } else {
24246 DSAStack->setDefaultDMAAttr(M, Kind, Loc: StartLoc);
24247 }
24248
24249 return new (getASTContext())
24250 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
24251}
24252
24253bool SemaOpenMP::ActOnStartOpenMPDeclareTargetContext(
24254 DeclareTargetContextInfo &DTCI) {
24255 DeclContext *CurLexicalContext = SemaRef.getCurLexicalContext();
24256 if (!CurLexicalContext->isFileContext() &&
24257 !CurLexicalContext->isExternCContext() &&
24258 !CurLexicalContext->isExternCXXContext() &&
24259 !isa<CXXRecordDecl>(Val: CurLexicalContext) &&
24260 !isa<ClassTemplateDecl>(Val: CurLexicalContext) &&
24261 !isa<ClassTemplatePartialSpecializationDecl>(Val: CurLexicalContext) &&
24262 !isa<ClassTemplateSpecializationDecl>(Val: CurLexicalContext)) {
24263 Diag(Loc: DTCI.Loc, DiagID: diag::err_omp_region_not_file_context);
24264 return false;
24265 }
24266
24267 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
24268 if (getLangOpts().HIP)
24269 Diag(Loc: DTCI.Loc, DiagID: diag::warn_hip_omp_target_directives);
24270
24271 DeclareTargetNesting.push_back(Elt: DTCI);
24272 return true;
24273}
24274
24275const SemaOpenMP::DeclareTargetContextInfo
24276SemaOpenMP::ActOnOpenMPEndDeclareTargetDirective() {
24277 assert(!DeclareTargetNesting.empty() &&
24278 "check isInOpenMPDeclareTargetContext() first!");
24279 return DeclareTargetNesting.pop_back_val();
24280}
24281
24282void SemaOpenMP::ActOnFinishedOpenMPDeclareTargetContext(
24283 DeclareTargetContextInfo &DTCI) {
24284 for (auto &It : DTCI.ExplicitlyMapped)
24285 ActOnOpenMPDeclareTargetName(ND: It.first, Loc: It.second.Loc, MT: It.second.MT, DTCI);
24286}
24287
24288void SemaOpenMP::DiagnoseUnterminatedOpenMPDeclareTarget() {
24289 if (DeclareTargetNesting.empty())
24290 return;
24291 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
24292 unsigned OMPVersion = getLangOpts().OpenMP;
24293 Diag(Loc: DTCI.Loc, DiagID: diag::warn_omp_unterminated_declare_target)
24294 << getOpenMPDirectiveName(D: DTCI.Kind, Ver: OMPVersion);
24295}
24296
24297NamedDecl *SemaOpenMP::lookupOpenMPDeclareTargetName(
24298 Scope *CurScope, CXXScopeSpec &ScopeSpec, const DeclarationNameInfo &Id) {
24299 LookupResult Lookup(SemaRef, Id, Sema::LookupOrdinaryName);
24300 SemaRef.LookupParsedName(R&: Lookup, S: CurScope, SS: &ScopeSpec,
24301 /*ObjectType=*/QualType(),
24302 /*AllowBuiltinCreation=*/true);
24303
24304 if (Lookup.isAmbiguous())
24305 return nullptr;
24306 Lookup.suppressDiagnostics();
24307
24308 if (!Lookup.isSingleResult()) {
24309 VarOrFuncDeclFilterCCC CCC(SemaRef);
24310 if (TypoCorrection Corrected =
24311 SemaRef.CorrectTypo(Typo: Id, LookupKind: Sema::LookupOrdinaryName, S: CurScope, SS: nullptr,
24312 CCC, Mode: CorrectTypoKind::ErrorRecovery)) {
24313 SemaRef.diagnoseTypo(Correction: Corrected,
24314 TypoDiag: SemaRef.PDiag(DiagID: diag::err_undeclared_var_use_suggest)
24315 << Id.getName());
24316 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: Corrected.getCorrectionDecl());
24317 return nullptr;
24318 }
24319
24320 Diag(Loc: Id.getLoc(), DiagID: diag::err_undeclared_var_use) << Id.getName();
24321 return nullptr;
24322 }
24323
24324 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
24325 if (!isa<VarDecl>(Val: ND) && !isa<FunctionDecl>(Val: ND) &&
24326 !isa<FunctionTemplateDecl>(Val: ND)) {
24327 Diag(Loc: Id.getLoc(), DiagID: diag::err_omp_invalid_target_decl) << Id.getName();
24328 return nullptr;
24329 }
24330 return ND;
24331}
24332
24333void SemaOpenMP::ActOnOpenMPDeclareTargetName(
24334 NamedDecl *ND, SourceLocation Loc, OMPDeclareTargetDeclAttr::MapTypeTy MT,
24335 DeclareTargetContextInfo &DTCI) {
24336 assert((isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
24337 isa<FunctionTemplateDecl>(ND)) &&
24338 "Expected variable, function or function template.");
24339
24340 if (auto *VD = dyn_cast<VarDecl>(Val: ND)) {
24341 // Only global variables can be marked as declare target.
24342 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
24343 !VD->isStaticDataMember()) {
24344 Diag(Loc, DiagID: diag::err_omp_declare_target_has_local_vars)
24345 << VD->getNameAsString();
24346 return;
24347 }
24348 }
24349 // Diagnose marking after use as it may lead to incorrect diagnosis and
24350 // codegen.
24351 if (getLangOpts().OpenMP >= 50 &&
24352 (ND->isUsed(/*CheckUsedAttr=*/false) || ND->isReferenced()))
24353 Diag(Loc, DiagID: diag::warn_omp_declare_target_after_first_use);
24354
24355 // Report affected OpenMP target offloading behavior when in HIP lang-mode.
24356 if (getLangOpts().HIP)
24357 Diag(Loc, DiagID: diag::warn_hip_omp_target_directives);
24358
24359 // Explicit declare target lists have precedence.
24360 const unsigned Level = -1;
24361
24362 auto *VD = cast<ValueDecl>(Val: ND);
24363 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
24364 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
24365 if (ActiveAttr && (*ActiveAttr)->getDevType() != DTCI.DT &&
24366 (*ActiveAttr)->getLevel() == Level) {
24367 Diag(Loc, DiagID: diag::err_omp_device_type_mismatch)
24368 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(Val: DTCI.DT)
24369 << OMPDeclareTargetDeclAttr::ConvertDevTypeTyToStr(
24370 Val: (*ActiveAttr)->getDevType());
24371 return;
24372 }
24373 if (ActiveAttr && (*ActiveAttr)->getMapType() != MT &&
24374 (*ActiveAttr)->getLevel() == Level) {
24375 Diag(Loc, DiagID: diag::err_omp_declare_target_to_and_link) << ND;
24376 return;
24377 }
24378
24379 if (ActiveAttr && (*ActiveAttr)->getLevel() == Level)
24380 return;
24381
24382 Expr *IndirectE = nullptr;
24383 bool IsIndirect = false;
24384 if (DTCI.Indirect) {
24385 IndirectE = *DTCI.Indirect;
24386 if (!IndirectE)
24387 IsIndirect = true;
24388 }
24389 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
24390 Ctx&: getASTContext(), MapType: MT, DevType: DTCI.DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
24391 Range: SourceRange(Loc, Loc));
24392 ND->addAttr(A);
24393 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
24394 ML->DeclarationMarkedOpenMPDeclareTarget(D: ND, Attr: A);
24395 checkDeclIsAllowedInOpenMPTarget(E: nullptr, D: ND, IdLoc: Loc);
24396 if (auto *VD = dyn_cast<VarDecl>(Val: ND);
24397 getLangOpts().OpenMP && VD && VD->hasAttr<OMPDeclareTargetDeclAttr>() &&
24398 VD->hasGlobalStorage())
24399 ActOnOpenMPDeclareTargetInitializer(D: ND);
24400}
24401
24402static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
24403 Sema &SemaRef, Decl *D) {
24404 if (!D || !isa<VarDecl>(Val: D))
24405 return;
24406 auto *VD = cast<VarDecl>(Val: D);
24407 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> MapTy =
24408 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
24409 if (SemaRef.LangOpts.OpenMP >= 50 &&
24410 (SemaRef.getCurLambda(/*IgnoreNonLambdaCapturingScope=*/true) ||
24411 SemaRef.getCurBlock() || SemaRef.getCurCapturedRegion()) &&
24412 VD->hasGlobalStorage()) {
24413 if (!MapTy || (*MapTy != OMPDeclareTargetDeclAttr::MT_To &&
24414 *MapTy != OMPDeclareTargetDeclAttr::MT_Enter)) {
24415 // OpenMP 5.0, 2.12.7 declare target Directive, Restrictions
24416 // If a lambda declaration and definition appears between a
24417 // declare target directive and the matching end declare target
24418 // directive, all variables that are captured by the lambda
24419 // expression must also appear in a to clause.
24420 SemaRef.Diag(Loc: VD->getLocation(),
24421 DiagID: diag::err_omp_lambda_capture_in_declare_target_not_to);
24422 SemaRef.Diag(Loc: SL, DiagID: diag::note_var_explicitly_captured_here)
24423 << VD << 0 << SR;
24424 return;
24425 }
24426 }
24427 if (MapTy)
24428 return;
24429 SemaRef.Diag(Loc: VD->getLocation(), DiagID: diag::warn_omp_not_in_target_context);
24430 SemaRef.Diag(Loc: SL, DiagID: diag::note_used_here) << SR;
24431}
24432
24433static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
24434 Sema &SemaRef, DSAStackTy *Stack,
24435 ValueDecl *VD) {
24436 return OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) ||
24437 checkTypeMappable(SL, SR, SemaRef, Stack, QTy: VD->getType(),
24438 /*FullCheck=*/false);
24439}
24440
24441void SemaOpenMP::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
24442 SourceLocation IdLoc) {
24443 if (!D || D->isInvalidDecl())
24444 return;
24445 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
24446 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
24447 if (auto *VD = dyn_cast<VarDecl>(Val: D)) {
24448 // Only global variables can be marked as declare target.
24449 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
24450 !VD->isStaticDataMember())
24451 return;
24452 // 2.10.6: threadprivate variable cannot appear in a declare target
24453 // directive.
24454 if (DSAStack->isThreadPrivate(D: VD)) {
24455 Diag(Loc: SL, DiagID: diag::err_omp_threadprivate_in_target);
24456 reportOriginalDsa(SemaRef, DSAStack, D: VD, DSAStack->getTopDSA(D: VD, FromParent: false));
24457 return;
24458 }
24459 }
24460 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(Val: D))
24461 D = FTD->getTemplatedDecl();
24462 if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) {
24463 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
24464 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: FD);
24465 if (IdLoc.isValid() && Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
24466 Diag(Loc: IdLoc, DiagID: diag::err_omp_function_in_link_clause);
24467 Diag(Loc: FD->getLocation(), DiagID: diag::note_defined_here) << FD;
24468 return;
24469 }
24470 }
24471 if (auto *VD = dyn_cast<ValueDecl>(Val: D)) {
24472 // Problem if any with var declared with incomplete type will be reported
24473 // as normal, so no need to check it here.
24474 if ((E || !VD->getType()->isIncompleteType()) &&
24475 !checkValueDeclInTarget(SL, SR, SemaRef, DSAStack, VD))
24476 return;
24477 if (!E && isInOpenMPDeclareTargetContext()) {
24478 // Checking declaration inside declare target region.
24479 if (isa<VarDecl>(Val: D) || isa<FunctionDecl>(Val: D) ||
24480 isa<FunctionTemplateDecl>(Val: D)) {
24481 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
24482 OMPDeclareTargetDeclAttr::getActiveAttr(VD);
24483 unsigned Level = DeclareTargetNesting.size();
24484 if (ActiveAttr && (*ActiveAttr)->getLevel() >= Level)
24485 return;
24486 DeclareTargetContextInfo &DTCI = DeclareTargetNesting.back();
24487 Expr *IndirectE = nullptr;
24488 bool IsIndirect = false;
24489 if (DTCI.Indirect) {
24490 IndirectE = *DTCI.Indirect;
24491 if (!IndirectE)
24492 IsIndirect = true;
24493 }
24494 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
24495 Ctx&: getASTContext(),
24496 MapType: getLangOpts().OpenMP >= 52 ? OMPDeclareTargetDeclAttr::MT_Enter
24497 : OMPDeclareTargetDeclAttr::MT_To,
24498 DevType: DTCI.DT, IndirectExpr: IndirectE, Indirect: IsIndirect, Level,
24499 Range: SourceRange(DTCI.Loc, DTCI.Loc));
24500 D->addAttr(A);
24501 if (ASTMutationListener *ML = getASTContext().getASTMutationListener())
24502 ML->DeclarationMarkedOpenMPDeclareTarget(D, Attr: A);
24503 }
24504 return;
24505 }
24506 }
24507 if (!E)
24508 return;
24509 checkDeclInTargetContext(SL: E->getExprLoc(), SR: E->getSourceRange(), SemaRef, D);
24510}
24511
24512/// This class visits every VarDecl that the initializer references and adds
24513/// OMPDeclareTargetDeclAttr to each of them.
24514class GlobalDeclRefChecker final : public StmtVisitor<GlobalDeclRefChecker> {
24515 SmallVector<VarDecl *> DeclVector;
24516 Attr *A;
24517
24518public:
24519 /// A StmtVisitor class function that visits all DeclRefExpr and adds
24520 /// OMPDeclareTargetDeclAttr to them.
24521 void VisitDeclRefExpr(DeclRefExpr *Node) {
24522 if (auto *VD = dyn_cast<VarDecl>(Val: Node->getDecl())) {
24523 VD->addAttr(A);
24524 DeclVector.push_back(Elt: VD);
24525 }
24526 }
24527 /// A function that iterates across each of the Expr's children.
24528 void VisitExpr(Expr *Ex) {
24529 for (auto *Child : Ex->children()) {
24530 Visit(S: Child);
24531 }
24532 }
24533 /// A function that keeps a record of all the Decls that are variables, has
24534 /// OMPDeclareTargetDeclAttr, and has global storage in the DeclVector. Pop
24535 /// each Decl one at a time and use the inherited 'visit' functions to look
24536 /// for DeclRefExpr.
24537 void declareTargetInitializer(Decl *TD) {
24538 A = TD->getAttr<OMPDeclareTargetDeclAttr>();
24539 DeclVector.push_back(Elt: cast<VarDecl>(Val: TD));
24540 llvm::SmallDenseSet<Decl *> Visited;
24541 while (!DeclVector.empty()) {
24542 VarDecl *TargetVarDecl = DeclVector.pop_back_val();
24543 if (!Visited.insert(V: TargetVarDecl).second)
24544 continue;
24545
24546 if (TargetVarDecl->hasAttr<OMPDeclareTargetDeclAttr>() &&
24547 TargetVarDecl->hasInit() && TargetVarDecl->hasGlobalStorage()) {
24548 if (Expr *Ex = TargetVarDecl->getInit())
24549 Visit(S: Ex);
24550 }
24551 }
24552 }
24553};
24554
24555/// Adding OMPDeclareTargetDeclAttr to variables with static storage
24556/// duration that are referenced in the initializer expression list of
24557/// variables with static storage duration in declare target directive.
24558void SemaOpenMP::ActOnOpenMPDeclareTargetInitializer(Decl *TargetDecl) {
24559 GlobalDeclRefChecker Checker;
24560 if (isa<VarDecl>(Val: TargetDecl))
24561 Checker.declareTargetInitializer(TD: TargetDecl);
24562}
24563
24564OMPClause *SemaOpenMP::ActOnOpenMPToClause(
24565 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
24566 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
24567 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
24568 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
24569 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
24570 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
24571 OMPC_MOTION_MODIFIER_unknown,
24572 OMPC_MOTION_MODIFIER_unknown};
24573 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
24574
24575 // Process motion-modifiers, flag errors for duplicate modifiers.
24576 unsigned Count = 0;
24577 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
24578 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
24579 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
24580 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
24581 continue;
24582 }
24583 assert(Count < NumberOfOMPMotionModifiers &&
24584 "Modifiers exceed the allowed number of motion modifiers");
24585 Modifiers[Count] = MotionModifiers[I];
24586 ModifiersLoc[Count] = MotionModifiersLoc[I];
24587 ++Count;
24588 }
24589
24590 MappableVarListInfo MVLI(VarList);
24591 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_to, MVLI, StartLoc: Locs.StartLoc,
24592 MapperIdScopeSpec, MapperId, UnresolvedMappers);
24593 if (MVLI.ProcessedVarList.empty())
24594 return nullptr;
24595 if (IteratorExpr)
24596 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
24597 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
24598 DSAStack->addIteratorVarDecl(VD);
24599 return OMPToClause::Create(
24600 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24601 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorModifier: IteratorExpr, MotionModifiers: Modifiers,
24602 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
24603 MapperId);
24604}
24605
24606OMPClause *SemaOpenMP::ActOnOpenMPFromClause(
24607 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
24608 ArrayRef<SourceLocation> MotionModifiersLoc, Expr *IteratorExpr,
24609 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
24610 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
24611 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
24612 OpenMPMotionModifierKind Modifiers[] = {OMPC_MOTION_MODIFIER_unknown,
24613 OMPC_MOTION_MODIFIER_unknown,
24614 OMPC_MOTION_MODIFIER_unknown};
24615 SourceLocation ModifiersLoc[NumberOfOMPMotionModifiers];
24616
24617 // Process motion-modifiers, flag errors for duplicate modifiers.
24618 unsigned Count = 0;
24619 for (unsigned I = 0, E = MotionModifiers.size(); I < E; ++I) {
24620 if (MotionModifiers[I] != OMPC_MOTION_MODIFIER_unknown &&
24621 llvm::is_contained(Range&: Modifiers, Element: MotionModifiers[I])) {
24622 Diag(Loc: MotionModifiersLoc[I], DiagID: diag::err_omp_duplicate_motion_modifier);
24623 continue;
24624 }
24625 assert(Count < NumberOfOMPMotionModifiers &&
24626 "Modifiers exceed the allowed number of motion modifiers");
24627 Modifiers[Count] = MotionModifiers[I];
24628 ModifiersLoc[Count] = MotionModifiersLoc[I];
24629 ++Count;
24630 }
24631
24632 MappableVarListInfo MVLI(VarList);
24633 checkMappableExpressionList(SemaRef, DSAStack, CKind: OMPC_from, MVLI, StartLoc: Locs.StartLoc,
24634 MapperIdScopeSpec, MapperId, UnresolvedMappers);
24635 if (MVLI.ProcessedVarList.empty())
24636 return nullptr;
24637 if (IteratorExpr)
24638 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: IteratorExpr))
24639 if (auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl()))
24640 DSAStack->addIteratorVarDecl(VD);
24641 return OMPFromClause::Create(
24642 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24643 ComponentLists: MVLI.VarComponents, UDMapperRefs: MVLI.UDMapperList, IteratorExpr, MotionModifiers: Modifiers,
24644 MotionModifiersLoc: ModifiersLoc, UDMQualifierLoc: MapperIdScopeSpec.getWithLocInContext(Context&: getASTContext()),
24645 MapperId);
24646}
24647
24648OMPClause *SemaOpenMP::ActOnOpenMPUseDevicePtrClause(
24649 ArrayRef<Expr *> VarList, const OMPVarListLocTy &Locs,
24650 OpenMPUseDevicePtrFallbackModifier FallbackModifier,
24651 SourceLocation FallbackModifierLoc) {
24652 MappableVarListInfo MVLI(VarList);
24653 SmallVector<Expr *, 8> PrivateCopies;
24654 SmallVector<Expr *, 8> Inits;
24655
24656 for (Expr *RefExpr : VarList) {
24657 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
24658 SourceLocation ELoc;
24659 SourceRange ERange;
24660 Expr *SimpleRefExpr = RefExpr;
24661 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
24662 if (Res.second) {
24663 // It will be analyzed later.
24664 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24665 PrivateCopies.push_back(Elt: nullptr);
24666 Inits.push_back(Elt: nullptr);
24667 }
24668 ValueDecl *D = Res.first;
24669 if (!D)
24670 continue;
24671
24672 QualType Type = D->getType();
24673 Type = Type.getNonReferenceType().getUnqualifiedType();
24674
24675 auto *VD = dyn_cast<VarDecl>(Val: D);
24676
24677 // Item should be a pointer or reference to pointer.
24678 if (!Type->isPointerType()) {
24679 Diag(Loc: ELoc, DiagID: diag::err_omp_usedeviceptr_not_a_pointer)
24680 << 0 << RefExpr->getSourceRange();
24681 continue;
24682 }
24683
24684 // Build the private variable and the expression that refers to it.
24685 auto VDPrivate =
24686 buildVarDecl(SemaRef, Loc: ELoc, Type, Name: D->getName(),
24687 Attrs: D->hasAttrs() ? &D->getAttrs() : nullptr,
24688 OrigRef: VD ? cast<DeclRefExpr>(Val: SimpleRefExpr) : nullptr);
24689 if (VDPrivate->isInvalidDecl())
24690 continue;
24691
24692 SemaRef.CurContext->addDecl(D: VDPrivate);
24693 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
24694 S&: SemaRef, D: VDPrivate, Ty: RefExpr->getType().getUnqualifiedType(), Loc: ELoc);
24695
24696 // Add temporary variable to initialize the private copy of the pointer.
24697 VarDecl *VDInit =
24698 buildVarDecl(SemaRef, Loc: RefExpr->getExprLoc(), Type, Name: ".devptr.temp");
24699 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
24700 S&: SemaRef, D: VDInit, Ty: RefExpr->getType(), Loc: RefExpr->getExprLoc());
24701 SemaRef.AddInitializerToDecl(
24702 dcl: VDPrivate, init: SemaRef.DefaultLvalueConversion(E: VDInitRefExpr).get(),
24703 /*DirectInit=*/false);
24704
24705 // If required, build a capture to implement the privatization initialized
24706 // with the current list item value.
24707 DeclRefExpr *Ref = nullptr;
24708 if (!VD)
24709 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
24710 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
24711 PrivateCopies.push_back(Elt: VDPrivateRefExpr);
24712 Inits.push_back(Elt: VDInitRefExpr);
24713
24714 // We need to add a data sharing attribute for this variable to make sure it
24715 // is correctly captured. A variable that shows up in a use_device_ptr has
24716 // similar properties of a first private variable.
24717 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
24718
24719 // Create a mappable component for the list item. List items in this clause
24720 // only need a component.
24721 MVLI.VarBaseDeclarations.push_back(Elt: D);
24722 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
24723 MVLI.VarComponents.back().emplace_back(Args&: SimpleRefExpr, Args&: D,
24724 /*IsNonContiguous=*/Args: false);
24725 }
24726
24727 if (MVLI.ProcessedVarList.empty())
24728 return nullptr;
24729
24730 return OMPUseDevicePtrClause::Create(
24731 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, PrivateVars: PrivateCopies, Inits,
24732 Declarations: MVLI.VarBaseDeclarations, ComponentLists: MVLI.VarComponents, FallbackModifier,
24733 FallbackModifierLoc);
24734}
24735
24736OMPClause *
24737SemaOpenMP::ActOnOpenMPUseDeviceAddrClause(ArrayRef<Expr *> VarList,
24738 const OMPVarListLocTy &Locs) {
24739 MappableVarListInfo MVLI(VarList);
24740
24741 for (Expr *RefExpr : VarList) {
24742 assert(RefExpr && "NULL expr in OpenMP use_device_addr clause.");
24743 SourceLocation ELoc;
24744 SourceRange ERange;
24745 Expr *SimpleRefExpr = RefExpr;
24746 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
24747 /*AllowArraySection=*/true,
24748 /*AllowAssumedSizeArray=*/true);
24749 if (Res.second) {
24750 // It will be analyzed later.
24751 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24752 }
24753 ValueDecl *D = Res.first;
24754 if (!D)
24755 continue;
24756 auto *VD = dyn_cast<VarDecl>(Val: D);
24757
24758 // If required, build a capture to implement the privatization initialized
24759 // with the current list item value.
24760 DeclRefExpr *Ref = nullptr;
24761 if (!VD)
24762 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
24763 MVLI.ProcessedVarList.push_back(Elt: VD ? RefExpr->IgnoreParens() : Ref);
24764
24765 // We need to add a data sharing attribute for this variable to make sure it
24766 // is correctly captured. A variable that shows up in a use_device_addr has
24767 // similar properties of a first private variable.
24768 DSAStack->addDSA(D, E: RefExpr->IgnoreParens(), A: OMPC_firstprivate, PrivateCopy: Ref);
24769
24770 // Use the map-like approach to fully populate VarComponents
24771 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
24772
24773 const Expr *BE = checkMapClauseExpressionBase(
24774 SemaRef, E: RefExpr, CurComponents, CKind: OMPC_use_device_addr,
24775 DSAStack->getCurrentDirective(),
24776 /*NoDiagnose=*/false);
24777
24778 if (!BE)
24779 continue;
24780
24781 assert(!CurComponents.empty() &&
24782 "use_device_addr clause expression with no components!");
24783
24784 // OpenMP use_device_addr: If a list item is an array section, the array
24785 // base must be a base language identifier. We caught the cases where
24786 // the array-section has a base-variable in getPrivateItem. e.g.
24787 // struct S {
24788 // int a[10];
24789 // }; S s1;
24790 // ... use_device_addr(s1.a[0]) // not ok, caught already
24791 //
24792 // But we still neeed to verify that the base-pointer is also a
24793 // base-language identifier, and catch cases like:
24794 // int *pa[10]; *p;
24795 // ... use_device_addr(pa[1][2]) // not ok, base-pointer is pa[1]
24796 // ... use_device_addr(p[1]) // ok
24797 // ... use_device_addr(this->p[1]) // ok
24798 auto AttachPtrResult = OMPClauseMappableExprCommon::findAttachPtrExpr(
24799 Components: CurComponents, DSAStack->getCurrentDirective());
24800 const Expr *AttachPtrExpr = AttachPtrResult.first;
24801
24802 if (AttachPtrExpr) {
24803 const Expr *BaseExpr = AttachPtrExpr->IgnoreParenImpCasts();
24804 bool IsValidBase = false;
24805
24806 if (isa<DeclRefExpr>(Val: BaseExpr))
24807 IsValidBase = true;
24808 else if (const auto *ME = dyn_cast<MemberExpr>(Val: BaseExpr);
24809 ME && isa<CXXThisExpr>(Val: ME->getBase()->IgnoreParenImpCasts()))
24810 IsValidBase = true;
24811
24812 if (!IsValidBase) {
24813 SemaRef.Diag(Loc: ELoc,
24814 DiagID: diag::err_omp_expected_base_pointer_var_name_member_expr)
24815 << (SemaRef.getCurrentThisType().isNull() ? 0 : 1)
24816 << AttachPtrExpr->getSourceRange();
24817 continue;
24818 }
24819 }
24820
24821 // Get the declaration from the components
24822 ValueDecl *CurDeclaration = CurComponents.back().getAssociatedDeclaration();
24823 assert((isa<CXXThisExpr>(BE) || CurDeclaration) &&
24824 "Unexpected null decl for use_device_addr clause.");
24825
24826 MVLI.VarBaseDeclarations.push_back(Elt: CurDeclaration);
24827 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
24828 MVLI.VarComponents.back().append(in_start: CurComponents.begin(),
24829 in_end: CurComponents.end());
24830 }
24831
24832 if (MVLI.ProcessedVarList.empty())
24833 return nullptr;
24834
24835 return OMPUseDeviceAddrClause::Create(
24836 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24837 ComponentLists: MVLI.VarComponents);
24838}
24839
24840OMPClause *
24841SemaOpenMP::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
24842 const OMPVarListLocTy &Locs) {
24843 MappableVarListInfo MVLI(VarList);
24844 for (Expr *RefExpr : VarList) {
24845 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
24846 SourceLocation ELoc;
24847 SourceRange ERange;
24848 Expr *SimpleRefExpr = RefExpr;
24849 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
24850 if (Res.second) {
24851 // It will be analyzed later.
24852 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24853 }
24854 ValueDecl *D = Res.first;
24855 if (!D)
24856 continue;
24857
24858 QualType Type = D->getType();
24859 // item should be a pointer or array or reference to pointer or array
24860 if (!Type.getNonReferenceType()->isPointerType() &&
24861 !Type.getNonReferenceType()->isArrayType()) {
24862 Diag(Loc: ELoc, DiagID: diag::err_omp_argument_type_isdeviceptr)
24863 << 0 << RefExpr->getSourceRange();
24864 continue;
24865 }
24866
24867 // Check if the declaration in the clause does not show up in any data
24868 // sharing attribute.
24869 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
24870 if (isOpenMPPrivate(Kind: DVar.CKind)) {
24871 unsigned OMPVersion = getLangOpts().OpenMP;
24872 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
24873 << getOpenMPClauseNameForDiag(C: DVar.CKind)
24874 << getOpenMPClauseNameForDiag(C: OMPC_is_device_ptr)
24875 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
24876 Ver: OMPVersion);
24877 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
24878 continue;
24879 }
24880
24881 const Expr *ConflictExpr;
24882 if (DSAStack->checkMappableExprComponentListsForDecl(
24883 VD: D, /*CurrentRegionOnly=*/true,
24884 Check: [&ConflictExpr](
24885 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
24886 OpenMPClauseKind) -> bool {
24887 ConflictExpr = R.front().getAssociatedExpression();
24888 return true;
24889 })) {
24890 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
24891 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
24892 << ConflictExpr->getSourceRange();
24893 continue;
24894 }
24895
24896 // Store the components in the stack so that they can be used to check
24897 // against other clauses later on.
24898 OMPClauseMappableExprCommon::MappableComponent MC(
24899 SimpleRefExpr, D, /*IsNonContiguous=*/false);
24900 DSAStack->addMappableExpressionComponents(
24901 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
24902
24903 // Record the expression we've just processed.
24904 MVLI.ProcessedVarList.push_back(Elt: SimpleRefExpr);
24905
24906 // Create a mappable component for the list item. List items in this clause
24907 // only need a component. We use a null declaration to signal fields in
24908 // 'this'.
24909 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
24910 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
24911 "Unexpected device pointer expression!");
24912 MVLI.VarBaseDeclarations.push_back(
24913 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
24914 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
24915 MVLI.VarComponents.back().push_back(Elt: MC);
24916 }
24917
24918 if (MVLI.ProcessedVarList.empty())
24919 return nullptr;
24920
24921 return OMPIsDevicePtrClause::Create(
24922 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
24923 ComponentLists: MVLI.VarComponents);
24924}
24925
24926OMPClause *
24927SemaOpenMP::ActOnOpenMPHasDeviceAddrClause(ArrayRef<Expr *> VarList,
24928 const OMPVarListLocTy &Locs) {
24929 MappableVarListInfo MVLI(VarList);
24930 for (Expr *RefExpr : VarList) {
24931 assert(RefExpr && "NULL expr in OpenMP has_device_addr clause.");
24932 SourceLocation ELoc;
24933 SourceRange ERange;
24934 Expr *SimpleRefExpr = RefExpr;
24935 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
24936 /*AllowArraySection=*/true);
24937 if (Res.second) {
24938 // It will be analyzed later.
24939 MVLI.ProcessedVarList.push_back(Elt: RefExpr);
24940 }
24941 ValueDecl *D = Res.first;
24942 if (!D)
24943 continue;
24944
24945 // Check if the declaration in the clause does not show up in any data
24946 // sharing attribute.
24947 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
24948 if (isOpenMPPrivate(Kind: DVar.CKind)) {
24949 unsigned OMPVersion = getLangOpts().OpenMP;
24950 Diag(Loc: ELoc, DiagID: diag::err_omp_variable_in_given_clause_and_dsa)
24951 << getOpenMPClauseNameForDiag(C: DVar.CKind)
24952 << getOpenMPClauseNameForDiag(C: OMPC_has_device_addr)
24953 << getOpenMPDirectiveName(DSAStack->getCurrentDirective(),
24954 Ver: OMPVersion);
24955 reportOriginalDsa(SemaRef, DSAStack, D, DVar);
24956 continue;
24957 }
24958
24959 const Expr *ConflictExpr;
24960 if (DSAStack->checkMappableExprComponentListsForDecl(
24961 VD: D, /*CurrentRegionOnly=*/true,
24962 Check: [&ConflictExpr](
24963 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
24964 OpenMPClauseKind) -> bool {
24965 ConflictExpr = R.front().getAssociatedExpression();
24966 return true;
24967 })) {
24968 Diag(Loc: ELoc, DiagID: diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
24969 Diag(Loc: ConflictExpr->getExprLoc(), DiagID: diag::note_used_here)
24970 << ConflictExpr->getSourceRange();
24971 continue;
24972 }
24973
24974 // Store the components in the stack so that they can be used to check
24975 // against other clauses later on.
24976 Expr *Component = SimpleRefExpr;
24977 auto *VD = dyn_cast<VarDecl>(Val: D);
24978 if (VD && (isa<ArraySectionExpr>(Val: RefExpr->IgnoreParenImpCasts()) ||
24979 isa<ArraySubscriptExpr>(Val: RefExpr->IgnoreParenImpCasts())))
24980 Component =
24981 SemaRef.DefaultFunctionArrayLvalueConversion(E: SimpleRefExpr).get();
24982 OMPClauseMappableExprCommon::MappableComponent MC(
24983 Component, D, /*IsNonContiguous=*/false);
24984 DSAStack->addMappableExpressionComponents(
24985 VD: D, Components: MC, /*WhereFoundClauseKind=*/OMPC_has_device_addr);
24986
24987 // Record the expression we've just processed.
24988 if (!VD && !SemaRef.CurContext->isDependentContext()) {
24989 DeclRefExpr *Ref =
24990 buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/true);
24991 assert(Ref && "has_device_addr capture failed");
24992 MVLI.ProcessedVarList.push_back(Elt: Ref);
24993 } else
24994 MVLI.ProcessedVarList.push_back(Elt: RefExpr->IgnoreParens());
24995
24996 // Create a mappable component for the list item. List items in this clause
24997 // only need a component. We use a null declaration to signal fields in
24998 // 'this'.
24999 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
25000 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
25001 "Unexpected device pointer expression!");
25002 MVLI.VarBaseDeclarations.push_back(
25003 Elt: isa<DeclRefExpr>(Val: SimpleRefExpr) ? D : nullptr);
25004 MVLI.VarComponents.resize(N: MVLI.VarComponents.size() + 1);
25005 MVLI.VarComponents.back().push_back(Elt: MC);
25006 }
25007
25008 if (MVLI.ProcessedVarList.empty())
25009 return nullptr;
25010
25011 return OMPHasDeviceAddrClause::Create(
25012 C: getASTContext(), Locs, Vars: MVLI.ProcessedVarList, Declarations: MVLI.VarBaseDeclarations,
25013 ComponentLists: MVLI.VarComponents);
25014}
25015
25016OMPClause *SemaOpenMP::ActOnOpenMPAllocateClause(
25017 Expr *Allocator, Expr *Alignment,
25018 OpenMPAllocateClauseModifier FirstAllocateModifier,
25019 SourceLocation FirstAllocateModifierLoc,
25020 OpenMPAllocateClauseModifier SecondAllocateModifier,
25021 SourceLocation SecondAllocateModifierLoc, ArrayRef<Expr *> VarList,
25022 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
25023 SourceLocation EndLoc) {
25024 if (Allocator) {
25025 // Allocator expression is dependent - skip it for now and build the
25026 // allocator when instantiated.
25027 bool AllocDependent =
25028 (Allocator->isTypeDependent() || Allocator->isValueDependent() ||
25029 Allocator->isInstantiationDependent() ||
25030 Allocator->containsUnexpandedParameterPack());
25031 if (!AllocDependent) {
25032 // OpenMP [2.11.4 allocate Clause, Description]
25033 // allocator is an expression of omp_allocator_handle_t type.
25034 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: Allocator->getExprLoc(), DSAStack))
25035 return nullptr;
25036
25037 ExprResult AllocatorRes = SemaRef.DefaultLvalueConversion(E: Allocator);
25038 if (AllocatorRes.isInvalid())
25039 return nullptr;
25040 AllocatorRes = SemaRef.PerformImplicitConversion(
25041 From: AllocatorRes.get(), DSAStack->getOMPAllocatorHandleT(),
25042 Action: AssignmentAction::Initializing,
25043 /*AllowExplicit=*/true);
25044 if (AllocatorRes.isInvalid())
25045 return nullptr;
25046 Allocator = AllocatorRes.isUsable() ? AllocatorRes.get() : nullptr;
25047 }
25048 } else {
25049 // OpenMP 5.0, 2.11.4 allocate Clause, Restrictions.
25050 // allocate clauses that appear on a target construct or on constructs in a
25051 // target region must specify an allocator expression unless a requires
25052 // directive with the dynamic_allocators clause is present in the same
25053 // compilation unit.
25054 if (getLangOpts().OpenMPIsTargetDevice &&
25055 !DSAStack->hasRequiresDeclWithClause<OMPDynamicAllocatorsClause>())
25056 SemaRef.targetDiag(Loc: StartLoc, DiagID: diag::err_expected_allocator_expression);
25057 }
25058 if (Alignment) {
25059 bool AlignmentDependent = Alignment->isTypeDependent() ||
25060 Alignment->isValueDependent() ||
25061 Alignment->isInstantiationDependent() ||
25062 Alignment->containsUnexpandedParameterPack();
25063 if (!AlignmentDependent) {
25064 ExprResult AlignResult =
25065 VerifyPositiveIntegerConstantInClause(E: Alignment, CKind: OMPC_allocate);
25066 Alignment = AlignResult.isUsable() ? AlignResult.get() : nullptr;
25067 }
25068 }
25069 // Analyze and build list of variables.
25070 SmallVector<Expr *, 8> Vars;
25071 for (Expr *RefExpr : VarList) {
25072 assert(RefExpr && "NULL expr in OpenMP allocate clause.");
25073 SourceLocation ELoc;
25074 SourceRange ERange;
25075 Expr *SimpleRefExpr = RefExpr;
25076 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
25077 if (Res.second) {
25078 // It will be analyzed later.
25079 Vars.push_back(Elt: RefExpr);
25080 }
25081 ValueDecl *D = Res.first;
25082 if (!D)
25083 continue;
25084
25085 auto *VD = dyn_cast<VarDecl>(Val: D);
25086 DeclRefExpr *Ref = nullptr;
25087 if (!VD && !SemaRef.CurContext->isDependentContext())
25088 Ref = buildCapture(S&: SemaRef, D, CaptureExpr: SimpleRefExpr, /*WithInit=*/false);
25089 Vars.push_back(Elt: (VD || SemaRef.CurContext->isDependentContext())
25090 ? RefExpr->IgnoreParens()
25091 : Ref);
25092 }
25093
25094 if (Vars.empty())
25095 return nullptr;
25096
25097 if (Allocator)
25098 DSAStack->addInnerAllocatorExpr(E: Allocator);
25099
25100 return OMPAllocateClause::Create(
25101 C: getASTContext(), StartLoc, LParenLoc, Allocator, Alignment, ColonLoc,
25102 Modifier1: FirstAllocateModifier, Modifier1Loc: FirstAllocateModifierLoc, Modifier2: SecondAllocateModifier,
25103 Modifier2Loc: SecondAllocateModifierLoc, EndLoc, VL: Vars);
25104}
25105
25106OMPClause *SemaOpenMP::ActOnOpenMPNontemporalClause(ArrayRef<Expr *> VarList,
25107 SourceLocation StartLoc,
25108 SourceLocation LParenLoc,
25109 SourceLocation EndLoc) {
25110 SmallVector<Expr *, 8> Vars;
25111 for (Expr *RefExpr : VarList) {
25112 assert(RefExpr && "NULL expr in OpenMP nontemporal clause.");
25113 SourceLocation ELoc;
25114 SourceRange ERange;
25115 Expr *SimpleRefExpr = RefExpr;
25116 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange);
25117 if (Res.second)
25118 // It will be analyzed later.
25119 Vars.push_back(Elt: RefExpr);
25120 ValueDecl *D = Res.first;
25121 if (!D)
25122 continue;
25123
25124 // OpenMP 5.0, 2.9.3.1 simd Construct, Restrictions.
25125 // A list-item cannot appear in more than one nontemporal clause.
25126 if (const Expr *PrevRef =
25127 DSAStack->addUniqueNontemporal(D, NewDE: SimpleRefExpr)) {
25128 Diag(Loc: ELoc, DiagID: diag::err_omp_used_in_clause_twice)
25129 << 0 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal) << ERange;
25130 Diag(Loc: PrevRef->getExprLoc(), DiagID: diag::note_omp_explicit_dsa)
25131 << getOpenMPClauseNameForDiag(C: OMPC_nontemporal);
25132 continue;
25133 }
25134
25135 Vars.push_back(Elt: RefExpr);
25136 }
25137
25138 if (Vars.empty())
25139 return nullptr;
25140
25141 return OMPNontemporalClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25142 EndLoc, VL: Vars);
25143}
25144
25145StmtResult SemaOpenMP::ActOnOpenMPScopeDirective(ArrayRef<OMPClause *> Clauses,
25146 Stmt *AStmt,
25147 SourceLocation StartLoc,
25148 SourceLocation EndLoc) {
25149 if (!AStmt)
25150 return StmtError();
25151
25152 SemaRef.setFunctionHasBranchProtectedScope();
25153
25154 return OMPScopeDirective::Create(C: getASTContext(), StartLoc, EndLoc, Clauses,
25155 AssociatedStmt: AStmt);
25156}
25157
25158OMPClause *SemaOpenMP::ActOnOpenMPInclusiveClause(ArrayRef<Expr *> VarList,
25159 SourceLocation StartLoc,
25160 SourceLocation LParenLoc,
25161 SourceLocation EndLoc) {
25162 SmallVector<Expr *, 8> Vars;
25163 for (Expr *RefExpr : VarList) {
25164 assert(RefExpr && "NULL expr in OpenMP inclusive clause.");
25165 SourceLocation ELoc;
25166 SourceRange ERange;
25167 Expr *SimpleRefExpr = RefExpr;
25168 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25169 /*AllowArraySection=*/true);
25170 if (Res.second)
25171 // It will be analyzed later.
25172 Vars.push_back(Elt: RefExpr);
25173 ValueDecl *D = Res.first;
25174 if (!D)
25175 continue;
25176
25177 const DSAStackTy::DSAVarData DVar =
25178 DSAStack->getTopDSA(D, /*FromParent=*/true);
25179 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
25180 // A list item that appears in the inclusive or exclusive clause must appear
25181 // in a reduction clause with the inscan modifier on the enclosing
25182 // worksharing-loop, worksharing-loop SIMD, or simd construct.
25183 if (DVar.CKind != OMPC_reduction || DVar.Modifier != OMPC_REDUCTION_inscan)
25184 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
25185 << RefExpr->getSourceRange();
25186
25187 if (DSAStack->getParentDirective() != OMPD_unknown)
25188 DSAStack->markDeclAsUsedInScanDirective(D);
25189 Vars.push_back(Elt: RefExpr);
25190 }
25191
25192 if (Vars.empty())
25193 return nullptr;
25194
25195 return OMPInclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25196 EndLoc, VL: Vars);
25197}
25198
25199OMPClause *SemaOpenMP::ActOnOpenMPExclusiveClause(ArrayRef<Expr *> VarList,
25200 SourceLocation StartLoc,
25201 SourceLocation LParenLoc,
25202 SourceLocation EndLoc) {
25203 SmallVector<Expr *, 8> Vars;
25204 for (Expr *RefExpr : VarList) {
25205 assert(RefExpr && "NULL expr in OpenMP exclusive clause.");
25206 SourceLocation ELoc;
25207 SourceRange ERange;
25208 Expr *SimpleRefExpr = RefExpr;
25209 auto Res = getPrivateItem(S&: SemaRef, RefExpr&: SimpleRefExpr, ELoc, ERange,
25210 /*AllowArraySection=*/true);
25211 if (Res.second)
25212 // It will be analyzed later.
25213 Vars.push_back(Elt: RefExpr);
25214 ValueDecl *D = Res.first;
25215 if (!D)
25216 continue;
25217
25218 OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
25219 DSAStackTy::DSAVarData DVar;
25220 if (ParentDirective != OMPD_unknown)
25221 DVar = DSAStack->getTopDSA(D, /*FromParent=*/true);
25222 // OpenMP 5.0, 2.9.6, scan Directive, Restrictions.
25223 // A list item that appears in the inclusive or exclusive clause must appear
25224 // in a reduction clause with the inscan modifier on the enclosing
25225 // worksharing-loop, worksharing-loop SIMD, or simd construct.
25226 if (ParentDirective == OMPD_unknown || DVar.CKind != OMPC_reduction ||
25227 DVar.Modifier != OMPC_REDUCTION_inscan) {
25228 Diag(Loc: ELoc, DiagID: diag::err_omp_inclusive_exclusive_not_reduction)
25229 << RefExpr->getSourceRange();
25230 } else {
25231 DSAStack->markDeclAsUsedInScanDirective(D);
25232 }
25233 Vars.push_back(Elt: RefExpr);
25234 }
25235
25236 if (Vars.empty())
25237 return nullptr;
25238
25239 return OMPExclusiveClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25240 EndLoc, VL: Vars);
25241}
25242
25243/// Tries to find omp_alloctrait_t type.
25244static bool findOMPAlloctraitT(Sema &S, SourceLocation Loc, DSAStackTy *Stack) {
25245 QualType OMPAlloctraitT = Stack->getOMPAlloctraitT();
25246 if (!OMPAlloctraitT.isNull())
25247 return true;
25248 IdentifierInfo &II = S.PP.getIdentifierTable().get(Name: "omp_alloctrait_t");
25249 ParsedType PT = S.getTypeName(II, NameLoc: Loc, S: S.getCurScope());
25250 if (!PT.getAsOpaquePtr() || PT.get().isNull()) {
25251 S.Diag(Loc, DiagID: diag::err_omp_implied_type_not_found) << "omp_alloctrait_t";
25252 return false;
25253 }
25254 Stack->setOMPAlloctraitT(PT.get());
25255 return true;
25256}
25257
25258OMPClause *SemaOpenMP::ActOnOpenMPUsesAllocatorClause(
25259 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc,
25260 ArrayRef<UsesAllocatorsData> Data) {
25261 ASTContext &Context = getASTContext();
25262 // OpenMP [2.12.5, target Construct]
25263 // allocator is an identifier of omp_allocator_handle_t type.
25264 if (!findOMPAllocatorHandleT(S&: SemaRef, Loc: StartLoc, DSAStack))
25265 return nullptr;
25266 // OpenMP [2.12.5, target Construct]
25267 // allocator-traits-array is an identifier of const omp_alloctrait_t * type.
25268 if (llvm::any_of(
25269 Range&: Data,
25270 P: [](const UsesAllocatorsData &D) { return D.AllocatorTraits; }) &&
25271 !findOMPAlloctraitT(S&: SemaRef, Loc: StartLoc, DSAStack))
25272 return nullptr;
25273 llvm::SmallPtrSet<CanonicalDeclPtr<Decl>, 4> PredefinedAllocators;
25274 for (int I = 0; I < OMPAllocateDeclAttr::OMPUserDefinedMemAlloc; ++I) {
25275 auto AllocatorKind = static_cast<OMPAllocateDeclAttr::AllocatorTypeTy>(I);
25276 StringRef Allocator =
25277 OMPAllocateDeclAttr::ConvertAllocatorTypeTyToStr(Val: AllocatorKind);
25278 DeclarationName AllocatorName = &Context.Idents.get(Name: Allocator);
25279 PredefinedAllocators.insert(Ptr: SemaRef.LookupSingleName(
25280 S: SemaRef.TUScope, Name: AllocatorName, Loc: StartLoc, NameKind: Sema::LookupAnyName));
25281 }
25282
25283 SmallVector<OMPUsesAllocatorsClause::Data, 4> NewData;
25284 for (const UsesAllocatorsData &D : Data) {
25285 Expr *AllocatorExpr = nullptr;
25286 // Check allocator expression.
25287 if (D.Allocator->isTypeDependent()) {
25288 AllocatorExpr = D.Allocator;
25289 } else {
25290 // Traits were specified - need to assign new allocator to the specified
25291 // allocator, so it must be an lvalue.
25292 AllocatorExpr = D.Allocator->IgnoreParenImpCasts();
25293 auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorExpr);
25294 bool IsPredefinedAllocator = false;
25295 if (DRE) {
25296 OMPAllocateDeclAttr::AllocatorTypeTy AllocatorTy =
25297 getAllocatorKind(S&: SemaRef, DSAStack, Allocator: AllocatorExpr);
25298 IsPredefinedAllocator =
25299 AllocatorTy !=
25300 OMPAllocateDeclAttr::AllocatorTypeTy::OMPUserDefinedMemAlloc;
25301 }
25302 QualType OMPAllocatorHandleT = DSAStack->getOMPAllocatorHandleT();
25303 QualType AllocatorExprType = AllocatorExpr->getType();
25304 bool IsTypeCompatible = IsPredefinedAllocator;
25305 IsTypeCompatible = IsTypeCompatible ||
25306 Context.hasSameUnqualifiedType(T1: AllocatorExprType,
25307 T2: OMPAllocatorHandleT);
25308 IsTypeCompatible =
25309 IsTypeCompatible ||
25310 Context.typesAreCompatible(T1: AllocatorExprType, T2: OMPAllocatorHandleT);
25311 bool IsNonConstantLValue =
25312 !AllocatorExprType.isConstant(Ctx: Context) && AllocatorExpr->isLValue();
25313 if (!DRE || !IsTypeCompatible ||
25314 (!IsPredefinedAllocator && !IsNonConstantLValue)) {
25315 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::err_omp_var_expected)
25316 << "omp_allocator_handle_t" << (DRE ? 1 : 0)
25317 << AllocatorExpr->getType() << D.Allocator->getSourceRange();
25318 continue;
25319 }
25320 // OpenMP [2.12.5, target Construct]
25321 // Predefined allocators appearing in a uses_allocators clause cannot have
25322 // traits specified.
25323 if (IsPredefinedAllocator && D.AllocatorTraits) {
25324 Diag(Loc: D.AllocatorTraits->getExprLoc(),
25325 DiagID: diag::err_omp_predefined_allocator_with_traits)
25326 << D.AllocatorTraits->getSourceRange();
25327 Diag(Loc: D.Allocator->getExprLoc(), DiagID: diag::note_omp_predefined_allocator)
25328 << cast<NamedDecl>(Val: DRE->getDecl())->getName()
25329 << D.Allocator->getSourceRange();
25330 continue;
25331 }
25332 // OpenMP [2.12.5, target Construct]
25333 // Non-predefined allocators appearing in a uses_allocators clause must
25334 // have traits specified.
25335 if (getLangOpts().OpenMP < 52) {
25336 if (!IsPredefinedAllocator && !D.AllocatorTraits) {
25337 Diag(Loc: D.Allocator->getExprLoc(),
25338 DiagID: diag::err_omp_nonpredefined_allocator_without_traits);
25339 continue;
25340 }
25341 }
25342 // No allocator traits - just convert it to rvalue.
25343 if (!D.AllocatorTraits)
25344 AllocatorExpr = SemaRef.DefaultLvalueConversion(E: AllocatorExpr).get();
25345 DSAStack->addUsesAllocatorsDecl(
25346 D: DRE->getDecl(),
25347 Kind: IsPredefinedAllocator
25348 ? DSAStackTy::UsesAllocatorsDeclKind::PredefinedAllocator
25349 : DSAStackTy::UsesAllocatorsDeclKind::UserDefinedAllocator);
25350 }
25351 Expr *AllocatorTraitsExpr = nullptr;
25352 if (D.AllocatorTraits) {
25353 if (D.AllocatorTraits->isTypeDependent()) {
25354 AllocatorTraitsExpr = D.AllocatorTraits;
25355 } else {
25356 // OpenMP [2.12.5, target Construct]
25357 // Arrays that contain allocator traits that appear in a uses_allocators
25358 // clause must be constant arrays, have constant values and be defined
25359 // in the same scope as the construct in which the clause appears.
25360 AllocatorTraitsExpr = D.AllocatorTraits->IgnoreParenImpCasts();
25361 // Check that traits expr is a constant array.
25362 QualType TraitTy;
25363 if (const ArrayType *Ty =
25364 AllocatorTraitsExpr->getType()->getAsArrayTypeUnsafe())
25365 if (const auto *ConstArrayTy = dyn_cast<ConstantArrayType>(Val: Ty))
25366 TraitTy = ConstArrayTy->getElementType();
25367 if (TraitTy.isNull() ||
25368 !(Context.hasSameUnqualifiedType(T1: TraitTy,
25369 DSAStack->getOMPAlloctraitT()) ||
25370 Context.typesAreCompatible(T1: TraitTy, DSAStack->getOMPAlloctraitT(),
25371 /*CompareUnqualified=*/true))) {
25372 Diag(Loc: D.AllocatorTraits->getExprLoc(),
25373 DiagID: diag::err_omp_expected_array_alloctraits)
25374 << AllocatorTraitsExpr->getType();
25375 continue;
25376 }
25377 // Do not map by default allocator traits if it is a standalone
25378 // variable.
25379 if (auto *DRE = dyn_cast<DeclRefExpr>(Val: AllocatorTraitsExpr))
25380 DSAStack->addUsesAllocatorsDecl(
25381 D: DRE->getDecl(),
25382 Kind: DSAStackTy::UsesAllocatorsDeclKind::AllocatorTrait);
25383 }
25384 }
25385 OMPUsesAllocatorsClause::Data &NewD = NewData.emplace_back();
25386 NewD.Allocator = AllocatorExpr;
25387 NewD.AllocatorTraits = AllocatorTraitsExpr;
25388 NewD.LParenLoc = D.LParenLoc;
25389 NewD.RParenLoc = D.RParenLoc;
25390 }
25391 return OMPUsesAllocatorsClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25392 EndLoc, Data: NewData);
25393}
25394
25395OMPClause *SemaOpenMP::ActOnOpenMPAffinityClause(
25396 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
25397 SourceLocation EndLoc, Expr *Modifier, ArrayRef<Expr *> Locators) {
25398 SmallVector<Expr *, 8> Vars;
25399 for (Expr *RefExpr : Locators) {
25400 assert(RefExpr && "NULL expr in OpenMP affinity clause.");
25401 if (isa<DependentScopeDeclRefExpr>(Val: RefExpr) || RefExpr->isTypeDependent()) {
25402 // It will be analyzed later.
25403 Vars.push_back(Elt: RefExpr);
25404 continue;
25405 }
25406
25407 SourceLocation ELoc = RefExpr->getExprLoc();
25408 Expr *SimpleExpr = RefExpr->IgnoreParenImpCasts();
25409
25410 if (!SimpleExpr->isLValue()) {
25411 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
25412 << 1 << 0 << RefExpr->getSourceRange();
25413 continue;
25414 }
25415
25416 ExprResult Res;
25417 {
25418 Sema::TentativeAnalysisScope Trap(SemaRef);
25419 Res = SemaRef.CreateBuiltinUnaryOp(OpLoc: ELoc, Opc: UO_AddrOf, InputExpr: SimpleExpr);
25420 }
25421 if (!Res.isUsable() && !isa<ArraySectionExpr>(Val: SimpleExpr) &&
25422 !isa<OMPArrayShapingExpr>(Val: SimpleExpr)) {
25423 Diag(Loc: ELoc, DiagID: diag::err_omp_expected_addressable_lvalue_or_array_item)
25424 << 1 << 0 << RefExpr->getSourceRange();
25425 continue;
25426 }
25427 Vars.push_back(Elt: SimpleExpr);
25428 }
25429
25430 return OMPAffinityClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25431 ColonLoc, EndLoc, Modifier, Locators: Vars);
25432}
25433
25434OMPClause *SemaOpenMP::ActOnOpenMPBindClause(OpenMPBindClauseKind Kind,
25435 SourceLocation KindLoc,
25436 SourceLocation StartLoc,
25437 SourceLocation LParenLoc,
25438 SourceLocation EndLoc) {
25439 if (Kind == OMPC_BIND_unknown) {
25440 Diag(Loc: KindLoc, DiagID: diag::err_omp_unexpected_clause_value)
25441 << getListOfPossibleValues(K: OMPC_bind, /*First=*/0,
25442 /*Last=*/unsigned(OMPC_BIND_unknown))
25443 << getOpenMPClauseNameForDiag(C: OMPC_bind);
25444 return nullptr;
25445 }
25446
25447 return OMPBindClause::Create(C: getASTContext(), K: Kind, KLoc: KindLoc, StartLoc,
25448 LParenLoc, EndLoc);
25449}
25450
25451OMPClause *SemaOpenMP::ActOnOpenMPXDynCGroupMemClause(Expr *Size,
25452 SourceLocation StartLoc,
25453 SourceLocation LParenLoc,
25454 SourceLocation EndLoc) {
25455 Expr *ValExpr = Size;
25456 Stmt *HelperValStmt = nullptr;
25457
25458 // OpenMP [2.5, Restrictions]
25459 // The ompx_dyn_cgroup_mem expression must evaluate to a positive integer
25460 // value.
25461 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_ompx_dyn_cgroup_mem,
25462 /*StrictlyPositive=*/false))
25463 return nullptr;
25464
25465 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
25466 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
25467 DKind, CKind: OMPC_ompx_dyn_cgroup_mem, OpenMPVersion: getLangOpts().OpenMP);
25468 if (CaptureRegion != OMPD_unknown &&
25469 !SemaRef.CurContext->isDependentContext()) {
25470 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
25471 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
25472 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
25473 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
25474 }
25475
25476 return new (getASTContext()) OMPXDynCGroupMemClause(
25477 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
25478}
25479
25480OMPClause *SemaOpenMP::ActOnOpenMPDynGroupprivateClause(
25481 OpenMPDynGroupprivateClauseModifier M1,
25482 OpenMPDynGroupprivateClauseFallbackModifier M2, Expr *Size,
25483 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation M1Loc,
25484 SourceLocation M2Loc, SourceLocation EndLoc) {
25485
25486 if ((M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ||
25487 (M2Loc.isValid() && M2 == OMPC_DYN_GROUPPRIVATE_FALLBACK_unknown)) {
25488 std::string Values = getListOfPossibleValues(
25489 K: OMPC_dyn_groupprivate, /*First=*/0, Last: OMPC_DYN_GROUPPRIVATE_unknown);
25490 Diag(Loc: (M1Loc.isValid() && M1 == OMPC_DYN_GROUPPRIVATE_unknown) ? M1Loc
25491 : M2Loc,
25492 DiagID: diag::err_omp_unexpected_clause_value)
25493 << Values << getOpenMPClauseName(C: OMPC_dyn_groupprivate);
25494 return nullptr;
25495 }
25496
25497 Expr *ValExpr = Size;
25498 Stmt *HelperValStmt = nullptr;
25499
25500 // OpenMP [2.5, Restrictions]
25501 // The dyn_groupprivate expression must evaluate to a positive integer
25502 // value.
25503 if (!isNonNegativeIntegerValue(ValExpr, SemaRef, CKind: OMPC_dyn_groupprivate,
25504 /*StrictlyPositive=*/false))
25505 return nullptr;
25506
25507 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
25508 OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
25509 DKind, CKind: OMPC_dyn_groupprivate, OpenMPVersion: getLangOpts().OpenMP);
25510 if (CaptureRegion != OMPD_unknown &&
25511 !SemaRef.CurContext->isDependentContext()) {
25512 ValExpr = SemaRef.MakeFullExpr(Arg: ValExpr).get();
25513 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
25514 ValExpr = tryBuildCapture(SemaRef, Capture: ValExpr, Captures).get();
25515 HelperValStmt = buildPreInits(Context&: getASTContext(), Captures);
25516 }
25517
25518 return new (getASTContext()) OMPDynGroupprivateClause(
25519 StartLoc, LParenLoc, EndLoc, ValExpr, HelperValStmt, CaptureRegion, M1,
25520 M1Loc, M2, M2Loc);
25521}
25522
25523OMPClause *SemaOpenMP::ActOnOpenMPDoacrossClause(
25524 OpenMPDoacrossClauseModifier DepType, SourceLocation DepLoc,
25525 SourceLocation ColonLoc, ArrayRef<Expr *> VarList, SourceLocation StartLoc,
25526 SourceLocation LParenLoc, SourceLocation EndLoc) {
25527
25528 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
25529 DepType != OMPC_DOACROSS_source && DepType != OMPC_DOACROSS_sink &&
25530 DepType != OMPC_DOACROSS_sink_omp_cur_iteration &&
25531 DepType != OMPC_DOACROSS_source_omp_cur_iteration) {
25532 Diag(Loc: DepLoc, DiagID: diag::err_omp_unexpected_clause_value)
25533 << "'source' or 'sink'" << getOpenMPClauseNameForDiag(C: OMPC_doacross);
25534 return nullptr;
25535 }
25536
25537 SmallVector<Expr *, 8> Vars;
25538 DSAStackTy::OperatorOffsetTy OpsOffs;
25539 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
25540 DoacrossDataInfoTy VarOffset = ProcessOpenMPDoacrossClauseCommon(
25541 SemaRef,
25542 IsSource: DepType == OMPC_DOACROSS_source ||
25543 DepType == OMPC_DOACROSS_source_omp_cur_iteration ||
25544 DepType == OMPC_DOACROSS_sink_omp_cur_iteration,
25545 VarList, DSAStack, EndLoc);
25546 Vars = VarOffset.Vars;
25547 OpsOffs = VarOffset.OpsOffs;
25548 TotalDepCount = VarOffset.TotalDepCount;
25549 auto *C = OMPDoacrossClause::Create(C: getASTContext(), StartLoc, LParenLoc,
25550 EndLoc, DepType, DepLoc, ColonLoc, VL: Vars,
25551 NumLoops: TotalDepCount.getZExtValue());
25552 if (DSAStack->isParentOrderedRegion())
25553 DSAStack->addDoacrossDependClause(C, OpsOffs);
25554 return C;
25555}
25556
25557OMPClause *SemaOpenMP::ActOnOpenMPXAttributeClause(ArrayRef<const Attr *> Attrs,
25558 SourceLocation StartLoc,
25559 SourceLocation LParenLoc,
25560 SourceLocation EndLoc) {
25561 return new (getASTContext())
25562 OMPXAttributeClause(Attrs, StartLoc, LParenLoc, EndLoc);
25563}
25564
25565OMPClause *SemaOpenMP::ActOnOpenMPXBareClause(SourceLocation StartLoc,
25566 SourceLocation EndLoc) {
25567 return new (getASTContext()) OMPXBareClause(StartLoc, EndLoc);
25568}
25569
25570OMPClause *SemaOpenMP::ActOnOpenMPHoldsClause(Expr *E, SourceLocation StartLoc,
25571 SourceLocation LParenLoc,
25572 SourceLocation EndLoc) {
25573 return new (getASTContext()) OMPHoldsClause(E, StartLoc, LParenLoc, EndLoc);
25574}
25575
25576OMPClause *SemaOpenMP::ActOnOpenMPDirectivePresenceClause(
25577 OpenMPClauseKind CK, llvm::ArrayRef<OpenMPDirectiveKind> DKVec,
25578 SourceLocation Loc, SourceLocation LLoc, SourceLocation RLoc) {
25579 switch (CK) {
25580 case OMPC_absent:
25581 return OMPAbsentClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
25582 case OMPC_contains:
25583 return OMPContainsClause::Create(C: getASTContext(), DKVec, Loc, LLoc, RLoc);
25584 default:
25585 llvm_unreachable("Unexpected OpenMP clause");
25586 }
25587}
25588
25589OMPClause *SemaOpenMP::ActOnOpenMPNullaryAssumptionClause(OpenMPClauseKind CK,
25590 SourceLocation Loc,
25591 SourceLocation RLoc) {
25592 switch (CK) {
25593 case OMPC_no_openmp:
25594 return new (getASTContext()) OMPNoOpenMPClause(Loc, RLoc);
25595 case OMPC_no_openmp_routines:
25596 return new (getASTContext()) OMPNoOpenMPRoutinesClause(Loc, RLoc);
25597 case OMPC_no_parallelism:
25598 return new (getASTContext()) OMPNoParallelismClause(Loc, RLoc);
25599 case OMPC_no_openmp_constructs:
25600 return new (getASTContext()) OMPNoOpenMPConstructsClause(Loc, RLoc);
25601 default:
25602 llvm_unreachable("Unexpected OpenMP clause");
25603 }
25604}
25605
25606ExprResult SemaOpenMP::ActOnOMPArraySectionExpr(
25607 Expr *Base, SourceLocation LBLoc, Expr *LowerBound,
25608 SourceLocation ColonLocFirst, SourceLocation ColonLocSecond, Expr *Length,
25609 Expr *Stride, SourceLocation RBLoc) {
25610 ASTContext &Context = getASTContext();
25611 if (Base->hasPlaceholderType() &&
25612 !Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
25613 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
25614 if (Result.isInvalid())
25615 return ExprError();
25616 Base = Result.get();
25617 }
25618 if (LowerBound && LowerBound->getType()->isNonOverloadPlaceholderType()) {
25619 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: LowerBound);
25620 if (Result.isInvalid())
25621 return ExprError();
25622 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25623 if (Result.isInvalid())
25624 return ExprError();
25625 LowerBound = Result.get();
25626 }
25627 if (Length && Length->getType()->isNonOverloadPlaceholderType()) {
25628 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Length);
25629 if (Result.isInvalid())
25630 return ExprError();
25631 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25632 if (Result.isInvalid())
25633 return ExprError();
25634 Length = Result.get();
25635 }
25636 if (Stride && Stride->getType()->isNonOverloadPlaceholderType()) {
25637 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Stride);
25638 if (Result.isInvalid())
25639 return ExprError();
25640 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25641 if (Result.isInvalid())
25642 return ExprError();
25643 Stride = Result.get();
25644 }
25645
25646 // Build an unanalyzed expression if either operand is type-dependent.
25647 if (Base->isTypeDependent() ||
25648 (LowerBound &&
25649 (LowerBound->isTypeDependent() || LowerBound->isValueDependent())) ||
25650 (Length && (Length->isTypeDependent() || Length->isValueDependent())) ||
25651 (Stride && (Stride->isTypeDependent() || Stride->isValueDependent()))) {
25652 return new (Context) ArraySectionExpr(
25653 Base, LowerBound, Length, Stride, Context.DependentTy, VK_LValue,
25654 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
25655 }
25656
25657 // Perform default conversions.
25658 QualType OriginalTy = ArraySectionExpr::getBaseOriginalType(Base);
25659 QualType ResultTy;
25660 if (OriginalTy->isAnyPointerType()) {
25661 ResultTy = OriginalTy->getPointeeType();
25662 } else if (OriginalTy->isArrayType()) {
25663 ResultTy = OriginalTy->getAsArrayTypeUnsafe()->getElementType();
25664 } else {
25665 return ExprError(
25666 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_typecheck_section_value)
25667 << Base->getSourceRange());
25668 }
25669 // C99 6.5.2.1p1
25670 if (LowerBound) {
25671 auto Res = PerformOpenMPImplicitIntegerConversion(Loc: LowerBound->getExprLoc(),
25672 Op: LowerBound);
25673 if (Res.isInvalid())
25674 return ExprError(Diag(Loc: LowerBound->getExprLoc(),
25675 DiagID: diag::err_omp_typecheck_section_not_integer)
25676 << 0 << LowerBound->getSourceRange());
25677 LowerBound = Res.get();
25678
25679 if (LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25680 LowerBound->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25681 Diag(Loc: LowerBound->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25682 << 0 << LowerBound->getSourceRange();
25683 }
25684 if (Length) {
25685 auto Res =
25686 PerformOpenMPImplicitIntegerConversion(Loc: Length->getExprLoc(), Op: Length);
25687 if (Res.isInvalid())
25688 return ExprError(Diag(Loc: Length->getExprLoc(),
25689 DiagID: diag::err_omp_typecheck_section_not_integer)
25690 << 1 << Length->getSourceRange());
25691 Length = Res.get();
25692
25693 if (Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25694 Length->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25695 Diag(Loc: Length->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25696 << 1 << Length->getSourceRange();
25697 }
25698 if (Stride) {
25699 ExprResult Res =
25700 PerformOpenMPImplicitIntegerConversion(Loc: Stride->getExprLoc(), Op: Stride);
25701 if (Res.isInvalid())
25702 return ExprError(Diag(Loc: Stride->getExprLoc(),
25703 DiagID: diag::err_omp_typecheck_section_not_integer)
25704 << 1 << Stride->getSourceRange());
25705 Stride = Res.get();
25706
25707 if (Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_S) ||
25708 Stride->getType()->isSpecificBuiltinType(K: BuiltinType::Char_U))
25709 Diag(Loc: Stride->getExprLoc(), DiagID: diag::warn_omp_section_is_char)
25710 << 1 << Stride->getSourceRange();
25711 }
25712
25713 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
25714 // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
25715 // type. Note that functions are not objects, and that (in C99 parlance)
25716 // incomplete types are not object types.
25717 if (ResultTy->isFunctionType()) {
25718 Diag(Loc: Base->getExprLoc(), DiagID: diag::err_omp_section_function_type)
25719 << ResultTy << Base->getSourceRange();
25720 return ExprError();
25721 }
25722
25723 if (SemaRef.RequireCompleteType(Loc: Base->getExprLoc(), T: ResultTy,
25724 DiagID: diag::err_omp_section_incomplete_type, Args: Base))
25725 return ExprError();
25726
25727 if (LowerBound && !OriginalTy->isAnyPointerType()) {
25728 Expr::EvalResult Result;
25729 if (LowerBound->EvaluateAsInt(Result, Ctx: Context)) {
25730 // OpenMP 5.0, [2.1.5 Array Sections]
25731 // The array section must be a subset of the original array.
25732 llvm::APSInt LowerBoundValue = Result.Val.getInt();
25733 if (LowerBoundValue.isNegative()) {
25734 Diag(Loc: LowerBound->getExprLoc(),
25735 DiagID: diag::err_omp_section_not_subset_of_array)
25736 << LowerBound->getSourceRange();
25737 return ExprError();
25738 }
25739 }
25740 }
25741
25742 if (Length) {
25743 Expr::EvalResult Result;
25744 if (Length->EvaluateAsInt(Result, Ctx: Context)) {
25745 // OpenMP 5.0, [2.1.5 Array Sections]
25746 // The length must evaluate to non-negative integers.
25747 llvm::APSInt LengthValue = Result.Val.getInt();
25748 if (LengthValue.isNegative()) {
25749 Diag(Loc: Length->getExprLoc(), DiagID: diag::err_omp_section_length_negative)
25750 << toString(I: LengthValue, /*Radix=*/10, /*Signed=*/true)
25751 << Length->getSourceRange();
25752 return ExprError();
25753 }
25754 }
25755 } else if (SemaRef.getLangOpts().OpenMP < 60 && ColonLocFirst.isValid() &&
25756 (OriginalTy.isNull() || (!OriginalTy->isConstantArrayType() &&
25757 !OriginalTy->isVariableArrayType()))) {
25758 // OpenMP 5.0, [2.1.5 Array Sections]
25759 // When the size of the array dimension is not known, the length must be
25760 // specified explicitly.
25761 Diag(Loc: ColonLocFirst, DiagID: diag::err_omp_section_length_undefined)
25762 << (!OriginalTy.isNull() && OriginalTy->isArrayType());
25763 return ExprError();
25764 }
25765
25766 if (Stride) {
25767 Expr::EvalResult Result;
25768 if (Stride->EvaluateAsInt(Result, Ctx: Context)) {
25769 // OpenMP 5.0, [2.1.5 Array Sections]
25770 // The stride must evaluate to a positive integer.
25771 llvm::APSInt StrideValue = Result.Val.getInt();
25772 if (!StrideValue.isStrictlyPositive()) {
25773 Diag(Loc: Stride->getExprLoc(), DiagID: diag::err_omp_section_stride_non_positive)
25774 << toString(I: StrideValue, /*Radix=*/10, /*Signed=*/true)
25775 << Stride->getSourceRange();
25776 return ExprError();
25777 }
25778 }
25779 }
25780
25781 if (!Base->hasPlaceholderType(K: BuiltinType::ArraySection)) {
25782 ExprResult Result = SemaRef.DefaultFunctionArrayLvalueConversion(E: Base);
25783 if (Result.isInvalid())
25784 return ExprError();
25785 Base = Result.get();
25786 }
25787 return new (Context) ArraySectionExpr(
25788 Base, LowerBound, Length, Stride, Context.ArraySectionTy, VK_LValue,
25789 OK_Ordinary, ColonLocFirst, ColonLocSecond, RBLoc);
25790}
25791
25792ExprResult SemaOpenMP::ActOnOMPArrayShapingExpr(
25793 Expr *Base, SourceLocation LParenLoc, SourceLocation RParenLoc,
25794 ArrayRef<Expr *> Dims, ArrayRef<SourceRange> Brackets) {
25795 ASTContext &Context = getASTContext();
25796 if (Base->hasPlaceholderType()) {
25797 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Base);
25798 if (Result.isInvalid())
25799 return ExprError();
25800 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25801 if (Result.isInvalid())
25802 return ExprError();
25803 Base = Result.get();
25804 }
25805 QualType BaseTy = Base->getType();
25806 // Delay analysis of the types/expressions if instantiation/specialization is
25807 // required.
25808 if (!BaseTy->isPointerType() && Base->isTypeDependent())
25809 return OMPArrayShapingExpr::Create(Context, T: Context.DependentTy, Op: Base,
25810 L: LParenLoc, R: RParenLoc, Dims, BracketRanges: Brackets);
25811 if (!BaseTy->isPointerType() ||
25812 (!Base->isTypeDependent() &&
25813 BaseTy->getPointeeType()->isIncompleteType()))
25814 return ExprError(Diag(Loc: Base->getExprLoc(),
25815 DiagID: diag::err_omp_non_pointer_type_array_shaping_base)
25816 << Base->getSourceRange());
25817
25818 SmallVector<Expr *, 4> NewDims;
25819 bool ErrorFound = false;
25820 for (Expr *Dim : Dims) {
25821 if (Dim->hasPlaceholderType()) {
25822 ExprResult Result = SemaRef.CheckPlaceholderExpr(E: Dim);
25823 if (Result.isInvalid()) {
25824 ErrorFound = true;
25825 continue;
25826 }
25827 Result = SemaRef.DefaultLvalueConversion(E: Result.get());
25828 if (Result.isInvalid()) {
25829 ErrorFound = true;
25830 continue;
25831 }
25832 Dim = Result.get();
25833 }
25834 if (!Dim->isTypeDependent()) {
25835 ExprResult Result =
25836 PerformOpenMPImplicitIntegerConversion(Loc: Dim->getExprLoc(), Op: Dim);
25837 if (Result.isInvalid()) {
25838 ErrorFound = true;
25839 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_typecheck_shaping_not_integer)
25840 << Dim->getSourceRange();
25841 continue;
25842 }
25843 Dim = Result.get();
25844 Expr::EvalResult EvResult;
25845 if (!Dim->isValueDependent() && Dim->EvaluateAsInt(Result&: EvResult, Ctx: Context)) {
25846 // OpenMP 5.0, [2.1.4 Array Shaping]
25847 // Each si is an integral type expression that must evaluate to a
25848 // positive integer.
25849 llvm::APSInt Value = EvResult.Val.getInt();
25850 if (!Value.isStrictlyPositive()) {
25851 Diag(Loc: Dim->getExprLoc(), DiagID: diag::err_omp_shaping_dimension_not_positive)
25852 << toString(I: Value, /*Radix=*/10, /*Signed=*/true)
25853 << Dim->getSourceRange();
25854 ErrorFound = true;
25855 continue;
25856 }
25857 }
25858 }
25859 NewDims.push_back(Elt: Dim);
25860 }
25861 if (ErrorFound)
25862 return ExprError();
25863 return OMPArrayShapingExpr::Create(Context, T: Context.OMPArrayShapingTy, Op: Base,
25864 L: LParenLoc, R: RParenLoc, Dims: NewDims, BracketRanges: Brackets);
25865}
25866
25867ExprResult SemaOpenMP::ActOnOMPIteratorExpr(Scope *S,
25868 SourceLocation IteratorKwLoc,
25869 SourceLocation LLoc,
25870 SourceLocation RLoc,
25871 ArrayRef<OMPIteratorData> Data) {
25872 ASTContext &Context = getASTContext();
25873 SmallVector<OMPIteratorExpr::IteratorDefinition, 4> ID;
25874 bool IsCorrect = true;
25875 for (const OMPIteratorData &D : Data) {
25876 TypeSourceInfo *TInfo = nullptr;
25877 SourceLocation StartLoc;
25878 QualType DeclTy;
25879 if (!D.Type.getAsOpaquePtr()) {
25880 // OpenMP 5.0, 2.1.6 Iterators
25881 // In an iterator-specifier, if the iterator-type is not specified then
25882 // the type of that iterator is of int type.
25883 DeclTy = Context.IntTy;
25884 StartLoc = D.DeclIdentLoc;
25885 } else {
25886 DeclTy = Sema::GetTypeFromParser(Ty: D.Type, TInfo: &TInfo);
25887 StartLoc = TInfo->getTypeLoc().getBeginLoc();
25888 }
25889
25890 bool IsDeclTyDependent = DeclTy->isDependentType() ||
25891 DeclTy->containsUnexpandedParameterPack() ||
25892 DeclTy->isInstantiationDependentType();
25893 if (!IsDeclTyDependent) {
25894 if (!DeclTy->isIntegralType(Ctx: Context) && !DeclTy->isAnyPointerType()) {
25895 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
25896 // The iterator-type must be an integral or pointer type.
25897 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
25898 << DeclTy;
25899 IsCorrect = false;
25900 continue;
25901 }
25902 if (DeclTy.isConstant(Ctx: Context)) {
25903 // OpenMP 5.0, 2.1.6 Iterators, Restrictions, C/C++
25904 // The iterator-type must not be const qualified.
25905 Diag(Loc: StartLoc, DiagID: diag::err_omp_iterator_not_integral_or_pointer)
25906 << DeclTy;
25907 IsCorrect = false;
25908 continue;
25909 }
25910 }
25911
25912 // Iterator declaration.
25913 assert(D.DeclIdent && "Identifier expected.");
25914 // Always try to create iterator declarator to avoid extra error messages
25915 // about unknown declarations use.
25916 auto *VD =
25917 VarDecl::Create(C&: Context, DC: SemaRef.CurContext, StartLoc, IdLoc: D.DeclIdentLoc,
25918 Id: D.DeclIdent, T: DeclTy, TInfo, S: SC_None);
25919 VD->setImplicit();
25920 if (S) {
25921 // Check for conflicting previous declaration.
25922 DeclarationNameInfo NameInfo(VD->getDeclName(), D.DeclIdentLoc);
25923 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
25924 RedeclarationKind::ForVisibleRedeclaration);
25925 Previous.suppressDiagnostics();
25926 SemaRef.LookupName(R&: Previous, S);
25927
25928 SemaRef.FilterLookupForScope(R&: Previous, Ctx: SemaRef.CurContext, S,
25929 /*ConsiderLinkage=*/false,
25930 /*AllowInlineNamespace=*/false);
25931 if (!Previous.empty()) {
25932 NamedDecl *Old = Previous.getRepresentativeDecl();
25933 Diag(Loc: D.DeclIdentLoc, DiagID: diag::err_redefinition) << VD->getDeclName();
25934 Diag(Loc: Old->getLocation(), DiagID: diag::note_previous_definition);
25935 } else {
25936 SemaRef.PushOnScopeChains(D: VD, S);
25937 }
25938 } else {
25939 SemaRef.CurContext->addDecl(D: VD);
25940 }
25941
25942 /// Act on the iterator variable declaration.
25943 ActOnOpenMPIteratorVarDecl(VD);
25944
25945 Expr *Begin = D.Range.Begin;
25946 if (!IsDeclTyDependent && Begin && !Begin->isTypeDependent()) {
25947 ExprResult BeginRes = SemaRef.PerformImplicitConversion(
25948 From: Begin, ToType: DeclTy, Action: AssignmentAction::Converting);
25949 Begin = BeginRes.get();
25950 }
25951 Expr *End = D.Range.End;
25952 if (!IsDeclTyDependent && End && !End->isTypeDependent()) {
25953 ExprResult EndRes = SemaRef.PerformImplicitConversion(
25954 From: End, ToType: DeclTy, Action: AssignmentAction::Converting);
25955 End = EndRes.get();
25956 }
25957 Expr *Step = D.Range.Step;
25958 if (!IsDeclTyDependent && Step && !Step->isTypeDependent()) {
25959 if (!Step->getType()->isIntegralType(Ctx: Context)) {
25960 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_not_integral)
25961 << Step << Step->getSourceRange();
25962 IsCorrect = false;
25963 continue;
25964 }
25965 std::optional<llvm::APSInt> Result =
25966 Step->getIntegerConstantExpr(Ctx: Context);
25967 // OpenMP 5.0, 2.1.6 Iterators, Restrictions
25968 // If the step expression of a range-specification equals zero, the
25969 // behavior is unspecified.
25970 if (Result && Result->isZero()) {
25971 Diag(Loc: Step->getExprLoc(), DiagID: diag::err_omp_iterator_step_constant_zero)
25972 << Step << Step->getSourceRange();
25973 IsCorrect = false;
25974 continue;
25975 }
25976 }
25977 if (!Begin || !End || !IsCorrect) {
25978 IsCorrect = false;
25979 continue;
25980 }
25981 OMPIteratorExpr::IteratorDefinition &IDElem = ID.emplace_back();
25982 IDElem.IteratorDecl = VD;
25983 IDElem.AssignmentLoc = D.AssignLoc;
25984 IDElem.Range.Begin = Begin;
25985 IDElem.Range.End = End;
25986 IDElem.Range.Step = Step;
25987 IDElem.ColonLoc = D.ColonLoc;
25988 IDElem.SecondColonLoc = D.SecColonLoc;
25989 }
25990 if (!IsCorrect) {
25991 // Invalidate all created iterator declarations if error is found.
25992 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
25993 if (Decl *ID = D.IteratorDecl)
25994 ID->setInvalidDecl();
25995 }
25996 return ExprError();
25997 }
25998 SmallVector<OMPIteratorHelperData, 4> Helpers;
25999 if (!SemaRef.CurContext->isDependentContext()) {
26000 // Build number of ityeration for each iteration range.
26001 // Ni = ((Stepi > 0) ? ((Endi + Stepi -1 - Begini)/Stepi) :
26002 // ((Begini-Stepi-1-Endi) / -Stepi);
26003 for (OMPIteratorExpr::IteratorDefinition &D : ID) {
26004 // (Endi - Begini)
26005 ExprResult Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Sub,
26006 LHSExpr: D.Range.End, RHSExpr: D.Range.Begin);
26007 if (!Res.isUsable()) {
26008 IsCorrect = false;
26009 continue;
26010 }
26011 ExprResult St, St1;
26012 if (D.Range.Step) {
26013 St = D.Range.Step;
26014 // (Endi - Begini) + Stepi
26015 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res.get(),
26016 RHSExpr: St.get());
26017 if (!Res.isUsable()) {
26018 IsCorrect = false;
26019 continue;
26020 }
26021 // (Endi - Begini) + Stepi - 1
26022 Res = SemaRef.CreateBuiltinBinOp(
26023 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res.get(),
26024 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
26025 if (!Res.isUsable()) {
26026 IsCorrect = false;
26027 continue;
26028 }
26029 // ((Endi - Begini) + Stepi - 1) / Stepi
26030 Res = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res.get(),
26031 RHSExpr: St.get());
26032 if (!Res.isUsable()) {
26033 IsCorrect = false;
26034 continue;
26035 }
26036 St1 = SemaRef.CreateBuiltinUnaryOp(OpLoc: D.AssignmentLoc, Opc: UO_Minus,
26037 InputExpr: D.Range.Step);
26038 // (Begini - Endi)
26039 ExprResult Res1 = SemaRef.CreateBuiltinBinOp(
26040 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: D.Range.Begin, RHSExpr: D.Range.End);
26041 if (!Res1.isUsable()) {
26042 IsCorrect = false;
26043 continue;
26044 }
26045 // (Begini - Endi) - Stepi
26046 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add, LHSExpr: Res1.get(),
26047 RHSExpr: St1.get());
26048 if (!Res1.isUsable()) {
26049 IsCorrect = false;
26050 continue;
26051 }
26052 // (Begini - Endi) - Stepi - 1
26053 Res1 = SemaRef.CreateBuiltinBinOp(
26054 OpLoc: D.AssignmentLoc, Opc: BO_Sub, LHSExpr: Res1.get(),
26055 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 1).get());
26056 if (!Res1.isUsable()) {
26057 IsCorrect = false;
26058 continue;
26059 }
26060 // ((Begini - Endi) - Stepi - 1) / (-Stepi)
26061 Res1 = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Div, LHSExpr: Res1.get(),
26062 RHSExpr: St1.get());
26063 if (!Res1.isUsable()) {
26064 IsCorrect = false;
26065 continue;
26066 }
26067 // Stepi > 0.
26068 ExprResult CmpRes = SemaRef.CreateBuiltinBinOp(
26069 OpLoc: D.AssignmentLoc, Opc: BO_GT, LHSExpr: D.Range.Step,
26070 RHSExpr: SemaRef.ActOnIntegerConstant(Loc: D.AssignmentLoc, Val: 0).get());
26071 if (!CmpRes.isUsable()) {
26072 IsCorrect = false;
26073 continue;
26074 }
26075 Res = SemaRef.ActOnConditionalOp(QuestionLoc: D.AssignmentLoc, ColonLoc: D.AssignmentLoc,
26076 CondExpr: CmpRes.get(), LHSExpr: Res.get(), RHSExpr: Res1.get());
26077 if (!Res.isUsable()) {
26078 IsCorrect = false;
26079 continue;
26080 }
26081 }
26082 Res = SemaRef.ActOnFinishFullExpr(Expr: Res.get(), /*DiscardedValue=*/false);
26083 if (!Res.isUsable()) {
26084 IsCorrect = false;
26085 continue;
26086 }
26087
26088 // Build counter update.
26089 // Build counter.
26090 auto *CounterVD = VarDecl::Create(C&: Context, DC: SemaRef.CurContext,
26091 StartLoc: D.IteratorDecl->getBeginLoc(),
26092 IdLoc: D.IteratorDecl->getBeginLoc(), Id: nullptr,
26093 T: Res.get()->getType(), TInfo: nullptr, S: SC_None);
26094 CounterVD->setImplicit();
26095 ExprResult RefRes =
26096 SemaRef.BuildDeclRefExpr(D: CounterVD, Ty: CounterVD->getType(), VK: VK_LValue,
26097 Loc: D.IteratorDecl->getBeginLoc());
26098 // Build counter update.
26099 // I = Begini + counter * Stepi;
26100 ExprResult UpdateRes;
26101 if (D.Range.Step) {
26102 UpdateRes = SemaRef.CreateBuiltinBinOp(
26103 OpLoc: D.AssignmentLoc, Opc: BO_Mul,
26104 LHSExpr: SemaRef.DefaultLvalueConversion(E: RefRes.get()).get(), RHSExpr: St.get());
26105 } else {
26106 UpdateRes = SemaRef.DefaultLvalueConversion(E: RefRes.get());
26107 }
26108 if (!UpdateRes.isUsable()) {
26109 IsCorrect = false;
26110 continue;
26111 }
26112 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Add,
26113 LHSExpr: D.Range.Begin, RHSExpr: UpdateRes.get());
26114 if (!UpdateRes.isUsable()) {
26115 IsCorrect = false;
26116 continue;
26117 }
26118 ExprResult VDRes =
26119 SemaRef.BuildDeclRefExpr(D: cast<VarDecl>(Val: D.IteratorDecl),
26120 Ty: cast<VarDecl>(Val: D.IteratorDecl)->getType(),
26121 VK: VK_LValue, Loc: D.IteratorDecl->getBeginLoc());
26122 UpdateRes = SemaRef.CreateBuiltinBinOp(OpLoc: D.AssignmentLoc, Opc: BO_Assign,
26123 LHSExpr: VDRes.get(), RHSExpr: UpdateRes.get());
26124 if (!UpdateRes.isUsable()) {
26125 IsCorrect = false;
26126 continue;
26127 }
26128 UpdateRes =
26129 SemaRef.ActOnFinishFullExpr(Expr: UpdateRes.get(), /*DiscardedValue=*/true);
26130 if (!UpdateRes.isUsable()) {
26131 IsCorrect = false;
26132 continue;
26133 }
26134 ExprResult CounterUpdateRes = SemaRef.CreateBuiltinUnaryOp(
26135 OpLoc: D.AssignmentLoc, Opc: UO_PreInc, InputExpr: RefRes.get());
26136 if (!CounterUpdateRes.isUsable()) {
26137 IsCorrect = false;
26138 continue;
26139 }
26140 CounterUpdateRes = SemaRef.ActOnFinishFullExpr(Expr: CounterUpdateRes.get(),
26141 /*DiscardedValue=*/true);
26142 if (!CounterUpdateRes.isUsable()) {
26143 IsCorrect = false;
26144 continue;
26145 }
26146 OMPIteratorHelperData &HD = Helpers.emplace_back();
26147 HD.CounterVD = CounterVD;
26148 HD.Upper = Res.get();
26149 HD.Update = UpdateRes.get();
26150 HD.CounterUpdate = CounterUpdateRes.get();
26151 }
26152 } else {
26153 Helpers.assign(NumElts: ID.size(), Elt: {});
26154 }
26155 if (!IsCorrect) {
26156 // Invalidate all created iterator declarations if error is found.
26157 for (const OMPIteratorExpr::IteratorDefinition &D : ID) {
26158 if (Decl *ID = D.IteratorDecl)
26159 ID->setInvalidDecl();
26160 }
26161 return ExprError();
26162 }
26163 return OMPIteratorExpr::Create(Context, T: Context.OMPIteratorTy, IteratorKwLoc,
26164 L: LLoc, R: RLoc, Data: ID, Helpers);
26165}
26166
26167/// Check if \p AssumptionStr is a known assumption and warn if not.
26168static void checkOMPAssumeAttr(Sema &S, SourceLocation Loc,
26169 StringRef AssumptionStr) {
26170 if (llvm::getKnownAssumptionStrings().count(Key: AssumptionStr))
26171 return;
26172
26173 unsigned BestEditDistance = 3;
26174 StringRef Suggestion;
26175 for (const auto &KnownAssumptionIt : llvm::getKnownAssumptionStrings()) {
26176 unsigned EditDistance =
26177 AssumptionStr.edit_distance(Other: KnownAssumptionIt.getKey());
26178 if (EditDistance < BestEditDistance) {
26179 Suggestion = KnownAssumptionIt.getKey();
26180 BestEditDistance = EditDistance;
26181 }
26182 }
26183
26184 if (!Suggestion.empty())
26185 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown_suggested)
26186 << AssumptionStr << Suggestion;
26187 else
26188 S.Diag(Loc, DiagID: diag::warn_omp_assume_attribute_string_unknown)
26189 << AssumptionStr;
26190}
26191
26192void SemaOpenMP::handleOMPAssumeAttr(Decl *D, const ParsedAttr &AL) {
26193 // Handle the case where the attribute has a text message.
26194 StringRef Str;
26195 SourceLocation AttrStrLoc;
26196 if (!SemaRef.checkStringLiteralArgumentAttr(Attr: AL, ArgNum: 0, Str, ArgLocation: &AttrStrLoc))
26197 return;
26198
26199 checkOMPAssumeAttr(S&: SemaRef, Loc: AttrStrLoc, AssumptionStr: Str);
26200
26201 D->addAttr(A: ::new (getASTContext()) OMPAssumeAttr(getASTContext(), AL, Str));
26202}
26203
26204SemaOpenMP::SemaOpenMP(Sema &S)
26205 : SemaBase(S), VarDataSharingAttributesStack(nullptr) {}
26206