make schedule.h self-contained
[ppcg.git] / gpu_group.c
blob061ea60287584ea9c8ef8a7bce1f187c732ea521
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 if (!group1 || !group2)
766 return NULL;
768 ctx = isl_map_get_ctx(group1->access);
769 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
770 if (!group)
771 return NULL;
772 group->local_array = group1->local_array;
773 group->array = group1->array;
774 group->access = isl_map_union(isl_map_copy(group1->access),
775 isl_map_copy(group2->access));
776 group->write = group1->write || group2->write;
777 group->exact_write = group1->exact_write && group2->exact_write;
778 group->slice = group1->slice || group2->slice;
779 group->n_ref = group1->n_ref + group2->n_ref;
780 group->refs = isl_alloc_array(ctx, struct gpu_stmt_access *,
781 group->n_ref);
782 if (!group->refs)
783 return gpu_array_ref_group_free(group);
784 for (i = 0; i < group1->n_ref; ++i)
785 group->refs[i] = group1->refs[i];
786 for (i = 0; i < group2->n_ref; ++i)
787 group->refs[group1->n_ref + i] = group2->refs[i];
789 return group;
792 /* Combine the given two groups into a single group and free
793 * the original two groups.
795 static struct gpu_array_ref_group *join_groups_and_free(
796 struct gpu_array_ref_group *group1,
797 struct gpu_array_ref_group *group2)
799 struct gpu_array_ref_group *group;
801 group = join_groups(group1, group2);
802 gpu_array_ref_group_free(group1);
803 gpu_array_ref_group_free(group2);
804 return group;
807 /* Report that the array reference group with the given access relation
808 * is not mapped to shared memory in the given kernel because
809 * it does not exhibit any reuse and is considered to be coalesced.
811 static void report_no_reuse_and_coalesced(struct ppcg_kernel *kernel,
812 __isl_keep isl_union_map *access)
814 isl_ctx *ctx;
815 isl_printer *p;
817 ctx = isl_union_map_get_ctx(access);
818 p = isl_printer_to_file(ctx, stdout);
819 p = isl_printer_print_str(p, "Array reference group ");
820 p = isl_printer_print_union_map(p, access);
821 p = isl_printer_print_str(p,
822 " not considered for mapping to shared memory in kernel");
823 p = isl_printer_print_int(p, kernel->id);
824 p = isl_printer_print_str(p,
825 " because it exhibits no reuse and is considered to be coalesced");
826 p = isl_printer_end_line(p);
827 isl_printer_free(p);
830 /* Given an access relation in terms of the data->thread_depth initial
831 * dimensions of the computed schedule and the thread identifiers
832 * (as parameters), check if the use of the corresponding private tile
833 * requires unrolling.
835 * If we are creating a private tile because we are forced to,
836 * then no unrolling is required.
837 * Otherwise we check if "access" is bijective and unrolling
838 * is required if it is not. Note that the access relation
839 * has already been determined to be bijective before the introduction
840 * of the thread identifiers and the removal of the schedule dimensions
841 * that are mapped to these threads. If the access relation is no longer
842 * bijective, then this means that more than one value of one of those
843 * schedule dimensions is mapped to the same thread and therefore
844 * unrolling is required.
846 static int check_requires_unroll(struct gpu_group_data *data,
847 __isl_keep isl_map *access, int force_private)
849 int bijective;
851 if (force_private)
852 return 0;
853 bijective = access_is_bijective(data, access);
854 if (bijective < 0)
855 return -1;
856 return !bijective;
859 /* Compute the private and/or shared memory tiles for the array
860 * reference group "group" of array "array".
861 * Return 0 on success and -1 on error.
863 * If the array is a read-only scalar or if the user requested
864 * not to use shared or private memory, then we do not need to do anything.
866 * If any reference in the reference group accesses more than one element,
867 * then we would have to make sure that the layout in shared memory
868 * is the same as that in global memory. Since we do not handle this yet
869 * (and it may not even be possible), we refuse to map to private or
870 * shared memory in such cases.
872 * If the array group involves any may writes (that are not must writes),
873 * then we would have to make sure that we load the data into shared/private
874 * memory first in case the data is not written by the kernel
875 * (but still written back out to global memory).
876 * Since we don't have any such mechanism at the moment, we don't
877 * compute shared/private tiles for groups involving may writes.
879 * We only try to compute a shared memory tile if there is any reuse
880 * or if the access is not coalesced.
881 * Reuse and coalescing are checked within the given kernel.
883 * For computing a private memory tile, we also require that there is
884 * some reuse. Moreover, we require that the access is private
885 * to the thread. That is, we check that any given array element
886 * is only accessed by a single thread.
887 * We compute an access relation that maps the outer
888 * data->thread_depth + data->n_thread schedule dimensions.
889 * The latter data->n_thread will be mapped to thread identifiers.
890 * We actually check that those iterators that will be wrapped
891 * partition the array space. This check is stricter than necessary
892 * since several iterations may be mapped onto the same thread
893 * and then they could be allowed to access the same memory elements,
894 * but our check does not allow this situation.
896 * We also check that the index expression only depends on parallel
897 * loops. That way, we can move those loops innermost and unroll them.
898 * Again, we use a test that is stricter than necessary.
899 * We actually check whether the index expression only depends
900 * on the iterators that are wrapped over the threads.
901 * These are necessarily parallel, but there may be more parallel loops.
903 * Combining the injectivity of the first test with the single-valuedness
904 * of the second test, we simply test for bijectivity.
906 * If the use of the private tile requires unrolling, but some
907 * of the other arrays are forcibly mapped to private memory,
908 * then we do not allow the use of this private tile since
909 * we cannot move the schedule dimensions that need to be unrolled down
910 * without performing some kind of expansion on those arrays
911 * that are forcibly mapped to private memory.
913 * If the array is marked force_private, then we bypass all checks
914 * and assume we can (and should) use registers.
916 * If it turns out we can (or have to) use registers, we compute
917 * the private memory tile size using can_tile, after introducing a dependence
918 * on the thread indices.
920 static int compute_group_bounds_core(struct ppcg_kernel *kernel,
921 struct gpu_array_ref_group *group, struct gpu_group_data *data)
923 isl_ctx *ctx = isl_space_get_ctx(group->array->space);
924 isl_union_map *access, *local;
925 int n_index = group->array->n_index;
926 int no_reuse, coalesced;
927 isl_map *acc;
928 int force_private = group->local_array->force_private;
929 int use_shared = kernel->options->use_shared_memory &&
930 data->n_thread > 0;
931 int use_private = force_private || kernel->options->use_private_memory;
932 int r = 0;
933 int requires_unroll;
935 if (!use_shared && !use_private)
936 return 0;
937 if (gpu_array_is_read_only_scalar(group->array))
938 return 0;
939 if (!force_private && !group->exact_write)
940 return 0;
941 if (group->slice)
942 return 0;
944 access = gpu_array_ref_group_access_relation(group, 1, 1);
945 local = localize_access(data, isl_union_map_copy(access));
946 no_reuse = isl_union_map_is_injective(local);
947 if (no_reuse < 0)
948 r = -1;
949 if (use_shared && no_reuse)
950 coalesced = access_is_coalesced(data, local);
951 isl_union_map_free(local);
953 if (r >= 0 && kernel->options->debug->verbose &&
954 use_shared && no_reuse && coalesced)
955 report_no_reuse_and_coalesced(kernel, access);
957 if (use_shared && (!no_reuse || !coalesced)) {
958 group->shared_tile = gpu_array_tile_create(ctx,
959 group->array->n_index);
960 if (!group->shared_tile)
961 r = -1;
962 else if (!can_tile(group->access, group->shared_tile))
963 group->shared_tile =
964 gpu_array_tile_free(group->shared_tile);
967 if (r < 0 || (!force_private && (!use_private || no_reuse))) {
968 isl_union_map_free(access);
969 return r;
972 access = isl_union_map_apply_domain(access,
973 isl_union_map_copy(data->thread_sched));
975 acc = isl_map_from_union_map(access);
977 if (!force_private && !access_is_bijective(data, acc)) {
978 isl_map_free(acc);
979 return 0;
982 acc = isl_map_intersect_domain(acc, isl_set_copy(data->privatization));
983 acc = isl_map_project_out(acc, isl_dim_in, data->thread_depth,
984 data->n_thread);
985 requires_unroll = check_requires_unroll(data, acc, force_private);
986 if (requires_unroll < 0 ||
987 (requires_unroll && kernel->any_force_private)) {
988 isl_map_free(acc);
989 return requires_unroll < 0 ? -1 : 0;
992 group->private_tile = gpu_array_tile_create(ctx, n_index);
993 if (!group->private_tile) {
994 isl_map_free(acc);
995 return -1;
997 group->private_tile->requires_unroll = requires_unroll;
998 if (!can_tile(acc, group->private_tile))
999 group->private_tile = gpu_array_tile_free(group->private_tile);
1001 isl_map_free(acc);
1003 if (force_private && !group->private_tile)
1004 isl_die(ctx, isl_error_internal,
1005 "unable to map array reference group to registers",
1006 return -1);
1008 return 0;
1011 /* Compute the private and/or shared memory tiles for the array
1012 * reference group "group" of array "array" and set the tile depth.
1013 * Return 0 on success and -1 on error.
1015 static int compute_group_bounds(struct ppcg_kernel *kernel,
1016 struct gpu_array_ref_group *group, struct gpu_group_data *data)
1018 if (!group)
1019 return -1;
1020 if (compute_group_bounds_core(kernel, group, data) < 0)
1021 return -1;
1022 if (set_depth(data, group) < 0)
1023 return -1;
1025 return 0;
1028 /* If two groups have overlapping access relations (as determined by
1029 * the "overlap" function) and if one of them involves a write,
1030 * then merge the two groups into one.
1031 * If "compute_bounds" is set, then call compute_group_bounds
1032 * on the merged groups.
1034 * Return the updated number of groups.
1035 * Return -1 on error.
1037 static int group_writes(struct ppcg_kernel *kernel,
1038 int n, struct gpu_array_ref_group **groups,
1039 int (*overlap)(struct gpu_array_ref_group *group1,
1040 struct gpu_array_ref_group *group2), int compute_bounds,
1041 struct gpu_group_data *data)
1043 int i, j;
1045 for (i = 0; i < n; ++i) {
1046 for (j = n - 1; j > i; --j) {
1047 if (!groups[i]->write && !groups[j]->write)
1048 continue;
1050 if (!overlap(groups[i], groups[j]))
1051 continue;
1053 groups[i] = join_groups_and_free(groups[i], groups[j]);
1054 if (j != n - 1)
1055 groups[j] = groups[n - 1];
1056 groups[n - 1] = NULL;
1057 n--;
1059 if (!groups[i])
1060 return -1;
1061 if (compute_bounds &&
1062 compute_group_bounds(kernel, groups[i], data) < 0)
1063 return -1;
1067 return n;
1070 /* If two groups have overlapping access relations (within the innermost
1071 * loop) and if one of them involves a write, then merge the two groups
1072 * into one.
1074 * Return the updated number of groups.
1076 static int group_overlapping_writes(struct ppcg_kernel *kernel,
1077 int n, struct gpu_array_ref_group **groups,
1078 struct gpu_group_data *data)
1080 return group_writes(kernel, n, groups, &accesses_overlap, 0, data);
1083 /* Check if the access relations of group1 and group2 overlap within
1084 * the outermost min(group1->depth, group2->depth) loops.
1086 static int depth_accesses_overlap(struct gpu_array_ref_group *group1,
1087 struct gpu_array_ref_group *group2)
1089 int depth;
1090 int dim;
1091 int empty;
1092 isl_map *map_i, *map_j, *map;
1094 depth = group1->depth;
1095 if (group2->depth < depth)
1096 depth = group2->depth;
1097 map_i = isl_map_copy(group1->access);
1098 dim = isl_map_dim(map_i, isl_dim_in);
1099 map_i = isl_map_eliminate(map_i, isl_dim_in, depth, dim - depth);
1100 map_j = isl_map_copy(group2->access);
1101 map_j = isl_map_eliminate(map_j, isl_dim_in, depth, dim - depth);
1102 map = isl_map_intersect(map_i, map_j);
1103 empty = isl_map_is_empty(map);
1104 isl_map_free(map);
1106 return !empty;
1109 /* If two groups have overlapping access relations (within the outer
1110 * depth loops) and if one of them involves a write,
1111 * then merge the two groups into one.
1113 * Return the updated number of groups.
1115 static int group_depth_overlapping_writes(struct ppcg_kernel *kernel,
1116 int n, struct gpu_array_ref_group **groups, struct gpu_group_data *data)
1118 return group_writes(kernel, n, groups, &depth_accesses_overlap, 1,
1119 data);
1122 /* Is the size of the tile specified by "tile" smaller than the sum of
1123 * the sizes of the tiles specified by "tile1" and "tile2"?
1125 static int smaller_tile(struct gpu_array_tile *tile,
1126 struct gpu_array_tile *tile1, struct gpu_array_tile *tile2)
1128 int smaller;
1129 isl_val *size, *size1, *size2;
1131 size = gpu_array_tile_size(tile);
1132 size1 = gpu_array_tile_size(tile1);
1133 size2 = gpu_array_tile_size(tile2);
1135 size = isl_val_sub(size, size1);
1136 size = isl_val_sub(size, size2);
1137 smaller = isl_val_is_neg(size);
1139 isl_val_free(size);
1141 return smaller;
1144 /* Given an initial grouping of array references and shared memory tiles
1145 * for each group that allows for a shared memory tile, merge two groups
1146 * if both have a shared memory tile, the merged group also has
1147 * a shared memory tile and the size of the tile for the merge group
1148 * is smaller than the sum of the tile sizes of the individual groups.
1150 * If merging two groups decreases the depth of the tile of
1151 * one or both of the two groups, then we need to check for overlapping
1152 * writes again.
1154 * Return the number of groups after merging.
1155 * Return -1 on error.
1157 static int group_common_shared_memory_tile(struct ppcg_kernel *kernel,
1158 struct gpu_array_info *array, int n,
1159 struct gpu_array_ref_group **groups, struct gpu_group_data *data)
1161 int i, j;
1162 int recompute_overlap = 0;
1163 isl_ctx *ctx = isl_space_get_ctx(array->space);
1165 for (i = 0; i < n; ++i) {
1166 if (!groups[i]->shared_tile)
1167 continue;
1168 for (j = n - 1; j > i; --j) {
1169 isl_map *map;
1170 int empty;
1171 struct gpu_array_ref_group *group;
1173 if (!groups[j]->shared_tile)
1174 continue;
1176 map = isl_map_intersect(isl_map_copy(groups[i]->access),
1177 isl_map_copy(groups[j]->access));
1178 empty = isl_map_is_empty(map);
1179 isl_map_free(map);
1181 if (empty)
1182 continue;
1184 group = join_groups(groups[i], groups[j]);
1185 if (compute_group_bounds(kernel, group, data) < 0) {
1186 gpu_array_ref_group_free(group);
1187 return -1;
1189 if (!group->shared_tile ||
1190 !smaller_tile(group->shared_tile,
1191 groups[i]->shared_tile,
1192 groups[j]->shared_tile)) {
1193 gpu_array_ref_group_free(group);
1194 continue;
1197 if (group->depth < groups[i]->depth ||
1198 group->depth < groups[j]->depth)
1199 recompute_overlap = 1;
1200 gpu_array_ref_group_free(groups[i]);
1201 gpu_array_ref_group_free(groups[j]);
1202 groups[i] = group;
1203 if (j != n - 1)
1204 groups[j] = groups[n - 1];
1205 n--;
1209 if (recompute_overlap)
1210 n = group_depth_overlapping_writes(kernel, n, groups, data);
1211 return n;
1214 /* Set array->n_group and array->groups to n and groups.
1216 * Additionally, set the "nr" field of each group.
1218 static void set_array_groups(struct gpu_local_array_info *array,
1219 int n, struct gpu_array_ref_group **groups)
1221 int i, j;
1223 array->n_group = n;
1224 array->groups = groups;
1226 for (i = 0; i < n; ++i)
1227 groups[i]->nr = i;
1230 /* Combine all groups in "groups" into a single group and return
1231 * the new number of groups (1 or 0 if there were no groups to start with).
1233 static int join_all_groups(int n, struct gpu_array_ref_group **groups)
1235 int i;
1237 for (i = n - 1; i > 0; --i) {
1238 groups[0] = join_groups_and_free(groups[0], groups[i]);
1239 groups[i] = NULL;
1240 n--;
1243 return n;
1246 /* Group array references that should be considered together when
1247 * deciding whether to access them from private, shared or global memory.
1248 * Return -1 on error.
1250 * In particular, if two array references overlap and if one of them
1251 * is a write, then the two references are grouped together.
1252 * We first perform an initial grouping based only on the access relation.
1253 * After computing shared and private memory tiles, we check for
1254 * overlapping writes again, but this time taking into account
1255 * the depth of the effective tile.
1257 * Furthermore, if two groups admit a shared memory tile and if the
1258 * combination of the two also admits a shared memory tile, we merge
1259 * the two groups.
1261 * If the array contains structures, then we compute a single
1262 * reference group without trying to find any tiles
1263 * since we do not map such arrays to private or shared
1264 * memory.
1266 static int group_array_references(struct ppcg_kernel *kernel,
1267 struct gpu_local_array_info *local, struct gpu_group_data *data)
1269 int i;
1270 int n;
1271 isl_ctx *ctx = isl_union_map_get_ctx(data->shared_sched);
1272 struct gpu_array_ref_group **groups;
1274 groups = isl_calloc_array(ctx, struct gpu_array_ref_group *,
1275 local->array->n_ref);
1276 if (!groups)
1277 return -1;
1279 n = populate_array_references(local, groups, data);
1281 if (local->array->has_compound_element) {
1282 n = join_all_groups(n, groups);
1283 set_array_groups(local, n, groups);
1284 return 0;
1287 n = group_overlapping_writes(kernel, n, groups, data);
1289 for (i = 0; i < n; ++i)
1290 if (compute_group_bounds(kernel, groups[i], data) < 0)
1291 n = -1;
1293 n = group_depth_overlapping_writes(kernel, n, groups, data);
1295 n = group_common_shared_memory_tile(kernel, local->array,
1296 n, groups, data);
1298 set_array_groups(local, n, groups);
1300 if (n >= 0)
1301 return 0;
1303 for (i = 0; i < local->array->n_ref; ++i)
1304 gpu_array_ref_group_free(groups[i]);
1305 return -1;
1308 /* For each scalar in the input program, check if there are any
1309 * order dependences active inside the current kernel, within
1310 * the same iteration of "host_schedule".
1311 * If so, mark the scalar as force_private so that it will be
1312 * mapped to a register.
1314 static void check_scalar_live_ranges_in_host(struct ppcg_kernel *kernel,
1315 __isl_take isl_union_map *host_schedule)
1317 int i;
1318 isl_union_map *sched;
1319 isl_union_set *domain;
1320 isl_union_map *same_host_iteration;
1322 kernel->any_force_private = 0;
1324 sched = isl_union_map_universe(isl_union_map_copy(host_schedule));
1325 domain = isl_union_map_domain(sched);
1327 same_host_iteration = isl_union_map_apply_range(host_schedule,
1328 isl_union_map_reverse(isl_union_map_copy(host_schedule)));
1330 for (i = 0; i < kernel->n_array; ++i) {
1331 struct gpu_local_array_info *local = &kernel->array[i];
1332 isl_union_map *order;
1334 local->force_private = 0;
1335 if (local->array->n_index != 0)
1336 continue;
1337 order = isl_union_map_copy(local->array->dep_order);
1338 order = isl_union_map_intersect_domain(order,
1339 isl_union_set_copy(domain));
1340 order = isl_union_map_intersect_range(order,
1341 isl_union_set_copy(domain));
1342 order = isl_union_map_intersect(order,
1343 isl_union_map_copy(same_host_iteration));
1344 if (!isl_union_map_is_empty(order)) {
1345 local->force_private = 1;
1346 kernel->any_force_private = 1;
1348 isl_union_map_free(order);
1351 isl_union_map_free(same_host_iteration);
1352 isl_union_set_free(domain);
1355 /* For each scalar in the input program, check if there are any
1356 * order dependences active inside the current kernel, within
1357 * the same iteration of the host schedule, i.e., the prefix
1358 * schedule at "node".
1359 * If so, mark the scalar as force_private so that it will be
1360 * mapped to a register.
1362 static void check_scalar_live_ranges(struct ppcg_kernel *kernel,
1363 __isl_keep isl_schedule_node *node)
1365 isl_union_map *sched;
1367 if (!kernel->options->live_range_reordering)
1368 return;
1370 sched = isl_schedule_node_get_prefix_schedule_union_map(node);
1372 check_scalar_live_ranges_in_host(kernel, sched);
1375 /* Create a set of dimension data->thread_depth + data->n_thread
1376 * that equates the residue of the final data->n_thread dimensions
1377 * modulo the "sizes" to the thread identifiers.
1378 * "space" is a parameter space containing the thread identifiers.
1379 * Store the computed set in data->privatization.
1381 static void compute_privatization(struct gpu_group_data *data,
1382 __isl_take isl_space *space, int *sizes)
1384 int i;
1385 isl_ctx *ctx;
1386 isl_local_space *ls;
1387 isl_set *set;
1389 ctx = isl_union_map_get_ctx(data->shared_sched);
1390 space = isl_space_set_from_params(space);
1391 space = isl_space_add_dims(space, isl_dim_set,
1392 data->thread_depth + data->n_thread);
1393 set = isl_set_universe(space);
1394 space = isl_set_get_space(set);
1395 ls = isl_local_space_from_space(space);
1397 for (i = 0; i < data->n_thread; ++i) {
1398 isl_aff *aff, *aff2;
1399 isl_constraint *c;
1400 isl_val *v;
1401 char name[20];
1402 int pos;
1404 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1405 isl_dim_set, data->thread_depth + i);
1406 v = isl_val_int_from_si(ctx, sizes[i]);
1407 aff = isl_aff_mod_val(aff, v);
1408 snprintf(name, sizeof(name), "t%d", i);
1409 pos = isl_set_find_dim_by_name(set, isl_dim_param, name);
1410 aff2 = isl_aff_var_on_domain(isl_local_space_copy(ls),
1411 isl_dim_param, pos);
1412 aff = isl_aff_sub(aff, aff2);
1413 c = isl_equality_from_aff(aff);
1414 set = isl_set_add_constraint(set, c);
1417 isl_local_space_free(ls);
1418 data->privatization = set;
1421 /* Group references of all arrays in "kernel".
1422 * "node" points to the kernel mark.
1424 * We first extract all required schedule information into
1425 * a gpu_group_data structure and then consider each array
1426 * in turn.
1428 int gpu_group_references(struct ppcg_kernel *kernel,
1429 __isl_keep isl_schedule_node *node)
1431 int i;
1432 int r = 0;
1433 isl_space *space;
1434 struct gpu_group_data data;
1436 check_scalar_live_ranges(kernel, node);
1438 data.scop = kernel->prog->scop;
1440 data.kernel_depth = isl_schedule_node_get_schedule_depth(node);
1441 data.host_sched = isl_schedule_node_get_prefix_schedule_relation(node);
1443 node = isl_schedule_node_copy(node);
1444 node = gpu_tree_move_down_to_thread(node, kernel->core);
1445 data.shared_sched =
1446 isl_schedule_node_get_prefix_schedule_relation(node);
1447 data.shared_sched = isl_union_map_detect_equalities(data.shared_sched);
1449 node = isl_schedule_node_child(node, 0);
1450 data.thread_depth = isl_schedule_node_get_schedule_depth(node);
1451 data.n_thread = isl_schedule_node_band_n_member(node);
1452 data.thread_sched = isl_union_map_copy(data.shared_sched);
1453 data.thread_sched = isl_union_map_flat_range_product(data.thread_sched,
1454 isl_schedule_node_band_get_partial_schedule_union_map(node));
1455 data.thread_sched = isl_union_map_detect_equalities(data.thread_sched);
1456 node = isl_schedule_node_child(node, 0);
1457 data.full_sched = isl_union_map_copy(data.thread_sched);
1458 data.full_sched = isl_union_map_flat_range_product(data.full_sched,
1459 isl_schedule_node_get_subtree_schedule_union_map(node));
1460 isl_schedule_node_free(node);
1462 space = isl_union_set_get_space(kernel->thread_filter);
1463 compute_privatization(&data, space, kernel->block_dim);
1465 for (i = 0; i < kernel->n_array; ++i) {
1466 r = group_array_references(kernel, &kernel->array[i], &data);
1467 if (r < 0)
1468 break;
1471 isl_union_map_free(data.host_sched);
1472 isl_union_map_free(data.shared_sched);
1473 isl_union_map_free(data.thread_sched);
1474 isl_union_map_free(data.full_sched);
1475 isl_set_free(data.privatization);
1477 return r;
1480 /* Given a description of an array tile "tile" and the "space"
1482 * { D -> A }
1484 * where D represents the first group->depth schedule dimensions
1485 * and A represents the array, construct an isl_multi_aff
1487 * { [D[i] -> A[a]] -> A'[a'] }
1489 * with A' a scaled down copy of A according to the shifts and strides
1490 * in "tile". In particular,
1492 * a' = (a + shift(i))/stride
1494 * "insert_array" represents
1496 * { [D -> A] -> D }
1498 * and is used to insert A into the domain of functions that only
1499 * reference D.
1501 static __isl_give isl_multi_aff *strided_tile(
1502 struct gpu_array_tile *tile, __isl_keep isl_space *space,
1503 __isl_keep isl_multi_aff *insert_array)
1505 int i;
1506 isl_ctx *ctx;
1507 isl_multi_aff *shift;
1508 isl_multi_val *stride;
1509 isl_space *space2;
1510 isl_local_space *ls;
1511 isl_multi_aff *tiling;
1513 ctx = isl_space_get_ctx(space);
1514 space2 = isl_space_domain(isl_space_copy(space));
1515 ls = isl_local_space_from_space(space2);
1516 space2 = isl_space_range(isl_space_copy(space));
1517 stride = isl_multi_val_zero(space2);
1518 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;
1525 if (tile->bound[i].shift) {
1526 stride_i = isl_val_copy(bound->stride);
1527 shift_i = isl_aff_copy(bound->shift);
1528 } else {
1529 stride_i = isl_val_one(ctx);
1530 shift_i = isl_aff_zero_on_domain(
1531 isl_local_space_copy(ls));
1534 stride = isl_multi_val_set_val(stride, i, stride_i);
1535 shift = isl_multi_aff_set_aff(shift, i, shift_i);
1537 isl_local_space_free(ls);
1539 shift = isl_multi_aff_pullback_multi_aff(shift,
1540 isl_multi_aff_copy(insert_array));
1542 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1543 tiling = isl_multi_aff_add(tiling, shift);
1544 tiling = isl_multi_aff_scale_down_multi_val(tiling, stride);
1546 return tiling;
1549 /* Compute a tiling for the array reference group "group".
1551 * The tiling is of the form
1553 * { [D[i] -> A[a]] -> T[t] }
1555 * where D represents the first group->depth schedule dimensions,
1556 * A represents the global array and T represents the shared or
1557 * private memory tile. The name of T is the name of the local
1558 * array.
1560 * If there is any stride in the accesses, then the mapping is
1562 * t = (a + shift(i))/stride - lb(i)
1564 * otherwise, it is simply
1566 * t = a - lb(i)
1568 void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group *group)
1570 int i;
1571 int dim;
1572 struct gpu_array_tile *tile;
1573 struct gpu_array_info *array = group->array;
1574 isl_space *space;
1575 isl_multi_aff *tiling, *lb, *insert_array;
1576 isl_printer *p;
1577 char *local_name;
1579 tile = group->private_tile;
1580 if (!tile)
1581 tile = group->shared_tile;
1582 if (!tile)
1583 return;
1585 space = isl_map_get_space(group->access);
1586 dim = isl_space_dim(space, isl_dim_in);
1587 space = isl_space_drop_dims(space, isl_dim_in, group->depth,
1588 dim - group->depth);
1589 insert_array = isl_multi_aff_domain_map(isl_space_copy(space));
1591 for (i = 0; i < tile->n; ++i)
1592 if (tile->bound[i].shift)
1593 break;
1595 if (i < tile->n)
1596 tiling = strided_tile(tile, space, insert_array);
1597 else
1598 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1600 lb = isl_multi_aff_zero(space);
1601 for (i = 0; i < tile->n; ++i) {
1602 isl_aff *lb_i = isl_aff_copy(tile->bound[i].lb);
1603 lb = isl_multi_aff_set_aff(lb, i, lb_i);
1605 lb = isl_multi_aff_pullback_multi_aff(lb, insert_array);
1607 tiling = isl_multi_aff_sub(tiling, lb);
1609 p = isl_printer_to_str(isl_multi_aff_get_ctx(tiling));
1610 p = gpu_array_ref_group_print_name(group, p);
1611 local_name = isl_printer_get_str(p);
1612 isl_printer_free(p);
1613 tiling = isl_multi_aff_set_tuple_name(tiling, isl_dim_out, local_name);
1614 free(local_name);
1616 tile->tiling = tiling;