cuda.c: print_reverse_list: improve error handling
[ppcg.git] / gpu_group.c
blob37ebff524f692803f2e923543bf747b1d9a5c29a
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl/constraint.h>
15 #include <isl/ilp.h>
17 #include "gpu_array_tile.h"
18 #include "gpu_group.h"
19 #include "gpu_tree.h"
20 #include "schedule.h"
22 /* Print the name of the local copy of a given group of array references.
24 __isl_give isl_printer *gpu_array_ref_group_print_name(
25 struct gpu_array_ref_group *group, __isl_take isl_printer *p)
27 int global = 0;
28 enum ppcg_group_access_type type;
30 type = gpu_array_ref_group_type(group);
31 if (type == ppcg_access_private)
32 p = isl_printer_print_str(p, "private_");
33 else if (type == ppcg_access_shared)
34 p = isl_printer_print_str(p, "shared_");
35 else
36 global = 1;
37 p = isl_printer_print_str(p, group->array->name);
38 if (!global && group->local_array->n_group > 1) {
39 p = isl_printer_print_str(p, "_");
40 p = isl_printer_print_int(p, group->nr);
43 return p;
46 /* Return the union of all read (read = 1) and/or write (write = 1)
47 * access relations in the group.
49 __isl_give isl_union_map *gpu_array_ref_group_access_relation(
50 struct gpu_array_ref_group *group, int read, int write)
52 int i;
53 isl_union_map *access;
55 access = isl_union_map_empty(isl_map_get_space(group->access));
56 for (i = 0; i < group->n_ref; ++i) {
57 isl_map *map_i;
59 if (!((read && group->refs[i]->read) ||
60 (write && group->refs[i]->write)))
61 continue;
62 map_i = isl_map_copy(group->refs[i]->access);
63 access = isl_union_map_union(access,
64 isl_union_map_from_map(map_i));
67 return access;
70 /* Should this array reference group be mapped to private, shared or global
71 * memory?
72 * If we have computed both a private and a shared tile, then
73 * the private tile is used, i.e., the group is mapped to private memory.
75 enum ppcg_group_access_type gpu_array_ref_group_type(
76 struct gpu_array_ref_group *group)
78 if (group->private_tile)
79 return ppcg_access_private;
80 if (group->shared_tile)
81 return ppcg_access_shared;
82 return ppcg_access_global;
86 /* Return the effective gpu_array_tile associated to "group" or
87 * NULL if there is no such gpu_array_tile.
89 struct gpu_array_tile *gpu_array_ref_group_tile(
90 struct gpu_array_ref_group *group)
92 switch (gpu_array_ref_group_type(group)) {
93 case ppcg_access_global:
94 return NULL;
95 case ppcg_access_shared:
96 return group->shared_tile;
97 case ppcg_access_private:
98 return group->private_tile;
102 /* Does the tile associated to "group" require unrolling of the schedule
103 * dimensions mapped to threads?
104 * Note that this can only happen for private tiles.
106 int gpu_array_ref_group_requires_unroll(struct gpu_array_ref_group *group)
108 struct gpu_array_tile *tile;
110 tile = gpu_array_ref_group_tile(group);
111 if (!tile)
112 return 0;
113 return tile->requires_unroll;
116 /* Given a constraint
118 * a(p,i) + j = g f(e)
120 * or -a(p,i) - j = g f(e) if sign < 0,
121 * store a(p,i) in bound->shift and g (stride) in bound->stride.
122 * a(p,i) is assumed to be an expression in only the parameters
123 * and the input dimensions.
125 static void extract_stride(__isl_keep isl_constraint *c,
126 struct gpu_array_bound *bound, __isl_keep isl_val *stride, int sign)
128 int i;
129 isl_val *v;
130 isl_space *space;
131 unsigned nparam;
132 unsigned nvar;
133 isl_aff *aff;
135 isl_val_free(bound->stride);
136 bound->stride = isl_val_copy(stride);
138 space = isl_constraint_get_space(c);
139 space = isl_space_domain(space);
141 nparam = isl_space_dim(space, isl_dim_param);
142 nvar = isl_space_dim(space, isl_dim_set);
144 v = isl_constraint_get_constant_val(c);
145 if (sign < 0)
146 v = isl_val_neg(v);
147 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
148 aff = isl_aff_set_constant_val(aff, v);
150 for (i = 0; i < nparam; ++i) {
151 if (!isl_constraint_involves_dims(c, isl_dim_param, i, 1))
152 continue;
153 v = isl_constraint_get_coefficient_val(c, isl_dim_param, i);
154 if (sign < 0)
155 v = isl_val_neg(v);
156 aff = isl_aff_add_coefficient_val(aff, isl_dim_param, i, v);
159 for (i = 0; i < nvar; ++i) {
160 if (!isl_constraint_involves_dims(c, isl_dim_in, i, 1))
161 continue;
162 v = isl_constraint_get_coefficient_val(c, isl_dim_in, i);
163 if (sign < 0)
164 v = isl_val_neg(v);
165 aff = isl_aff_add_coefficient_val(aff, isl_dim_in, i, v);
168 bound->shift = aff;
171 /* Given an equality constraint of a map with a single output dimension j,
172 * check if the constraint is of the form
174 * a(p,i) + j = g f(e)
176 * with a(p,i) an expression in the parameters and input dimensions
177 * and f(e) an expression in the existentially quantified variables.
178 * If so, and if g is larger than any such g from a previously considered
179 * constraint, then call extract_stride to record the stride information
180 * in bound.
182 static isl_stat check_stride_constraint(__isl_take isl_constraint *c,
183 void *user)
185 int i;
186 isl_ctx *ctx;
187 isl_val *v;
188 unsigned n_div;
189 struct gpu_array_bound *bound = user;
191 ctx = isl_constraint_get_ctx(c);
192 n_div = isl_constraint_dim(c, isl_dim_div);
193 v = isl_constraint_get_coefficient_val(c, isl_dim_out, 0);
195 if (n_div && (isl_val_is_one(v) || isl_val_is_negone(v))) {
196 int s = isl_val_sgn(v);
197 isl_val *stride = isl_val_zero(ctx);
199 isl_val_free(v);
200 for (i = 0; i < n_div; ++i) {
201 v = isl_constraint_get_coefficient_val(c,
202 isl_dim_div, i);
203 stride = isl_val_gcd(stride, v);
205 if (!isl_val_is_zero(stride) &&
206 isl_val_gt(stride, bound->stride))
207 extract_stride(c, bound, stride, s);
209 isl_val_free(stride);
210 } else
211 isl_val_free(v);
213 isl_constraint_free(c);
214 return isl_stat_ok;
217 /* Given contraints on an array index i, check if we can find
218 * a shift a(p) and a stride g such that
220 * a(p) + i = 0 mod g
222 * If so, record the information in bound and apply the mapping
223 * i -> (i + a(p))/g to the array index in bounds and return
224 * the new constraints.
225 * If not, simply return the original constraints.
227 * If bounds is a subset of the space
229 * D -> i
231 * then the bound recorded in bound->shift is of the form
233 * D -> s(D)
235 * with s(D) equal to a(p) above.
236 * Next, we construct a mapping of the form
238 * [D -> i] -> [D -> (i + S(D))/g]
240 * This mapping is computed as follows.
241 * We first introduce "i" in the domain through precomposition
242 * with [D -> i] -> D obtaining
244 * [D -> i] -> s(D)
246 * Adding [D -> i] -> i produces
248 * [D -> i] -> i + s(D)
250 * and the domain product with [D -> i] -> D yields
252 * [D -> i] -> [D -> i + s(D)]
254 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
256 static __isl_give isl_basic_map *check_stride(struct gpu_array_bound *bound,
257 __isl_take isl_basic_map *bounds)
259 isl_space *space;
260 isl_basic_map *hull;
261 isl_basic_map *shift, *id, *bmap, *scale;
262 isl_basic_set *bset;
263 isl_aff *aff;
265 bound->stride = NULL;
267 hull = isl_basic_map_affine_hull(isl_basic_map_copy(bounds));
269 isl_basic_map_foreach_constraint(hull, &check_stride_constraint, bound);
271 isl_basic_map_free(hull);
273 if (!bound->stride)
274 return bounds;
276 shift = isl_basic_map_from_aff(isl_aff_copy(bound->shift));
277 space = isl_basic_map_get_space(bounds);
278 bmap = isl_basic_map_domain_map(isl_basic_map_universe(space));
279 shift = isl_basic_map_apply_range(bmap, shift);
280 space = isl_basic_map_get_space(bounds);
281 id = isl_basic_map_range_map(isl_basic_map_universe(space));
282 shift = isl_basic_map_sum(id, shift);
283 space = isl_basic_map_get_space(bounds);
284 id = isl_basic_map_domain_map(isl_basic_map_universe(space));
285 shift = isl_basic_map_range_product(id, shift);
287 space = isl_space_domain(isl_basic_map_get_space(bounds));
288 id = isl_basic_map_identity(isl_space_map_from_set(space));
289 space = isl_space_range(isl_basic_map_get_space(bounds));
290 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
291 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
292 aff = isl_aff_scale_down_val(aff, isl_val_copy(bound->stride));
293 scale = isl_basic_map_from_aff(aff);
294 scale = isl_basic_map_product(id, scale);
296 bmap = isl_basic_map_apply_range(shift, scale);
297 bset = isl_basic_set_apply(isl_basic_map_wrap(bounds), bmap);
298 bounds = isl_basic_set_unwrap(bset);
300 return bounds;
303 /* Data used in compute_array_dim_size and compute_size_in_direction.
305 * pos is the position of the variable representing the array index,
306 * i.e., the variable for which want to compute the size. This variable
307 * is also the last variable in the set.
309 struct gpu_size_info {
310 isl_basic_set *bset;
311 struct gpu_array_bound *bound;
312 int pos;
315 /* Given a constraint from the basic set describing the bounds on
316 * an array index, check if it is a lower bound, say m i >= b(x), and,
317 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
318 * upper bound. If so, and if this bound is smaller than any bound
319 * derived from earlier constraints, set the size to this bound on
320 * the expression and the lower bound to ceil(b(x)/m).
322 static isl_stat compute_size_in_direction(__isl_take isl_constraint *c,
323 void *user)
325 struct gpu_size_info *size = user;
326 unsigned nparam;
327 unsigned n_div;
328 isl_val *v;
329 isl_aff *aff;
330 isl_aff *lb;
332 nparam = isl_basic_set_dim(size->bset, isl_dim_param);
333 n_div = isl_constraint_dim(c, isl_dim_div);
335 if (isl_constraint_involves_dims(c, isl_dim_div, 0, n_div) ||
336 !isl_constraint_is_lower_bound(c, isl_dim_set, size->pos)) {
337 isl_constraint_free(c);
338 return isl_stat_ok;
341 aff = isl_constraint_get_bound(c, isl_dim_set, size->pos);
342 aff = isl_aff_ceil(aff);
344 lb = isl_aff_copy(aff);
346 aff = isl_aff_neg(aff);
347 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, size->pos, 1);
349 v = isl_basic_set_max_val(size->bset, aff);
350 isl_aff_free(aff);
352 if (isl_val_is_int(v)) {
353 v = isl_val_add_ui(v, 1);
354 if (!size->bound->size || isl_val_lt(v, size->bound->size)) {
355 isl_val_free(size->bound->size);
356 size->bound->size = isl_val_copy(v);
357 lb = isl_aff_drop_dims(lb, isl_dim_in, size->pos, 1);
358 isl_aff_free(size->bound->lb);
359 size->bound->lb = isl_aff_copy(lb);
362 isl_val_free(v);
363 isl_aff_free(lb);
365 isl_constraint_free(c);
367 return isl_stat_ok;
370 /* Given a basic map "bounds" that maps parameters and input dimensions
371 * to a single output dimension, look for an expression in the parameters
372 * and input dimensions such that the range of the output dimension shifted
373 * by this expression is a constant.
375 * In particular, we currently only consider lower bounds on the output
376 * dimension as candidate expressions.
378 static int compute_array_dim_size(struct gpu_array_bound *bound,
379 __isl_take isl_basic_map *bounds)
381 struct gpu_size_info size;
383 bounds = isl_basic_map_detect_equalities(bounds);
384 bounds = check_stride(bound, bounds);
386 bound->size = NULL;
387 bound->lb = NULL;
389 size.bound = bound;
390 size.pos = isl_basic_map_dim(bounds, isl_dim_in);
391 size.bset = isl_basic_map_wrap(bounds);
392 size.bset = isl_basic_set_flatten(size.bset);
393 size.bset = isl_set_simple_hull(isl_basic_set_compute_divs(size.bset));
394 isl_basic_set_foreach_constraint(size.bset, &compute_size_in_direction,
395 &size);
396 isl_basic_set_free(size.bset);
398 return bound->size ? 0 : -1;
401 /* Check if we can find a memory tile for the given array
402 * based on the given accesses, and if so, put the results in "tile".
404 * We project the accesses on each index in turn and look for a parametric
405 * offset such that the size is constant.
407 * tile->depth is initialized to the input dimension of the computed bounds.
409 static int can_tile(__isl_keep isl_map *access, struct gpu_array_tile *tile)
411 int i;
413 tile->depth = isl_map_dim(access, isl_dim_in);
415 for (i = 0; i < tile->n; ++i) {
416 isl_map *access_i;
417 isl_basic_map *hull;
419 access_i = isl_map_copy(access);
420 access_i = isl_map_project_out(access_i, isl_dim_out, 0, i);
421 access_i = isl_map_project_out(access_i, isl_dim_out,
422 1, tile->n - (i + 1));
423 access_i = isl_map_compute_divs(access_i);
424 hull = isl_map_simple_hull(access_i);
425 if (compute_array_dim_size(&tile->bound[i], hull) < 0)
426 return 0;
429 return 1;
432 /* Internal data structure for gpu_group_references.
434 * scop represents the input scop.
435 * kernel_depth is the schedule depth where the kernel launch will
436 * be introduced, i.e., it is the depth of the band that is mapped
437 * to blocks.
438 * thread_depth is the schedule depth where the thread mark is located,
439 * i.e., it is the depth of the band that is mapped to threads and also
440 * the schedule depth at which the copying to/from shared/private memory
441 * is computed. The copy operation may then later be hoisted to
442 * a higher level.
443 * n_thread is the number of schedule dimensions in the band that
444 * is mapped to threads.
445 * privatization lives in the range of thread_sched (i.e., it is
446 * of dimension thread_depth + n_thread) and encodes the mapping
447 * to thread identifiers (as parameters).
448 * host_sched contains the kernel_depth dimensions of the host schedule.
449 * shared_sched contains the first thread_depth dimensions of the
450 * kernel schedule.
451 * thread_sched contains the first (thread_depth + n_thread) dimensions
452 * of the kernel schedule.
453 * full_sched is a union_map representation of the entire kernel schedule.
455 struct gpu_group_data {
456 struct ppcg_scop *scop;
457 int kernel_depth;
458 int thread_depth;
459 int n_thread;
460 isl_set *privatization;
461 isl_union_map *host_sched;
462 isl_union_map *shared_sched;
463 isl_union_map *thread_sched;
464 isl_union_map *full_sched;
467 /* Construct a map from domain_space to domain_space that increments
468 * the dimension at position "pos" and leaves all other dimensions
469 * constant.
471 static __isl_give isl_map *next(__isl_take isl_space *domain_space, int pos)
473 isl_space *space;
474 isl_aff *aff;
475 isl_multi_aff *next;
477 space = isl_space_map_from_set(domain_space);
478 next = isl_multi_aff_identity(space);
479 aff = isl_multi_aff_get_aff(next, pos);
480 aff = isl_aff_add_constant_si(aff, 1);
481 next = isl_multi_aff_set_aff(next, pos, aff);
483 return isl_map_from_multi_aff(next);
486 /* Check if the given access is coalesced (or if there is no point
487 * in trying to coalesce the access by mapping the array to shared memory).
488 * That is, check whether incrementing the dimension that will get
489 * wrapped over the last thread index results in incrementing
490 * the last array index.
492 * If no two consecutive array elements are ever accessed by "access",
493 * then mapping the corresponding array to shared memory will not
494 * improve coalescing. In fact, the copying will likely be performed
495 * by a single thread. Consider the access as coalesced such that
496 * the caller will not try and map the array to shared memory just
497 * to improve coalescing.
499 * This function is only called for access relations without reuse and
500 * kernels with at least one thread identifier.
502 static int access_is_coalesced(struct gpu_group_data *data,
503 __isl_keep isl_union_map *access)
505 int dim;
506 isl_space *space;
507 isl_set *accessed;
508 isl_map *access_map;
509 isl_map *next_thread_x;
510 isl_map *next_element;
511 isl_map *map;
512 int coalesced, empty;
514 access = isl_union_map_copy(access);
515 access = isl_union_map_apply_domain(access,
516 isl_union_map_copy(data->full_sched));
517 access_map = isl_map_from_union_map(access);
519 space = isl_map_get_space(access_map);
520 space = isl_space_range(space);
521 dim = isl_space_dim(space, isl_dim_set);
522 if (dim == 0)
523 next_element = isl_map_empty(isl_space_map_from_set(space));
524 else
525 next_element = next(space, dim - 1);
527 accessed = isl_map_range(isl_map_copy(access_map));
528 map = isl_map_copy(next_element);
529 map = isl_map_intersect_domain(map, isl_set_copy(accessed));
530 map = isl_map_intersect_range(map, accessed);
531 empty = isl_map_is_empty(map);
532 isl_map_free(map);
534 if (empty < 0 || empty) {
535 isl_map_free(next_element);
536 isl_map_free(access_map);
537 return empty;
540 space = isl_map_get_space(access_map);
541 space = isl_space_domain(space);
542 next_thread_x = next(space, data->thread_depth + data->n_thread - 1);
544 map = isl_map_apply_domain(next_thread_x, isl_map_copy(access_map));
545 map = isl_map_apply_range(map, access_map);
547 coalesced = isl_map_is_subset(map, next_element);
549 isl_map_free(next_element);
550 isl_map_free(map);
552 return coalesced;
555 /* Replace the host schedule dimensions in the access relation "access"
556 * by parameters, so that they are treated as fixed when checking for reuse
557 * (within a kernel) or whether two consecutive elements are accessed
558 * (within a kernel).
560 static __isl_give isl_union_map *localize_access(struct gpu_group_data *data,
561 __isl_take isl_union_map *access)
563 int n;
564 isl_space *space;
565 isl_set *param;
566 isl_union_map *umap;
567 isl_id_list *ids;
569 umap = isl_union_map_copy(data->host_sched);
570 space = isl_union_map_get_space(umap);
571 n = data->kernel_depth;
572 ids = ppcg_scop_generate_names(data->scop, n, "__ppcg_host_");
573 param = parametrization(space, n, 0, ids);
574 isl_id_list_free(ids);
575 umap = isl_union_map_intersect_range(umap,
576 isl_union_set_from_set(param));
577 access = isl_union_map_intersect_domain(access,
578 isl_union_map_domain(umap));
580 return access;
583 /* Given an access relation in terms of at least data->thread_depth initial
584 * dimensions of the computed schedule, check if it is bijective for
585 * fixed values of the first data->thread_depth dimensions.
586 * We perform this check by equating these dimensions to parameters.
588 static int access_is_bijective(struct gpu_group_data *data,
589 __isl_keep isl_map *access)
591 int res;
592 int dim;
593 isl_set *par;
594 isl_space *space;
595 isl_id_list *ids;
597 access = isl_map_copy(access);
598 space = isl_space_params(isl_map_get_space(access));
599 ids = ppcg_scop_generate_names(data->scop, data->thread_depth, "s");
600 dim = isl_map_dim(access, isl_dim_in);
601 par = parametrization(space, dim, 0, ids);
602 isl_id_list_free(ids);
603 access = isl_map_intersect_domain(access, par);
604 res = isl_map_is_bijective(access);
605 isl_map_free(access);
607 return res;
610 /* Compute the number of outer schedule tile dimensions that affect
611 * the offset of "tile".
612 * If there is no such dimension, then return the index
613 * of the first kernel dimension, i.e., data->kernel_depth.
615 static int compute_tile_depth(struct gpu_group_data *data,
616 struct gpu_array_tile *tile)
618 int i, j;
620 for (j = tile->depth - 1; j >= data->kernel_depth; --j) {
621 for (i = 0; i < tile->n; ++i) {
622 isl_aff *lb;
623 isl_aff *shift;
625 lb = tile->bound[i].lb;
626 if (isl_aff_involves_dims(lb, isl_dim_in, j, 1))
627 break;
629 shift = tile->bound[i].shift;
630 if (!shift)
631 continue;
632 if (isl_aff_involves_dims(shift, isl_dim_in, j, 1))
633 break;
635 if (i < tile->n)
636 break;
639 return ++j;
642 /* Return the lowest depth between data->kernel_depth and data->thread_depth
643 * at which every array element accessed through "acc" is accessed
644 * by a single thread. The input dimension of "acc" is
645 * data->thread_depth + data->n_thread, where the final data->n_thread
646 * dimensions are those that will be mapped to threads.
647 * If the values for these dimensions are uniquely determined
648 * by the array index and a given number of outer dimensions, then
649 * there is only one thread accessing that array element within those
650 * outer dimensions.
652 * The input space of "acc" is first split up, such that it has the form
654 * [O -> T] -> A
656 * with O the outer dimensions, T the dimensions that will be mapped to threads
657 * and A the array index.
659 * Then the positions of T and A are interchanged to simplify the test
660 * whether T uniquely depends on O and A.
661 * In particular, the above access relation is first combined with
663 * [O -> T] -> T
665 * to form
667 * [O -> T] -> [A -> T]
669 * from which
671 * O -> [A -> T]
673 * is extracted, which is then uncurried to
675 * [O -> A] -> T
677 * Finally, the final dimensions of O are projected out one by one
678 * until T is no longer uniquely determined by A and the remaining
679 * dimensions in O. The value returned is that of the last dimension
680 * that was successfully projected out.
681 * Note that there is no need to test whether [O -> A] -> T itself
682 * is single-valued as that was already tested in access_is_bijective.
684 static int compute_accessed_by_single_thread_depth(struct gpu_group_data *data,
685 __isl_keep isl_map *acc)
687 int i;
688 isl_space *space;
689 isl_map *map;
690 isl_bool sv;
692 if (data->thread_depth == data->kernel_depth)
693 return data->thread_depth;
695 acc = isl_map_copy(acc);
697 space = isl_map_get_space(acc);
698 space = isl_space_params(space);
699 space = isl_space_set_from_params(space);
700 space = isl_space_add_dims(space, isl_dim_set, data->thread_depth);
701 space = isl_space_from_domain(space);
702 space = isl_space_add_dims(space, isl_dim_out, data->n_thread);
703 space = isl_space_wrap(space);
704 map = isl_set_flatten_map(isl_set_universe(space));
705 acc = isl_map_apply_range(map, acc);
707 space = isl_space_domain(isl_map_get_space(acc));
708 map = isl_map_range_map(isl_map_universe(isl_space_unwrap(space)));
709 acc = isl_map_range_product(acc, map);
710 acc = isl_map_domain_factor_domain(acc);
711 acc = isl_map_uncurry(acc);
713 for (i = data->thread_depth - 1; i >= data->kernel_depth; --i) {
714 acc = isl_map_project_out(acc, isl_dim_in, i, 1);
715 sv = isl_map_is_single_valued(acc);
716 if (sv < 0)
717 return -1;
718 if (!sv)
719 break;
722 isl_map_free(acc);
724 return ++i;
727 /* Adjust the fields of "tile" to reflect the new input dimension "depth".
728 * The dimension beyond "depth" are assumed not to affect the tile,
729 * so they can simply be dropped.
731 static int tile_adjust_depth(struct gpu_array_tile *tile, int depth)
733 int i;
735 if (tile->depth == depth)
736 return 0;
738 for (i = 0; i < tile->n; ++i) {
739 tile->bound[i].lb = isl_aff_drop_dims(tile->bound[i].lb,
740 isl_dim_in, depth, tile->depth - depth);
741 if (!tile->bound[i].lb)
742 return -1;
743 if (!tile->bound[i].shift)
744 continue;
745 tile->bound[i].shift = isl_aff_drop_dims(tile->bound[i].shift,
746 isl_dim_in, depth, tile->depth - depth);
747 if (!tile->bound[i].shift)
748 return -1;
751 tile->depth = depth;
753 return 0;
756 /* Determine the number of schedule dimensions that affect the offset of the
757 * shared or private tile "tile" and store the result in tile->depth, with
758 * a lower bound of data->kernel_depth.
759 * Also adjust the fields of the tile to only refer to the tile->depth
760 * outer schedule dimensions.
762 static isl_stat tile_set_depth(struct gpu_group_data *data,
763 struct gpu_array_tile *tile)
765 if (tile_adjust_depth(tile, compute_tile_depth(data, tile)) < 0)
766 return isl_stat_error;
768 return isl_stat_ok;
771 /* Determine the number of schedule dimensions that affect the offset of the
772 * shared tile and store the minimum of the private and shared tile depth
773 * in group->min_depth, with a lower bound of data->kernel_depth.
774 * If there is no tile defined on the array reference group,
775 * then set group->min_depth to data->thread_depth.
777 static int set_depth(struct gpu_group_data *data,
778 struct gpu_array_ref_group *group)
780 group->min_depth = data->thread_depth;
782 if (group->private_tile) {
783 if (group->private_tile->depth < group->min_depth)
784 group->min_depth = group->private_tile->depth;
786 if (group->shared_tile) {
787 if (tile_set_depth(data, group->shared_tile) < 0)
788 return -1;
789 if (group->shared_tile->depth < group->min_depth)
790 group->min_depth = group->shared_tile->depth;
793 return 0;
796 /* Fill up the groups array with singleton groups, i.e., one group
797 * per reference, initializing the array, access, write, n_ref and refs fields.
798 * In particular the access field is initialized to the scheduled
799 * access relation of the array reference.
801 * Return the number of elements initialized, i.e., the number of
802 * active references in the current kernel.
804 static int populate_array_references(struct gpu_local_array_info *local,
805 struct gpu_array_ref_group **groups, struct gpu_group_data *data)
807 int i;
808 int n;
809 isl_ctx *ctx = isl_union_map_get_ctx(data->shared_sched);
811 n = 0;
812 for (i = 0; i < local->array->n_ref; ++i) {
813 isl_union_map *umap;
814 isl_map *map;
815 struct gpu_array_ref_group *group;
816 struct gpu_stmt_access *access = local->array->refs[i];
818 map = isl_map_copy(access->access);
819 umap = isl_union_map_from_map(map);
820 umap = isl_union_map_apply_domain(umap,
821 isl_union_map_copy(data->shared_sched));
823 if (isl_union_map_is_empty(umap)) {
824 isl_union_map_free(umap);
825 continue;
828 map = isl_map_from_union_map(umap);
829 map = isl_map_detect_equalities(map);
831 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
832 if (!group)
833 return -1;
834 group->local_array = local;
835 group->array = local->array;
836 group->access = map;
837 group->write = access->write;
838 group->exact_write = access->exact_write;
839 group->slice = access->n_index < local->array->n_index;
840 group->refs = &local->array->refs[i];
841 group->n_ref = 1;
843 groups[n++] = group;
846 return n;
849 /* If group->n_ref == 1, then group->refs was set by
850 * populate_array_references to point directly into
851 * group->array->refs and should not be freed.
852 * If group->n_ref > 1, then group->refs was set by join_groups
853 * to point to a newly allocated array.
855 struct gpu_array_ref_group *gpu_array_ref_group_free(
856 struct gpu_array_ref_group *group)
858 if (!group)
859 return NULL;
860 gpu_array_tile_free(group->shared_tile);
861 gpu_array_tile_free(group->private_tile);
862 isl_map_free(group->access);
863 if (group->n_ref > 1)
864 free(group->refs);
865 free(group);
866 return NULL;
869 /* Check if the access relations of group1 and group2 overlap within
870 * shared_sched.
872 static int accesses_overlap(struct gpu_array_ref_group *group1,
873 struct gpu_array_ref_group *group2)
875 int disjoint;
877 disjoint = isl_map_is_disjoint(group1->access, group2->access);
878 if (disjoint < 0)
879 return -1;
881 return !disjoint;
884 /* Combine the given two groups into a single group, containing
885 * the references of both groups.
887 static struct gpu_array_ref_group *join_groups(
888 struct gpu_array_ref_group *group1,
889 struct gpu_array_ref_group *group2)
891 int i;
892 isl_ctx *ctx;
893 struct gpu_array_ref_group *group;
895 if (!group1 || !group2)
896 return NULL;
898 ctx = isl_map_get_ctx(group1->access);
899 group = isl_calloc_type(ctx, struct gpu_array_ref_group);
900 if (!group)
901 return NULL;
902 group->local_array = group1->local_array;
903 group->array = group1->array;
904 group->access = isl_map_union(isl_map_copy(group1->access),
905 isl_map_copy(group2->access));
906 group->write = group1->write || group2->write;
907 group->exact_write = group1->exact_write && group2->exact_write;
908 group->slice = group1->slice || group2->slice;
909 group->n_ref = group1->n_ref + group2->n_ref;
910 group->refs = isl_alloc_array(ctx, struct gpu_stmt_access *,
911 group->n_ref);
912 if (!group->refs)
913 return gpu_array_ref_group_free(group);
914 for (i = 0; i < group1->n_ref; ++i)
915 group->refs[i] = group1->refs[i];
916 for (i = 0; i < group2->n_ref; ++i)
917 group->refs[group1->n_ref + i] = group2->refs[i];
919 return group;
922 /* Combine the given two groups into a single group and free
923 * the original two groups.
925 static struct gpu_array_ref_group *join_groups_and_free(
926 struct gpu_array_ref_group *group1,
927 struct gpu_array_ref_group *group2)
929 struct gpu_array_ref_group *group;
931 group = join_groups(group1, group2);
932 gpu_array_ref_group_free(group1);
933 gpu_array_ref_group_free(group2);
934 return group;
937 /* Report that the array reference group with the given access relation
938 * is not mapped to shared memory in the given kernel because
939 * it does not exhibit any reuse and is considered to be coalesced.
941 static void report_no_reuse_and_coalesced(struct ppcg_kernel *kernel,
942 __isl_keep isl_union_map *access)
944 isl_ctx *ctx;
945 isl_printer *p;
947 ctx = isl_union_map_get_ctx(access);
948 p = isl_printer_to_file(ctx, stdout);
949 p = isl_printer_print_str(p, "Array reference group ");
950 p = isl_printer_print_union_map(p, access);
951 p = isl_printer_print_str(p,
952 " not considered for mapping to shared memory in kernel");
953 p = isl_printer_print_int(p, kernel->id);
954 p = isl_printer_print_str(p,
955 " because it exhibits no reuse and is considered to be coalesced");
956 p = isl_printer_end_line(p);
957 isl_printer_free(p);
960 /* Given an access relation in terms of the data->thread_depth initial
961 * dimensions of the computed schedule and the thread identifiers
962 * (as parameters), check if the use of the corresponding private tile
963 * requires unrolling.
965 * If we are creating a private tile because we are forced to,
966 * then no unrolling is required.
967 * Otherwise we check if "access" is bijective and unrolling
968 * is required if it is not. Note that the access relation
969 * has already been determined to be bijective before the introduction
970 * of the thread identifiers and the removal of the schedule dimensions
971 * that are mapped to these threads. If the access relation is no longer
972 * bijective, then this means that more than one value of one of those
973 * schedule dimensions is mapped to the same thread and therefore
974 * unrolling is required.
976 static int check_requires_unroll(struct gpu_group_data *data,
977 __isl_keep isl_map *access, int force_private)
979 int bijective;
981 if (force_private)
982 return 0;
983 bijective = access_is_bijective(data, access);
984 if (bijective < 0)
985 return -1;
986 return !bijective;
989 /* Compute the private and/or shared memory tiles for the array
990 * reference group "group" of array "array".
991 * Return 0 on success and -1 on error.
993 * If the array is a read-only scalar or if the user requested
994 * not to use shared or private memory, then we do not need to do anything.
996 * If any reference in the reference group accesses more than one element,
997 * then we would have to make sure that the layout in shared memory
998 * is the same as that in global memory. Since we do not handle this yet
999 * (and it may not even be possible), we refuse to map to private or
1000 * shared memory in such cases.
1002 * If the array group involves any may writes (that are not must writes),
1003 * then we would have to make sure that we load the data into shared/private
1004 * memory first in case the data is not written by the kernel
1005 * (but still written back out to global memory).
1006 * Since we don't have any such mechanism at the moment, we don't
1007 * compute shared/private tiles for groups involving may writes.
1009 * We only try to compute a shared memory tile if there is any reuse
1010 * or if the access is not coalesced.
1011 * Reuse and coalescing are checked within the given kernel.
1013 * For computing a private memory tile, we also require that there is
1014 * some reuse. Moreover, we require that the access is private
1015 * to the thread. That is, we check that any given array element
1016 * is only accessed by a single thread.
1017 * We compute an access relation that maps the outer
1018 * data->thread_depth + data->n_thread schedule dimensions.
1019 * The latter data->n_thread will be mapped to thread identifiers.
1020 * We actually check that those iterators that will be wrapped
1021 * partition the array space. This check is stricter than necessary
1022 * since several iterations may be mapped onto the same thread
1023 * and then they could be allowed to access the same memory elements,
1024 * but our check does not allow this situation.
1026 * For private memory tiles, the number of schedule dimensions that
1027 * affect the offset is computed and stored in tile->depth, with
1028 * a lower bound of data->kernel_depth. If this depth is smaller
1029 * than the minimal depth that still ensures that every element
1030 * is accessed by a single thread, then the depth is raised
1031 * to this minimal depth.
1032 * The fields of the tile are then adjusted to only refer to the tile->depth
1033 * outer schedule dimensions.
1035 * We also check that the index expression only depends on parallel
1036 * loops. That way, we can move those loops innermost and unroll them.
1037 * Again, we use a test that is stricter than necessary.
1038 * We actually check whether the index expression only depends
1039 * on the iterators that are wrapped over the threads.
1040 * These are necessarily parallel, but there may be more parallel loops.
1042 * Combining the injectivity of the first test with the single-valuedness
1043 * of the second test, we simply test for bijectivity.
1045 * If the use of the private tile requires unrolling, but some
1046 * of the other arrays are forcibly mapped to private memory,
1047 * then we do not allow the use of this private tile since
1048 * we cannot move the schedule dimensions that need to be unrolled down
1049 * without performing some kind of expansion on those arrays
1050 * that are forcibly mapped to private memory.
1052 * If the array is marked force_private, then we bypass all checks
1053 * and assume we can (and should) use registers.
1055 * If it turns out we can (or have to) use registers, we compute
1056 * the private memory tile size using can_tile, after introducing a dependence
1057 * on the thread indices.
1059 static int compute_group_bounds_core(struct ppcg_kernel *kernel,
1060 struct gpu_array_ref_group *group, struct gpu_group_data *data)
1062 isl_ctx *ctx = isl_space_get_ctx(group->array->space);
1063 isl_union_map *access, *local;
1064 int n_index = group->array->n_index;
1065 int no_reuse, coalesced;
1066 isl_map *acc;
1067 int force_private = group->local_array->force_private;
1068 int use_shared = kernel->options->use_shared_memory &&
1069 data->n_thread > 0;
1070 int use_private = force_private || kernel->options->use_private_memory;
1071 int r = 0;
1072 int requires_unroll;
1073 int unique_depth;
1075 if (!use_shared && !use_private)
1076 return 0;
1077 if (gpu_array_is_read_only_scalar(group->array))
1078 return 0;
1079 if (!force_private && !group->exact_write)
1080 return 0;
1081 if (group->slice)
1082 return 0;
1084 access = gpu_array_ref_group_access_relation(group, 1, 1);
1085 local = localize_access(data, isl_union_map_copy(access));
1086 no_reuse = isl_union_map_is_injective(local);
1087 if (no_reuse < 0)
1088 r = -1;
1089 if (use_shared && no_reuse)
1090 coalesced = access_is_coalesced(data, local);
1091 isl_union_map_free(local);
1093 if (r >= 0 && kernel->options->debug->verbose &&
1094 use_shared && no_reuse && coalesced)
1095 report_no_reuse_and_coalesced(kernel, access);
1097 if (use_shared && (!no_reuse || !coalesced)) {
1098 group->shared_tile = gpu_array_tile_create(ctx,
1099 group->array->n_index);
1100 if (!group->shared_tile)
1101 r = -1;
1102 else if (!can_tile(group->access, group->shared_tile))
1103 group->shared_tile =
1104 gpu_array_tile_free(group->shared_tile);
1107 if (r < 0 || (!force_private && (!use_private || no_reuse))) {
1108 isl_union_map_free(access);
1109 return r;
1112 access = isl_union_map_apply_domain(access,
1113 isl_union_map_copy(data->thread_sched));
1115 acc = isl_map_from_union_map(access);
1117 if (!force_private && !access_is_bijective(data, acc)) {
1118 isl_map_free(acc);
1119 return 0;
1122 unique_depth = compute_accessed_by_single_thread_depth(data, acc);
1124 acc = isl_map_intersect_domain(acc, isl_set_copy(data->privatization));
1125 acc = isl_map_project_out(acc, isl_dim_in, data->thread_depth,
1126 data->n_thread);
1127 requires_unroll = check_requires_unroll(data, acc, force_private);
1128 if (unique_depth < 0 || requires_unroll < 0 ||
1129 (requires_unroll && kernel->any_force_private)) {
1130 isl_map_free(acc);
1131 return requires_unroll < 0 ? -1 : 0;
1134 group->private_tile = gpu_array_tile_create(ctx, n_index);
1135 if (!group->private_tile) {
1136 isl_map_free(acc);
1137 return -1;
1139 group->private_tile->requires_unroll = requires_unroll;
1140 if (!can_tile(acc, group->private_tile))
1141 group->private_tile = gpu_array_tile_free(group->private_tile);
1143 isl_map_free(acc);
1145 if (group->private_tile) {
1146 struct gpu_array_tile *tile = group->private_tile;
1147 int tile_depth = compute_tile_depth(data, tile);
1148 if (tile_depth < unique_depth)
1149 tile_depth = unique_depth;
1150 if (tile_adjust_depth(tile, tile_depth) < 0)
1151 return -1;
1154 if (force_private && !group->private_tile)
1155 isl_die(ctx, isl_error_internal,
1156 "unable to map array reference group to registers",
1157 return -1);
1159 return 0;
1162 /* Compute the private and/or shared memory tiles for the array
1163 * reference group "group" of array "array" and set the tile depth.
1164 * Return 0 on success and -1 on error.
1166 static int compute_group_bounds(struct ppcg_kernel *kernel,
1167 struct gpu_array_ref_group *group, struct gpu_group_data *data)
1169 if (!group)
1170 return -1;
1171 if (compute_group_bounds_core(kernel, group, data) < 0)
1172 return -1;
1173 if (set_depth(data, group) < 0)
1174 return -1;
1176 return 0;
1179 /* If two groups have overlapping access relations (as determined by
1180 * the "overlap" function) and if one of them involves a write,
1181 * then merge the two groups into one.
1182 * If "compute_bounds" is set, then call compute_group_bounds
1183 * on the merged groups.
1185 * Return the updated number of groups.
1186 * Return -1 on error.
1188 static int group_writes(struct ppcg_kernel *kernel,
1189 int n, struct gpu_array_ref_group **groups,
1190 int (*overlap)(struct gpu_array_ref_group *group1,
1191 struct gpu_array_ref_group *group2), int compute_bounds,
1192 struct gpu_group_data *data)
1194 int i, j;
1196 for (i = 0; i < n; ++i) {
1197 for (j = n - 1; j > i; --j) {
1198 if (!groups[i]->write && !groups[j]->write)
1199 continue;
1201 if (!overlap(groups[i], groups[j]))
1202 continue;
1204 groups[i] = join_groups_and_free(groups[i], groups[j]);
1205 if (j != n - 1)
1206 groups[j] = groups[n - 1];
1207 groups[n - 1] = NULL;
1208 n--;
1210 if (!groups[i])
1211 return -1;
1212 if (compute_bounds &&
1213 compute_group_bounds(kernel, groups[i], data) < 0)
1214 return -1;
1218 return n;
1221 /* If two groups have overlapping access relations (within the innermost
1222 * loop) and if one of them involves a write, then merge the two groups
1223 * into one.
1225 * Return the updated number of groups.
1227 static int group_overlapping_writes(struct ppcg_kernel *kernel,
1228 int n, struct gpu_array_ref_group **groups,
1229 struct gpu_group_data *data)
1231 return group_writes(kernel, n, groups, &accesses_overlap, 0, data);
1234 /* Check if the access relations of group1 and group2 overlap within
1235 * the outermost min(group1->min_depth, group2->min_depth) loops.
1237 static int depth_accesses_overlap(struct gpu_array_ref_group *group1,
1238 struct gpu_array_ref_group *group2)
1240 int depth;
1241 int dim;
1242 int empty;
1243 isl_map *map_i, *map_j, *map;
1245 depth = group1->min_depth;
1246 if (group2->min_depth < depth)
1247 depth = group2->min_depth;
1248 map_i = isl_map_copy(group1->access);
1249 dim = isl_map_dim(map_i, isl_dim_in);
1250 map_i = isl_map_eliminate(map_i, isl_dim_in, depth, dim - depth);
1251 map_j = isl_map_copy(group2->access);
1252 map_j = isl_map_eliminate(map_j, isl_dim_in, depth, dim - depth);
1253 map = isl_map_intersect(map_i, map_j);
1254 empty = isl_map_is_empty(map);
1255 isl_map_free(map);
1257 return !empty;
1260 /* If two groups have overlapping access relations (within the outer
1261 * depth loops) and if one of them involves a write,
1262 * then merge the two groups into one.
1264 * Return the updated number of groups.
1266 static int group_depth_overlapping_writes(struct ppcg_kernel *kernel,
1267 int n, struct gpu_array_ref_group **groups, struct gpu_group_data *data)
1269 return group_writes(kernel, n, groups, &depth_accesses_overlap, 1,
1270 data);
1273 /* Is the size of the tile specified by "tile" smaller than the sum of
1274 * the sizes of the tiles specified by "tile1" and "tile2"?
1276 static int smaller_tile(struct gpu_array_tile *tile,
1277 struct gpu_array_tile *tile1, struct gpu_array_tile *tile2)
1279 int smaller;
1280 isl_val *size, *size1, *size2;
1282 size = gpu_array_tile_size(tile);
1283 size1 = gpu_array_tile_size(tile1);
1284 size2 = gpu_array_tile_size(tile2);
1286 size = isl_val_sub(size, size1);
1287 size = isl_val_sub(size, size2);
1288 smaller = isl_val_is_neg(size);
1290 isl_val_free(size);
1292 return smaller;
1295 /* Given an initial grouping of array references and shared memory tiles
1296 * for each group that allows for a shared memory tile, merge two groups
1297 * if both have a shared memory tile, the merged group also has
1298 * a shared memory tile and the size of the tile for the merge group
1299 * is smaller than the sum of the tile sizes of the individual groups.
1301 * If merging two groups decreases the depth of the tile of
1302 * one or both of the two groups, then we need to check for overlapping
1303 * writes again.
1305 * Return the number of groups after merging.
1306 * Return -1 on error.
1308 static int group_common_shared_memory_tile(struct ppcg_kernel *kernel,
1309 struct gpu_array_info *array, int n,
1310 struct gpu_array_ref_group **groups, struct gpu_group_data *data)
1312 int i, j;
1313 int recompute_overlap = 0;
1314 isl_ctx *ctx = isl_space_get_ctx(array->space);
1316 for (i = 0; i < n; ++i) {
1317 if (!groups[i]->shared_tile)
1318 continue;
1319 for (j = n - 1; j > i; --j) {
1320 struct gpu_array_ref_group *group;
1322 if (!groups[j]->shared_tile)
1323 continue;
1325 if (!depth_accesses_overlap(groups[i], groups[j]))
1326 continue;
1328 group = join_groups(groups[i], groups[j]);
1329 if (compute_group_bounds(kernel, group, data) < 0) {
1330 gpu_array_ref_group_free(group);
1331 return -1;
1333 if (!group->shared_tile ||
1334 !smaller_tile(group->shared_tile,
1335 groups[i]->shared_tile,
1336 groups[j]->shared_tile)) {
1337 gpu_array_ref_group_free(group);
1338 continue;
1341 if (group->min_depth < groups[i]->min_depth ||
1342 group->min_depth < groups[j]->min_depth)
1343 recompute_overlap = 1;
1344 gpu_array_ref_group_free(groups[i]);
1345 gpu_array_ref_group_free(groups[j]);
1346 groups[i] = group;
1347 if (j != n - 1)
1348 groups[j] = groups[n - 1];
1349 n--;
1353 if (recompute_overlap)
1354 n = group_depth_overlapping_writes(kernel, n, groups, data);
1355 return n;
1358 /* Set array->n_group and array->groups to n and groups.
1360 * Additionally, set the "nr" field of each group.
1362 static void set_array_groups(struct gpu_local_array_info *array,
1363 int n, struct gpu_array_ref_group **groups)
1365 int i, j;
1367 array->n_group = n;
1368 array->groups = groups;
1370 for (i = 0; i < n; ++i)
1371 groups[i]->nr = i;
1374 /* Combine all groups in "groups" into a single group and return
1375 * the new number of groups (1 or 0 if there were no groups to start with).
1377 static int join_all_groups(int n, struct gpu_array_ref_group **groups)
1379 int i;
1381 for (i = n - 1; i > 0; --i) {
1382 groups[0] = join_groups_and_free(groups[0], groups[i]);
1383 groups[i] = NULL;
1384 n--;
1387 return n;
1390 /* Group array references that should be considered together when
1391 * deciding whether to access them from private, shared or global memory.
1392 * Return -1 on error.
1394 * In particular, if two array references overlap and if one of them
1395 * is a write, then the two references are grouped together.
1396 * We first perform an initial grouping based only on the access relation.
1397 * After computing shared and private memory tiles, we check for
1398 * overlapping writes again, but this time taking into account
1399 * the depth of the effective tile.
1401 * Furthermore, if two groups admit a shared memory tile and if the
1402 * combination of the two also admits a shared memory tile, we merge
1403 * the two groups.
1405 * If the array contains structures, then we compute a single
1406 * reference group without trying to find any tiles
1407 * since we do not map such arrays to private or shared
1408 * memory.
1410 static int group_array_references(struct ppcg_kernel *kernel,
1411 struct gpu_local_array_info *local, struct gpu_group_data *data)
1413 int i;
1414 int n;
1415 isl_ctx *ctx = isl_union_map_get_ctx(data->shared_sched);
1416 struct gpu_array_ref_group **groups;
1418 groups = isl_calloc_array(ctx, struct gpu_array_ref_group *,
1419 local->array->n_ref);
1420 if (!groups)
1421 return -1;
1423 n = populate_array_references(local, groups, data);
1425 if (local->array->has_compound_element) {
1426 n = join_all_groups(n, groups);
1427 set_array_groups(local, n, groups);
1428 return 0;
1431 n = group_overlapping_writes(kernel, n, groups, data);
1433 for (i = 0; i < n; ++i)
1434 if (compute_group_bounds(kernel, groups[i], data) < 0)
1435 n = -1;
1437 n = group_depth_overlapping_writes(kernel, n, groups, data);
1439 n = group_common_shared_memory_tile(kernel, local->array,
1440 n, groups, data);
1442 set_array_groups(local, n, groups);
1444 if (n >= 0)
1445 return 0;
1447 for (i = 0; i < local->array->n_ref; ++i)
1448 gpu_array_ref_group_free(groups[i]);
1449 return -1;
1452 /* For each scalar in the input program, check if there are any
1453 * order dependences active inside the current kernel, within
1454 * the same iteration of "host_schedule".
1455 * If so, mark the scalar as force_private so that it will be
1456 * mapped to a register.
1458 static void check_scalar_live_ranges_in_host(struct ppcg_kernel *kernel,
1459 __isl_take isl_union_map *host_schedule)
1461 int i;
1462 isl_union_map *sched;
1463 isl_union_set *domain;
1464 isl_union_map *same_host_iteration;
1466 kernel->any_force_private = 0;
1468 sched = isl_union_map_universe(isl_union_map_copy(host_schedule));
1469 domain = isl_union_map_domain(sched);
1471 same_host_iteration = isl_union_map_apply_range(host_schedule,
1472 isl_union_map_reverse(isl_union_map_copy(host_schedule)));
1474 for (i = 0; i < kernel->n_array; ++i) {
1475 struct gpu_local_array_info *local = &kernel->array[i];
1476 isl_union_map *order;
1478 local->force_private = 0;
1479 if (local->array->n_index != 0)
1480 continue;
1481 order = isl_union_map_copy(local->array->dep_order);
1482 order = isl_union_map_intersect_domain(order,
1483 isl_union_set_copy(domain));
1484 order = isl_union_map_intersect_range(order,
1485 isl_union_set_copy(domain));
1486 order = isl_union_map_intersect(order,
1487 isl_union_map_copy(same_host_iteration));
1488 if (!isl_union_map_is_empty(order)) {
1489 local->force_private = 1;
1490 kernel->any_force_private = 1;
1492 isl_union_map_free(order);
1495 isl_union_map_free(same_host_iteration);
1496 isl_union_set_free(domain);
1499 /* For each scalar in the input program, check if there are any
1500 * order dependences active inside the current kernel, within
1501 * the same iteration of the host schedule, i.e., the prefix
1502 * schedule at "node".
1503 * If so, mark the scalar as force_private so that it will be
1504 * mapped to a register.
1506 static void check_scalar_live_ranges(struct ppcg_kernel *kernel,
1507 __isl_keep isl_schedule_node *node)
1509 isl_union_map *sched;
1511 if (!kernel->options->live_range_reordering)
1512 return;
1514 sched = isl_schedule_node_get_prefix_schedule_union_map(node);
1516 check_scalar_live_ranges_in_host(kernel, sched);
1519 /* Create a set of dimension data->thread_depth + data->n_thread
1520 * that equates the residue of the final data->n_thread dimensions
1521 * modulo the "sizes" to the thread identifiers.
1522 * "space" is a parameter space containing the thread identifiers.
1523 * Store the computed set in data->privatization.
1525 static void compute_privatization(struct gpu_group_data *data,
1526 __isl_take isl_space *space, int *sizes)
1528 int i;
1529 isl_ctx *ctx;
1530 isl_local_space *ls;
1531 isl_set *set;
1533 ctx = isl_union_map_get_ctx(data->shared_sched);
1534 space = isl_space_set_from_params(space);
1535 space = isl_space_add_dims(space, isl_dim_set,
1536 data->thread_depth + data->n_thread);
1537 set = isl_set_universe(space);
1538 space = isl_set_get_space(set);
1539 ls = isl_local_space_from_space(space);
1541 for (i = 0; i < data->n_thread; ++i) {
1542 isl_aff *aff, *aff2;
1543 isl_constraint *c;
1544 isl_val *v;
1545 char name[20];
1546 int pos;
1548 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1549 isl_dim_set, data->thread_depth + i);
1550 v = isl_val_int_from_si(ctx, sizes[i]);
1551 aff = isl_aff_mod_val(aff, v);
1552 snprintf(name, sizeof(name), "t%d", i);
1553 pos = isl_set_find_dim_by_name(set, isl_dim_param, name);
1554 aff2 = isl_aff_var_on_domain(isl_local_space_copy(ls),
1555 isl_dim_param, pos);
1556 aff = isl_aff_sub(aff, aff2);
1557 c = isl_equality_from_aff(aff);
1558 set = isl_set_add_constraint(set, c);
1561 isl_local_space_free(ls);
1562 data->privatization = set;
1565 /* Group references of all arrays in "kernel".
1566 * "node" points to the kernel mark.
1568 * We first extract all required schedule information into
1569 * a gpu_group_data structure and then consider each array
1570 * in turn.
1572 int gpu_group_references(struct ppcg_kernel *kernel,
1573 __isl_keep isl_schedule_node *node)
1575 int i;
1576 int r = 0;
1577 isl_space *space;
1578 struct gpu_group_data data;
1580 check_scalar_live_ranges(kernel, node);
1582 data.scop = kernel->prog->scop;
1584 data.kernel_depth = isl_schedule_node_get_schedule_depth(node);
1585 data.host_sched = isl_schedule_node_get_prefix_schedule_relation(node);
1587 node = isl_schedule_node_copy(node);
1588 node = gpu_tree_move_down_to_thread(node, kernel->core);
1589 data.shared_sched =
1590 isl_schedule_node_get_prefix_schedule_relation(node);
1591 data.shared_sched = isl_union_map_detect_equalities(data.shared_sched);
1593 node = isl_schedule_node_child(node, 0);
1594 data.thread_depth = isl_schedule_node_get_schedule_depth(node);
1595 data.n_thread = isl_schedule_node_band_n_member(node);
1596 data.thread_sched = isl_union_map_copy(data.shared_sched);
1597 data.thread_sched = isl_union_map_flat_range_product(data.thread_sched,
1598 isl_schedule_node_band_get_partial_schedule_union_map(node));
1599 data.thread_sched = isl_union_map_detect_equalities(data.thread_sched);
1600 node = isl_schedule_node_child(node, 0);
1601 data.full_sched = isl_union_map_copy(data.thread_sched);
1602 data.full_sched = isl_union_map_flat_range_product(data.full_sched,
1603 isl_schedule_node_get_subtree_schedule_union_map(node));
1604 isl_schedule_node_free(node);
1606 space = isl_union_set_get_space(kernel->thread_filter);
1607 compute_privatization(&data, space, kernel->block_dim);
1609 for (i = 0; i < kernel->n_array; ++i) {
1610 r = group_array_references(kernel, &kernel->array[i], &data);
1611 if (r < 0)
1612 break;
1615 isl_union_map_free(data.host_sched);
1616 isl_union_map_free(data.shared_sched);
1617 isl_union_map_free(data.thread_sched);
1618 isl_union_map_free(data.full_sched);
1619 isl_set_free(data.privatization);
1621 return r;
1624 /* Given a description of an array tile "tile" and the "space"
1626 * { D -> A }
1628 * where D represents the first tile->depth schedule dimensions
1629 * and A represents the array, construct an isl_multi_aff
1631 * { [D[i] -> A[a]] -> A'[a'] }
1633 * with A' a scaled down copy of A according to the shifts and strides
1634 * in "tile". In particular,
1636 * a' = (a + shift(i))/stride
1638 * "insert_array" represents
1640 * { [D -> A] -> D }
1642 * and is used to insert A into the domain of functions that only
1643 * reference D.
1645 static __isl_give isl_multi_aff *strided_tile(
1646 struct gpu_array_tile *tile, __isl_keep isl_space *space,
1647 __isl_keep isl_multi_aff *insert_array)
1649 int i;
1650 isl_ctx *ctx;
1651 isl_multi_aff *shift;
1652 isl_multi_val *stride;
1653 isl_space *space2;
1654 isl_local_space *ls;
1655 isl_multi_aff *tiling;
1657 ctx = isl_space_get_ctx(space);
1658 space2 = isl_space_domain(isl_space_copy(space));
1659 ls = isl_local_space_from_space(space2);
1660 space2 = isl_space_range(isl_space_copy(space));
1661 stride = isl_multi_val_zero(space2);
1662 shift = isl_multi_aff_zero(isl_space_copy(space));
1664 for (i = 0; i < tile->n; ++i) {
1665 struct gpu_array_bound *bound = &tile->bound[i];
1666 isl_val *stride_i;
1667 isl_aff *shift_i;
1669 if (tile->bound[i].shift) {
1670 stride_i = isl_val_copy(bound->stride);
1671 shift_i = isl_aff_copy(bound->shift);
1672 } else {
1673 stride_i = isl_val_one(ctx);
1674 shift_i = isl_aff_zero_on_domain(
1675 isl_local_space_copy(ls));
1678 stride = isl_multi_val_set_val(stride, i, stride_i);
1679 shift = isl_multi_aff_set_aff(shift, i, shift_i);
1681 isl_local_space_free(ls);
1683 shift = isl_multi_aff_pullback_multi_aff(shift,
1684 isl_multi_aff_copy(insert_array));
1686 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1687 tiling = isl_multi_aff_add(tiling, shift);
1688 tiling = isl_multi_aff_scale_down_multi_val(tiling, stride);
1690 return tiling;
1693 /* Compute a tiling for the array reference group "group".
1695 * The tiling is of the form
1697 * { [D[i] -> A[a]] -> T[t] }
1699 * where D represents the first tile->depth schedule dimensions,
1700 * A represents the global array and T represents the shared or
1701 * private memory tile. The name of T is the name of the local
1702 * array.
1704 * If there is any stride in the accesses, then the mapping is
1706 * t = (a + shift(i))/stride - lb(i)
1708 * otherwise, it is simply
1710 * t = a - lb(i)
1712 void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group *group)
1714 int i;
1715 struct gpu_array_tile *tile;
1716 struct gpu_array_info *array = group->array;
1717 isl_space *space;
1718 isl_multi_aff *tiling, *lb, *insert_array;
1719 isl_printer *p;
1720 char *local_name;
1722 tile = gpu_array_ref_group_tile(group);
1723 if (!tile)
1724 return;
1726 space = isl_map_get_space(group->access);
1727 space = isl_space_from_range(isl_space_range(space));
1728 space = isl_space_add_dims(space, isl_dim_in, tile->depth);
1729 insert_array = isl_multi_aff_domain_map(isl_space_copy(space));
1731 for (i = 0; i < tile->n; ++i)
1732 if (tile->bound[i].shift)
1733 break;
1735 if (i < tile->n)
1736 tiling = strided_tile(tile, space, insert_array);
1737 else
1738 tiling = isl_multi_aff_range_map(isl_space_copy(space));
1740 lb = isl_multi_aff_zero(space);
1741 for (i = 0; i < tile->n; ++i) {
1742 isl_aff *lb_i = isl_aff_copy(tile->bound[i].lb);
1743 lb = isl_multi_aff_set_aff(lb, i, lb_i);
1745 lb = isl_multi_aff_pullback_multi_aff(lb, insert_array);
1747 tiling = isl_multi_aff_sub(tiling, lb);
1749 p = isl_printer_to_str(isl_multi_aff_get_ctx(tiling));
1750 p = gpu_array_ref_group_print_name(group, p);
1751 local_name = isl_printer_get_str(p);
1752 isl_printer_free(p);
1753 tiling = isl_multi_aff_set_tuple_name(tiling, isl_dim_out, local_name);
1754 free(local_name);
1756 tile->tiling = tiling;