cuda.c: extract out copy_array_{to,from}_device
[ppcg.git] / gpu_group.c
blob5c6407584a08a6b09df3f8cb8db4bfcf865173be
1 #include <isl/ilp.h>
3 #include "gpu_array_tile.h"
4 #include "gpu_group.h"
5 #include "gpu_tree.h"
6 #include "schedule.h"
8 /* Print the name of the local copy of a given group of array references.
9 */
10 __isl_give isl_printer *gpu_array_ref_group_print_name(
11 struct gpu_array_ref_group *group, __isl_take isl_printer *p)
13 int global = 0;
15 if (group->private_tile)
16 p = isl_printer_print_str(p, "private_");
17 else if (group->shared_tile)
18 p = isl_printer_print_str(p, "shared_");
19 else
20 global = 1;
21 p = isl_printer_print_str(p, group->array->name);
22 if (!global && group->local_array->n_group > 1) {
23 p = isl_printer_print_str(p, "_");
24 p = isl_printer_print_int(p, group->nr);
27 return p;
30 /* Return the union of all read (read = 1) and/or write (write = 1)
31 * access relations in the group.
33 __isl_give isl_union_map *gpu_array_ref_group_access_relation(
34 struct gpu_array_ref_group *group, int read, int write)
36 int i;
37 isl_union_map *access;
39 access = isl_union_map_empty(isl_map_get_space(group->access));
40 for (i = 0; i < group->n_ref; ++i) {
41 isl_map *map_i;
43 if (!((read && group->refs[i]->read) ||
44 (write && group->refs[i]->write)))
45 continue;
46 map_i = isl_map_copy(group->refs[i]->access);
47 access = isl_union_map_union(access,
48 isl_union_map_from_map(map_i));
51 return access;
54 /* Return the effective gpu_array_tile associated to "group" or
55 * NULL if there is no such gpu_array_tile.
56 * If we have computed both a private and a shared tile, then
57 * the private tile is used.
59 struct gpu_array_tile *gpu_array_ref_group_tile(
60 struct gpu_array_ref_group *group)
62 if (group->private_tile)
63 return group->private_tile;
64 if (group->shared_tile)
65 return group->shared_tile;
66 return NULL;
69 /* Does the tile associated to "group" require unrolling of the schedule
70 * dimensions mapped to threads?
71 * Note that this can only happen for private tiles.
73 int gpu_array_ref_group_requires_unroll(struct gpu_array_ref_group *group)
75 struct gpu_array_tile *tile;
77 tile = gpu_array_ref_group_tile(group);
78 if (!tile)
79 return 0;
80 return tile->requires_unroll;
83 /* Given a constraint
85 * a(p,i) + j = g f(e)
87 * or -a(p,i) - j = g f(e) if sign < 0,
88 * store a(p,i) in bound->shift and g (stride) in bound->stride.
89 * a(p,i) is assumed to be an expression in only the parameters
90 * and the input dimensions.
92 static void extract_stride(__isl_keep isl_constraint *c,
93 struct gpu_array_bound *bound, __isl_keep isl_val *stride, int sign)
95 int i;
96 isl_val *v;
97 isl_space *space;
98 unsigned nparam;
99 unsigned nvar;
100 isl_aff *aff;
102 isl_val_free(bound->stride);
103 bound->stride = isl_val_copy(stride);
105 space = isl_constraint_get_space(c);
106 space = isl_space_domain(space);
108 nparam = isl_space_dim(space, isl_dim_param);
109 nvar = isl_space_dim(space, isl_dim_set);
111 v = isl_constraint_get_constant_val(c);
112 if (sign < 0)
113 v = isl_val_neg(v);
114 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
115 aff = isl_aff_set_constant_val(aff, v);
117 for (i = 0; i < nparam; ++i) {
118 if (!isl_constraint_involves_dims(c, isl_dim_param, i, 1))
119 continue;
120 v = isl_constraint_get_coefficient_val(c, isl_dim_param, i);
121 if (sign < 0)
122 v = isl_val_neg(v);
123 aff = isl_aff_add_coefficient_val(aff, isl_dim_param, i, v);
126 for (i = 0; i < nvar; ++i) {
127 if (!isl_constraint_involves_dims(c, isl_dim_in, i, 1))
128 continue;
129 v = isl_constraint_get_coefficient_val(c, isl_dim_in, i);
130 if (sign < 0)
131 v = isl_val_neg(v);
132 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, i, v);
135 bound->shift = aff;
138 /* Given an equality constraint of a map with a single output dimension j,
139 * check if the constraint is of the form
141 * a(p,i) + j = g f(e)
143 * with a(p,i) an expression in the parameters and input dimensions
144 * and f(e) an expression in the existentially quantified variables.
145 * If so, and if g is larger than any such g from a previously considered
146 * constraint, then call extract_stride to record the stride information
147 * in bound.
149 static int check_stride_constraint(__isl_take isl_constraint *c, void *user)
151 int i;
152 isl_ctx *ctx;
153 isl_val *v;
154 unsigned n_div;
155 struct gpu_array_bound *bound = user;
157 ctx = isl_constraint_get_ctx(c);
158 n_div = isl_constraint_dim(c, isl_dim_div);
159 v = isl_constraint_get_coefficient_val(c, isl_dim_out, 0);
161 if (n_div && (isl_val_is_one(v) || isl_val_is_negone(v))) {
162 int s = isl_val_sgn(v);
163 isl_val *stride = isl_val_zero(ctx);
165 isl_val_free(v);
166 for (i = 0; i < n_div; ++i) {
167 v = isl_constraint_get_coefficient_val(c,
168 isl_dim_div, i);
169 stride = isl_val_gcd(stride, v);
171 if (!isl_val_is_zero(stride) &&
172 isl_val_gt(stride, bound->stride))
173 extract_stride(c, bound, stride, s);
175 isl_val_free(stride);
176 } else
177 isl_val_free(v);
179 isl_constraint_free(c);
180 return 0;
183 /* Given contraints on an array index i, check if we can find
184 * a shift a(p) and a stride g such that
186 * a(p) + i = 0 mod g
188 * If so, record the information in bound and apply the mapping
189 * i -> (i + a(p))/g to the array index in bounds and return
190 * the new constraints.
191 * If not, simply return the original constraints.
193 * If bounds is a subset of the space
195 * D -> i
197 * then the bound recorded in bound->shift is of the form
199 * D -> s(D)
201 * with s(D) equal to a(p) above.
202 * Next, we construct a mapping of the form
204 * [D -> i] -> [D -> (i + S(D))/g]
206 * This mapping is computed as follows.
207 * We first introduce "i" in the domain through precomposition
208 * with [D -> i] -> D obtaining
210 * [D -> i] -> s(D)
212 * Adding [D -> i] -> i produces
214 * [D -> i] -> i + s(D)
216 * and the domain product with [D -> i] -> D yields
218 * [D -> i] -> [D -> i + s(D)]
220 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
222 static __isl_give isl_basic_map *check_stride(struct gpu_array_bound *bound,
223 __isl_take isl_basic_map *bounds)
225 isl_space *space;
226 isl_basic_map *hull;
227 isl_basic_map *shift, *id, *bmap, *scale;
228 isl_basic_set *bset;
229 isl_aff *aff;
231 bound->stride = NULL;
233 hull = isl_basic_map_affine_hull(isl_basic_map_copy(bounds));
235 isl_basic_map_foreach_constraint(hull, &check_stride_constraint, bound);
237 isl_basic_map_free(hull);
239 if (!bound->stride)
240 return bounds;
242 shift = isl_basic_map_from_aff(isl_aff_copy(bound->shift));
243 space = isl_basic_map_get_space(bounds);
244 bmap = isl_basic_map_domain_map(isl_basic_map_universe(space));
245 shift = isl_basic_map_apply_range(bmap, shift);
246 space = isl_basic_map_get_space(bounds);
247 id = isl_basic_map_range_map(isl_basic_map_universe(space));
248 shift = isl_basic_map_sum(id, shift);
249 space = isl_basic_map_get_space(bounds);
250 id = isl_basic_map_domain_map(isl_basic_map_universe(space));
251 shift = isl_basic_map_range_product(id, shift);
253 space = isl_space_domain(isl_basic_map_get_space(bounds));
254 id = isl_basic_map_identity(isl_space_map_from_set(space));
255 space = isl_space_range(isl_basic_map_get_space(bounds));
256 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
257 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
258 aff = isl_aff_scale_down_val(aff, isl_val_copy(bound->stride));
259 scale = isl_basic_map_from_aff(aff);
260 scale = isl_basic_map_product(id, scale);
262 bmap = isl_basic_map_apply_range(shift, scale);
263 bset = isl_basic_set_apply(isl_basic_map_wrap(bounds), bmap);
264 bounds = isl_basic_set_unwrap(bset);
266 return bounds;
269 /* Data used in compute_array_dim_size and compute_size_in_direction.
271 * pos is the position of the variable representing the array index,
272 * i.e., the variable for which want to compute the size. This variable
273 * is also the last variable in the set.
275 struct gpu_size_info {
276 isl_basic_set *bset;
277 struct gpu_array_bound *bound;
278 int pos;
281 /* Given a constraint from the basic set describing the bounds on
282 * an array index, check if it is a lower bound, say m i >= b(x), and,
283 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
284 * upper bound. If so, and if this bound is smaller than any bound
285 * derived from earlier constraints, set the size to this bound on
286 * the expression and the lower bound to ceil(b(x)/m).
288 static int compute_size_in_direction(__isl_take isl_constraint *c, void *user)
290 struct gpu_size_info *size = user;
291 unsigned nparam;
292 unsigned n_div;
293 isl_val *v;
294 isl_aff *aff;
295 isl_aff *lb;
297 nparam = isl_basic_set_dim(size->bset, isl_dim_param);
298 n_div = isl_constraint_dim(c, isl_dim_div);
300 if (isl_constraint_involves_dims(c, isl_dim_div, 0, n_div) ||
301 !isl_constraint_is_lower_bound(c, isl_dim_set, size->pos)) {
302 isl_constraint_free(c);
303 return 0;
306 aff = isl_constraint_get_bound(c, isl_dim_set, size->pos);
307 aff = isl_aff_ceil(aff);
309 lb = isl_aff_copy(aff);
311 aff = isl_aff_neg(aff);
312 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, size->pos, 1);
314 v = isl_basic_set_max_val(size->bset, aff);
315 isl_aff_free(aff);
317 if (isl_val_is_int(v)) {
318 v = isl_val_add_ui(v, 1);
319 if (!size->bound->size || isl_val_lt(v, size->bound->size)) {
320 isl_val_free(size->bound->size);
321 size->bound->size = isl_val_copy(v);
322 lb = isl_aff_drop_dims(lb, isl_dim_in, size->pos, 1);
323 isl_aff_free(size->bound->lb);
324 size->bound->lb = isl_aff_copy(lb);
327 isl_val_free(v);
328 isl_aff_free(lb);
330 isl_constraint_free(c);
332 return 0;
335 /* Given a basic map "bounds" that maps parameters and input dimensions
336 * to a single output dimension, look for an expression in the parameters
337 * and input dimensions such that the range of the output dimension shifted
338 * by this expression is a constant.
340 * In particular, we currently only consider lower bounds on the output
341 * dimension as candidate expressions.
343 static int compute_array_dim_size(struct gpu_array_bound *bound,
344 __isl_take isl_basic_map *bounds)
346 struct gpu_size_info size;
348 bounds = isl_basic_map_detect_equalities(bounds);
349 bounds = check_stride(bound, bounds);
351 bound->size = NULL;
352 bound->lb = NULL;
354 size.bound = bound;
355 size.pos = isl_basic_map_dim(bounds, isl_dim_in);
356 size.bset = isl_basic_map_wrap(bounds);
357 size.bset = isl_basic_set_flatten(size.bset);
358 size.bset = isl_set_simple_hull(isl_basic_set_compute_divs(size.bset));
359 isl_basic_set_foreach_constraint(size.bset, &compute_size_in_direction,
360 &size);
361 isl_basic_set_free(size.bset);
363 return bound->size ? 0 : -1;
366 /* Check if we can find a memory tile for the given array
367 * based on the given accesses, and if so, put the results in "tile".
369 * We project the accesses on each index in turn and look for a parametric
370 * offset such that the size is constant.
372 static int can_tile(__isl_keep isl_map *access, struct gpu_array_tile *tile)
374 int i;
376 for (i = 0; i < tile->n; ++i) {
377 isl_map *access_i;
378 isl_basic_map *hull;
380 access_i = isl_map_copy(access);
381 access_i = isl_map_project_out(access_i, isl_dim_out, 0, i);
382 access_i = isl_map_project_out(access_i, isl_dim_out,
383 1, tile->n - (i + 1));
384 access_i = isl_map_compute_divs(access_i);
385 hull = isl_map_simple_hull(access_i);
386 if (compute_array_dim_size(&tile->bound[i], hull) < 0)
387 return 0;
390 return 1;
393 /* Internal data structure for gpu_group_references.
395 * scop represents the input scop.
396 * kernel_depth is the schedule depth where the kernel launch will
397 * be introduced, i.e., it is the depth of the band that is mapped
398 * to blocks.
399 * thread_depth is the schedule depth where the thread mark is located,
400 * i.e., it is the depth of the band that is mapped to threads and also
401 * the schedule depth at which the copying to/from shared/private memory
402 * is computed. The copy operation may then later be hoisted to
403 * a higher level.
404 * n_thread is the number of schedule dimensions in the band that
405 * is mapped to threads.
406 * privatization lives in the range of thread_sched (i.e., it is
407 * of dimension thread_depth + n_thread) and encodes the mapping
408 * to thread identifiers (as parameters).
409 * host_sched contains the kernel_depth dimensions of the host schedule.
410 * shared_sched contains the first thread_depth dimensions of the
411 * kernel schedule.
412 * thread_sched contains the first (thread_depth + n_thread) dimensions
413 * of the kernel schedule.
414 * full_sched is a union_map representation of the entire kernel schedule.
416 struct gpu_group_data {
417 struct ppcg_scop *scop;
418 int kernel_depth;
419 int thread_depth;
420 int n_thread;
421 isl_set *privatization;
422 isl_union_map *host_sched;
423 isl_union_map *shared_sched;
424 isl_union_map *thread_sched;
425 isl_union_map *full_sched;
428 /* Construct a map from domain_dim to domain_dim that increments
429 * the dimension at position "pos" and leaves all other dimensions
430 * constant.
432 static __isl_give isl_map *next(__isl_take isl_space *domain_dim, int pos)
434 int i;
435 int len = isl_space_dim(domain_dim, isl_dim_set);
436 isl_space *dim;
437 isl_basic_map *next;
438 isl_local_space *ls;
440 dim = isl_space_map_from_set(domain_dim);
441 next = isl_basic_map_universe(isl_space_copy(dim));
442 ls = isl_local_space_from_space(dim);
444 for (i = 0; i < len; ++i) {
445 isl_constraint *c;
447 c = isl_equality_alloc(isl_local_space_copy(ls));
448 c = isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
449 c = isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
450 if (i == pos)
451 c = isl_constraint_set_constant_si(c, 1);
452 next = isl_basic_map_add_constraint(next, c);
455 isl_local_space_free(ls);
457 return isl_map_from_basic_map(next);
460 /* Check if the given access is coalesced (or if there is no point
461 * in trying to coalesce the access by mapping the array to shared memory).
462 * That is, check whether incrementing the dimension that will get
463 * wrapped over the last thread index results in incrementing
464 * the last array index.
466 * If no two consecutive array elements are ever accessed by "access",
467 * then mapping the corresponding array to shared memory will not
468 * improve coalescing. In fact, the copying will likely be performed
469 * by a single thread. Consider the access as coalesced such that
470 * the caller will not try and map the array to shared memory just
471 * to improve coalescing.
473 * This function is only called for access relations without reuse and
474 * kernels with at least one thread identifier.
476 static int access_is_coalesced(struct gpu_group_data *data,
477 __isl_keep isl_union_map *access)
479 isl_space *space;
480 isl_set *accessed;
481 isl_map *access_map;
482 isl_map *next_thread_x;
483 isl_map *next_element;
484 isl_map *map;
485 int coalesced, empty;
487 access = isl_union_map_copy(access);
488 access = isl_union_map_apply_domain(access,
489 isl_union_map_copy(data->full_sched));
490 access_map = isl_map_from_union_map(access);
492 space = isl_map_get_space(access_map);
493 space = isl_space_range(space);
494 next_element = next(space, isl_space_dim(space, isl_dim_set) - 1);
496 accessed = isl_map_range(isl_map_copy(access_map));
497 map = isl_map_copy(next_element);
498 map = isl_map_intersect_domain(map, isl_set_copy(accessed));
499 map = isl_map_intersect_range(map, accessed);
500 empty = isl_map_is_empty(map);
501 isl_map_free(map);
503 if (empty < 0 || empty) {
504 isl_map_free(next_element);
505 isl_map_free(access_map);
506 return empty;
509 space = isl_map_get_space(access_map);
510 space = isl_space_domain(space);
511 next_thread_x = next(space, data->thread_depth + data->n_thread - 1);
513 map = isl_map_apply_domain(next_thread_x, isl_map_copy(access_map));
514 map = isl_map_apply_range(map, access_map);
516 coalesced = isl_map_is_subset(map, next_element);
518 isl_map_free(next_element);
519 isl_map_free(map);
521 return coalesced;
524 /* Replace the host schedule dimensions in the access relation "access"
525 * by parameters, so that they are treated as fixed when checking for reuse
526 * (within a kernel) or whether two consecutive elements are accessed
527 * (within a kernel).
529 static __isl_give isl_union_map *localize_access(struct gpu_group_data *data,
530 __isl_take isl_union_map *access)
532 int n;
533 isl_space *space;
534 isl_set *param;
535 isl_union_map *umap;
536 isl_id_list *ids;
538 umap = isl_union_map_copy(data->host_sched);
539 space = isl_union_map_get_space(umap);
540 n = data->kernel_depth;
541 ids = ppcg_scop_generate_names(data->scop, n, "__ppcg_host_");
542 param = parametrization(space, n, 0, ids);
543 isl_id_list_free(ids);
544 umap = isl_union_map_intersect_range(umap,
545 isl_union_set_from_set(param));
546 access = isl_union_map_intersect_domain(access,
547 isl_union_map_domain(umap));
549 return access;
552 /* Given an access relation in terms of at least data->thread_depth initial
553 * dimensions of the computed schedule, check if it is bijective for
554 * fixed values of the first data->thread_depth dimensions.
555 * We perform this check by equating these dimensions to parameters.
557 static int access_is_bijective(struct gpu_group_data *data,
558 __isl_keep isl_map *access)
560 int res;
561 int dim;
562 isl_set *par;
563 isl_space *space;
564 isl_id_list *ids;
566 access = isl_map_copy(access);
567 space = isl_space_params(isl_map_get_space(access));
568 ids = ppcg_scop_generate_names(data->scop, data->thread_depth, "s");
569 dim = isl_map_dim(access, isl_dim_in);
570 par = parametrization(space, dim, 0, ids);
571 isl_id_list_free(ids);
572 access = isl_map_intersect_domain(access, par);
573 res = isl_map_is_bijective(access);
574 isl_map_free(access);
576 return res;
579 /* Compute the number of outer schedule tile dimensions that affect
580 * the offset of "tile".
581 * If there is no such dimension, then return the index
582 * of the first kernel dimension, i.e., data->kernel_depth.
584 static int compute_tile_depth(struct gpu_group_data *data,
585 struct gpu_array_tile *tile)
587 int i, j;
589 for (j = data->thread_depth - 1; j >= data->kernel_depth; --j) {
590 for (i = 0; i < tile->n; ++i) {
591 isl_aff *lb;
592 isl_aff *shift;
594 lb = tile->bound[i].lb;
595 if (isl_aff_involves_dims(lb, isl_dim_in, j, 1))
596 break;
598 shift = tile->bound[i].shift;
599 if (!shift)
600 continue;
601 if (isl_aff_involves_dims(shift, isl_dim_in, j, 1))
602 break;
604 if (i < tile->n)
605 break;
608 return ++j;
611 /* Adjust the fields of "tile" to reflect the new input dimension "new_dim",
612 * where "old_dim" is the old dimension.
613 * The dimension beyond "new_dim" are assumed not to affect the tile,
614 * so they can simply be dropped.
616 static int tile_adjust_depth(struct gpu_array_tile *tile,
617 int old_dim, int new_dim)
619 int i;
621 if (old_dim == new_dim)
622 return 0;
624 for (i = 0; i < tile->n; ++i) {
625 tile->bound[i].lb = isl_aff_drop_dims(tile->bound[i].lb,
626 isl_dim_in, new_dim, old_dim - new_dim);
627 if (!tile->bound[i].lb)
628 return -1;
629 if (!tile->bound[i].shift)
630 continue;
631 tile->bound[i].shift = isl_aff_drop_dims(tile->bound[i].shift,
632 isl_dim_in, new_dim, old_dim - new_dim);
633 if (!tile->bound[i].shift)
634 return -1;
637 return 0;
640 /* Determine the number of schedule dimensions that affect the offset of the
641 * shared or private tile and store the result in group->depth, with
642 * a lower bound of data->kernel_depth.
643 * If there is no tile defined on the array reference group,
644 * then set group->depth to data->thread_depth.
645 * Also adjust the fields of the tile to only refer to the group->depth
646 * outer schedule dimensions.
648 static int set_depth(struct gpu_group_data *data,
649 struct gpu_array_ref_group *group)
651 struct gpu_array_tile *tile;
653 group->depth = data->thread_depth;
655 tile = gpu_array_ref_group_tile(group);
656 if (!tile)
657 return 0;
659 group->depth = compute_tile_depth(data, tile);
660 if (tile_adjust_depth(tile, data->thread_depth, group->depth) < 0)
661 return -1;
663 return 0;
666 /* Fill up the groups array with singleton groups, i.e., one group
667 * per reference, initializing the array, access, write, n_ref and refs fields.
668 * In particular the access field is initialized to the scheduled
669 * access relation of the array reference.
671 * Return the number of elements initialized, i.e., the number of
672 * active references in the current kernel.
674 static int populate_array_references(struct gpu_local_array_info *local,
675 struct gpu_array_ref_group **groups, struct gpu_group_data *data)
677 int i;
678 int n;
679 isl_ctx *ctx = isl_union_map_get_ctx(data->shared_sched);
681 n = 0;
682 for (i = 0; i < local->array->n_ref; ++i) {
683 isl_union_map *umap;
684 isl_map *map;
685 struct gpu_array_ref_group *group;
686 struct gpu_stmt_access *access = local->array->refs[i];
688 map = isl_map_copy(access->access);
689 umap = isl_union_map_from_map(map);
690 umap = isl_union_map_apply_domain(umap,
691 isl_union_map_copy(data->shared_sched));
693 if (isl_union_map_is_empty(umap)) {
694 isl_union_map_free(umap);
695 continue;
698 map = isl_map_from_union_map(umap);
699 map = isl_map_detect_equalities(map);
701 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
702 if (!group)
703 return -1;
704 group->local_array = local;
705 group->array = local->array;
706 group->access = map;
707 group->write = access->write;
708 group->exact_write = access->exact_write;
709 group->slice = access->n_index < local->array->n_index;
710 group->refs = &local->array->refs[i];
711 group->n_ref = 1;
713 groups[n++] = group;
716 return n;
719 /* If group->n_ref == 1, then group->refs was set by
720 * populate_array_references to point directly into
721 * group->array->refs and should not be freed.
722 * If group->n_ref > 1, then group->refs was set by join_groups
723 * to point to a newly allocated array.
725 struct gpu_array_ref_group *gpu_array_ref_group_free(
726 struct gpu_array_ref_group *group)
728 if (!group)
729 return NULL;
730 gpu_array_tile_free(group->shared_tile);
731 gpu_array_tile_free(group->private_tile);
732 isl_map_free(group->access);
733 if (group->n_ref > 1)
734 free(group->refs);
735 free(group);
736 return NULL;
739 /* Check if the access relations of group1 and group2 overlap within
740 * shared_sched.
742 static int accesses_overlap(struct gpu_array_ref_group *group1,
743 struct gpu_array_ref_group *group2)
745 int disjoint;
747 disjoint = isl_map_is_disjoint(group1->access, group2->access);
748 if (disjoint < 0)
749 return -1;
751 return !disjoint;
754 /* Combine the given two groups into a single group, containing
755 * the references of both groups.
757 static struct gpu_array_ref_group *join_groups(
758 struct gpu_array_ref_group *group1,
759 struct gpu_array_ref_group *group2)
761 int i;
762 isl_ctx *ctx;
763 struct gpu_array_ref_group *group;
765 ctx = isl_map_get_ctx(group1->access);
766 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
767 if (!group)
768 return NULL;
769 group->local_array = group1->local_array;
770 group->array = group1->array;
771 group->access = isl_map_union(isl_map_copy(group1->access),
772 isl_map_copy(group2->access));
773 group->write = group1->write || group2->write;
774 group->exact_write = group1->exact_write && group2->exact_write;
775 group->slice = group1->slice || group2->slice;
776 group->n_ref = group1->n_ref + group2->n_ref;
777 group->refs = isl_alloc_array(ctx, struct gpu_stmt_access *,
778 group->n_ref);
779 if (!group->refs)
780 return gpu_array_ref_group_free(group);
781 for (i = 0; i < group1->n_ref; ++i)
782 group->refs[i] = group1->refs[i];
783 for (i = 0; i < group2->n_ref; ++i)
784 group->refs[group1->n_ref + i] = group2->refs[i];
786 return group;
789 /* Combine the given two groups into a single group and free
790 * the original two groups.
792 static struct gpu_array_ref_group *join_groups_and_free(
793 struct gpu_array_ref_group *group1,
794 struct gpu_array_ref_group *group2)
796 struct gpu_array_ref_group *group;
798 group = join_groups(group1, group2);
799 gpu_array_ref_group_free(group1);
800 gpu_array_ref_group_free(group2);
801 return group;
804 /* Report that the array reference group with the given access relation
805 * is not mapped to shared memory in the given kernel because
806 * it does not exhibit any reuse and is considered to be coalesced.
808 static void report_no_reuse_and_coalesced(struct ppcg_kernel *kernel,
809 __isl_keep isl_union_map *access)
811 isl_ctx *ctx;
812 isl_printer *p;
814 ctx = isl_union_map_get_ctx(access);
815 p = isl_printer_to_file(ctx, stdout);
816 p = isl_printer_print_str(p, "Array reference group ");
817 p = isl_printer_print_union_map(p, access);
818 p = isl_printer_print_str(p,
819 " not considered for mapping to shared memory in kernel");
820 p = isl_printer_print_int(p, kernel->id);
821 p = isl_printer_print_str(p,
822 " because it exhibits no reuse and is considered to be coalesced");
823 p = isl_printer_end_line(p);
824 isl_printer_free(p);
827 /* Given an access relation in terms of the data->thread_depth initial
828 * dimensions of the computed schedule and the thread identifiers
829 * (as parameters), check if the use of the corresponding private tile
830 * requires unrolling.
832 * If we are creating a private tile because we are forced to,
833 * then no unrolling is required.
834 * Otherwise we check if "access" is bijective and unrolling
835 * is required if it is not. Note that the access relation
836 * has already been determined to be bijective before the introduction
837 * of the thread identifiers and the removal of the schedule dimensions
838 * that are mapped to these threads. If the access relation is no longer
839 * bijective, then this means that more than one value of one of those
840 * schedule dimensions is mapped to the same thread and therefore
841 * unrolling is required.
843 static int check_requires_unroll(struct gpu_group_data *data,
844 __isl_keep isl_map *access, int force_private)
846 int bijective;
848 if (force_private)
849 return 0;
850 bijective = access_is_bijective(data, access);
851 if (bijective < 0)
852 return -1;
853 return !bijective;
856 /* Compute the private and/or shared memory tiles for the array
857 * reference group "group" of array "array".
858 * Return 0 on success and -1 on error.
860 * If the array is a read-only scalar or if the user requested
861 * not to use shared or private memory, then we do not need to do anything.
863 * If any reference in the reference group accesses more than one element,
864 * then we would have to make sure that the layout in shared memory
865 * is the same as that in global memory. Since we do not handle this yet
866 * (and it may not even be possible), we refuse to map to private or
867 * shared memory in such cases.
869 * If the array group involves any may writes (that are not must writes),
870 * then we would have to make sure that we load the data into shared/private
871 * memory first in case the data is not written by the kernel
872 * (but still written back out to global memory).
873 * Since we don't have any such mechanism at the moment, we don't
874 * compute shared/private tiles for groups involving may writes.
876 * We only try to compute a shared memory tile if there is any reuse
877 * or if the access is not coalesced.
878 * Reuse and coalescing are checked within the given kernel.
880 * For computing a private memory tile, we also require that there is
881 * some reuse. Moreover, we require that the access is private
882 * to the thread. That is, we check that any given array element
883 * is only accessed by a single thread.
884 * We compute an access relation that maps the outer
885 * data->thread_depth + data->n_thread schedule dimensions.
886 * The latter data->n_thread will be mapped to thread identifiers.
887 * We actually check that those iterators that will be wrapped
888 * partition the array space. This check is stricter than necessary
889 * since several iterations may be mapped onto the same thread
890 * and then they could be allowed to access the same memory elements,
891 * but our check does not allow this situation.
893 * We also check that the index expression only depends on parallel
894 * loops. That way, we can move those loops innermost and unroll them.
895 * Again, we use a test that is stricter than necessary.
896 * We actually check whether the index expression only depends
897 * on the iterators that are wrapped over the threads.
898 * These are necessarily parallel, but there may be more parallel loops.
900 * Combining the injectivity of the first test with the single-valuedness
901 * of the second test, we simply test for bijectivity.
903 * If the use of the private tile requires unrolling, but some
904 * of the other arrays are forcibly mapped to private memory,
905 * then we do not allow the use of this private tile since
906 * we cannot move the schedule dimensions that need to be unrolled down
907 * without performing some kind of expansion on those arrays
908 * that are forcibly mapped to private memory.
910 * If the array is marked force_private, then we bypass all checks
911 * and assume we can (and should) use registers.
913 * If it turns out we can (or have to) use registers, we compute
914 * the private memory tile size using can_tile, after introducing a dependence
915 * on the thread indices.
917 static int compute_group_bounds_core(struct ppcg_kernel *kernel,
918 struct gpu_array_ref_group *group, struct gpu_group_data *data)
920 isl_ctx *ctx = isl_space_get_ctx(group->array->space);
921 isl_union_map *access, *local;
922 int n_index = group->array->n_index;
923 int no_reuse, coalesced;
924 isl_map *acc;
925 int force_private = group->local_array->force_private;
926 int use_shared = kernel->options->use_shared_memory &&
927 data->n_thread > 0;
928 int use_private = force_private || kernel->options->use_private_memory;
929 int r = 0;
930 int requires_unroll;
932 if (!use_shared && !use_private)
933 return 0;
934 if (gpu_array_is_read_only_scalar(group->array))
935 return 0;
936 if (!force_private && !group->exact_write)
937 return 0;
938 if (group->slice)
939 return 0;
941 access = gpu_array_ref_group_access_relation(group, 1, 1);
942 local = localize_access(data, isl_union_map_copy(access));
943 no_reuse = isl_union_map_is_injective(local);
944 if (no_reuse < 0)
945 r = -1;
946 if (use_shared && no_reuse)
947 coalesced = access_is_coalesced(data, local);
948 isl_union_map_free(local);
950 if (r >= 0 && kernel->options->debug->verbose &&
951 use_shared && no_reuse && coalesced)
952 report_no_reuse_and_coalesced(kernel, access);
954 if (use_shared && (!no_reuse || !coalesced)) {
955 group->shared_tile = gpu_array_tile_create(ctx,
956 group->array->n_index);
957 if (!group->shared_tile)
958 r = -1;
959 else if (!can_tile(group->access, group->shared_tile))
960 group->shared_tile =
961 gpu_array_tile_free(group->shared_tile);
964 if (r < 0 || (!force_private && (!use_private || no_reuse))) {
965 isl_union_map_free(access);
966 return r;
969 access = isl_union_map_apply_domain(access,
970 isl_union_map_copy(data->thread_sched));
972 acc = isl_map_from_union_map(access);
974 if (!force_private && !access_is_bijective(data, acc)) {
975 isl_map_free(acc);
976 return 0;
979 acc = isl_map_intersect_domain(acc, isl_set_copy(data->privatization));
980 acc = isl_map_project_out(acc, isl_dim_in, data->thread_depth,
981 data->n_thread);
982 requires_unroll = check_requires_unroll(data, acc, force_private);
983 if (requires_unroll < 0 ||
984 (requires_unroll && kernel->any_force_private)) {
985 isl_map_free(acc);
986 return requires_unroll < 0 ? -1 : 0;
989 group->private_tile = gpu_array_tile_create(ctx, n_index);
990 if (!group->private_tile) {
991 isl_map_free(acc);
992 return -1;
994 group->private_tile->requires_unroll = requires_unroll;
995 if (!can_tile(acc, group->private_tile))
996 group->private_tile = gpu_array_tile_free(group->private_tile);
998 isl_map_free(acc);
1000 if (force_private && !group->private_tile)
1001 isl_die(ctx, isl_error_internal,
1002 "unable to map array reference group to registers",
1003 return -1);
1005 return 0;
1008 /* Compute the private and/or shared memory tiles for the array
1009 * reference group "group" of array "array" and set the tile depth.
1010 * Return 0 on success and -1 on error.
1012 static int compute_group_bounds(struct ppcg_kernel *kernel,
1013 struct gpu_array_ref_group *group, struct gpu_group_data *data)
1015 if (!group)
1016 return -1;
1017 if (compute_group_bounds_core(kernel, group, data) < 0)
1018 return -1;
1019 if (set_depth(data, group) < 0)
1020 return -1;
1022 return 0;
1025 /* If two groups have overlapping access relations (as determined by
1026 * the "overlap" function) and if one of them involves a write,
1027 * then merge the two groups into one.
1028 * If "compute_bounds" is set, then call compute_group_bounds
1029 * on the merged groups.
1031 * Return the updated number of groups.
1032 * Return -1 on error.
1034 static int group_writes(struct ppcg_kernel *kernel,
1035 int n, struct gpu_array_ref_group **groups,
1036 int (*overlap)(struct gpu_array_ref_group *group1,
1037 struct gpu_array_ref_group *group2), int compute_bounds,
1038 struct gpu_group_data *data)
1040 int i, j;
1042 for (i = 0; i < n; ++i) {
1043 for (j = n - 1; j > i; --j) {
1044 if (!groups[i]->write && !groups[j]->write)
1045 continue;
1047 if (!overlap(groups[i], groups[j]))
1048 continue;
1050 groups[i] = join_groups_and_free(groups[i], groups[j]);
1051 if (j != n - 1)
1052 groups[j] = groups[n - 1];
1053 groups[n - 1] = NULL;
1054 n--;
1056 if (!groups[i])
1057 return -1;
1058 if (compute_bounds &&
1059 compute_group_bounds(kernel, groups[i], data) < 0)
1060 return -1;
1064 return n;
1067 /* If two groups have overlapping access relations (within the innermost
1068 * loop) and if one of them involves a write, then merge the two groups
1069 * into one.
1071 * Return the updated number of groups.
1073 static int group_overlapping_writes(struct ppcg_kernel *kernel,
1074 int n, struct gpu_array_ref_group **groups,
1075 struct gpu_group_data *data)
1077 return group_writes(kernel, n, groups, &accesses_overlap, 0, data);
1080 /* Check if the access relations of group1 and group2 overlap within
1081 * the outermost min(group1->depth, group2->depth) loops.
1083 static int depth_accesses_overlap(struct gpu_array_ref_group *group1,
1084 struct gpu_array_ref_group *group2)
1086 int depth;
1087 int dim;
1088 int empty;
1089 isl_map *map_i, *map_j, *map;
1091 depth = group1->depth;
1092 if (group2->depth < depth)
1093 depth = group2->depth;
1094 map_i = isl_map_copy(group1->access);
1095 dim = isl_map_dim(map_i, isl_dim_in);
1096 map_i = isl_map_eliminate(map_i, isl_dim_in, depth, dim - depth);
1097 map_j = isl_map_copy(group2->access);
1098 map_j = isl_map_eliminate(map_j, isl_dim_in, depth, dim - depth);
1099 map = isl_map_intersect(map_i, map_j);
1100 empty = isl_map_is_empty(map);
1101 isl_map_free(map);
1103 return !empty;
1106 /* If two groups have overlapping access relations (within the outer
1107 * depth loops) and if one of them involves a write,
1108 * then merge the two groups into one.
1110 * Return the updated number of groups.
1112 static int group_depth_overlapping_writes(struct ppcg_kernel *kernel,
1113 int n, struct gpu_array_ref_group **groups, struct gpu_group_data *data)
1115 return group_writes(kernel, n, groups, &depth_accesses_overlap, 1,
1116 data);
1119 /* Is the size of the tile specified by "tile" smaller than the sum of
1120 * the sizes of the tiles specified by "tile1" and "tile2"?
1122 static int smaller_tile(struct gpu_array_tile *tile,
1123 struct gpu_array_tile *tile1, struct gpu_array_tile *tile2)
1125 int smaller;
1126 isl_val *size, *size1, *size2;
1128 size = gpu_array_tile_size(tile);
1129 size1 = gpu_array_tile_size(tile1);
1130 size2 = gpu_array_tile_size(tile2);
1132 size = isl_val_sub(size, size1);
1133 size = isl_val_sub(size, size2);
1134 smaller = isl_val_is_neg(size);
1136 isl_val_free(size);
1138 return smaller;
1141 /* Given an initial grouping of array references and shared memory tiles
1142 * for each group that allows for a shared memory tile, merge two groups
1143 * if both have a shared memory tile, the merged group also has
1144 * a shared memory tile and the size of the tile for the merge group
1145 * is smaller than the sum of the tile sizes of the individual groups.
1147 * If merging two groups decreases the depth of the tile of
1148 * one or both of the two groups, then we need to check for overlapping
1149 * writes again.
1151 * Return the number of groups after merging.
1152 * Return -1 on error.
1154 static int group_common_shared_memory_tile(struct ppcg_kernel *kernel,
1155 struct gpu_array_info *array, int n,
1156 struct gpu_array_ref_group **groups, struct gpu_group_data *data)
1158 int i, j;
1159 int recompute_overlap = 0;
1160 isl_ctx *ctx = isl_space_get_ctx(array->space);
1162 for (i = 0; i < n; ++i) {
1163 if (!groups[i]->shared_tile)
1164 continue;
1165 for (j = n - 1; j > i; --j) {
1166 isl_map *map;
1167 int empty;
1168 struct gpu_array_ref_group *group;
1170 if (!groups[j]->shared_tile)
1171 continue;
1173 map = isl_map_intersect(isl_map_copy(groups[i]->access),
1174 isl_map_copy(groups[j]->access));
1175 empty = isl_map_is_empty(map);
1176 isl_map_free(map);
1178 if (empty)
1179 continue;
1181 group = join_groups(groups[i], groups[j]);
1182 if (compute_group_bounds(kernel, group, data) < 0) {
1183 gpu_array_ref_group_free(group);
1184 return -1;
1186 if (!group->shared_tile ||
1187 !smaller_tile(group->shared_tile,
1188 groups[i]->shared_tile,
1189 groups[j]->shared_tile)) {
1190 gpu_array_ref_group_free(group);
1191 continue;
1194 if (group->depth < groups[i]->depth ||
1195 group->depth < groups[j]->depth)
1196 recompute_overlap = 1;
1197 gpu_array_ref_group_free(groups[i]);
1198 gpu_array_ref_group_free(groups[j]);
1199 groups[i] = group;
1200 if (j != n - 1)
1201 groups[j] = groups[n - 1];
1202 n--;
1206 if (recompute_overlap)
1207 n = group_depth_overlapping_writes(kernel, n, groups, data);
1208 return n;
1211 /* Set array->n_group and array->groups to n and groups.
1213 * Additionally, set the "nr" field of each group.
1215 static void set_array_groups(struct gpu_local_array_info *array,
1216 int n, struct gpu_array_ref_group **groups)
1218 int i, j;
1220 array->n_group = n;
1221 array->groups = groups;
1223 for (i = 0; i < n; ++i)
1224 groups[i]->nr = i;
1227 /* Group array references that should be considered together when
1228 * deciding whether to access them from private, shared or global memory.
1229 * Return -1 on error.
1231 * In particular, if two array references overlap and if one of them
1232 * is a write, then the two references are grouped together.
1233 * We first perform an initial grouping based only on the access relation.
1234 * After computing shared and private memory tiles, we check for
1235 * overlapping writes again, but this time taking into account
1236 * the depth of the effective tile.
1238 * Furthermore, if two groups admit a shared memory tile and if the
1239 * combination of the two also admits a shared memory tile, we merge
1240 * the two groups.
1242 * If the array contains structures, then there is no need to compute
1243 * reference groups since we do not map such arrays to private or shared
1244 * memory.
1246 static int group_array_references(struct ppcg_kernel *kernel,
1247 struct gpu_local_array_info *local, struct gpu_group_data *data)
1249 int i;
1250 int n;
1251 isl_ctx *ctx = isl_union_map_get_ctx(data->shared_sched);
1252 struct gpu_array_ref_group **groups;
1254 if (local->array->has_compound_element)
1255 return 0;
1257 groups = isl_calloc_array(ctx, struct gpu_array_ref_group *,
1258 local->array->n_ref);
1259 if (!groups)
1260 return -1;
1262 n = populate_array_references(local, groups, data);
1264 n = group_overlapping_writes(kernel, n, groups, data);
1266 for (i = 0; i < n; ++i)
1267 if (compute_group_bounds(kernel, groups[i], data) < 0)
1268 n = -1;
1270 n = group_depth_overlapping_writes(kernel, n, groups, data);
1272 n = group_common_shared_memory_tile(kernel, local->array,
1273 n, groups, data);
1275 set_array_groups(local, n, groups);
1277 if (n >= 0)
1278 return 0;
1280 for (i = 0; i < local->array->n_ref; ++i)
1281 gpu_array_ref_group_free(groups[i]);
1282 return -1;
1285 /* For each scalar in the input program, check if there are any
1286 * order dependences active inside the current kernel, within
1287 * the same iteration of "host_schedule".
1288 * If so, mark the scalar as force_private so that it will be
1289 * mapped to a register.
1291 static void check_scalar_live_ranges_in_host(struct ppcg_kernel *kernel,
1292 __isl_take isl_union_map *host_schedule)
1294 int i;
1295 isl_union_map *sched;
1296 isl_union_set *domain;
1297 isl_union_map *same_host_iteration;
1299 kernel->any_force_private = 0;
1301 sched = isl_union_map_universe(isl_union_map_copy(host_schedule));
1302 domain = isl_union_map_domain(sched);
1304 same_host_iteration = isl_union_map_apply_range(host_schedule,
1305 isl_union_map_reverse(isl_union_map_copy(host_schedule)));
1307 for (i = 0; i < kernel->n_array; ++i) {
1308 struct gpu_local_array_info *local = &kernel->array[i];
1309 isl_union_map *order;
1311 local->force_private = 0;
1312 if (local->array->n_index != 0)
1313 continue;
1314 order = isl_union_map_copy(local->array->dep_order);
1315 order = isl_union_map_intersect_domain(order,
1316 isl_union_set_copy(domain));
1317 order = isl_union_map_intersect_range(order,
1318 isl_union_set_copy(domain));
1319 order = isl_union_map_intersect(order,
1320 isl_union_map_copy(same_host_iteration));
1321 if (!isl_union_map_is_empty(order)) {
1322 local->force_private = 1;
1323 kernel->any_force_private = 1;
1325 isl_union_map_free(order);
1328 isl_union_map_free(same_host_iteration);
1329 isl_union_set_free(domain);
1332 /* For each scalar in the input program, check if there are any
1333 * order dependences active inside the current kernel, within
1334 * the same iteration of the host schedule, i.e., the prefix
1335 * schedule at "node".
1336 * If so, mark the scalar as force_private so that it will be
1337 * mapped to a register.
1339 static void check_scalar_live_ranges(struct ppcg_kernel *kernel,
1340 __isl_keep isl_schedule_node *node)
1342 isl_union_map *sched;
1344 if (!kernel->options->live_range_reordering)
1345 return;
1347 sched = isl_schedule_node_get_prefix_schedule_union_map(node);
1349 check_scalar_live_ranges_in_host(kernel, sched);
1352 /* Create a set of dimension data->thread_depth + data->n_thread
1353 * that equates the residue of the final data->n_thread dimensions
1354 * modulo the "sizes" to the thread identifiers.
1355 * "space" is a parameter space containing the thread identifiers.
1356 * Store the computed set in data->privatization.
1358 static void compute_privatization(struct gpu_group_data *data,
1359 __isl_take isl_space *space, int *sizes)
1361 int i;
1362 isl_ctx *ctx;
1363 isl_local_space *ls;
1364 isl_set *set;
1366 ctx = isl_union_map_get_ctx(data->shared_sched);
1367 space = isl_space_set_from_params(space);
1368 space = isl_space_add_dims(space, isl_dim_set,
1369 data->thread_depth + data->n_thread);
1370 set = isl_set_universe(space);
1371 space = isl_set_get_space(set);
1372 ls = isl_local_space_from_space(space);
1374 for (i = 0; i < data->n_thread; ++i) {
1375 isl_aff *aff, *aff2;
1376 isl_constraint *c;
1377 isl_val *v;
1378 char name[20];
1379 int pos;
1381 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1382 isl_dim_set, data->thread_depth + i);
1383 v = isl_val_int_from_si(ctx, sizes[i]);
1384 aff = isl_aff_mod_val(aff, v);
1385 snprintf(name, sizeof(name), "t%d", i);
1386 pos = isl_set_find_dim_by_name(set, isl_dim_param, name);
1387 aff2 = isl_aff_var_on_domain(isl_local_space_copy(ls),
1388 isl_dim_param, pos);
1389 aff = isl_aff_sub(aff, aff2);
1390 c = isl_equality_from_aff(aff);
1391 set = isl_set_add_constraint(set, c);
1394 isl_local_space_free(ls);
1395 data->privatization = set;
1398 /* Group references of all arrays in "kernel".
1399 * "node" points to the kernel mark.
1401 * We first extract all required schedule information into
1402 * a gpu_group_data structure and then consider each array
1403 * in turn.
1405 int gpu_group_references(struct ppcg_kernel *kernel,
1406 __isl_keep isl_schedule_node *node)
1408 int i;
1409 int r = 0;
1410 isl_space *space;
1411 struct gpu_group_data data;
1413 check_scalar_live_ranges(kernel, node);
1415 data.scop = kernel->prog->scop;
1417 data.kernel_depth = isl_schedule_node_get_schedule_depth(node);
1418 data.host_sched = isl_schedule_node_get_prefix_schedule_relation(node);
1420 node = isl_schedule_node_copy(node);
1421 node = gpu_tree_move_down_to_thread(node, kernel->core);
1422 data.shared_sched =
1423 isl_schedule_node_get_prefix_schedule_relation(node);
1424 data.shared_sched = isl_union_map_detect_equalities(data.shared_sched);
1426 node = isl_schedule_node_child(node, 0);
1427 data.thread_depth = isl_schedule_node_get_schedule_depth(node);
1428 data.n_thread = isl_schedule_node_band_n_member(node);
1429 data.thread_sched = isl_union_map_copy(data.shared_sched);
1430 data.thread_sched = isl_union_map_flat_range_product(data.thread_sched,
1431 isl_schedule_node_band_get_partial_schedule_union_map(node));
1432 data.thread_sched = isl_union_map_detect_equalities(data.thread_sched);
1433 node = isl_schedule_node_child(node, 0);
1434 data.full_sched = isl_union_map_copy(data.thread_sched);
1435 data.full_sched = isl_union_map_flat_range_product(data.full_sched,
1436 isl_schedule_node_get_subtree_schedule_union_map(node));
1437 isl_schedule_node_free(node);
1439 space = isl_union_set_get_space(kernel->thread_filter);
1440 compute_privatization(&data, space, kernel->block_dim);
1442 for (i = 0; i < kernel->n_array; ++i) {
1443 r = group_array_references(kernel, &kernel->array[i], &data);
1444 if (r < 0)
1445 break;
1448 isl_union_map_free(data.host_sched);
1449 isl_union_map_free(data.shared_sched);
1450 isl_union_map_free(data.thread_sched);
1451 isl_union_map_free(data.full_sched);
1452 isl_set_free(data.privatization);
1454 return r;
1457 /* Given a description of an array tile "tile" and the "space"
1459 * { D -> A }
1461 * where D represents the first group->depth schedule dimensions
1462 * and A represents the array, construct an isl_multi_aff
1464 * { [D[i] -> A[a]] -> A'[a'] }
1466 * with A' a scaled down copy of A according to the shifts and strides
1467 * in "tile". In particular,
1469 * a' = (a + shift(i))/stride
1471 * "insert_array" represents
1473 * { [D -> A] -> D }
1475 * and is used to insert A into the domain of functions that only
1476 * reference D.
1478 static __isl_give isl_multi_aff *strided_tile(
1479 struct gpu_array_tile *tile, __isl_keep isl_space *space,
1480 __isl_keep isl_multi_aff *insert_array)
1482 int i;
1483 isl_ctx *ctx;
1484 isl_multi_aff *shift;
1485 isl_multi_val *stride;
1486 isl_space *space2;
1487 isl_local_space *ls;
1488 isl_multi_aff *tiling;
1490 ctx = isl_space_get_ctx(space);
1491 space2 = isl_space_domain(isl_space_copy(space));
1492 ls = isl_local_space_from_space(space2);
1493 space2 = isl_space_range(isl_space_copy(space));
1494 stride = isl_multi_val_zero(space2);
1495 shift = isl_multi_aff_zero(isl_space_copy(space));
1497 for (i = 0; i < tile->n; ++i) {
1498 struct gpu_array_bound *bound = &tile->bound[i];
1499 isl_val *stride_i;
1500 isl_aff *shift_i;
1502 if (tile->bound[i].shift) {
1503 stride_i = isl_val_copy(bound->stride);
1504 shift_i = isl_aff_copy(bound->shift);
1505 } else {
1506 stride_i = isl_val_one(ctx);
1507 shift_i = isl_aff_zero_on_domain(
1508 isl_local_space_copy(ls));
1511 stride = isl_multi_val_set_val(stride, i, stride_i);
1512 shift = isl_multi_aff_set_aff(shift, i, shift_i);
1514 isl_local_space_free(ls);
1516 shift = isl_multi_aff_pullback_multi_aff(shift,
1517 isl_multi_aff_copy(insert_array));
1519 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1520 tiling = isl_multi_aff_add(tiling, shift);
1521 tiling = isl_multi_aff_scale_down_multi_val(tiling, stride);
1523 return tiling;
1526 /* Compute a tiling for the array reference group "group".
1528 * The tiling is of the form
1530 * { [D[i] -> A[a]] -> T[t] }
1532 * where D represents the first group->depth schedule dimensions,
1533 * A represents the global array and T represents the shared or
1534 * private memory tile. The name of T is the name of the local
1535 * array.
1537 * If there is any stride in the accesses, then the mapping is
1539 * t = (a + shift(i))/stride - lb(i)
1541 * otherwise, it is simply
1543 * t = a - lb(i)
1545 void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group *group)
1547 int i;
1548 int dim;
1549 struct gpu_array_tile *tile;
1550 struct gpu_array_info *array = group->array;
1551 isl_space *space;
1552 isl_multi_aff *tiling, *lb, *insert_array;
1553 isl_printer *p;
1554 char *local_name;
1556 tile = group->private_tile;
1557 if (!tile)
1558 tile = group->shared_tile;
1559 if (!tile)
1560 return;
1562 space = isl_map_get_space(group->access);
1563 dim = isl_space_dim(space, isl_dim_in);
1564 space = isl_space_drop_dims(space, isl_dim_in, group->depth,
1565 dim - group->depth);
1566 insert_array = isl_multi_aff_domain_map(isl_space_copy(space));
1568 for (i = 0; i < tile->n; ++i)
1569 if (tile->bound[i].shift)
1570 break;
1572 if (i < tile->n)
1573 tiling = strided_tile(tile, space, insert_array);
1574 else
1575 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1577 lb = isl_multi_aff_zero(space);
1578 for (i = 0; i < tile->n; ++i) {
1579 isl_aff *lb_i = isl_aff_copy(tile->bound[i].lb);
1580 lb = isl_multi_aff_set_aff(lb, i, lb_i);
1582 lb = isl_multi_aff_pullback_multi_aff(lb, insert_array);
1584 tiling = isl_multi_aff_sub(tiling, lb);
1586 p = isl_printer_to_str(isl_multi_aff_get_ctx(tiling));
1587 p = gpu_array_ref_group_print_name(group, p);
1588 local_name = isl_printer_get_str(p);
1589 isl_printer_free(p);
1590 tiling = isl_multi_aff_set_tuple_name(tiling, isl_dim_out, local_name);
1591 free(local_name);
1593 tile->tiling = tiling;