[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / AST / OpenMPClause.cpp
blob4e19339a1361e6f2550a119b33253337f8b98e04
1 //===- OpenMPClause.cpp - Classes for OpenMP clauses ----------------------===//
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 //
9 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/OpenMPClause.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclOpenMP.h"
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Basic/OpenMPKinds.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <optional>
28 using namespace clang;
29 using namespace llvm;
30 using namespace omp;
32 OMPClause::child_range OMPClause::children() {
33 switch (getClauseKind()) {
34 default:
35 break;
36 #define GEN_CLANG_CLAUSE_CLASS
37 #define CLAUSE_CLASS(Enum, Str, Class) \
38 case Enum: \
39 return static_cast<Class *>(this)->children();
40 #include "llvm/Frontend/OpenMP/OMP.inc"
42 llvm_unreachable("unknown OMPClause");
45 OMPClause::child_range OMPClause::used_children() {
46 switch (getClauseKind()) {
47 #define GEN_CLANG_CLAUSE_CLASS
48 #define CLAUSE_CLASS(Enum, Str, Class) \
49 case Enum: \
50 return static_cast<Class *>(this)->used_children();
51 #define CLAUSE_NO_CLASS(Enum, Str) \
52 case Enum: \
53 break;
54 #include "llvm/Frontend/OpenMP/OMP.inc"
56 llvm_unreachable("unknown OMPClause");
59 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
60 auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
61 return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
64 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
65 switch (C->getClauseKind()) {
66 case OMPC_schedule:
67 return static_cast<const OMPScheduleClause *>(C);
68 case OMPC_dist_schedule:
69 return static_cast<const OMPDistScheduleClause *>(C);
70 case OMPC_firstprivate:
71 return static_cast<const OMPFirstprivateClause *>(C);
72 case OMPC_lastprivate:
73 return static_cast<const OMPLastprivateClause *>(C);
74 case OMPC_reduction:
75 return static_cast<const OMPReductionClause *>(C);
76 case OMPC_task_reduction:
77 return static_cast<const OMPTaskReductionClause *>(C);
78 case OMPC_in_reduction:
79 return static_cast<const OMPInReductionClause *>(C);
80 case OMPC_linear:
81 return static_cast<const OMPLinearClause *>(C);
82 case OMPC_if:
83 return static_cast<const OMPIfClause *>(C);
84 case OMPC_num_threads:
85 return static_cast<const OMPNumThreadsClause *>(C);
86 case OMPC_num_teams:
87 return static_cast<const OMPNumTeamsClause *>(C);
88 case OMPC_thread_limit:
89 return static_cast<const OMPThreadLimitClause *>(C);
90 case OMPC_device:
91 return static_cast<const OMPDeviceClause *>(C);
92 case OMPC_grainsize:
93 return static_cast<const OMPGrainsizeClause *>(C);
94 case OMPC_num_tasks:
95 return static_cast<const OMPNumTasksClause *>(C);
96 case OMPC_final:
97 return static_cast<const OMPFinalClause *>(C);
98 case OMPC_priority:
99 return static_cast<const OMPPriorityClause *>(C);
100 case OMPC_novariants:
101 return static_cast<const OMPNovariantsClause *>(C);
102 case OMPC_nocontext:
103 return static_cast<const OMPNocontextClause *>(C);
104 case OMPC_filter:
105 return static_cast<const OMPFilterClause *>(C);
106 case OMPC_ompx_dyn_cgroup_mem:
107 return static_cast<const OMPXDynCGroupMemClause *>(C);
108 case OMPC_default:
109 case OMPC_proc_bind:
110 case OMPC_safelen:
111 case OMPC_simdlen:
112 case OMPC_sizes:
113 case OMPC_allocator:
114 case OMPC_allocate:
115 case OMPC_collapse:
116 case OMPC_private:
117 case OMPC_shared:
118 case OMPC_aligned:
119 case OMPC_copyin:
120 case OMPC_copyprivate:
121 case OMPC_ordered:
122 case OMPC_nowait:
123 case OMPC_untied:
124 case OMPC_mergeable:
125 case OMPC_threadprivate:
126 case OMPC_flush:
127 case OMPC_depobj:
128 case OMPC_read:
129 case OMPC_write:
130 case OMPC_update:
131 case OMPC_capture:
132 case OMPC_compare:
133 case OMPC_seq_cst:
134 case OMPC_acq_rel:
135 case OMPC_acquire:
136 case OMPC_release:
137 case OMPC_relaxed:
138 case OMPC_depend:
139 case OMPC_threads:
140 case OMPC_simd:
141 case OMPC_map:
142 case OMPC_nogroup:
143 case OMPC_hint:
144 case OMPC_defaultmap:
145 case OMPC_unknown:
146 case OMPC_uniform:
147 case OMPC_to:
148 case OMPC_from:
149 case OMPC_use_device_ptr:
150 case OMPC_use_device_addr:
151 case OMPC_is_device_ptr:
152 case OMPC_has_device_addr:
153 case OMPC_unified_address:
154 case OMPC_unified_shared_memory:
155 case OMPC_reverse_offload:
156 case OMPC_dynamic_allocators:
157 case OMPC_atomic_default_mem_order:
158 case OMPC_at:
159 case OMPC_severity:
160 case OMPC_message:
161 case OMPC_device_type:
162 case OMPC_match:
163 case OMPC_nontemporal:
164 case OMPC_order:
165 case OMPC_destroy:
166 case OMPC_detach:
167 case OMPC_inclusive:
168 case OMPC_exclusive:
169 case OMPC_uses_allocators:
170 case OMPC_affinity:
171 case OMPC_when:
172 case OMPC_bind:
173 case OMPC_ompx_bare:
174 break;
175 default:
176 break;
179 return nullptr;
182 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
183 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
184 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
187 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
188 switch (C->getClauseKind()) {
189 case OMPC_lastprivate:
190 return static_cast<const OMPLastprivateClause *>(C);
191 case OMPC_reduction:
192 return static_cast<const OMPReductionClause *>(C);
193 case OMPC_task_reduction:
194 return static_cast<const OMPTaskReductionClause *>(C);
195 case OMPC_in_reduction:
196 return static_cast<const OMPInReductionClause *>(C);
197 case OMPC_linear:
198 return static_cast<const OMPLinearClause *>(C);
199 case OMPC_schedule:
200 case OMPC_dist_schedule:
201 case OMPC_firstprivate:
202 case OMPC_default:
203 case OMPC_proc_bind:
204 case OMPC_if:
205 case OMPC_final:
206 case OMPC_num_threads:
207 case OMPC_safelen:
208 case OMPC_simdlen:
209 case OMPC_sizes:
210 case OMPC_allocator:
211 case OMPC_allocate:
212 case OMPC_collapse:
213 case OMPC_private:
214 case OMPC_shared:
215 case OMPC_aligned:
216 case OMPC_copyin:
217 case OMPC_copyprivate:
218 case OMPC_ordered:
219 case OMPC_nowait:
220 case OMPC_untied:
221 case OMPC_mergeable:
222 case OMPC_threadprivate:
223 case OMPC_flush:
224 case OMPC_depobj:
225 case OMPC_read:
226 case OMPC_write:
227 case OMPC_update:
228 case OMPC_capture:
229 case OMPC_compare:
230 case OMPC_seq_cst:
231 case OMPC_acq_rel:
232 case OMPC_acquire:
233 case OMPC_release:
234 case OMPC_relaxed:
235 case OMPC_depend:
236 case OMPC_device:
237 case OMPC_threads:
238 case OMPC_simd:
239 case OMPC_map:
240 case OMPC_num_teams:
241 case OMPC_thread_limit:
242 case OMPC_priority:
243 case OMPC_grainsize:
244 case OMPC_nogroup:
245 case OMPC_num_tasks:
246 case OMPC_hint:
247 case OMPC_defaultmap:
248 case OMPC_unknown:
249 case OMPC_uniform:
250 case OMPC_to:
251 case OMPC_from:
252 case OMPC_use_device_ptr:
253 case OMPC_use_device_addr:
254 case OMPC_is_device_ptr:
255 case OMPC_has_device_addr:
256 case OMPC_unified_address:
257 case OMPC_unified_shared_memory:
258 case OMPC_reverse_offload:
259 case OMPC_dynamic_allocators:
260 case OMPC_atomic_default_mem_order:
261 case OMPC_at:
262 case OMPC_severity:
263 case OMPC_message:
264 case OMPC_device_type:
265 case OMPC_match:
266 case OMPC_nontemporal:
267 case OMPC_order:
268 case OMPC_destroy:
269 case OMPC_novariants:
270 case OMPC_nocontext:
271 case OMPC_detach:
272 case OMPC_inclusive:
273 case OMPC_exclusive:
274 case OMPC_uses_allocators:
275 case OMPC_affinity:
276 case OMPC_when:
277 case OMPC_bind:
278 break;
279 default:
280 break;
283 return nullptr;
286 /// Gets the address of the original, non-captured, expression used in the
287 /// clause as the preinitializer.
288 static Stmt **getAddrOfExprAsWritten(Stmt *S) {
289 if (!S)
290 return nullptr;
291 if (auto *DS = dyn_cast<DeclStmt>(S)) {
292 assert(DS->isSingleDecl() && "Only single expression must be captured.");
293 if (auto *OED = dyn_cast<OMPCapturedExprDecl>(DS->getSingleDecl()))
294 return OED->getInitAddress();
296 return nullptr;
299 OMPClause::child_range OMPIfClause::used_children() {
300 if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
301 return child_range(C, C + 1);
302 return child_range(&Condition, &Condition + 1);
305 OMPClause::child_range OMPGrainsizeClause::used_children() {
306 if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
307 return child_range(C, C + 1);
308 return child_range(&Grainsize, &Grainsize + 1);
311 OMPClause::child_range OMPNumTasksClause::used_children() {
312 if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
313 return child_range(C, C + 1);
314 return child_range(&NumTasks, &NumTasks + 1);
317 OMPClause::child_range OMPFinalClause::used_children() {
318 if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
319 return child_range(C, C + 1);
320 return children();
323 OMPClause::child_range OMPPriorityClause::used_children() {
324 if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
325 return child_range(C, C + 1);
326 return child_range(&Priority, &Priority + 1);
329 OMPClause::child_range OMPNovariantsClause::used_children() {
330 if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
331 return child_range(C, C + 1);
332 return children();
335 OMPClause::child_range OMPNocontextClause::used_children() {
336 if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
337 return child_range(C, C + 1);
338 return children();
341 OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
342 unsigned NumLoops,
343 SourceLocation StartLoc,
344 SourceLocation LParenLoc,
345 SourceLocation EndLoc) {
346 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
347 auto *Clause =
348 new (Mem) OMPOrderedClause(Num, NumLoops, StartLoc, LParenLoc, EndLoc);
349 for (unsigned I = 0; I < NumLoops; ++I) {
350 Clause->setLoopNumIterations(I, nullptr);
351 Clause->setLoopCounter(I, nullptr);
353 return Clause;
356 OMPOrderedClause *OMPOrderedClause::CreateEmpty(const ASTContext &C,
357 unsigned NumLoops) {
358 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
359 auto *Clause = new (Mem) OMPOrderedClause(NumLoops);
360 for (unsigned I = 0; I < NumLoops; ++I) {
361 Clause->setLoopNumIterations(I, nullptr);
362 Clause->setLoopCounter(I, nullptr);
364 return Clause;
367 void OMPOrderedClause::setLoopNumIterations(unsigned NumLoop,
368 Expr *NumIterations) {
369 assert(NumLoop < NumberOfLoops && "out of loops number.");
370 getTrailingObjects<Expr *>()[NumLoop] = NumIterations;
373 ArrayRef<Expr *> OMPOrderedClause::getLoopNumIterations() const {
374 return llvm::ArrayRef(getTrailingObjects<Expr *>(), NumberOfLoops);
377 void OMPOrderedClause::setLoopCounter(unsigned NumLoop, Expr *Counter) {
378 assert(NumLoop < NumberOfLoops && "out of loops number.");
379 getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop] = Counter;
382 Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) {
383 assert(NumLoop < NumberOfLoops && "out of loops number.");
384 return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
387 const Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) const {
388 assert(NumLoop < NumberOfLoops && "out of loops number.");
389 return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
392 OMPUpdateClause *OMPUpdateClause::Create(const ASTContext &C,
393 SourceLocation StartLoc,
394 SourceLocation EndLoc) {
395 return new (C) OMPUpdateClause(StartLoc, EndLoc, /*IsExtended=*/false);
398 OMPUpdateClause *
399 OMPUpdateClause::Create(const ASTContext &C, SourceLocation StartLoc,
400 SourceLocation LParenLoc, SourceLocation ArgumentLoc,
401 OpenMPDependClauseKind DK, SourceLocation EndLoc) {
402 void *Mem =
403 C.Allocate(totalSizeToAlloc<SourceLocation, OpenMPDependClauseKind>(2, 1),
404 alignof(OMPUpdateClause));
405 auto *Clause =
406 new (Mem) OMPUpdateClause(StartLoc, EndLoc, /*IsExtended=*/true);
407 Clause->setLParenLoc(LParenLoc);
408 Clause->setArgumentLoc(ArgumentLoc);
409 Clause->setDependencyKind(DK);
410 return Clause;
413 OMPUpdateClause *OMPUpdateClause::CreateEmpty(const ASTContext &C,
414 bool IsExtended) {
415 if (!IsExtended)
416 return new (C) OMPUpdateClause(/*IsExtended=*/false);
417 void *Mem =
418 C.Allocate(totalSizeToAlloc<SourceLocation, OpenMPDependClauseKind>(2, 1),
419 alignof(OMPUpdateClause));
420 auto *Clause = new (Mem) OMPUpdateClause(/*IsExtended=*/true);
421 Clause->IsExtended = true;
422 return Clause;
425 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
426 assert(VL.size() == varlist_size() &&
427 "Number of private copies is not the same as the preallocated buffer");
428 std::copy(VL.begin(), VL.end(), varlist_end());
431 OMPPrivateClause *
432 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
433 SourceLocation LParenLoc, SourceLocation EndLoc,
434 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
435 // Allocate space for private variables and initializer expressions.
436 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
437 OMPPrivateClause *Clause =
438 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
439 Clause->setVarRefs(VL);
440 Clause->setPrivateCopies(PrivateVL);
441 return Clause;
444 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
445 unsigned N) {
446 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
447 return new (Mem) OMPPrivateClause(N);
450 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
451 assert(VL.size() == varlist_size() &&
452 "Number of private copies is not the same as the preallocated buffer");
453 std::copy(VL.begin(), VL.end(), varlist_end());
456 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
457 assert(VL.size() == varlist_size() &&
458 "Number of inits is not the same as the preallocated buffer");
459 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
462 OMPFirstprivateClause *
463 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
464 SourceLocation LParenLoc, SourceLocation EndLoc,
465 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
466 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
467 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
468 OMPFirstprivateClause *Clause =
469 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
470 Clause->setVarRefs(VL);
471 Clause->setPrivateCopies(PrivateVL);
472 Clause->setInits(InitVL);
473 Clause->setPreInitStmt(PreInit);
474 return Clause;
477 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
478 unsigned N) {
479 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
480 return new (Mem) OMPFirstprivateClause(N);
483 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
484 assert(PrivateCopies.size() == varlist_size() &&
485 "Number of private copies is not the same as the preallocated buffer");
486 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
489 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
490 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
491 "not the same as the "
492 "preallocated buffer");
493 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
496 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
497 assert(DstExprs.size() == varlist_size() && "Number of destination "
498 "expressions is not the same as "
499 "the preallocated buffer");
500 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
503 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
504 assert(AssignmentOps.size() == varlist_size() &&
505 "Number of assignment expressions is not the same as the preallocated "
506 "buffer");
507 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
508 getDestinationExprs().end());
511 OMPLastprivateClause *OMPLastprivateClause::Create(
512 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
513 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
514 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps,
515 OpenMPLastprivateModifier LPKind, SourceLocation LPKindLoc,
516 SourceLocation ColonLoc, Stmt *PreInit, Expr *PostUpdate) {
517 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
518 OMPLastprivateClause *Clause = new (Mem) OMPLastprivateClause(
519 StartLoc, LParenLoc, EndLoc, LPKind, LPKindLoc, ColonLoc, VL.size());
520 Clause->setVarRefs(VL);
521 Clause->setSourceExprs(SrcExprs);
522 Clause->setDestinationExprs(DstExprs);
523 Clause->setAssignmentOps(AssignmentOps);
524 Clause->setPreInitStmt(PreInit);
525 Clause->setPostUpdateExpr(PostUpdate);
526 return Clause;
529 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
530 unsigned N) {
531 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
532 return new (Mem) OMPLastprivateClause(N);
535 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
536 SourceLocation StartLoc,
537 SourceLocation LParenLoc,
538 SourceLocation EndLoc,
539 ArrayRef<Expr *> VL) {
540 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
541 OMPSharedClause *Clause =
542 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
543 Clause->setVarRefs(VL);
544 return Clause;
547 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
548 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
549 return new (Mem) OMPSharedClause(N);
552 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
553 assert(PL.size() == varlist_size() &&
554 "Number of privates is not the same as the preallocated buffer");
555 std::copy(PL.begin(), PL.end(), varlist_end());
558 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
559 assert(IL.size() == varlist_size() &&
560 "Number of inits is not the same as the preallocated buffer");
561 std::copy(IL.begin(), IL.end(), getPrivates().end());
564 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
565 assert(UL.size() == varlist_size() &&
566 "Number of updates is not the same as the preallocated buffer");
567 std::copy(UL.begin(), UL.end(), getInits().end());
570 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
571 assert(FL.size() == varlist_size() &&
572 "Number of final updates is not the same as the preallocated buffer");
573 std::copy(FL.begin(), FL.end(), getUpdates().end());
576 void OMPLinearClause::setUsedExprs(ArrayRef<Expr *> UE) {
577 assert(
578 UE.size() == varlist_size() + 1 &&
579 "Number of used expressions is not the same as the preallocated buffer");
580 std::copy(UE.begin(), UE.end(), getFinals().end() + 2);
583 OMPLinearClause *OMPLinearClause::Create(
584 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
585 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
586 SourceLocation ColonLoc, SourceLocation StepModifierLoc,
587 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> PL,
588 ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep, Stmt *PreInit,
589 Expr *PostUpdate) {
590 // Allocate space for 5 lists (Vars, Inits, Updates, Finals), 2 expressions
591 // (Step and CalcStep), list of used expression + step.
592 void *Mem =
593 C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2 + VL.size() + 1));
594 OMPLinearClause *Clause =
595 new (Mem) OMPLinearClause(StartLoc, LParenLoc, Modifier, ModifierLoc,
596 ColonLoc, StepModifierLoc, EndLoc, VL.size());
597 Clause->setVarRefs(VL);
598 Clause->setPrivates(PL);
599 Clause->setInits(IL);
600 // Fill update and final expressions with zeroes, they are provided later,
601 // after the directive construction.
602 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
603 nullptr);
604 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
605 nullptr);
606 std::fill(Clause->getUsedExprs().begin(), Clause->getUsedExprs().end(),
607 nullptr);
608 Clause->setStep(Step);
609 Clause->setCalcStep(CalcStep);
610 Clause->setPreInitStmt(PreInit);
611 Clause->setPostUpdateExpr(PostUpdate);
612 return Clause;
615 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
616 unsigned NumVars) {
617 // Allocate space for 5 lists (Vars, Inits, Updates, Finals), 2 expressions
618 // (Step and CalcStep), list of used expression + step.
619 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2 + NumVars +1));
620 return new (Mem) OMPLinearClause(NumVars);
623 OMPClause::child_range OMPLinearClause::used_children() {
624 // Range includes only non-nullptr elements.
625 return child_range(
626 reinterpret_cast<Stmt **>(getUsedExprs().begin()),
627 reinterpret_cast<Stmt **>(llvm::find(getUsedExprs(), nullptr)));
630 OMPAlignedClause *
631 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
632 SourceLocation LParenLoc, SourceLocation ColonLoc,
633 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
634 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
635 OMPAlignedClause *Clause = new (Mem)
636 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
637 Clause->setVarRefs(VL);
638 Clause->setAlignment(A);
639 return Clause;
642 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
643 unsigned NumVars) {
644 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
645 return new (Mem) OMPAlignedClause(NumVars);
648 OMPAlignClause *OMPAlignClause::Create(const ASTContext &C, Expr *A,
649 SourceLocation StartLoc,
650 SourceLocation LParenLoc,
651 SourceLocation EndLoc) {
652 return new (C) OMPAlignClause(A, StartLoc, LParenLoc, EndLoc);
655 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
656 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
657 "not the same as the "
658 "preallocated buffer");
659 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
662 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
663 assert(DstExprs.size() == varlist_size() && "Number of destination "
664 "expressions is not the same as "
665 "the preallocated buffer");
666 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
669 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
670 assert(AssignmentOps.size() == varlist_size() &&
671 "Number of assignment expressions is not the same as the preallocated "
672 "buffer");
673 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
674 getDestinationExprs().end());
677 OMPCopyinClause *OMPCopyinClause::Create(
678 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
679 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
680 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
681 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
682 OMPCopyinClause *Clause =
683 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
684 Clause->setVarRefs(VL);
685 Clause->setSourceExprs(SrcExprs);
686 Clause->setDestinationExprs(DstExprs);
687 Clause->setAssignmentOps(AssignmentOps);
688 return Clause;
691 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
692 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
693 return new (Mem) OMPCopyinClause(N);
696 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
697 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
698 "not the same as the "
699 "preallocated buffer");
700 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
703 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
704 assert(DstExprs.size() == varlist_size() && "Number of destination "
705 "expressions is not the same as "
706 "the preallocated buffer");
707 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
710 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
711 assert(AssignmentOps.size() == varlist_size() &&
712 "Number of assignment expressions is not the same as the preallocated "
713 "buffer");
714 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
715 getDestinationExprs().end());
718 OMPCopyprivateClause *OMPCopyprivateClause::Create(
719 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
720 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
721 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
722 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
723 OMPCopyprivateClause *Clause =
724 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
725 Clause->setVarRefs(VL);
726 Clause->setSourceExprs(SrcExprs);
727 Clause->setDestinationExprs(DstExprs);
728 Clause->setAssignmentOps(AssignmentOps);
729 return Clause;
732 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
733 unsigned N) {
734 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
735 return new (Mem) OMPCopyprivateClause(N);
738 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
739 assert(Privates.size() == varlist_size() &&
740 "Number of private copies is not the same as the preallocated buffer");
741 std::copy(Privates.begin(), Privates.end(), varlist_end());
744 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
745 assert(
746 LHSExprs.size() == varlist_size() &&
747 "Number of LHS expressions is not the same as the preallocated buffer");
748 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
751 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
752 assert(
753 RHSExprs.size() == varlist_size() &&
754 "Number of RHS expressions is not the same as the preallocated buffer");
755 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
758 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
759 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
760 "expressions is not the same "
761 "as the preallocated buffer");
762 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
765 void OMPReductionClause::setInscanCopyOps(ArrayRef<Expr *> Ops) {
766 assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
767 assert(Ops.size() == varlist_size() && "Number of copy "
768 "expressions is not the same "
769 "as the preallocated buffer");
770 llvm::copy(Ops, getReductionOps().end());
773 void OMPReductionClause::setInscanCopyArrayTemps(
774 ArrayRef<Expr *> CopyArrayTemps) {
775 assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
776 assert(CopyArrayTemps.size() == varlist_size() &&
777 "Number of copy temp expressions is not the same as the preallocated "
778 "buffer");
779 llvm::copy(CopyArrayTemps, getInscanCopyOps().end());
782 void OMPReductionClause::setInscanCopyArrayElems(
783 ArrayRef<Expr *> CopyArrayElems) {
784 assert(Modifier == OMPC_REDUCTION_inscan && "Expected inscan reduction.");
785 assert(CopyArrayElems.size() == varlist_size() &&
786 "Number of copy temp expressions is not the same as the preallocated "
787 "buffer");
788 llvm::copy(CopyArrayElems, getInscanCopyArrayTemps().end());
791 OMPReductionClause *OMPReductionClause::Create(
792 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
793 SourceLocation ModifierLoc, SourceLocation EndLoc, SourceLocation ColonLoc,
794 OpenMPReductionClauseModifier Modifier, ArrayRef<Expr *> VL,
795 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
796 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
797 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
798 ArrayRef<Expr *> CopyOps, ArrayRef<Expr *> CopyArrayTemps,
799 ArrayRef<Expr *> CopyArrayElems, Stmt *PreInit, Expr *PostUpdate) {
800 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
801 (Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * VL.size()));
802 auto *Clause = new (Mem)
803 OMPReductionClause(StartLoc, LParenLoc, ModifierLoc, EndLoc, ColonLoc,
804 Modifier, VL.size(), QualifierLoc, NameInfo);
805 Clause->setVarRefs(VL);
806 Clause->setPrivates(Privates);
807 Clause->setLHSExprs(LHSExprs);
808 Clause->setRHSExprs(RHSExprs);
809 Clause->setReductionOps(ReductionOps);
810 Clause->setPreInitStmt(PreInit);
811 Clause->setPostUpdateExpr(PostUpdate);
812 if (Modifier == OMPC_REDUCTION_inscan) {
813 Clause->setInscanCopyOps(CopyOps);
814 Clause->setInscanCopyArrayTemps(CopyArrayTemps);
815 Clause->setInscanCopyArrayElems(CopyArrayElems);
816 } else {
817 assert(CopyOps.empty() &&
818 "copy operations are expected in inscan reductions only.");
819 assert(CopyArrayTemps.empty() &&
820 "copy array temps are expected in inscan reductions only.");
821 assert(CopyArrayElems.empty() &&
822 "copy array temps are expected in inscan reductions only.");
824 return Clause;
827 OMPReductionClause *
828 OMPReductionClause::CreateEmpty(const ASTContext &C, unsigned N,
829 OpenMPReductionClauseModifier Modifier) {
830 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(
831 (Modifier == OMPC_REDUCTION_inscan ? 8 : 5) * N));
832 auto *Clause = new (Mem) OMPReductionClause(N);
833 Clause->setModifier(Modifier);
834 return Clause;
837 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
838 assert(Privates.size() == varlist_size() &&
839 "Number of private copies is not the same as the preallocated buffer");
840 std::copy(Privates.begin(), Privates.end(), varlist_end());
843 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
844 assert(
845 LHSExprs.size() == varlist_size() &&
846 "Number of LHS expressions is not the same as the preallocated buffer");
847 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
850 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
851 assert(
852 RHSExprs.size() == varlist_size() &&
853 "Number of RHS expressions is not the same as the preallocated buffer");
854 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
857 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
858 assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
859 "expressions is not the same "
860 "as the preallocated buffer");
861 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
864 OMPTaskReductionClause *OMPTaskReductionClause::Create(
865 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
866 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
867 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
868 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
869 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
870 Expr *PostUpdate) {
871 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
872 OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
873 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
874 Clause->setVarRefs(VL);
875 Clause->setPrivates(Privates);
876 Clause->setLHSExprs(LHSExprs);
877 Clause->setRHSExprs(RHSExprs);
878 Clause->setReductionOps(ReductionOps);
879 Clause->setPreInitStmt(PreInit);
880 Clause->setPostUpdateExpr(PostUpdate);
881 return Clause;
884 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
885 unsigned N) {
886 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
887 return new (Mem) OMPTaskReductionClause(N);
890 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
891 assert(Privates.size() == varlist_size() &&
892 "Number of private copies is not the same as the preallocated buffer");
893 std::copy(Privates.begin(), Privates.end(), varlist_end());
896 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
897 assert(
898 LHSExprs.size() == varlist_size() &&
899 "Number of LHS expressions is not the same as the preallocated buffer");
900 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
903 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
904 assert(
905 RHSExprs.size() == varlist_size() &&
906 "Number of RHS expressions is not the same as the preallocated buffer");
907 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
910 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
911 assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
912 "expressions is not the same "
913 "as the preallocated buffer");
914 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
917 void OMPInReductionClause::setTaskgroupDescriptors(
918 ArrayRef<Expr *> TaskgroupDescriptors) {
919 assert(TaskgroupDescriptors.size() == varlist_size() &&
920 "Number of in reduction descriptors is not the same as the "
921 "preallocated buffer");
922 std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
923 getReductionOps().end());
926 OMPInReductionClause *OMPInReductionClause::Create(
927 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
928 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
929 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
930 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
931 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
932 ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
933 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
934 OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
935 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
936 Clause->setVarRefs(VL);
937 Clause->setPrivates(Privates);
938 Clause->setLHSExprs(LHSExprs);
939 Clause->setRHSExprs(RHSExprs);
940 Clause->setReductionOps(ReductionOps);
941 Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
942 Clause->setPreInitStmt(PreInit);
943 Clause->setPostUpdateExpr(PostUpdate);
944 return Clause;
947 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
948 unsigned N) {
949 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
950 return new (Mem) OMPInReductionClause(N);
953 OMPSizesClause *OMPSizesClause::Create(const ASTContext &C,
954 SourceLocation StartLoc,
955 SourceLocation LParenLoc,
956 SourceLocation EndLoc,
957 ArrayRef<Expr *> Sizes) {
958 OMPSizesClause *Clause = CreateEmpty(C, Sizes.size());
959 Clause->setLocStart(StartLoc);
960 Clause->setLParenLoc(LParenLoc);
961 Clause->setLocEnd(EndLoc);
962 Clause->setSizesRefs(Sizes);
963 return Clause;
966 OMPSizesClause *OMPSizesClause::CreateEmpty(const ASTContext &C,
967 unsigned NumSizes) {
968 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumSizes));
969 return new (Mem) OMPSizesClause(NumSizes);
972 OMPFullClause *OMPFullClause::Create(const ASTContext &C,
973 SourceLocation StartLoc,
974 SourceLocation EndLoc) {
975 OMPFullClause *Clause = CreateEmpty(C);
976 Clause->setLocStart(StartLoc);
977 Clause->setLocEnd(EndLoc);
978 return Clause;
981 OMPFullClause *OMPFullClause::CreateEmpty(const ASTContext &C) {
982 return new (C) OMPFullClause();
985 OMPPartialClause *OMPPartialClause::Create(const ASTContext &C,
986 SourceLocation StartLoc,
987 SourceLocation LParenLoc,
988 SourceLocation EndLoc,
989 Expr *Factor) {
990 OMPPartialClause *Clause = CreateEmpty(C);
991 Clause->setLocStart(StartLoc);
992 Clause->setLParenLoc(LParenLoc);
993 Clause->setLocEnd(EndLoc);
994 Clause->setFactor(Factor);
995 return Clause;
998 OMPPartialClause *OMPPartialClause::CreateEmpty(const ASTContext &C) {
999 return new (C) OMPPartialClause();
1002 OMPAllocateClause *
1003 OMPAllocateClause::Create(const ASTContext &C, SourceLocation StartLoc,
1004 SourceLocation LParenLoc, Expr *Allocator,
1005 SourceLocation ColonLoc, SourceLocation EndLoc,
1006 ArrayRef<Expr *> VL) {
1007 // Allocate space for private variables and initializer expressions.
1008 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
1009 auto *Clause = new (Mem) OMPAllocateClause(StartLoc, LParenLoc, Allocator,
1010 ColonLoc, EndLoc, VL.size());
1011 Clause->setVarRefs(VL);
1012 return Clause;
1015 OMPAllocateClause *OMPAllocateClause::CreateEmpty(const ASTContext &C,
1016 unsigned N) {
1017 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1018 return new (Mem) OMPAllocateClause(N);
1021 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
1022 SourceLocation StartLoc,
1023 SourceLocation LParenLoc,
1024 SourceLocation EndLoc,
1025 ArrayRef<Expr *> VL) {
1026 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
1027 OMPFlushClause *Clause =
1028 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
1029 Clause->setVarRefs(VL);
1030 return Clause;
1033 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
1034 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1035 return new (Mem) OMPFlushClause(N);
1038 OMPDepobjClause *OMPDepobjClause::Create(const ASTContext &C,
1039 SourceLocation StartLoc,
1040 SourceLocation LParenLoc,
1041 SourceLocation RParenLoc,
1042 Expr *Depobj) {
1043 auto *Clause = new (C) OMPDepobjClause(StartLoc, LParenLoc, RParenLoc);
1044 Clause->setDepobj(Depobj);
1045 return Clause;
1048 OMPDepobjClause *OMPDepobjClause::CreateEmpty(const ASTContext &C) {
1049 return new (C) OMPDepobjClause();
1052 OMPDependClause *
1053 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
1054 SourceLocation LParenLoc, SourceLocation EndLoc,
1055 DependDataTy Data, Expr *DepModifier,
1056 ArrayRef<Expr *> VL, unsigned NumLoops) {
1057 void *Mem = C.Allocate(
1058 totalSizeToAlloc<Expr *>(VL.size() + /*depend-modifier*/ 1 + NumLoops),
1059 alignof(OMPDependClause));
1060 OMPDependClause *Clause = new (Mem)
1061 OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
1062 Clause->setDependencyKind(Data.DepKind);
1063 Clause->setDependencyLoc(Data.DepLoc);
1064 Clause->setColonLoc(Data.ColonLoc);
1065 Clause->setOmpAllMemoryLoc(Data.OmpAllMemoryLoc);
1066 Clause->setModifier(DepModifier);
1067 Clause->setVarRefs(VL);
1068 for (unsigned I = 0 ; I < NumLoops; ++I)
1069 Clause->setLoopData(I, nullptr);
1070 return Clause;
1073 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N,
1074 unsigned NumLoops) {
1075 void *Mem =
1076 C.Allocate(totalSizeToAlloc<Expr *>(N + /*depend-modifier*/ 1 + NumLoops),
1077 alignof(OMPDependClause));
1078 return new (Mem) OMPDependClause(N, NumLoops);
1081 void OMPDependClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
1082 assert((getDependencyKind() == OMPC_DEPEND_sink ||
1083 getDependencyKind() == OMPC_DEPEND_source) &&
1084 NumLoop < NumLoops &&
1085 "Expected sink or source depend + loop index must be less number of "
1086 "loops.");
1087 auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1088 *It = Cnt;
1091 Expr *OMPDependClause::getLoopData(unsigned NumLoop) {
1092 assert((getDependencyKind() == OMPC_DEPEND_sink ||
1093 getDependencyKind() == OMPC_DEPEND_source) &&
1094 NumLoop < NumLoops &&
1095 "Expected sink or source depend + loop index must be less number of "
1096 "loops.");
1097 auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1098 return *It;
1101 const Expr *OMPDependClause::getLoopData(unsigned NumLoop) const {
1102 assert((getDependencyKind() == OMPC_DEPEND_sink ||
1103 getDependencyKind() == OMPC_DEPEND_source) &&
1104 NumLoop < NumLoops &&
1105 "Expected sink or source depend + loop index must be less number of "
1106 "loops.");
1107 const auto *It = std::next(getVarRefs().end(), NumLoop + 1);
1108 return *It;
1111 void OMPDependClause::setModifier(Expr *DepModifier) {
1112 *getVarRefs().end() = DepModifier;
1114 Expr *OMPDependClause::getModifier() { return *getVarRefs().end(); }
1116 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
1117 MappableExprComponentListsRef ComponentLists) {
1118 unsigned TotalNum = 0u;
1119 for (auto &C : ComponentLists)
1120 TotalNum += C.size();
1121 return TotalNum;
1124 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
1125 ArrayRef<const ValueDecl *> Declarations) {
1126 unsigned TotalNum = 0u;
1127 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
1128 for (const ValueDecl *D : Declarations) {
1129 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
1130 if (Cache.count(VD))
1131 continue;
1132 ++TotalNum;
1133 Cache.insert(VD);
1135 return TotalNum;
1138 OMPMapClause *OMPMapClause::Create(
1139 const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1140 ArrayRef<ValueDecl *> Declarations,
1141 MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1142 Expr *IteratorModifier, ArrayRef<OpenMPMapModifierKind> MapModifiers,
1143 ArrayRef<SourceLocation> MapModifiersLoc,
1144 NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId,
1145 OpenMPMapClauseKind Type, bool TypeIsImplicit, SourceLocation TypeLoc) {
1146 OMPMappableExprListSizeTy Sizes;
1147 Sizes.NumVars = Vars.size();
1148 Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1149 Sizes.NumComponentLists = ComponentLists.size();
1150 Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1152 // We need to allocate:
1153 // 2 x NumVars x Expr* - we have an original list expression and an associated
1154 // user-defined mapper for each clause list entry.
1155 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1156 // with each component list.
1157 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1158 // number of lists for each unique declaration and the size of each component
1159 // list.
1160 // NumComponents x MappableComponent - the total of all the components in all
1161 // the lists.
1162 void *Mem = C.Allocate(
1163 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1164 OMPClauseMappableExprCommon::MappableComponent>(
1165 2 * Sizes.NumVars + 1, Sizes.NumUniqueDeclarations,
1166 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1167 Sizes.NumComponents));
1168 OMPMapClause *Clause = new (Mem)
1169 OMPMapClause(MapModifiers, MapModifiersLoc, UDMQualifierLoc, MapperId,
1170 Type, TypeIsImplicit, TypeLoc, Locs, Sizes);
1172 Clause->setVarRefs(Vars);
1173 Clause->setUDMapperRefs(UDMapperRefs);
1174 Clause->setIteratorModifier(IteratorModifier);
1175 Clause->setClauseInfo(Declarations, ComponentLists);
1176 Clause->setMapType(Type);
1177 Clause->setMapLoc(TypeLoc);
1178 return Clause;
1181 OMPMapClause *
1182 OMPMapClause::CreateEmpty(const ASTContext &C,
1183 const OMPMappableExprListSizeTy &Sizes) {
1184 void *Mem = C.Allocate(
1185 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1186 OMPClauseMappableExprCommon::MappableComponent>(
1187 2 * Sizes.NumVars + 1, Sizes.NumUniqueDeclarations,
1188 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1189 Sizes.NumComponents));
1190 OMPMapClause *Clause = new (Mem) OMPMapClause(Sizes);
1191 Clause->setIteratorModifier(nullptr);
1192 return Clause;
1195 OMPToClause *OMPToClause::Create(
1196 const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1197 ArrayRef<ValueDecl *> Declarations,
1198 MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1199 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1200 ArrayRef<SourceLocation> MotionModifiersLoc,
1201 NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
1202 OMPMappableExprListSizeTy Sizes;
1203 Sizes.NumVars = Vars.size();
1204 Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1205 Sizes.NumComponentLists = ComponentLists.size();
1206 Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1208 // We need to allocate:
1209 // 2 x NumVars x Expr* - we have an original list expression and an associated
1210 // user-defined mapper for each clause list entry.
1211 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1212 // with each component list.
1213 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1214 // number of lists for each unique declaration and the size of each component
1215 // list.
1216 // NumComponents x MappableComponent - the total of all the components in all
1217 // the lists.
1218 void *Mem = C.Allocate(
1219 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1220 OMPClauseMappableExprCommon::MappableComponent>(
1221 2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1222 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1223 Sizes.NumComponents));
1225 auto *Clause = new (Mem) OMPToClause(MotionModifiers, MotionModifiersLoc,
1226 UDMQualifierLoc, MapperId, Locs, Sizes);
1228 Clause->setVarRefs(Vars);
1229 Clause->setUDMapperRefs(UDMapperRefs);
1230 Clause->setClauseInfo(Declarations, ComponentLists);
1231 return Clause;
1234 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C,
1235 const OMPMappableExprListSizeTy &Sizes) {
1236 void *Mem = C.Allocate(
1237 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1238 OMPClauseMappableExprCommon::MappableComponent>(
1239 2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1240 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1241 Sizes.NumComponents));
1242 return new (Mem) OMPToClause(Sizes);
1245 OMPFromClause *OMPFromClause::Create(
1246 const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1247 ArrayRef<ValueDecl *> Declarations,
1248 MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
1249 ArrayRef<OpenMPMotionModifierKind> MotionModifiers,
1250 ArrayRef<SourceLocation> MotionModifiersLoc,
1251 NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
1252 OMPMappableExprListSizeTy Sizes;
1253 Sizes.NumVars = Vars.size();
1254 Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1255 Sizes.NumComponentLists = ComponentLists.size();
1256 Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1258 // We need to allocate:
1259 // 2 x NumVars x Expr* - we have an original list expression and an associated
1260 // user-defined mapper for each clause list entry.
1261 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1262 // with each component list.
1263 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1264 // number of lists for each unique declaration and the size of each component
1265 // list.
1266 // NumComponents x MappableComponent - the total of all the components in all
1267 // the lists.
1268 void *Mem = C.Allocate(
1269 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1270 OMPClauseMappableExprCommon::MappableComponent>(
1271 2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1272 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1273 Sizes.NumComponents));
1275 auto *Clause =
1276 new (Mem) OMPFromClause(MotionModifiers, MotionModifiersLoc,
1277 UDMQualifierLoc, MapperId, Locs, Sizes);
1279 Clause->setVarRefs(Vars);
1280 Clause->setUDMapperRefs(UDMapperRefs);
1281 Clause->setClauseInfo(Declarations, ComponentLists);
1282 return Clause;
1285 OMPFromClause *
1286 OMPFromClause::CreateEmpty(const ASTContext &C,
1287 const OMPMappableExprListSizeTy &Sizes) {
1288 void *Mem = C.Allocate(
1289 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1290 OMPClauseMappableExprCommon::MappableComponent>(
1291 2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1292 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1293 Sizes.NumComponents));
1294 return new (Mem) OMPFromClause(Sizes);
1297 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
1298 assert(VL.size() == varlist_size() &&
1299 "Number of private copies is not the same as the preallocated buffer");
1300 std::copy(VL.begin(), VL.end(), varlist_end());
1303 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
1304 assert(VL.size() == varlist_size() &&
1305 "Number of inits is not the same as the preallocated buffer");
1306 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
1309 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
1310 const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
1311 ArrayRef<Expr *> PrivateVars, ArrayRef<Expr *> Inits,
1312 ArrayRef<ValueDecl *> Declarations,
1313 MappableExprComponentListsRef ComponentLists) {
1314 OMPMappableExprListSizeTy Sizes;
1315 Sizes.NumVars = Vars.size();
1316 Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1317 Sizes.NumComponentLists = ComponentLists.size();
1318 Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1320 // We need to allocate:
1321 // NumVars x Expr* - we have an original list expression for each clause
1322 // list entry.
1323 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1324 // with each component list.
1325 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1326 // number of lists for each unique declaration and the size of each component
1327 // list.
1328 // NumComponents x MappableComponent - the total of all the components in all
1329 // the lists.
1330 void *Mem = C.Allocate(
1331 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1332 OMPClauseMappableExprCommon::MappableComponent>(
1333 3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1334 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1335 Sizes.NumComponents));
1337 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(Locs, Sizes);
1339 Clause->setVarRefs(Vars);
1340 Clause->setPrivateCopies(PrivateVars);
1341 Clause->setInits(Inits);
1342 Clause->setClauseInfo(Declarations, ComponentLists);
1343 return Clause;
1346 OMPUseDevicePtrClause *
1347 OMPUseDevicePtrClause::CreateEmpty(const ASTContext &C,
1348 const OMPMappableExprListSizeTy &Sizes) {
1349 void *Mem = C.Allocate(
1350 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1351 OMPClauseMappableExprCommon::MappableComponent>(
1352 3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1353 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1354 Sizes.NumComponents));
1355 return new (Mem) OMPUseDevicePtrClause(Sizes);
1358 OMPUseDeviceAddrClause *
1359 OMPUseDeviceAddrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1360 ArrayRef<Expr *> Vars,
1361 ArrayRef<ValueDecl *> Declarations,
1362 MappableExprComponentListsRef ComponentLists) {
1363 OMPMappableExprListSizeTy Sizes;
1364 Sizes.NumVars = Vars.size();
1365 Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1366 Sizes.NumComponentLists = ComponentLists.size();
1367 Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1369 // We need to allocate:
1370 // 3 x NumVars x Expr* - we have an original list expression for each clause
1371 // list entry and an equal number of private copies and inits.
1372 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1373 // with each component list.
1374 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1375 // number of lists for each unique declaration and the size of each component
1376 // list.
1377 // NumComponents x MappableComponent - the total of all the components in all
1378 // the lists.
1379 void *Mem = C.Allocate(
1380 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1381 OMPClauseMappableExprCommon::MappableComponent>(
1382 Sizes.NumVars, Sizes.NumUniqueDeclarations,
1383 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1384 Sizes.NumComponents));
1386 auto *Clause = new (Mem) OMPUseDeviceAddrClause(Locs, Sizes);
1388 Clause->setVarRefs(Vars);
1389 Clause->setClauseInfo(Declarations, ComponentLists);
1390 return Clause;
1393 OMPUseDeviceAddrClause *
1394 OMPUseDeviceAddrClause::CreateEmpty(const ASTContext &C,
1395 const OMPMappableExprListSizeTy &Sizes) {
1396 void *Mem = C.Allocate(
1397 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1398 OMPClauseMappableExprCommon::MappableComponent>(
1399 Sizes.NumVars, Sizes.NumUniqueDeclarations,
1400 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1401 Sizes.NumComponents));
1402 return new (Mem) OMPUseDeviceAddrClause(Sizes);
1405 OMPIsDevicePtrClause *
1406 OMPIsDevicePtrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1407 ArrayRef<Expr *> Vars,
1408 ArrayRef<ValueDecl *> Declarations,
1409 MappableExprComponentListsRef ComponentLists) {
1410 OMPMappableExprListSizeTy Sizes;
1411 Sizes.NumVars = Vars.size();
1412 Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1413 Sizes.NumComponentLists = ComponentLists.size();
1414 Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1416 // We need to allocate:
1417 // NumVars x Expr* - we have an original list expression for each clause list
1418 // entry.
1419 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1420 // with each component list.
1421 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1422 // number of lists for each unique declaration and the size of each component
1423 // list.
1424 // NumComponents x MappableComponent - the total of all the components in all
1425 // the lists.
1426 void *Mem = C.Allocate(
1427 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1428 OMPClauseMappableExprCommon::MappableComponent>(
1429 Sizes.NumVars, Sizes.NumUniqueDeclarations,
1430 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1431 Sizes.NumComponents));
1433 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(Locs, Sizes);
1435 Clause->setVarRefs(Vars);
1436 Clause->setClauseInfo(Declarations, ComponentLists);
1437 return Clause;
1440 OMPIsDevicePtrClause *
1441 OMPIsDevicePtrClause::CreateEmpty(const ASTContext &C,
1442 const OMPMappableExprListSizeTy &Sizes) {
1443 void *Mem = C.Allocate(
1444 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1445 OMPClauseMappableExprCommon::MappableComponent>(
1446 Sizes.NumVars, Sizes.NumUniqueDeclarations,
1447 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1448 Sizes.NumComponents));
1449 return new (Mem) OMPIsDevicePtrClause(Sizes);
1452 OMPHasDeviceAddrClause *
1453 OMPHasDeviceAddrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1454 ArrayRef<Expr *> Vars,
1455 ArrayRef<ValueDecl *> Declarations,
1456 MappableExprComponentListsRef ComponentLists) {
1457 OMPMappableExprListSizeTy Sizes;
1458 Sizes.NumVars = Vars.size();
1459 Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1460 Sizes.NumComponentLists = ComponentLists.size();
1461 Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1463 // We need to allocate:
1464 // NumVars x Expr* - we have an original list expression for each clause list
1465 // entry.
1466 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1467 // with each component list.
1468 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1469 // number of lists for each unique declaration and the size of each component
1470 // list.
1471 // NumComponents x MappableComponent - the total of all the components in all
1472 // the lists.
1473 void *Mem = C.Allocate(
1474 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1475 OMPClauseMappableExprCommon::MappableComponent>(
1476 Sizes.NumVars, Sizes.NumUniqueDeclarations,
1477 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1478 Sizes.NumComponents));
1480 auto *Clause = new (Mem) OMPHasDeviceAddrClause(Locs, Sizes);
1482 Clause->setVarRefs(Vars);
1483 Clause->setClauseInfo(Declarations, ComponentLists);
1484 return Clause;
1487 OMPHasDeviceAddrClause *
1488 OMPHasDeviceAddrClause::CreateEmpty(const ASTContext &C,
1489 const OMPMappableExprListSizeTy &Sizes) {
1490 void *Mem = C.Allocate(
1491 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1492 OMPClauseMappableExprCommon::MappableComponent>(
1493 Sizes.NumVars, Sizes.NumUniqueDeclarations,
1494 Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1495 Sizes.NumComponents));
1496 return new (Mem) OMPHasDeviceAddrClause(Sizes);
1499 OMPNontemporalClause *OMPNontemporalClause::Create(const ASTContext &C,
1500 SourceLocation StartLoc,
1501 SourceLocation LParenLoc,
1502 SourceLocation EndLoc,
1503 ArrayRef<Expr *> VL) {
1504 // Allocate space for nontemporal variables + private references.
1505 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
1506 auto *Clause =
1507 new (Mem) OMPNontemporalClause(StartLoc, LParenLoc, EndLoc, VL.size());
1508 Clause->setVarRefs(VL);
1509 return Clause;
1512 OMPNontemporalClause *OMPNontemporalClause::CreateEmpty(const ASTContext &C,
1513 unsigned N) {
1514 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
1515 return new (Mem) OMPNontemporalClause(N);
1518 void OMPNontemporalClause::setPrivateRefs(ArrayRef<Expr *> VL) {
1519 assert(VL.size() == varlist_size() && "Number of private references is not "
1520 "the same as the preallocated buffer");
1521 std::copy(VL.begin(), VL.end(), varlist_end());
1524 OMPInclusiveClause *OMPInclusiveClause::Create(const ASTContext &C,
1525 SourceLocation StartLoc,
1526 SourceLocation LParenLoc,
1527 SourceLocation EndLoc,
1528 ArrayRef<Expr *> VL) {
1529 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
1530 auto *Clause =
1531 new (Mem) OMPInclusiveClause(StartLoc, LParenLoc, EndLoc, VL.size());
1532 Clause->setVarRefs(VL);
1533 return Clause;
1536 OMPInclusiveClause *OMPInclusiveClause::CreateEmpty(const ASTContext &C,
1537 unsigned N) {
1538 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1539 return new (Mem) OMPInclusiveClause(N);
1542 OMPExclusiveClause *OMPExclusiveClause::Create(const ASTContext &C,
1543 SourceLocation StartLoc,
1544 SourceLocation LParenLoc,
1545 SourceLocation EndLoc,
1546 ArrayRef<Expr *> VL) {
1547 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
1548 auto *Clause =
1549 new (Mem) OMPExclusiveClause(StartLoc, LParenLoc, EndLoc, VL.size());
1550 Clause->setVarRefs(VL);
1551 return Clause;
1554 OMPExclusiveClause *OMPExclusiveClause::CreateEmpty(const ASTContext &C,
1555 unsigned N) {
1556 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1557 return new (Mem) OMPExclusiveClause(N);
1560 void OMPUsesAllocatorsClause::setAllocatorsData(
1561 ArrayRef<OMPUsesAllocatorsClause::Data> Data) {
1562 assert(Data.size() == NumOfAllocators &&
1563 "Size of allocators data is not the same as the preallocated buffer.");
1564 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
1565 const OMPUsesAllocatorsClause::Data &D = Data[I];
1566 getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1567 static_cast<int>(ExprOffsets::Allocator)] =
1568 D.Allocator;
1569 getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1570 static_cast<int>(
1571 ExprOffsets::AllocatorTraits)] =
1572 D.AllocatorTraits;
1573 getTrailingObjects<
1574 SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1575 static_cast<int>(ParenLocsOffsets::LParen)] =
1576 D.LParenLoc;
1577 getTrailingObjects<
1578 SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1579 static_cast<int>(ParenLocsOffsets::RParen)] =
1580 D.RParenLoc;
1584 OMPUsesAllocatorsClause::Data
1585 OMPUsesAllocatorsClause::getAllocatorData(unsigned I) const {
1586 OMPUsesAllocatorsClause::Data Data;
1587 Data.Allocator =
1588 getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1589 static_cast<int>(ExprOffsets::Allocator)];
1590 Data.AllocatorTraits =
1591 getTrailingObjects<Expr *>()[I * static_cast<int>(ExprOffsets::Total) +
1592 static_cast<int>(
1593 ExprOffsets::AllocatorTraits)];
1594 Data.LParenLoc = getTrailingObjects<
1595 SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1596 static_cast<int>(ParenLocsOffsets::LParen)];
1597 Data.RParenLoc = getTrailingObjects<
1598 SourceLocation>()[I * static_cast<int>(ParenLocsOffsets::Total) +
1599 static_cast<int>(ParenLocsOffsets::RParen)];
1600 return Data;
1603 OMPUsesAllocatorsClause *
1604 OMPUsesAllocatorsClause::Create(const ASTContext &C, SourceLocation StartLoc,
1605 SourceLocation LParenLoc, SourceLocation EndLoc,
1606 ArrayRef<OMPUsesAllocatorsClause::Data> Data) {
1607 void *Mem = C.Allocate(totalSizeToAlloc<Expr *, SourceLocation>(
1608 static_cast<int>(ExprOffsets::Total) * Data.size(),
1609 static_cast<int>(ParenLocsOffsets::Total) * Data.size()));
1610 auto *Clause = new (Mem)
1611 OMPUsesAllocatorsClause(StartLoc, LParenLoc, EndLoc, Data.size());
1612 Clause->setAllocatorsData(Data);
1613 return Clause;
1616 OMPUsesAllocatorsClause *
1617 OMPUsesAllocatorsClause::CreateEmpty(const ASTContext &C, unsigned N) {
1618 void *Mem = C.Allocate(totalSizeToAlloc<Expr *, SourceLocation>(
1619 static_cast<int>(ExprOffsets::Total) * N,
1620 static_cast<int>(ParenLocsOffsets::Total) * N));
1621 return new (Mem) OMPUsesAllocatorsClause(N);
1624 OMPAffinityClause *
1625 OMPAffinityClause::Create(const ASTContext &C, SourceLocation StartLoc,
1626 SourceLocation LParenLoc, SourceLocation ColonLoc,
1627 SourceLocation EndLoc, Expr *Modifier,
1628 ArrayRef<Expr *> Locators) {
1629 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Locators.size() + 1));
1630 auto *Clause = new (Mem)
1631 OMPAffinityClause(StartLoc, LParenLoc, ColonLoc, EndLoc, Locators.size());
1632 Clause->setModifier(Modifier);
1633 Clause->setVarRefs(Locators);
1634 return Clause;
1637 OMPAffinityClause *OMPAffinityClause::CreateEmpty(const ASTContext &C,
1638 unsigned N) {
1639 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
1640 return new (Mem) OMPAffinityClause(N);
1643 OMPInitClause *OMPInitClause::Create(const ASTContext &C, Expr *InteropVar,
1644 OMPInteropInfo &InteropInfo,
1645 SourceLocation StartLoc,
1646 SourceLocation LParenLoc,
1647 SourceLocation VarLoc,
1648 SourceLocation EndLoc) {
1650 void *Mem =
1651 C.Allocate(totalSizeToAlloc<Expr *>(InteropInfo.PreferTypes.size() + 1));
1652 auto *Clause = new (Mem) OMPInitClause(
1653 InteropInfo.IsTarget, InteropInfo.IsTargetSync, StartLoc, LParenLoc,
1654 VarLoc, EndLoc, InteropInfo.PreferTypes.size() + 1);
1655 Clause->setInteropVar(InteropVar);
1656 llvm::copy(InteropInfo.PreferTypes, Clause->getTrailingObjects<Expr *>() + 1);
1657 return Clause;
1660 OMPInitClause *OMPInitClause::CreateEmpty(const ASTContext &C, unsigned N) {
1661 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
1662 return new (Mem) OMPInitClause(N);
1665 OMPBindClause *
1666 OMPBindClause::Create(const ASTContext &C, OpenMPBindClauseKind K,
1667 SourceLocation KLoc, SourceLocation StartLoc,
1668 SourceLocation LParenLoc, SourceLocation EndLoc) {
1669 return new (C) OMPBindClause(K, KLoc, StartLoc, LParenLoc, EndLoc);
1672 OMPBindClause *OMPBindClause::CreateEmpty(const ASTContext &C) {
1673 return new (C) OMPBindClause();
1676 OMPDoacrossClause *
1677 OMPDoacrossClause::Create(const ASTContext &C, SourceLocation StartLoc,
1678 SourceLocation LParenLoc, SourceLocation EndLoc,
1679 OpenMPDoacrossClauseModifier DepType,
1680 SourceLocation DepLoc, SourceLocation ColonLoc,
1681 ArrayRef<Expr *> VL, unsigned NumLoops) {
1682 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + NumLoops),
1683 alignof(OMPDoacrossClause));
1684 OMPDoacrossClause *Clause = new (Mem)
1685 OMPDoacrossClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
1686 Clause->setDependenceType(DepType);
1687 Clause->setDependenceLoc(DepLoc);
1688 Clause->setColonLoc(ColonLoc);
1689 Clause->setVarRefs(VL);
1690 for (unsigned I = 0; I < NumLoops; ++I)
1691 Clause->setLoopData(I, nullptr);
1692 return Clause;
1695 OMPDoacrossClause *OMPDoacrossClause::CreateEmpty(const ASTContext &C,
1696 unsigned N,
1697 unsigned NumLoops) {
1698 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + NumLoops),
1699 alignof(OMPDoacrossClause));
1700 return new (Mem) OMPDoacrossClause(N, NumLoops);
1703 void OMPDoacrossClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
1704 assert(NumLoop < NumLoops && "Loop index must be less number of loops.");
1705 auto *It = std::next(getVarRefs().end(), NumLoop);
1706 *It = Cnt;
1709 Expr *OMPDoacrossClause::getLoopData(unsigned NumLoop) {
1710 assert(NumLoop < NumLoops && "Loop index must be less number of loops.");
1711 auto *It = std::next(getVarRefs().end(), NumLoop);
1712 return *It;
1715 const Expr *OMPDoacrossClause::getLoopData(unsigned NumLoop) const {
1716 assert(NumLoop < NumLoops && "Loop index must be less number of loops.");
1717 const auto *It = std::next(getVarRefs().end(), NumLoop);
1718 return *It;
1721 //===----------------------------------------------------------------------===//
1722 // OpenMP clauses printing methods
1723 //===----------------------------------------------------------------------===//
1725 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
1726 OS << "if(";
1727 if (Node->getNameModifier() != OMPD_unknown)
1728 OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
1729 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1730 OS << ")";
1733 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
1734 OS << "final(";
1735 Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1736 OS << ")";
1739 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
1740 OS << "num_threads(";
1741 Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
1742 OS << ")";
1745 void OMPClausePrinter::VisitOMPAlignClause(OMPAlignClause *Node) {
1746 OS << "align(";
1747 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
1748 OS << ")";
1751 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
1752 OS << "safelen(";
1753 Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
1754 OS << ")";
1757 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
1758 OS << "simdlen(";
1759 Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
1760 OS << ")";
1763 void OMPClausePrinter::VisitOMPSizesClause(OMPSizesClause *Node) {
1764 OS << "sizes(";
1765 bool First = true;
1766 for (auto *Size : Node->getSizesRefs()) {
1767 if (!First)
1768 OS << ", ";
1769 Size->printPretty(OS, nullptr, Policy, 0);
1770 First = false;
1772 OS << ")";
1775 void OMPClausePrinter::VisitOMPFullClause(OMPFullClause *Node) { OS << "full"; }
1777 void OMPClausePrinter::VisitOMPPartialClause(OMPPartialClause *Node) {
1778 OS << "partial";
1780 if (Expr *Factor = Node->getFactor()) {
1781 OS << '(';
1782 Factor->printPretty(OS, nullptr, Policy, 0);
1783 OS << ')';
1787 void OMPClausePrinter::VisitOMPAllocatorClause(OMPAllocatorClause *Node) {
1788 OS << "allocator(";
1789 Node->getAllocator()->printPretty(OS, nullptr, Policy, 0);
1790 OS << ")";
1793 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
1794 OS << "collapse(";
1795 Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
1796 OS << ")";
1799 void OMPClausePrinter::VisitOMPDetachClause(OMPDetachClause *Node) {
1800 OS << "detach(";
1801 Node->getEventHandler()->printPretty(OS, nullptr, Policy, 0);
1802 OS << ")";
1805 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
1806 OS << "default("
1807 << getOpenMPSimpleClauseTypeName(OMPC_default,
1808 unsigned(Node->getDefaultKind()))
1809 << ")";
1812 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
1813 OS << "proc_bind("
1814 << getOpenMPSimpleClauseTypeName(OMPC_proc_bind,
1815 unsigned(Node->getProcBindKind()))
1816 << ")";
1819 void OMPClausePrinter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {
1820 OS << "unified_address";
1823 void OMPClausePrinter::VisitOMPUnifiedSharedMemoryClause(
1824 OMPUnifiedSharedMemoryClause *) {
1825 OS << "unified_shared_memory";
1828 void OMPClausePrinter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {
1829 OS << "reverse_offload";
1832 void OMPClausePrinter::VisitOMPDynamicAllocatorsClause(
1833 OMPDynamicAllocatorsClause *) {
1834 OS << "dynamic_allocators";
1837 void OMPClausePrinter::VisitOMPAtomicDefaultMemOrderClause(
1838 OMPAtomicDefaultMemOrderClause *Node) {
1839 OS << "atomic_default_mem_order("
1840 << getOpenMPSimpleClauseTypeName(OMPC_atomic_default_mem_order,
1841 Node->getAtomicDefaultMemOrderKind())
1842 << ")";
1845 void OMPClausePrinter::VisitOMPAtClause(OMPAtClause *Node) {
1846 OS << "at(" << getOpenMPSimpleClauseTypeName(OMPC_at, Node->getAtKind())
1847 << ")";
1850 void OMPClausePrinter::VisitOMPSeverityClause(OMPSeverityClause *Node) {
1851 OS << "severity("
1852 << getOpenMPSimpleClauseTypeName(OMPC_severity, Node->getSeverityKind())
1853 << ")";
1856 void OMPClausePrinter::VisitOMPMessageClause(OMPMessageClause *Node) {
1857 OS << "message(\""
1858 << cast<StringLiteral>(Node->getMessageString())->getString() << "\")";
1861 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
1862 OS << "schedule(";
1863 if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1864 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1865 Node->getFirstScheduleModifier());
1866 if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1867 OS << ", ";
1868 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1869 Node->getSecondScheduleModifier());
1871 OS << ": ";
1873 OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
1874 if (auto *E = Node->getChunkSize()) {
1875 OS << ", ";
1876 E->printPretty(OS, nullptr, Policy);
1878 OS << ")";
1881 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
1882 OS << "ordered";
1883 if (auto *Num = Node->getNumForLoops()) {
1884 OS << "(";
1885 Num->printPretty(OS, nullptr, Policy, 0);
1886 OS << ")";
1890 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
1891 OS << "nowait";
1894 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
1895 OS << "untied";
1898 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
1899 OS << "nogroup";
1902 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
1903 OS << "mergeable";
1906 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
1908 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
1910 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *Node) {
1911 OS << "update";
1912 if (Node->isExtended()) {
1913 OS << "(";
1914 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
1915 Node->getDependencyKind());
1916 OS << ")";
1920 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
1921 OS << "capture";
1924 void OMPClausePrinter::VisitOMPCompareClause(OMPCompareClause *) {
1925 OS << "compare";
1928 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
1929 OS << "seq_cst";
1932 void OMPClausePrinter::VisitOMPAcqRelClause(OMPAcqRelClause *) {
1933 OS << "acq_rel";
1936 void OMPClausePrinter::VisitOMPAcquireClause(OMPAcquireClause *) {
1937 OS << "acquire";
1940 void OMPClausePrinter::VisitOMPReleaseClause(OMPReleaseClause *) {
1941 OS << "release";
1944 void OMPClausePrinter::VisitOMPRelaxedClause(OMPRelaxedClause *) {
1945 OS << "relaxed";
1948 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
1949 OS << "threads";
1952 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
1954 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
1955 OS << "device(";
1956 OpenMPDeviceClauseModifier Modifier = Node->getModifier();
1957 if (Modifier != OMPC_DEVICE_unknown) {
1958 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
1959 << ": ";
1961 Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
1962 OS << ")";
1965 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
1966 OS << "num_teams(";
1967 Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
1968 OS << ")";
1971 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
1972 OS << "thread_limit(";
1973 Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
1974 OS << ")";
1977 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
1978 OS << "priority(";
1979 Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
1980 OS << ")";
1983 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
1984 OS << "grainsize(";
1985 OpenMPGrainsizeClauseModifier Modifier = Node->getModifier();
1986 if (Modifier != OMPC_GRAINSIZE_unknown) {
1987 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
1988 << ": ";
1990 Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
1991 OS << ")";
1994 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
1995 OS << "num_tasks(";
1996 OpenMPNumTasksClauseModifier Modifier = Node->getModifier();
1997 if (Modifier != OMPC_NUMTASKS_unknown) {
1998 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
1999 << ": ";
2001 Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
2002 OS << ")";
2005 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
2006 OS << "hint(";
2007 Node->getHint()->printPretty(OS, nullptr, Policy, 0);
2008 OS << ")";
2011 void OMPClausePrinter::VisitOMPInitClause(OMPInitClause *Node) {
2012 OS << "init(";
2013 bool First = true;
2014 for (const Expr *E : Node->prefs()) {
2015 if (First)
2016 OS << "prefer_type(";
2017 else
2018 OS << ",";
2019 E->printPretty(OS, nullptr, Policy);
2020 First = false;
2022 if (!First)
2023 OS << "), ";
2024 if (Node->getIsTarget())
2025 OS << "target";
2026 if (Node->getIsTargetSync()) {
2027 if (Node->getIsTarget())
2028 OS << ", ";
2029 OS << "targetsync";
2031 OS << " : ";
2032 Node->getInteropVar()->printPretty(OS, nullptr, Policy);
2033 OS << ")";
2036 void OMPClausePrinter::VisitOMPUseClause(OMPUseClause *Node) {
2037 OS << "use(";
2038 Node->getInteropVar()->printPretty(OS, nullptr, Policy);
2039 OS << ")";
2042 void OMPClausePrinter::VisitOMPDestroyClause(OMPDestroyClause *Node) {
2043 OS << "destroy";
2044 if (Expr *E = Node->getInteropVar()) {
2045 OS << "(";
2046 E->printPretty(OS, nullptr, Policy);
2047 OS << ")";
2051 void OMPClausePrinter::VisitOMPNovariantsClause(OMPNovariantsClause *Node) {
2052 OS << "novariants";
2053 if (Expr *E = Node->getCondition()) {
2054 OS << "(";
2055 E->printPretty(OS, nullptr, Policy, 0);
2056 OS << ")";
2060 void OMPClausePrinter::VisitOMPNocontextClause(OMPNocontextClause *Node) {
2061 OS << "nocontext";
2062 if (Expr *E = Node->getCondition()) {
2063 OS << "(";
2064 E->printPretty(OS, nullptr, Policy, 0);
2065 OS << ")";
2069 template<typename T>
2070 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
2071 for (typename T::varlist_iterator I = Node->varlist_begin(),
2072 E = Node->varlist_end();
2073 I != E; ++I) {
2074 assert(*I && "Expected non-null Stmt");
2075 OS << (I == Node->varlist_begin() ? StartSym : ',');
2076 if (auto *DRE = dyn_cast<DeclRefExpr>(*I)) {
2077 if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
2078 DRE->printPretty(OS, nullptr, Policy, 0);
2079 else
2080 DRE->getDecl()->printQualifiedName(OS);
2081 } else
2082 (*I)->printPretty(OS, nullptr, Policy, 0);
2086 void OMPClausePrinter::VisitOMPAllocateClause(OMPAllocateClause *Node) {
2087 if (Node->varlist_empty())
2088 return;
2089 OS << "allocate";
2090 if (Expr *Allocator = Node->getAllocator()) {
2091 OS << "(";
2092 Allocator->printPretty(OS, nullptr, Policy, 0);
2093 OS << ":";
2094 VisitOMPClauseList(Node, ' ');
2095 } else {
2096 VisitOMPClauseList(Node, '(');
2098 OS << ")";
2101 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
2102 if (!Node->varlist_empty()) {
2103 OS << "private";
2104 VisitOMPClauseList(Node, '(');
2105 OS << ")";
2109 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
2110 if (!Node->varlist_empty()) {
2111 OS << "firstprivate";
2112 VisitOMPClauseList(Node, '(');
2113 OS << ")";
2117 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
2118 if (!Node->varlist_empty()) {
2119 OS << "lastprivate";
2120 OpenMPLastprivateModifier LPKind = Node->getKind();
2121 if (LPKind != OMPC_LASTPRIVATE_unknown) {
2122 OS << "("
2123 << getOpenMPSimpleClauseTypeName(OMPC_lastprivate, Node->getKind())
2124 << ":";
2126 VisitOMPClauseList(Node, LPKind == OMPC_LASTPRIVATE_unknown ? '(' : ' ');
2127 OS << ")";
2131 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
2132 if (!Node->varlist_empty()) {
2133 OS << "shared";
2134 VisitOMPClauseList(Node, '(');
2135 OS << ")";
2139 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
2140 if (!Node->varlist_empty()) {
2141 OS << "reduction(";
2142 if (Node->getModifierLoc().isValid())
2143 OS << getOpenMPSimpleClauseTypeName(OMPC_reduction, Node->getModifier())
2144 << ", ";
2145 NestedNameSpecifier *QualifierLoc =
2146 Node->getQualifierLoc().getNestedNameSpecifier();
2147 OverloadedOperatorKind OOK =
2148 Node->getNameInfo().getName().getCXXOverloadedOperator();
2149 if (QualifierLoc == nullptr && OOK != OO_None) {
2150 // Print reduction identifier in C format
2151 OS << getOperatorSpelling(OOK);
2152 } else {
2153 // Use C++ format
2154 if (QualifierLoc != nullptr)
2155 QualifierLoc->print(OS, Policy);
2156 OS << Node->getNameInfo();
2158 OS << ":";
2159 VisitOMPClauseList(Node, ' ');
2160 OS << ")";
2164 void OMPClausePrinter::VisitOMPTaskReductionClause(
2165 OMPTaskReductionClause *Node) {
2166 if (!Node->varlist_empty()) {
2167 OS << "task_reduction(";
2168 NestedNameSpecifier *QualifierLoc =
2169 Node->getQualifierLoc().getNestedNameSpecifier();
2170 OverloadedOperatorKind OOK =
2171 Node->getNameInfo().getName().getCXXOverloadedOperator();
2172 if (QualifierLoc == nullptr && OOK != OO_None) {
2173 // Print reduction identifier in C format
2174 OS << getOperatorSpelling(OOK);
2175 } else {
2176 // Use C++ format
2177 if (QualifierLoc != nullptr)
2178 QualifierLoc->print(OS, Policy);
2179 OS << Node->getNameInfo();
2181 OS << ":";
2182 VisitOMPClauseList(Node, ' ');
2183 OS << ")";
2187 void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
2188 if (!Node->varlist_empty()) {
2189 OS << "in_reduction(";
2190 NestedNameSpecifier *QualifierLoc =
2191 Node->getQualifierLoc().getNestedNameSpecifier();
2192 OverloadedOperatorKind OOK =
2193 Node->getNameInfo().getName().getCXXOverloadedOperator();
2194 if (QualifierLoc == nullptr && OOK != OO_None) {
2195 // Print reduction identifier in C format
2196 OS << getOperatorSpelling(OOK);
2197 } else {
2198 // Use C++ format
2199 if (QualifierLoc != nullptr)
2200 QualifierLoc->print(OS, Policy);
2201 OS << Node->getNameInfo();
2203 OS << ":";
2204 VisitOMPClauseList(Node, ' ');
2205 OS << ")";
2209 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
2210 if (!Node->varlist_empty()) {
2211 OS << "linear";
2212 VisitOMPClauseList(Node, '(');
2213 if (Node->getModifierLoc().isValid() || Node->getStep() != nullptr) {
2214 OS << ": ";
2216 if (Node->getModifierLoc().isValid()) {
2217 OS << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
2219 if (Node->getStep() != nullptr) {
2220 if (Node->getModifierLoc().isValid()) {
2221 OS << ", ";
2223 OS << "step(";
2224 Node->getStep()->printPretty(OS, nullptr, Policy, 0);
2225 OS << ")";
2227 OS << ")";
2231 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
2232 if (!Node->varlist_empty()) {
2233 OS << "aligned";
2234 VisitOMPClauseList(Node, '(');
2235 if (Node->getAlignment() != nullptr) {
2236 OS << ": ";
2237 Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
2239 OS << ")";
2243 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
2244 if (!Node->varlist_empty()) {
2245 OS << "copyin";
2246 VisitOMPClauseList(Node, '(');
2247 OS << ")";
2251 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
2252 if (!Node->varlist_empty()) {
2253 OS << "copyprivate";
2254 VisitOMPClauseList(Node, '(');
2255 OS << ")";
2259 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
2260 if (!Node->varlist_empty()) {
2261 VisitOMPClauseList(Node, '(');
2262 OS << ")";
2266 void OMPClausePrinter::VisitOMPDepobjClause(OMPDepobjClause *Node) {
2267 OS << "(";
2268 Node->getDepobj()->printPretty(OS, nullptr, Policy, 0);
2269 OS << ")";
2272 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
2273 OS << "depend(";
2274 if (Expr *DepModifier = Node->getModifier()) {
2275 DepModifier->printPretty(OS, nullptr, Policy);
2276 OS << ", ";
2278 OpenMPDependClauseKind DepKind = Node->getDependencyKind();
2279 OpenMPDependClauseKind PrintKind = DepKind;
2280 bool IsOmpAllMemory = false;
2281 if (PrintKind == OMPC_DEPEND_outallmemory) {
2282 PrintKind = OMPC_DEPEND_out;
2283 IsOmpAllMemory = true;
2284 } else if (PrintKind == OMPC_DEPEND_inoutallmemory) {
2285 PrintKind = OMPC_DEPEND_inout;
2286 IsOmpAllMemory = true;
2288 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), PrintKind);
2289 if (!Node->varlist_empty() || IsOmpAllMemory)
2290 OS << " :";
2291 VisitOMPClauseList(Node, ' ');
2292 if (IsOmpAllMemory) {
2293 OS << (Node->varlist_empty() ? " " : ",");
2294 OS << "omp_all_memory";
2296 OS << ")";
2299 template <typename T>
2300 static void PrintMapper(raw_ostream &OS, T *Node,
2301 const PrintingPolicy &Policy) {
2302 OS << '(';
2303 NestedNameSpecifier *MapperNNS =
2304 Node->getMapperQualifierLoc().getNestedNameSpecifier();
2305 if (MapperNNS)
2306 MapperNNS->print(OS, Policy);
2307 OS << Node->getMapperIdInfo() << ')';
2310 template <typename T>
2311 static void PrintIterator(raw_ostream &OS, T *Node,
2312 const PrintingPolicy &Policy) {
2313 if (Expr *IteratorModifier = Node->getIteratorModifier())
2314 IteratorModifier->printPretty(OS, nullptr, Policy);
2317 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
2318 if (!Node->varlist_empty()) {
2319 OS << "map(";
2320 if (Node->getMapType() != OMPC_MAP_unknown) {
2321 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
2322 if (Node->getMapTypeModifier(I) != OMPC_MAP_MODIFIER_unknown) {
2323 if (Node->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator) {
2324 PrintIterator(OS, Node, Policy);
2325 } else {
2326 OS << getOpenMPSimpleClauseTypeName(OMPC_map,
2327 Node->getMapTypeModifier(I));
2328 if (Node->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_mapper)
2329 PrintMapper(OS, Node, Policy);
2331 OS << ',';
2334 OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
2335 OS << ':';
2337 VisitOMPClauseList(Node, ' ');
2338 OS << ")";
2342 template <typename T> void OMPClausePrinter::VisitOMPMotionClause(T *Node) {
2343 if (Node->varlist_empty())
2344 return;
2345 OS << getOpenMPClauseName(Node->getClauseKind());
2346 unsigned ModifierCount = 0;
2347 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2348 if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown)
2349 ++ModifierCount;
2351 if (ModifierCount) {
2352 OS << '(';
2353 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2354 if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown) {
2355 OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2356 Node->getMotionModifier(I));
2357 if (Node->getMotionModifier(I) == OMPC_MOTION_MODIFIER_mapper)
2358 PrintMapper(OS, Node, Policy);
2359 if (I < ModifierCount - 1)
2360 OS << ", ";
2363 OS << ':';
2364 VisitOMPClauseList(Node, ' ');
2365 } else {
2366 VisitOMPClauseList(Node, '(');
2368 OS << ")";
2371 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
2372 VisitOMPMotionClause(Node);
2375 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
2376 VisitOMPMotionClause(Node);
2379 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
2380 OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
2381 OMPC_dist_schedule, Node->getDistScheduleKind());
2382 if (auto *E = Node->getChunkSize()) {
2383 OS << ", ";
2384 E->printPretty(OS, nullptr, Policy);
2386 OS << ")";
2389 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
2390 OS << "defaultmap(";
2391 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2392 Node->getDefaultmapModifier());
2393 if (Node->getDefaultmapKind() != OMPC_DEFAULTMAP_unknown) {
2394 OS << ": ";
2395 OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2396 Node->getDefaultmapKind());
2398 OS << ")";
2401 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
2402 if (!Node->varlist_empty()) {
2403 OS << "use_device_ptr";
2404 VisitOMPClauseList(Node, '(');
2405 OS << ")";
2409 void OMPClausePrinter::VisitOMPUseDeviceAddrClause(
2410 OMPUseDeviceAddrClause *Node) {
2411 if (!Node->varlist_empty()) {
2412 OS << "use_device_addr";
2413 VisitOMPClauseList(Node, '(');
2414 OS << ")";
2418 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
2419 if (!Node->varlist_empty()) {
2420 OS << "is_device_ptr";
2421 VisitOMPClauseList(Node, '(');
2422 OS << ")";
2426 void OMPClausePrinter::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *Node) {
2427 if (!Node->varlist_empty()) {
2428 OS << "has_device_addr";
2429 VisitOMPClauseList(Node, '(');
2430 OS << ")";
2434 void OMPClausePrinter::VisitOMPNontemporalClause(OMPNontemporalClause *Node) {
2435 if (!Node->varlist_empty()) {
2436 OS << "nontemporal";
2437 VisitOMPClauseList(Node, '(');
2438 OS << ")";
2442 void OMPClausePrinter::VisitOMPOrderClause(OMPOrderClause *Node) {
2443 OS << "order(";
2444 if (Node->getModifier() != OMPC_ORDER_MODIFIER_unknown) {
2445 OS << getOpenMPSimpleClauseTypeName(OMPC_order, Node->getModifier());
2446 OS << ": ";
2448 OS << getOpenMPSimpleClauseTypeName(OMPC_order, Node->getKind()) << ")";
2451 void OMPClausePrinter::VisitOMPInclusiveClause(OMPInclusiveClause *Node) {
2452 if (!Node->varlist_empty()) {
2453 OS << "inclusive";
2454 VisitOMPClauseList(Node, '(');
2455 OS << ")";
2459 void OMPClausePrinter::VisitOMPExclusiveClause(OMPExclusiveClause *Node) {
2460 if (!Node->varlist_empty()) {
2461 OS << "exclusive";
2462 VisitOMPClauseList(Node, '(');
2463 OS << ")";
2467 void OMPClausePrinter::VisitOMPUsesAllocatorsClause(
2468 OMPUsesAllocatorsClause *Node) {
2469 if (Node->getNumberOfAllocators() == 0)
2470 return;
2471 OS << "uses_allocators(";
2472 for (unsigned I = 0, E = Node->getNumberOfAllocators(); I < E; ++I) {
2473 OMPUsesAllocatorsClause::Data Data = Node->getAllocatorData(I);
2474 Data.Allocator->printPretty(OS, nullptr, Policy);
2475 if (Data.AllocatorTraits) {
2476 OS << "(";
2477 Data.AllocatorTraits->printPretty(OS, nullptr, Policy);
2478 OS << ")";
2480 if (I < E - 1)
2481 OS << ",";
2483 OS << ")";
2486 void OMPClausePrinter::VisitOMPAffinityClause(OMPAffinityClause *Node) {
2487 if (Node->varlist_empty())
2488 return;
2489 OS << "affinity";
2490 char StartSym = '(';
2491 if (Expr *Modifier = Node->getModifier()) {
2492 OS << "(";
2493 Modifier->printPretty(OS, nullptr, Policy);
2494 OS << " :";
2495 StartSym = ' ';
2497 VisitOMPClauseList(Node, StartSym);
2498 OS << ")";
2501 void OMPClausePrinter::VisitOMPFilterClause(OMPFilterClause *Node) {
2502 OS << "filter(";
2503 Node->getThreadID()->printPretty(OS, nullptr, Policy, 0);
2504 OS << ")";
2507 void OMPClausePrinter::VisitOMPBindClause(OMPBindClause *Node) {
2508 OS << "bind("
2509 << getOpenMPSimpleClauseTypeName(OMPC_bind, unsigned(Node->getBindKind()))
2510 << ")";
2513 void OMPClausePrinter::VisitOMPXDynCGroupMemClause(
2514 OMPXDynCGroupMemClause *Node) {
2515 OS << "ompx_dyn_cgroup_mem(";
2516 Node->getSize()->printPretty(OS, nullptr, Policy, 0);
2517 OS << ")";
2520 void OMPClausePrinter::VisitOMPDoacrossClause(OMPDoacrossClause *Node) {
2521 OS << "doacross(";
2522 OpenMPDoacrossClauseModifier DepType = Node->getDependenceType();
2524 switch (DepType) {
2525 case OMPC_DOACROSS_source:
2526 OS << "source:";
2527 break;
2528 case OMPC_DOACROSS_sink:
2529 OS << "sink:";
2530 break;
2531 case OMPC_DOACROSS_source_omp_cur_iteration:
2532 OS << "source: omp_cur_iteration";
2533 break;
2534 case OMPC_DOACROSS_sink_omp_cur_iteration:
2535 OS << "sink: omp_cur_iteration - 1";
2536 break;
2537 default:
2538 llvm_unreachable("unknown docaross modifier");
2540 VisitOMPClauseList(Node, ' ');
2541 OS << ")";
2544 void OMPClausePrinter::VisitOMPXAttributeClause(OMPXAttributeClause *Node) {
2545 OS << "ompx_attribute(";
2546 bool IsFirst = true;
2547 for (auto &Attr : Node->getAttrs()) {
2548 if (!IsFirst)
2549 OS << ", ";
2550 Attr->printPretty(OS, Policy);
2551 IsFirst = false;
2553 OS << ")";
2556 void OMPClausePrinter::VisitOMPXBareClause(OMPXBareClause *Node) {
2557 OS << "ompx_bare";
2560 void OMPTraitInfo::getAsVariantMatchInfo(ASTContext &ASTCtx,
2561 VariantMatchInfo &VMI) const {
2562 for (const OMPTraitSet &Set : Sets) {
2563 for (const OMPTraitSelector &Selector : Set.Selectors) {
2565 // User conditions are special as we evaluate the condition here.
2566 if (Selector.Kind == TraitSelector::user_condition) {
2567 assert(Selector.ScoreOrCondition &&
2568 "Ill-formed user condition, expected condition expression!");
2569 assert(Selector.Properties.size() == 1 &&
2570 Selector.Properties.front().Kind ==
2571 TraitProperty::user_condition_unknown &&
2572 "Ill-formed user condition, expected unknown trait property!");
2574 if (std::optional<APSInt> CondVal =
2575 Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx))
2576 VMI.addTrait(CondVal->isZero() ? TraitProperty::user_condition_false
2577 : TraitProperty::user_condition_true,
2578 "<condition>");
2579 else
2580 VMI.addTrait(TraitProperty::user_condition_false, "<condition>");
2581 continue;
2584 std::optional<llvm::APSInt> Score;
2585 llvm::APInt *ScorePtr = nullptr;
2586 if (Selector.ScoreOrCondition) {
2587 if ((Score = Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx)))
2588 ScorePtr = &*Score;
2589 else
2590 VMI.addTrait(TraitProperty::user_condition_false,
2591 "<non-constant-score>");
2594 for (const OMPTraitProperty &Property : Selector.Properties)
2595 VMI.addTrait(Set.Kind, Property.Kind, Property.RawString, ScorePtr);
2597 if (Set.Kind != TraitSet::construct)
2598 continue;
2600 // TODO: This might not hold once we implement SIMD properly.
2601 assert(Selector.Properties.size() == 1 &&
2602 Selector.Properties.front().Kind ==
2603 getOpenMPContextTraitPropertyForSelector(
2604 Selector.Kind) &&
2605 "Ill-formed construct selector!");
2610 void OMPTraitInfo::print(llvm::raw_ostream &OS,
2611 const PrintingPolicy &Policy) const {
2612 bool FirstSet = true;
2613 for (const OMPTraitSet &Set : Sets) {
2614 if (!FirstSet)
2615 OS << ", ";
2616 FirstSet = false;
2617 OS << getOpenMPContextTraitSetName(Set.Kind) << "={";
2619 bool FirstSelector = true;
2620 for (const OMPTraitSelector &Selector : Set.Selectors) {
2621 if (!FirstSelector)
2622 OS << ", ";
2623 FirstSelector = false;
2624 OS << getOpenMPContextTraitSelectorName(Selector.Kind);
2626 bool AllowsTraitScore = false;
2627 bool RequiresProperty = false;
2628 isValidTraitSelectorForTraitSet(
2629 Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2631 if (!RequiresProperty)
2632 continue;
2634 OS << "(";
2635 if (Selector.Kind == TraitSelector::user_condition) {
2636 if (Selector.ScoreOrCondition)
2637 Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2638 else
2639 OS << "...";
2640 } else {
2642 if (Selector.ScoreOrCondition) {
2643 OS << "score(";
2644 Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2645 OS << "): ";
2648 bool FirstProperty = true;
2649 for (const OMPTraitProperty &Property : Selector.Properties) {
2650 if (!FirstProperty)
2651 OS << ", ";
2652 FirstProperty = false;
2653 OS << getOpenMPContextTraitPropertyName(Property.Kind,
2654 Property.RawString);
2657 OS << ")";
2659 OS << "}";
2663 std::string OMPTraitInfo::getMangledName() const {
2664 std::string MangledName;
2665 llvm::raw_string_ostream OS(MangledName);
2666 for (const OMPTraitSet &Set : Sets) {
2667 OS << '$' << 'S' << unsigned(Set.Kind);
2668 for (const OMPTraitSelector &Selector : Set.Selectors) {
2670 bool AllowsTraitScore = false;
2671 bool RequiresProperty = false;
2672 isValidTraitSelectorForTraitSet(
2673 Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2674 OS << '$' << 's' << unsigned(Selector.Kind);
2676 if (!RequiresProperty ||
2677 Selector.Kind == TraitSelector::user_condition)
2678 continue;
2680 for (const OMPTraitProperty &Property : Selector.Properties)
2681 OS << '$' << 'P'
2682 << getOpenMPContextTraitPropertyName(Property.Kind,
2683 Property.RawString);
2686 return MangledName;
2689 OMPTraitInfo::OMPTraitInfo(StringRef MangledName) {
2690 unsigned long U;
2691 do {
2692 if (!MangledName.consume_front("$S"))
2693 break;
2694 if (MangledName.consumeInteger(10, U))
2695 break;
2696 Sets.push_back(OMPTraitSet());
2697 OMPTraitSet &Set = Sets.back();
2698 Set.Kind = TraitSet(U);
2699 do {
2700 if (!MangledName.consume_front("$s"))
2701 break;
2702 if (MangledName.consumeInteger(10, U))
2703 break;
2704 Set.Selectors.push_back(OMPTraitSelector());
2705 OMPTraitSelector &Selector = Set.Selectors.back();
2706 Selector.Kind = TraitSelector(U);
2707 do {
2708 if (!MangledName.consume_front("$P"))
2709 break;
2710 Selector.Properties.push_back(OMPTraitProperty());
2711 OMPTraitProperty &Property = Selector.Properties.back();
2712 std::pair<StringRef, StringRef> PropRestPair = MangledName.split('$');
2713 Property.RawString = PropRestPair.first;
2714 Property.Kind = getOpenMPContextTraitPropertyKind(
2715 Set.Kind, Selector.Kind, PropRestPair.first);
2716 MangledName = MangledName.drop_front(PropRestPair.first.size());
2717 } while (true);
2718 } while (true);
2719 } while (true);
2722 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2723 const OMPTraitInfo &TI) {
2724 LangOptions LO;
2725 PrintingPolicy Policy(LO);
2726 TI.print(OS, Policy);
2727 return OS;
2729 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2730 const OMPTraitInfo *TI) {
2731 return TI ? OS << *TI : OS;
2734 TargetOMPContext::TargetOMPContext(
2735 ASTContext &ASTCtx, std::function<void(StringRef)> &&DiagUnknownTrait,
2736 const FunctionDecl *CurrentFunctionDecl,
2737 ArrayRef<llvm::omp::TraitProperty> ConstructTraits)
2738 : OMPContext(ASTCtx.getLangOpts().OpenMPIsTargetDevice,
2739 ASTCtx.getTargetInfo().getTriple()),
2740 FeatureValidityCheck([&](StringRef FeatureName) {
2741 return ASTCtx.getTargetInfo().isValidFeatureName(FeatureName);
2743 DiagUnknownTrait(std::move(DiagUnknownTrait)) {
2744 ASTCtx.getFunctionFeatureMap(FeatureMap, CurrentFunctionDecl);
2746 for (llvm::omp::TraitProperty Property : ConstructTraits)
2747 addTrait(Property);
2750 bool TargetOMPContext::matchesISATrait(StringRef RawString) const {
2751 auto It = FeatureMap.find(RawString);
2752 if (It != FeatureMap.end())
2753 return It->second;
2754 if (!FeatureValidityCheck(RawString))
2755 DiagUnknownTrait(RawString);
2756 return false;