gpu_group.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*
  2. * Copyright 2010-2011 INRIA Saclay
  3. * Copyright 2012-2014 Ecole Normale Superieure
  4. * Copyright 2015 Sven Verdoolaege
  5. *
  6. * Use of this software is governed by the MIT license
  7. *
  8. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  9. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  10. * 91893 Orsay, France
  11. * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  12. */
  13. #include <isl/constraint.h>
  14. #include <isl/ilp.h>
  15. #include "gpu_array_tile.h"
  16. #include "gpu_group.h"
  17. #include "gpu_tree.h"
  18. #include "schedule.h"
  19. /* Print the name of the local copy of a given group of array references.
  20. */
  21. __isl_give isl_printer *gpu_array_ref_group_print_name(
  22. struct gpu_array_ref_group *group, __isl_take isl_printer *p)
  23. {
  24. int global = 0;
  25. enum ppcg_group_access_type type;
  26. type = gpu_array_ref_group_type(group);
  27. if (type == ppcg_access_private)
  28. p = isl_printer_print_str(p, "private_");
  29. else if (type == ppcg_access_shared)
  30. p = isl_printer_print_str(p, "shared_");
  31. else
  32. global = 1;
  33. p = isl_printer_print_str(p, group->array->name);
  34. if (!global && group->local_array->n_group > 1) {
  35. p = isl_printer_print_str(p, "_");
  36. p = isl_printer_print_int(p, group->nr);
  37. }
  38. return p;
  39. }
  40. /* Return the union of all read (read = 1) and/or write (write = 1)
  41. * access relations in the group.
  42. */
  43. __isl_give isl_union_map *gpu_array_ref_group_access_relation(
  44. struct gpu_array_ref_group *group, int read, int write)
  45. {
  46. int i;
  47. isl_union_map *access;
  48. access = isl_union_map_empty(isl_map_get_space(group->access));
  49. for (i = 0; i < group->n_ref; ++i) {
  50. isl_map *map_i;
  51. if (!((read && group->refs[i]->read) ||
  52. (write && group->refs[i]->write)))
  53. continue;
  54. map_i = isl_map_copy(group->refs[i]->access);
  55. access = isl_union_map_union(access,
  56. isl_union_map_from_map(map_i));
  57. }
  58. return access;
  59. }
  60. /* Should this array reference group be mapped to private, shared or global
  61. * memory?
  62. * If we have computed both a private and a shared tile, then
  63. * the tile with the smallest depth is used. If both have the same depth,
  64. * then the private tile is used.
  65. */
  66. enum ppcg_group_access_type gpu_array_ref_group_type(
  67. struct gpu_array_ref_group *group)
  68. {
  69. if (group->private_tile && group->shared_tile &&
  70. group->shared_tile->depth < group->private_tile->depth)
  71. return ppcg_access_shared;
  72. if (group->private_tile)
  73. return ppcg_access_private;
  74. if (group->shared_tile)
  75. return ppcg_access_shared;
  76. return ppcg_access_global;
  77. }
  78. /* Return the effective gpu_array_tile associated to "group" or
  79. * NULL if there is no such gpu_array_tile.
  80. */
  81. struct gpu_array_tile *gpu_array_ref_group_tile(
  82. struct gpu_array_ref_group *group)
  83. {
  84. switch (gpu_array_ref_group_type(group)) {
  85. case ppcg_access_global:
  86. return NULL;
  87. case ppcg_access_shared:
  88. return group->shared_tile;
  89. case ppcg_access_private:
  90. return group->private_tile;
  91. }
  92. }
  93. /* Does the tile associated to "group" require unrolling of the schedule
  94. * dimensions mapped to threads?
  95. * Note that this can only happen for private tiles.
  96. */
  97. int gpu_array_ref_group_requires_unroll(struct gpu_array_ref_group *group)
  98. {
  99. struct gpu_array_tile *tile;
  100. tile = gpu_array_ref_group_tile(group);
  101. if (!tile)
  102. return 0;
  103. return tile->requires_unroll;
  104. }
  105. /* Given a constraint
  106. *
  107. * a(p,i) + j = g f(e)
  108. *
  109. * or -a(p,i) - j = g f(e) if sign < 0,
  110. * store a(p,i) in bound->shift and g (stride) in bound->stride.
  111. * a(p,i) is assumed to be an expression in only the parameters
  112. * and the input dimensions.
  113. */
  114. static void extract_stride(__isl_keep isl_constraint *c,
  115. struct gpu_array_bound *bound, __isl_keep isl_val *stride, int sign)
  116. {
  117. int i;
  118. isl_val *v;
  119. isl_space *space;
  120. unsigned nparam;
  121. unsigned nvar;
  122. isl_aff *aff;
  123. isl_val_free(bound->stride);
  124. bound->stride = isl_val_copy(stride);
  125. space = isl_constraint_get_space(c);
  126. space = isl_space_domain(space);
  127. nparam = isl_space_dim(space, isl_dim_param);
  128. nvar = isl_space_dim(space, isl_dim_set);
  129. v = isl_constraint_get_constant_val(c);
  130. if (sign < 0)
  131. v = isl_val_neg(v);
  132. aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
  133. aff = isl_aff_set_constant_val(aff, v);
  134. for (i = 0; i < nparam; ++i) {
  135. if (!isl_constraint_involves_dims(c, isl_dim_param, i, 1))
  136. continue;
  137. v = isl_constraint_get_coefficient_val(c, isl_dim_param, i);
  138. if (sign < 0)
  139. v = isl_val_neg(v);
  140. aff = isl_aff_add_coefficient_val(aff, isl_dim_param, i, v);
  141. }
  142. for (i = 0; i < nvar; ++i) {
  143. if (!isl_constraint_involves_dims(c, isl_dim_in, i, 1))
  144. continue;
  145. v = isl_constraint_get_coefficient_val(c, isl_dim_in, i);
  146. if (sign < 0)
  147. v = isl_val_neg(v);
  148. aff = isl_aff_add_coefficient_val(aff, isl_dim_in, i, v);
  149. }
  150. bound->shift = aff;
  151. }
  152. /* Given an equality constraint of a map with a single output dimension j,
  153. * check if the constraint is of the form
  154. *
  155. * a(p,i) + j = g f(e)
  156. *
  157. * with a(p,i) an expression in the parameters and input dimensions
  158. * and f(e) an expression in the existentially quantified variables.
  159. * If so, and if g is larger than any such g from a previously considered
  160. * constraint, then call extract_stride to record the stride information
  161. * in bound.
  162. */
  163. static isl_stat check_stride_constraint(__isl_take isl_constraint *c,
  164. void *user)
  165. {
  166. int i;
  167. isl_ctx *ctx;
  168. isl_val *v;
  169. unsigned n_div;
  170. struct gpu_array_bound *bound = user;
  171. ctx = isl_constraint_get_ctx(c);
  172. n_div = isl_constraint_dim(c, isl_dim_div);
  173. v = isl_constraint_get_coefficient_val(c, isl_dim_out, 0);
  174. if (n_div && (isl_val_is_one(v) || isl_val_is_negone(v))) {
  175. int s = isl_val_sgn(v);
  176. isl_val *stride = isl_val_zero(ctx);
  177. isl_val_free(v);
  178. for (i = 0; i < n_div; ++i) {
  179. v = isl_constraint_get_coefficient_val(c,
  180. isl_dim_div, i);
  181. stride = isl_val_gcd(stride, v);
  182. }
  183. if (!isl_val_is_zero(stride) &&
  184. isl_val_gt(stride, bound->stride))
  185. extract_stride(c, bound, stride, s);
  186. isl_val_free(stride);
  187. } else
  188. isl_val_free(v);
  189. isl_constraint_free(c);
  190. return isl_stat_ok;
  191. }
  192. /* Given contraints on an array index i, check if we can find
  193. * a shift a(p) and a stride g such that
  194. *
  195. * a(p) + i = 0 mod g
  196. *
  197. * If so, record the information in bound and apply the mapping
  198. * i -> (i + a(p))/g to the array index in bounds and return
  199. * the new constraints.
  200. * If not, simply return the original constraints.
  201. *
  202. * If bounds is a subset of the space
  203. *
  204. * D -> i
  205. *
  206. * then the bound recorded in bound->shift is of the form
  207. *
  208. * D -> s(D)
  209. *
  210. * with s(D) equal to a(p) above.
  211. * Next, we construct a mapping of the form
  212. *
  213. * [D -> i] -> [D -> (i + S(D))/g]
  214. *
  215. * This mapping is computed as follows.
  216. * We first introduce "i" in the domain through precomposition
  217. * with [D -> i] -> D obtaining
  218. *
  219. * [D -> i] -> s(D)
  220. *
  221. * Adding [D -> i] -> i produces
  222. *
  223. * [D -> i] -> i + s(D)
  224. *
  225. * and the domain product with [D -> i] -> D yields
  226. *
  227. * [D -> i] -> [D -> i + s(D)]
  228. *
  229. * Composition with [D -> i] -> [D -> i/g] gives the desired result.
  230. */
  231. static __isl_give isl_basic_map *check_stride(struct gpu_array_bound *bound,
  232. __isl_take isl_basic_map *bounds)
  233. {
  234. isl_space *space;
  235. isl_basic_map *hull;
  236. isl_basic_map *shift, *id, *bmap, *scale;
  237. isl_basic_set *bset;
  238. isl_aff *aff;
  239. bound->stride = NULL;
  240. hull = isl_basic_map_affine_hull(isl_basic_map_copy(bounds));
  241. isl_basic_map_foreach_constraint(hull, &check_stride_constraint, bound);
  242. isl_basic_map_free(hull);
  243. if (!bound->stride)
  244. return bounds;
  245. shift = isl_basic_map_from_aff(isl_aff_copy(bound->shift));
  246. space = isl_basic_map_get_space(bounds);
  247. bmap = isl_basic_map_domain_map(isl_basic_map_universe(space));
  248. shift = isl_basic_map_apply_range(bmap, shift);
  249. space = isl_basic_map_get_space(bounds);
  250. id = isl_basic_map_range_map(isl_basic_map_universe(space));
  251. shift = isl_basic_map_sum(id, shift);
  252. space = isl_basic_map_get_space(bounds);
  253. id = isl_basic_map_domain_map(isl_basic_map_universe(space));
  254. shift = isl_basic_map_range_product(id, shift);
  255. space = isl_space_domain(isl_basic_map_get_space(bounds));
  256. id = isl_basic_map_identity(isl_space_map_from_set(space));
  257. space = isl_space_range(isl_basic_map_get_space(bounds));
  258. aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
  259. aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
  260. aff = isl_aff_scale_down_val(aff, isl_val_copy(bound->stride));
  261. scale = isl_basic_map_from_aff(aff);
  262. scale = isl_basic_map_product(id, scale);
  263. bmap = isl_basic_map_apply_range(shift, scale);
  264. bset = isl_basic_set_apply(isl_basic_map_wrap(bounds), bmap);
  265. bounds = isl_basic_set_unwrap(bset);
  266. return bounds;
  267. }
  268. /* Data used in compute_array_dim_size and compute_size_in_direction.
  269. *
  270. * pos is the position of the variable representing the array index,
  271. * i.e., the variable for which want to compute the size. This variable
  272. * is also the last variable in the set.
  273. */
  274. struct gpu_size_info {
  275. isl_basic_set *bset;
  276. struct gpu_array_bound *bound;
  277. int pos;
  278. };
  279. /* Given a constraint from the basic set describing the bounds on
  280. * an array index, check if it is a lower bound, say m i >= b(x), and,
  281. * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
  282. * upper bound. If so, and if this bound is smaller than any bound
  283. * derived from earlier constraints, set the size to this bound on
  284. * the expression and the lower bound to ceil(b(x)/m).
  285. */
  286. static isl_stat compute_size_in_direction(__isl_take isl_constraint *c,
  287. void *user)
  288. {
  289. struct gpu_size_info *size = user;
  290. unsigned nparam;
  291. unsigned n_div;
  292. isl_val *v;
  293. isl_aff *aff;
  294. isl_aff *lb;
  295. nparam = isl_basic_set_dim(size->bset, isl_dim_param);
  296. n_div = isl_constraint_dim(c, isl_dim_div);
  297. if (isl_constraint_involves_dims(c, isl_dim_div, 0, n_div) ||
  298. !isl_constraint_is_lower_bound(c, isl_dim_set, size->pos)) {
  299. isl_constraint_free(c);
  300. return isl_stat_ok;
  301. }
  302. aff = isl_constraint_get_bound(c, isl_dim_set, size->pos);
  303. aff = isl_aff_ceil(aff);
  304. lb = isl_aff_copy(aff);
  305. aff = isl_aff_neg(aff);
  306. aff = isl_aff_add_coefficient_si(aff, isl_dim_in, size->pos, 1);
  307. v = isl_basic_set_max_val(size->bset, aff);
  308. isl_aff_free(aff);
  309. if (isl_val_is_int(v)) {
  310. v = isl_val_add_ui(v, 1);
  311. if (!size->bound->size || isl_val_lt(v, size->bound->size)) {
  312. isl_val_free(size->bound->size);
  313. size->bound->size = isl_val_copy(v);
  314. lb = isl_aff_drop_dims(lb, isl_dim_in, size->pos, 1);
  315. isl_aff_free(size->bound->lb);
  316. size->bound->lb = isl_aff_copy(lb);
  317. }
  318. }
  319. isl_val_free(v);
  320. isl_aff_free(lb);
  321. isl_constraint_free(c);
  322. return isl_stat_ok;
  323. }
  324. /* Given a basic map "bounds" that maps parameters and input dimensions
  325. * to a single output dimension, look for an expression in the parameters
  326. * and input dimensions such that the range of the output dimension shifted
  327. * by this expression is a constant.
  328. *
  329. * In particular, we currently only consider lower bounds on the output
  330. * dimension as candidate expressions.
  331. */
  332. static int compute_array_dim_size(struct gpu_array_bound *bound,
  333. __isl_take isl_basic_map *bounds)
  334. {
  335. struct gpu_size_info size;
  336. bounds = isl_basic_map_detect_equalities(bounds);
  337. bounds = check_stride(bound, bounds);
  338. bound->size = NULL;
  339. bound->lb = NULL;
  340. size.bound = bound;
  341. size.pos = isl_basic_map_dim(bounds, isl_dim_in);
  342. size.bset = isl_basic_map_wrap(bounds);
  343. size.bset = isl_basic_set_flatten(size.bset);
  344. size.bset = isl_set_simple_hull(isl_basic_set_compute_divs(size.bset));
  345. isl_basic_set_foreach_constraint(size.bset, &compute_size_in_direction,
  346. &size);
  347. isl_basic_set_free(size.bset);
  348. return bound->size ? 0 : -1;
  349. }
  350. /* Check if we can find a memory tile for the given array
  351. * based on the given accesses, and if so, put the results in "tile".
  352. *
  353. * We project the accesses on each index in turn and look for a parametric
  354. * offset such that the size is constant.
  355. *
  356. * tile->depth is initialized to the input dimension of the computed bounds.
  357. */
  358. static int can_tile(__isl_keep isl_map *access, struct gpu_array_tile *tile)
  359. {
  360. int i;
  361. tile->depth = isl_map_dim(access, isl_dim_in);
  362. for (i = 0; i < tile->n; ++i) {
  363. isl_map *access_i;
  364. isl_basic_map *hull;
  365. access_i = isl_map_copy(access);
  366. access_i = isl_map_project_out(access_i, isl_dim_out, 0, i);
  367. access_i = isl_map_project_out(access_i, isl_dim_out,
  368. 1, tile->n - (i + 1));
  369. access_i = isl_map_compute_divs(access_i);
  370. hull = isl_map_simple_hull(access_i);
  371. if (compute_array_dim_size(&tile->bound[i], hull) < 0)
  372. return 0;
  373. }
  374. return 1;
  375. }
  376. /* Internal data structure for gpu_group_references.
  377. *
  378. * scop represents the input scop.
  379. * kernel_depth is the schedule depth where the kernel launch will
  380. * be introduced, i.e., it is the depth of the band that is mapped
  381. * to blocks.
  382. * shared_depth is the schedule depth at which the copying to/from
  383. * shared memory is computed. The copy operation may then
  384. * later be hoisted to a higher level.
  385. * thread_depth is the schedule depth where the thread mark is located,
  386. * i.e., it is the depth of the band that is mapped to threads and also
  387. * the schedule depth at which the copying to/from private memory
  388. * is computed. The copy operation may then later be hoisted to
  389. * a higher level.
  390. * n_thread is the number of schedule dimensions in the band that
  391. * is mapped to threads.
  392. * privatization lives in the range of thread_sched (i.e., it is
  393. * of dimension thread_depth + n_thread) and encodes the mapping
  394. * to thread identifiers (as parameters).
  395. * host_sched contains the kernel_depth dimensions of the host schedule.
  396. * shared_sched contains the first shared_depth dimensions of the
  397. * kernel schedule.
  398. * copy_sched contains the first thread_depth dimensions of the
  399. * kernel schedule.
  400. * thread_sched contains the first (thread_depth + n_thread) dimensions
  401. * of the kernel schedule.
  402. * full_sched is a union_map representation of the entire kernel schedule.
  403. * The schedules are all formulated in terms of the original statement
  404. * instances, i.e., those that appear in the domains of the access
  405. * relations.
  406. */
  407. struct gpu_group_data {
  408. struct ppcg_scop *scop;
  409. int kernel_depth;
  410. int shared_depth;
  411. int thread_depth;
  412. int n_thread;
  413. isl_set *privatization;
  414. isl_union_map *host_sched;
  415. isl_union_map *shared_sched;
  416. isl_union_map *copy_sched;
  417. isl_union_map *thread_sched;
  418. isl_union_map *full_sched;
  419. };
  420. /* Construct a map from domain_space to domain_space that increments
  421. * the dimension at position "pos" and leaves all other dimensions
  422. * constant.
  423. */
  424. static __isl_give isl_map *next(__isl_take isl_space *domain_space, int pos)
  425. {
  426. isl_space *space;
  427. isl_aff *aff;
  428. isl_multi_aff *next;
  429. space = isl_space_map_from_set(domain_space);
  430. next = isl_multi_aff_identity(space);
  431. aff = isl_multi_aff_get_aff(next, pos);
  432. aff = isl_aff_add_constant_si(aff, 1);
  433. next = isl_multi_aff_set_aff(next, pos, aff);
  434. return isl_map_from_multi_aff(next);
  435. }
  436. /* Check if the given access is coalesced (or if there is no point
  437. * in trying to coalesce the access by mapping the array to shared memory).
  438. * That is, check whether incrementing the dimension that will get
  439. * wrapped over the last thread index results in incrementing
  440. * the last array index.
  441. *
  442. * If no two consecutive array elements are ever accessed by "access",
  443. * then mapping the corresponding array to shared memory will not
  444. * improve coalescing. In fact, the copying will likely be performed
  445. * by a single thread. Consider the access as coalesced such that
  446. * the caller will not try and map the array to shared memory just
  447. * to improve coalescing.
  448. *
  449. * This function is only called for access relations without reuse and
  450. * kernels with at least one thread identifier.
  451. */
  452. static int access_is_coalesced(struct gpu_group_data *data,
  453. __isl_keep isl_union_map *access)
  454. {
  455. int dim;
  456. isl_space *space;
  457. isl_set *accessed;
  458. isl_map *access_map;
  459. isl_map *next_thread_x;
  460. isl_map *next_element;
  461. isl_map *map;
  462. int coalesced, empty;
  463. access = isl_union_map_copy(access);
  464. access = isl_union_map_apply_domain(access,
  465. isl_union_map_copy(data->full_sched));
  466. access_map = isl_map_from_union_map(access);
  467. space = isl_map_get_space(access_map);
  468. space = isl_space_range(space);
  469. dim = isl_space_dim(space, isl_dim_set);
  470. if (dim == 0)
  471. next_element = isl_map_empty(isl_space_map_from_set(space));
  472. else
  473. next_element = next(space, dim - 1);
  474. accessed = isl_map_range(isl_map_copy(access_map));
  475. map = isl_map_copy(next_element);
  476. map = isl_map_intersect_domain(map, isl_set_copy(accessed));
  477. map = isl_map_intersect_range(map, accessed);
  478. empty = isl_map_is_empty(map);
  479. isl_map_free(map);
  480. if (empty < 0 || empty) {
  481. isl_map_free(next_element);
  482. isl_map_free(access_map);
  483. return empty;
  484. }
  485. space = isl_map_get_space(access_map);
  486. space = isl_space_domain(space);
  487. next_thread_x = next(space, data->thread_depth + data->n_thread - 1);
  488. map = isl_map_apply_domain(next_thread_x, isl_map_copy(access_map));
  489. map = isl_map_apply_range(map, access_map);
  490. coalesced = isl_map_is_subset(map, next_element);
  491. isl_map_free(next_element);
  492. isl_map_free(map);
  493. return coalesced;
  494. }
  495. /* Replace the host schedule dimensions in the access relation "access"
  496. * by parameters, so that they are treated as fixed when checking for reuse
  497. * (within a kernel) or whether two consecutive elements are accessed
  498. * (within a kernel).
  499. */
  500. static __isl_give isl_union_map *localize_access(struct gpu_group_data *data,
  501. __isl_take isl_union_map *access)
  502. {
  503. int n;
  504. isl_space *space;
  505. isl_set *param;
  506. isl_union_map *umap;
  507. isl_id_list *ids;
  508. umap = isl_union_map_copy(data->host_sched);
  509. space = isl_union_map_get_space(umap);
  510. n = data->kernel_depth;
  511. ids = ppcg_scop_generate_names(data->scop, n, "__ppcg_host_");
  512. param = parametrization(space, n, 0, ids);
  513. isl_id_list_free(ids);
  514. umap = isl_union_map_intersect_range(umap,
  515. isl_union_set_from_set(param));
  516. access = isl_union_map_intersect_domain(access,
  517. isl_union_map_domain(umap));
  518. return access;
  519. }
  520. /* Given an access relation in terms of at least data->thread_depth initial
  521. * dimensions of the computed schedule, check if it is bijective for
  522. * fixed values of the first data->thread_depth dimensions.
  523. * We perform this check by equating these dimensions to parameters.
  524. */
  525. static int access_is_bijective(struct gpu_group_data *data,
  526. __isl_keep isl_map *access)
  527. {
  528. int res;
  529. int dim;
  530. isl_set *par;
  531. isl_space *space;
  532. isl_id_list *ids;
  533. access = isl_map_copy(access);
  534. space = isl_space_params(isl_map_get_space(access));
  535. ids = ppcg_scop_generate_names(data->scop, data->thread_depth, "s");
  536. dim = isl_map_dim(access, isl_dim_in);
  537. par = parametrization(space, dim, 0, ids);
  538. isl_id_list_free(ids);
  539. access = isl_map_intersect_domain(access, par);
  540. res = isl_map_is_bijective(access);
  541. isl_map_free(access);
  542. return res;
  543. }
  544. /* Compute the number of outer schedule tile dimensions that affect
  545. * the offset of "tile".
  546. * If there is no such dimension, then return the index
  547. * of the first kernel dimension, i.e., data->kernel_depth.
  548. */
  549. static int compute_tile_depth(struct gpu_group_data *data,
  550. struct gpu_array_tile *tile)
  551. {
  552. int i, j;
  553. for (j = tile->depth - 1; j >= data->kernel_depth; --j) {
  554. for (i = 0; i < tile->n; ++i) {
  555. isl_aff *lb;
  556. isl_aff *shift;
  557. lb = tile->bound[i].lb;
  558. if (isl_aff_involves_dims(lb, isl_dim_in, j, 1))
  559. break;
  560. shift = tile->bound[i].shift;
  561. if (!shift)
  562. continue;
  563. if (isl_aff_involves_dims(shift, isl_dim_in, j, 1))
  564. break;
  565. }
  566. if (i < tile->n)
  567. break;
  568. }
  569. return ++j;
  570. }
  571. /* Return the lowest depth between data->kernel_depth and data->thread_depth
  572. * at which every array element accessed through "acc" is accessed
  573. * by a single thread. The input dimension of "acc" is
  574. * data->thread_depth + data->n_thread, where the final data->n_thread
  575. * dimensions are those that will be mapped to threads.
  576. * If the values for these dimensions are uniquely determined
  577. * by the array index and a given number of outer dimensions, then
  578. * there is only one thread accessing that array element within those
  579. * outer dimensions.
  580. *
  581. * The input space of "acc" is first split up, such that it has the form
  582. *
  583. * [O -> T] -> A
  584. *
  585. * with O the outer dimensions, T the dimensions that will be mapped to threads
  586. * and A the array index.
  587. *
  588. * Then the positions of T and A are interchanged to simplify the test
  589. * whether T uniquely depends on O and A.
  590. * In particular, the above access relation is first combined with
  591. *
  592. * [O -> T] -> T
  593. *
  594. * to form
  595. *
  596. * [O -> T] -> [A -> T]
  597. *
  598. * from which
  599. *
  600. * O -> [A -> T]
  601. *
  602. * is extracted, which is then uncurried to
  603. *
  604. * [O -> A] -> T
  605. *
  606. * Finally, the final dimensions of O are projected out one by one
  607. * until T is no longer uniquely determined by A and the remaining
  608. * dimensions in O. The value returned is that of the last dimension
  609. * that was successfully projected out.
  610. * Note that there is no need to test whether [O -> A] -> T itself
  611. * is single-valued as that was already tested in access_is_bijective.
  612. */
  613. static int compute_accessed_by_single_thread_depth(struct gpu_group_data *data,
  614. __isl_keep isl_map *acc)
  615. {
  616. int i;
  617. isl_space *space;
  618. isl_map *map;
  619. isl_bool sv;
  620. if (data->thread_depth == data->kernel_depth)
  621. return data->thread_depth;
  622. acc = isl_map_copy(acc);
  623. space = isl_map_get_space(acc);
  624. space = isl_space_params(space);
  625. space = isl_space_set_from_params(space);
  626. space = isl_space_add_dims(space, isl_dim_set, data->thread_depth);
  627. space = isl_space_from_domain(space);
  628. space = isl_space_add_dims(space, isl_dim_out, data->n_thread);
  629. space = isl_space_wrap(space);
  630. map = isl_set_flatten_map(isl_set_universe(space));
  631. acc = isl_map_apply_range(map, acc);
  632. space = isl_space_domain(isl_map_get_space(acc));
  633. map = isl_map_range_map(isl_map_universe(isl_space_unwrap(space)));
  634. acc = isl_map_range_product(acc, map);
  635. acc = isl_map_domain_factor_domain(acc);
  636. acc = isl_map_uncurry(acc);
  637. for (i = data->thread_depth - 1; i >= data->kernel_depth; --i) {
  638. acc = isl_map_project_out(acc, isl_dim_in, i, 1);
  639. sv = isl_map_is_single_valued(acc);
  640. if (sv < 0)
  641. return -1;
  642. if (!sv)
  643. break;
  644. }
  645. isl_map_free(acc);
  646. return ++i;
  647. }
  648. /* Adjust the fields of "tile" to reflect the new input dimension "depth".
  649. * The dimension beyond "depth" are assumed not to affect the tile,
  650. * so they can simply be dropped.
  651. */
  652. static int tile_adjust_depth(struct gpu_array_tile *tile, int depth)
  653. {
  654. int i;
  655. if (tile->depth == depth)
  656. return 0;
  657. for (i = 0; i < tile->n; ++i) {
  658. tile->bound[i].lb = isl_aff_drop_dims(tile->bound[i].lb,
  659. isl_dim_in, depth, tile->depth - depth);
  660. if (!tile->bound[i].lb)
  661. return -1;
  662. if (!tile->bound[i].shift)
  663. continue;
  664. tile->bound[i].shift = isl_aff_drop_dims(tile->bound[i].shift,
  665. isl_dim_in, depth, tile->depth - depth);
  666. if (!tile->bound[i].shift)
  667. return -1;
  668. }
  669. tile->depth = depth;
  670. return 0;
  671. }
  672. /* Determine the number of schedule dimensions that affect the offset of the
  673. * shared or private tile "tile" and store the result in tile->depth, with
  674. * a lower bound of data->kernel_depth.
  675. * Also adjust the fields of the tile to only refer to the tile->depth
  676. * outer schedule dimensions.
  677. */
  678. static isl_stat tile_set_depth(struct gpu_group_data *data,
  679. struct gpu_array_tile *tile)
  680. {
  681. if (tile_adjust_depth(tile, compute_tile_depth(data, tile)) < 0)
  682. return isl_stat_error;
  683. return isl_stat_ok;
  684. }
  685. /* Determine the number of schedule dimensions that affect the offset of the
  686. * shared tile and store the minimum of the private and shared tile depth
  687. * in group->min_depth, with a lower bound of data->kernel_depth.
  688. * If there is no tile defined on the array reference group,
  689. * then set group->min_depth to data->thread_depth.
  690. */
  691. static int set_depth(struct gpu_group_data *data,
  692. struct gpu_array_ref_group *group)
  693. {
  694. group->min_depth = data->thread_depth;
  695. if (group->private_tile) {
  696. if (group->private_tile->depth < group->min_depth)
  697. group->min_depth = group->private_tile->depth;
  698. }
  699. if (group->shared_tile) {
  700. if (tile_set_depth(data, group->shared_tile) < 0)
  701. return -1;
  702. if (group->shared_tile->depth < group->min_depth)
  703. group->min_depth = group->shared_tile->depth;
  704. }
  705. return 0;
  706. }
  707. /* Fill up the groups array with singleton groups, i.e., one group
  708. * per reference, initializing the array, access, write, n_ref and refs fields.
  709. * In particular the access field is initialized to the scheduled
  710. * access relation of the array reference.
  711. *
  712. * Return the number of elements initialized, i.e., the number of
  713. * active references in the current kernel.
  714. */
  715. static int populate_array_references(struct gpu_local_array_info *local,
  716. struct gpu_array_ref_group **groups, struct gpu_group_data *data)
  717. {
  718. int i;
  719. int n;
  720. isl_ctx *ctx = isl_union_map_get_ctx(data->copy_sched);
  721. n = 0;
  722. for (i = 0; i < local->array->n_ref; ++i) {
  723. isl_union_map *umap;
  724. isl_map *map;
  725. struct gpu_array_ref_group *group;
  726. struct gpu_stmt_access *access = local->array->refs[i];
  727. map = isl_map_copy(access->access);
  728. umap = isl_union_map_from_map(map);
  729. umap = isl_union_map_apply_domain(umap,
  730. isl_union_map_copy(data->copy_sched));
  731. if (isl_union_map_is_empty(umap)) {
  732. isl_union_map_free(umap);
  733. continue;
  734. }
  735. map = isl_map_from_union_map(umap);
  736. map = isl_map_detect_equalities(map);
  737. group = isl_calloc_type(ctx, struct gpu_array_ref_group);
  738. if (!group)
  739. return -1;
  740. group->local_array = local;
  741. group->array = local->array;
  742. group->access = map;
  743. group->write = access->write;
  744. group->exact_write = access->exact_write;
  745. group->slice = access->n_index < local->array->n_index;
  746. group->refs = &local->array->refs[i];
  747. group->n_ref = 1;
  748. groups[n++] = group;
  749. }
  750. return n;
  751. }
  752. /* If group->n_ref == 1, then group->refs was set by
  753. * populate_array_references to point directly into
  754. * group->array->refs and should not be freed.
  755. * If group->n_ref > 1, then group->refs was set by join_groups
  756. * to point to a newly allocated array.
  757. */
  758. struct gpu_array_ref_group *gpu_array_ref_group_free(
  759. struct gpu_array_ref_group *group)
  760. {
  761. if (!group)
  762. return NULL;
  763. gpu_array_tile_free(group->shared_tile);
  764. gpu_array_tile_free(group->private_tile);
  765. isl_map_free(group->access);
  766. if (group->n_ref > 1)
  767. free(group->refs);
  768. free(group);
  769. return NULL;
  770. }
  771. /* Check if the access relations of group1 and group2 overlap within
  772. * copy_sched.
  773. */
  774. static int accesses_overlap(struct gpu_array_ref_group *group1,
  775. struct gpu_array_ref_group *group2)
  776. {
  777. int disjoint;
  778. disjoint = isl_map_is_disjoint(group1->access, group2->access);
  779. if (disjoint < 0)
  780. return -1;
  781. return !disjoint;
  782. }
  783. /* Combine the given two groups into a single group, containing
  784. * the references of both groups.
  785. */
  786. static struct gpu_array_ref_group *join_groups(
  787. struct gpu_array_ref_group *group1,
  788. struct gpu_array_ref_group *group2)
  789. {
  790. int i;
  791. isl_ctx *ctx;
  792. struct gpu_array_ref_group *group;
  793. if (!group1 || !group2)
  794. return NULL;
  795. ctx = isl_map_get_ctx(group1->access);
  796. group = isl_calloc_type(ctx, struct gpu_array_ref_group);
  797. if (!group)
  798. return NULL;
  799. group->local_array = group1->local_array;
  800. group->array = group1->array;
  801. group->access = isl_map_union(isl_map_copy(group1->access),
  802. isl_map_copy(group2->access));
  803. group->write = group1->write || group2->write;
  804. group->exact_write = group1->exact_write && group2->exact_write;
  805. group->slice = group1->slice || group2->slice;
  806. group->n_ref = group1->n_ref + group2->n_ref;
  807. group->refs = isl_alloc_array(ctx, struct gpu_stmt_access *,
  808. group->n_ref);
  809. if (!group->refs)
  810. return gpu_array_ref_group_free(group);
  811. for (i = 0; i < group1->n_ref; ++i)
  812. group->refs[i] = group1->refs[i];
  813. for (i = 0; i < group2->n_ref; ++i)
  814. group->refs[group1->n_ref + i] = group2->refs[i];
  815. return group;
  816. }
  817. /* Combine the given two groups into a single group and free
  818. * the original two groups.
  819. */
  820. static struct gpu_array_ref_group *join_groups_and_free(
  821. struct gpu_array_ref_group *group1,
  822. struct gpu_array_ref_group *group2)
  823. {
  824. struct gpu_array_ref_group *group;
  825. group = join_groups(group1, group2);
  826. gpu_array_ref_group_free(group1);
  827. gpu_array_ref_group_free(group2);
  828. return group;
  829. }
  830. /* Report that the array reference group with the given access relation
  831. * is not mapped to shared memory in the given kernel because
  832. * it does not exhibit any reuse and is considered to be coalesced.
  833. */
  834. static void report_no_reuse_and_coalesced(struct ppcg_kernel *kernel,
  835. __isl_keep isl_union_map *access)
  836. {
  837. isl_ctx *ctx;
  838. isl_printer *p;
  839. ctx = isl_union_map_get_ctx(access);
  840. p = isl_printer_to_file(ctx, stdout);
  841. p = isl_printer_print_str(p, "Array reference group ");
  842. p = isl_printer_print_union_map(p, access);
  843. p = isl_printer_print_str(p,
  844. " not considered for mapping to shared memory in kernel");
  845. p = isl_printer_print_int(p, kernel->id);
  846. p = isl_printer_print_str(p,
  847. " because it exhibits no reuse and is considered to be coalesced");
  848. p = isl_printer_end_line(p);
  849. isl_printer_free(p);
  850. }
  851. /* Given an access relation in terms of the data->thread_depth initial
  852. * dimensions of the computed schedule and the thread identifiers
  853. * (as parameters), check if the use of the corresponding private tile
  854. * requires unrolling.
  855. *
  856. * If we are creating a private tile because we are forced to,
  857. * then no unrolling is required.
  858. * Otherwise we check if "access" is bijective and unrolling
  859. * is required if it is not. Note that the access relation
  860. * has already been determined to be bijective before the introduction
  861. * of the thread identifiers and the removal of the schedule dimensions
  862. * that are mapped to these threads. If the access relation is no longer
  863. * bijective, then this means that more than one value of one of those
  864. * schedule dimensions is mapped to the same thread and therefore
  865. * unrolling is required.
  866. */
  867. static int check_requires_unroll(struct gpu_group_data *data,
  868. __isl_keep isl_map *access, int force_private)
  869. {
  870. int bijective;
  871. if (force_private)
  872. return 0;
  873. bijective = access_is_bijective(data, access);
  874. if (bijective < 0)
  875. return -1;
  876. return !bijective;
  877. }
  878. /* Map the domain of "access" to the outer data->shared_depth
  879. * schedule dimensions. When data->shared_depth is equal to
  880. * data->thread_depth, this result is already available in group->access.
  881. */
  882. static __isl_give isl_map *shared_access(struct gpu_array_ref_group *group,
  883. __isl_keep isl_union_map *access, struct gpu_group_data *data)
  884. {
  885. isl_union_map *shared;
  886. if (data->shared_depth == data->thread_depth)
  887. return isl_map_copy(group->access);
  888. shared = isl_union_map_copy(access);
  889. shared = isl_union_map_apply_domain(shared,
  890. isl_union_map_copy(data->shared_sched));
  891. return isl_map_from_union_map(shared);
  892. }
  893. /* Compute the private and/or shared memory tiles for the array
  894. * reference group "group" of array "array".
  895. * Return 0 on success and -1 on error.
  896. *
  897. * If the array is a read-only scalar or if the user requested
  898. * not to use shared or private memory, then we do not need to do anything.
  899. *
  900. * If any reference in the reference group accesses more than one element,
  901. * then we would have to make sure that the layout in shared memory
  902. * is the same as that in global memory. Since we do not handle this yet
  903. * (and it may not even be possible), we refuse to map to private or
  904. * shared memory in such cases.
  905. *
  906. * If the array group involves any may writes (that are not must writes),
  907. * then we would have to make sure that we load the data into shared/private
  908. * memory first in case the data is not written by the kernel
  909. * (but still written back out to global memory).
  910. * Since we don't have any such mechanism at the moment, we don't
  911. * compute shared/private tiles for groups involving may writes.
  912. *
  913. * We only try to compute a shared memory tile if there is any reuse
  914. * or if the access is not coalesced.
  915. * Reuse and coalescing are checked within the given kernel.
  916. *
  917. * For computing a private memory tile, we also require that there is
  918. * some reuse. Moreover, we require that the access is private
  919. * to the thread. That is, we check that any given array element
  920. * is only accessed by a single thread.
  921. * We compute an access relation that maps the outer
  922. * data->thread_depth + data->n_thread schedule dimensions.
  923. * The latter data->n_thread will be mapped to thread identifiers.
  924. * We actually check that those iterators that will be wrapped
  925. * partition the array space. This check is stricter than necessary
  926. * since several iterations may be mapped onto the same thread
  927. * and then they could be allowed to access the same memory elements,
  928. * but our check does not allow this situation.
  929. *
  930. * For private memory tiles, the number of schedule dimensions that
  931. * affect the offset is computed and stored in tile->depth, with
  932. * a lower bound of data->kernel_depth. If this depth is smaller
  933. * than the minimal depth that still ensures that every element
  934. * is accessed by a single thread, then the depth is raised
  935. * to this minimal depth.
  936. * The fields of the tile are then adjusted to only refer to the tile->depth
  937. * outer schedule dimensions.
  938. *
  939. * We also check that the index expression only depends on parallel
  940. * loops. That way, we can move those loops innermost and unroll them.
  941. * Again, we use a test that is stricter than necessary.
  942. * We actually check whether the index expression only depends
  943. * on the iterators that are wrapped over the threads.
  944. * These are necessarily parallel, but there may be more parallel loops.
  945. *
  946. * Combining the injectivity of the first test with the single-valuedness
  947. * of the second test, we simply test for bijectivity.
  948. *
  949. * If the use of the private tile requires unrolling, but some
  950. * of the other arrays are forcibly mapped to private memory,
  951. * then we do not allow the use of this private tile since
  952. * we cannot move the schedule dimensions that need to be unrolled down
  953. * without performing some kind of expansion on those arrays
  954. * that are forcibly mapped to private memory.
  955. *
  956. * If the array is marked force_private, then we bypass all checks
  957. * and assume we can (and should) use registers only.
  958. *
  959. * If it turns out we can (or have to) use registers, we compute
  960. * the private memory tile size using can_tile, after introducing a dependence
  961. * on the thread indices.
  962. */
  963. static int compute_group_bounds_core(struct ppcg_kernel *kernel,
  964. struct gpu_array_ref_group *group, struct gpu_group_data *data)
  965. {
  966. isl_ctx *ctx = isl_space_get_ctx(group->array->space);
  967. isl_union_map *access, *local;
  968. int n_index = group->array->n_index;
  969. int no_reuse, coalesced;
  970. isl_map *acc;
  971. int force_private = group->local_array->force_private;
  972. int use_shared = !force_private && kernel->options->use_shared_memory &&
  973. data->n_thread > 0;
  974. int use_private = force_private || kernel->options->use_private_memory;
  975. int r = 0;
  976. int requires_unroll;
  977. int unique_depth;
  978. if (!use_shared && !use_private)
  979. return 0;
  980. if (gpu_array_is_read_only_scalar(group->array))
  981. return 0;
  982. if (!force_private && !group->exact_write)
  983. return 0;
  984. if (group->slice)
  985. return 0;
  986. access = gpu_array_ref_group_access_relation(group, 1, 1);
  987. local = localize_access(data, isl_union_map_copy(access));
  988. no_reuse = isl_union_map_is_injective(local);
  989. if (no_reuse < 0)
  990. r = -1;
  991. if (use_shared && no_reuse)
  992. coalesced = access_is_coalesced(data, local);
  993. isl_union_map_free(local);
  994. if (r >= 0 && kernel->options->debug->verbose &&
  995. use_shared && no_reuse && coalesced)
  996. report_no_reuse_and_coalesced(kernel, access);
  997. if (use_shared && (!no_reuse || !coalesced)) {
  998. group->shared_tile = gpu_array_tile_create(ctx,
  999. group->array->n_index);
  1000. acc = shared_access(group, access, data);
  1001. if (!group->shared_tile)
  1002. r = -1;
  1003. else if (!can_tile(acc, group->shared_tile))
  1004. group->shared_tile =
  1005. gpu_array_tile_free(group->shared_tile);
  1006. isl_map_free(acc);
  1007. }
  1008. if (r < 0 || (!force_private && (!use_private || no_reuse))) {
  1009. isl_union_map_free(access);
  1010. return r;
  1011. }
  1012. access = isl_union_map_apply_domain(access,
  1013. isl_union_map_copy(data->thread_sched));
  1014. acc = isl_map_from_union_map(access);
  1015. if (!force_private && !access_is_bijective(data, acc)) {
  1016. isl_map_free(acc);
  1017. return 0;
  1018. }
  1019. unique_depth = compute_accessed_by_single_thread_depth(data, acc);
  1020. acc = isl_map_intersect_domain(acc, isl_set_copy(data->privatization));
  1021. acc = isl_map_project_out(acc, isl_dim_in, data->thread_depth,
  1022. data->n_thread);
  1023. requires_unroll = check_requires_unroll(data, acc, force_private);
  1024. if (unique_depth < 0 || requires_unroll < 0 ||
  1025. (requires_unroll && kernel->any_force_private)) {
  1026. isl_map_free(acc);
  1027. return requires_unroll < 0 ? -1 : 0;
  1028. }
  1029. group->private_tile = gpu_array_tile_create(ctx, n_index);
  1030. if (!group->private_tile) {
  1031. isl_map_free(acc);
  1032. return -1;
  1033. }
  1034. group->private_tile->requires_unroll = requires_unroll;
  1035. if (!can_tile(acc, group->private_tile))
  1036. group->private_tile = gpu_array_tile_free(group->private_tile);
  1037. isl_map_free(acc);
  1038. if (group->private_tile) {
  1039. struct gpu_array_tile *tile = group->private_tile;
  1040. int tile_depth = compute_tile_depth(data, tile);
  1041. if (tile_depth < unique_depth)
  1042. tile_depth = unique_depth;
  1043. if (tile_adjust_depth(tile, tile_depth) < 0)
  1044. return -1;
  1045. }
  1046. if (force_private && !group->private_tile)
  1047. isl_die(ctx, isl_error_internal,
  1048. "unable to map array reference group to registers",
  1049. return -1);
  1050. return 0;
  1051. }
  1052. /* Compute the private and/or shared memory tiles for the array
  1053. * reference group "group" of array "array" and set the tile depth.
  1054. * Return 0 on success and -1 on error.
  1055. */
  1056. static int compute_group_bounds(struct ppcg_kernel *kernel,
  1057. struct gpu_array_ref_group *group, struct gpu_group_data *data)
  1058. {
  1059. if (!group)
  1060. return -1;
  1061. if (compute_group_bounds_core(kernel, group, data) < 0)
  1062. return -1;
  1063. if (set_depth(data, group) < 0)
  1064. return -1;
  1065. return 0;
  1066. }
  1067. /* If two groups have overlapping access relations (as determined by
  1068. * the "overlap" function) and if one of them involves a write,
  1069. * then merge the two groups into one.
  1070. * If "compute_bounds" is set, then call compute_group_bounds
  1071. * on the merged groups.
  1072. *
  1073. * Return the updated number of groups.
  1074. * Return -1 on error.
  1075. */
  1076. static int group_writes(struct ppcg_kernel *kernel,
  1077. int n, struct gpu_array_ref_group **groups,
  1078. int (*overlap)(struct gpu_array_ref_group *group1,
  1079. struct gpu_array_ref_group *group2), int compute_bounds,
  1080. struct gpu_group_data *data)
  1081. {
  1082. int i, j;
  1083. for (i = 0; i < n; ++i) {
  1084. for (j = n - 1; j > i; --j) {
  1085. if (!groups[i]->write && !groups[j]->write)
  1086. continue;
  1087. if (!overlap(groups[i], groups[j]))
  1088. continue;
  1089. groups[i] = join_groups_and_free(groups[i], groups[j]);
  1090. if (j != n - 1)
  1091. groups[j] = groups[n - 1];
  1092. groups[n - 1] = NULL;
  1093. n--;
  1094. if (!groups[i])
  1095. return -1;
  1096. if (compute_bounds &&
  1097. compute_group_bounds(kernel, groups[i], data) < 0)
  1098. return -1;
  1099. }
  1100. }
  1101. return n;
  1102. }
  1103. /* If two groups have overlapping access relations (within the innermost
  1104. * loop) and if one of them involves a write, then merge the two groups
  1105. * into one.
  1106. *
  1107. * Return the updated number of groups.
  1108. */
  1109. static int group_overlapping_writes(struct ppcg_kernel *kernel,
  1110. int n, struct gpu_array_ref_group **groups,
  1111. struct gpu_group_data *data)
  1112. {
  1113. return group_writes(kernel, n, groups, &accesses_overlap, 0, data);
  1114. }
  1115. /* Check if the access relations of group1 and group2 overlap within
  1116. * the outermost min(group1->min_depth, group2->min_depth) loops.
  1117. */
  1118. static int depth_accesses_overlap(struct gpu_array_ref_group *group1,
  1119. struct gpu_array_ref_group *group2)
  1120. {
  1121. int depth;
  1122. int dim;
  1123. int empty;
  1124. isl_map *map_i, *map_j, *map;
  1125. depth = group1->min_depth;
  1126. if (group2->min_depth < depth)
  1127. depth = group2->min_depth;
  1128. map_i = isl_map_copy(group1->access);
  1129. dim = isl_map_dim(map_i, isl_dim_in);
  1130. map_i = isl_map_eliminate(map_i, isl_dim_in, depth, dim - depth);
  1131. map_j = isl_map_copy(group2->access);
  1132. map_j = isl_map_eliminate(map_j, isl_dim_in, depth, dim - depth);
  1133. map = isl_map_intersect(map_i, map_j);
  1134. empty = isl_map_is_empty(map);
  1135. isl_map_free(map);
  1136. return !empty;
  1137. }
  1138. /* If two groups have overlapping access relations (within the outer
  1139. * depth loops) and if one of them involves a write,
  1140. * then merge the two groups into one.
  1141. *
  1142. * Return the updated number of groups.
  1143. */
  1144. static int group_depth_overlapping_writes(struct ppcg_kernel *kernel,
  1145. int n, struct gpu_array_ref_group **groups, struct gpu_group_data *data)
  1146. {
  1147. return group_writes(kernel, n, groups, &depth_accesses_overlap, 1,
  1148. data);
  1149. }
  1150. /* Is the size of the tile specified by "tile" smaller than the sum of
  1151. * the sizes of the tiles specified by "tile1" and "tile2"?
  1152. */
  1153. static int smaller_tile(struct gpu_array_tile *tile,
  1154. struct gpu_array_tile *tile1, struct gpu_array_tile *tile2)
  1155. {
  1156. int smaller;
  1157. isl_val *size, *size1, *size2;
  1158. size = gpu_array_tile_size(tile);
  1159. size1 = gpu_array_tile_size(tile1);
  1160. size2 = gpu_array_tile_size(tile2);
  1161. size = isl_val_sub(size, size1);
  1162. size = isl_val_sub(size, size2);
  1163. smaller = isl_val_is_neg(size);
  1164. isl_val_free(size);
  1165. return smaller;
  1166. }
  1167. /* Given an initial grouping of array references and shared memory tiles
  1168. * for each group that allows for a shared memory tile, merge two groups
  1169. * if both have a shared memory tile, the merged group also has
  1170. * a shared memory tile and the size of the tile for the merge group
  1171. * is smaller than the sum of the tile sizes of the individual groups.
  1172. *
  1173. * If merging two groups decreases the depth of the tile of
  1174. * one or both of the two groups, then we need to check for overlapping
  1175. * writes again.
  1176. *
  1177. * Return the number of groups after merging.
  1178. * Return -1 on error.
  1179. */
  1180. static int group_common_shared_memory_tile(struct ppcg_kernel *kernel,
  1181. struct gpu_array_info *array, int n,
  1182. struct gpu_array_ref_group **groups, struct gpu_group_data *data)
  1183. {
  1184. int i, j;
  1185. int recompute_overlap = 0;
  1186. for (i = 0; i < n; ++i) {
  1187. if (!groups[i]->shared_tile)
  1188. continue;
  1189. for (j = n - 1; j > i; --j) {
  1190. struct gpu_array_ref_group *group;
  1191. if (!groups[j]->shared_tile)
  1192. continue;
  1193. if (!depth_accesses_overlap(groups[i], groups[j]))
  1194. continue;
  1195. group = join_groups(groups[i], groups[j]);
  1196. if (compute_group_bounds(kernel, group, data) < 0) {
  1197. gpu_array_ref_group_free(group);
  1198. return -1;
  1199. }
  1200. if (!group->shared_tile ||
  1201. !smaller_tile(group->shared_tile,
  1202. groups[i]->shared_tile,
  1203. groups[j]->shared_tile)) {
  1204. gpu_array_ref_group_free(group);
  1205. continue;
  1206. }
  1207. if (group->min_depth < groups[i]->min_depth ||
  1208. group->min_depth < groups[j]->min_depth)
  1209. recompute_overlap = 1;
  1210. gpu_array_ref_group_free(groups[i]);
  1211. gpu_array_ref_group_free(groups[j]);
  1212. groups[i] = group;
  1213. if (j != n - 1)
  1214. groups[j] = groups[n - 1];
  1215. n--;
  1216. }
  1217. }
  1218. if (recompute_overlap)
  1219. n = group_depth_overlapping_writes(kernel, n, groups, data);
  1220. return n;
  1221. }
  1222. /* Set array->n_group and array->groups to n and groups.
  1223. *
  1224. * Additionally, set the "nr" field of each group.
  1225. */
  1226. static void set_array_groups(struct gpu_local_array_info *array,
  1227. int n, struct gpu_array_ref_group **groups)
  1228. {
  1229. int i;
  1230. array->n_group = n;
  1231. array->groups = groups;
  1232. for (i = 0; i < n; ++i)
  1233. groups[i]->nr = i;
  1234. }
  1235. /* Combine all groups in "groups" into a single group and return
  1236. * the new number of groups (1 or 0 if there were no groups to start with).
  1237. */
  1238. static int join_all_groups(int n, struct gpu_array_ref_group **groups)
  1239. {
  1240. int i;
  1241. for (i = n - 1; i > 0; --i) {
  1242. groups[0] = join_groups_and_free(groups[0], groups[i]);
  1243. groups[i] = NULL;
  1244. n--;
  1245. }
  1246. return n;
  1247. }
  1248. /* Group array references that should be considered together when
  1249. * deciding whether to access them from private, shared or global memory.
  1250. * Return -1 on error.
  1251. *
  1252. * In particular, if two array references overlap and if one of them
  1253. * is a write, then the two references are grouped together.
  1254. * We first perform an initial grouping based only on the access relation.
  1255. * After computing shared and private memory tiles, we check for
  1256. * overlapping writes again, but this time taking into account
  1257. * the depth of the effective tile.
  1258. *
  1259. * Furthermore, if two groups admit a shared memory tile and if the
  1260. * combination of the two also admits a shared memory tile, we merge
  1261. * the two groups.
  1262. *
  1263. * If the array contains structures, then we compute a single
  1264. * reference group without trying to find any tiles
  1265. * since we do not map such arrays to private or shared
  1266. * memory. The only exception is when those arrays of structures
  1267. * are required to be mapped to private memory.
  1268. */
  1269. static int group_array_references(struct ppcg_kernel *kernel,
  1270. struct gpu_local_array_info *local, struct gpu_group_data *data)
  1271. {
  1272. int i;
  1273. int n;
  1274. isl_ctx *ctx = isl_union_map_get_ctx(data->shared_sched);
  1275. struct gpu_array_ref_group **groups;
  1276. groups = isl_calloc_array(ctx, struct gpu_array_ref_group *,
  1277. local->array->n_ref);
  1278. if (!groups)
  1279. return -1;
  1280. n = populate_array_references(local, groups, data);
  1281. if (local->array->has_compound_element && !local->force_private) {
  1282. n = join_all_groups(n, groups);
  1283. set_array_groups(local, n, groups);
  1284. return 0;
  1285. }
  1286. n = group_overlapping_writes(kernel, n, groups, data);
  1287. for (i = 0; i < n; ++i)
  1288. if (compute_group_bounds(kernel, groups[i], data) < 0)
  1289. n = -1;
  1290. n = group_depth_overlapping_writes(kernel, n, groups, data);
  1291. n = group_common_shared_memory_tile(kernel, local->array,
  1292. n, groups, data);
  1293. set_array_groups(local, n, groups);
  1294. if (n >= 0)
  1295. return 0;
  1296. for (i = 0; i < local->array->n_ref; ++i)
  1297. gpu_array_ref_group_free(groups[i]);
  1298. return -1;
  1299. }
  1300. /* For each array in the input program that can be mapped to private memory,
  1301. * check if there are any order dependences active inside the current kernel,
  1302. * within the same iteration of the host schedule, i.e., the prefix
  1303. * schedule at "node".
  1304. * If so, mark the array as force_private so that its reference groups will be
  1305. * mapped to a registers.
  1306. *
  1307. * Note that the arrays that cannot be mapped to private memory have
  1308. * had their order dependences added to prog->array_order and
  1309. * subsequently to the coincidence constraints.
  1310. */
  1311. static void check_can_be_private_live_ranges(struct ppcg_kernel *kernel,
  1312. __isl_keep isl_schedule_node *node)
  1313. {
  1314. int i;
  1315. isl_union_set *domain;
  1316. isl_multi_union_pw_aff *prefix;
  1317. isl_union_pw_multi_aff *contraction;
  1318. if (!kernel->options->live_range_reordering)
  1319. return;
  1320. kernel->any_force_private = 0;
  1321. prefix = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
  1322. contraction = isl_union_pw_multi_aff_copy(kernel->contraction);
  1323. prefix = isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix,
  1324. contraction);
  1325. domain = isl_union_set_copy(kernel->expanded_domain);
  1326. domain = isl_union_set_universe(domain);
  1327. for (i = 0; i < kernel->n_array; ++i) {
  1328. struct gpu_local_array_info *local = &kernel->array[i];
  1329. isl_union_map *order;
  1330. local->force_private = 0;
  1331. if (!gpu_array_can_be_private(local->array))
  1332. continue;
  1333. order = isl_union_map_copy(local->array->dep_order);
  1334. order = isl_union_map_intersect_domain(order,
  1335. isl_union_set_copy(domain));
  1336. order = isl_union_map_intersect_range(order,
  1337. isl_union_set_copy(domain));
  1338. order = isl_union_map_eq_at_multi_union_pw_aff(order,
  1339. isl_multi_union_pw_aff_copy(prefix));
  1340. if (!isl_union_map_is_empty(order)) {
  1341. local->force_private = 1;
  1342. kernel->any_force_private = 1;
  1343. }
  1344. isl_union_map_free(order);
  1345. }
  1346. isl_multi_union_pw_aff_free(prefix);
  1347. isl_union_set_free(domain);
  1348. }
  1349. /* Expand the domain of the schedule "s" by plugging in
  1350. * the contraction "contraction" and return the result.
  1351. */
  1352. static __isl_give isl_union_map *expand(__isl_take isl_union_map *s,
  1353. __isl_keep isl_union_pw_multi_aff *contraction)
  1354. {
  1355. contraction = isl_union_pw_multi_aff_copy(contraction);
  1356. s = isl_union_map_preimage_domain_union_pw_multi_aff(s, contraction);
  1357. return s;
  1358. }
  1359. /* Create a set of dimension data->thread_depth + data->n_thread
  1360. * that equates the residue of the final data->n_thread dimensions
  1361. * modulo the kernel->block_dim sizes to the thread identifiers.
  1362. * Store the computed set in data->privatization.
  1363. *
  1364. * The construction starts with the space of kernel->thread_filter,
  1365. * which is known to reference all thread identifiers.
  1366. */
  1367. static void compute_privatization(struct gpu_group_data *data,
  1368. struct ppcg_kernel *kernel)
  1369. {
  1370. int i;
  1371. isl_ctx *ctx;
  1372. isl_space *space;
  1373. isl_local_space *ls;
  1374. isl_set *set;
  1375. ctx = isl_union_map_get_ctx(data->shared_sched);
  1376. space = isl_union_set_get_space(kernel->thread_filter);
  1377. space = isl_space_set_from_params(space);
  1378. space = isl_space_add_dims(space, isl_dim_set,
  1379. data->thread_depth + data->n_thread);
  1380. set = isl_set_universe(space);
  1381. space = isl_set_get_space(set);
  1382. ls = isl_local_space_from_space(space);
  1383. for (i = 0; i < data->n_thread; ++i) {
  1384. isl_aff *aff, *aff2;
  1385. isl_constraint *c;
  1386. isl_val *v;
  1387. isl_id *id;
  1388. int pos;
  1389. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  1390. isl_dim_set, data->thread_depth + i);
  1391. v = isl_val_int_from_si(ctx, kernel->block_dim[i]);
  1392. aff = isl_aff_mod_val(aff, v);
  1393. id = isl_id_list_get_id(kernel->thread_ids, i);
  1394. pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
  1395. isl_id_free(id);
  1396. aff2 = isl_aff_var_on_domain(isl_local_space_copy(ls),
  1397. isl_dim_param, pos);
  1398. aff = isl_aff_sub(aff, aff2);
  1399. c = isl_equality_from_aff(aff);
  1400. set = isl_set_add_constraint(set, c);
  1401. }
  1402. isl_local_space_free(ls);
  1403. data->privatization = set;
  1404. }
  1405. /* Return the prefix schedule at "node" as a relation
  1406. * between domain elements and schedule dimensions after detecting
  1407. * equalities in this relation.
  1408. */
  1409. static __isl_give isl_union_map *prefix_with_equalities(
  1410. __isl_keep isl_schedule_node *node)
  1411. {
  1412. isl_union_map *schedule;
  1413. schedule = isl_schedule_node_get_prefix_schedule_relation(node);
  1414. schedule = isl_union_map_detect_equalities(schedule);
  1415. return schedule;
  1416. }
  1417. /* Group references of all arrays in "kernel".
  1418. * "node" points to the kernel mark.
  1419. * The mapping to shared memory in computed at the "shared" mark.
  1420. *
  1421. * We first extract all required schedule information into
  1422. * a gpu_group_data structure and then consider each array
  1423. * in turn.
  1424. */
  1425. int gpu_group_references(struct ppcg_kernel *kernel,
  1426. __isl_keep isl_schedule_node *node)
  1427. {
  1428. int i;
  1429. int r = 0;
  1430. isl_union_pw_multi_aff *contraction;
  1431. struct gpu_group_data data;
  1432. check_can_be_private_live_ranges(kernel, node);
  1433. data.scop = kernel->prog->scop;
  1434. data.kernel_depth = isl_schedule_node_get_schedule_depth(node);
  1435. data.host_sched = isl_schedule_node_get_prefix_schedule_relation(node);
  1436. node = isl_schedule_node_copy(node);
  1437. node = gpu_tree_move_down_to_shared(node, kernel->core);
  1438. data.shared_depth = isl_schedule_node_get_schedule_depth(node);
  1439. data.shared_sched = prefix_with_equalities(node);
  1440. node = gpu_tree_move_down_to_thread(node, kernel->core);
  1441. node = isl_schedule_node_child(node, 0);
  1442. data.thread_depth = isl_schedule_node_get_schedule_depth(node);
  1443. data.n_thread = isl_schedule_node_band_n_member(node);
  1444. if (data.thread_depth == data.shared_depth)
  1445. data.copy_sched = isl_union_map_copy(data.shared_sched);
  1446. else
  1447. data.copy_sched = prefix_with_equalities(node);
  1448. data.thread_sched = isl_union_map_copy(data.copy_sched);
  1449. data.thread_sched = isl_union_map_flat_range_product(data.thread_sched,
  1450. isl_schedule_node_band_get_partial_schedule_union_map(node));
  1451. data.thread_sched = isl_union_map_detect_equalities(data.thread_sched);
  1452. contraction = isl_union_pw_multi_aff_copy(kernel->contraction);
  1453. data.host_sched = expand(data.host_sched, contraction);
  1454. data.shared_sched = expand(data.shared_sched, contraction);
  1455. if (data.thread_depth == data.shared_depth) {
  1456. isl_union_map_free(data.copy_sched);
  1457. data.copy_sched = isl_union_map_copy(data.shared_sched);
  1458. } else {
  1459. data.copy_sched = expand(data.copy_sched, contraction);
  1460. }
  1461. data.thread_sched = expand(data.thread_sched, contraction);
  1462. isl_union_pw_multi_aff_free(contraction);
  1463. node = isl_schedule_node_child(node, 0);
  1464. data.full_sched = isl_union_map_copy(data.thread_sched);
  1465. data.full_sched = isl_union_map_flat_range_product(data.full_sched,
  1466. isl_schedule_node_get_subtree_schedule_union_map(node));
  1467. isl_schedule_node_free(node);
  1468. compute_privatization(&data, kernel);
  1469. for (i = 0; i < kernel->n_array; ++i) {
  1470. r = group_array_references(kernel, &kernel->array[i], &data);
  1471. if (r < 0)
  1472. break;
  1473. }
  1474. isl_union_map_free(data.host_sched);
  1475. isl_union_map_free(data.shared_sched);
  1476. isl_union_map_free(data.copy_sched);
  1477. isl_union_map_free(data.thread_sched);
  1478. isl_union_map_free(data.full_sched);
  1479. isl_set_free(data.privatization);
  1480. return r;
  1481. }
  1482. /* Given a description of an array tile "tile" and the "space"
  1483. *
  1484. * { D -> A }
  1485. *
  1486. * where D represents the first tile->depth schedule dimensions
  1487. * and A represents the array, construct an isl_multi_aff
  1488. *
  1489. * { [D[i] -> A[a]] -> A'[a'] }
  1490. *
  1491. * with A' a scaled down copy of A according to the shifts and strides
  1492. * in "tile". In particular,
  1493. *
  1494. * a' = (a + shift(i))/stride
  1495. *
  1496. * "insert_array" represents
  1497. *
  1498. * { [D -> A] -> D }
  1499. *
  1500. * and is used to insert A into the domain of functions that only
  1501. * reference D.
  1502. */
  1503. static __isl_give isl_multi_aff *strided_tile(
  1504. struct gpu_array_tile *tile, __isl_keep isl_space *space,
  1505. __isl_keep isl_multi_aff *insert_array)
  1506. {
  1507. int i;
  1508. isl_ctx *ctx;
  1509. isl_multi_aff *shift;
  1510. isl_multi_val *stride;
  1511. isl_space *space2;
  1512. isl_local_space *ls;
  1513. isl_multi_aff *tiling;
  1514. ctx = isl_space_get_ctx(space);
  1515. space2 = isl_space_domain(isl_space_copy(space));
  1516. ls = isl_local_space_from_space(space2);
  1517. space2 = isl_space_range(isl_space_copy(space));
  1518. stride = isl_multi_val_zero(space2);
  1519. shift = isl_multi_aff_zero(isl_space_copy(space));
  1520. for (i = 0; i < tile->n; ++i) {
  1521. struct gpu_array_bound *bound = &tile->bound[i];
  1522. isl_val *stride_i;
  1523. isl_aff *shift_i;
  1524. if (tile->bound[i].shift) {
  1525. stride_i = isl_val_copy(bound->stride);
  1526. shift_i = isl_aff_copy(bound->shift);
  1527. } else {
  1528. stride_i = isl_val_one(ctx);
  1529. shift_i = isl_aff_zero_on_domain(
  1530. isl_local_space_copy(ls));
  1531. }
  1532. stride = isl_multi_val_set_val(stride, i, stride_i);
  1533. shift = isl_multi_aff_set_aff(shift, i, shift_i);
  1534. }
  1535. isl_local_space_free(ls);
  1536. shift = isl_multi_aff_pullback_multi_aff(shift,
  1537. isl_multi_aff_copy(insert_array));
  1538. tiling = isl_multi_aff_range_map(isl_space_copy(space));
  1539. tiling = isl_multi_aff_add(tiling, shift);
  1540. tiling = isl_multi_aff_scale_down_multi_val(tiling, stride);
  1541. return tiling;
  1542. }
  1543. /* Compute a tiling for the array reference group "group".
  1544. *
  1545. * The tiling is of the form
  1546. *
  1547. * { [D[i] -> A[a]] -> T[t] }
  1548. *
  1549. * where D represents the first tile->depth schedule dimensions,
  1550. * A represents the global array and T represents the shared or
  1551. * private memory tile. The name of T is the name of the local
  1552. * array.
  1553. *
  1554. * If there is any stride in the accesses, then the mapping is
  1555. *
  1556. * t = (a + shift(i))/stride - lb(i)
  1557. *
  1558. * otherwise, it is simply
  1559. *
  1560. * t = a - lb(i)
  1561. */
  1562. void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group *group)
  1563. {
  1564. int i;
  1565. struct gpu_array_tile *tile;
  1566. isl_space *space;
  1567. isl_multi_aff *tiling, *lb, *insert_array;
  1568. isl_printer *p;
  1569. char *local_name;
  1570. tile = gpu_array_ref_group_tile(group);
  1571. if (!tile)
  1572. return;
  1573. space = isl_map_get_space(group->access);
  1574. space = isl_space_from_range(isl_space_range(space));
  1575. space = isl_space_add_dims(space, isl_dim_in, tile->depth);
  1576. insert_array = isl_multi_aff_domain_map(isl_space_copy(space));
  1577. for (i = 0; i < tile->n; ++i)
  1578. if (tile->bound[i].shift)
  1579. break;
  1580. if (i < tile->n)
  1581. tiling = strided_tile(tile, space, insert_array);
  1582. else
  1583. tiling = isl_multi_aff_range_map(isl_space_copy(space));
  1584. lb = isl_multi_aff_zero(space);
  1585. for (i = 0; i < tile->n; ++i) {
  1586. isl_aff *lb_i = isl_aff_copy(tile->bound[i].lb);
  1587. lb = isl_multi_aff_set_aff(lb, i, lb_i);
  1588. }
  1589. lb = isl_multi_aff_pullback_multi_aff(lb, insert_array);
  1590. tiling = isl_multi_aff_sub(tiling, lb);
  1591. p = isl_printer_to_str(isl_multi_aff_get_ctx(tiling));
  1592. p = gpu_array_ref_group_print_name(group, p);
  1593. local_name = isl_printer_get_str(p);
  1594. isl_printer_free(p);
  1595. tiling = isl_multi_aff_set_tuple_name(tiling, isl_dim_out, local_name);
  1596. free(local_name);
  1597. tile->tiling = tiling;
  1598. }