add dependences between sources with shared sinks to forced dependences
[ppcg.git] / gpu_group.c
blob8325b38573e2a33a268758b3e56d505e9edb4756
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 /* Given a constraint
55 * a(p,i) + j = g f(e)
57 * or -a(p,i) - j = g f(e) if sign < 0,
58 * store a(p,i) in bound->shift and g (stride) in bound->stride.
59 * a(p,i) is assumed to be an expression in only the parameters
60 * and the input dimensions.
62 static void extract_stride(__isl_keep isl_constraint *c,
63 struct gpu_array_bound *bound, __isl_keep isl_val *stride, int sign)
65 int i;
66 isl_val *v;
67 isl_space *space;
68 unsigned nparam;
69 unsigned nvar;
70 isl_aff *aff;
72 isl_val_free(bound->stride);
73 bound->stride = isl_val_copy(stride);
75 space = isl_constraint_get_space(c);
76 space = isl_space_domain(space);
78 nparam = isl_space_dim(space, isl_dim_param);
79 nvar = isl_space_dim(space, isl_dim_set);
81 v = isl_constraint_get_constant_val(c);
82 if (sign < 0)
83 v = isl_val_neg(v);
84 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
85 aff = isl_aff_set_constant_val(aff, v);
87 for (i = 0; i < nparam; ++i) {
88 if (!isl_constraint_involves_dims(c, isl_dim_param, i, 1))
89 continue;
90 v = isl_constraint_get_coefficient_val(c, isl_dim_param, i);
91 if (sign < 0)
92 v = isl_val_neg(v);
93 aff = isl_aff_add_coefficient_val(aff, isl_dim_param, i, v);
96 for (i = 0; i < nvar; ++i) {
97 if (!isl_constraint_involves_dims(c, isl_dim_in, i, 1))
98 continue;
99 v = isl_constraint_get_coefficient_val(c, isl_dim_in, i);
100 if (sign < 0)
101 v = isl_val_neg(v);
102 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, i, v);
105 bound->shift = aff;
108 /* Given an equality constraint of a map with a single output dimension j,
109 * check if the constraint is of the form
111 * a(p,i) + j = g f(e)
113 * with a(p,i) an expression in the parameters and input dimensions
114 * and f(e) an expression in the existentially quantified variables.
115 * If so, and if g is larger than any such g from a previously considered
116 * constraint, then call extract_stride to record the stride information
117 * in bound.
119 static int check_stride_constraint(__isl_take isl_constraint *c, void *user)
121 int i;
122 isl_ctx *ctx;
123 isl_val *v;
124 unsigned n_div;
125 struct gpu_array_bound *bound = user;
127 ctx = isl_constraint_get_ctx(c);
128 n_div = isl_constraint_dim(c, isl_dim_div);
129 v = isl_constraint_get_coefficient_val(c, isl_dim_out, 0);
131 if (n_div && (isl_val_is_one(v) || isl_val_is_negone(v))) {
132 int s = isl_val_sgn(v);
133 isl_val *stride = isl_val_zero(ctx);
135 isl_val_free(v);
136 for (i = 0; i < n_div; ++i) {
137 v = isl_constraint_get_coefficient_val(c,
138 isl_dim_div, i);
139 stride = isl_val_gcd(stride, v);
141 if (!isl_val_is_zero(stride) &&
142 isl_val_gt(stride, bound->stride))
143 extract_stride(c, bound, stride, s);
145 isl_val_free(stride);
146 } else
147 isl_val_free(v);
149 isl_constraint_free(c);
150 return 0;
153 /* Given contraints on an array index i, check if we can find
154 * a shift a(p) and a stride g such that
156 * a(p) + i = 0 mod g
158 * If so, record the information in bound and apply the mapping
159 * i -> (i + a(p))/g to the array index in bounds and return
160 * the new constraints.
161 * If not, simply return the original constraints.
163 * If bounds is a subset of the space
165 * D -> i
167 * then the bound recorded in bound->shift is of the form
169 * D -> s(D)
171 * with s(D) equal to a(p) above.
172 * Next, we construct a mapping of the form
174 * [D -> i] -> [D -> (i + S(D))/g]
176 * This mapping is computed as follows.
177 * We first introduce "i" in the domain through precomposition
178 * with [D -> i] -> D obtaining
180 * [D -> i] -> s(D)
182 * Adding [D -> i] -> i produces
184 * [D -> i] -> i + s(D)
186 * and the domain product with [D -> i] -> D yields
188 * [D -> i] -> [D -> i + s(D)]
190 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
192 static __isl_give isl_basic_map *check_stride(struct gpu_array_bound *bound,
193 __isl_take isl_basic_map *bounds)
195 isl_space *space;
196 isl_basic_map *hull;
197 isl_basic_map *shift, *id, *bmap, *scale;
198 isl_basic_set *bset;
199 isl_aff *aff;
201 bound->stride = NULL;
203 hull = isl_basic_map_affine_hull(isl_basic_map_copy(bounds));
205 isl_basic_map_foreach_constraint(hull, &check_stride_constraint, bound);
207 isl_basic_map_free(hull);
209 if (!bound->stride)
210 return bounds;
212 shift = isl_basic_map_from_aff(isl_aff_copy(bound->shift));
213 space = isl_basic_map_get_space(bounds);
214 bmap = isl_basic_map_domain_map(isl_basic_map_universe(space));
215 shift = isl_basic_map_apply_range(bmap, shift);
216 space = isl_basic_map_get_space(bounds);
217 id = isl_basic_map_range_map(isl_basic_map_universe(space));
218 shift = isl_basic_map_sum(id, shift);
219 space = isl_basic_map_get_space(bounds);
220 id = isl_basic_map_domain_map(isl_basic_map_universe(space));
221 shift = isl_basic_map_range_product(id, shift);
223 space = isl_space_domain(isl_basic_map_get_space(bounds));
224 id = isl_basic_map_identity(isl_space_map_from_set(space));
225 space = isl_space_range(isl_basic_map_get_space(bounds));
226 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
227 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
228 aff = isl_aff_scale_down_val(aff, isl_val_copy(bound->stride));
229 scale = isl_basic_map_from_aff(aff);
230 scale = isl_basic_map_product(id, scale);
232 bmap = isl_basic_map_apply_range(shift, scale);
233 bset = isl_basic_set_apply(isl_basic_map_wrap(bounds), bmap);
234 bounds = isl_basic_set_unwrap(bset);
236 return bounds;
239 /* Data used in compute_array_dim_size and compute_size_in_direction.
241 * pos is the position of the variable representing the array index,
242 * i.e., the variable for which want to compute the size. This variable
243 * is also the last variable in the set.
245 struct gpu_size_info {
246 isl_basic_set *bset;
247 struct gpu_array_bound *bound;
248 int pos;
251 /* Given a constraint from the basic set describing the bounds on
252 * an array index, check if it is a lower bound, say m i >= b(x), and,
253 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
254 * upper bound. If so, and if this bound is smaller than any bound
255 * derived from earlier constraints, set the size to this bound on
256 * the expression and the lower bound to ceil(b(x)/m).
258 static int compute_size_in_direction(__isl_take isl_constraint *c, void *user)
260 struct gpu_size_info *size = user;
261 unsigned nparam;
262 unsigned n_div;
263 isl_val *v;
264 isl_aff *aff;
265 isl_aff *lb;
267 nparam = isl_basic_set_dim(size->bset, isl_dim_param);
268 n_div = isl_constraint_dim(c, isl_dim_div);
270 if (isl_constraint_involves_dims(c, isl_dim_div, 0, n_div) ||
271 !isl_constraint_is_lower_bound(c, isl_dim_set, size->pos)) {
272 isl_constraint_free(c);
273 return 0;
276 aff = isl_constraint_get_bound(c, isl_dim_set, size->pos);
277 aff = isl_aff_ceil(aff);
279 lb = isl_aff_copy(aff);
281 aff = isl_aff_neg(aff);
282 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, size->pos, 1);
284 v = isl_basic_set_max_val(size->bset, aff);
285 isl_aff_free(aff);
287 if (isl_val_is_int(v)) {
288 v = isl_val_add_ui(v, 1);
289 if (!size->bound->size || isl_val_lt(v, size->bound->size)) {
290 isl_val_free(size->bound->size);
291 size->bound->size = isl_val_copy(v);
292 lb = isl_aff_drop_dims(lb, isl_dim_in, size->pos, 1);
293 isl_aff_free(size->bound->lb);
294 size->bound->lb = isl_aff_copy(lb);
297 isl_val_free(v);
298 isl_aff_free(lb);
300 isl_constraint_free(c);
302 return 0;
305 /* Given a basic map "bounds" that maps parameters and input dimensions
306 * to a single output dimension, look for an expression in the parameters
307 * and input dimensions such that the range of the output dimension shifted
308 * by this expression is a constant.
310 * In particular, we currently only consider lower bounds on the output
311 * dimension as candidate expressions.
313 static int compute_array_dim_size(struct gpu_array_bound *bound,
314 __isl_take isl_basic_map *bounds)
316 struct gpu_size_info size;
318 bounds = isl_basic_map_detect_equalities(bounds);
319 bounds = check_stride(bound, bounds);
321 bound->size = NULL;
322 bound->lb = NULL;
324 size.bound = bound;
325 size.pos = isl_basic_map_dim(bounds, isl_dim_in);
326 size.bset = isl_basic_map_wrap(bounds);
327 size.bset = isl_basic_set_flatten(size.bset);
328 size.bset = isl_set_simple_hull(isl_basic_set_compute_divs(size.bset));
329 isl_basic_set_foreach_constraint(size.bset, &compute_size_in_direction,
330 &size);
331 isl_basic_set_free(size.bset);
333 return bound->size ? 0 : -1;
336 /* Check if we can find a memory tile for the given array
337 * based on the given accesses, and if so, put the results in "tile".
339 * We project the accesses on each index in turn and look for a parametric
340 * offset such that the size is constant.
342 static int can_tile(__isl_keep isl_map *access, struct gpu_array_tile *tile)
344 int i;
346 for (i = 0; i < tile->n; ++i) {
347 isl_map *access_i;
348 isl_basic_map *hull;
350 access_i = isl_map_copy(access);
351 access_i = isl_map_project_out(access_i, isl_dim_out, 0, i);
352 access_i = isl_map_project_out(access_i, isl_dim_out,
353 1, tile->n - (i + 1));
354 access_i = isl_map_compute_divs(access_i);
355 hull = isl_map_simple_hull(access_i);
356 if (compute_array_dim_size(&tile->bound[i], hull) < 0)
357 return 0;
360 return 1;
363 /* Construct a map from domain_dim to domain_dim that increments
364 * the dimension at position "pos" and leaves all other dimensions
365 * constant.
367 static __isl_give isl_map *next(__isl_take isl_space *domain_dim, int pos)
369 int i;
370 int len = isl_space_dim(domain_dim, isl_dim_set);
371 isl_space *dim;
372 isl_basic_map *next;
373 isl_local_space *ls;
375 dim = isl_space_map_from_set(domain_dim);
376 next = isl_basic_map_universe(isl_space_copy(dim));
377 ls = isl_local_space_from_space(dim);
379 for (i = 0; i < len; ++i) {
380 isl_constraint *c;
382 c = isl_equality_alloc(isl_local_space_copy(ls));
383 c = isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
384 c = isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
385 if (i == pos)
386 c = isl_constraint_set_constant_si(c, 1);
387 next = isl_basic_map_add_constraint(next, c);
390 isl_local_space_free(ls);
392 return isl_map_from_basic_map(next);
395 /* Check if the given access is coalesced.
396 * That is, check whether incrementing the dimension that will get
397 * wrapped over the last thread index results in incrementing
398 * the last array index.
400 * This function is only called for access relations without reuse and
401 * kernels with at least one block dimension.
403 static int access_is_coalesced(struct gpu_gen *gen,
404 __isl_keep isl_union_map *access)
406 isl_space *dim;
407 isl_map *access_map;
408 isl_map *next_thread_x;
409 isl_map *next_element;
410 isl_map *map;
411 int coalesced;
413 access = isl_union_map_copy(access);
414 access = isl_union_map_apply_domain(access,
415 isl_union_map_copy(gen->tiled_sched));
416 access_map = isl_map_from_union_map(access);
418 dim = isl_map_get_space(access_map);
419 dim = isl_space_domain(dim);
420 next_thread_x = next(dim, gen->shared_len + gen->n_block - 1);
422 dim = isl_map_get_space(access_map);
423 dim = isl_space_range(dim);
424 next_element = next(dim, isl_space_dim(dim, isl_dim_set) - 1);
426 map = isl_map_apply_domain(next_thread_x, isl_map_copy(access_map));
427 map = isl_map_apply_range(map, access_map);
429 coalesced = isl_map_is_subset(map, next_element);
431 isl_map_free(next_element);
432 isl_map_free(map);
434 return coalesced;
437 /* Given an access relation in terms of the first gen->shared_len + gen->n_block
438 * dimensions of the computed schedule, check if it is bijective for
439 * fixed values of the first gen->shared_len dimensions.
440 * We perform this check by equating these dimensions to parameters.
442 static int access_is_bijective(struct gpu_gen *gen, __isl_keep isl_map *access)
444 int res;
445 isl_set *par;
446 isl_space *space;
447 isl_id_list *ids;
449 access = isl_map_copy(access);
450 space = isl_space_params(isl_map_get_space(access));
451 ids = ppcg_scop_generate_names(gen->prog->scop, gen->shared_len, "s");
452 par = parametrization(space, gen->shared_len + gen->n_block, 0, ids);
453 isl_id_list_free(ids);
454 access = isl_map_intersect_domain(access, par);
455 res = isl_map_is_bijective(access);
456 isl_map_free(access);
458 return res;
461 /* Look for the last shared tile loop that affects the offset of "tile"
462 * and return the result.
463 * If there is no such loop, then return the index of the loop
464 * before the first shared tile loop, in particular gen->tile_first - 1.
466 static int compute_tile_last_shared(struct gpu_gen *gen,
467 struct gpu_array_tile *tile)
469 int i, j;
471 for (j = gen->shared_len - 1; j >= gen->tile_first; --j) {
472 for (i = 0; i < tile->n; ++i) {
473 isl_aff *lb;
474 isl_aff *shift;
476 lb = tile->bound[i].lb;
477 if (isl_aff_involves_dims(lb, isl_dim_in, j, 1))
478 break;
480 shift = tile->bound[i].shift;
481 if (!shift)
482 continue;
483 if (isl_aff_involves_dims(shift, isl_dim_in, j, 1))
484 break;
486 if (i < tile->n)
487 break;
490 return j;
493 /* Look for the last shared tile loop that affects the offset of the
494 * shared or private tile and store the result in group->last_shared.
495 * If there is no such loop, then group->last_shared is set to a value
496 * before the first shared tile loop, in particular gen->tile_first - 1.
497 * If there is no tile defined on the array reference group,
498 * then set group->last_shared to gen->shared_len - 1.
500 static void set_last_shared(struct gpu_gen *gen,
501 struct gpu_array_ref_group *group)
503 struct gpu_array_tile *tile;
505 group->last_shared = gen->shared_len - 1;
507 tile = group->private_tile;
508 if (!tile)
509 tile = group->shared_tile;
510 if (!tile)
511 return;
513 group->last_shared = compute_tile_last_shared(gen, tile);
516 /* Fill up the groups array with singleton groups, i.e., one group
517 * per reference, initializing the array, access, write, n_ref and refs fields.
518 * In particular the access field is initialized to the scheduled
519 * access relation of the array reference.
521 * Return the number of elements initialized, i.e., the number of
522 * active references in the current kernel.
524 static int populate_array_references(struct gpu_local_array_info *local,
525 __isl_keep isl_union_map *sched, struct gpu_array_ref_group **groups)
527 int i;
528 int n;
529 isl_ctx *ctx = isl_union_map_get_ctx(sched);
531 n = 0;
532 for (i = 0; i < local->array->n_ref; ++i) {
533 isl_union_map *umap;
534 isl_map *map;
535 struct gpu_array_ref_group *group;
536 struct gpu_stmt_access *access = local->array->refs[i];
538 map = isl_map_copy(access->access);
539 umap = isl_union_map_from_map(map);
540 umap = isl_union_map_apply_domain(umap,
541 isl_union_map_copy(sched));
543 if (isl_union_map_is_empty(umap)) {
544 isl_union_map_free(umap);
545 continue;
548 map = isl_map_from_union_map(umap);
549 map = isl_map_detect_equalities(map);
551 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
552 if (!group)
553 return -1;
554 group->local_array = local;
555 group->array = local->array;
556 group->access = map;
557 group->write = access->write;
558 group->exact_write = access->exact_write;
559 group->slice = access->n_index < local->array->n_index;
560 group->refs = &local->array->refs[i];
561 group->n_ref = 1;
563 groups[n++] = group;
566 return n;
569 /* If group->n_ref == 1, then group->refs was set by
570 * populate_array_references to point directly into
571 * group->array->refs and should not be freed.
572 * If group->n_ref > 1, then group->refs was set by join_groups
573 * to point to a newly allocated array.
575 struct gpu_array_ref_group *gpu_array_ref_group_free(
576 struct gpu_array_ref_group *group)
578 if (!group)
579 return NULL;
580 gpu_array_tile_free(group->shared_tile);
581 gpu_array_tile_free(group->private_tile);
582 isl_map_free(group->access);
583 if (group->n_ref > 1)
584 free(group->refs);
585 free(group);
586 return NULL;
589 /* Given a map where the input dimensions represent the tile loops,
590 * eliminate the innermost of those that have a fixed value
591 * until we reach one that does not (obviously) have a fixed value.
593 static __isl_give isl_map *eliminate_fixed_inner_loops(
594 __isl_take isl_map *access)
596 int i, n;
598 n = isl_map_dim(access, isl_dim_in);
600 for (i = n - 1; i >= 0; --i) {
601 if (!map_plain_is_fixed(access, isl_dim_in, i))
602 break;
603 access = isl_map_eliminate(access, isl_dim_in, i, 1);
605 return access;
608 /* Check if the access relations of group1 and group2 overlap within
609 * the innermost loop. In particular, ignore any inner dimension
610 * with a fixed value.
611 * The copying to and from shared memory will be performed within
612 * the innermost actual loop so we are only allowed to consider
613 * the dimensions up to that innermost loop while checking whether
614 * two access relations overlap.
616 static int accesses_overlap(struct gpu_array_ref_group *group1,
617 struct gpu_array_ref_group *group2)
619 int empty;
620 isl_map *access1, *access2;
622 access1 = isl_map_copy(group1->access);
623 access1 = eliminate_fixed_inner_loops(access1);
624 access2 = isl_map_copy(group2->access);
625 access2 = eliminate_fixed_inner_loops(access2);
626 access1 = isl_map_intersect(access1, access2);
627 empty = isl_map_is_empty(access1);
628 isl_map_free(access1);
630 return !empty;
633 /* Combine the given two groups into a single group, containing
634 * the references of both groups.
636 static struct gpu_array_ref_group *join_groups(
637 struct gpu_array_ref_group *group1,
638 struct gpu_array_ref_group *group2)
640 int i;
641 isl_ctx *ctx;
642 struct gpu_array_ref_group *group;
644 ctx = isl_map_get_ctx(group1->access);
645 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
646 if (!group)
647 return NULL;
648 group->local_array = group1->local_array;
649 group->array = group1->array;
650 group->access = isl_map_union(isl_map_copy(group1->access),
651 isl_map_copy(group2->access));
652 group->write = group1->write || group2->write;
653 group->exact_write = group1->exact_write && group2->exact_write;
654 group->slice = group1->slice || group2->slice;
655 group->n_ref = group1->n_ref + group2->n_ref;
656 group->refs = isl_alloc_array(ctx, struct gpu_stmt_access *,
657 group->n_ref);
658 if (!group->refs)
659 return gpu_array_ref_group_free(group);
660 for (i = 0; i < group1->n_ref; ++i)
661 group->refs[i] = group1->refs[i];
662 for (i = 0; i < group2->n_ref; ++i)
663 group->refs[group1->n_ref + i] = group2->refs[i];
665 return group;
668 /* Combine the given two groups into a single group and free
669 * the original two groups.
671 static struct gpu_array_ref_group *join_groups_and_free(
672 struct gpu_array_ref_group *group1,
673 struct gpu_array_ref_group *group2)
675 struct gpu_array_ref_group *group;
677 group = join_groups(group1, group2);
678 gpu_array_ref_group_free(group1);
679 gpu_array_ref_group_free(group2);
680 return group;
683 /* Report that the array reference group with the given access relation
684 * is not mapped to shared memory in the given kernel because
685 * it does not exhibit any reuse and is considered to be coalesced.
687 static void report_no_reuse_and_coalesced(struct ppcg_kernel *kernel,
688 __isl_keep isl_union_map *access)
690 isl_ctx *ctx;
691 isl_printer *p;
693 ctx = isl_union_map_get_ctx(access);
694 p = isl_printer_to_file(ctx, stdout);
695 p = isl_printer_print_str(p, "Array reference group ");
696 p = isl_printer_print_union_map(p, access);
697 p = isl_printer_print_str(p,
698 " not considered for mapping to shared memory in kernel");
699 p = isl_printer_print_int(p, kernel->id);
700 p = isl_printer_print_str(p,
701 " because it exhibits no reuse and is considered to be coalesced");
702 p = isl_printer_end_line(p);
703 isl_printer_free(p);
706 /* Compute the private and/or shared memory tiles for the array
707 * reference group "group" of array "array".
708 * Return 0 on success and -1 on error.
710 * If the array is a read-only scalar or if the user requested
711 * not to use shared or private memory, then we do not need to do anything.
713 * If any reference in the reference group accesses more than one element,
714 * then we would have to make sure that the layout in shared memory
715 * is the same as that in global memory. Since we do not handle this yet
716 * (and it may not even be possible), we refuse to map to private or
717 * shared memory in such cases.
719 * If the array group involves any may writes (that are not must writes),
720 * then we would have to make sure that we load the data into shared/private
721 * memory first in case the data is not written by the kernel
722 * (but still written back out to global memory).
723 * Since we don't have any such mechanism at the moment, we don't
724 * compute shared/private tiles for groups involving may writes.
726 * We only try to compute a shared memory tile if there is any reuse
727 * or if the access is not coalesced.
729 * For computing a private memory tile, we also require that there is
730 * some reuse. Moreover, we require that the access is private
731 * to the thread. That is, we check that any given array element
732 * is only accessed by a single thread.
733 * We compute an access relation that maps the shared tile loop iterators
734 * and the shared point loop iterators that will be wrapped over the
735 * threads to the array elements.
736 * We actually check that those iterators that will be wrapped
737 * partition the array space. This check is stricter than necessary
738 * since several iterations may be mapped onto the same thread
739 * and then they could be allowed to access the same memory elements,
740 * but our check does not allow this situation.
742 * We also check that the index expression only depends on parallel
743 * loops. That way, we can move those loops innermost and unroll them.
744 * Again, we use a test that is stricter than necessary.
745 * We actually check whether the index expression only depends
746 * on the iterators that are wrapped over the threads.
747 * These are necessarily parallel, but there may be more parallel loops.
749 * Combining the injectivity of the first test with the single-valuedness
750 * of the second test, we simply test for bijectivity.
752 * If the array is marked force_private, then we bypass all checks
753 * and assume we can (and should) use registers.
755 * If it turns out we can (or have to) use registers, we compute
756 * the private memory tile size using can_tile, after introducing a dependence
757 * on the thread indices.
759 static int compute_group_bounds_core(struct gpu_gen *gen,
760 struct gpu_array_ref_group *group)
762 isl_ctx *ctx = isl_space_get_ctx(group->array->space);
763 isl_union_map *access;
764 int n_index = group->array->n_index;
765 int no_reuse, coalesced;
766 isl_map *acc;
767 int force_private = group->local_array->force_private;
768 int use_shared = gen->options->use_shared_memory && gen->n_block > 0;
769 int use_private = force_private || gen->options->use_private_memory;
770 int r = 0;
772 if (!use_shared && !use_private)
773 return 0;
774 if (gpu_array_is_read_only_scalar(group->array))
775 return 0;
776 if (!force_private && !group->exact_write)
777 return 0;
778 if (group->slice)
779 return 0;
781 access = gpu_array_ref_group_access_relation(group, 1, 1);
782 no_reuse = isl_union_map_is_injective(access);
783 if (no_reuse < 0)
784 r = -1;
785 if (use_shared && no_reuse)
786 coalesced = access_is_coalesced(gen, access);
788 if (r >= 0 && gen->options->debug->verbose &&
789 use_shared && no_reuse && coalesced)
790 report_no_reuse_and_coalesced(gen->kernel, access);
792 if (use_shared && (!no_reuse || !coalesced)) {
793 group->shared_tile = gpu_array_tile_create(ctx,
794 group->array->n_index);
795 if (!group->shared_tile)
796 r = -1;
797 else if (!can_tile(group->access, group->shared_tile))
798 group->shared_tile =
799 gpu_array_tile_free(group->shared_tile);
802 if (r < 0 || (!force_private && (!use_private || no_reuse))) {
803 isl_union_map_free(access);
804 return r;
807 access = isl_union_map_apply_domain(access,
808 isl_union_map_copy(gen->shared_sched));
810 acc = isl_map_from_union_map(access);
812 if (!force_private && !access_is_bijective(gen, acc)) {
813 isl_map_free(acc);
814 return 0;
817 group->private_tile = gpu_array_tile_create(gen->ctx, n_index);
818 if (!group->private_tile) {
819 isl_map_free(acc);
820 return -1;
822 acc = isl_map_apply_domain(acc, isl_map_copy(gen->privatization));
823 if (!can_tile(acc, group->private_tile))
824 group->private_tile = gpu_array_tile_free(group->private_tile);
826 isl_map_free(acc);
828 if (force_private && !group->private_tile)
829 isl_die(ctx, isl_error_internal,
830 "unable to map array reference group to registers",
831 return -1);
833 return 0;
836 /* Compute the private and/or shared memory tiles for the array
837 * reference group "group" of array "array" and set last_shared.
838 * Return 0 on success and -1 on error.
840 static int compute_group_bounds(struct gpu_gen *gen,
841 struct gpu_array_ref_group *group)
843 if (!group)
844 return -1;
845 if (compute_group_bounds_core(gen, group) < 0)
846 return -1;
847 set_last_shared(gen, group);
849 return 0;
852 /* If two groups have overlapping access relations (as determined by
853 * the "overlap" function) and if one of them involves a write,
854 * then merge the two groups into one.
855 * If "compute_bounds" is set, then call compute_group_bounds
856 * on the merged groups.
858 * Return the updated number of groups.
859 * Return -1 on error.
861 static int group_writes(struct gpu_gen *gen,
862 int n, struct gpu_array_ref_group **groups,
863 int (*overlap)(struct gpu_array_ref_group *group1,
864 struct gpu_array_ref_group *group2), int compute_bounds)
866 int i, j;
868 for (i = 0; i < n; ++i) {
869 for (j = n - 1; j > i; --j) {
870 if (!groups[i]->write && !groups[j]->write)
871 continue;
873 if (!overlap(groups[i], groups[j]))
874 continue;
876 groups[i] = join_groups_and_free(groups[i], groups[j]);
877 if (j != n - 1)
878 groups[j] = groups[n - 1];
879 groups[n - 1] = NULL;
880 n--;
882 if (!groups[i])
883 return -1;
884 if (compute_bounds &&
885 compute_group_bounds(gen, groups[i]) < 0)
886 return -1;
890 return n;
893 /* If two groups have overlapping access relations (within the innermost
894 * loop) and if one of them involves a write, then merge the two groups
895 * into one.
897 * Return the updated number of groups.
899 static int group_overlapping_writes(struct gpu_gen *gen,
900 int n, struct gpu_array_ref_group **groups)
902 return group_writes(gen, n, groups, &accesses_overlap, 0);
905 /* Check if the access relations of group1 and group2 overlap within
906 * the outermost min(group1->last_shared, group2->last_shared) loops.
908 static int last_shared_accesses_overlap(struct gpu_array_ref_group *group1,
909 struct gpu_array_ref_group *group2)
911 int last_shared;
912 int dim;
913 int empty;
914 isl_map *map_i, *map_j, *map;
916 last_shared = group1->last_shared;
917 if (group2->last_shared < last_shared)
918 last_shared = group2->last_shared;
919 map_i = isl_map_copy(group1->access);
920 dim = isl_map_dim(map_i, isl_dim_in);
921 map_i = isl_map_eliminate(map_i, isl_dim_in,
922 last_shared + 1, dim - (last_shared + 1));
923 map_j = isl_map_copy(group2->access);
924 map_j = isl_map_eliminate(map_j, isl_dim_in,
925 last_shared + 1, dim - (last_shared + 1));
926 map = isl_map_intersect(map_i, map_j);
927 empty = isl_map_is_empty(map);
928 isl_map_free(map);
930 return !empty;
933 /* If two groups have overlapping access relations (within the outer
934 * last_shared loops) and if one of them involves a write,
935 * then merge the two groups into one.
937 * Return the updated number of groups.
939 static int group_last_shared_overlapping_writes(struct gpu_gen *gen, int n,
940 struct gpu_array_ref_group **groups)
942 return group_writes(gen, n, groups, &last_shared_accesses_overlap, 1);
945 /* Is the size of the tile specified by "tile" smaller than the sum of
946 * the sizes of the tiles specified by "tile1" and "tile2"?
948 static int smaller_tile(struct gpu_array_tile *tile,
949 struct gpu_array_tile *tile1, struct gpu_array_tile *tile2)
951 int smaller;
952 isl_val *size, *size1, *size2;
954 size = gpu_array_tile_size(tile);
955 size1 = gpu_array_tile_size(tile1);
956 size2 = gpu_array_tile_size(tile2);
958 size = isl_val_sub(size, size1);
959 size = isl_val_sub(size, size2);
960 smaller = isl_val_is_neg(size);
962 isl_val_free(size);
964 return smaller;
967 /* Given an initial grouping of array references and shared memory tiles
968 * for each group that allows for a shared memory tile, merge two groups
969 * if both have a shared memory tile, the merged group also has
970 * a shared memory tile and the size of the tile for the merge group
971 * is smaller than the sum of the tile sizes of the individual groups.
973 * If merging two groups decreases the "last_shared" dimension of
974 * one or both of the two groups, then we need to check for overlapping
975 * writes again.
977 * Return the number of groups after merging.
978 * Return -1 on error.
980 static int group_common_shared_memory_tile(struct gpu_gen *gen,
981 struct gpu_array_info *array, int n,
982 struct gpu_array_ref_group **groups)
984 int i, j;
985 int recompute_overlap = 0;
986 isl_ctx *ctx = isl_space_get_ctx(array->space);
988 for (i = 0; i < n; ++i) {
989 if (!groups[i]->shared_tile)
990 continue;
991 for (j = n - 1; j > i; --j) {
992 isl_map *map;
993 int empty;
994 struct gpu_array_ref_group *group;
996 if (!groups[j]->shared_tile)
997 continue;
999 map = isl_map_intersect(isl_map_copy(groups[i]->access),
1000 isl_map_copy(groups[j]->access));
1001 empty = isl_map_is_empty(map);
1002 isl_map_free(map);
1004 if (empty)
1005 continue;
1007 group = join_groups(groups[i], groups[j]);
1008 if (compute_group_bounds(gen, group) < 0) {
1009 gpu_array_ref_group_free(group);
1010 return -1;
1012 if (!group->shared_tile ||
1013 !smaller_tile(group->shared_tile,
1014 groups[i]->shared_tile,
1015 groups[j]->shared_tile)) {
1016 gpu_array_ref_group_free(group);
1017 continue;
1020 if (group->last_shared < groups[i]->last_shared ||
1021 group->last_shared < groups[j]->last_shared)
1022 recompute_overlap = 1;
1023 gpu_array_ref_group_free(groups[i]);
1024 gpu_array_ref_group_free(groups[j]);
1025 groups[i] = group;
1026 if (j != n - 1)
1027 groups[j] = groups[n - 1];
1028 n--;
1032 if (recompute_overlap)
1033 n = group_last_shared_overlapping_writes(gen, n, groups);
1034 return n;
1037 /* Set array->n_group and array->groups to n and groups.
1039 * Additionally, set the "nr" field of each group.
1041 static void set_array_groups(struct gpu_local_array_info *array,
1042 int n, struct gpu_array_ref_group **groups)
1044 int i, j;
1046 array->n_group = n;
1047 array->groups = groups;
1049 for (i = 0; i < n; ++i)
1050 groups[i]->nr = i;
1053 /* Group array references that should be considered together when
1054 * deciding whether to access them from private, shared or global memory.
1055 * Return -1 on error.
1057 * In particular, if two array references overlap and if one of them
1058 * is a write, then the two references are grouped together.
1059 * We first perform an initial grouping based only on the access relation.
1060 * After computing shared and private memory tiles, we check for
1061 * overlapping writes again, but this time taking into account
1062 * the "last_shared" property.
1064 * Furthermore, if two groups admit a shared memory tile and if the
1065 * combination of the two also admits a shared memory tile, we merge
1066 * the two groups.
1068 * If the array contains structures, then there is no need to compute
1069 * reference groups since we do not map such arrays to private or shared
1070 * memory.
1072 static int group_array_references(struct gpu_gen *gen,
1073 struct gpu_local_array_info *local, __isl_keep isl_union_map *sched)
1075 int i;
1076 int n;
1077 isl_ctx *ctx = isl_union_map_get_ctx(sched);
1078 struct gpu_array_ref_group **groups;
1080 if (local->array->has_compound_element)
1081 return 0;
1083 groups = isl_calloc_array(ctx, struct gpu_array_ref_group *,
1084 local->array->n_ref);
1085 if (!groups)
1086 return -1;
1088 n = populate_array_references(local, sched, groups);
1090 n = group_overlapping_writes(gen, n, groups);
1092 for (i = 0; i < n; ++i)
1093 if (compute_group_bounds(gen, groups[i]) < 0)
1094 n = -1;
1096 n = group_last_shared_overlapping_writes(gen, n, groups);
1098 n = group_common_shared_memory_tile(gen, local->array, n, groups);
1100 set_array_groups(local, n, groups);
1102 if (n >= 0)
1103 return 0;
1105 for (i = 0; i < local->array->n_ref; ++i)
1106 gpu_array_ref_group_free(groups[i]);
1107 return -1;
1110 /* For each scalar in the input program, check if there are any
1111 * order dependences active inside the current kernel, within
1112 * the same iteration of the host schedule.
1113 * If so, mark the scalar as force_private so that it will be
1114 * mapped to a register.
1116 static void check_scalar_live_ranges(struct gpu_gen *gen)
1118 int i;
1119 isl_map *proj;
1120 isl_union_map *sched;
1121 isl_union_set *domain;
1122 isl_union_map *same_host_iteration;
1124 gen->kernel->any_force_private = 0;
1126 if (!gen->options->live_range_reordering)
1127 return;
1129 sched = gen->shared_sched;
1130 sched = isl_union_map_universe(isl_union_map_copy(sched));
1131 domain = isl_union_map_domain(sched);
1133 sched = isl_union_map_copy(gen->sched);
1134 proj = projection(isl_union_map_get_space(sched),
1135 gen->untiled_len, gen->tile_first);
1136 sched = isl_union_map_apply_range(sched, isl_union_map_from_map(proj));
1137 same_host_iteration = isl_union_map_apply_range(sched,
1138 isl_union_map_reverse(isl_union_map_copy(sched)));
1140 for (i = 0; i < gen->kernel->n_array; ++i) {
1141 struct gpu_local_array_info *local = &gen->kernel->array[i];
1142 isl_union_map *order;
1144 local->force_private = 0;
1145 if (local->array->n_index != 0)
1146 continue;
1147 order = isl_union_map_copy(local->array->dep_order);
1148 order = isl_union_map_intersect_domain(order,
1149 isl_union_set_copy(domain));
1150 order = isl_union_map_intersect_range(order,
1151 isl_union_set_copy(domain));
1152 order = isl_union_map_intersect(order,
1153 isl_union_map_copy(same_host_iteration));
1154 if (!isl_union_map_is_empty(order)) {
1155 local->force_private = 1;
1156 gen->kernel->any_force_private = 1;
1158 isl_union_map_free(order);
1161 isl_union_map_free(same_host_iteration);
1162 isl_union_set_free(domain);
1165 /* Group references of all arrays in the current kernel.
1167 int gpu_group_references(struct gpu_gen *gen)
1169 int i;
1170 int r = 0;
1171 isl_union_map *sched;
1173 check_scalar_live_ranges(gen);
1175 sched = isl_union_map_apply_range(isl_union_map_copy(gen->shared_sched),
1176 isl_union_map_copy(gen->shared_proj));
1178 for (i = 0; i < gen->kernel->n_array; ++i) {
1179 r = group_array_references(gen, &gen->kernel->array[i], sched);
1180 if (r < 0)
1181 break;
1184 isl_union_map_free(sched);
1186 return r;
1189 /* Given a description of an array tile "tile" and the "space"
1191 * { D -> A }
1193 * where D represents the first shared_len schedule dimensions
1194 * and A represents the array, construct an isl_multi_aff
1196 * { [D[i] -> A[a]] -> A'[a'] }
1198 * with A' a scaled down copy of A according to the shifts and strides
1199 * in "tile". In particular,
1201 * a' = (a + shift(i))/stride
1203 * "insert_array" represents
1205 * { [D -> A] -> D }
1207 * and is used to insert A into the domain of functions that only
1208 * reference D.
1210 static __isl_give isl_multi_aff *strided_tile(
1211 struct gpu_array_tile *tile, __isl_keep isl_space *space,
1212 __isl_keep isl_multi_aff *insert_array)
1214 int i;
1215 isl_ctx *ctx;
1216 isl_multi_aff *shift;
1217 isl_multi_val *stride;
1218 isl_space *space2;
1219 isl_local_space *ls;
1220 isl_multi_aff *tiling;
1222 ctx = isl_space_get_ctx(space);
1223 space2 = isl_space_domain(isl_space_copy(space));
1224 ls = isl_local_space_from_space(space2);
1225 space2 = isl_space_range(isl_space_copy(space));
1226 stride = isl_multi_val_zero(space2);
1227 shift = isl_multi_aff_zero(isl_space_copy(space));
1229 for (i = 0; i < tile->n; ++i) {
1230 struct gpu_array_bound *bound = &tile->bound[i];
1231 isl_val *stride_i;
1232 isl_aff *shift_i;
1234 if (tile->bound[i].shift) {
1235 stride_i = isl_val_copy(bound->stride);
1236 shift_i = isl_aff_copy(bound->shift);
1237 } else {
1238 stride_i = isl_val_one(ctx);
1239 shift_i = isl_aff_zero_on_domain(
1240 isl_local_space_copy(ls));
1243 stride = isl_multi_val_set_val(stride, i, stride_i);
1244 shift = isl_multi_aff_set_aff(shift, i, shift_i);
1246 isl_local_space_free(ls);
1248 shift = isl_multi_aff_pullback_multi_aff(shift,
1249 isl_multi_aff_copy(insert_array));
1251 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1252 tiling = isl_multi_aff_add(tiling, shift);
1253 tiling = isl_multi_aff_scale_down_multi_val(tiling, stride);
1255 return tiling;
1258 /* Compute a tiling for the array reference group "group".
1260 * The tiling is of the form
1262 * { [D[i] -> A[a]] -> T[t] }
1264 * where D represents the first shared_len schedule dimensions,
1265 * A represents the global array and T represents the shared or
1266 * private memory tile. The name of T is the name of the local
1267 * array.
1269 * If there is any stride in the accesses, then the mapping is
1271 * t = (a + shift(i))/stride - lb(i)
1273 * otherwise, it is simply
1275 * t = a - lb(i)
1277 void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group *group)
1279 int i;
1280 struct gpu_array_tile *tile;
1281 struct gpu_array_info *array = group->array;
1282 isl_space *space;
1283 isl_multi_aff *tiling, *lb, *insert_array;
1284 isl_printer *p;
1285 char *local_name;
1287 tile = group->private_tile;
1288 if (!tile)
1289 tile = group->shared_tile;
1290 if (!tile)
1291 return;
1293 space = isl_map_get_space(group->access);
1294 insert_array = isl_multi_aff_domain_map(isl_space_copy(space));
1296 for (i = 0; i < tile->n; ++i)
1297 if (tile->bound[i].shift)
1298 break;
1300 if (i < tile->n)
1301 tiling = strided_tile(tile, space, insert_array);
1302 else
1303 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1305 lb = isl_multi_aff_zero(space);
1306 for (i = 0; i < tile->n; ++i) {
1307 isl_aff *lb_i = isl_aff_copy(tile->bound[i].lb);
1308 lb = isl_multi_aff_set_aff(lb, i, lb_i);
1310 lb = isl_multi_aff_pullback_multi_aff(lb, insert_array);
1312 tiling = isl_multi_aff_sub(tiling, lb);
1314 p = isl_printer_to_str(isl_multi_aff_get_ctx(tiling));
1315 p = gpu_array_ref_group_print_name(group, p);
1316 local_name = isl_printer_get_str(p);
1317 isl_printer_free(p);
1318 tiling = isl_multi_aff_set_tuple_name(tiling, isl_dim_out, local_name);
1319 free(local_name);
1321 tile->tiling = tiling;