1 //===- OpenMPClause.cpp - Classes for OpenMP clauses ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/OpenMPClause.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/ErrorHandling.h"
24 using namespace clang;
26 OMPClause::child_range OMPClause::children() {
27 switch (getClauseKind()) {
30 #define OPENMP_CLAUSE(Name, Class) \
32 return static_cast<Class *>(this)->children();
33 #include "clang/Basic/OpenMPKinds.def"
35 llvm_unreachable("unknown OMPClause");
38 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
39 auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
40 return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
43 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
44 switch (C->getClauseKind()) {
46 return static_cast<const OMPScheduleClause *>(C);
47 case OMPC_dist_schedule:
48 return static_cast<const OMPDistScheduleClause *>(C);
49 case OMPC_firstprivate:
50 return static_cast<const OMPFirstprivateClause *>(C);
51 case OMPC_lastprivate:
52 return static_cast<const OMPLastprivateClause *>(C);
54 return static_cast<const OMPReductionClause *>(C);
55 case OMPC_task_reduction:
56 return static_cast<const OMPTaskReductionClause *>(C);
57 case OMPC_in_reduction:
58 return static_cast<const OMPInReductionClause *>(C);
60 return static_cast<const OMPLinearClause *>(C);
62 return static_cast<const OMPIfClause *>(C);
63 case OMPC_num_threads:
64 return static_cast<const OMPNumThreadsClause *>(C);
66 return static_cast<const OMPNumTeamsClause *>(C);
67 case OMPC_thread_limit:
68 return static_cast<const OMPThreadLimitClause *>(C);
70 return static_cast<const OMPDeviceClause *>(C);
81 case OMPC_copyprivate:
86 case OMPC_threadprivate:
102 case OMPC_defaultmap:
107 case OMPC_use_device_ptr:
108 case OMPC_is_device_ptr:
115 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
116 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
117 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
120 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
121 switch (C->getClauseKind()) {
122 case OMPC_lastprivate:
123 return static_cast<const OMPLastprivateClause *>(C);
125 return static_cast<const OMPReductionClause *>(C);
126 case OMPC_task_reduction:
127 return static_cast<const OMPTaskReductionClause *>(C);
128 case OMPC_in_reduction:
129 return static_cast<const OMPInReductionClause *>(C);
131 return static_cast<const OMPLinearClause *>(C);
133 case OMPC_dist_schedule:
134 case OMPC_firstprivate:
139 case OMPC_num_threads:
147 case OMPC_copyprivate:
152 case OMPC_threadprivate:
165 case OMPC_thread_limit:
171 case OMPC_defaultmap:
176 case OMPC_use_device_ptr:
177 case OMPC_is_device_ptr:
184 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
185 assert(VL.size() == varlist_size() &&
186 "Number of private copies is not the same as the preallocated buffer");
187 std::copy(VL.begin(), VL.end(), varlist_end());
191 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
192 SourceLocation LParenLoc, SourceLocation EndLoc,
193 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
194 // Allocate space for private variables and initializer expressions.
195 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
196 OMPPrivateClause *Clause =
197 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
198 Clause->setVarRefs(VL);
199 Clause->setPrivateCopies(PrivateVL);
203 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
205 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
206 return new (Mem) OMPPrivateClause(N);
209 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
210 assert(VL.size() == varlist_size() &&
211 "Number of private copies is not the same as the preallocated buffer");
212 std::copy(VL.begin(), VL.end(), varlist_end());
215 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
216 assert(VL.size() == varlist_size() &&
217 "Number of inits is not the same as the preallocated buffer");
218 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
221 OMPFirstprivateClause *
222 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
223 SourceLocation LParenLoc, SourceLocation EndLoc,
224 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
225 ArrayRef<Expr *> InitVL, Stmt *PreInit) {
226 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
227 OMPFirstprivateClause *Clause =
228 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
229 Clause->setVarRefs(VL);
230 Clause->setPrivateCopies(PrivateVL);
231 Clause->setInits(InitVL);
232 Clause->setPreInitStmt(PreInit);
236 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
238 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
239 return new (Mem) OMPFirstprivateClause(N);
242 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
243 assert(PrivateCopies.size() == varlist_size() &&
244 "Number of private copies is not the same as the preallocated buffer");
245 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
248 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
249 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
250 "not the same as the "
251 "preallocated buffer");
252 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
255 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
256 assert(DstExprs.size() == varlist_size() && "Number of destination "
257 "expressions is not the same as "
258 "the preallocated buffer");
259 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
262 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
263 assert(AssignmentOps.size() == varlist_size() &&
264 "Number of assignment expressions is not the same as the preallocated "
266 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
267 getDestinationExprs().end());
270 OMPLastprivateClause *OMPLastprivateClause::Create(
271 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
272 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
273 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
275 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
276 OMPLastprivateClause *Clause =
277 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
278 Clause->setVarRefs(VL);
279 Clause->setSourceExprs(SrcExprs);
280 Clause->setDestinationExprs(DstExprs);
281 Clause->setAssignmentOps(AssignmentOps);
282 Clause->setPreInitStmt(PreInit);
283 Clause->setPostUpdateExpr(PostUpdate);
287 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
289 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
290 return new (Mem) OMPLastprivateClause(N);
293 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
294 SourceLocation StartLoc,
295 SourceLocation LParenLoc,
296 SourceLocation EndLoc,
297 ArrayRef<Expr *> VL) {
298 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
299 OMPSharedClause *Clause =
300 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
301 Clause->setVarRefs(VL);
305 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
306 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
307 return new (Mem) OMPSharedClause(N);
310 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
311 assert(PL.size() == varlist_size() &&
312 "Number of privates is not the same as the preallocated buffer");
313 std::copy(PL.begin(), PL.end(), varlist_end());
316 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
317 assert(IL.size() == varlist_size() &&
318 "Number of inits is not the same as the preallocated buffer");
319 std::copy(IL.begin(), IL.end(), getPrivates().end());
322 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
323 assert(UL.size() == varlist_size() &&
324 "Number of updates is not the same as the preallocated buffer");
325 std::copy(UL.begin(), UL.end(), getInits().end());
328 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
329 assert(FL.size() == varlist_size() &&
330 "Number of final updates is not the same as the preallocated buffer");
331 std::copy(FL.begin(), FL.end(), getUpdates().end());
334 OMPLinearClause *OMPLinearClause::Create(
335 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
336 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
337 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
338 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
339 Stmt *PreInit, Expr *PostUpdate) {
340 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
341 // (Step and CalcStep).
342 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
343 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
344 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
345 Clause->setVarRefs(VL);
346 Clause->setPrivates(PL);
347 Clause->setInits(IL);
348 // Fill update and final expressions with zeroes, they are provided later,
349 // after the directive construction.
350 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
352 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
354 Clause->setStep(Step);
355 Clause->setCalcStep(CalcStep);
356 Clause->setPreInitStmt(PreInit);
357 Clause->setPostUpdateExpr(PostUpdate);
361 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
363 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
364 // (Step and CalcStep).
365 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
366 return new (Mem) OMPLinearClause(NumVars);
370 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
371 SourceLocation LParenLoc, SourceLocation ColonLoc,
372 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
373 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
374 OMPAlignedClause *Clause = new (Mem)
375 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
376 Clause->setVarRefs(VL);
377 Clause->setAlignment(A);
381 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
383 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
384 return new (Mem) OMPAlignedClause(NumVars);
387 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
388 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
389 "not the same as the "
390 "preallocated buffer");
391 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
394 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
395 assert(DstExprs.size() == varlist_size() && "Number of destination "
396 "expressions is not the same as "
397 "the preallocated buffer");
398 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
401 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
402 assert(AssignmentOps.size() == varlist_size() &&
403 "Number of assignment expressions is not the same as the preallocated "
405 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
406 getDestinationExprs().end());
409 OMPCopyinClause *OMPCopyinClause::Create(
410 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
411 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
412 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
413 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
414 OMPCopyinClause *Clause =
415 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
416 Clause->setVarRefs(VL);
417 Clause->setSourceExprs(SrcExprs);
418 Clause->setDestinationExprs(DstExprs);
419 Clause->setAssignmentOps(AssignmentOps);
423 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
424 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
425 return new (Mem) OMPCopyinClause(N);
428 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
429 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
430 "not the same as the "
431 "preallocated buffer");
432 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
435 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
436 assert(DstExprs.size() == varlist_size() && "Number of destination "
437 "expressions is not the same as "
438 "the preallocated buffer");
439 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
442 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
443 assert(AssignmentOps.size() == varlist_size() &&
444 "Number of assignment expressions is not the same as the preallocated "
446 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
447 getDestinationExprs().end());
450 OMPCopyprivateClause *OMPCopyprivateClause::Create(
451 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
452 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
453 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
454 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
455 OMPCopyprivateClause *Clause =
456 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
457 Clause->setVarRefs(VL);
458 Clause->setSourceExprs(SrcExprs);
459 Clause->setDestinationExprs(DstExprs);
460 Clause->setAssignmentOps(AssignmentOps);
464 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
466 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
467 return new (Mem) OMPCopyprivateClause(N);
470 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
471 assert(Privates.size() == varlist_size() &&
472 "Number of private copies is not the same as the preallocated buffer");
473 std::copy(Privates.begin(), Privates.end(), varlist_end());
476 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
478 LHSExprs.size() == varlist_size() &&
479 "Number of LHS expressions is not the same as the preallocated buffer");
480 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
483 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
485 RHSExprs.size() == varlist_size() &&
486 "Number of RHS expressions is not the same as the preallocated buffer");
487 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
490 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
491 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
492 "expressions is not the same "
493 "as the preallocated buffer");
494 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
497 OMPReductionClause *OMPReductionClause::Create(
498 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
499 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
500 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
501 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
502 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
504 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
505 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
506 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
507 Clause->setVarRefs(VL);
508 Clause->setPrivates(Privates);
509 Clause->setLHSExprs(LHSExprs);
510 Clause->setRHSExprs(RHSExprs);
511 Clause->setReductionOps(ReductionOps);
512 Clause->setPreInitStmt(PreInit);
513 Clause->setPostUpdateExpr(PostUpdate);
517 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
519 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
520 return new (Mem) OMPReductionClause(N);
523 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
524 assert(Privates.size() == varlist_size() &&
525 "Number of private copies is not the same as the preallocated buffer");
526 std::copy(Privates.begin(), Privates.end(), varlist_end());
529 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
531 LHSExprs.size() == varlist_size() &&
532 "Number of LHS expressions is not the same as the preallocated buffer");
533 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
536 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
538 RHSExprs.size() == varlist_size() &&
539 "Number of RHS expressions is not the same as the preallocated buffer");
540 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
543 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
544 assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
545 "expressions is not the same "
546 "as the preallocated buffer");
547 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
550 OMPTaskReductionClause *OMPTaskReductionClause::Create(
551 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
552 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
553 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
554 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
555 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
557 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
558 OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
559 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
560 Clause->setVarRefs(VL);
561 Clause->setPrivates(Privates);
562 Clause->setLHSExprs(LHSExprs);
563 Clause->setRHSExprs(RHSExprs);
564 Clause->setReductionOps(ReductionOps);
565 Clause->setPreInitStmt(PreInit);
566 Clause->setPostUpdateExpr(PostUpdate);
570 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
572 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
573 return new (Mem) OMPTaskReductionClause(N);
576 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
577 assert(Privates.size() == varlist_size() &&
578 "Number of private copies is not the same as the preallocated buffer");
579 std::copy(Privates.begin(), Privates.end(), varlist_end());
582 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
584 LHSExprs.size() == varlist_size() &&
585 "Number of LHS expressions is not the same as the preallocated buffer");
586 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
589 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
591 RHSExprs.size() == varlist_size() &&
592 "Number of RHS expressions is not the same as the preallocated buffer");
593 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
596 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
597 assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
598 "expressions is not the same "
599 "as the preallocated buffer");
600 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
603 void OMPInReductionClause::setTaskgroupDescriptors(
604 ArrayRef<Expr *> TaskgroupDescriptors) {
605 assert(TaskgroupDescriptors.size() == varlist_size() &&
606 "Number of in reduction descriptors is not the same as the "
607 "preallocated buffer");
608 std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
609 getReductionOps().end());
612 OMPInReductionClause *OMPInReductionClause::Create(
613 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
614 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
615 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
616 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
617 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
618 ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
619 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
620 OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
621 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
622 Clause->setVarRefs(VL);
623 Clause->setPrivates(Privates);
624 Clause->setLHSExprs(LHSExprs);
625 Clause->setRHSExprs(RHSExprs);
626 Clause->setReductionOps(ReductionOps);
627 Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
628 Clause->setPreInitStmt(PreInit);
629 Clause->setPostUpdateExpr(PostUpdate);
633 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
635 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
636 return new (Mem) OMPInReductionClause(N);
639 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
640 SourceLocation StartLoc,
641 SourceLocation LParenLoc,
642 SourceLocation EndLoc,
643 ArrayRef<Expr *> VL) {
644 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
645 OMPFlushClause *Clause =
646 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
647 Clause->setVarRefs(VL);
651 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
652 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
653 return new (Mem) OMPFlushClause(N);
656 OMPDependClause *OMPDependClause::Create(
657 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
658 SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
659 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
660 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
661 OMPDependClause *Clause =
662 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
663 Clause->setVarRefs(VL);
664 Clause->setDependencyKind(DepKind);
665 Clause->setDependencyLoc(DepLoc);
666 Clause->setColonLoc(ColonLoc);
667 Clause->setCounterValue(nullptr);
671 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
672 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
673 return new (Mem) OMPDependClause(N);
676 void OMPDependClause::setCounterValue(Expr *V) {
677 assert(getDependencyKind() == OMPC_DEPEND_sink ||
678 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
679 *getVarRefs().end() = V;
682 const Expr *OMPDependClause::getCounterValue() const {
683 auto *V = *getVarRefs().end();
684 assert(getDependencyKind() == OMPC_DEPEND_sink ||
685 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
689 Expr *OMPDependClause::getCounterValue() {
690 auto *V = *getVarRefs().end();
691 assert(getDependencyKind() == OMPC_DEPEND_sink ||
692 getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
696 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
697 MappableExprComponentListsRef ComponentLists) {
698 unsigned TotalNum = 0u;
699 for (auto &C : ComponentLists)
700 TotalNum += C.size();
704 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
705 ArrayRef<ValueDecl *> Declarations) {
706 unsigned TotalNum = 0u;
707 llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
708 for (auto *D : Declarations) {
709 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
719 OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
720 SourceLocation LParenLoc, SourceLocation EndLoc,
721 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
722 MappableExprComponentListsRef ComponentLists,
723 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
724 bool TypeIsImplicit, SourceLocation TypeLoc) {
725 unsigned NumVars = Vars.size();
726 unsigned NumUniqueDeclarations =
727 getUniqueDeclarationsTotalNumber(Declarations);
728 unsigned NumComponentLists = ComponentLists.size();
729 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
731 // We need to allocate:
732 // NumVars x Expr* - we have an original list expression for each clause list
734 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
735 // with each component list.
736 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
737 // number of lists for each unique declaration and the size of each component
739 // NumComponents x MappableComponent - the total of all the components in all
741 void *Mem = C.Allocate(
742 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
743 OMPClauseMappableExprCommon::MappableComponent>(
744 NumVars, NumUniqueDeclarations,
745 NumUniqueDeclarations + NumComponentLists, NumComponents));
746 OMPMapClause *Clause = new (Mem) OMPMapClause(
747 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
748 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
750 Clause->setVarRefs(Vars);
751 Clause->setClauseInfo(Declarations, ComponentLists);
752 Clause->setMapTypeModifier(TypeModifier);
753 Clause->setMapType(Type);
754 Clause->setMapLoc(TypeLoc);
758 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
759 unsigned NumUniqueDeclarations,
760 unsigned NumComponentLists,
761 unsigned NumComponents) {
762 void *Mem = C.Allocate(
763 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
764 OMPClauseMappableExprCommon::MappableComponent>(
765 NumVars, NumUniqueDeclarations,
766 NumUniqueDeclarations + NumComponentLists, NumComponents));
767 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
768 NumComponentLists, NumComponents);
771 OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
772 SourceLocation LParenLoc,
773 SourceLocation EndLoc, ArrayRef<Expr *> Vars,
774 ArrayRef<ValueDecl *> Declarations,
775 MappableExprComponentListsRef ComponentLists) {
776 unsigned NumVars = Vars.size();
777 unsigned NumUniqueDeclarations =
778 getUniqueDeclarationsTotalNumber(Declarations);
779 unsigned NumComponentLists = ComponentLists.size();
780 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
782 // We need to allocate:
783 // NumVars x Expr* - we have an original list expression for each clause list
785 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
786 // with each component list.
787 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
788 // number of lists for each unique declaration and the size of each component
790 // NumComponents x MappableComponent - the total of all the components in all
792 void *Mem = C.Allocate(
793 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
794 OMPClauseMappableExprCommon::MappableComponent>(
795 NumVars, NumUniqueDeclarations,
796 NumUniqueDeclarations + NumComponentLists, NumComponents));
798 OMPToClause *Clause = new (Mem)
799 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
800 NumComponentLists, NumComponents);
802 Clause->setVarRefs(Vars);
803 Clause->setClauseInfo(Declarations, ComponentLists);
807 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
808 unsigned NumUniqueDeclarations,
809 unsigned NumComponentLists,
810 unsigned NumComponents) {
811 void *Mem = C.Allocate(
812 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
813 OMPClauseMappableExprCommon::MappableComponent>(
814 NumVars, NumUniqueDeclarations,
815 NumUniqueDeclarations + NumComponentLists, NumComponents));
816 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
817 NumComponentLists, NumComponents);
821 OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
822 SourceLocation LParenLoc, SourceLocation EndLoc,
823 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
824 MappableExprComponentListsRef ComponentLists) {
825 unsigned NumVars = Vars.size();
826 unsigned NumUniqueDeclarations =
827 getUniqueDeclarationsTotalNumber(Declarations);
828 unsigned NumComponentLists = ComponentLists.size();
829 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
831 // We need to allocate:
832 // NumVars x Expr* - we have an original list expression for each clause list
834 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
835 // with each component list.
836 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
837 // number of lists for each unique declaration and the size of each component
839 // NumComponents x MappableComponent - the total of all the components in all
841 void *Mem = C.Allocate(
842 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
843 OMPClauseMappableExprCommon::MappableComponent>(
844 NumVars, NumUniqueDeclarations,
845 NumUniqueDeclarations + NumComponentLists, NumComponents));
847 OMPFromClause *Clause = new (Mem)
848 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
849 NumComponentLists, NumComponents);
851 Clause->setVarRefs(Vars);
852 Clause->setClauseInfo(Declarations, ComponentLists);
856 OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
857 unsigned NumUniqueDeclarations,
858 unsigned NumComponentLists,
859 unsigned NumComponents) {
860 void *Mem = C.Allocate(
861 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
862 OMPClauseMappableExprCommon::MappableComponent>(
863 NumVars, NumUniqueDeclarations,
864 NumUniqueDeclarations + NumComponentLists, NumComponents));
865 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
866 NumComponentLists, NumComponents);
869 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
870 assert(VL.size() == varlist_size() &&
871 "Number of private copies is not the same as the preallocated buffer");
872 std::copy(VL.begin(), VL.end(), varlist_end());
875 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
876 assert(VL.size() == varlist_size() &&
877 "Number of inits is not the same as the preallocated buffer");
878 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
881 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
882 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
883 SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
884 ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
885 MappableExprComponentListsRef ComponentLists) {
886 unsigned NumVars = Vars.size();
887 unsigned NumUniqueDeclarations =
888 getUniqueDeclarationsTotalNumber(Declarations);
889 unsigned NumComponentLists = ComponentLists.size();
890 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
892 // We need to allocate:
893 // 3 x NumVars x Expr* - we have an original list expression for each clause
894 // list entry and an equal number of private copies and inits.
895 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
896 // with each component list.
897 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
898 // number of lists for each unique declaration and the size of each component
900 // NumComponents x MappableComponent - the total of all the components in all
902 void *Mem = C.Allocate(
903 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
904 OMPClauseMappableExprCommon::MappableComponent>(
905 3 * NumVars, NumUniqueDeclarations,
906 NumUniqueDeclarations + NumComponentLists, NumComponents));
908 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
909 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
910 NumComponentLists, NumComponents);
912 Clause->setVarRefs(Vars);
913 Clause->setPrivateCopies(PrivateVars);
914 Clause->setInits(Inits);
915 Clause->setClauseInfo(Declarations, ComponentLists);
919 OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
920 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
921 unsigned NumComponentLists, unsigned NumComponents) {
922 void *Mem = C.Allocate(
923 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
924 OMPClauseMappableExprCommon::MappableComponent>(
925 3 * NumVars, NumUniqueDeclarations,
926 NumUniqueDeclarations + NumComponentLists, NumComponents));
927 return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
928 NumComponentLists, NumComponents);
931 OMPIsDevicePtrClause *
932 OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
933 SourceLocation LParenLoc, SourceLocation EndLoc,
934 ArrayRef<Expr *> Vars,
935 ArrayRef<ValueDecl *> Declarations,
936 MappableExprComponentListsRef ComponentLists) {
937 unsigned NumVars = Vars.size();
938 unsigned NumUniqueDeclarations =
939 getUniqueDeclarationsTotalNumber(Declarations);
940 unsigned NumComponentLists = ComponentLists.size();
941 unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
943 // We need to allocate:
944 // NumVars x Expr* - we have an original list expression for each clause list
946 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
947 // with each component list.
948 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
949 // number of lists for each unique declaration and the size of each component
951 // NumComponents x MappableComponent - the total of all the components in all
953 void *Mem = C.Allocate(
954 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
955 OMPClauseMappableExprCommon::MappableComponent>(
956 NumVars, NumUniqueDeclarations,
957 NumUniqueDeclarations + NumComponentLists, NumComponents));
959 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
960 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
961 NumComponentLists, NumComponents);
963 Clause->setVarRefs(Vars);
964 Clause->setClauseInfo(Declarations, ComponentLists);
968 OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
969 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
970 unsigned NumComponentLists, unsigned NumComponents) {
971 void *Mem = C.Allocate(
972 totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
973 OMPClauseMappableExprCommon::MappableComponent>(
974 NumVars, NumUniqueDeclarations,
975 NumUniqueDeclarations + NumComponentLists, NumComponents));
976 return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
977 NumComponentLists, NumComponents);