3 #include "gpu_array_tile.h"
7 /* Print the name of the local copy of a given group of array references.
9 __isl_give isl_printer
*gpu_array_ref_group_print_name(
10 struct gpu_array_ref_group
*group
, __isl_take isl_printer
*p
)
14 if (group
->private_tile
)
15 p
= isl_printer_print_str(p
, "private_");
16 else if (group
->shared_tile
)
17 p
= isl_printer_print_str(p
, "shared_");
20 p
= isl_printer_print_str(p
, group
->array
->name
);
21 if (!global
&& group
->local_array
->n_group
> 1) {
22 p
= isl_printer_print_str(p
, "_");
23 p
= isl_printer_print_int(p
, group
->nr
);
29 /* Return the union of all read (read = 1) and/or write (write = 1)
30 * access relations in the group.
32 __isl_give isl_union_map
*gpu_array_ref_group_access_relation(
33 struct gpu_array_ref_group
*group
, int read
, int write
)
36 isl_union_map
*access
;
38 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
39 for (i
= 0; i
< group
->n_ref
; ++i
) {
42 if (!((read
&& group
->refs
[i
]->read
) ||
43 (write
&& group
->refs
[i
]->write
)))
45 map_i
= isl_map_copy(group
->refs
[i
]->access
);
46 access
= isl_union_map_union(access
,
47 isl_union_map_from_map(map_i
));
53 /* Return the effective gpu_array_tile associated to "group" or
54 * NULL if there is no such gpu_array_tile.
55 * If we have computed both a private and a shared tile, then
56 * the private tile is used.
58 struct gpu_array_tile
*gpu_array_ref_group_tile(
59 struct gpu_array_ref_group
*group
)
61 if (group
->private_tile
)
62 return group
->private_tile
;
63 if (group
->shared_tile
)
64 return group
->shared_tile
;
68 /* Does the tile associated to "group" require unrolling of the schedule
69 * dimensions mapped to threads?
70 * Note that this can only happen for private tiles.
72 int gpu_array_ref_group_requires_unroll(struct gpu_array_ref_group
*group
)
74 struct gpu_array_tile
*tile
;
76 tile
= gpu_array_ref_group_tile(group
);
79 return tile
->requires_unroll
;
86 * or -a(p,i) - j = g f(e) if sign < 0,
87 * store a(p,i) in bound->shift and g (stride) in bound->stride.
88 * a(p,i) is assumed to be an expression in only the parameters
89 * and the input dimensions.
91 static void extract_stride(__isl_keep isl_constraint
*c
,
92 struct gpu_array_bound
*bound
, __isl_keep isl_val
*stride
, int sign
)
101 isl_val_free(bound
->stride
);
102 bound
->stride
= isl_val_copy(stride
);
104 space
= isl_constraint_get_space(c
);
105 space
= isl_space_domain(space
);
107 nparam
= isl_space_dim(space
, isl_dim_param
);
108 nvar
= isl_space_dim(space
, isl_dim_set
);
110 v
= isl_constraint_get_constant_val(c
);
113 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
114 aff
= isl_aff_set_constant_val(aff
, v
);
116 for (i
= 0; i
< nparam
; ++i
) {
117 if (!isl_constraint_involves_dims(c
, isl_dim_param
, i
, 1))
119 v
= isl_constraint_get_coefficient_val(c
, isl_dim_param
, i
);
122 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_param
, i
, v
);
125 for (i
= 0; i
< nvar
; ++i
) {
126 if (!isl_constraint_involves_dims(c
, isl_dim_in
, i
, 1))
128 v
= isl_constraint_get_coefficient_val(c
, isl_dim_in
, i
);
131 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, i
, v
);
137 /* Given an equality constraint of a map with a single output dimension j,
138 * check if the constraint is of the form
140 * a(p,i) + j = g f(e)
142 * with a(p,i) an expression in the parameters and input dimensions
143 * and f(e) an expression in the existentially quantified variables.
144 * If so, and if g is larger than any such g from a previously considered
145 * constraint, then call extract_stride to record the stride information
148 static int check_stride_constraint(__isl_take isl_constraint
*c
, void *user
)
154 struct gpu_array_bound
*bound
= user
;
156 ctx
= isl_constraint_get_ctx(c
);
157 n_div
= isl_constraint_dim(c
, isl_dim_div
);
158 v
= isl_constraint_get_coefficient_val(c
, isl_dim_out
, 0);
160 if (n_div
&& (isl_val_is_one(v
) || isl_val_is_negone(v
))) {
161 int s
= isl_val_sgn(v
);
162 isl_val
*stride
= isl_val_zero(ctx
);
165 for (i
= 0; i
< n_div
; ++i
) {
166 v
= isl_constraint_get_coefficient_val(c
,
168 stride
= isl_val_gcd(stride
, v
);
170 if (!isl_val_is_zero(stride
) &&
171 isl_val_gt(stride
, bound
->stride
))
172 extract_stride(c
, bound
, stride
, s
);
174 isl_val_free(stride
);
178 isl_constraint_free(c
);
182 /* Given contraints on an array index i, check if we can find
183 * a shift a(p) and a stride g such that
187 * If so, record the information in bound and apply the mapping
188 * i -> (i + a(p))/g to the array index in bounds and return
189 * the new constraints.
190 * If not, simply return the original constraints.
192 * If bounds is a subset of the space
196 * then the bound recorded in bound->shift is of the form
200 * with s(D) equal to a(p) above.
201 * Next, we construct a mapping of the form
203 * [D -> i] -> [D -> (i + S(D))/g]
205 * This mapping is computed as follows.
206 * We first introduce "i" in the domain through precomposition
207 * with [D -> i] -> D obtaining
211 * Adding [D -> i] -> i produces
213 * [D -> i] -> i + s(D)
215 * and the domain product with [D -> i] -> D yields
217 * [D -> i] -> [D -> i + s(D)]
219 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
221 static __isl_give isl_basic_map
*check_stride(struct gpu_array_bound
*bound
,
222 __isl_take isl_basic_map
*bounds
)
226 isl_basic_map
*shift
, *id
, *bmap
, *scale
;
230 bound
->stride
= NULL
;
232 hull
= isl_basic_map_affine_hull(isl_basic_map_copy(bounds
));
234 isl_basic_map_foreach_constraint(hull
, &check_stride_constraint
, bound
);
236 isl_basic_map_free(hull
);
241 shift
= isl_basic_map_from_aff(isl_aff_copy(bound
->shift
));
242 space
= isl_basic_map_get_space(bounds
);
243 bmap
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
244 shift
= isl_basic_map_apply_range(bmap
, shift
);
245 space
= isl_basic_map_get_space(bounds
);
246 id
= isl_basic_map_range_map(isl_basic_map_universe(space
));
247 shift
= isl_basic_map_sum(id
, shift
);
248 space
= isl_basic_map_get_space(bounds
);
249 id
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
250 shift
= isl_basic_map_range_product(id
, shift
);
252 space
= isl_space_domain(isl_basic_map_get_space(bounds
));
253 id
= isl_basic_map_identity(isl_space_map_from_set(space
));
254 space
= isl_space_range(isl_basic_map_get_space(bounds
));
255 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
256 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
257 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(bound
->stride
));
258 scale
= isl_basic_map_from_aff(aff
);
259 scale
= isl_basic_map_product(id
, scale
);
261 bmap
= isl_basic_map_apply_range(shift
, scale
);
262 bset
= isl_basic_set_apply(isl_basic_map_wrap(bounds
), bmap
);
263 bounds
= isl_basic_set_unwrap(bset
);
268 /* Data used in compute_array_dim_size and compute_size_in_direction.
270 * pos is the position of the variable representing the array index,
271 * i.e., the variable for which want to compute the size. This variable
272 * is also the last variable in the set.
274 struct gpu_size_info
{
276 struct gpu_array_bound
*bound
;
280 /* Given a constraint from the basic set describing the bounds on
281 * an array index, check if it is a lower bound, say m i >= b(x), and,
282 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
283 * upper bound. If so, and if this bound is smaller than any bound
284 * derived from earlier constraints, set the size to this bound on
285 * the expression and the lower bound to ceil(b(x)/m).
287 static int compute_size_in_direction(__isl_take isl_constraint
*c
, void *user
)
289 struct gpu_size_info
*size
= user
;
296 nparam
= isl_basic_set_dim(size
->bset
, isl_dim_param
);
297 n_div
= isl_constraint_dim(c
, isl_dim_div
);
299 if (isl_constraint_involves_dims(c
, isl_dim_div
, 0, n_div
) ||
300 !isl_constraint_is_lower_bound(c
, isl_dim_set
, size
->pos
)) {
301 isl_constraint_free(c
);
305 aff
= isl_constraint_get_bound(c
, isl_dim_set
, size
->pos
);
306 aff
= isl_aff_ceil(aff
);
308 lb
= isl_aff_copy(aff
);
310 aff
= isl_aff_neg(aff
);
311 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, size
->pos
, 1);
313 v
= isl_basic_set_max_val(size
->bset
, aff
);
316 if (isl_val_is_int(v
)) {
317 v
= isl_val_add_ui(v
, 1);
318 if (!size
->bound
->size
|| isl_val_lt(v
, size
->bound
->size
)) {
319 isl_val_free(size
->bound
->size
);
320 size
->bound
->size
= isl_val_copy(v
);
321 lb
= isl_aff_drop_dims(lb
, isl_dim_in
, size
->pos
, 1);
322 isl_aff_free(size
->bound
->lb
);
323 size
->bound
->lb
= isl_aff_copy(lb
);
329 isl_constraint_free(c
);
334 /* Given a basic map "bounds" that maps parameters and input dimensions
335 * to a single output dimension, look for an expression in the parameters
336 * and input dimensions such that the range of the output dimension shifted
337 * by this expression is a constant.
339 * In particular, we currently only consider lower bounds on the output
340 * dimension as candidate expressions.
342 static int compute_array_dim_size(struct gpu_array_bound
*bound
,
343 __isl_take isl_basic_map
*bounds
)
345 struct gpu_size_info size
;
347 bounds
= isl_basic_map_detect_equalities(bounds
);
348 bounds
= check_stride(bound
, bounds
);
354 size
.pos
= isl_basic_map_dim(bounds
, isl_dim_in
);
355 size
.bset
= isl_basic_map_wrap(bounds
);
356 size
.bset
= isl_basic_set_flatten(size
.bset
);
357 size
.bset
= isl_set_simple_hull(isl_basic_set_compute_divs(size
.bset
));
358 isl_basic_set_foreach_constraint(size
.bset
, &compute_size_in_direction
,
360 isl_basic_set_free(size
.bset
);
362 return bound
->size
? 0 : -1;
365 /* Check if we can find a memory tile for the given array
366 * based on the given accesses, and if so, put the results in "tile".
368 * We project the accesses on each index in turn and look for a parametric
369 * offset such that the size is constant.
371 static int can_tile(__isl_keep isl_map
*access
, struct gpu_array_tile
*tile
)
375 for (i
= 0; i
< tile
->n
; ++i
) {
379 access_i
= isl_map_copy(access
);
380 access_i
= isl_map_project_out(access_i
, isl_dim_out
, 0, i
);
381 access_i
= isl_map_project_out(access_i
, isl_dim_out
,
382 1, tile
->n
- (i
+ 1));
383 access_i
= isl_map_compute_divs(access_i
);
384 hull
= isl_map_simple_hull(access_i
);
385 if (compute_array_dim_size(&tile
->bound
[i
], hull
) < 0)
392 /* Construct a map from domain_dim to domain_dim that increments
393 * the dimension at position "pos" and leaves all other dimensions
396 static __isl_give isl_map
*next(__isl_take isl_space
*domain_dim
, int pos
)
399 int len
= isl_space_dim(domain_dim
, isl_dim_set
);
404 dim
= isl_space_map_from_set(domain_dim
);
405 next
= isl_basic_map_universe(isl_space_copy(dim
));
406 ls
= isl_local_space_from_space(dim
);
408 for (i
= 0; i
< len
; ++i
) {
411 c
= isl_equality_alloc(isl_local_space_copy(ls
));
412 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, 1);
413 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
415 c
= isl_constraint_set_constant_si(c
, 1);
416 next
= isl_basic_map_add_constraint(next
, c
);
419 isl_local_space_free(ls
);
421 return isl_map_from_basic_map(next
);
424 /* Check if the given access is coalesced.
425 * That is, check whether incrementing the dimension that will get
426 * wrapped over the last thread index results in incrementing
427 * the last array index.
429 * This function is only called for access relations without reuse and
430 * kernels with at least one block dimension.
432 static int access_is_coalesced(struct gpu_gen
*gen
,
433 __isl_keep isl_union_map
*access
)
437 isl_map
*next_thread_x
;
438 isl_map
*next_element
;
442 access
= isl_union_map_copy(access
);
443 access
= isl_union_map_apply_domain(access
,
444 isl_union_map_copy(gen
->tiled_sched
));
445 access_map
= isl_map_from_union_map(access
);
447 dim
= isl_map_get_space(access_map
);
448 dim
= isl_space_domain(dim
);
449 next_thread_x
= next(dim
, gen
->shared_len
+ gen
->kernel
->n_block
- 1);
451 dim
= isl_map_get_space(access_map
);
452 dim
= isl_space_range(dim
);
453 next_element
= next(dim
, isl_space_dim(dim
, isl_dim_set
) - 1);
455 map
= isl_map_apply_domain(next_thread_x
, isl_map_copy(access_map
));
456 map
= isl_map_apply_range(map
, access_map
);
458 coalesced
= isl_map_is_subset(map
, next_element
);
460 isl_map_free(next_element
);
466 /* Given an access relation in terms of at least gen->shared_len initial
467 * dimensions of the computed schedule, check if it is bijective for
468 * fixed values of the first gen->shared_len dimensions.
469 * We perform this check by equating these dimensions to parameters.
471 static int access_is_bijective(struct gpu_gen
*gen
, __isl_keep isl_map
*access
)
479 access
= isl_map_copy(access
);
480 space
= isl_space_params(isl_map_get_space(access
));
481 ids
= ppcg_scop_generate_names(gen
->prog
->scop
, gen
->shared_len
, "s");
482 dim
= isl_map_dim(access
, isl_dim_in
);
483 par
= parametrization(space
, dim
, 0, ids
);
484 isl_id_list_free(ids
);
485 access
= isl_map_intersect_domain(access
, par
);
486 res
= isl_map_is_bijective(access
);
487 isl_map_free(access
);
492 /* Look for the last shared tile loop that affects the offset of "tile"
493 * and return the result.
494 * If there is no such loop, then return the index of the loop
495 * before the first shared tile loop, in particular gen->tile_first - 1.
497 static int compute_tile_last_shared(struct gpu_gen
*gen
,
498 struct gpu_array_tile
*tile
)
502 for (j
= gen
->shared_len
- 1; j
>= gen
->tile_first
; --j
) {
503 for (i
= 0; i
< tile
->n
; ++i
) {
507 lb
= tile
->bound
[i
].lb
;
508 if (isl_aff_involves_dims(lb
, isl_dim_in
, j
, 1))
511 shift
= tile
->bound
[i
].shift
;
514 if (isl_aff_involves_dims(shift
, isl_dim_in
, j
, 1))
524 /* Look for the last shared tile loop that affects the offset of the
525 * shared or private tile and store the result in group->last_shared.
526 * If there is no such loop, then group->last_shared is set to a value
527 * before the first shared tile loop, in particular gen->tile_first - 1.
528 * If there is no tile defined on the array reference group,
529 * then set group->last_shared to gen->shared_len - 1.
531 static void set_last_shared(struct gpu_gen
*gen
,
532 struct gpu_array_ref_group
*group
)
534 struct gpu_array_tile
*tile
;
536 group
->last_shared
= gen
->shared_len
- 1;
538 tile
= gpu_array_ref_group_tile(group
);
542 group
->last_shared
= compute_tile_last_shared(gen
, tile
);
545 /* Fill up the groups array with singleton groups, i.e., one group
546 * per reference, initializing the array, access, write, n_ref and refs fields.
547 * In particular the access field is initialized to the scheduled
548 * access relation of the array reference.
550 * Return the number of elements initialized, i.e., the number of
551 * active references in the current kernel.
553 static int populate_array_references(struct gpu_local_array_info
*local
,
554 __isl_keep isl_union_map
*sched
, struct gpu_array_ref_group
**groups
)
558 isl_ctx
*ctx
= isl_union_map_get_ctx(sched
);
561 for (i
= 0; i
< local
->array
->n_ref
; ++i
) {
564 struct gpu_array_ref_group
*group
;
565 struct gpu_stmt_access
*access
= local
->array
->refs
[i
];
567 map
= isl_map_copy(access
->access
);
568 umap
= isl_union_map_from_map(map
);
569 umap
= isl_union_map_apply_domain(umap
,
570 isl_union_map_copy(sched
));
572 if (isl_union_map_is_empty(umap
)) {
573 isl_union_map_free(umap
);
577 map
= isl_map_from_union_map(umap
);
578 map
= isl_map_detect_equalities(map
);
580 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
583 group
->local_array
= local
;
584 group
->array
= local
->array
;
586 group
->write
= access
->write
;
587 group
->exact_write
= access
->exact_write
;
588 group
->slice
= access
->n_index
< local
->array
->n_index
;
589 group
->refs
= &local
->array
->refs
[i
];
598 /* If group->n_ref == 1, then group->refs was set by
599 * populate_array_references to point directly into
600 * group->array->refs and should not be freed.
601 * If group->n_ref > 1, then group->refs was set by join_groups
602 * to point to a newly allocated array.
604 struct gpu_array_ref_group
*gpu_array_ref_group_free(
605 struct gpu_array_ref_group
*group
)
609 gpu_array_tile_free(group
->shared_tile
);
610 gpu_array_tile_free(group
->private_tile
);
611 isl_map_free(group
->access
);
612 if (group
->n_ref
> 1)
618 /* Given a map where the input dimensions represent the tile loops,
619 * eliminate the innermost of those that have a fixed value
620 * until we reach one that does not (obviously) have a fixed value.
622 static __isl_give isl_map
*eliminate_fixed_inner_loops(
623 __isl_take isl_map
*access
)
627 n
= isl_map_dim(access
, isl_dim_in
);
629 for (i
= n
- 1; i
>= 0; --i
) {
630 if (!map_plain_is_fixed(access
, isl_dim_in
, i
))
632 access
= isl_map_eliminate(access
, isl_dim_in
, i
, 1);
637 /* Check if the access relations of group1 and group2 overlap within
638 * the innermost loop. In particular, ignore any inner dimension
639 * with a fixed value.
640 * The copying to and from shared memory will be performed within
641 * the innermost actual loop so we are only allowed to consider
642 * the dimensions up to that innermost loop while checking whether
643 * two access relations overlap.
645 static int accesses_overlap(struct gpu_array_ref_group
*group1
,
646 struct gpu_array_ref_group
*group2
)
649 isl_map
*access1
, *access2
;
651 access1
= isl_map_copy(group1
->access
);
652 access1
= eliminate_fixed_inner_loops(access1
);
653 access2
= isl_map_copy(group2
->access
);
654 access2
= eliminate_fixed_inner_loops(access2
);
655 access1
= isl_map_intersect(access1
, access2
);
656 empty
= isl_map_is_empty(access1
);
657 isl_map_free(access1
);
662 /* Combine the given two groups into a single group, containing
663 * the references of both groups.
665 static struct gpu_array_ref_group
*join_groups(
666 struct gpu_array_ref_group
*group1
,
667 struct gpu_array_ref_group
*group2
)
671 struct gpu_array_ref_group
*group
;
673 ctx
= isl_map_get_ctx(group1
->access
);
674 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
677 group
->local_array
= group1
->local_array
;
678 group
->array
= group1
->array
;
679 group
->access
= isl_map_union(isl_map_copy(group1
->access
),
680 isl_map_copy(group2
->access
));
681 group
->write
= group1
->write
|| group2
->write
;
682 group
->exact_write
= group1
->exact_write
&& group2
->exact_write
;
683 group
->slice
= group1
->slice
|| group2
->slice
;
684 group
->n_ref
= group1
->n_ref
+ group2
->n_ref
;
685 group
->refs
= isl_alloc_array(ctx
, struct gpu_stmt_access
*,
688 return gpu_array_ref_group_free(group
);
689 for (i
= 0; i
< group1
->n_ref
; ++i
)
690 group
->refs
[i
] = group1
->refs
[i
];
691 for (i
= 0; i
< group2
->n_ref
; ++i
)
692 group
->refs
[group1
->n_ref
+ i
] = group2
->refs
[i
];
697 /* Combine the given two groups into a single group and free
698 * the original two groups.
700 static struct gpu_array_ref_group
*join_groups_and_free(
701 struct gpu_array_ref_group
*group1
,
702 struct gpu_array_ref_group
*group2
)
704 struct gpu_array_ref_group
*group
;
706 group
= join_groups(group1
, group2
);
707 gpu_array_ref_group_free(group1
);
708 gpu_array_ref_group_free(group2
);
712 /* Report that the array reference group with the given access relation
713 * is not mapped to shared memory in the given kernel because
714 * it does not exhibit any reuse and is considered to be coalesced.
716 static void report_no_reuse_and_coalesced(struct ppcg_kernel
*kernel
,
717 __isl_keep isl_union_map
*access
)
722 ctx
= isl_union_map_get_ctx(access
);
723 p
= isl_printer_to_file(ctx
, stdout
);
724 p
= isl_printer_print_str(p
, "Array reference group ");
725 p
= isl_printer_print_union_map(p
, access
);
726 p
= isl_printer_print_str(p
,
727 " not considered for mapping to shared memory in kernel");
728 p
= isl_printer_print_int(p
, kernel
->id
);
729 p
= isl_printer_print_str(p
,
730 " because it exhibits no reuse and is considered to be coalesced");
731 p
= isl_printer_end_line(p
);
735 /* Given an access relation in terms of the gen->shared_len initial
736 * dimensions of the computed schedule and the thread identifiers
737 * (as parameters), check if the use of the corresponding private tile
738 * requires unrolling.
740 * If we are creating a private tile because we are forced to,
741 * then no unrolling is required.
742 * Otherwise we check if "access" is bijective and unrolling
743 * is required if it is not. Note that the access relation
744 * has already been determined to be bijective before the introduction
745 * of the thread identifiers and the removal of the schedule dimensions
746 * that are mapped to these threads. If the access relation is no longer
747 * bijective, then this means that more than one value of one of those
748 * schedule dimensions is mapped to the same thread and therefore
749 * unrolling is required.
751 static int check_requires_unroll(struct gpu_gen
*gen
,
752 __isl_keep isl_map
*access
, int force_private
)
758 bijective
= access_is_bijective(gen
, access
);
764 /* Compute the private and/or shared memory tiles for the array
765 * reference group "group" of array "array".
766 * Return 0 on success and -1 on error.
768 * If the array is a read-only scalar or if the user requested
769 * not to use shared or private memory, then we do not need to do anything.
771 * If any reference in the reference group accesses more than one element,
772 * then we would have to make sure that the layout in shared memory
773 * is the same as that in global memory. Since we do not handle this yet
774 * (and it may not even be possible), we refuse to map to private or
775 * shared memory in such cases.
777 * If the array group involves any may writes (that are not must writes),
778 * then we would have to make sure that we load the data into shared/private
779 * memory first in case the data is not written by the kernel
780 * (but still written back out to global memory).
781 * Since we don't have any such mechanism at the moment, we don't
782 * compute shared/private tiles for groups involving may writes.
784 * We only try to compute a shared memory tile if there is any reuse
785 * or if the access is not coalesced.
787 * For computing a private memory tile, we also require that there is
788 * some reuse. Moreover, we require that the access is private
789 * to the thread. That is, we check that any given array element
790 * is only accessed by a single thread.
791 * We compute an access relation that maps the shared tile loop iterators
792 * and the shared point loop iterators that will be wrapped over the
793 * threads to the array elements.
794 * We actually check that those iterators that will be wrapped
795 * partition the array space. This check is stricter than necessary
796 * since several iterations may be mapped onto the same thread
797 * and then they could be allowed to access the same memory elements,
798 * but our check does not allow this situation.
800 * We also check that the index expression only depends on parallel
801 * loops. That way, we can move those loops innermost and unroll them.
802 * Again, we use a test that is stricter than necessary.
803 * We actually check whether the index expression only depends
804 * on the iterators that are wrapped over the threads.
805 * These are necessarily parallel, but there may be more parallel loops.
807 * Combining the injectivity of the first test with the single-valuedness
808 * of the second test, we simply test for bijectivity.
810 * If the use of the private tile requires unrolling, but some
811 * of the other arrays are forcibly mapped to private memory,
812 * then we do not allow the use of this private tile since
813 * we cannot move the schedule dimensions that need to be unrolled down
814 * without performing some kind of expansion on those arrays
815 * that are forcibly mapped to private memory.
817 * If the array is marked force_private, then we bypass all checks
818 * and assume we can (and should) use registers.
820 * If it turns out we can (or have to) use registers, we compute
821 * the private memory tile size using can_tile, after introducing a dependence
822 * on the thread indices.
824 static int compute_group_bounds_core(struct gpu_gen
*gen
,
825 struct gpu_array_ref_group
*group
)
827 isl_ctx
*ctx
= isl_space_get_ctx(group
->array
->space
);
828 isl_union_map
*access
;
829 int n_index
= group
->array
->n_index
;
830 int no_reuse
, coalesced
;
832 int force_private
= group
->local_array
->force_private
;
833 int use_shared
= gen
->options
->use_shared_memory
&&
834 gen
->kernel
->n_block
> 0;
835 int use_private
= force_private
|| gen
->options
->use_private_memory
;
839 if (!use_shared
&& !use_private
)
841 if (gpu_array_is_read_only_scalar(group
->array
))
843 if (!force_private
&& !group
->exact_write
)
848 access
= gpu_array_ref_group_access_relation(group
, 1, 1);
849 no_reuse
= isl_union_map_is_injective(access
);
852 if (use_shared
&& no_reuse
)
853 coalesced
= access_is_coalesced(gen
, access
);
855 if (r
>= 0 && gen
->options
->debug
->verbose
&&
856 use_shared
&& no_reuse
&& coalesced
)
857 report_no_reuse_and_coalesced(gen
->kernel
, access
);
859 if (use_shared
&& (!no_reuse
|| !coalesced
)) {
860 group
->shared_tile
= gpu_array_tile_create(ctx
,
861 group
->array
->n_index
);
862 if (!group
->shared_tile
)
864 else if (!can_tile(group
->access
, group
->shared_tile
))
866 gpu_array_tile_free(group
->shared_tile
);
869 if (r
< 0 || (!force_private
&& (!use_private
|| no_reuse
))) {
870 isl_union_map_free(access
);
874 access
= isl_union_map_apply_domain(access
,
875 isl_union_map_copy(gen
->shared_sched
));
877 acc
= isl_map_from_union_map(access
);
879 if (!force_private
&& !access_is_bijective(gen
, acc
)) {
884 acc
= isl_map_apply_domain(acc
, isl_map_copy(gen
->privatization
));
885 requires_unroll
= check_requires_unroll(gen
, acc
, force_private
);
886 if (requires_unroll
< 0 ||
887 (requires_unroll
&& gen
->kernel
->any_force_private
)) {
889 return requires_unroll
< 0 ? -1 : 0;
892 group
->private_tile
= gpu_array_tile_create(gen
->ctx
, n_index
);
893 if (!group
->private_tile
) {
897 group
->private_tile
->requires_unroll
= requires_unroll
;
898 if (!can_tile(acc
, group
->private_tile
))
899 group
->private_tile
= gpu_array_tile_free(group
->private_tile
);
903 if (force_private
&& !group
->private_tile
)
904 isl_die(ctx
, isl_error_internal
,
905 "unable to map array reference group to registers",
911 /* Compute the private and/or shared memory tiles for the array
912 * reference group "group" of array "array" and set last_shared.
913 * Return 0 on success and -1 on error.
915 static int compute_group_bounds(struct gpu_gen
*gen
,
916 struct gpu_array_ref_group
*group
)
920 if (compute_group_bounds_core(gen
, group
) < 0)
922 set_last_shared(gen
, group
);
927 /* If two groups have overlapping access relations (as determined by
928 * the "overlap" function) and if one of them involves a write,
929 * then merge the two groups into one.
930 * If "compute_bounds" is set, then call compute_group_bounds
931 * on the merged groups.
933 * Return the updated number of groups.
934 * Return -1 on error.
936 static int group_writes(struct gpu_gen
*gen
,
937 int n
, struct gpu_array_ref_group
**groups
,
938 int (*overlap
)(struct gpu_array_ref_group
*group1
,
939 struct gpu_array_ref_group
*group2
), int compute_bounds
)
943 for (i
= 0; i
< n
; ++i
) {
944 for (j
= n
- 1; j
> i
; --j
) {
945 if (!groups
[i
]->write
&& !groups
[j
]->write
)
948 if (!overlap(groups
[i
], groups
[j
]))
951 groups
[i
] = join_groups_and_free(groups
[i
], groups
[j
]);
953 groups
[j
] = groups
[n
- 1];
954 groups
[n
- 1] = NULL
;
959 if (compute_bounds
&&
960 compute_group_bounds(gen
, groups
[i
]) < 0)
968 /* If two groups have overlapping access relations (within the innermost
969 * loop) and if one of them involves a write, then merge the two groups
972 * Return the updated number of groups.
974 static int group_overlapping_writes(struct gpu_gen
*gen
,
975 int n
, struct gpu_array_ref_group
**groups
)
977 return group_writes(gen
, n
, groups
, &accesses_overlap
, 0);
980 /* Check if the access relations of group1 and group2 overlap within
981 * the outermost min(group1->last_shared, group2->last_shared) loops.
983 static int last_shared_accesses_overlap(struct gpu_array_ref_group
*group1
,
984 struct gpu_array_ref_group
*group2
)
989 isl_map
*map_i
, *map_j
, *map
;
991 last_shared
= group1
->last_shared
;
992 if (group2
->last_shared
< last_shared
)
993 last_shared
= group2
->last_shared
;
994 map_i
= isl_map_copy(group1
->access
);
995 dim
= isl_map_dim(map_i
, isl_dim_in
);
996 map_i
= isl_map_eliminate(map_i
, isl_dim_in
,
997 last_shared
+ 1, dim
- (last_shared
+ 1));
998 map_j
= isl_map_copy(group2
->access
);
999 map_j
= isl_map_eliminate(map_j
, isl_dim_in
,
1000 last_shared
+ 1, dim
- (last_shared
+ 1));
1001 map
= isl_map_intersect(map_i
, map_j
);
1002 empty
= isl_map_is_empty(map
);
1008 /* If two groups have overlapping access relations (within the outer
1009 * last_shared loops) and if one of them involves a write,
1010 * then merge the two groups into one.
1012 * Return the updated number of groups.
1014 static int group_last_shared_overlapping_writes(struct gpu_gen
*gen
, int n
,
1015 struct gpu_array_ref_group
**groups
)
1017 return group_writes(gen
, n
, groups
, &last_shared_accesses_overlap
, 1);
1020 /* Is the size of the tile specified by "tile" smaller than the sum of
1021 * the sizes of the tiles specified by "tile1" and "tile2"?
1023 static int smaller_tile(struct gpu_array_tile
*tile
,
1024 struct gpu_array_tile
*tile1
, struct gpu_array_tile
*tile2
)
1027 isl_val
*size
, *size1
, *size2
;
1029 size
= gpu_array_tile_size(tile
);
1030 size1
= gpu_array_tile_size(tile1
);
1031 size2
= gpu_array_tile_size(tile2
);
1033 size
= isl_val_sub(size
, size1
);
1034 size
= isl_val_sub(size
, size2
);
1035 smaller
= isl_val_is_neg(size
);
1042 /* Given an initial grouping of array references and shared memory tiles
1043 * for each group that allows for a shared memory tile, merge two groups
1044 * if both have a shared memory tile, the merged group also has
1045 * a shared memory tile and the size of the tile for the merge group
1046 * is smaller than the sum of the tile sizes of the individual groups.
1048 * If merging two groups decreases the "last_shared" dimension of
1049 * one or both of the two groups, then we need to check for overlapping
1052 * Return the number of groups after merging.
1053 * Return -1 on error.
1055 static int group_common_shared_memory_tile(struct gpu_gen
*gen
,
1056 struct gpu_array_info
*array
, int n
,
1057 struct gpu_array_ref_group
**groups
)
1060 int recompute_overlap
= 0;
1061 isl_ctx
*ctx
= isl_space_get_ctx(array
->space
);
1063 for (i
= 0; i
< n
; ++i
) {
1064 if (!groups
[i
]->shared_tile
)
1066 for (j
= n
- 1; j
> i
; --j
) {
1069 struct gpu_array_ref_group
*group
;
1071 if (!groups
[j
]->shared_tile
)
1074 map
= isl_map_intersect(isl_map_copy(groups
[i
]->access
),
1075 isl_map_copy(groups
[j
]->access
));
1076 empty
= isl_map_is_empty(map
);
1082 group
= join_groups(groups
[i
], groups
[j
]);
1083 if (compute_group_bounds(gen
, group
) < 0) {
1084 gpu_array_ref_group_free(group
);
1087 if (!group
->shared_tile
||
1088 !smaller_tile(group
->shared_tile
,
1089 groups
[i
]->shared_tile
,
1090 groups
[j
]->shared_tile
)) {
1091 gpu_array_ref_group_free(group
);
1095 if (group
->last_shared
< groups
[i
]->last_shared
||
1096 group
->last_shared
< groups
[j
]->last_shared
)
1097 recompute_overlap
= 1;
1098 gpu_array_ref_group_free(groups
[i
]);
1099 gpu_array_ref_group_free(groups
[j
]);
1102 groups
[j
] = groups
[n
- 1];
1107 if (recompute_overlap
)
1108 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
1112 /* Set array->n_group and array->groups to n and groups.
1114 * Additionally, set the "nr" field of each group.
1116 static void set_array_groups(struct gpu_local_array_info
*array
,
1117 int n
, struct gpu_array_ref_group
**groups
)
1122 array
->groups
= groups
;
1124 for (i
= 0; i
< n
; ++i
)
1128 /* Group array references that should be considered together when
1129 * deciding whether to access them from private, shared or global memory.
1130 * Return -1 on error.
1132 * In particular, if two array references overlap and if one of them
1133 * is a write, then the two references are grouped together.
1134 * We first perform an initial grouping based only on the access relation.
1135 * After computing shared and private memory tiles, we check for
1136 * overlapping writes again, but this time taking into account
1137 * the "last_shared" property.
1139 * Furthermore, if two groups admit a shared memory tile and if the
1140 * combination of the two also admits a shared memory tile, we merge
1143 * If the array contains structures, then there is no need to compute
1144 * reference groups since we do not map such arrays to private or shared
1147 static int group_array_references(struct gpu_gen
*gen
,
1148 struct gpu_local_array_info
*local
, __isl_keep isl_union_map
*sched
)
1152 isl_ctx
*ctx
= isl_union_map_get_ctx(sched
);
1153 struct gpu_array_ref_group
**groups
;
1155 if (local
->array
->has_compound_element
)
1158 groups
= isl_calloc_array(ctx
, struct gpu_array_ref_group
*,
1159 local
->array
->n_ref
);
1163 n
= populate_array_references(local
, sched
, groups
);
1165 n
= group_overlapping_writes(gen
, n
, groups
);
1167 for (i
= 0; i
< n
; ++i
)
1168 if (compute_group_bounds(gen
, groups
[i
]) < 0)
1171 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
1173 n
= group_common_shared_memory_tile(gen
, local
->array
, n
, groups
);
1175 set_array_groups(local
, n
, groups
);
1180 for (i
= 0; i
< local
->array
->n_ref
; ++i
)
1181 gpu_array_ref_group_free(groups
[i
]);
1185 /* For each scalar in the input program, check if there are any
1186 * order dependences active inside the current kernel, within
1187 * the same iteration of the host schedule.
1188 * If so, mark the scalar as force_private so that it will be
1189 * mapped to a register.
1191 static void check_scalar_live_ranges(struct gpu_gen
*gen
)
1195 isl_union_map
*sched
;
1196 isl_union_set
*domain
;
1197 isl_union_map
*same_host_iteration
;
1199 gen
->kernel
->any_force_private
= 0;
1201 if (!gen
->options
->live_range_reordering
)
1204 sched
= gen
->shared_sched
;
1205 sched
= isl_union_map_universe(isl_union_map_copy(sched
));
1206 domain
= isl_union_map_domain(sched
);
1208 sched
= isl_union_map_copy(gen
->sched
);
1209 proj
= projection(isl_union_map_get_space(sched
),
1210 gen
->untiled_len
, gen
->tile_first
);
1211 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
1212 same_host_iteration
= isl_union_map_apply_range(sched
,
1213 isl_union_map_reverse(isl_union_map_copy(sched
)));
1215 for (i
= 0; i
< gen
->kernel
->n_array
; ++i
) {
1216 struct gpu_local_array_info
*local
= &gen
->kernel
->array
[i
];
1217 isl_union_map
*order
;
1219 local
->force_private
= 0;
1220 if (local
->array
->n_index
!= 0)
1222 order
= isl_union_map_copy(local
->array
->dep_order
);
1223 order
= isl_union_map_intersect_domain(order
,
1224 isl_union_set_copy(domain
));
1225 order
= isl_union_map_intersect_range(order
,
1226 isl_union_set_copy(domain
));
1227 order
= isl_union_map_intersect(order
,
1228 isl_union_map_copy(same_host_iteration
));
1229 if (!isl_union_map_is_empty(order
)) {
1230 local
->force_private
= 1;
1231 gen
->kernel
->any_force_private
= 1;
1233 isl_union_map_free(order
);
1236 isl_union_map_free(same_host_iteration
);
1237 isl_union_set_free(domain
);
1240 /* Group references of all arrays in the current kernel.
1242 int gpu_group_references(struct gpu_gen
*gen
)
1246 isl_union_map
*sched
;
1248 check_scalar_live_ranges(gen
);
1250 sched
= isl_union_map_apply_range(isl_union_map_copy(gen
->shared_sched
),
1251 isl_union_map_copy(gen
->shared_proj
));
1253 for (i
= 0; i
< gen
->kernel
->n_array
; ++i
) {
1254 r
= group_array_references(gen
, &gen
->kernel
->array
[i
], sched
);
1259 isl_union_map_free(sched
);
1264 /* Given a description of an array tile "tile" and the "space"
1268 * where D represents the first shared_len schedule dimensions
1269 * and A represents the array, construct an isl_multi_aff
1271 * { [D[i] -> A[a]] -> A'[a'] }
1273 * with A' a scaled down copy of A according to the shifts and strides
1274 * in "tile". In particular,
1276 * a' = (a + shift(i))/stride
1278 * "insert_array" represents
1282 * and is used to insert A into the domain of functions that only
1285 static __isl_give isl_multi_aff
*strided_tile(
1286 struct gpu_array_tile
*tile
, __isl_keep isl_space
*space
,
1287 __isl_keep isl_multi_aff
*insert_array
)
1291 isl_multi_aff
*shift
;
1292 isl_multi_val
*stride
;
1294 isl_local_space
*ls
;
1295 isl_multi_aff
*tiling
;
1297 ctx
= isl_space_get_ctx(space
);
1298 space2
= isl_space_domain(isl_space_copy(space
));
1299 ls
= isl_local_space_from_space(space2
);
1300 space2
= isl_space_range(isl_space_copy(space
));
1301 stride
= isl_multi_val_zero(space2
);
1302 shift
= isl_multi_aff_zero(isl_space_copy(space
));
1304 for (i
= 0; i
< tile
->n
; ++i
) {
1305 struct gpu_array_bound
*bound
= &tile
->bound
[i
];
1309 if (tile
->bound
[i
].shift
) {
1310 stride_i
= isl_val_copy(bound
->stride
);
1311 shift_i
= isl_aff_copy(bound
->shift
);
1313 stride_i
= isl_val_one(ctx
);
1314 shift_i
= isl_aff_zero_on_domain(
1315 isl_local_space_copy(ls
));
1318 stride
= isl_multi_val_set_val(stride
, i
, stride_i
);
1319 shift
= isl_multi_aff_set_aff(shift
, i
, shift_i
);
1321 isl_local_space_free(ls
);
1323 shift
= isl_multi_aff_pullback_multi_aff(shift
,
1324 isl_multi_aff_copy(insert_array
));
1326 tiling
= isl_multi_aff_range_map(isl_space_copy(space
));
1327 tiling
= isl_multi_aff_add(tiling
, shift
);
1328 tiling
= isl_multi_aff_scale_down_multi_val(tiling
, stride
);
1333 /* Compute a tiling for the array reference group "group".
1335 * The tiling is of the form
1337 * { [D[i] -> A[a]] -> T[t] }
1339 * where D represents the first shared_len schedule dimensions,
1340 * A represents the global array and T represents the shared or
1341 * private memory tile. The name of T is the name of the local
1344 * If there is any stride in the accesses, then the mapping is
1346 * t = (a + shift(i))/stride - lb(i)
1348 * otherwise, it is simply
1352 void gpu_array_ref_group_compute_tiling(struct gpu_array_ref_group
*group
)
1355 struct gpu_array_tile
*tile
;
1356 struct gpu_array_info
*array
= group
->array
;
1358 isl_multi_aff
*tiling
, *lb
, *insert_array
;
1362 tile
= group
->private_tile
;
1364 tile
= group
->shared_tile
;
1368 space
= isl_map_get_space(group
->access
);
1369 insert_array
= isl_multi_aff_domain_map(isl_space_copy(space
));
1371 for (i
= 0; i
< tile
->n
; ++i
)
1372 if (tile
->bound
[i
].shift
)
1376 tiling
= strided_tile(tile
, space
, insert_array
);
1378 tiling
= isl_multi_aff_range_map(isl_space_copy(space
));
1380 lb
= isl_multi_aff_zero(space
);
1381 for (i
= 0; i
< tile
->n
; ++i
) {
1382 isl_aff
*lb_i
= isl_aff_copy(tile
->bound
[i
].lb
);
1383 lb
= isl_multi_aff_set_aff(lb
, i
, lb_i
);
1385 lb
= isl_multi_aff_pullback_multi_aff(lb
, insert_array
);
1387 tiling
= isl_multi_aff_sub(tiling
, lb
);
1389 p
= isl_printer_to_str(isl_multi_aff_get_ctx(tiling
));
1390 p
= gpu_array_ref_group_print_name(group
, p
);
1391 local_name
= isl_printer_get_str(p
);
1392 isl_printer_free(p
);
1393 tiling
= isl_multi_aff_set_tuple_name(tiling
, isl_dim_out
, local_name
);
1396 tile
->tiling
= tiling
;