gpu_array_tile: keep track of whether the tile requires unrolling
[ppcg.git] / gpu_group.c
blob17297a464b6a74d93fcb3d9aaea9ba4857bf8939
1 #include <isl/ilp.h>
3 #include "gpu_array_tile.h"
4 #include "gpu_group.h"
5 #include "schedule.h"
7 /* Print the name of the local copy of a given group of array references.
8 */
9 __isl_give isl_printer *gpu_array_ref_group_print_name(
10 struct gpu_array_ref_group *group, __isl_take isl_printer *p)
12 int global = 0;
14 if (group->private_tile)
15 p = isl_printer_print_str(p, "private_");
16 else if (group->shared_tile)
17 p = isl_printer_print_str(p, "shared_");
18 else
19 global = 1;
20 p = isl_printer_print_str(p, group->array->name);
21 if (!global && group->local_array->n_group > 1) {
22 p = isl_printer_print_str(p, "_");
23 p = isl_printer_print_int(p, group->nr);
26 return p;
29 /* Return the union of all read (read = 1) and/or write (write = 1)
30 * access relations in the group.
32 __isl_give isl_union_map *gpu_array_ref_group_access_relation(
33 struct gpu_array_ref_group *group, int read, int write)
35 int i;
36 isl_union_map *access;
38 access = isl_union_map_empty(isl_map_get_space(group->access));
39 for (i = 0; i < group->n_ref; ++i) {
40 isl_map *map_i;
42 if (!((read && group->refs[i]->read) ||
43 (write && group->refs[i]->write)))
44 continue;
45 map_i = isl_map_copy(group->refs[i]->access);
46 access = isl_union_map_union(access,
47 isl_union_map_from_map(map_i));
50 return access;
53 /* Return the effective gpu_array_tile associated to "group" or
54 * NULL if there is no such gpu_array_tile.
55 * If we have computed both a private and a shared tile, then
56 * the private tile is used.
58 struct gpu_array_tile *gpu_array_ref_group_tile(
59 struct gpu_array_ref_group *group)
61 if (group->private_tile)
62 return group->private_tile;
63 if (group->shared_tile)
64 return group->shared_tile;
65 return NULL;
68 /* Does the tile associated to "group" require unrolling of the schedule
69 * dimensions mapped to threads?
70 * Note that this can only happen for private tiles.
72 int gpu_array_ref_group_requires_unroll(struct gpu_array_ref_group *group)
74 struct gpu_array_tile *tile;
76 tile = gpu_array_ref_group_tile(group);
77 if (!tile)
78 return 0;
79 return tile->requires_unroll;
82 /* Given a constraint
84 * a(p,i) + j = g f(e)
86 * or -a(p,i) - j = g f(e) if sign < 0,
87 * store a(p,i) in bound->shift and g (stride) in bound->stride.
88 * a(p,i) is assumed to be an expression in only the parameters
89 * and the input dimensions.
91 static void extract_stride(__isl_keep isl_constraint *c,
92 struct gpu_array_bound *bound, __isl_keep isl_val *stride, int sign)
94 int i;
95 isl_val *v;
96 isl_space *space;
97 unsigned nparam;
98 unsigned nvar;
99 isl_aff *aff;
101 isl_val_free(bound->stride);
102 bound->stride = isl_val_copy(stride);
104 space = isl_constraint_get_space(c);
105 space = isl_space_domain(space);
107 nparam = isl_space_dim(space, isl_dim_param);
108 nvar = isl_space_dim(space, isl_dim_set);
110 v = isl_constraint_get_constant_val(c);
111 if (sign < 0)
112 v = isl_val_neg(v);
113 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
114 aff = isl_aff_set_constant_val(aff, v);
116 for (i = 0; i < nparam; ++i) {
117 if (!isl_constraint_involves_dims(c, isl_dim_param, i, 1))
118 continue;
119 v = isl_constraint_get_coefficient_val(c, isl_dim_param, i);
120 if (sign < 0)
121 v = isl_val_neg(v);
122 aff = isl_aff_add_coefficient_val(aff, isl_dim_param, i, v);
125 for (i = 0; i < nvar; ++i) {
126 if (!isl_constraint_involves_dims(c, isl_dim_in, i, 1))
127 continue;
128 v = isl_constraint_get_coefficient_val(c, isl_dim_in, i);
129 if (sign < 0)
130 v = isl_val_neg(v);
131 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, i, v);
134 bound->shift = aff;
137 /* Given an equality constraint of a map with a single output dimension j,
138 * check if the constraint is of the form
140 * a(p,i) + j = g f(e)
142 * with a(p,i) an expression in the parameters and input dimensions
143 * and f(e) an expression in the existentially quantified variables.
144 * If so, and if g is larger than any such g from a previously considered
145 * constraint, then call extract_stride to record the stride information
146 * in bound.
148 static int check_stride_constraint(__isl_take isl_constraint *c, void *user)
150 int i;
151 isl_ctx *ctx;
152 isl_val *v;
153 unsigned n_div;
154 struct gpu_array_bound *bound = user;
156 ctx = isl_constraint_get_ctx(c);
157 n_div = isl_constraint_dim(c, isl_dim_div);
158 v = isl_constraint_get_coefficient_val(c, isl_dim_out, 0);
160 if (n_div && (isl_val_is_one(v) || isl_val_is_negone(v))) {
161 int s = isl_val_sgn(v);
162 isl_val *stride = isl_val_zero(ctx);
164 isl_val_free(v);
165 for (i = 0; i < n_div; ++i) {
166 v = isl_constraint_get_coefficient_val(c,
167 isl_dim_div, i);
168 stride = isl_val_gcd(stride, v);
170 if (!isl_val_is_zero(stride) &&
171 isl_val_gt(stride, bound->stride))
172 extract_stride(c, bound, stride, s);
174 isl_val_free(stride);
175 } else
176 isl_val_free(v);
178 isl_constraint_free(c);
179 return 0;
182 /* Given contraints on an array index i, check if we can find
183 * a shift a(p) and a stride g such that
185 * a(p) + i = 0 mod g
187 * If so, record the information in bound and apply the mapping
188 * i -> (i + a(p))/g to the array index in bounds and return
189 * the new constraints.
190 * If not, simply return the original constraints.
192 * If bounds is a subset of the space
194 * D -> i
196 * then the bound recorded in bound->shift is of the form
198 * D -> s(D)
200 * with s(D) equal to a(p) above.
201 * Next, we construct a mapping of the form
203 * [D -> i] -> [D -> (i + S(D))/g]
205 * This mapping is computed as follows.
206 * We first introduce "i" in the domain through precomposition
207 * with [D -> i] -> D obtaining
209 * [D -> i] -> s(D)
211 * Adding [D -> i] -> i produces
213 * [D -> i] -> i + s(D)
215 * and the domain product with [D -> i] -> D yields
217 * [D -> i] -> [D -> i + s(D)]
219 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
221 static __isl_give isl_basic_map *check_stride(struct gpu_array_bound *bound,
222 __isl_take isl_basic_map *bounds)
224 isl_space *space;
225 isl_basic_map *hull;
226 isl_basic_map *shift, *id, *bmap, *scale;
227 isl_basic_set *bset;
228 isl_aff *aff;
230 bound->stride = NULL;
232 hull = isl_basic_map_affine_hull(isl_basic_map_copy(bounds));
234 isl_basic_map_foreach_constraint(hull, &check_stride_constraint, bound);
236 isl_basic_map_free(hull);
238 if (!bound->stride)
239 return bounds;
241 shift = isl_basic_map_from_aff(isl_aff_copy(bound->shift));
242 space = isl_basic_map_get_space(bounds);
243 bmap = isl_basic_map_domain_map(isl_basic_map_universe(space));
244 shift = isl_basic_map_apply_range(bmap, shift);
245 space = isl_basic_map_get_space(bounds);
246 id = isl_basic_map_range_map(isl_basic_map_universe(space));
247 shift = isl_basic_map_sum(id, shift);
248 space = isl_basic_map_get_space(bounds);
249 id = isl_basic_map_domain_map(isl_basic_map_universe(space));
250 shift = isl_basic_map_range_product(id, shift);
252 space = isl_space_domain(isl_basic_map_get_space(bounds));
253 id = isl_basic_map_identity(isl_space_map_from_set(space));
254 space = isl_space_range(isl_basic_map_get_space(bounds));
255 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
256 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
257 aff = isl_aff_scale_down_val(aff, isl_val_copy(bound->stride));
258 scale = isl_basic_map_from_aff(aff);
259 scale = isl_basic_map_product(id, scale);
261 bmap = isl_basic_map_apply_range(shift, scale);
262 bset = isl_basic_set_apply(isl_basic_map_wrap(bounds), bmap);
263 bounds = isl_basic_set_unwrap(bset);
265 return bounds;
268 /* Data used in compute_array_dim_size and compute_size_in_direction.
270 * pos is the position of the variable representing the array index,
271 * i.e., the variable for which want to compute the size. This variable
272 * is also the last variable in the set.
274 struct gpu_size_info {
275 isl_basic_set *bset;
276 struct gpu_array_bound *bound;
277 int pos;
280 /* Given a constraint from the basic set describing the bounds on
281 * an array index, check if it is a lower bound, say m i >= b(x), and,
282 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
283 * upper bound. If so, and if this bound is smaller than any bound
284 * derived from earlier constraints, set the size to this bound on
285 * the expression and the lower bound to ceil(b(x)/m).
287 static int compute_size_in_direction(__isl_take isl_constraint *c, void *user)
289 struct gpu_size_info *size = user;
290 unsigned nparam;
291 unsigned n_div;
292 isl_val *v;
293 isl_aff *aff;
294 isl_aff *lb;
296 nparam = isl_basic_set_dim(size->bset, isl_dim_param);
297 n_div = isl_constraint_dim(c, isl_dim_div);
299 if (isl_constraint_involves_dims(c, isl_dim_div, 0, n_div) ||
300 !isl_constraint_is_lower_bound(c, isl_dim_set, size->pos)) {
301 isl_constraint_free(c);
302 return 0;
305 aff = isl_constraint_get_bound(c, isl_dim_set, size->pos);
306 aff = isl_aff_ceil(aff);
308 lb = isl_aff_copy(aff);
310 aff = isl_aff_neg(aff);
311 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, size->pos, 1);
313 v = isl_basic_set_max_val(size->bset, aff);
314 isl_aff_free(aff);
316 if (isl_val_is_int(v)) {
317 v = isl_val_add_ui(v, 1);
318 if (!size->bound->size || isl_val_lt(v, size->bound->size)) {
319 isl_val_free(size->bound->size);
320 size->bound->size = isl_val_copy(v);
321 lb = isl_aff_drop_dims(lb, isl_dim_in, size->pos, 1);
322 isl_aff_free(size->bound->lb);
323 size->bound->lb = isl_aff_copy(lb);
326 isl_val_free(v);
327 isl_aff_free(lb);
329 isl_constraint_free(c);
331 return 0;
334 /* Given a basic map "bounds" that maps parameters and input dimensions
335 * to a single output dimension, look for an expression in the parameters
336 * and input dimensions such that the range of the output dimension shifted
337 * by this expression is a constant.
339 * In particular, we currently only consider lower bounds on the output
340 * dimension as candidate expressions.
342 static int compute_array_dim_size(struct gpu_array_bound *bound,
343 __isl_take isl_basic_map *bounds)
345 struct gpu_size_info size;
347 bounds = isl_basic_map_detect_equalities(bounds);
348 bounds = check_stride(bound, bounds);
350 bound->size = NULL;
351 bound->lb = NULL;
353 size.bound = bound;
354 size.pos = isl_basic_map_dim(bounds, isl_dim_in);
355 size.bset = isl_basic_map_wrap(bounds);
356 size.bset = isl_basic_set_flatten(size.bset);
357 size.bset = isl_set_simple_hull(isl_basic_set_compute_divs(size.bset));
358 isl_basic_set_foreach_constraint(size.bset, &compute_size_in_direction,
359 &size);
360 isl_basic_set_free(size.bset);
362 return bound->size ? 0 : -1;
365 /* Check if we can find a memory tile for the given array
366 * based on the given accesses, and if so, put the results in "tile".
368 * We project the accesses on each index in turn and look for a parametric
369 * offset such that the size is constant.
371 static int can_tile(__isl_keep isl_map *access, struct gpu_array_tile *tile)
373 int i;
375 for (i = 0; i < tile->n; ++i) {
376 isl_map *access_i;
377 isl_basic_map *hull;
379 access_i = isl_map_copy(access);
380 access_i = isl_map_project_out(access_i, isl_dim_out, 0, i);
381 access_i = isl_map_project_out(access_i, isl_dim_out,
382 1, tile->n - (i + 1));
383 access_i = isl_map_compute_divs(access_i);
384 hull = isl_map_simple_hull(access_i);
385 if (compute_array_dim_size(&tile->bound[i], hull) < 0)
386 return 0;
389 return 1;
392 /* Construct a map from domain_dim to domain_dim that increments
393 * the dimension at position "pos" and leaves all other dimensions
394 * constant.
396 static __isl_give isl_map *next(__isl_take isl_space *domain_dim, int pos)
398 int i;
399 int len = isl_space_dim(domain_dim, isl_dim_set);
400 isl_space *dim;
401 isl_basic_map *next;
402 isl_local_space *ls;
404 dim = isl_space_map_from_set(domain_dim);
405 next = isl_basic_map_universe(isl_space_copy(dim));
406 ls = isl_local_space_from_space(dim);
408 for (i = 0; i < len; ++i) {
409 isl_constraint *c;
411 c = isl_equality_alloc(isl_local_space_copy(ls));
412 c = isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
413 c = isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
414 if (i == pos)
415 c = isl_constraint_set_constant_si(c, 1);
416 next = isl_basic_map_add_constraint(next, c);
419 isl_local_space_free(ls);
421 return isl_map_from_basic_map(next);
424 /* Check if the given access is coalesced.
425 * That is, check whether incrementing the dimension that will get
426 * wrapped over the last thread index results in incrementing
427 * the last array index.
429 * This function is only called for access relations without reuse and
430 * kernels with at least one block dimension.
432 static int access_is_coalesced(struct gpu_gen *gen,
433 __isl_keep isl_union_map *access)
435 isl_space *dim;
436 isl_map *access_map;
437 isl_map *next_thread_x;
438 isl_map *next_element;
439 isl_map *map;
440 int coalesced;
442 access = isl_union_map_copy(access);
443 access = isl_union_map_apply_domain(access,
444 isl_union_map_copy(gen->tiled_sched));
445 access_map = isl_map_from_union_map(access);
447 dim = isl_map_get_space(access_map);
448 dim = isl_space_domain(dim);
449 next_thread_x = next(dim, gen->shared_len + gen->kernel->n_block - 1);
451 dim = isl_map_get_space(access_map);
452 dim = isl_space_range(dim);
453 next_element = next(dim, isl_space_dim(dim, isl_dim_set) - 1);
455 map = isl_map_apply_domain(next_thread_x, isl_map_copy(access_map));
456 map = isl_map_apply_range(map, access_map);
458 coalesced = isl_map_is_subset(map, next_element);
460 isl_map_free(next_element);
461 isl_map_free(map);
463 return coalesced;
466 /* Given an access relation in terms of at least gen->shared_len initial
467 * dimensions of the computed schedule, check if it is bijective for
468 * fixed values of the first gen->shared_len dimensions.
469 * We perform this check by equating these dimensions to parameters.
471 static int access_is_bijective(struct gpu_gen *gen, __isl_keep isl_map *access)
473 int res;
474 int dim;
475 isl_set *par;
476 isl_space *space;
477 isl_id_list *ids;
479 access = isl_map_copy(access);
480 space = isl_space_params(isl_map_get_space(access));
481 ids = ppcg_scop_generate_names(gen->prog->scop, gen->shared_len, "s");
482 dim = isl_map_dim(access, isl_dim_in);
483 par = parametrization(space, dim, 0, ids);
484 isl_id_list_free(ids);
485 access = isl_map_intersect_domain(access, par);
486 res = isl_map_is_bijective(access);
487 isl_map_free(access);
489 return res;
492 /* Look for the last shared tile loop that affects the offset of "tile"
493 * and return the result.
494 * If there is no such loop, then return the index of the loop
495 * before the first shared tile loop, in particular gen->tile_first - 1.
497 static int compute_tile_last_shared(struct gpu_gen *gen,
498 struct gpu_array_tile *tile)
500 int i, j;
502 for (j = gen->shared_len - 1; j >= gen->tile_first; --j) {
503 for (i = 0; i < tile->n; ++i) {
504 isl_aff *lb;
505 isl_aff *shift;
507 lb = tile->bound[i].lb;
508 if (isl_aff_involves_dims(lb, isl_dim_in, j, 1))
509 break;
511 shift = tile->bound[i].shift;
512 if (!shift)
513 continue;
514 if (isl_aff_involves_dims(shift, isl_dim_in, j, 1))
515 break;
517 if (i < tile->n)
518 break;
521 return j;
524 /* Look for the last shared tile loop that affects the offset of the
525 * shared or private tile and store the result in group->last_shared.
526 * If there is no such loop, then group->last_shared is set to a value
527 * before the first shared tile loop, in particular gen->tile_first - 1.
528 * If there is no tile defined on the array reference group,
529 * then set group->last_shared to gen->shared_len - 1.
531 static void set_last_shared(struct gpu_gen *gen,
532 struct gpu_array_ref_group *group)
534 struct gpu_array_tile *tile;
536 group->last_shared = gen->shared_len - 1;
538 tile = gpu_array_ref_group_tile(group);
539 if (!tile)
540 return;
542 group->last_shared = compute_tile_last_shared(gen, tile);
545 /* Fill up the groups array with singleton groups, i.e., one group
546 * per reference, initializing the array, access, write, n_ref and refs fields.
547 * In particular the access field is initialized to the scheduled
548 * access relation of the array reference.
550 * Return the number of elements initialized, i.e., the number of
551 * active references in the current kernel.
553 static int populate_array_references(struct gpu_local_array_info *local,
554 __isl_keep isl_union_map *sched, struct gpu_array_ref_group **groups)
556 int i;
557 int n;
558 isl_ctx *ctx = isl_union_map_get_ctx(sched);
560 n = 0;
561 for (i = 0; i < local->array->n_ref; ++i) {
562 isl_union_map *umap;
563 isl_map *map;
564 struct gpu_array_ref_group *group;
565 struct gpu_stmt_access *access = local->array->refs[i];
567 map = isl_map_copy(access->access);
568 umap = isl_union_map_from_map(map);
569 umap = isl_union_map_apply_domain(umap,
570 isl_union_map_copy(sched));
572 if (isl_union_map_is_empty(umap)) {
573 isl_union_map_free(umap);
574 continue;
577 map = isl_map_from_union_map(umap);
578 map = isl_map_detect_equalities(map);
580 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
581 if (!group)
582 return -1;
583 group->local_array = local;
584 group->array = local->array;
585 group->access = map;
586 group->write = access->write;
587 group->exact_write = access->exact_write;
588 group->slice = access->n_index < local->array->n_index;
589 group->refs = &local->array->refs[i];
590 group->n_ref = 1;
592 groups[n++] = group;
595 return n;
598 /* If group->n_ref == 1, then group->refs was set by
599 * populate_array_references to point directly into
600 * group->array->refs and should not be freed.
601 * If group->n_ref > 1, then group->refs was set by join_groups
602 * to point to a newly allocated array.
604 struct gpu_array_ref_group *gpu_array_ref_group_free(
605 struct gpu_array_ref_group *group)
607 if (!group)
608 return NULL;
609 gpu_array_tile_free(group->shared_tile);
610 gpu_array_tile_free(group->private_tile);
611 isl_map_free(group->access);
612 if (group->n_ref > 1)
613 free(group->refs);
614 free(group);
615 return NULL;
618 /* Given a map where the input dimensions represent the tile loops,
619 * eliminate the innermost of those that have a fixed value
620 * until we reach one that does not (obviously) have a fixed value.
622 static __isl_give isl_map *eliminate_fixed_inner_loops(
623 __isl_take isl_map *access)
625 int i, n;
627 n = isl_map_dim(access, isl_dim_in);
629 for (i = n - 1; i >= 0; --i) {
630 if (!map_plain_is_fixed(access, isl_dim_in, i))
631 break;
632 access = isl_map_eliminate(access, isl_dim_in, i, 1);
634 return access;
637 /* Check if the access relations of group1 and group2 overlap within
638 * the innermost loop. In particular, ignore any inner dimension
639 * with a fixed value.
640 * The copying to and from shared memory will be performed within
641 * the innermost actual loop so we are only allowed to consider
642 * the dimensions up to that innermost loop while checking whether
643 * two access relations overlap.
645 static int accesses_overlap(struct gpu_array_ref_group *group1,
646 struct gpu_array_ref_group *group2)
648 int empty;
649 isl_map *access1, *access2;
651 access1 = isl_map_copy(group1->access);
652 access1 = eliminate_fixed_inner_loops(access1);
653 access2 = isl_map_copy(group2->access);
654 access2 = eliminate_fixed_inner_loops(access2);
655 access1 = isl_map_intersect(access1, access2);
656 empty = isl_map_is_empty(access1);
657 isl_map_free(access1);
659 return !empty;
662 /* Combine the given two groups into a single group, containing
663 * the references of both groups.
665 static struct gpu_array_ref_group *join_groups(
666 struct gpu_array_ref_group *group1,
667 struct gpu_array_ref_group *group2)
669 int i;
670 isl_ctx *ctx;
671 struct gpu_array_ref_group *group;
673 ctx = isl_map_get_ctx(group1->access);
674 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
675 if (!group)
676 return NULL;
677 group->local_array = group1->local_array;
678 group->array = group1->array;
679 group->access = isl_map_union(isl_map_copy(group1->access),
680 isl_map_copy(group2->access));
681 group->write = group1->write || group2->write;
682 group->exact_write = group1->exact_write && group2->exact_write;
683 group->slice = group1->slice || group2->slice;
684 group->n_ref = group1->n_ref + group2->n_ref;
685 group->refs = isl_alloc_array(ctx, struct gpu_stmt_access *,
686 group->n_ref);
687 if (!group->refs)
688 return gpu_array_ref_group_free(group);
689 for (i = 0; i < group1->n_ref; ++i)
690 group->refs[i] = group1->refs[i];
691 for (i = 0; i < group2->n_ref; ++i)
692 group->refs[group1->n_ref + i] = group2->refs[i];
694 return group;
697 /* Combine the given two groups into a single group and free
698 * the original two groups.
700 static struct gpu_array_ref_group *join_groups_and_free(
701 struct gpu_array_ref_group *group1,
702 struct gpu_array_ref_group *group2)
704 struct gpu_array_ref_group *group;
706 group = join_groups(group1, group2);
707 gpu_array_ref_group_free(group1);
708 gpu_array_ref_group_free(group2);
709 return group;
712 /* Report that the array reference group with the given access relation
713 * is not mapped to shared memory in the given kernel because
714 * it does not exhibit any reuse and is considered to be coalesced.
716 static void report_no_reuse_and_coalesced(struct ppcg_kernel *kernel,
717 __isl_keep isl_union_map *access)
719 isl_ctx *ctx;
720 isl_printer *p;
722 ctx = isl_union_map_get_ctx(access);
723 p = isl_printer_to_file(ctx, stdout);
724 p = isl_printer_print_str(p, "Array reference group ");
725 p = isl_printer_print_union_map(p, access);
726 p = isl_printer_print_str(p,
727 " not considered for mapping to shared memory in kernel");
728 p = isl_printer_print_int(p, kernel->id);
729 p = isl_printer_print_str(p,
730 " because it exhibits no reuse and is considered to be coalesced");
731 p = isl_printer_end_line(p);
732 isl_printer_free(p);
735 /* Given an access relation in terms of the gen->shared_len initial
736 * dimensions of the computed schedule and the thread identifiers
737 * (as parameters), check if the use of the corresponding private tile
738 * requires unrolling.
740 * If we are creating a private tile because we are forced to,
741 * then no unrolling is required.
742 * Otherwise we check if "access" is bijective and unrolling
743 * is required if it is not. Note that the access relation
744 * has already been determined to be bijective before the introduction
745 * of the thread identifiers and the removal of the schedule dimensions
746 * that are mapped to these threads. If the access relation is no longer
747 * bijective, then this means that more than one value of one of those
748 * schedule dimensions is mapped to the same thread and therefore
749 * unrolling is required.
751 static int check_requires_unroll(struct gpu_gen *gen,
752 __isl_keep isl_map *access, int force_private)
754 int bijective;
756 if (force_private)
757 return 0;
758 bijective = access_is_bijective(gen, access);
759 if (bijective < 0)
760 return -1;
761 return !bijective;
764 /* Compute the private and/or shared memory tiles for the array
765 * reference group "group" of array "array".
766 * Return 0 on success and -1 on error.
768 * If the array is a read-only scalar or if the user requested
769 * not to use shared or private memory, then we do not need to do anything.
771 * If any reference in the reference group accesses more than one element,
772 * then we would have to make sure that the layout in shared memory
773 * is the same as that in global memory. Since we do not handle this yet
774 * (and it may not even be possible), we refuse to map to private or
775 * shared memory in such cases.
777 * If the array group involves any may writes (that are not must writes),
778 * then we would have to make sure that we load the data into shared/private
779 * memory first in case the data is not written by the kernel
780 * (but still written back out to global memory).
781 * Since we don't have any such mechanism at the moment, we don't
782 * compute shared/private tiles for groups involving may writes.
784 * We only try to compute a shared memory tile if there is any reuse
785 * or if the access is not coalesced.
787 * For computing a private memory tile, we also require that there is
788 * some reuse. Moreover, we require that the access is private
789 * to the thread. That is, we check that any given array element
790 * is only accessed by a single thread.
791 * We compute an access relation that maps the shared tile loop iterators
792 * and the shared point loop iterators that will be wrapped over the
793 * threads to the array elements.
794 * We actually check that those iterators that will be wrapped
795 * partition the array space. This check is stricter than necessary
796 * since several iterations may be mapped onto the same thread
797 * and then they could be allowed to access the same memory elements,
798 * but our check does not allow this situation.
800 * We also check that the index expression only depends on parallel
801 * loops. That way, we can move those loops innermost and unroll them.
802 * Again, we use a test that is stricter than necessary.
803 * We actually check whether the index expression only depends
804 * on the iterators that are wrapped over the threads.
805 * These are necessarily parallel, but there may be more parallel loops.
807 * Combining the injectivity of the first test with the single-valuedness
808 * of the second test, we simply test for bijectivity.
810 * If the array is marked force_private, then we bypass all checks
811 * and assume we can (and should) use registers.
813 * If it turns out we can (or have to) use registers, we compute
814 * the private memory tile size using can_tile, after introducing a dependence
815 * on the thread indices.
817 static int compute_group_bounds_core(struct gpu_gen *gen,
818 struct gpu_array_ref_group *group)
820 isl_ctx *ctx = isl_space_get_ctx(group->array->space);
821 isl_union_map *access;
822 int n_index = group->array->n_index;
823 int no_reuse, coalesced;
824 isl_map *acc;
825 int force_private = group->local_array->force_private;
826 int use_shared = gen->options->use_shared_memory &&
827 gen->kernel->n_block > 0;
828 int use_private = force_private || gen->options->use_private_memory;
829 int r = 0;
830 int requires_unroll;
832 if (!use_shared && !use_private)
833 return 0;
834 if (gpu_array_is_read_only_scalar(group->array))
835 return 0;
836 if (!force_private && !group->exact_write)
837 return 0;
838 if (group->slice)
839 return 0;
841 access = gpu_array_ref_group_access_relation(group, 1, 1);
842 no_reuse = isl_union_map_is_injective(access);
843 if (no_reuse < 0)
844 r = -1;
845 if (use_shared && no_reuse)
846 coalesced = access_is_coalesced(gen, access);
848 if (r >= 0 && gen->options->debug->verbose &&
849 use_shared && no_reuse && coalesced)
850 report_no_reuse_and_coalesced(gen->kernel, access);
852 if (use_shared && (!no_reuse || !coalesced)) {
853 group->shared_tile = gpu_array_tile_create(ctx,
854 group->array->n_index);
855 if (!group->shared_tile)
856 r = -1;
857 else if (!can_tile(group->access, group->shared_tile))
858 group->shared_tile =
859 gpu_array_tile_free(group->shared_tile);
862 if (r < 0 || (!force_private && (!use_private || no_reuse))) {
863 isl_union_map_free(access);
864 return r;
867 access = isl_union_map_apply_domain(access,
868 isl_union_map_copy(gen->shared_sched));
870 acc = isl_map_from_union_map(access);
872 if (!force_private && !access_is_bijective(gen, acc)) {
873 isl_map_free(acc);
874 return 0;
877 group->private_tile = gpu_array_tile_create(gen->ctx, n_index);
878 if (!group->private_tile) {
879 isl_map_free(acc);
880 return -1;
882 acc = isl_map_apply_domain(acc, isl_map_copy(gen->privatization));
883 requires_unroll = check_requires_unroll(gen, acc, force_private);
884 group->private_tile->requires_unroll = requires_unroll;
885 if (!can_tile(acc, group->private_tile))
886 group->private_tile = gpu_array_tile_free(group->private_tile);
888 isl_map_free(acc);
890 if (force_private && !group->private_tile)
891 isl_die(ctx, isl_error_internal,
892 "unable to map array reference group to registers",
893 return -1);
895 return 0;
898 /* Compute the private and/or shared memory tiles for the array
899 * reference group "group" of array "array" and set last_shared.
900 * Return 0 on success and -1 on error.
902 static int compute_group_bounds(struct gpu_gen *gen,
903 struct gpu_array_ref_group *group)
905 if (!group)
906 return -1;
907 if (compute_group_bounds_core(gen, group) < 0)
908 return -1;
909 set_last_shared(gen, group);
911 return 0;
914 /* If two groups have overlapping access relations (as determined by
915 * the "overlap" function) and if one of them involves a write,
916 * then merge the two groups into one.
917 * If "compute_bounds" is set, then call compute_group_bounds
918 * on the merged groups.
920 * Return the updated number of groups.
921 * Return -1 on error.
923 static int group_writes(struct gpu_gen *gen,
924 int n, struct gpu_array_ref_group **groups,
925 int (*overlap)(struct gpu_array_ref_group *group1,
926 struct gpu_array_ref_group *group2), int compute_bounds)
928 int i, j;
930 for (i = 0; i < n; ++i) {
931 for (j = n - 1; j > i; --j) {
932 if (!groups[i]->write && !groups[j]->write)
933 continue;
935 if (!overlap(groups[i], groups[j]))
936 continue;
938 groups[i] = join_groups_and_free(groups[i], groups[j]);
939 if (j != n - 1)
940 groups[j] = groups[n - 1];
941 groups[n - 1] = NULL;
942 n--;
944 if (!groups[i])
945 return -1;
946 if (compute_bounds &&
947 compute_group_bounds(gen, groups[i]) < 0)
948 return -1;
952 return n;
955 /* If two groups have overlapping access relations (within the innermost
956 * loop) and if one of them involves a write, then merge the two groups
957 * into one.
959 * Return the updated number of groups.
961 static int group_overlapping_writes(struct gpu_gen *gen,
962 int n, struct gpu_array_ref_group **groups)
964 return group_writes(gen, n, groups, &accesses_overlap, 0);
967 /* Check if the access relations of group1 and group2 overlap within
968 * the outermost min(group1->last_shared, group2->last_shared) loops.
970 static int last_shared_accesses_overlap(struct gpu_array_ref_group *group1,
971 struct gpu_array_ref_group *group2)
973 int last_shared;
974 int dim;
975 int empty;
976 isl_map *map_i, *map_j, *map;
978 last_shared = group1->last_shared;
979 if (group2->last_shared < last_shared)
980 last_shared = group2->last_shared;
981 map_i = isl_map_copy(group1->access);
982 dim = isl_map_dim(map_i, isl_dim_in);
983 map_i = isl_map_eliminate(map_i, isl_dim_in,
984 last_shared + 1, dim - (last_shared + 1));
985 map_j = isl_map_copy(group2->access);
986 map_j = isl_map_eliminate(map_j, isl_dim_in,
987 last_shared + 1, dim - (last_shared + 1));
988 map = isl_map_intersect(map_i, map_j);
989 empty = isl_map_is_empty(map);
990 isl_map_free(map);
992 return !empty;
995 /* If two groups have overlapping access relations (within the outer
996 * last_shared loops) and if one of them involves a write,
997 * then merge the two groups into one.
999 * Return the updated number of groups.
1001 static int group_last_shared_overlapping_writes(struct gpu_gen *gen, int n,
1002 struct gpu_array_ref_group **groups)
1004 return group_writes(gen, n, groups, &last_shared_accesses_overlap, 1);
1007 /* Is the size of the tile specified by "tile" smaller than the sum of
1008 * the sizes of the tiles specified by "tile1" and "tile2"?
1010 static int smaller_tile(struct gpu_array_tile *tile,
1011 struct gpu_array_tile *tile1, struct gpu_array_tile *tile2)
1013 int smaller;
1014 isl_val *size, *size1, *size2;
1016 size = gpu_array_tile_size(tile);
1017 size1 = gpu_array_tile_size(tile1);
1018 size2 = gpu_array_tile_size(tile2);
1020 size = isl_val_sub(size, size1);
1021 size = isl_val_sub(size, size2);
1022 smaller = isl_val_is_neg(size);
1024 isl_val_free(size);
1026 return smaller;
1029 /* Given an initial grouping of array references and shared memory tiles
1030 * for each group that allows for a shared memory tile, merge two groups
1031 * if both have a shared memory tile, the merged group also has
1032 * a shared memory tile and the size of the tile for the merge group
1033 * is smaller than the sum of the tile sizes of the individual groups.
1035 * If merging two groups decreases the "last_shared" dimension of
1036 * one or both of the two groups, then we need to check for overlapping
1037 * writes again.
1039 * Return the number of groups after merging.
1040 * Return -1 on error.
1042 static int group_common_shared_memory_tile(struct gpu_gen *gen,
1043 struct gpu_array_info *array, int n,
1044 struct gpu_array_ref_group **groups)
1046 int i, j;
1047 int recompute_overlap = 0;
1048 isl_ctx *ctx = isl_space_get_ctx(array->space);
1050 for (i = 0; i < n; ++i) {
1051 if (!groups[i]->shared_tile)
1052 continue;
1053 for (j = n - 1; j > i; --j) {
1054 isl_map *map;
1055 int empty;
1056 struct gpu_array_ref_group *group;
1058 if (!groups[j]->shared_tile)
1059 continue;
1061 map = isl_map_intersect(isl_map_copy(groups[i]->access),
1062 isl_map_copy(groups[j]->access));
1063 empty = isl_map_is_empty(map);
1064 isl_map_free(map);
1066 if (empty)
1067 continue;
1069 group = join_groups(groups[i], groups[j]);
1070 if (compute_group_bounds(gen, group) < 0) {
1071 gpu_array_ref_group_free(group);
1072 return -1;
1074 if (!group->shared_tile ||
1075 !smaller_tile(group->shared_tile,
1076 groups[i]->shared_tile,
1077 groups[j]->shared_tile)) {
1078 gpu_array_ref_group_free(group);
1079 continue;
1082 if (group->last_shared < groups[i]->last_shared ||
1083 group->last_shared < groups[j]->last_shared)
1084 recompute_overlap = 1;
1085 gpu_array_ref_group_free(groups[i]);
1086 gpu_array_ref_group_free(groups[j]);
1087 groups[i] = group;
1088 if (j != n - 1)
1089 groups[j] = groups[n - 1];
1090 n--;
1094 if (recompute_overlap)
1095 n = group_last_shared_overlapping_writes(gen, n, groups);
1096 return n;
1099 /* Set array->n_group and array->groups to n and groups.
1101 * Additionally, set the "nr" field of each group.
1103 static void set_array_groups(struct gpu_local_array_info *array,
1104 int n, struct gpu_array_ref_group **groups)
1106 int i, j;
1108 array->n_group = n;
1109 array->groups = groups;
1111 for (i = 0; i < n; ++i)
1112 groups[i]->nr = i;
1115 /* Group array references that should be considered together when
1116 * deciding whether to access them from private, shared or global memory.
1117 * Return -1 on error.
1119 * In particular, if two array references overlap and if one of them
1120 * is a write, then the two references are grouped together.
1121 * We first perform an initial grouping based only on the access relation.
1122 * After computing shared and private memory tiles, we check for
1123 * overlapping writes again, but this time taking into account
1124 * the "last_shared" property.
1126 * Furthermore, if two groups admit a shared memory tile and if the
1127 * combination of the two also admits a shared memory tile, we merge
1128 * the two groups.
1130 * If the array contains structures, then there is no need to compute
1131 * reference groups since we do not map such arrays to private or shared
1132 * memory.
1134 static int group_array_references(struct gpu_gen *gen,
1135 struct gpu_local_array_info *local, __isl_keep isl_union_map *sched)
1137 int i;
1138 int n;
1139 isl_ctx *ctx = isl_union_map_get_ctx(sched);
1140 struct gpu_array_ref_group **groups;
1142 if (local->array->has_compound_element)
1143 return 0;
1145 groups = isl_calloc_array(ctx, struct gpu_array_ref_group *,
1146 local->array->n_ref);
1147 if (!groups)
1148 return -1;
1150 n = populate_array_references(local, sched, groups);
1152 n = group_overlapping_writes(gen, n, groups);
1154 for (i = 0; i < n; ++i)
1155 if (compute_group_bounds(gen, groups[i]) < 0)
1156 n = -1;
1158 n = group_last_shared_overlapping_writes(gen, n, groups);
1160 n = group_common_shared_memory_tile(gen, local->array, n, groups);
1162 set_array_groups(local, n, groups);
1164 if (n >= 0)
1165 return 0;
1167 for (i = 0; i < local->array->n_ref; ++i)
1168 gpu_array_ref_group_free(groups[i]);
1169 return -1;
1172 /* For each scalar in the input program, check if there are any
1173 * order dependences active inside the current kernel, within
1174 * the same iteration of the host schedule.
1175 * If so, mark the scalar as force_private so that it will be
1176 * mapped to a register.
1178 static void check_scalar_live_ranges(struct gpu_gen *gen)
1180 int i;
1181 isl_map *proj;
1182 isl_union_map *sched;
1183 isl_union_set *domain;
1184 isl_union_map *same_host_iteration;
1186 gen->kernel->any_force_private = 0;
1188 if (!gen->options->live_range_reordering)
1189 return;
1191 sched = gen->shared_sched;
1192 sched = isl_union_map_universe(isl_union_map_copy(sched));
1193 domain = isl_union_map_domain(sched);
1195 sched = isl_union_map_copy(gen->sched);
1196 proj = projection(isl_union_map_get_space(sched),
1197 gen->untiled_len, gen->tile_first);
1198 sched = isl_union_map_apply_range(sched, isl_union_map_from_map(proj));
1199 same_host_iteration = isl_union_map_apply_range(sched,
1200 isl_union_map_reverse(isl_union_map_copy(sched)));
1202 for (i = 0; i < gen->kernel->n_array; ++i) {
1203 struct gpu_local_array_info *local = &gen->kernel->array[i];
1204 isl_union_map *order;
1206 local->force_private = 0;
1207 if (local->array->n_index != 0)
1208 continue;
1209 order = isl_union_map_copy(local->array->dep_order);
1210 order = isl_union_map_intersect_domain(order,
1211 isl_union_set_copy(domain));
1212 order = isl_union_map_intersect_range(order,
1213 isl_union_set_copy(domain));
1214 order = isl_union_map_intersect(order,
1215 isl_union_map_copy(same_host_iteration));
1216 if (!isl_union_map_is_empty(order)) {
1217 local->force_private = 1;
1218 gen->kernel->any_force_private = 1;
1220 isl_union_map_free(order);
1223 isl_union_map_free(same_host_iteration);
1224 isl_union_set_free(domain);
1227 /* Group references of all arrays in the current kernel.
1229 int gpu_group_references(struct gpu_gen *gen)
1231 int i;
1232 int r = 0;
1233 isl_union_map *sched;
1235 check_scalar_live_ranges(gen);
1237 sched = isl_union_map_apply_range(isl_union_map_copy(gen->shared_sched),
1238 isl_union_map_copy(gen->shared_proj));
1240 for (i = 0; i < gen->kernel->n_array; ++i) {
1241 r = group_array_references(gen, &gen->kernel->array[i], sched);
1242 if (r < 0)
1243 break;
1246 isl_union_map_free(sched);
1248 return r;
1251 /* Given a description of an array tile "tile" and the "space"
1253 * { D -> A }
1255 * where D represents the first shared_len schedule dimensions
1256 * and A represents the array, construct an isl_multi_aff
1258 * { [D[i] -> A[a]] -> A'[a'] }
1260 * with A' a scaled down copy of A according to the shifts and strides
1261 * in "tile". In particular,
1263 * a' = (a + shift(i))/stride
1265 * "insert_array" represents
1267 * { [D -> A] -> D }
1269 * and is used to insert A into the domain of functions that only
1270 * reference D.
1272 static __isl_give isl_multi_aff *strided_tile(
1273 struct gpu_array_tile *tile, __isl_keep isl_space *space,
1274 __isl_keep isl_multi_aff *insert_array)
1276 int i;
1277 isl_ctx *ctx;
1278 isl_multi_aff *shift;
1279 isl_multi_val *stride;
1280 isl_space *space2;
1281 isl_local_space *ls;
1282 isl_multi_aff *tiling;
1284 ctx = isl_space_get_ctx(space);
1285 space2 = isl_space_domain(isl_space_copy(space));
1286 ls = isl_local_space_from_space(space2);
1287 space2 = isl_space_range(isl_space_copy(space));
1288 stride = isl_multi_val_zero(space2);
1289 shift = isl_multi_aff_zero(isl_space_copy(space));
1291 for (i = 0; i < tile->n; ++i) {
1292 struct gpu_array_bound *bound = &tile->bound[i];
1293 isl_val *stride_i;
1294 isl_aff *shift_i;
1296 if (tile->bound[i].shift) {
1297 stride_i = isl_val_copy(bound->stride);
1298 shift_i = isl_aff_copy(bound->shift);
1299 } else {
1300 stride_i = isl_val_one(ctx);
1301 shift_i = isl_aff_zero_on_domain(
1302 isl_local_space_copy(ls));
1305 stride = isl_multi_val_set_val(stride, i, stride_i);
1306 shift = isl_multi_aff_set_aff(shift, i, shift_i);
1308 isl_local_space_free(ls);
1310 shift = isl_multi_aff_pullback_multi_aff(shift,
1311 isl_multi_aff_copy(insert_array));
1313 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1314 tiling = isl_multi_aff_add(tiling, shift);
1315 tiling = isl_multi_aff_scale_down_multi_val(tiling, stride);
1317 return tiling;
1320 /* Compute a tiling for the array reference group "group".
1322 * The tiling is of the form
1324 * { [D[i] -> A[a]] -> T[t] }
1326 * where D represents the first shared_len schedule dimensions,
1327 * A represents the global array and T represents the shared or
1328 * private memory tile. The name of T is the name of the local
1329 * array.
1331 * If there is any stride in the accesses, then the mapping is
1333 * t = (a + shift(i))/stride - lb(i)
1335 * otherwise, it is simply
1337 * t = a - lb(i)
1339 void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group *group)
1341 int i;
1342 struct gpu_array_tile *tile;
1343 struct gpu_array_info *array = group->array;
1344 isl_space *space;
1345 isl_multi_aff *tiling, *lb, *insert_array;
1346 isl_printer *p;
1347 char *local_name;
1349 tile = group->private_tile;
1350 if (!tile)
1351 tile = group->shared_tile;
1352 if (!tile)
1353 return;
1355 space = isl_map_get_space(group->access);
1356 insert_array = isl_multi_aff_domain_map(isl_space_copy(space));
1358 for (i = 0; i < tile->n; ++i)
1359 if (tile->bound[i].shift)
1360 break;
1362 if (i < tile->n)
1363 tiling = strided_tile(tile, space, insert_array);
1364 else
1365 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1367 lb = isl_multi_aff_zero(space);
1368 for (i = 0; i < tile->n; ++i) {
1369 isl_aff *lb_i = isl_aff_copy(tile->bound[i].lb);
1370 lb = isl_multi_aff_set_aff(lb, i, lb_i);
1372 lb = isl_multi_aff_pullback_multi_aff(lb, insert_array);
1374 tiling = isl_multi_aff_sub(tiling, lb);
1376 p = isl_printer_to_str(isl_multi_aff_get_ctx(tiling));
1377 p = gpu_array_ref_group_print_name(group, p);
1378 local_name = isl_printer_get_str(p);
1379 isl_printer_free(p);
1380 tiling = isl_multi_aff_set_tuple_name(tiling, isl_dim_out, local_name);
1381 free(local_name);
1383 tile->tiling = tiling;