OpenMPClause.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. //===- OpenMPClause.cpp - Classes for OpenMP clauses ----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the subclesses of Stmt class declared in OpenMPClause.h
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/OpenMPClause.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/Basic/LLVM.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/Support/Casting.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include <algorithm>
  21. #include <cassert>
  22. using namespace clang;
  23. OMPClause::child_range OMPClause::children() {
  24. switch (getClauseKind()) {
  25. default:
  26. break;
  27. #define OPENMP_CLAUSE(Name, Class) \
  28. case OMPC_##Name: \
  29. return static_cast<Class *>(this)->children();
  30. #include "clang/Basic/OpenMPKinds.def"
  31. }
  32. llvm_unreachable("unknown OMPClause");
  33. }
  34. OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
  35. auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
  36. return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
  37. }
  38. const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
  39. switch (C->getClauseKind()) {
  40. case OMPC_schedule:
  41. return static_cast<const OMPScheduleClause *>(C);
  42. case OMPC_dist_schedule:
  43. return static_cast<const OMPDistScheduleClause *>(C);
  44. case OMPC_firstprivate:
  45. return static_cast<const OMPFirstprivateClause *>(C);
  46. case OMPC_lastprivate:
  47. return static_cast<const OMPLastprivateClause *>(C);
  48. case OMPC_reduction:
  49. return static_cast<const OMPReductionClause *>(C);
  50. case OMPC_task_reduction:
  51. return static_cast<const OMPTaskReductionClause *>(C);
  52. case OMPC_in_reduction:
  53. return static_cast<const OMPInReductionClause *>(C);
  54. case OMPC_linear:
  55. return static_cast<const OMPLinearClause *>(C);
  56. case OMPC_if:
  57. return static_cast<const OMPIfClause *>(C);
  58. case OMPC_num_threads:
  59. return static_cast<const OMPNumThreadsClause *>(C);
  60. case OMPC_num_teams:
  61. return static_cast<const OMPNumTeamsClause *>(C);
  62. case OMPC_thread_limit:
  63. return static_cast<const OMPThreadLimitClause *>(C);
  64. case OMPC_device:
  65. return static_cast<const OMPDeviceClause *>(C);
  66. case OMPC_default:
  67. case OMPC_proc_bind:
  68. case OMPC_final:
  69. case OMPC_safelen:
  70. case OMPC_simdlen:
  71. case OMPC_collapse:
  72. case OMPC_private:
  73. case OMPC_shared:
  74. case OMPC_aligned:
  75. case OMPC_copyin:
  76. case OMPC_copyprivate:
  77. case OMPC_ordered:
  78. case OMPC_nowait:
  79. case OMPC_untied:
  80. case OMPC_mergeable:
  81. case OMPC_threadprivate:
  82. case OMPC_flush:
  83. case OMPC_read:
  84. case OMPC_write:
  85. case OMPC_update:
  86. case OMPC_capture:
  87. case OMPC_seq_cst:
  88. case OMPC_depend:
  89. case OMPC_threads:
  90. case OMPC_simd:
  91. case OMPC_map:
  92. case OMPC_priority:
  93. case OMPC_grainsize:
  94. case OMPC_nogroup:
  95. case OMPC_num_tasks:
  96. case OMPC_hint:
  97. case OMPC_defaultmap:
  98. case OMPC_unknown:
  99. case OMPC_uniform:
  100. case OMPC_to:
  101. case OMPC_from:
  102. case OMPC_use_device_ptr:
  103. case OMPC_is_device_ptr:
  104. case OMPC_unified_address:
  105. break;
  106. }
  107. return nullptr;
  108. }
  109. OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
  110. auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
  111. return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
  112. }
  113. const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
  114. switch (C->getClauseKind()) {
  115. case OMPC_lastprivate:
  116. return static_cast<const OMPLastprivateClause *>(C);
  117. case OMPC_reduction:
  118. return static_cast<const OMPReductionClause *>(C);
  119. case OMPC_task_reduction:
  120. return static_cast<const OMPTaskReductionClause *>(C);
  121. case OMPC_in_reduction:
  122. return static_cast<const OMPInReductionClause *>(C);
  123. case OMPC_linear:
  124. return static_cast<const OMPLinearClause *>(C);
  125. case OMPC_schedule:
  126. case OMPC_dist_schedule:
  127. case OMPC_firstprivate:
  128. case OMPC_default:
  129. case OMPC_proc_bind:
  130. case OMPC_if:
  131. case OMPC_final:
  132. case OMPC_num_threads:
  133. case OMPC_safelen:
  134. case OMPC_simdlen:
  135. case OMPC_collapse:
  136. case OMPC_private:
  137. case OMPC_shared:
  138. case OMPC_aligned:
  139. case OMPC_copyin:
  140. case OMPC_copyprivate:
  141. case OMPC_ordered:
  142. case OMPC_nowait:
  143. case OMPC_untied:
  144. case OMPC_mergeable:
  145. case OMPC_threadprivate:
  146. case OMPC_flush:
  147. case OMPC_read:
  148. case OMPC_write:
  149. case OMPC_update:
  150. case OMPC_capture:
  151. case OMPC_seq_cst:
  152. case OMPC_depend:
  153. case OMPC_device:
  154. case OMPC_threads:
  155. case OMPC_simd:
  156. case OMPC_map:
  157. case OMPC_num_teams:
  158. case OMPC_thread_limit:
  159. case OMPC_priority:
  160. case OMPC_grainsize:
  161. case OMPC_nogroup:
  162. case OMPC_num_tasks:
  163. case OMPC_hint:
  164. case OMPC_defaultmap:
  165. case OMPC_unknown:
  166. case OMPC_uniform:
  167. case OMPC_to:
  168. case OMPC_from:
  169. case OMPC_use_device_ptr:
  170. case OMPC_is_device_ptr:
  171. case OMPC_unified_address:
  172. break;
  173. }
  174. return nullptr;
  175. }
  176. OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
  177. unsigned NumLoops,
  178. SourceLocation StartLoc,
  179. SourceLocation LParenLoc,
  180. SourceLocation EndLoc) {
  181. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
  182. auto *Clause =
  183. new (Mem) OMPOrderedClause(Num, NumLoops, StartLoc, LParenLoc, EndLoc);
  184. for (unsigned I = 0; I < NumLoops; ++I) {
  185. Clause->setLoopNumIterations(I, nullptr);
  186. Clause->setLoopCounter(I, nullptr);
  187. }
  188. return Clause;
  189. }
  190. OMPOrderedClause *OMPOrderedClause::CreateEmpty(const ASTContext &C,
  191. unsigned NumLoops) {
  192. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
  193. auto *Clause = new (Mem) OMPOrderedClause(NumLoops);
  194. for (unsigned I = 0; I < NumLoops; ++I) {
  195. Clause->setLoopNumIterations(I, nullptr);
  196. Clause->setLoopCounter(I, nullptr);
  197. }
  198. return Clause;
  199. }
  200. void OMPOrderedClause::setLoopNumIterations(unsigned NumLoop,
  201. Expr *NumIterations) {
  202. assert(NumLoop < NumberOfLoops && "out of loops number.");
  203. getTrailingObjects<Expr *>()[NumLoop] = NumIterations;
  204. }
  205. ArrayRef<Expr *> OMPOrderedClause::getLoopNumIterations() const {
  206. return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumberOfLoops);
  207. }
  208. void OMPOrderedClause::setLoopCounter(unsigned NumLoop, Expr *Counter) {
  209. assert(NumLoop < NumberOfLoops && "out of loops number.");
  210. getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop] = Counter;
  211. }
  212. Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) {
  213. assert(NumLoop < NumberOfLoops && "out of loops number.");
  214. return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
  215. }
  216. const Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) const {
  217. assert(NumLoop < NumberOfLoops && "out of loops number.");
  218. return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
  219. }
  220. void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
  221. assert(VL.size() == varlist_size() &&
  222. "Number of private copies is not the same as the preallocated buffer");
  223. std::copy(VL.begin(), VL.end(), varlist_end());
  224. }
  225. OMPPrivateClause *
  226. OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
  227. SourceLocation LParenLoc, SourceLocation EndLoc,
  228. ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
  229. // Allocate space for private variables and initializer expressions.
  230. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
  231. OMPPrivateClause *Clause =
  232. new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  233. Clause->setVarRefs(VL);
  234. Clause->setPrivateCopies(PrivateVL);
  235. return Clause;
  236. }
  237. OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
  238. unsigned N) {
  239. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
  240. return new (Mem) OMPPrivateClause(N);
  241. }
  242. void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
  243. assert(VL.size() == varlist_size() &&
  244. "Number of private copies is not the same as the preallocated buffer");
  245. std::copy(VL.begin(), VL.end(), varlist_end());
  246. }
  247. void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
  248. assert(VL.size() == varlist_size() &&
  249. "Number of inits is not the same as the preallocated buffer");
  250. std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
  251. }
  252. OMPFirstprivateClause *
  253. OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
  254. SourceLocation LParenLoc, SourceLocation EndLoc,
  255. ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
  256. ArrayRef<Expr *> InitVL, Stmt *PreInit) {
  257. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
  258. OMPFirstprivateClause *Clause =
  259. new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  260. Clause->setVarRefs(VL);
  261. Clause->setPrivateCopies(PrivateVL);
  262. Clause->setInits(InitVL);
  263. Clause->setPreInitStmt(PreInit);
  264. return Clause;
  265. }
  266. OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
  267. unsigned N) {
  268. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
  269. return new (Mem) OMPFirstprivateClause(N);
  270. }
  271. void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
  272. assert(PrivateCopies.size() == varlist_size() &&
  273. "Number of private copies is not the same as the preallocated buffer");
  274. std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
  275. }
  276. void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
  277. assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
  278. "not the same as the "
  279. "preallocated buffer");
  280. std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
  281. }
  282. void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
  283. assert(DstExprs.size() == varlist_size() && "Number of destination "
  284. "expressions is not the same as "
  285. "the preallocated buffer");
  286. std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
  287. }
  288. void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
  289. assert(AssignmentOps.size() == varlist_size() &&
  290. "Number of assignment expressions is not the same as the preallocated "
  291. "buffer");
  292. std::copy(AssignmentOps.begin(), AssignmentOps.end(),
  293. getDestinationExprs().end());
  294. }
  295. OMPLastprivateClause *OMPLastprivateClause::Create(
  296. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  297. SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
  298. ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
  299. Expr *PostUpdate) {
  300. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
  301. OMPLastprivateClause *Clause =
  302. new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  303. Clause->setVarRefs(VL);
  304. Clause->setSourceExprs(SrcExprs);
  305. Clause->setDestinationExprs(DstExprs);
  306. Clause->setAssignmentOps(AssignmentOps);
  307. Clause->setPreInitStmt(PreInit);
  308. Clause->setPostUpdateExpr(PostUpdate);
  309. return Clause;
  310. }
  311. OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
  312. unsigned N) {
  313. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
  314. return new (Mem) OMPLastprivateClause(N);
  315. }
  316. OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
  317. SourceLocation StartLoc,
  318. SourceLocation LParenLoc,
  319. SourceLocation EndLoc,
  320. ArrayRef<Expr *> VL) {
  321. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
  322. OMPSharedClause *Clause =
  323. new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
  324. Clause->setVarRefs(VL);
  325. return Clause;
  326. }
  327. OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
  328. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
  329. return new (Mem) OMPSharedClause(N);
  330. }
  331. void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
  332. assert(PL.size() == varlist_size() &&
  333. "Number of privates is not the same as the preallocated buffer");
  334. std::copy(PL.begin(), PL.end(), varlist_end());
  335. }
  336. void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
  337. assert(IL.size() == varlist_size() &&
  338. "Number of inits is not the same as the preallocated buffer");
  339. std::copy(IL.begin(), IL.end(), getPrivates().end());
  340. }
  341. void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
  342. assert(UL.size() == varlist_size() &&
  343. "Number of updates is not the same as the preallocated buffer");
  344. std::copy(UL.begin(), UL.end(), getInits().end());
  345. }
  346. void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
  347. assert(FL.size() == varlist_size() &&
  348. "Number of final updates is not the same as the preallocated buffer");
  349. std::copy(FL.begin(), FL.end(), getUpdates().end());
  350. }
  351. OMPLinearClause *OMPLinearClause::Create(
  352. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  353. OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
  354. SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
  355. ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
  356. Stmt *PreInit, Expr *PostUpdate) {
  357. // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
  358. // (Step and CalcStep).
  359. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
  360. OMPLinearClause *Clause = new (Mem) OMPLinearClause(
  361. StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
  362. Clause->setVarRefs(VL);
  363. Clause->setPrivates(PL);
  364. Clause->setInits(IL);
  365. // Fill update and final expressions with zeroes, they are provided later,
  366. // after the directive construction.
  367. std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
  368. nullptr);
  369. std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
  370. nullptr);
  371. Clause->setStep(Step);
  372. Clause->setCalcStep(CalcStep);
  373. Clause->setPreInitStmt(PreInit);
  374. Clause->setPostUpdateExpr(PostUpdate);
  375. return Clause;
  376. }
  377. OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
  378. unsigned NumVars) {
  379. // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
  380. // (Step and CalcStep).
  381. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
  382. return new (Mem) OMPLinearClause(NumVars);
  383. }
  384. OMPAlignedClause *
  385. OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
  386. SourceLocation LParenLoc, SourceLocation ColonLoc,
  387. SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
  388. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
  389. OMPAlignedClause *Clause = new (Mem)
  390. OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
  391. Clause->setVarRefs(VL);
  392. Clause->setAlignment(A);
  393. return Clause;
  394. }
  395. OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
  396. unsigned NumVars) {
  397. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
  398. return new (Mem) OMPAlignedClause(NumVars);
  399. }
  400. void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
  401. assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
  402. "not the same as the "
  403. "preallocated buffer");
  404. std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
  405. }
  406. void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
  407. assert(DstExprs.size() == varlist_size() && "Number of destination "
  408. "expressions is not the same as "
  409. "the preallocated buffer");
  410. std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
  411. }
  412. void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
  413. assert(AssignmentOps.size() == varlist_size() &&
  414. "Number of assignment expressions is not the same as the preallocated "
  415. "buffer");
  416. std::copy(AssignmentOps.begin(), AssignmentOps.end(),
  417. getDestinationExprs().end());
  418. }
  419. OMPCopyinClause *OMPCopyinClause::Create(
  420. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  421. SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
  422. ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
  423. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
  424. OMPCopyinClause *Clause =
  425. new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
  426. Clause->setVarRefs(VL);
  427. Clause->setSourceExprs(SrcExprs);
  428. Clause->setDestinationExprs(DstExprs);
  429. Clause->setAssignmentOps(AssignmentOps);
  430. return Clause;
  431. }
  432. OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
  433. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
  434. return new (Mem) OMPCopyinClause(N);
  435. }
  436. void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
  437. assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
  438. "not the same as the "
  439. "preallocated buffer");
  440. std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
  441. }
  442. void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
  443. assert(DstExprs.size() == varlist_size() && "Number of destination "
  444. "expressions is not the same as "
  445. "the preallocated buffer");
  446. std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
  447. }
  448. void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
  449. assert(AssignmentOps.size() == varlist_size() &&
  450. "Number of assignment expressions is not the same as the preallocated "
  451. "buffer");
  452. std::copy(AssignmentOps.begin(), AssignmentOps.end(),
  453. getDestinationExprs().end());
  454. }
  455. OMPCopyprivateClause *OMPCopyprivateClause::Create(
  456. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  457. SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
  458. ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
  459. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
  460. OMPCopyprivateClause *Clause =
  461. new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
  462. Clause->setVarRefs(VL);
  463. Clause->setSourceExprs(SrcExprs);
  464. Clause->setDestinationExprs(DstExprs);
  465. Clause->setAssignmentOps(AssignmentOps);
  466. return Clause;
  467. }
  468. OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
  469. unsigned N) {
  470. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
  471. return new (Mem) OMPCopyprivateClause(N);
  472. }
  473. void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
  474. assert(Privates.size() == varlist_size() &&
  475. "Number of private copies is not the same as the preallocated buffer");
  476. std::copy(Privates.begin(), Privates.end(), varlist_end());
  477. }
  478. void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
  479. assert(
  480. LHSExprs.size() == varlist_size() &&
  481. "Number of LHS expressions is not the same as the preallocated buffer");
  482. std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
  483. }
  484. void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
  485. assert(
  486. RHSExprs.size() == varlist_size() &&
  487. "Number of RHS expressions is not the same as the preallocated buffer");
  488. std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
  489. }
  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());
  495. }
  496. OMPReductionClause *OMPReductionClause::Create(
  497. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  498. SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
  499. NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
  500. ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
  501. ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
  502. Expr *PostUpdate) {
  503. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
  504. OMPReductionClause *Clause = new (Mem) OMPReductionClause(
  505. StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
  506. Clause->setVarRefs(VL);
  507. Clause->setPrivates(Privates);
  508. Clause->setLHSExprs(LHSExprs);
  509. Clause->setRHSExprs(RHSExprs);
  510. Clause->setReductionOps(ReductionOps);
  511. Clause->setPreInitStmt(PreInit);
  512. Clause->setPostUpdateExpr(PostUpdate);
  513. return Clause;
  514. }
  515. OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
  516. unsigned N) {
  517. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
  518. return new (Mem) OMPReductionClause(N);
  519. }
  520. void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
  521. assert(Privates.size() == varlist_size() &&
  522. "Number of private copies is not the same as the preallocated buffer");
  523. std::copy(Privates.begin(), Privates.end(), varlist_end());
  524. }
  525. void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
  526. assert(
  527. LHSExprs.size() == varlist_size() &&
  528. "Number of LHS expressions is not the same as the preallocated buffer");
  529. std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
  530. }
  531. void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
  532. assert(
  533. RHSExprs.size() == varlist_size() &&
  534. "Number of RHS expressions is not the same as the preallocated buffer");
  535. std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
  536. }
  537. void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
  538. assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
  539. "expressions is not the same "
  540. "as the preallocated buffer");
  541. std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
  542. }
  543. OMPTaskReductionClause *OMPTaskReductionClause::Create(
  544. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  545. SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
  546. NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
  547. ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
  548. ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
  549. Expr *PostUpdate) {
  550. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
  551. OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
  552. StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
  553. Clause->setVarRefs(VL);
  554. Clause->setPrivates(Privates);
  555. Clause->setLHSExprs(LHSExprs);
  556. Clause->setRHSExprs(RHSExprs);
  557. Clause->setReductionOps(ReductionOps);
  558. Clause->setPreInitStmt(PreInit);
  559. Clause->setPostUpdateExpr(PostUpdate);
  560. return Clause;
  561. }
  562. OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
  563. unsigned N) {
  564. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
  565. return new (Mem) OMPTaskReductionClause(N);
  566. }
  567. void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
  568. assert(Privates.size() == varlist_size() &&
  569. "Number of private copies is not the same as the preallocated buffer");
  570. std::copy(Privates.begin(), Privates.end(), varlist_end());
  571. }
  572. void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
  573. assert(
  574. LHSExprs.size() == varlist_size() &&
  575. "Number of LHS expressions is not the same as the preallocated buffer");
  576. std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
  577. }
  578. void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
  579. assert(
  580. RHSExprs.size() == varlist_size() &&
  581. "Number of RHS expressions is not the same as the preallocated buffer");
  582. std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
  583. }
  584. void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
  585. assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
  586. "expressions is not the same "
  587. "as the preallocated buffer");
  588. std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
  589. }
  590. void OMPInReductionClause::setTaskgroupDescriptors(
  591. ArrayRef<Expr *> TaskgroupDescriptors) {
  592. assert(TaskgroupDescriptors.size() == varlist_size() &&
  593. "Number of in reduction descriptors is not the same as the "
  594. "preallocated buffer");
  595. std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
  596. getReductionOps().end());
  597. }
  598. OMPInReductionClause *OMPInReductionClause::Create(
  599. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  600. SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
  601. NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
  602. ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
  603. ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
  604. ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
  605. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
  606. OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
  607. StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
  608. Clause->setVarRefs(VL);
  609. Clause->setPrivates(Privates);
  610. Clause->setLHSExprs(LHSExprs);
  611. Clause->setRHSExprs(RHSExprs);
  612. Clause->setReductionOps(ReductionOps);
  613. Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
  614. Clause->setPreInitStmt(PreInit);
  615. Clause->setPostUpdateExpr(PostUpdate);
  616. return Clause;
  617. }
  618. OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
  619. unsigned N) {
  620. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
  621. return new (Mem) OMPInReductionClause(N);
  622. }
  623. OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
  624. SourceLocation StartLoc,
  625. SourceLocation LParenLoc,
  626. SourceLocation EndLoc,
  627. ArrayRef<Expr *> VL) {
  628. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
  629. OMPFlushClause *Clause =
  630. new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
  631. Clause->setVarRefs(VL);
  632. return Clause;
  633. }
  634. OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
  635. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
  636. return new (Mem) OMPFlushClause(N);
  637. }
  638. OMPDependClause *
  639. OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
  640. SourceLocation LParenLoc, SourceLocation EndLoc,
  641. OpenMPDependClauseKind DepKind, SourceLocation DepLoc,
  642. SourceLocation ColonLoc, ArrayRef<Expr *> VL,
  643. unsigned NumLoops) {
  644. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + NumLoops));
  645. OMPDependClause *Clause = new (Mem)
  646. OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
  647. Clause->setVarRefs(VL);
  648. Clause->setDependencyKind(DepKind);
  649. Clause->setDependencyLoc(DepLoc);
  650. Clause->setColonLoc(ColonLoc);
  651. for (unsigned I = 0 ; I < NumLoops; ++I)
  652. Clause->setLoopData(I, nullptr);
  653. return Clause;
  654. }
  655. OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N,
  656. unsigned NumLoops) {
  657. void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + NumLoops));
  658. return new (Mem) OMPDependClause(N, NumLoops);
  659. }
  660. void OMPDependClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
  661. assert((getDependencyKind() == OMPC_DEPEND_sink ||
  662. getDependencyKind() == OMPC_DEPEND_source) &&
  663. NumLoop < NumLoops &&
  664. "Expected sink or source depend + loop index must be less number of "
  665. "loops.");
  666. auto It = std::next(getVarRefs().end(), NumLoop);
  667. *It = Cnt;
  668. }
  669. Expr *OMPDependClause::getLoopData(unsigned NumLoop) {
  670. assert((getDependencyKind() == OMPC_DEPEND_sink ||
  671. getDependencyKind() == OMPC_DEPEND_source) &&
  672. NumLoop < NumLoops &&
  673. "Expected sink or source depend + loop index must be less number of "
  674. "loops.");
  675. auto It = std::next(getVarRefs().end(), NumLoop);
  676. return *It;
  677. }
  678. const Expr *OMPDependClause::getLoopData(unsigned NumLoop) const {
  679. assert((getDependencyKind() == OMPC_DEPEND_sink ||
  680. getDependencyKind() == OMPC_DEPEND_source) &&
  681. NumLoop < NumLoops &&
  682. "Expected sink or source depend + loop index must be less number of "
  683. "loops.");
  684. auto It = std::next(getVarRefs().end(), NumLoop);
  685. return *It;
  686. }
  687. unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
  688. MappableExprComponentListsRef ComponentLists) {
  689. unsigned TotalNum = 0u;
  690. for (auto &C : ComponentLists)
  691. TotalNum += C.size();
  692. return TotalNum;
  693. }
  694. unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
  695. ArrayRef<const ValueDecl *> Declarations) {
  696. unsigned TotalNum = 0u;
  697. llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
  698. for (const ValueDecl *D : Declarations) {
  699. const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
  700. if (Cache.count(VD))
  701. continue;
  702. ++TotalNum;
  703. Cache.insert(VD);
  704. }
  705. return TotalNum;
  706. }
  707. OMPMapClause *
  708. OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
  709. SourceLocation LParenLoc, SourceLocation EndLoc,
  710. ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
  711. MappableExprComponentListsRef ComponentLists,
  712. OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
  713. bool TypeIsImplicit, SourceLocation TypeLoc) {
  714. unsigned NumVars = Vars.size();
  715. unsigned NumUniqueDeclarations =
  716. getUniqueDeclarationsTotalNumber(Declarations);
  717. unsigned NumComponentLists = ComponentLists.size();
  718. unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
  719. // We need to allocate:
  720. // NumVars x Expr* - we have an original list expression for each clause list
  721. // entry.
  722. // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
  723. // with each component list.
  724. // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
  725. // number of lists for each unique declaration and the size of each component
  726. // list.
  727. // NumComponents x MappableComponent - the total of all the components in all
  728. // the lists.
  729. void *Mem = C.Allocate(
  730. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  731. OMPClauseMappableExprCommon::MappableComponent>(
  732. NumVars, NumUniqueDeclarations,
  733. NumUniqueDeclarations + NumComponentLists, NumComponents));
  734. OMPMapClause *Clause = new (Mem) OMPMapClause(
  735. TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
  736. NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
  737. Clause->setVarRefs(Vars);
  738. Clause->setClauseInfo(Declarations, ComponentLists);
  739. Clause->setMapTypeModifier(TypeModifier);
  740. Clause->setMapType(Type);
  741. Clause->setMapLoc(TypeLoc);
  742. return Clause;
  743. }
  744. OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
  745. unsigned NumUniqueDeclarations,
  746. unsigned NumComponentLists,
  747. unsigned NumComponents) {
  748. void *Mem = C.Allocate(
  749. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  750. OMPClauseMappableExprCommon::MappableComponent>(
  751. NumVars, NumUniqueDeclarations,
  752. NumUniqueDeclarations + NumComponentLists, NumComponents));
  753. return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
  754. NumComponentLists, NumComponents);
  755. }
  756. OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
  757. SourceLocation LParenLoc,
  758. SourceLocation EndLoc, ArrayRef<Expr *> Vars,
  759. ArrayRef<ValueDecl *> Declarations,
  760. MappableExprComponentListsRef ComponentLists) {
  761. unsigned NumVars = Vars.size();
  762. unsigned NumUniqueDeclarations =
  763. getUniqueDeclarationsTotalNumber(Declarations);
  764. unsigned NumComponentLists = ComponentLists.size();
  765. unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
  766. // We need to allocate:
  767. // NumVars x Expr* - we have an original list expression for each clause list
  768. // entry.
  769. // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
  770. // with each component list.
  771. // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
  772. // number of lists for each unique declaration and the size of each component
  773. // list.
  774. // NumComponents x MappableComponent - the total of all the components in all
  775. // the lists.
  776. void *Mem = C.Allocate(
  777. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  778. OMPClauseMappableExprCommon::MappableComponent>(
  779. NumVars, NumUniqueDeclarations,
  780. NumUniqueDeclarations + NumComponentLists, NumComponents));
  781. OMPToClause *Clause = new (Mem)
  782. OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
  783. NumComponentLists, NumComponents);
  784. Clause->setVarRefs(Vars);
  785. Clause->setClauseInfo(Declarations, ComponentLists);
  786. return Clause;
  787. }
  788. OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
  789. unsigned NumUniqueDeclarations,
  790. unsigned NumComponentLists,
  791. unsigned NumComponents) {
  792. void *Mem = C.Allocate(
  793. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  794. OMPClauseMappableExprCommon::MappableComponent>(
  795. NumVars, NumUniqueDeclarations,
  796. NumUniqueDeclarations + NumComponentLists, NumComponents));
  797. return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
  798. NumComponentLists, NumComponents);
  799. }
  800. OMPFromClause *
  801. OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
  802. SourceLocation LParenLoc, SourceLocation EndLoc,
  803. ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
  804. MappableExprComponentListsRef ComponentLists) {
  805. unsigned NumVars = Vars.size();
  806. unsigned NumUniqueDeclarations =
  807. getUniqueDeclarationsTotalNumber(Declarations);
  808. unsigned NumComponentLists = ComponentLists.size();
  809. unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
  810. // We need to allocate:
  811. // NumVars x Expr* - we have an original list expression for each clause list
  812. // entry.
  813. // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
  814. // with each component list.
  815. // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
  816. // number of lists for each unique declaration and the size of each component
  817. // list.
  818. // NumComponents x MappableComponent - the total of all the components in all
  819. // the lists.
  820. void *Mem = C.Allocate(
  821. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  822. OMPClauseMappableExprCommon::MappableComponent>(
  823. NumVars, NumUniqueDeclarations,
  824. NumUniqueDeclarations + NumComponentLists, NumComponents));
  825. OMPFromClause *Clause = new (Mem)
  826. OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
  827. NumComponentLists, NumComponents);
  828. Clause->setVarRefs(Vars);
  829. Clause->setClauseInfo(Declarations, ComponentLists);
  830. return Clause;
  831. }
  832. OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
  833. unsigned NumUniqueDeclarations,
  834. unsigned NumComponentLists,
  835. unsigned NumComponents) {
  836. void *Mem = C.Allocate(
  837. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  838. OMPClauseMappableExprCommon::MappableComponent>(
  839. NumVars, NumUniqueDeclarations,
  840. NumUniqueDeclarations + NumComponentLists, NumComponents));
  841. return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
  842. NumComponentLists, NumComponents);
  843. }
  844. void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
  845. assert(VL.size() == varlist_size() &&
  846. "Number of private copies is not the same as the preallocated buffer");
  847. std::copy(VL.begin(), VL.end(), varlist_end());
  848. }
  849. void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
  850. assert(VL.size() == varlist_size() &&
  851. "Number of inits is not the same as the preallocated buffer");
  852. std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
  853. }
  854. OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
  855. const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
  856. SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
  857. ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
  858. MappableExprComponentListsRef ComponentLists) {
  859. unsigned NumVars = Vars.size();
  860. unsigned NumUniqueDeclarations =
  861. getUniqueDeclarationsTotalNumber(Declarations);
  862. unsigned NumComponentLists = ComponentLists.size();
  863. unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
  864. // We need to allocate:
  865. // 3 x NumVars x Expr* - we have an original list expression for each clause
  866. // list entry and an equal number of private copies and inits.
  867. // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
  868. // with each component list.
  869. // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
  870. // number of lists for each unique declaration and the size of each component
  871. // list.
  872. // NumComponents x MappableComponent - the total of all the components in all
  873. // the lists.
  874. void *Mem = C.Allocate(
  875. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  876. OMPClauseMappableExprCommon::MappableComponent>(
  877. 3 * NumVars, NumUniqueDeclarations,
  878. NumUniqueDeclarations + NumComponentLists, NumComponents));
  879. OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
  880. StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
  881. NumComponentLists, NumComponents);
  882. Clause->setVarRefs(Vars);
  883. Clause->setPrivateCopies(PrivateVars);
  884. Clause->setInits(Inits);
  885. Clause->setClauseInfo(Declarations, ComponentLists);
  886. return Clause;
  887. }
  888. OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
  889. const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
  890. unsigned NumComponentLists, unsigned NumComponents) {
  891. void *Mem = C.Allocate(
  892. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  893. OMPClauseMappableExprCommon::MappableComponent>(
  894. 3 * NumVars, NumUniqueDeclarations,
  895. NumUniqueDeclarations + NumComponentLists, NumComponents));
  896. return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
  897. NumComponentLists, NumComponents);
  898. }
  899. OMPIsDevicePtrClause *
  900. OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
  901. SourceLocation LParenLoc, SourceLocation EndLoc,
  902. ArrayRef<Expr *> Vars,
  903. ArrayRef<ValueDecl *> Declarations,
  904. MappableExprComponentListsRef ComponentLists) {
  905. unsigned NumVars = Vars.size();
  906. unsigned NumUniqueDeclarations =
  907. getUniqueDeclarationsTotalNumber(Declarations);
  908. unsigned NumComponentLists = ComponentLists.size();
  909. unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
  910. // We need to allocate:
  911. // NumVars x Expr* - we have an original list expression for each clause list
  912. // entry.
  913. // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
  914. // with each component list.
  915. // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
  916. // number of lists for each unique declaration and the size of each component
  917. // list.
  918. // NumComponents x MappableComponent - the total of all the components in all
  919. // the lists.
  920. void *Mem = C.Allocate(
  921. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  922. OMPClauseMappableExprCommon::MappableComponent>(
  923. NumVars, NumUniqueDeclarations,
  924. NumUniqueDeclarations + NumComponentLists, NumComponents));
  925. OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
  926. StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
  927. NumComponentLists, NumComponents);
  928. Clause->setVarRefs(Vars);
  929. Clause->setClauseInfo(Declarations, ComponentLists);
  930. return Clause;
  931. }
  932. OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
  933. const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
  934. unsigned NumComponentLists, unsigned NumComponents) {
  935. void *Mem = C.Allocate(
  936. totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
  937. OMPClauseMappableExprCommon::MappableComponent>(
  938. NumVars, NumUniqueDeclarations,
  939. NumUniqueDeclarations + NumComponentLists, NumComponents));
  940. return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
  941. NumComponentLists, NumComponents);
  942. }