3 #include "gpu_array_tile.h"
8 /* Print the name of the local copy of a given group of array references.
10 __isl_give isl_printer
*gpu_array_ref_group_print_name(
11 struct gpu_array_ref_group
*group
, __isl_take isl_printer
*p
)
15 if (group
->private_tile
)
16 p
= isl_printer_print_str(p
, "private_");
17 else if (group
->shared_tile
)
18 p
= isl_printer_print_str(p
, "shared_");
21 p
= isl_printer_print_str(p
, group
->array
->name
);
22 if (!global
&& group
->local_array
->n_group
> 1) {
23 p
= isl_printer_print_str(p
, "_");
24 p
= isl_printer_print_int(p
, group
->nr
);
30 /* Return the union of all read (read = 1) and/or write (write = 1)
31 * access relations in the group.
33 __isl_give isl_union_map
*gpu_array_ref_group_access_relation(
34 struct gpu_array_ref_group
*group
, int read
, int write
)
37 isl_union_map
*access
;
39 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
40 for (i
= 0; i
< group
->n_ref
; ++i
) {
43 if (!((read
&& group
->refs
[i
]->read
) ||
44 (write
&& group
->refs
[i
]->write
)))
46 map_i
= isl_map_copy(group
->refs
[i
]->access
);
47 access
= isl_union_map_union(access
,
48 isl_union_map_from_map(map_i
));
54 /* Return the effective gpu_array_tile associated to "group" or
55 * NULL if there is no such gpu_array_tile.
56 * If we have computed both a private and a shared tile, then
57 * the private tile is used.
59 struct gpu_array_tile
*gpu_array_ref_group_tile(
60 struct gpu_array_ref_group
*group
)
62 if (group
->private_tile
)
63 return group
->private_tile
;
64 if (group
->shared_tile
)
65 return group
->shared_tile
;
69 /* Does the tile associated to "group" require unrolling of the schedule
70 * dimensions mapped to threads?
71 * Note that this can only happen for private tiles.
73 int gpu_array_ref_group_requires_unroll(struct gpu_array_ref_group
*group
)
75 struct gpu_array_tile
*tile
;
77 tile
= gpu_array_ref_group_tile(group
);
80 return tile
->requires_unroll
;
87 * or -a(p,i) - j = g f(e) if sign < 0,
88 * store a(p,i) in bound->shift and g (stride) in bound->stride.
89 * a(p,i) is assumed to be an expression in only the parameters
90 * and the input dimensions.
92 static void extract_stride(__isl_keep isl_constraint
*c
,
93 struct gpu_array_bound
*bound
, __isl_keep isl_val
*stride
, int sign
)
102 isl_val_free(bound
->stride
);
103 bound
->stride
= isl_val_copy(stride
);
105 space
= isl_constraint_get_space(c
);
106 space
= isl_space_domain(space
);
108 nparam
= isl_space_dim(space
, isl_dim_param
);
109 nvar
= isl_space_dim(space
, isl_dim_set
);
111 v
= isl_constraint_get_constant_val(c
);
114 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
115 aff
= isl_aff_set_constant_val(aff
, v
);
117 for (i
= 0; i
< nparam
; ++i
) {
118 if (!isl_constraint_involves_dims(c
, isl_dim_param
, i
, 1))
120 v
= isl_constraint_get_coefficient_val(c
, isl_dim_param
, i
);
123 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_param
, i
, v
);
126 for (i
= 0; i
< nvar
; ++i
) {
127 if (!isl_constraint_involves_dims(c
, isl_dim_in
, i
, 1))
129 v
= isl_constraint_get_coefficient_val(c
, isl_dim_in
, i
);
132 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, i
, v
);
138 /* Given an equality constraint of a map with a single output dimension j,
139 * check if the constraint is of the form
141 * a(p,i) + j = g f(e)
143 * with a(p,i) an expression in the parameters and input dimensions
144 * and f(e) an expression in the existentially quantified variables.
145 * If so, and if g is larger than any such g from a previously considered
146 * constraint, then call extract_stride to record the stride information
149 static int check_stride_constraint(__isl_take isl_constraint
*c
, void *user
)
155 struct gpu_array_bound
*bound
= user
;
157 ctx
= isl_constraint_get_ctx(c
);
158 n_div
= isl_constraint_dim(c
, isl_dim_div
);
159 v
= isl_constraint_get_coefficient_val(c
, isl_dim_out
, 0);
161 if (n_div
&& (isl_val_is_one(v
) || isl_val_is_negone(v
))) {
162 int s
= isl_val_sgn(v
);
163 isl_val
*stride
= isl_val_zero(ctx
);
166 for (i
= 0; i
< n_div
; ++i
) {
167 v
= isl_constraint_get_coefficient_val(c
,
169 stride
= isl_val_gcd(stride
, v
);
171 if (!isl_val_is_zero(stride
) &&
172 isl_val_gt(stride
, bound
->stride
))
173 extract_stride(c
, bound
, stride
, s
);
175 isl_val_free(stride
);
179 isl_constraint_free(c
);
183 /* Given contraints on an array index i, check if we can find
184 * a shift a(p) and a stride g such that
188 * If so, record the information in bound and apply the mapping
189 * i -> (i + a(p))/g to the array index in bounds and return
190 * the new constraints.
191 * If not, simply return the original constraints.
193 * If bounds is a subset of the space
197 * then the bound recorded in bound->shift is of the form
201 * with s(D) equal to a(p) above.
202 * Next, we construct a mapping of the form
204 * [D -> i] -> [D -> (i + S(D))/g]
206 * This mapping is computed as follows.
207 * We first introduce "i" in the domain through precomposition
208 * with [D -> i] -> D obtaining
212 * Adding [D -> i] -> i produces
214 * [D -> i] -> i + s(D)
216 * and the domain product with [D -> i] -> D yields
218 * [D -> i] -> [D -> i + s(D)]
220 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
222 static __isl_give isl_basic_map
*check_stride(struct gpu_array_bound
*bound
,
223 __isl_take isl_basic_map
*bounds
)
227 isl_basic_map
*shift
, *id
, *bmap
, *scale
;
231 bound
->stride
= NULL
;
233 hull
= isl_basic_map_affine_hull(isl_basic_map_copy(bounds
));
235 isl_basic_map_foreach_constraint(hull
, &check_stride_constraint
, bound
);
237 isl_basic_map_free(hull
);
242 shift
= isl_basic_map_from_aff(isl_aff_copy(bound
->shift
));
243 space
= isl_basic_map_get_space(bounds
);
244 bmap
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
245 shift
= isl_basic_map_apply_range(bmap
, shift
);
246 space
= isl_basic_map_get_space(bounds
);
247 id
= isl_basic_map_range_map(isl_basic_map_universe(space
));
248 shift
= isl_basic_map_sum(id
, shift
);
249 space
= isl_basic_map_get_space(bounds
);
250 id
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
251 shift
= isl_basic_map_range_product(id
, shift
);
253 space
= isl_space_domain(isl_basic_map_get_space(bounds
));
254 id
= isl_basic_map_identity(isl_space_map_from_set(space
));
255 space
= isl_space_range(isl_basic_map_get_space(bounds
));
256 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
257 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
258 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(bound
->stride
));
259 scale
= isl_basic_map_from_aff(aff
);
260 scale
= isl_basic_map_product(id
, scale
);
262 bmap
= isl_basic_map_apply_range(shift
, scale
);
263 bset
= isl_basic_set_apply(isl_basic_map_wrap(bounds
), bmap
);
264 bounds
= isl_basic_set_unwrap(bset
);
269 /* Data used in compute_array_dim_size and compute_size_in_direction.
271 * pos is the position of the variable representing the array index,
272 * i.e., the variable for which want to compute the size. This variable
273 * is also the last variable in the set.
275 struct gpu_size_info
{
277 struct gpu_array_bound
*bound
;
281 /* Given a constraint from the basic set describing the bounds on
282 * an array index, check if it is a lower bound, say m i >= b(x), and,
283 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
284 * upper bound. If so, and if this bound is smaller than any bound
285 * derived from earlier constraints, set the size to this bound on
286 * the expression and the lower bound to ceil(b(x)/m).
288 static int compute_size_in_direction(__isl_take isl_constraint
*c
, void *user
)
290 struct gpu_size_info
*size
= user
;
297 nparam
= isl_basic_set_dim(size
->bset
, isl_dim_param
);
298 n_div
= isl_constraint_dim(c
, isl_dim_div
);
300 if (isl_constraint_involves_dims(c
, isl_dim_div
, 0, n_div
) ||
301 !isl_constraint_is_lower_bound(c
, isl_dim_set
, size
->pos
)) {
302 isl_constraint_free(c
);
306 aff
= isl_constraint_get_bound(c
, isl_dim_set
, size
->pos
);
307 aff
= isl_aff_ceil(aff
);
309 lb
= isl_aff_copy(aff
);
311 aff
= isl_aff_neg(aff
);
312 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, size
->pos
, 1);
314 v
= isl_basic_set_max_val(size
->bset
, aff
);
317 if (isl_val_is_int(v
)) {
318 v
= isl_val_add_ui(v
, 1);
319 if (!size
->bound
->size
|| isl_val_lt(v
, size
->bound
->size
)) {
320 isl_val_free(size
->bound
->size
);
321 size
->bound
->size
= isl_val_copy(v
);
322 lb
= isl_aff_drop_dims(lb
, isl_dim_in
, size
->pos
, 1);
323 isl_aff_free(size
->bound
->lb
);
324 size
->bound
->lb
= isl_aff_copy(lb
);
330 isl_constraint_free(c
);
335 /* Given a basic map "bounds" that maps parameters and input dimensions
336 * to a single output dimension, look for an expression in the parameters
337 * and input dimensions such that the range of the output dimension shifted
338 * by this expression is a constant.
340 * In particular, we currently only consider lower bounds on the output
341 * dimension as candidate expressions.
343 static int compute_array_dim_size(struct gpu_array_bound
*bound
,
344 __isl_take isl_basic_map
*bounds
)
346 struct gpu_size_info size
;
348 bounds
= isl_basic_map_detect_equalities(bounds
);
349 bounds
= check_stride(bound
, bounds
);
355 size
.pos
= isl_basic_map_dim(bounds
, isl_dim_in
);
356 size
.bset
= isl_basic_map_wrap(bounds
);
357 size
.bset
= isl_basic_set_flatten(size
.bset
);
358 size
.bset
= isl_set_simple_hull(isl_basic_set_compute_divs(size
.bset
));
359 isl_basic_set_foreach_constraint(size
.bset
, &compute_size_in_direction
,
361 isl_basic_set_free(size
.bset
);
363 return bound
->size
? 0 : -1;
366 /* Check if we can find a memory tile for the given array
367 * based on the given accesses, and if so, put the results in "tile".
369 * We project the accesses on each index in turn and look for a parametric
370 * offset such that the size is constant.
372 static int can_tile(__isl_keep isl_map
*access
, struct gpu_array_tile
*tile
)
376 for (i
= 0; i
< tile
->n
; ++i
) {
380 access_i
= isl_map_copy(access
);
381 access_i
= isl_map_project_out(access_i
, isl_dim_out
, 0, i
);
382 access_i
= isl_map_project_out(access_i
, isl_dim_out
,
383 1, tile
->n
- (i
+ 1));
384 access_i
= isl_map_compute_divs(access_i
);
385 hull
= isl_map_simple_hull(access_i
);
386 if (compute_array_dim_size(&tile
->bound
[i
], hull
) < 0)
393 /* Internal data structure for gpu_group_references.
395 * scop represents the input scop.
396 * kernel_depth is the schedule depth where the kernel launch will
397 * be introduced, i.e., it is the depth of the band that is mapped
399 * thread_depth is the schedule depth where the thread mark is located,
400 * i.e., it is the depth of the band that is mapped to threads and also
401 * the schedule depth at which the copying to/from shared/private memory
402 * is computed. The copy operation may then later be hoisted to
404 * n_thread is the number of schedule dimensions in the band that
405 * is mapped to threads.
406 * privatization lives in the range of thread_sched (i.e., it is
407 * of dimension thread_depth + n_thread) and encodes the mapping
408 * to thread identifiers (as parameters).
409 * host_sched contains the kernel_depth dimensions of the host schedule.
410 * shared_sched contains the first thread_depth dimensions of the
412 * thread_sched contains the first (thread_depth + n_thread) dimensions
413 * of the kernel schedule.
414 * full_sched is a union_map representation of the entire kernel schedule.
416 struct gpu_group_data
{
417 struct ppcg_scop
*scop
;
421 isl_set
*privatization
;
422 isl_union_map
*host_sched
;
423 isl_union_map
*shared_sched
;
424 isl_union_map
*thread_sched
;
425 isl_union_map
*full_sched
;
428 /* Construct a map from domain_dim to domain_dim that increments
429 * the dimension at position "pos" and leaves all other dimensions
432 static __isl_give isl_map
*next(__isl_take isl_space
*domain_dim
, int pos
)
435 int len
= isl_space_dim(domain_dim
, isl_dim_set
);
440 dim
= isl_space_map_from_set(domain_dim
);
441 next
= isl_basic_map_universe(isl_space_copy(dim
));
442 ls
= isl_local_space_from_space(dim
);
444 for (i
= 0; i
< len
; ++i
) {
447 c
= isl_equality_alloc(isl_local_space_copy(ls
));
448 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, 1);
449 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
451 c
= isl_constraint_set_constant_si(c
, 1);
452 next
= isl_basic_map_add_constraint(next
, c
);
455 isl_local_space_free(ls
);
457 return isl_map_from_basic_map(next
);
460 /* Check if the given access is coalesced (or if there is no point
461 * in trying to coalesce the access by mapping the array to shared memory).
462 * That is, check whether incrementing the dimension that will get
463 * wrapped over the last thread index results in incrementing
464 * the last array index.
466 * If no two consecutive array elements are ever accessed by "access",
467 * then mapping the corresponding array to shared memory will not
468 * improve coalescing. In fact, the copying will likely be performed
469 * by a single thread. Consider the access as coalesced such that
470 * the caller will not try and map the array to shared memory just
471 * to improve coalescing.
473 * This function is only called for access relations without reuse and
474 * kernels with at least one thread identifier.
476 static int access_is_coalesced(struct gpu_group_data
*data
,
477 __isl_keep isl_union_map
*access
)
482 isl_map
*next_thread_x
;
483 isl_map
*next_element
;
485 int coalesced
, empty
;
487 access
= isl_union_map_copy(access
);
488 access
= isl_union_map_apply_domain(access
,
489 isl_union_map_copy(data
->full_sched
));
490 access_map
= isl_map_from_union_map(access
);
492 space
= isl_map_get_space(access_map
);
493 space
= isl_space_range(space
);
494 next_element
= next(space
, isl_space_dim(space
, isl_dim_set
) - 1);
496 accessed
= isl_map_range(isl_map_copy(access_map
));
497 map
= isl_map_copy(next_element
);
498 map
= isl_map_intersect_domain(map
, isl_set_copy(accessed
));
499 map
= isl_map_intersect_range(map
, accessed
);
500 empty
= isl_map_is_empty(map
);
503 if (empty
< 0 || empty
) {
504 isl_map_free(next_element
);
505 isl_map_free(access_map
);
509 space
= isl_map_get_space(access_map
);
510 space
= isl_space_domain(space
);
511 next_thread_x
= next(space
, data
->thread_depth
+ data
->n_thread
- 1);
513 map
= isl_map_apply_domain(next_thread_x
, isl_map_copy(access_map
));
514 map
= isl_map_apply_range(map
, access_map
);
516 coalesced
= isl_map_is_subset(map
, next_element
);
518 isl_map_free(next_element
);
524 /* Replace the host schedule dimensions in the access relation "access"
525 * by parameters, so that they are treated as fixed when checking for reuse
526 * (within a kernel) or whether two consecutive elements are accessed
529 static __isl_give isl_union_map
*localize_access(struct gpu_group_data
*data
,
530 __isl_take isl_union_map
*access
)
538 umap
= isl_union_map_copy(data
->host_sched
);
539 space
= isl_union_map_get_space(umap
);
540 n
= data
->kernel_depth
;
541 ids
= ppcg_scop_generate_names(data
->scop
, n
, "__ppcg_host_");
542 param
= parametrization(space
, n
, 0, ids
);
543 isl_id_list_free(ids
);
544 umap
= isl_union_map_intersect_range(umap
,
545 isl_union_set_from_set(param
));
546 access
= isl_union_map_intersect_domain(access
,
547 isl_union_map_domain(umap
));
552 /* Given an access relation in terms of at least data->thread_depth initial
553 * dimensions of the computed schedule, check if it is bijective for
554 * fixed values of the first data->thread_depth dimensions.
555 * We perform this check by equating these dimensions to parameters.
557 static int access_is_bijective(struct gpu_group_data
*data
,
558 __isl_keep isl_map
*access
)
566 access
= isl_map_copy(access
);
567 space
= isl_space_params(isl_map_get_space(access
));
568 ids
= ppcg_scop_generate_names(data
->scop
, data
->thread_depth
, "s");
569 dim
= isl_map_dim(access
, isl_dim_in
);
570 par
= parametrization(space
, dim
, 0, ids
);
571 isl_id_list_free(ids
);
572 access
= isl_map_intersect_domain(access
, par
);
573 res
= isl_map_is_bijective(access
);
574 isl_map_free(access
);
579 /* Compute the number of outer schedule tile dimensions that affect
580 * the offset of "tile".
581 * If there is no such dimension, then return the index
582 * of the first kernel dimension, i.e., data->kernel_depth.
584 static int compute_tile_depth(struct gpu_group_data
*data
,
585 struct gpu_array_tile
*tile
)
589 for (j
= data
->thread_depth
- 1; j
>= data
->kernel_depth
; --j
) {
590 for (i
= 0; i
< tile
->n
; ++i
) {
594 lb
= tile
->bound
[i
].lb
;
595 if (isl_aff_involves_dims(lb
, isl_dim_in
, j
, 1))
598 shift
= tile
->bound
[i
].shift
;
601 if (isl_aff_involves_dims(shift
, isl_dim_in
, j
, 1))
611 /* Adjust the fields of "tile" to reflect the new input dimension "new_dim",
612 * where "old_dim" is the old dimension.
613 * The dimension beyond "new_dim" are assumed not to affect the tile,
614 * so they can simply be dropped.
616 static int tile_adjust_depth(struct gpu_array_tile
*tile
,
617 int old_dim
, int new_dim
)
621 if (old_dim
== new_dim
)
624 for (i
= 0; i
< tile
->n
; ++i
) {
625 tile
->bound
[i
].lb
= isl_aff_drop_dims(tile
->bound
[i
].lb
,
626 isl_dim_in
, new_dim
, old_dim
- new_dim
);
627 if (!tile
->bound
[i
].lb
)
629 if (!tile
->bound
[i
].shift
)
631 tile
->bound
[i
].shift
= isl_aff_drop_dims(tile
->bound
[i
].shift
,
632 isl_dim_in
, new_dim
, old_dim
- new_dim
);
633 if (!tile
->bound
[i
].shift
)
640 /* Determine the number of schedule dimensions that affect the offset of the
641 * shared or private tile and store the result in group->depth, with
642 * a lower bound of data->kernel_depth.
643 * If there is no tile defined on the array reference group,
644 * then set group->depth to data->thread_depth.
645 * Also adjust the fields of the tile to only refer to the group->depth
646 * outer schedule dimensions.
648 static int set_depth(struct gpu_group_data
*data
,
649 struct gpu_array_ref_group
*group
)
651 struct gpu_array_tile
*tile
;
653 group
->depth
= data
->thread_depth
;
655 tile
= gpu_array_ref_group_tile(group
);
659 group
->depth
= compute_tile_depth(data
, tile
);
660 if (tile_adjust_depth(tile
, data
->thread_depth
, group
->depth
) < 0)
666 /* Fill up the groups array with singleton groups, i.e., one group
667 * per reference, initializing the array, access, write, n_ref and refs fields.
668 * In particular the access field is initialized to the scheduled
669 * access relation of the array reference.
671 * Return the number of elements initialized, i.e., the number of
672 * active references in the current kernel.
674 static int populate_array_references(struct gpu_local_array_info
*local
,
675 struct gpu_array_ref_group
**groups
, struct gpu_group_data
*data
)
679 isl_ctx
*ctx
= isl_union_map_get_ctx(data
->shared_sched
);
682 for (i
= 0; i
< local
->array
->n_ref
; ++i
) {
685 struct gpu_array_ref_group
*group
;
686 struct gpu_stmt_access
*access
= local
->array
->refs
[i
];
688 map
= isl_map_copy(access
->access
);
689 umap
= isl_union_map_from_map(map
);
690 umap
= isl_union_map_apply_domain(umap
,
691 isl_union_map_copy(data
->shared_sched
));
693 if (isl_union_map_is_empty(umap
)) {
694 isl_union_map_free(umap
);
698 map
= isl_map_from_union_map(umap
);
699 map
= isl_map_detect_equalities(map
);
701 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
704 group
->local_array
= local
;
705 group
->array
= local
->array
;
707 group
->write
= access
->write
;
708 group
->exact_write
= access
->exact_write
;
709 group
->slice
= access
->n_index
< local
->array
->n_index
;
710 group
->refs
= &local
->array
->refs
[i
];
719 /* If group->n_ref == 1, then group->refs was set by
720 * populate_array_references to point directly into
721 * group->array->refs and should not be freed.
722 * If group->n_ref > 1, then group->refs was set by join_groups
723 * to point to a newly allocated array.
725 struct gpu_array_ref_group
*gpu_array_ref_group_free(
726 struct gpu_array_ref_group
*group
)
730 gpu_array_tile_free(group
->shared_tile
);
731 gpu_array_tile_free(group
->private_tile
);
732 isl_map_free(group
->access
);
733 if (group
->n_ref
> 1)
739 /* Check if the access relations of group1 and group2 overlap within
742 static int accesses_overlap(struct gpu_array_ref_group
*group1
,
743 struct gpu_array_ref_group
*group2
)
747 disjoint
= isl_map_is_disjoint(group1
->access
, group2
->access
);
754 /* Combine the given two groups into a single group, containing
755 * the references of both groups.
757 static struct gpu_array_ref_group
*join_groups(
758 struct gpu_array_ref_group
*group1
,
759 struct gpu_array_ref_group
*group2
)
763 struct gpu_array_ref_group
*group
;
765 if (!group1
|| !group2
)
768 ctx
= isl_map_get_ctx(group1
->access
);
769 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
772 group
->local_array
= group1
->local_array
;
773 group
->array
= group1
->array
;
774 group
->access
= isl_map_union(isl_map_copy(group1
->access
),
775 isl_map_copy(group2
->access
));
776 group
->write
= group1
->write
|| group2
->write
;
777 group
->exact_write
= group1
->exact_write
&& group2
->exact_write
;
778 group
->slice
= group1
->slice
|| group2
->slice
;
779 group
->n_ref
= group1
->n_ref
+ group2
->n_ref
;
780 group
->refs
= isl_alloc_array(ctx
, struct gpu_stmt_access
*,
783 return gpu_array_ref_group_free(group
);
784 for (i
= 0; i
< group1
->n_ref
; ++i
)
785 group
->refs
[i
] = group1
->refs
[i
];
786 for (i
= 0; i
< group2
->n_ref
; ++i
)
787 group
->refs
[group1
->n_ref
+ i
] = group2
->refs
[i
];
792 /* Combine the given two groups into a single group and free
793 * the original two groups.
795 static struct gpu_array_ref_group
*join_groups_and_free(
796 struct gpu_array_ref_group
*group1
,
797 struct gpu_array_ref_group
*group2
)
799 struct gpu_array_ref_group
*group
;
801 group
= join_groups(group1
, group2
);
802 gpu_array_ref_group_free(group1
);
803 gpu_array_ref_group_free(group2
);
807 /* Report that the array reference group with the given access relation
808 * is not mapped to shared memory in the given kernel because
809 * it does not exhibit any reuse and is considered to be coalesced.
811 static void report_no_reuse_and_coalesced(struct ppcg_kernel
*kernel
,
812 __isl_keep isl_union_map
*access
)
817 ctx
= isl_union_map_get_ctx(access
);
818 p
= isl_printer_to_file(ctx
, stdout
);
819 p
= isl_printer_print_str(p
, "Array reference group ");
820 p
= isl_printer_print_union_map(p
, access
);
821 p
= isl_printer_print_str(p
,
822 " not considered for mapping to shared memory in kernel");
823 p
= isl_printer_print_int(p
, kernel
->id
);
824 p
= isl_printer_print_str(p
,
825 " because it exhibits no reuse and is considered to be coalesced");
826 p
= isl_printer_end_line(p
);
830 /* Given an access relation in terms of the data->thread_depth initial
831 * dimensions of the computed schedule and the thread identifiers
832 * (as parameters), check if the use of the corresponding private tile
833 * requires unrolling.
835 * If we are creating a private tile because we are forced to,
836 * then no unrolling is required.
837 * Otherwise we check if "access" is bijective and unrolling
838 * is required if it is not. Note that the access relation
839 * has already been determined to be bijective before the introduction
840 * of the thread identifiers and the removal of the schedule dimensions
841 * that are mapped to these threads. If the access relation is no longer
842 * bijective, then this means that more than one value of one of those
843 * schedule dimensions is mapped to the same thread and therefore
844 * unrolling is required.
846 static int check_requires_unroll(struct gpu_group_data
*data
,
847 __isl_keep isl_map
*access
, int force_private
)
853 bijective
= access_is_bijective(data
, access
);
859 /* Compute the private and/or shared memory tiles for the array
860 * reference group "group" of array "array".
861 * Return 0 on success and -1 on error.
863 * If the array is a read-only scalar or if the user requested
864 * not to use shared or private memory, then we do not need to do anything.
866 * If any reference in the reference group accesses more than one element,
867 * then we would have to make sure that the layout in shared memory
868 * is the same as that in global memory. Since we do not handle this yet
869 * (and it may not even be possible), we refuse to map to private or
870 * shared memory in such cases.
872 * If the array group involves any may writes (that are not must writes),
873 * then we would have to make sure that we load the data into shared/private
874 * memory first in case the data is not written by the kernel
875 * (but still written back out to global memory).
876 * Since we don't have any such mechanism at the moment, we don't
877 * compute shared/private tiles for groups involving may writes.
879 * We only try to compute a shared memory tile if there is any reuse
880 * or if the access is not coalesced.
881 * Reuse and coalescing are checked within the given kernel.
883 * For computing a private memory tile, we also require that there is
884 * some reuse. Moreover, we require that the access is private
885 * to the thread. That is, we check that any given array element
886 * is only accessed by a single thread.
887 * We compute an access relation that maps the outer
888 * data->thread_depth + data->n_thread schedule dimensions.
889 * The latter data->n_thread will be mapped to thread identifiers.
890 * We actually check that those iterators that will be wrapped
891 * partition the array space. This check is stricter than necessary
892 * since several iterations may be mapped onto the same thread
893 * and then they could be allowed to access the same memory elements,
894 * but our check does not allow this situation.
896 * We also check that the index expression only depends on parallel
897 * loops. That way, we can move those loops innermost and unroll them.
898 * Again, we use a test that is stricter than necessary.
899 * We actually check whether the index expression only depends
900 * on the iterators that are wrapped over the threads.
901 * These are necessarily parallel, but there may be more parallel loops.
903 * Combining the injectivity of the first test with the single-valuedness
904 * of the second test, we simply test for bijectivity.
906 * If the use of the private tile requires unrolling, but some
907 * of the other arrays are forcibly mapped to private memory,
908 * then we do not allow the use of this private tile since
909 * we cannot move the schedule dimensions that need to be unrolled down
910 * without performing some kind of expansion on those arrays
911 * that are forcibly mapped to private memory.
913 * If the array is marked force_private, then we bypass all checks
914 * and assume we can (and should) use registers.
916 * If it turns out we can (or have to) use registers, we compute
917 * the private memory tile size using can_tile, after introducing a dependence
918 * on the thread indices.
920 static int compute_group_bounds_core(struct ppcg_kernel
*kernel
,
921 struct gpu_array_ref_group
*group
, struct gpu_group_data
*data
)
923 isl_ctx
*ctx
= isl_space_get_ctx(group
->array
->space
);
924 isl_union_map
*access
, *local
;
925 int n_index
= group
->array
->n_index
;
926 int no_reuse
, coalesced
;
928 int force_private
= group
->local_array
->force_private
;
929 int use_shared
= kernel
->options
->use_shared_memory
&&
931 int use_private
= force_private
|| kernel
->options
->use_private_memory
;
935 if (!use_shared
&& !use_private
)
937 if (gpu_array_is_read_only_scalar(group
->array
))
939 if (!force_private
&& !group
->exact_write
)
944 access
= gpu_array_ref_group_access_relation(group
, 1, 1);
945 local
= localize_access(data
, isl_union_map_copy(access
));
946 no_reuse
= isl_union_map_is_injective(local
);
949 if (use_shared
&& no_reuse
)
950 coalesced
= access_is_coalesced(data
, local
);
951 isl_union_map_free(local
);
953 if (r
>= 0 && kernel
->options
->debug
->verbose
&&
954 use_shared
&& no_reuse
&& coalesced
)
955 report_no_reuse_and_coalesced(kernel
, access
);
957 if (use_shared
&& (!no_reuse
|| !coalesced
)) {
958 group
->shared_tile
= gpu_array_tile_create(ctx
,
959 group
->array
->n_index
);
960 if (!group
->shared_tile
)
962 else if (!can_tile(group
->access
, group
->shared_tile
))
964 gpu_array_tile_free(group
->shared_tile
);
967 if (r
< 0 || (!force_private
&& (!use_private
|| no_reuse
))) {
968 isl_union_map_free(access
);
972 access
= isl_union_map_apply_domain(access
,
973 isl_union_map_copy(data
->thread_sched
));
975 acc
= isl_map_from_union_map(access
);
977 if (!force_private
&& !access_is_bijective(data
, acc
)) {
982 acc
= isl_map_intersect_domain(acc
, isl_set_copy(data
->privatization
));
983 acc
= isl_map_project_out(acc
, isl_dim_in
, data
->thread_depth
,
985 requires_unroll
= check_requires_unroll(data
, acc
, force_private
);
986 if (requires_unroll
< 0 ||
987 (requires_unroll
&& kernel
->any_force_private
)) {
989 return requires_unroll
< 0 ? -1 : 0;
992 group
->private_tile
= gpu_array_tile_create(ctx
, n_index
);
993 if (!group
->private_tile
) {
997 group
->private_tile
->requires_unroll
= requires_unroll
;
998 if (!can_tile(acc
, group
->private_tile
))
999 group
->private_tile
= gpu_array_tile_free(group
->private_tile
);
1003 if (force_private
&& !group
->private_tile
)
1004 isl_die(ctx
, isl_error_internal
,
1005 "unable to map array reference group to registers",
1011 /* Compute the private and/or shared memory tiles for the array
1012 * reference group "group" of array "array" and set the tile depth.
1013 * Return 0 on success and -1 on error.
1015 static int compute_group_bounds(struct ppcg_kernel
*kernel
,
1016 struct gpu_array_ref_group
*group
, struct gpu_group_data
*data
)
1020 if (compute_group_bounds_core(kernel
, group
, data
) < 0)
1022 if (set_depth(data
, group
) < 0)
1028 /* If two groups have overlapping access relations (as determined by
1029 * the "overlap" function) and if one of them involves a write,
1030 * then merge the two groups into one.
1031 * If "compute_bounds" is set, then call compute_group_bounds
1032 * on the merged groups.
1034 * Return the updated number of groups.
1035 * Return -1 on error.
1037 static int group_writes(struct ppcg_kernel
*kernel
,
1038 int n
, struct gpu_array_ref_group
**groups
,
1039 int (*overlap
)(struct gpu_array_ref_group
*group1
,
1040 struct gpu_array_ref_group
*group2
), int compute_bounds
,
1041 struct gpu_group_data
*data
)
1045 for (i
= 0; i
< n
; ++i
) {
1046 for (j
= n
- 1; j
> i
; --j
) {
1047 if (!groups
[i
]->write
&& !groups
[j
]->write
)
1050 if (!overlap(groups
[i
], groups
[j
]))
1053 groups
[i
] = join_groups_and_free(groups
[i
], groups
[j
]);
1055 groups
[j
] = groups
[n
- 1];
1056 groups
[n
- 1] = NULL
;
1061 if (compute_bounds
&&
1062 compute_group_bounds(kernel
, groups
[i
], data
) < 0)
1070 /* If two groups have overlapping access relations (within the innermost
1071 * loop) and if one of them involves a write, then merge the two groups
1074 * Return the updated number of groups.
1076 static int group_overlapping_writes(struct ppcg_kernel
*kernel
,
1077 int n
, struct gpu_array_ref_group
**groups
,
1078 struct gpu_group_data
*data
)
1080 return group_writes(kernel
, n
, groups
, &accesses_overlap
, 0, data
);
1083 /* Check if the access relations of group1 and group2 overlap within
1084 * the outermost min(group1->depth, group2->depth) loops.
1086 static int depth_accesses_overlap(struct gpu_array_ref_group
*group1
,
1087 struct gpu_array_ref_group
*group2
)
1092 isl_map
*map_i
, *map_j
, *map
;
1094 depth
= group1
->depth
;
1095 if (group2
->depth
< depth
)
1096 depth
= group2
->depth
;
1097 map_i
= isl_map_copy(group1
->access
);
1098 dim
= isl_map_dim(map_i
, isl_dim_in
);
1099 map_i
= isl_map_eliminate(map_i
, isl_dim_in
, depth
, dim
- depth
);
1100 map_j
= isl_map_copy(group2
->access
);
1101 map_j
= isl_map_eliminate(map_j
, isl_dim_in
, depth
, dim
- depth
);
1102 map
= isl_map_intersect(map_i
, map_j
);
1103 empty
= isl_map_is_empty(map
);
1109 /* If two groups have overlapping access relations (within the outer
1110 * depth loops) and if one of them involves a write,
1111 * then merge the two groups into one.
1113 * Return the updated number of groups.
1115 static int group_depth_overlapping_writes(struct ppcg_kernel
*kernel
,
1116 int n
, struct gpu_array_ref_group
**groups
, struct gpu_group_data
*data
)
1118 return group_writes(kernel
, n
, groups
, &depth_accesses_overlap
, 1,
1122 /* Is the size of the tile specified by "tile" smaller than the sum of
1123 * the sizes of the tiles specified by "tile1" and "tile2"?
1125 static int smaller_tile(struct gpu_array_tile
*tile
,
1126 struct gpu_array_tile
*tile1
, struct gpu_array_tile
*tile2
)
1129 isl_val
*size
, *size1
, *size2
;
1131 size
= gpu_array_tile_size(tile
);
1132 size1
= gpu_array_tile_size(tile1
);
1133 size2
= gpu_array_tile_size(tile2
);
1135 size
= isl_val_sub(size
, size1
);
1136 size
= isl_val_sub(size
, size2
);
1137 smaller
= isl_val_is_neg(size
);
1144 /* Given an initial grouping of array references and shared memory tiles
1145 * for each group that allows for a shared memory tile, merge two groups
1146 * if both have a shared memory tile, the merged group also has
1147 * a shared memory tile and the size of the tile for the merge group
1148 * is smaller than the sum of the tile sizes of the individual groups.
1150 * If merging two groups decreases the depth of the tile of
1151 * one or both of the two groups, then we need to check for overlapping
1154 * Return the number of groups after merging.
1155 * Return -1 on error.
1157 static int group_common_shared_memory_tile(struct ppcg_kernel
*kernel
,
1158 struct gpu_array_info
*array
, int n
,
1159 struct gpu_array_ref_group
**groups
, struct gpu_group_data
*data
)
1162 int recompute_overlap
= 0;
1163 isl_ctx
*ctx
= isl_space_get_ctx(array
->space
);
1165 for (i
= 0; i
< n
; ++i
) {
1166 if (!groups
[i
]->shared_tile
)
1168 for (j
= n
- 1; j
> i
; --j
) {
1171 struct gpu_array_ref_group
*group
;
1173 if (!groups
[j
]->shared_tile
)
1176 map
= isl_map_intersect(isl_map_copy(groups
[i
]->access
),
1177 isl_map_copy(groups
[j
]->access
));
1178 empty
= isl_map_is_empty(map
);
1184 group
= join_groups(groups
[i
], groups
[j
]);
1185 if (compute_group_bounds(kernel
, group
, data
) < 0) {
1186 gpu_array_ref_group_free(group
);
1189 if (!group
->shared_tile
||
1190 !smaller_tile(group
->shared_tile
,
1191 groups
[i
]->shared_tile
,
1192 groups
[j
]->shared_tile
)) {
1193 gpu_array_ref_group_free(group
);
1197 if (group
->depth
< groups
[i
]->depth
||
1198 group
->depth
< groups
[j
]->depth
)
1199 recompute_overlap
= 1;
1200 gpu_array_ref_group_free(groups
[i
]);
1201 gpu_array_ref_group_free(groups
[j
]);
1204 groups
[j
] = groups
[n
- 1];
1209 if (recompute_overlap
)
1210 n
= group_depth_overlapping_writes(kernel
, n
, groups
, data
);
1214 /* Set array->n_group and array->groups to n and groups.
1216 * Additionally, set the "nr" field of each group.
1218 static void set_array_groups(struct gpu_local_array_info
*array
,
1219 int n
, struct gpu_array_ref_group
**groups
)
1224 array
->groups
= groups
;
1226 for (i
= 0; i
< n
; ++i
)
1230 /* Combine all groups in "groups" into a single group and return
1231 * the new number of groups (1 or 0 if there were no groups to start with).
1233 static int join_all_groups(int n
, struct gpu_array_ref_group
**groups
)
1237 for (i
= n
- 1; i
> 0; --i
) {
1238 groups
[0] = join_groups_and_free(groups
[0], groups
[i
]);
1246 /* Group array references that should be considered together when
1247 * deciding whether to access them from private, shared or global memory.
1248 * Return -1 on error.
1250 * In particular, if two array references overlap and if one of them
1251 * is a write, then the two references are grouped together.
1252 * We first perform an initial grouping based only on the access relation.
1253 * After computing shared and private memory tiles, we check for
1254 * overlapping writes again, but this time taking into account
1255 * the depth of the effective tile.
1257 * Furthermore, if two groups admit a shared memory tile and if the
1258 * combination of the two also admits a shared memory tile, we merge
1261 * If the array contains structures, then we compute a single
1262 * reference group without trying to find any tiles
1263 * since we do not map such arrays to private or shared
1266 static int group_array_references(struct ppcg_kernel
*kernel
,
1267 struct gpu_local_array_info
*local
, struct gpu_group_data
*data
)
1271 isl_ctx
*ctx
= isl_union_map_get_ctx(data
->shared_sched
);
1272 struct gpu_array_ref_group
**groups
;
1274 groups
= isl_calloc_array(ctx
, struct gpu_array_ref_group
*,
1275 local
->array
->n_ref
);
1279 n
= populate_array_references(local
, groups
, data
);
1281 if (local
->array
->has_compound_element
) {
1282 n
= join_all_groups(n
, groups
);
1283 set_array_groups(local
, n
, groups
);
1287 n
= group_overlapping_writes(kernel
, n
, groups
, data
);
1289 for (i
= 0; i
< n
; ++i
)
1290 if (compute_group_bounds(kernel
, groups
[i
], data
) < 0)
1293 n
= group_depth_overlapping_writes(kernel
, n
, groups
, data
);
1295 n
= group_common_shared_memory_tile(kernel
, local
->array
,
1298 set_array_groups(local
, n
, groups
);
1303 for (i
= 0; i
< local
->array
->n_ref
; ++i
)
1304 gpu_array_ref_group_free(groups
[i
]);
1308 /* For each scalar in the input program, check if there are any
1309 * order dependences active inside the current kernel, within
1310 * the same iteration of "host_schedule".
1311 * If so, mark the scalar as force_private so that it will be
1312 * mapped to a register.
1314 static void check_scalar_live_ranges_in_host(struct ppcg_kernel
*kernel
,
1315 __isl_take isl_union_map
*host_schedule
)
1318 isl_union_map
*sched
;
1319 isl_union_set
*domain
;
1320 isl_union_map
*same_host_iteration
;
1322 kernel
->any_force_private
= 0;
1324 sched
= isl_union_map_universe(isl_union_map_copy(host_schedule
));
1325 domain
= isl_union_map_domain(sched
);
1327 same_host_iteration
= isl_union_map_apply_range(host_schedule
,
1328 isl_union_map_reverse(isl_union_map_copy(host_schedule
)));
1330 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1331 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1332 isl_union_map
*order
;
1334 local
->force_private
= 0;
1335 if (local
->array
->n_index
!= 0)
1337 order
= isl_union_map_copy(local
->array
->dep_order
);
1338 order
= isl_union_map_intersect_domain(order
,
1339 isl_union_set_copy(domain
));
1340 order
= isl_union_map_intersect_range(order
,
1341 isl_union_set_copy(domain
));
1342 order
= isl_union_map_intersect(order
,
1343 isl_union_map_copy(same_host_iteration
));
1344 if (!isl_union_map_is_empty(order
)) {
1345 local
->force_private
= 1;
1346 kernel
->any_force_private
= 1;
1348 isl_union_map_free(order
);
1351 isl_union_map_free(same_host_iteration
);
1352 isl_union_set_free(domain
);
1355 /* For each scalar in the input program, check if there are any
1356 * order dependences active inside the current kernel, within
1357 * the same iteration of the host schedule, i.e., the prefix
1358 * schedule at "node".
1359 * If so, mark the scalar as force_private so that it will be
1360 * mapped to a register.
1362 static void check_scalar_live_ranges(struct ppcg_kernel
*kernel
,
1363 __isl_keep isl_schedule_node
*node
)
1365 isl_union_map
*sched
;
1367 if (!kernel
->options
->live_range_reordering
)
1370 sched
= isl_schedule_node_get_prefix_schedule_union_map(node
);
1372 check_scalar_live_ranges_in_host(kernel
, sched
);
1375 /* Create a set of dimension data->thread_depth + data->n_thread
1376 * that equates the residue of the final data->n_thread dimensions
1377 * modulo the "sizes" to the thread identifiers.
1378 * "space" is a parameter space containing the thread identifiers.
1379 * Store the computed set in data->privatization.
1381 static void compute_privatization(struct gpu_group_data
*data
,
1382 __isl_take isl_space
*space
, int *sizes
)
1386 isl_local_space
*ls
;
1389 ctx
= isl_union_map_get_ctx(data
->shared_sched
);
1390 space
= isl_space_set_from_params(space
);
1391 space
= isl_space_add_dims(space
, isl_dim_set
,
1392 data
->thread_depth
+ data
->n_thread
);
1393 set
= isl_set_universe(space
);
1394 space
= isl_set_get_space(set
);
1395 ls
= isl_local_space_from_space(space
);
1397 for (i
= 0; i
< data
->n_thread
; ++i
) {
1398 isl_aff
*aff
, *aff2
;
1404 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
1405 isl_dim_set
, data
->thread_depth
+ i
);
1406 v
= isl_val_int_from_si(ctx
, sizes
[i
]);
1407 aff
= isl_aff_mod_val(aff
, v
);
1408 snprintf(name
, sizeof(name
), "t%d", i
);
1409 pos
= isl_set_find_dim_by_name(set
, isl_dim_param
, name
);
1410 aff2
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
1411 isl_dim_param
, pos
);
1412 aff
= isl_aff_sub(aff
, aff2
);
1413 c
= isl_equality_from_aff(aff
);
1414 set
= isl_set_add_constraint(set
, c
);
1417 isl_local_space_free(ls
);
1418 data
->privatization
= set
;
1421 /* Group references of all arrays in "kernel".
1422 * "node" points to the kernel mark.
1424 * We first extract all required schedule information into
1425 * a gpu_group_data structure and then consider each array
1428 int gpu_group_references(struct ppcg_kernel
*kernel
,
1429 __isl_keep isl_schedule_node
*node
)
1434 struct gpu_group_data data
;
1436 check_scalar_live_ranges(kernel
, node
);
1438 data
.scop
= kernel
->prog
->scop
;
1440 data
.kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
1441 data
.host_sched
= isl_schedule_node_get_prefix_schedule_relation(node
);
1443 node
= isl_schedule_node_copy(node
);
1444 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
1446 isl_schedule_node_get_prefix_schedule_relation(node
);
1447 data
.shared_sched
= isl_union_map_detect_equalities(data
.shared_sched
);
1449 node
= isl_schedule_node_child(node
, 0);
1450 data
.thread_depth
= isl_schedule_node_get_schedule_depth(node
);
1451 data
.n_thread
= isl_schedule_node_band_n_member(node
);
1452 data
.thread_sched
= isl_union_map_copy(data
.shared_sched
);
1453 data
.thread_sched
= isl_union_map_flat_range_product(data
.thread_sched
,
1454 isl_schedule_node_band_get_partial_schedule_union_map(node
));
1455 data
.thread_sched
= isl_union_map_detect_equalities(data
.thread_sched
);
1456 node
= isl_schedule_node_child(node
, 0);
1457 data
.full_sched
= isl_union_map_copy(data
.thread_sched
);
1458 data
.full_sched
= isl_union_map_flat_range_product(data
.full_sched
,
1459 isl_schedule_node_get_subtree_schedule_union_map(node
));
1460 isl_schedule_node_free(node
);
1462 space
= isl_union_set_get_space(kernel
->thread_filter
);
1463 compute_privatization(&data
, space
, kernel
->block_dim
);
1465 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1466 r
= group_array_references(kernel
, &kernel
->array
[i
], &data
);
1471 isl_union_map_free(data
.host_sched
);
1472 isl_union_map_free(data
.shared_sched
);
1473 isl_union_map_free(data
.thread_sched
);
1474 isl_union_map_free(data
.full_sched
);
1475 isl_set_free(data
.privatization
);
1480 /* Given a description of an array tile "tile" and the "space"
1484 * where D represents the first group->depth schedule dimensions
1485 * and A represents the array, construct an isl_multi_aff
1487 * { [D[i] -> A[a]] -> A'[a'] }
1489 * with A' a scaled down copy of A according to the shifts and strides
1490 * in "tile". In particular,
1492 * a' = (a + shift(i))/stride
1494 * "insert_array" represents
1498 * and is used to insert A into the domain of functions that only
1501 static __isl_give isl_multi_aff
*strided_tile(
1502 struct gpu_array_tile
*tile
, __isl_keep isl_space
*space
,
1503 __isl_keep isl_multi_aff
*insert_array
)
1507 isl_multi_aff
*shift
;
1508 isl_multi_val
*stride
;
1510 isl_local_space
*ls
;
1511 isl_multi_aff
*tiling
;
1513 ctx
= isl_space_get_ctx(space
);
1514 space2
= isl_space_domain(isl_space_copy(space
));
1515 ls
= isl_local_space_from_space(space2
);
1516 space2
= isl_space_range(isl_space_copy(space
));
1517 stride
= isl_multi_val_zero(space2
);
1518 shift
= isl_multi_aff_zero(isl_space_copy(space
));
1520 for (i
= 0; i
< tile
->n
; ++i
) {
1521 struct gpu_array_bound
*bound
= &tile
->bound
[i
];
1525 if (tile
->bound
[i
].shift
) {
1526 stride_i
= isl_val_copy(bound
->stride
);
1527 shift_i
= isl_aff_copy(bound
->shift
);
1529 stride_i
= isl_val_one(ctx
);
1530 shift_i
= isl_aff_zero_on_domain(
1531 isl_local_space_copy(ls
));
1534 stride
= isl_multi_val_set_val(stride
, i
, stride_i
);
1535 shift
= isl_multi_aff_set_aff(shift
, i
, shift_i
);
1537 isl_local_space_free(ls
);
1539 shift
= isl_multi_aff_pullback_multi_aff(shift
,
1540 isl_multi_aff_copy(insert_array
));
1542 tiling
= isl_multi_aff_range_map(isl_space_copy(space
));
1543 tiling
= isl_multi_aff_add(tiling
, shift
);
1544 tiling
= isl_multi_aff_scale_down_multi_val(tiling
, stride
);
1549 /* Compute a tiling for the array reference group "group".
1551 * The tiling is of the form
1553 * { [D[i] -> A[a]] -> T[t] }
1555 * where D represents the first group->depth schedule dimensions,
1556 * A represents the global array and T represents the shared or
1557 * private memory tile. The name of T is the name of the local
1560 * If there is any stride in the accesses, then the mapping is
1562 * t = (a + shift(i))/stride - lb(i)
1564 * otherwise, it is simply
1568 void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group
*group
)
1572 struct gpu_array_tile
*tile
;
1573 struct gpu_array_info
*array
= group
->array
;
1575 isl_multi_aff
*tiling
, *lb
, *insert_array
;
1579 tile
= group
->private_tile
;
1581 tile
= group
->shared_tile
;
1585 space
= isl_map_get_space(group
->access
);
1586 dim
= isl_space_dim(space
, isl_dim_in
);
1587 space
= isl_space_drop_dims(space
, isl_dim_in
, group
->depth
,
1588 dim
- group
->depth
);
1589 insert_array
= isl_multi_aff_domain_map(isl_space_copy(space
));
1591 for (i
= 0; i
< tile
->n
; ++i
)
1592 if (tile
->bound
[i
].shift
)
1596 tiling
= strided_tile(tile
, space
, insert_array
);
1598 tiling
= isl_multi_aff_range_map(isl_space_copy(space
));
1600 lb
= isl_multi_aff_zero(space
);
1601 for (i
= 0; i
< tile
->n
; ++i
) {
1602 isl_aff
*lb_i
= isl_aff_copy(tile
->bound
[i
].lb
);
1603 lb
= isl_multi_aff_set_aff(lb
, i
, lb_i
);
1605 lb
= isl_multi_aff_pullback_multi_aff(lb
, insert_array
);
1607 tiling
= isl_multi_aff_sub(tiling
, lb
);
1609 p
= isl_printer_to_str(isl_multi_aff_get_ctx(tiling
));
1610 p
= gpu_array_ref_group_print_name(group
, p
);
1611 local_name
= isl_printer_get_str(p
);
1612 isl_printer_free(p
);
1613 tiling
= isl_multi_aff_set_tuple_name(tiling
, isl_dim_out
, local_name
);
1616 tile
->tiling
= tiling
;