2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 #include <isl/polynomial.h>
18 #include <isl/union_set.h>
22 #include <isl/schedule.h>
23 #include <isl/schedule_node.h>
24 #include <isl/options.h>
25 #include <isl/ast_build.h>
29 #include "gpu_array_tile.h"
30 #include "gpu_group.h"
33 #include "ppcg_options.h"
37 struct gpu_array_info
;
39 /* Return the name of the outer array (of structs) accessed by "access".
41 static const char *get_outer_array_name(__isl_keep isl_map
*access
)
46 space
= isl_space_range(isl_map_get_space(access
));
47 while (space
&& isl_space_is_wrapping(space
))
48 space
= isl_space_domain(isl_space_unwrap(space
));
49 name
= isl_space_get_tuple_name(space
, isl_dim_set
);
50 isl_space_free(space
);
55 /* Collect all references to the given array and store pointers to them
58 static void collect_references(struct gpu_prog
*prog
,
59 struct gpu_array_info
*array
)
65 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
66 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
67 struct gpu_stmt_access
*access
;
69 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
71 name
= get_outer_array_name(access
->access
);
72 if (name
&& !strcmp(array
->name
, name
))
78 array
->refs
= isl_alloc_array(prog
->ctx
, struct gpu_stmt_access
*, n
);
82 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
83 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
84 struct gpu_stmt_access
*access
;
86 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
88 name
= get_outer_array_name(access
->access
);
89 if (!name
|| strcmp(array
->name
, name
))
92 array
->refs
[n
++] = access
;
97 /* Compute and return the extent of "array", taking into account the set of
100 * In particular, the extent in the outer dimension is taken
101 * from "accessed", while the extents in the remaining dimensions
102 * are taken from array->extent.
104 * The extent in the outer dimension cannot be taken from array->extent
105 * because that may be unbounded. Furthermore, even if it is bounded,
106 * it may be larger than the piece of the array that is being accessed.
108 static __isl_give isl_set
*compute_extent(struct pet_array
*array
,
109 __isl_keep isl_set
*accessed
)
116 extent
= isl_set_copy(array
->extent
);
118 n_index
= isl_set_dim(accessed
, isl_dim_set
);
122 extent
= isl_set_project_out(extent
, isl_dim_set
, 0, 1);
123 outer
= isl_set_copy(accessed
);
124 outer
= isl_set_project_out(outer
, isl_dim_set
, 1, n_index
- 1);
125 extent
= isl_set_flat_product(outer
, extent
);
126 id
= isl_set_get_tuple_id(accessed
);
127 extent
= isl_set_set_tuple_id(extent
, id
);
132 /* Is the array "array" being extracted a read-only scalar?
134 * That is, is "array" a scalar that is never possibly written to.
135 * An array containing structures is never considered to be a scalar.
137 static int is_read_only_scalar(struct gpu_array_info
*array
,
138 struct gpu_prog
*prog
)
141 isl_union_map
*write
;
144 if (array
->has_compound_element
)
146 if (array
->n_index
!= 0)
149 write
= isl_union_map_copy(prog
->may_write
);
150 space
= isl_set_universe(isl_space_copy(array
->space
));
151 write
= isl_union_map_intersect_range(write
,
152 isl_union_set_from_set(space
));
153 empty
= isl_union_map_is_empty(write
);
154 isl_union_map_free(write
);
159 /* Compute bounds on the host array "pa" based on the corresponding
160 * accessed elements in "arrays"
161 * and collect all references to the array.
162 * Store the results in "info".
164 * If the array is zero-dimensional and does not contain structures,
165 * i.e., if the array is a scalar, we check whether it is read-only.
166 * We also check whether the array is accessed at all.
168 static int extract_array_info(struct gpu_prog
*prog
,
169 struct gpu_array_info
*info
, struct pet_array
*pa
,
170 __isl_keep isl_union_set
*arrays
)
176 isl_set
*accessed
, *extent
;
178 n_index
= isl_set_dim(pa
->extent
, isl_dim_set
);
179 name
= isl_set_get_tuple_name(pa
->extent
);
180 bounds
= isl_alloc_array(prog
->ctx
, isl_pw_aff
*, n_index
);
184 info
->space
= isl_set_get_space(pa
->extent
);
185 info
->name
= strdup(name
);
186 info
->n_index
= n_index
;
187 info
->bound
= bounds
;
188 info
->linearize
= prog
->scop
->options
->linearize_device_arrays
;
190 info
->type
= strdup(pa
->element_type
);
191 info
->size
= pa
->element_size
;
192 info
->local
= pa
->declared
&& !pa
->exposed
;
193 info
->has_compound_element
= pa
->element_is_record
;
194 info
->read_only_scalar
= is_read_only_scalar(info
, prog
);
196 accessed
= isl_union_set_extract_set(arrays
,
197 isl_space_copy(info
->space
));
198 empty
= isl_set_is_empty(accessed
);
199 extent
= compute_extent(pa
, accessed
);
200 isl_set_free(accessed
);
201 info
->extent
= extent
;
204 info
->accessed
= !empty
;
205 for (i
= 0; i
< n_index
; ++i
) {
211 dom
= isl_set_copy(extent
);
212 dom
= isl_set_project_out(dom
, isl_dim_set
, i
+ 1,
214 dom
= isl_set_project_out(dom
, isl_dim_set
, 0, i
);
215 if (!isl_set_dim_has_upper_bound(dom
, isl_dim_set
, 0)) {
216 fprintf(stderr
, "unable to determine extent of '%s' "
217 "in dimension %d\n", info
->name
, i
);
218 dom
= isl_set_free(dom
);
220 bound
= isl_set_dim_max(dom
, 0);
221 dom
= isl_pw_aff_domain(isl_pw_aff_copy(bound
));
222 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
223 one
= isl_aff_zero_on_domain(ls
);
224 one
= isl_aff_add_constant_si(one
, 1);
225 bound
= isl_pw_aff_add(bound
, isl_pw_aff_alloc(dom
, one
));
226 bound
= isl_pw_aff_gist(bound
, isl_set_copy(prog
->context
));
229 if (!isl_pw_aff_is_cst(bound
))
233 collect_references(prog
, info
);
238 /* Remove independence from the order constraints "order" on array "array".
239 * Since the pairs of iterations in the filter relation of an independence
240 * are guaranteed to be completely independent by the user, there is
241 * no need to ensure that live ranges are ordered along thong pairs.
242 * We make an exception for local variables, though, as the independence
243 * guarantee does not apply to those.
245 * The order constraints are used in two places.
246 * Those on scalars are used in check_scalar_live_ranges to check if
247 * we need to force the scalar to be private. Any non-local scalar
248 * should not be forced scalar if it only appears in independent loops.
249 * Those on non-scalars are added to the coincidence constraints
250 * in compute_schedule because we do not support any array expansion.
251 * Accesses to non-local arrays should not prevent a loop from being
252 * considered coincident so we should indeed remove those constraints
253 * from the order constraints.
255 static __isl_give isl_union_map
*remove_independences(struct gpu_prog
*prog
,
256 struct gpu_array_info
*array
, __isl_take isl_union_map
*order
)
260 for (i
= 0; i
< prog
->scop
->pet
->n_independence
; ++i
) {
261 struct pet_independence
*pi
= prog
->scop
->pet
->independences
[i
];
262 if (isl_union_set_contains(pi
->local
, array
->space
))
265 order
= isl_union_map_subtract(order
,
266 isl_union_map_copy(pi
->filter
));
272 /* For each array in "prog", store the (untagged) order dependences
273 * derived from the array in array->dep_order.
274 * In particular, consider all references that access the given array
275 * and take the order dependences that have one of these references
276 * as source. (Since an order dependence relates two references to
277 * the same array, the target of these order dependences will also
278 * be one of these references.)
279 * Additionally, store the union of these array->dep_order relations
280 * for all non-scalar arrays in prog->array_order.
282 void collect_order_dependences(struct gpu_prog
*prog
)
286 isl_union_map
*accesses
;
288 space
= isl_union_map_get_space(prog
->read
);
289 prog
->array_order
= isl_union_map_empty(space
);
291 accesses
= isl_union_map_copy(prog
->scop
->tagged_reads
);
292 accesses
= isl_union_map_union(accesses
,
293 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
294 accesses
= isl_union_map_universe(accesses
);
295 accesses
= isl_union_map_apply_range(accesses
,
296 isl_union_map_copy(prog
->to_outer
));
298 for (i
= 0; i
< prog
->n_array
; ++i
) {
299 struct gpu_array_info
*array
= &prog
->array
[i
];
302 isl_union_map
*order
;
304 set
= isl_set_universe(isl_space_copy(array
->space
));
305 uset
= isl_union_set_from_set(set
);
306 uset
= isl_union_map_domain(
307 isl_union_map_intersect_range(isl_union_map_copy(accesses
),
309 order
= isl_union_map_copy(prog
->scop
->tagged_dep_order
);
310 order
= isl_union_map_intersect_domain(order
, uset
);
311 order
= isl_union_map_zip(order
);
312 order
= isl_union_set_unwrap(isl_union_map_domain(order
));
313 order
= remove_independences(prog
, array
, order
);
314 array
->dep_order
= order
;
316 if (gpu_array_is_scalar(array
) && !array
->has_compound_element
)
319 prog
->array_order
= isl_union_map_union(prog
->array_order
,
320 isl_union_map_copy(array
->dep_order
));
323 isl_union_map_free(accesses
);
326 /* Construct a gpu_array_info for each array referenced by prog->scop and
327 * collect them in prog->array.
329 * The sizes are based on the extents and the set of possibly accessed
330 * elements by "prog".
331 * If there are any member accesses involved, then they are first mapped
332 * to the outer arrays of structs.
334 * If we are allowing live range reordering, then also set
335 * the dep_order field. Otherwise leave it NULL.
337 static int collect_array_info(struct gpu_prog
*prog
)
341 isl_union_set
*arrays
;
343 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
344 arrays
= isl_union_set_union(arrays
,
345 isl_union_map_range(isl_union_map_copy(prog
->may_write
)));
347 arrays
= isl_union_set_apply(arrays
,
348 isl_union_map_copy(prog
->to_outer
));
350 arrays
= isl_union_set_coalesce(arrays
);
352 prog
->n_array
= prog
->scop
->pet
->n_array
;
353 prog
->array
= isl_calloc_array(prog
->ctx
,
354 struct gpu_array_info
, prog
->n_array
);
356 for (i
= 0; i
< prog
->scop
->pet
->n_array
; ++i
)
357 if (extract_array_info(prog
, &prog
->array
[i
],
358 prog
->scop
->pet
->arrays
[i
], arrays
) < 0)
361 isl_union_set_free(arrays
);
363 if (prog
->scop
->options
->live_range_reordering
)
364 collect_order_dependences(prog
);
369 static void free_array_info(struct gpu_prog
*prog
)
373 for (i
= 0; i
< prog
->n_array
; ++i
) {
374 int n_index
= prog
->array
[i
].n_index
;
375 free(prog
->array
[i
].type
);
376 free(prog
->array
[i
].name
);
377 for (j
= 0; j
< n_index
; ++j
)
378 isl_pw_aff_free(prog
->array
[i
].bound
[j
]);
379 isl_space_free(prog
->array
[i
].space
);
380 isl_set_free(prog
->array
[i
].extent
);
381 free(prog
->array
[i
].bound
);
382 free(prog
->array
[i
].refs
);
383 isl_union_map_free(prog
->array
[i
].dep_order
);
388 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
389 * as an array or through a pointer reference, but as a single data element.
390 * At the moment, scalars are represented as zero-dimensional arrays.
391 * Note that the single data element may be an entire structure.
393 int gpu_array_is_scalar(struct gpu_array_info
*array
)
395 return array
->n_index
== 0;
398 /* Is "array" a read-only scalar?
400 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
402 return array
->read_only_scalar
;
405 /* Does "array" need to be allocated on the device?
406 * If it is a read-only scalar, then it will be passed as an argument
407 * to the kernel and therefore does not require any allocation.
408 * If this device memory is not accessed at all, then it does not
409 * need to be allocated either.
411 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
)
413 if (gpu_array_is_read_only_scalar(array
))
420 /* Return the set of parameter values for which the array has a positive
421 * size in all dimensions.
422 * If the sizes are only valid for some parameter values, then those
423 * constraints are also taken into account.
425 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
)
434 space
= isl_space_params(isl_space_copy(array
->space
));
435 guard
= isl_set_universe(space
);
437 for (i
= 0; i
< array
->n_index
; ++i
) {
439 isl_set
*guard_i
, *zero
;
441 bound
= isl_pw_aff_copy(array
->bound
[i
]);
442 guard_i
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound
));
443 zero
= isl_pw_aff_zero_set(bound
);
444 guard_i
= isl_set_subtract(guard_i
, zero
);
445 guard
= isl_set_intersect(guard
, guard_i
);
451 /* Internal data structure for extract_size_of_type.
452 * "type" specifies the name of the space that we want to extract.
453 * "res" is used to store the subset of that space.
455 struct ppcg_extract_size_data
{
460 /* This function is called for each set in a union_set.
461 * If the name of the set matches data->type, we store the
464 static isl_stat
extract_size_of_type(__isl_take isl_set
*size
, void *user
)
466 struct ppcg_extract_size_data
*data
= user
;
469 name
= isl_set_get_tuple_name(size
);
470 if (name
&& !strcmp(name
, data
->type
)) {
472 return isl_stat_error
;
479 /* Given a union map { kernel[i] -> *[...] },
480 * return the range in the space called "type" for the kernel with
481 * sequence number "id".
483 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
484 const char *type
, int id
)
488 isl_union_set
*local_sizes
;
489 struct ppcg_extract_size_data data
= { type
, NULL
};
494 space
= isl_union_map_get_space(sizes
);
495 space
= isl_space_set_from_params(space
);
496 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
497 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
498 dom
= isl_set_universe(space
);
499 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
501 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
502 isl_union_map_copy(sizes
));
503 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
504 isl_union_set_free(local_sizes
);
508 /* Given a singleton set, extract the first (at most *len) elements
509 * of the single integer tuple into *sizes and update *len if needed.
511 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
519 dim
= isl_set_dim(set
, isl_dim_set
);
523 for (i
= 0; i
< *len
; ++i
) {
526 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
529 sizes
[i
] = isl_val_get_num_si(v
);
536 /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
537 * if the option debug->dump_sizes is set.
539 static void set_used_sizes(struct gpu_gen
*gen
, const char *type
, int id
,
546 if (!gen
->options
->debug
->dump_sizes
)
549 space
= isl_union_map_get_space(gen
->used_sizes
);
550 space
= isl_space_set_from_params(space
);
551 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
552 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
553 space
= isl_space_from_domain(space
);
554 space
= isl_space_add_dims(space
, isl_dim_out
, len
);
555 space
= isl_space_set_tuple_name(space
, isl_dim_out
, type
);
557 map
= isl_map_universe(space
);
558 map
= isl_map_fix_si(map
, isl_dim_in
, 0, id
);
559 for (i
= 0; i
< len
; ++i
)
560 map
= isl_map_fix_si(map
, isl_dim_out
, i
, sizes
[i
]);
562 gen
->used_sizes
= isl_union_map_add_map(gen
->used_sizes
, map
);
565 /* Extract user specified "tile" sizes from the "sizes" command line option,
566 * defaulting to option->tile_size in each dimension.
567 * *tile_len contains the maximum number of tile sizes needed.
568 * Update *tile_len to the number of specified tile sizes, if any, and
569 * return a pointer to the tile sizes (or NULL on error).
570 * Add the effectively used sizes to gen->used_sizes.
572 static int *read_tile_sizes(struct gpu_gen
*gen
, int *tile_len
)
578 tile_size
= isl_alloc_array(gen
->ctx
, int, *tile_len
);
581 for (n
= 0; n
< *tile_len
; ++n
)
582 tile_size
[n
] = gen
->options
->tile_size
;
584 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
585 read_sizes_from_set(size
, tile_size
, tile_len
);
586 set_used_sizes(gen
, "tile", gen
->kernel_id
, tile_size
, *tile_len
);
591 /* Extract user specified "block" sizes from the "sizes" command line option,
592 * after filling in some potentially useful defaults.
594 static void read_block_sizes(struct ppcg_kernel
*kernel
,
595 __isl_keep isl_union_map
*sizes
)
599 if (kernel
->n_block
> 3)
601 switch (kernel
->n_block
) {
603 kernel
->block_dim
[0] = 512;
606 kernel
->block_dim
[0] = 32;
607 kernel
->block_dim
[1] = 16;
610 kernel
->block_dim
[0] = 32;
611 kernel
->block_dim
[1] = 4;
612 kernel
->block_dim
[2] = 4;
616 size
= extract_sizes(sizes
, "block", kernel
->id
);
617 read_sizes_from_set(size
, kernel
->block_dim
, &kernel
->n_block
);
620 /* Extract user specified "grid" sizes from the "sizes" command line option,
621 * after filling in some potentially useful defaults.
623 static void read_grid_sizes(struct ppcg_kernel
*kernel
,
624 __isl_keep isl_union_map
*sizes
)
628 if (kernel
->n_grid
> 2)
630 switch (kernel
->n_grid
) {
632 kernel
->grid_dim
[0] = 32768;
635 kernel
->grid_dim
[0] = 256;
636 kernel
->grid_dim
[1] = 256;
640 size
= extract_sizes(sizes
, "grid", kernel
->id
);
641 read_sizes_from_set(size
, kernel
->grid_dim
, &kernel
->n_grid
);
644 /* Extract user specified grid and block sizes from the gen->sizes
645 * command line option after filling in some potentially useful defaults.
646 * Store the extracted sizes in "kernel".
647 * Add the effectively used sizes to gen->used_sizes.
649 static void read_grid_and_block_sizes(struct ppcg_kernel
*kernel
,
652 read_block_sizes(kernel
, gen
->sizes
);
653 read_grid_sizes(kernel
, gen
->sizes
);
654 set_used_sizes(gen
, "block", kernel
->id
,
655 kernel
->block_dim
, kernel
->n_block
);
656 set_used_sizes(gen
, "grid", kernel
->id
,
657 kernel
->grid_dim
, kernel
->n_grid
);
660 static void *free_stmts(struct gpu_stmt
*stmts
, int n
)
667 for (i
= 0; i
< n
; ++i
) {
668 struct gpu_stmt_access
*access
, *next
;
670 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
672 isl_id_free(access
->ref_id
);
673 isl_map_free(access
->access
);
674 isl_map_free(access
->tagged_access
);
678 isl_id_free(stmts
[i
].id
);
685 /* Add parameters p[i] with identifiers "ids" to "set",
686 * with bounds to 0 <= p[i] < size[i].
688 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
689 int *size
, __isl_keep isl_id_list
*ids
)
694 len
= isl_id_list_n_id(ids
);
695 nparam
= isl_set_dim(set
, isl_dim_param
);
696 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
698 for (i
= 0; i
< len
; ++i
) {
701 id
= isl_id_list_get_id(ids
, i
);
702 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
703 set
= isl_set_lower_bound_si(set
, isl_dim_param
, nparam
+ i
, 0);
704 set
= isl_set_upper_bound_si(set
, isl_dim_param
,
705 nparam
+ i
, size
[i
] - 1);
711 /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
714 * { : 0 <= p[i] < size[i] }
716 * or an overapproximation.
718 static __isl_give isl_set
*add_bounded_parameters_dynamic(
719 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
720 __isl_keep isl_id_list
*ids
)
727 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
728 nparam
= isl_set_dim(set
, isl_dim_param
);
729 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
731 for (i
= 0; i
< len
; ++i
) {
734 id
= isl_id_list_get_id(ids
, i
);
735 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
738 space
= isl_space_params(isl_set_get_space(set
));
739 ls
= isl_local_space_from_space(space
);
740 for (i
= 0; i
< len
; ++i
) {
741 isl_pw_aff
*param
, *size_i
, *zero
;
744 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
745 isl_dim_param
, nparam
+ i
);
747 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
748 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
749 bound
= isl_set_from_basic_set(isl_set_simple_hull(bound
));
750 set
= isl_set_intersect_params(set
, bound
);
752 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
753 bound
= isl_pw_aff_ge_set(param
, zero
);
754 set
= isl_set_intersect_params(set
, bound
);
756 isl_local_space_free(ls
);
761 /* Return the union of all tagged access relations in the group.
763 static __isl_give isl_union_map
*group_tagged_access_relation(
764 struct gpu_array_ref_group
*group
)
767 isl_union_map
*access
;
769 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
770 for (i
= 0; i
< group
->n_ref
; ++i
) {
773 map_i
= isl_map_copy(group
->refs
[i
]->tagged_access
);
774 access
= isl_union_map_union(access
,
775 isl_union_map_from_map(map_i
));
781 /* Return the extent of "array", recomputed from the bounds.
782 * The recomputed extent may be simpler than the original extent.
784 static __isl_give isl_set
*array_extent(struct gpu_array_info
*array
)
792 id
= isl_set_get_tuple_id(array
->extent
);
793 space
= isl_set_get_space(array
->extent
);
794 extent
= isl_set_universe(isl_space_copy(space
));
795 ls
= isl_local_space_from_space(space
);
796 for (i
= 0; i
< array
->n_index
; ++i
) {
802 extent
= isl_set_lower_bound_si(extent
, isl_dim_set
, i
, 0);
804 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
806 index
= isl_pw_aff_from_aff(aff
);
807 bound
= isl_pw_aff_copy(array
->bound
[i
]);
808 bound
= isl_pw_aff_from_range(bound
);
809 bound
= isl_pw_aff_add_dims(bound
, isl_dim_in
, array
->n_index
);
810 bound
= isl_pw_aff_set_tuple_id(bound
, isl_dim_in
,
812 lt
= isl_pw_aff_lt_set(index
, bound
);
813 extent
= isl_set_intersect(extent
, lt
);
815 isl_local_space_free(ls
);
821 /* Return a map from the first group->shared_tile->depth dimensions
822 * of the computed schedule to the array tile in
823 * global memory that corresponds to the shared memory copy.
825 * In particular, return a map
831 * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
835 * 0 <= a <= array_size - 1 (2)
837 * Note that if some stride has been detected (i.e., when
838 * group->shared_tile->bound[i].shift is set), then a in (1) refers
839 * to the shifted and scaled down version.
841 * Constraints (1) are obtained by mapping the size constraints on the
842 * shared/private memory tile back to the access relation.
843 * Constraints (2) are obtained from the (recomputed) extent.
845 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
848 int n_index
= group
->array
->n_index
;
854 space
= isl_multi_aff_get_space(group
->shared_tile
->tiling
);
855 space
= isl_space_range(space
);
856 local
= isl_set_universe(space
);
857 for (i
= 0; i
< n_index
; ++i
) {
860 local
= isl_set_lower_bound_si(local
, isl_dim_set
, i
, 0);
861 bound
= isl_val_copy(group
->shared_tile
->bound
[i
].size
);
862 bound
= isl_val_sub_ui(bound
, 1);
863 local
= isl_set_upper_bound_val(local
, isl_dim_set
, i
, bound
);
865 local
= isl_set_preimage_multi_aff(local
,
866 isl_multi_aff_copy(group
->shared_tile
->tiling
));
867 tile
= isl_set_unwrap(local
);
868 extent
= array_extent(group
->array
);
869 tile
= isl_map_intersect_range(tile
, extent
);
874 /* Given a mapping "iterator_map" from the AST schedule to a domain,
875 * return the corresponding mapping from the AST schedule to
876 * to the outer kernel->copy_schedule_dim dimensions of
877 * the schedule computed by PPCG for this kernel.
879 * Note that kernel->copy_schedule_dim is at least as large as
880 * the largest depth of any array reference group associated to the kernel.
881 * This is needed as the returned schedule is used to extract a mapping
882 * to the outer tile->depth dimensions in transform_index.
884 static __isl_give isl_pw_multi_aff
*compute_sched_to_copy(
885 struct ppcg_kernel
*kernel
, __isl_take isl_pw_multi_aff
*iterator_map
)
887 isl_union_pw_multi_aff
*upma
;
888 isl_pw_multi_aff
*pma
;
891 space
= isl_space_range(isl_pw_multi_aff_get_space(iterator_map
));
892 space
= isl_space_from_domain(space
);
893 space
= isl_space_add_dims(space
, isl_dim_out
,
894 kernel
->copy_schedule_dim
);
896 upma
= isl_union_pw_multi_aff_copy(kernel
->copy_schedule
);
897 pma
= isl_union_pw_multi_aff_extract_pw_multi_aff(upma
, space
);
898 isl_union_pw_multi_aff_free(upma
);
900 return isl_pw_multi_aff_pullback_pw_multi_aff(pma
, iterator_map
);
903 /* If max_shared_memory is not set to infinity (-1), then make
904 * sure that the total amount of shared memory required by the
905 * array reference groups mapped to shared memory by "kernel"
906 * is no larger than this maximum.
908 * We apply a greedy approach and discard (keep in global memory)
909 * those groups that would result in a total memory size that
910 * is larger than the maximum.
912 * This function should be called after any function that may
913 * affect the decision on whether to place a reference group
914 * in private, shared or global memory.
916 static void check_shared_memory_bound(struct ppcg_kernel
*kernel
)
919 isl_val
*left
, *size
;
921 if (kernel
->options
->max_shared_memory
< 0)
924 left
= isl_val_int_from_si(kernel
->ctx
,
925 kernel
->options
->max_shared_memory
);
927 for (i
= 0; i
< kernel
->n_array
; ++i
) {
928 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
930 for (j
= 0; j
< local
->n_group
; ++j
) {
931 struct gpu_array_ref_group
*group
;
932 enum ppcg_group_access_type type
;
934 group
= local
->groups
[j
];
935 type
= gpu_array_ref_group_type(group
);
936 if (type
!= ppcg_access_shared
)
939 size
= gpu_array_tile_size(group
->shared_tile
);
940 size
= isl_val_mul_ui(size
, local
->array
->size
);
942 if (isl_val_le(size
, left
)) {
943 left
= isl_val_sub(left
, size
);
949 gpu_array_tile_free(group
->shared_tile
);
956 /* Mark all arrays of "kernel" that have an array reference group
957 * that is not mapped to private or shared memory as
958 * accessing the corresponding global device memory.
960 static void mark_global_arrays(struct ppcg_kernel
*kernel
)
964 for (i
= 0; i
< kernel
->n_array
; ++i
) {
965 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
969 for (j
= 0; j
< local
->n_group
; ++j
) {
970 if (gpu_array_ref_group_tile(local
->groups
[j
]))
974 local
->array
->global
= 1;
980 /* Compute a tiling for all the array reference groups in "kernel".
982 static void compute_group_tilings(struct ppcg_kernel
*kernel
)
986 for (i
= 0; i
< kernel
->n_array
; ++i
) {
987 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
989 for (j
= 0; j
< array
->n_group
; ++j
)
990 gpu_array_ref_group_compute_tiling(array
->groups
[j
]);
994 /* Compute the size of a bounding box around the origin and "set",
995 * where "set" is assumed to contain only non-negative elements.
996 * In particular, compute the maximal value of "set" in each direction
999 static __isl_give isl_multi_pw_aff
*extract_size(__isl_take isl_set
*set
,
1000 __isl_take isl_set
*context
)
1003 isl_multi_pw_aff
*mpa
;
1005 context
= isl_set_params(context
);
1006 n
= isl_set_dim(set
, isl_dim_set
);
1007 mpa
= isl_multi_pw_aff_zero(isl_set_get_space(set
));
1008 for (i
= 0; i
< n
; ++i
) {
1013 bound
= isl_set_dim_max(isl_set_copy(set
), i
);
1014 bound
= isl_pw_aff_coalesce(bound
);
1015 bound
= isl_pw_aff_gist(bound
, isl_set_copy(context
));
1017 space
= isl_pw_aff_get_domain_space(bound
);
1018 one
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1019 one
= isl_aff_add_constant_si(one
, 1);
1020 bound
= isl_pw_aff_add(bound
, isl_pw_aff_from_aff(one
));
1021 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, bound
);
1024 isl_set_free(context
);
1029 /* Compute the effective grid size as a list of the sizes in each dimension.
1031 * The grid size specified by the user or set by default
1032 * in read_grid_sizes() and applied by the block filter,
1033 * may be too large for the given code in the sense that
1034 * it may contain blocks that don't need to execute anything.
1035 * We therefore don't return this grid size, but instead the
1036 * smallest grid size that ensures that all blocks that actually
1037 * execute code are included in the grid.
1039 * We first extract a description of the grid, i.e., the possible values
1040 * of the block ids, from the domain elements in "domain" and
1041 * kernel->block_filter.
1042 * The block ids are parameters in kernel->block_filter.
1043 * We simply need to change them into set dimensions.
1045 * Then, for each block dimension, we compute the maximal value of the block id
1048 static __isl_give isl_multi_pw_aff
*extract_grid_size(
1049 struct ppcg_kernel
*kernel
, __isl_take isl_union_set
*domain
)
1054 domain
= isl_union_set_intersect(domain
,
1055 isl_union_set_copy(kernel
->block_filter
));
1056 grid
= isl_union_set_params(domain
);
1057 grid
= isl_set_from_params(grid
);
1058 grid
= isl_set_add_dims(grid
, isl_dim_set
, kernel
->n_grid
);
1059 for (i
= 0; i
< kernel
->n_grid
; ++i
) {
1063 id
= isl_id_list_get_id(kernel
->block_ids
, i
);
1064 pos
= isl_set_find_dim_by_id(grid
, isl_dim_param
, id
);
1067 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
1068 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
1071 return extract_size(grid
, isl_set_copy(kernel
->context
));
1074 /* Compute the size of a fixed bounding box around the origin and "set",
1075 * where "set" is assumed to contain only non-negative elements,
1076 * and store the results in "size".
1077 * In particular, compute the maximal value of "set" in each direction
1080 static void extract_fixed_size(__isl_take isl_set
*set
, int *size
)
1083 isl_local_space
*ls
;
1086 n
= isl_set_dim(set
, isl_dim_set
);
1087 ls
= isl_local_space_from_space(isl_set_get_space(set
));
1088 obj
= isl_aff_zero_on_domain(ls
);
1089 for (i
= 0; i
< n
; ++i
) {
1092 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 1);
1093 max
= isl_set_max_val(set
, obj
);
1094 size
[i
] = isl_val_get_num_si(max
) + 1;
1096 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 0);
1102 /* Compute the effective block size as a list of the sizes in each dimension
1103 * and store the sizes in kernel->block_dim.
1105 * The block size specified by the user or set by default
1106 * in read_block_sizes() and applied by the thread filter,
1107 * may be too large for the given code in the sense that
1108 * it may contain threads that don't need to execute anything.
1109 * We therefore update this block size in kernel->block_dim
1110 * to the smallest block size that ensures that all threads
1111 * that actually execute code are included in the block.
1113 * The possible values of the thread ids is obtained from
1114 * the domain elements "domain" and kernel->thread_filter.
1115 * The current implementation eliminates all parameters, ensuring
1116 * that the size is a fixed constant in each dimension.
1117 * In principle we could also compute parametric sizes.
1118 * We would have to make sure to project out all b%d and t%d parameters,
1121 static isl_stat
extract_block_size(struct ppcg_kernel
*kernel
,
1122 __isl_take isl_union_set
*domain
)
1128 domain
= isl_union_set_intersect(domain
,
1129 isl_union_set_copy(kernel
->thread_filter
));
1130 block
= isl_union_set_params(domain
);
1131 block
= isl_set_from_params(block
);
1132 block
= isl_set_add_dims(block
, isl_dim_set
, kernel
->n_block
);
1133 for (i
= 0; i
< kernel
->n_block
; ++i
) {
1138 return isl_stat_error
;
1140 id
= isl_id_list_get_id(kernel
->thread_ids
, i
);
1141 pos
= isl_set_find_dim_by_id(block
, isl_dim_param
, id
);
1144 isl_die(isl_set_get_ctx(block
), isl_error_internal
,
1145 "missing constraints on thread identifier",
1146 block
= isl_set_free(block
));
1147 block
= isl_set_equate(block
, isl_dim_param
, pos
,
1150 nparam
= isl_set_dim(block
, isl_dim_param
);
1151 block
= isl_set_project_out(block
, isl_dim_param
, 0, nparam
);
1154 return isl_stat_error
;
1156 extract_fixed_size(block
, kernel
->block_dim
);
1161 struct ppcg_kernel
*ppcg_kernel_free(struct ppcg_kernel
*kernel
)
1168 isl_id_list_free(kernel
->block_ids
);
1169 isl_id_list_free(kernel
->thread_ids
);
1170 isl_multi_pw_aff_free(kernel
->grid_size
);
1171 isl_set_free(kernel
->context
);
1172 isl_union_set_free(kernel
->core
);
1173 isl_union_set_free(kernel
->arrays
);
1174 isl_space_free(kernel
->space
);
1175 isl_ast_node_free(kernel
->tree
);
1176 isl_union_set_free(kernel
->block_filter
);
1177 isl_union_set_free(kernel
->thread_filter
);
1178 isl_union_pw_multi_aff_free(kernel
->copy_schedule
);
1179 isl_union_set_free(kernel
->sync_writes
);
1181 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1182 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1184 for (j
= 0; j
< array
->n_group
; ++j
)
1185 gpu_array_ref_group_free(array
->groups
[j
]);
1186 free(array
->groups
);
1188 isl_pw_aff_list_free(array
->bound
);
1190 free(kernel
->array
);
1192 for (i
= 0; i
< kernel
->n_var
; ++i
) {
1193 free(kernel
->var
[i
].name
);
1194 isl_vec_free(kernel
->var
[i
].size
);
1203 /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
1205 static void ppcg_kernel_free_wrap(void *user
)
1207 struct ppcg_kernel
*kernel
= user
;
1209 ppcg_kernel_free(kernel
);
1212 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
1213 struct ppcg_kernel_var
*var
)
1216 struct gpu_array_tile
*tile
;
1220 var
->array
= group
->array
;
1222 var
->type
= gpu_array_ref_group_type(group
);
1223 tile
= gpu_array_ref_group_tile(group
);
1225 p
= isl_printer_to_str(ctx
);
1226 p
= gpu_array_ref_group_print_name(group
, p
);
1227 var
->name
= isl_printer_get_str(p
);
1228 isl_printer_free(p
);
1230 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
1232 for (j
= 0; j
< group
->array
->n_index
; ++j
)
1233 var
->size
= isl_vec_set_element_val(var
->size
, j
,
1234 isl_val_copy(tile
->bound
[j
].size
));
1237 static int create_kernel_vars(struct ppcg_kernel
*kernel
)
1242 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1243 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1245 for (j
= 0; j
< array
->n_group
; ++j
) {
1246 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1247 enum ppcg_group_access_type type
;
1249 type
= gpu_array_ref_group_type(group
);
1250 if (type
!= ppcg_access_global
)
1256 kernel
->var
= isl_calloc_array(kernel
->ctx
, struct ppcg_kernel_var
, n
);
1261 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1262 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1264 for (j
= 0; j
< array
->n_group
; ++j
) {
1265 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1266 enum ppcg_group_access_type type
;
1268 type
= gpu_array_ref_group_type(group
);
1269 if (type
== ppcg_access_global
)
1271 create_kernel_var(kernel
->ctx
, group
, &kernel
->var
[n
]);
1279 /* Replace "pa" by the zero function defined over the universe domain
1280 * in the space of "pa".
1282 static __isl_give isl_pw_aff
*set_universally_zero(__isl_take isl_pw_aff
*pa
)
1287 space
= isl_space_domain(isl_pw_aff_get_space(pa
));
1288 isl_pw_aff_free(pa
);
1289 zero
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1291 return isl_pw_aff_from_aff(zero
);
1294 /* The sizes of the arrays on the host that have been computed by
1295 * extract_array_info may depend on the parameters. Use the extra
1296 * constraints on the parameters that are valid at "host_domain"
1297 * to simplify these expressions and store the results in kernel->array.
1299 * We only need these localized bounds for arrays that are accessed
1300 * by the current kernel. If we have found at least one reference group
1301 * then the array is accessed by the kernel.
1303 * The resulting sizes may be functions that are nowhere defined
1304 * in case the access function cannot possibly access anything inside
1305 * the kernel for some reason. If so, they are replaced by the zero
1306 * function. Since the access function cannot actually access anything,
1307 * there is no harm in printing the array sizes as zero.
1309 static void localize_bounds(struct ppcg_kernel
*kernel
,
1310 __isl_keep isl_set
*host_domain
)
1315 context
= isl_set_copy(host_domain
);
1316 context
= isl_set_params(context
);
1318 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1319 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1320 isl_pw_aff_list
*bound
;
1323 if (local
->n_group
== 0)
1326 n_index
= local
->array
->n_index
;
1327 bound
= isl_pw_aff_list_alloc(kernel
->ctx
, n_index
);
1329 for (j
= 0; j
< n_index
; ++j
) {
1333 pwaff
= isl_pw_aff_copy(local
->array
->bound
[j
]);
1334 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
1335 empty
= isl_pw_aff_is_empty(pwaff
);
1337 pwaff
= isl_pw_aff_free(pwaff
);
1339 pwaff
= set_universally_zero(pwaff
);
1340 bound
= isl_pw_aff_list_add(bound
, pwaff
);
1343 local
->n_index
= n_index
;
1344 local
->bound
= bound
;
1346 isl_set_free(context
);
1349 /* Create the array of gpu_local_array_info structures "array"
1350 * inside "kernel". The number of elements in this array is
1351 * the same as the number of arrays in "prog".
1352 * Initialize the "array" field of each local array to point
1353 * to the corresponding array in "prog".
1355 static struct ppcg_kernel
*ppcg_kernel_create_local_arrays(
1356 struct ppcg_kernel
*kernel
, struct gpu_prog
*prog
)
1361 ctx
= isl_set_get_ctx(prog
->context
);
1362 kernel
->array
= isl_calloc_array(ctx
,
1363 struct gpu_local_array_info
, prog
->n_array
);
1365 return ppcg_kernel_free(kernel
);
1366 kernel
->n_array
= prog
->n_array
;
1368 for (i
= 0; i
< prog
->n_array
; ++i
)
1369 kernel
->array
[i
].array
= &prog
->array
[i
];
1374 /* Does "kernel" need to be passed an argument corresponding to array "i"?
1376 * The argument is only needed if the kernel accesses this device memory.
1378 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
)
1380 return kernel
->array
[i
].global
;
1383 /* Find the element in gen->stmt that has the given "id".
1384 * Return NULL if no such gpu_stmt can be found.
1386 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
1390 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
1391 if (id
== prog
->stmts
[i
].id
)
1395 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
1398 void ppcg_kernel_stmt_free(void *user
)
1401 struct ppcg_kernel_stmt
*stmt
= user
;
1406 switch (stmt
->type
) {
1407 case ppcg_kernel_copy
:
1408 isl_ast_expr_free(stmt
->u
.c
.index
);
1409 isl_ast_expr_free(stmt
->u
.c
.local_index
);
1411 case ppcg_kernel_domain
:
1412 isl_id_to_ast_expr_free(stmt
->u
.d
.ref2expr
);
1414 case ppcg_kernel_sync
:
1421 /* Return the gpu_stmt_access in the list "accesses" that corresponds
1424 static struct gpu_stmt_access
*find_access(struct gpu_stmt_access
*accesses
,
1425 __isl_keep isl_id
*ref_id
)
1427 struct gpu_stmt_access
*access
;
1429 for (access
= accesses
; access
; access
= access
->next
)
1430 if (access
->ref_id
== ref_id
)
1436 /* Return the index of the array called "name" in the list of arrays.
1438 static int find_array_index(struct ppcg_kernel
*kernel
, const char *name
)
1442 for (i
= 0; i
< kernel
->n_array
; ++i
)
1443 if (!strcmp(name
, kernel
->array
[i
].array
->name
))
1449 /* Internal data structure for the index and AST expression transformation
1450 * callbacks for pet_stmt_build_ast_exprs.
1452 * "kernel" is the kernel for which are computing AST expressions and
1453 * may be NULL if we are not inside a kernel.
1454 * "accesses" is the list of gpu_stmt_access in the statement.
1455 * "iterator_map" expresses the statement iterators in terms of
1456 * the AST loop iterators.
1457 * "sched2copy" expresses the outer copy_schedule_dim dimensions of
1458 * the kernel schedule in terms of the AST loop iterators and
1459 * may be NULL if we are not inside a kernel.
1461 * The following fields are set in transform_index and used in transform_expr.
1462 * "array" is the array that is being accessed.
1463 * "global" is set if the global array is accessed (rather than
1464 * shared/private memory).
1465 * "local_array" refers to information on the array specialized
1466 * to the current kernel.
1468 struct ppcg_transform_data
{
1469 struct ppcg_kernel
*kernel
;
1470 struct gpu_stmt_access
*accesses
;
1471 isl_pw_multi_aff
*iterator_map
;
1472 isl_pw_multi_aff
*sched2copy
;
1474 struct gpu_array_info
*array
;
1476 struct gpu_local_array_info
*local_array
;
1479 /* Return a pointer to the gpu_array_ref_group in "local"
1480 * that contains the reference "access".
1481 * Return NULL if no such group can be found.
1483 static struct gpu_array_ref_group
*find_ref_group(
1484 struct gpu_local_array_info
*local
, struct gpu_stmt_access
*access
)
1488 for (i
= 0; i
< local
->n_group
; ++i
) {
1489 struct gpu_array_ref_group
*group
= local
->groups
[i
];
1491 for (j
= 0; j
< group
->n_ref
; ++j
)
1492 if (group
->refs
[j
] == access
)
1499 /* Index transformation callback for pet_stmt_build_ast_exprs.
1501 * "index" expresses the array indices in terms of statement iterators
1503 * We first reformulate "index" in terms of the AST loop iterators.
1504 * Then we check if we are accessing the global array or
1505 * a shared/private copy. In particular, if we are not inside a kernel
1506 * then we must be accessing a global array.
1507 * In the former case, we simply return
1508 * the updated index. If "index" is an affine expression rather
1509 * than an array access, then we also return the updated index here.
1511 * If no reference groups have been computed for the array,
1512 * then we can only be accessing the global array.
1514 * Otherwise, we apply the tiling to the index.
1515 * This tiling is of the form
1519 * where D corresponds to the outer tile->depth dimensions of
1520 * the kernel schedule.
1521 * The index is of the form
1525 * We update the tiling to refer to the AST loop iterators
1529 * and modify index to keep track of those iterators
1533 * Combining these two yields a tiled index expression in terms
1534 * of the AST loop iterators
1538 static __isl_give isl_multi_pw_aff
*transform_index(
1539 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*ref_id
,
1542 struct ppcg_transform_data
*data
= user
;
1543 struct gpu_stmt_access
*access
;
1544 struct gpu_array_ref_group
*group
;
1545 struct gpu_array_tile
*tile
;
1546 isl_pw_multi_aff
*iterator_map
;
1551 isl_multi_pw_aff
*tiling
;
1552 isl_pw_multi_aff
*pma
;
1553 isl_multi_pw_aff
*mpa
;
1554 isl_pw_multi_aff
*sched2depth
;
1558 iterator_map
= isl_pw_multi_aff_copy(data
->iterator_map
);
1559 index
= isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
1564 access
= find_access(data
->accesses
, ref_id
);
1567 if (!isl_map_has_tuple_name(access
->access
, isl_dim_out
))
1570 name
= get_outer_array_name(access
->access
);
1571 i
= find_array_index(data
->kernel
, name
);
1573 isl_die(isl_multi_pw_aff_get_ctx(index
), isl_error_internal
,
1574 "cannot find array",
1575 return isl_multi_pw_aff_free(index
));
1576 data
->local_array
= &data
->kernel
->array
[i
];
1577 data
->array
= data
->local_array
->array
;
1579 group
= find_ref_group(data
->local_array
, access
);
1585 tile
= gpu_array_ref_group_tile(group
);
1586 data
->global
= !tile
;
1590 space
= isl_space_range(isl_multi_pw_aff_get_space(index
));
1591 space
= isl_space_map_from_set(space
);
1592 pma
= isl_pw_multi_aff_identity(space
);
1593 sched2depth
= isl_pw_multi_aff_copy(data
->sched2copy
);
1594 dim
= isl_pw_multi_aff_dim(sched2depth
, isl_dim_out
);
1595 sched2depth
= isl_pw_multi_aff_drop_dims(sched2depth
, isl_dim_out
,
1596 tile
->depth
, dim
- tile
->depth
);
1597 pma
= isl_pw_multi_aff_product(sched2depth
, pma
);
1598 tiling
= isl_multi_pw_aff_from_multi_aff(
1599 isl_multi_aff_copy(tile
->tiling
));
1600 tiling
= isl_multi_pw_aff_pullback_pw_multi_aff(tiling
, pma
);
1602 space
= isl_space_domain(isl_multi_pw_aff_get_space(index
));
1603 space
= isl_space_map_from_set(space
);
1604 mpa
= isl_multi_pw_aff_identity(space
);
1605 index
= isl_multi_pw_aff_range_product(mpa
, index
);
1606 index
= isl_multi_pw_aff_pullback_multi_pw_aff(tiling
, index
);
1611 /* Dereference "expr" by adding an index [0].
1612 * The original "expr" is assumed not to have any indices.
1614 * If "expr" is a member access, then the dereferencing needs
1615 * to be applied to the structure argument of this member access.
1617 static __isl_give isl_ast_expr
*dereference(__isl_take isl_ast_expr
*expr
)
1620 isl_ast_expr
*arg0
, *res
;
1621 isl_ast_expr_list
*list
;
1623 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1625 return isl_ast_expr_free(expr
);
1626 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1627 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1630 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1631 arg
= dereference(arg
);
1632 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1633 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1637 isl_ast_expr_free(arg0
);
1639 ctx
= isl_ast_expr_get_ctx(expr
);
1640 res
= isl_ast_expr_from_val(isl_val_zero(ctx
));
1641 list
= isl_ast_expr_list_from_ast_expr(res
);
1642 res
= isl_ast_expr_get_op_arg(expr
, 0);
1643 res
= isl_ast_expr_access(res
, list
);
1644 isl_ast_expr_free(expr
);
1649 /* Linearize the index expression "expr" based on the array bounds
1652 * That is, transform expression
1654 * A[i_0][i_1]...[i_n]
1658 * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
1660 * where b_0, b_1, ..., b_n are the bounds on the array.
1662 * If the base of "expr" is a member access, then the linearization needs
1663 * to be applied to the structure argument of this member access.
1665 * In the base case, if "expr" has no arguments (other than the name of
1666 * the array), then we are passing an entire array to a function.
1667 * In this case, there is nothing to linearize.
1668 * Note that at this point an expression with no arguments can
1669 * only be an entire array because the scalar case and
1670 * the case of single struct are handled by the caller.
1672 * If the number of specified index expressions in "expr"
1673 * is smaller than the dimension of the accessed array,
1674 * then the missing i_j also do not appear in the linearized expression.
1675 * Furthermore, since such an expression does not refer to a single
1676 * element while the default linearized expression would refer to
1677 * a single element, we return the expression
1679 * A + (..((i_0 * b_1 + i_1) ... ) * b_l + i_l)
1681 * instead. Note that because of the special case handling above,
1682 * we can assume here that there is at least one index expression.
1684 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
1685 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
)
1692 isl_ast_expr_list
*list
;
1693 isl_ast_build
*build
;
1695 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1696 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1697 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1700 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1701 arg
= gpu_local_array_info_linearize_index(array
, arg
);
1702 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1703 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1707 isl_ast_expr_free(arg0
);
1709 if (isl_ast_expr_get_op_n_arg(expr
) == 1)
1712 ctx
= isl_ast_expr_get_ctx(expr
);
1713 context
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
1714 build
= isl_ast_build_from_context(context
);
1716 n
= isl_ast_expr_get_op_n_arg(expr
);
1717 res
= isl_ast_expr_get_op_arg(expr
, 1);
1718 for (i
= 1; i
< array
->n_index
; ++i
) {
1719 isl_pw_aff
*bound_i
;
1720 isl_ast_expr
*expr_i
;
1722 bound_i
= isl_pw_aff_list_get_pw_aff(array
->bound
, i
);
1723 expr_i
= isl_ast_build_expr_from_pw_aff(build
, bound_i
);
1724 res
= isl_ast_expr_mul(res
, expr_i
);
1728 expr_i
= isl_ast_expr_get_op_arg(expr
, i
+ 1);
1729 res
= isl_ast_expr_add(res
, expr_i
);
1732 isl_ast_build_free(build
);
1734 if (1 + array
->n_index
> n
) {
1735 res
= isl_ast_expr_add(isl_ast_expr_get_op_arg(expr
, 0), res
);
1737 list
= isl_ast_expr_list_from_ast_expr(res
);
1738 res
= isl_ast_expr_get_op_arg(expr
, 0);
1739 res
= isl_ast_expr_access(res
, list
);
1742 isl_ast_expr_free(expr
);
1747 /* AST expression transformation callback for pet_stmt_build_ast_exprs.
1749 * If the AST expression refers to an array that is not accessed
1750 * at all, then this means the value of the expression is not used,
1751 * so we might as well print zero (NULL pointer) instead.
1753 * If the AST expression refers to a global scalar that is not
1754 * a read-only scalar, then its address was passed to the kernel and
1755 * we need to dereference it.
1757 * If the AST expression refers to an access to a global array,
1758 * then we linearize the access exploiting the bounds in data->local_array.
1760 static __isl_give isl_ast_expr
*transform_expr(__isl_take isl_ast_expr
*expr
,
1761 __isl_keep isl_id
*id
, void *user
)
1763 struct ppcg_transform_data
*data
= user
;
1767 if (!data
->array
->accessed
) {
1770 ctx
= isl_ast_expr_get_ctx(expr
);
1771 isl_ast_expr_free(expr
);
1772 return isl_ast_expr_from_val(isl_val_zero(ctx
));
1774 if (gpu_array_is_read_only_scalar(data
->array
))
1778 if (data
->array
->n_index
== 0)
1779 return dereference(expr
);
1780 if (!data
->array
->linearize
)
1783 return gpu_local_array_info_linearize_index(data
->local_array
, expr
);
1786 /* This function is called for each instance of a user statement
1787 * in the kernel "kernel", identified by "gpu_stmt".
1788 * "kernel" may be NULL if we are not inside a kernel.
1790 * We attach a struct ppcg_kernel_stmt to the "node", containing
1791 * a computed AST expression for each access, through an annotation
1793 * These AST expressions are computed from iterator_map,
1794 * which expresses the domain
1795 * elements in terms of the generated loops, and sched2copy,
1796 * which expresses the outer copy_schedule_dim dimensions of
1797 * the kernel schedule computed by PPCG in terms of the generated loops.
1799 static __isl_give isl_ast_node
*create_domain_leaf(
1800 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1801 __isl_keep isl_ast_build
*build
, struct gpu_stmt
*gpu_stmt
)
1803 struct ppcg_transform_data data
;
1804 struct ppcg_kernel_stmt
*stmt
;
1807 isl_pw_multi_aff
*sched2copy
;
1809 isl_pw_multi_aff
*iterator_map
;
1810 isl_union_map
*schedule
;
1814 ctx
= isl_ast_node_get_ctx(node
);
1816 stmt
= isl_calloc_type(ctx
, struct ppcg_kernel_stmt
);
1818 return isl_ast_node_free(node
);
1820 schedule
= isl_ast_build_get_schedule(build
);
1821 map
= isl_map_reverse(isl_map_from_union_map(schedule
));
1822 iterator_map
= isl_pw_multi_aff_from_map(map
);
1824 sched2copy
= compute_sched_to_copy(kernel
,
1825 isl_pw_multi_aff_copy(iterator_map
));
1829 stmt
->type
= ppcg_kernel_domain
;
1830 stmt
->u
.d
.stmt
= gpu_stmt
;
1832 data
.kernel
= kernel
;
1833 data
.accesses
= stmt
->u
.d
.stmt
->accesses
;
1834 data
.iterator_map
= iterator_map
;
1835 data
.sched2copy
= sched2copy
;
1836 stmt
->u
.d
.ref2expr
= pet_stmt_build_ast_exprs(stmt
->u
.d
.stmt
->stmt
,
1837 build
, &transform_index
, &data
,
1838 &transform_expr
, &data
);
1840 isl_pw_multi_aff_free(iterator_map
);
1841 isl_pw_multi_aff_free(sched2copy
);
1843 id
= isl_id_alloc(ctx
, "user", stmt
);
1844 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1845 return isl_ast_node_set_annotation(node
, id
);
1848 /* This function is called for each statement node in the AST
1849 * for copying to or from shared/private memory.
1850 * Attach a pointer to a ppcg_kernel_stmt representing the copy
1851 * statement to the node.
1852 * The statement name is "read" or "write", depending on whether we are
1853 * reading from global memory or writing to global memory.
1855 * The schedule is of the form
1859 * where D corresponds to the outer tile->depth dimensions of
1860 * the kernel schedule, A to the global array and L to the outer
1861 * generated AST schedule.
1862 * We compute the inverse and strip off the type, resulting in
1866 * We combine this mapping with on the one hand the projection
1870 * and on the other hand the group tiling
1878 * and store the corresponding expressions in stmt->index and stmt->local_index,
1879 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
1881 static __isl_give isl_ast_node
*create_access_leaf(struct ppcg_kernel
*kernel
,
1882 struct gpu_array_ref_group
*group
, __isl_take isl_ast_node
*node
,
1883 __isl_keep isl_ast_build
*build
)
1885 struct ppcg_kernel_stmt
*stmt
;
1886 struct gpu_array_tile
*tile
;
1891 isl_pw_multi_aff
*pma
, *pma2
;
1894 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1896 return isl_ast_node_free(node
);
1898 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
1899 type
= isl_map_get_tuple_name(access
, isl_dim_in
);
1900 stmt
->u
.c
.read
= !strcmp(type
, "read");
1901 access
= isl_map_reverse(access
);
1902 pma
= isl_pw_multi_aff_from_map(access
);
1903 pma
= isl_pw_multi_aff_reset_tuple_id(pma
, isl_dim_out
);
1905 space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
1906 space
= isl_space_unwrap(space
);
1907 pma2
= isl_pw_multi_aff_range_map(space
);
1908 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
,
1909 isl_pw_multi_aff_copy(pma
));
1910 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1911 stmt
->u
.c
.index
= expr
;
1913 tile
= gpu_array_ref_group_tile(group
);
1914 pma2
= isl_pw_multi_aff_from_multi_aff(
1915 isl_multi_aff_copy(tile
->tiling
));
1916 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
, pma
);
1917 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1918 stmt
->u
.c
.local_index
= expr
;
1920 stmt
->u
.c
.array
= group
->array
;
1921 stmt
->u
.c
.local_array
= group
->local_array
;
1922 stmt
->type
= ppcg_kernel_copy
;
1924 id
= isl_id_alloc(kernel
->ctx
, NULL
, stmt
);
1925 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1926 return isl_ast_node_set_annotation(node
, id
);
1929 /* Create a synchronization ppcg_kernel_stmt and
1930 * attach it to the node "node" representing the synchronization.
1932 static __isl_give isl_ast_node
*create_sync_leaf(
1933 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1934 __isl_keep isl_ast_build
*build
)
1936 struct ppcg_kernel_stmt
*stmt
;
1939 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1941 return isl_ast_node_free(node
);
1943 stmt
->type
= ppcg_kernel_sync
;
1944 id
= isl_id_alloc(kernel
->ctx
, NULL
, stmt
);
1945 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1946 return isl_ast_node_set_annotation(node
, id
);
1949 /* Internal data structure for at_domain.
1951 * "prog" represents the entire scop.
1952 * "kernel" points to the kernel to which the current schedule node
1953 * belongs. It is set by before_mark and reset by after_mark.
1954 * It may be NULL if we are outside any kernel.
1956 struct ppcg_at_domain_data
{
1957 struct gpu_prog
*prog
;
1958 struct ppcg_kernel
*kernel
;
1961 /* This function is called for each instance of a user statement
1962 * in the kernel. This may be one of the original user statements
1963 * or a statement introduced by PPCG.
1965 * We first check if the statement id corresponds to a gpu statement,
1966 * which indicates the statement is an original user statement. Any statement
1967 * that is not an original user statement has been introduced by PPCG and
1968 * requires special handling.
1970 * If the user statement is one of the original user statements, then we call
1971 * create_domain_leaf. Otherwise, we check if it is a copy or synchronization
1972 * statement and call the appropriate functions. Statements that copy an array
1973 * to/from the device do not need any further treatment.
1974 * Neither do "init_device" and "clear_device".
1976 static __isl_give isl_ast_node
*at_domain(__isl_take isl_ast_node
*node
,
1977 __isl_keep isl_ast_build
*build
, void *user
)
1979 struct ppcg_at_domain_data
*data
= user
;
1980 struct gpu_stmt
*gpu_stmt
;
1981 isl_ast_expr
*expr
, *arg
;
1987 expr
= isl_ast_node_user_get_expr(node
);
1988 arg
= isl_ast_expr_get_op_arg(expr
, 0);
1989 id
= isl_ast_expr_get_id(arg
);
1990 name
= isl_id_get_name(id
);
1991 p
= isl_id_get_user(id
);
1992 isl_ast_expr_free(expr
);
1993 isl_ast_expr_free(arg
);
1995 gpu_stmt
= find_stmt(data
->prog
, id
);
1996 is_sync
= gpu_tree_id_is_sync(id
, data
->kernel
);
2000 return create_domain_leaf(data
->kernel
, node
, build
, gpu_stmt
);
2002 if (!prefixcmp(name
, "to_device_") || !prefixcmp(name
, "from_device_"))
2004 if (!strcmp(name
, "init_device") || !strcmp(name
, "clear_device"))
2007 return isl_ast_node_free(node
);
2008 if (!strcmp(name
, "read") || !strcmp(name
, "write")) {
2009 struct gpu_array_ref_group
*group
= p
;
2010 return create_access_leaf(data
->kernel
, group
, node
, build
);
2013 isl_die(data
->prog
->ctx
, isl_error_internal
,
2014 "unknown statement type",
2015 return isl_ast_node_free(node
));
2016 return create_sync_leaf(data
->kernel
, node
, build
);
2019 /* Given a set of wrapped references "ref", return the corresponding
2020 * access relations based on the tagged access relations "tagged".
2022 * The elements of "ref" are of the form
2026 * with D an iteration domains and R a reference.
2027 * The elements of "tagged" are of the form
2033 * Extend "tagged" to include the iteration domain in the range, i.e.,
2035 * [D -> R] -> [D -> A]
2037 * apply the result to "ref" and then unwrap the resulting set
2038 * to obtain relations of the form
2042 static __isl_give isl_union_map
*wrapped_reference_to_access(
2043 __isl_take isl_union_set
*ref
, __isl_take isl_union_map
*tagged
)
2045 isl_union_map
*tag2access
;
2047 tag2access
= isl_union_map_copy(tagged
);
2048 tag2access
= isl_union_map_universe(tag2access
);
2049 tag2access
= isl_union_set_unwrap(isl_union_map_domain(tag2access
));
2050 tag2access
= isl_union_map_domain_map(tag2access
);
2051 tag2access
= isl_union_map_range_product(tag2access
, tagged
);
2053 ref
= isl_union_set_coalesce(ref
);
2054 ref
= isl_union_set_apply(ref
, tag2access
);
2056 return isl_union_set_unwrap(ref
);
2059 /* Given an access relation "access" from one or more array reference groups,
2060 * remove those reads if ("read" is 1) or writes (if "read" is 0)
2061 * that are only needed to communicate data within
2062 * the same iteration of "sched".
2063 * "tagged" contains all tagged access relations to all
2064 * the array reference groups accessed by "access" from statement
2065 * instances scheduled by "sched".
2067 * If the access is a read then it is either an element of
2069 * live_in union (range flow)
2071 * where live_in and flow may be overapproximations, or
2072 * it reads an uninitialized value (that is not live-in because
2073 * there is an intermediate kill) or it reads a value that was
2074 * written within the same (compound) statement instance.
2075 * If the access is a write then it is either an element of
2077 * live_out union (domain flow)
2079 * or it writes a value that is never read (and is not live-out
2080 * because of an intermediate kill) or only
2081 * within the same (compound) statement instance.
2082 * In both cases, the access relation is also a subset of
2083 * the group access relation.
2085 * The cases where an uninitialized value is read or a value is written
2086 * that is never read or where the dataflow occurs within a statement
2087 * instance are also considered local and may also be removed.
2089 * Essentially, we compute the intersection of "access" with either
2091 * live_in union (range non-local-flow)
2095 * live_out union (domain non-local-flow)
2097 * We first construct a relation "local"
2099 * [[D -> R] -> [D' -> R']]
2101 * of pairs of domain iterations accessing the reference group
2102 * and references in the group that are coscheduled by "sched".
2104 * If this relation does not intersect the dataflow dependences,
2105 * then there is nothing we can possibly remove, unless the dataflow
2106 * dependences themselves only relate a subset of the accesses.
2107 * In particular, the accesses may not be involved in any dataflow
2108 * dependences, either because they are uninitialized reads/dead writes
2109 * or because the dataflow occurs inside a statement instance.
2111 * Since the computation below may break up the access relation
2112 * into smaller pieces, we only perform the intersection with
2113 * the non-local dependent accesses if the local pairs
2114 * intersect the dataflow dependences. Otherwise, we intersect
2115 * with the universe of the non-local dependent accesses.
2116 * This should at least remove accesses from statements that
2117 * do not participate in any dependences.
2119 * In particular, we remove the "local" dataflow dependences from
2120 * the set of all dataflow dependences, or at least those
2121 * that may contribute to a domain/range that intersects
2122 * the domain of "access".
2123 * Note that if the potential dataflow dependences are an overapproximation
2124 * of the actual dataflow dependences, then the result remains an
2125 * overapproximation of the non-local dataflow dependences.
2126 * Copying to/from global memory is only needed for the references
2127 * in the domain/range of the result or for accesses that are live out/in
2128 * for the entire scop.
2130 * We therefore map the domain/range of the "external" relation
2131 * to the corresponding access relation and take the union with
2132 * the live out/in relation.
2134 static __isl_give isl_union_map
*remove_local_accesses(
2135 struct gpu_prog
*prog
, __isl_take isl_union_map
*tagged
,
2136 __isl_take isl_union_map
*access
, __isl_take isl_union_map
*sched
,
2140 isl_union_pw_multi_aff
*tagger
;
2141 isl_union_set
*domain
, *access_domain
;
2142 isl_union_map
*local
, *external
, *universe
;
2143 isl_union_set
*tag_set
;
2145 if (isl_union_map_is_empty(access
)) {
2146 isl_union_map_free(sched
);
2147 isl_union_map_free(tagged
);
2151 tagger
= isl_union_pw_multi_aff_copy(prog
->scop
->tagger
);
2152 domain
= isl_union_map_domain(isl_union_map_copy(tagged
));
2153 tagger
= isl_union_pw_multi_aff_intersect_domain(tagger
,
2154 isl_union_set_copy(domain
));
2155 sched
= isl_union_map_preimage_domain_union_pw_multi_aff(sched
, tagger
);
2157 local
= isl_union_map_apply_range(sched
,
2158 isl_union_map_reverse(isl_union_map_copy(sched
)));
2159 local
= isl_union_map_intersect(local
,
2160 isl_union_map_copy(prog
->scop
->tagged_dep_flow
));
2162 empty
= isl_union_map_is_empty(local
);
2164 external
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
2165 universe
= isl_union_map_universe(isl_union_map_copy(access
));
2166 access_domain
= isl_union_map_domain(universe
);
2167 domain
= isl_union_set_universe(domain
);
2168 universe
= isl_union_set_unwrap(domain
);
2169 universe
= isl_union_map_intersect_domain(universe
, access_domain
);
2170 domain
= isl_union_map_wrap(universe
);
2172 external
= isl_union_map_intersect_range(external
, domain
);
2174 external
= isl_union_map_intersect_domain(external
, domain
);
2175 external
= isl_union_map_intersect_params(external
,
2176 isl_set_copy(prog
->scop
->context
));
2177 external
= isl_union_map_subtract(external
, local
);
2180 tag_set
= isl_union_map_range(external
);
2181 external
= wrapped_reference_to_access(tag_set
, tagged
);
2182 external
= isl_union_map_union(external
,
2183 isl_union_map_copy(prog
->scop
->live_in
));
2185 tag_set
= isl_union_map_domain(external
);
2186 external
= wrapped_reference_to_access(tag_set
, tagged
);
2187 external
= isl_union_map_union(external
,
2188 isl_union_map_copy(prog
->scop
->live_out
));
2192 external
= isl_union_map_free(external
);
2194 external
= isl_union_map_universe(external
);
2196 access
= isl_union_map_intersect(access
, external
);
2201 /* Given an access relation "access" from "group", remove those reads
2202 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2203 * communicate data within the same iteration of the schedule at the
2204 * position where the copying of the group is inserted.
2205 * "node" points to this position, i.e., the depth at "node"
2206 * is equal to tile->depth.
2208 * We extract a schedule that picks out the iterations of the outer
2209 * tile->depth dimensions and call remove_local_accesses.
2211 static __isl_give isl_union_map
*remove_local_accesses_group(
2212 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
2213 __isl_take isl_union_map
*access
, __isl_keep isl_schedule_node
*node
,
2216 isl_union_map
*sched
, *tagged
;
2218 if (isl_union_map_is_empty(access
))
2221 tagged
= group_tagged_access_relation(group
);
2222 sched
= isl_schedule_node_get_prefix_schedule_relation(node
);
2224 return remove_local_accesses(kernel
->prog
, tagged
, access
, sched
, read
);
2227 /* This function is called before the AST generator starts traversing
2228 * the schedule subtree of a node with mark "mark".
2230 * If the mark is called "kernel", store the kernel pointer in data->kernel
2231 * for use in at_domain.
2233 static int before_mark(__isl_keep isl_id
*mark
,
2234 __isl_keep isl_ast_build
*build
, void *user
)
2236 struct ppcg_at_domain_data
*data
= user
;
2240 if (!strcmp(isl_id_get_name(mark
), "kernel"))
2241 data
->kernel
= isl_id_get_user(mark
);
2245 /* This function is called after the AST generator has finished traversing
2246 * the schedule subtree of a mark node. "node" points to the corresponding
2249 * If the mark is called "kernel", then replace "node" by a user node
2250 * that "calls" the kernel, representing the launch of the kernel.
2251 * The original "node" is stored inside the kernel object so that
2252 * it can be used to print the device code.
2253 * Note that this assumes that a kernel is only launched once.
2254 * Also clear data->kernel.
2256 static __isl_give isl_ast_node
*after_mark(__isl_take isl_ast_node
*node
,
2257 __isl_keep isl_ast_build
*build
, void *user
)
2262 isl_ast_expr_list
*list
;
2263 struct ppcg_kernel
*kernel
;
2264 struct ppcg_at_domain_data
*data
= user
;
2266 ctx
= isl_ast_node_get_ctx(node
);
2267 id
= isl_ast_node_mark_get_id(node
);
2269 return isl_ast_node_free(node
);
2270 if (strcmp(isl_id_get_name(id
), "kernel") || !data
->kernel
) {
2274 kernel
= data
->kernel
;
2275 data
->kernel
= NULL
;
2276 kernel
->space
= isl_ast_build_get_schedule_space(build
);
2277 kernel
->tree
= isl_ast_node_mark_get_node(node
);
2278 isl_ast_node_free(node
);
2280 expr
= isl_ast_expr_from_id(isl_id_copy(id
));
2281 list
= isl_ast_expr_list_alloc(ctx
, 0);
2282 expr
= isl_ast_expr_call(expr
, list
);
2283 node
= isl_ast_node_alloc_user(expr
);
2284 node
= isl_ast_node_set_annotation(node
, id
);
2289 static isl_bool
update_depth(__isl_keep isl_schedule_node
*node
, void *user
)
2294 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2295 return isl_bool_true
;
2296 node_depth
= isl_schedule_node_get_schedule_depth(node
);
2297 if (node_depth
> *depth
)
2298 *depth
= node_depth
;
2300 return isl_bool_false
;
2303 /* Use isl to generate code for both the host and the device
2305 * The device code is marked by "kernel" mark nodes in the schedule tree,
2306 * containing a pointer to a ppcg_kernel object.
2307 * The returned AST only contains the AST for the host code.
2308 * The ASTs for the device code are embedded in ppcg_kernel objects
2309 * attached to the leaf nodes that call "kernel".
2311 static __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
,
2312 __isl_take isl_schedule
*schedule
)
2314 struct ppcg_at_domain_data data
;
2315 isl_ast_build
*build
;
2317 isl_id_list
*iterators
;
2320 data
.prog
= gen
->prog
;
2324 if (isl_schedule_foreach_schedule_node_top_down(schedule
, &update_depth
,
2327 build
= isl_ast_build_alloc(gen
->prog
->ctx
);
2328 iterators
= ppcg_scop_generate_names(gen
->prog
->scop
, depth
, "c");
2329 build
= isl_ast_build_set_iterators(build
, iterators
);
2330 build
= isl_ast_build_set_at_each_domain(build
, &at_domain
, &data
);
2331 build
= isl_ast_build_set_before_each_mark(build
, &before_mark
, &data
);
2332 build
= isl_ast_build_set_after_each_mark(build
, &after_mark
, &data
);
2333 if (gen
->prog
->scop
->options
->debug
->dump_final_schedule
)
2334 isl_schedule_dump(schedule
);
2335 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2336 isl_ast_build_free(build
);
2341 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
2345 return isl_union_map_read_from_str(ctx
, str
);
2348 /* Can "node" be tiled and then mapped to block and thread identifiers?
2349 * That is, is it permutable with at least one coincident dimension?
2351 static int is_permutable(__isl_keep isl_schedule_node
*node
)
2356 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
2358 if (!isl_schedule_node_band_get_permutable(node
))
2360 if (isl_schedule_node_band_n_member(node
) < 1)
2362 if (!isl_schedule_node_band_member_get_coincident(node
, 0))
2368 /* A isl_schedule_foreach_schedule_node_top_down callback
2369 * for setting *any_permutable and aborting the search
2370 * if "node" is a permutable band with coincident dimensions.
2371 * Otherwise, continue searching.
2373 static isl_bool
set_permutable(__isl_keep isl_schedule_node
*node
, void *user
)
2375 int *any_permutable
= user
;
2378 permutable
= is_permutable(node
);
2380 return isl_bool_error
;
2382 return isl_bool_true
;
2384 *any_permutable
= 1;
2386 return isl_bool_error
;
2389 /* Does the subtree rooted at "node" have any suitably permutable band nodes?
2390 * That is, does it have any nodes that are permutable and that
2391 * have a least one coincident dimension?
2393 static int subtree_has_permutable_bands(__isl_keep isl_schedule_node
*node
)
2395 int any_parallelism
= 0;
2397 if (isl_schedule_node_foreach_descendant_top_down(node
, &set_permutable
,
2398 &any_parallelism
) < 0 &&
2402 return any_parallelism
;
2405 /* Does "schedule" contain any permutable band with at least one coincident
2408 static int has_any_permutable_node(__isl_keep isl_schedule
*schedule
)
2410 isl_schedule_node
*root
;
2413 root
= isl_schedule_get_root(schedule
);
2414 any_permutable
= subtree_has_permutable_bands(root
);
2415 isl_schedule_node_free(root
);
2417 return any_permutable
;
2420 /* Is "node" a candidate for mapping to block and thread identifiers?
2421 * In particular, is it permutable with at least one coincident dimension?
2422 * Alternatively, does the subtree rooted at "node" not contain
2423 * any such permutable node? Filter nodes are skipped in this case,
2424 * because a band node will be inserted in front of the returned
2425 * node and this is not possible for filter nodes that are children
2426 * of set or sequence nodes.
2428 static int is_candidate(__isl_keep isl_schedule_node
*node
)
2432 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
)
2434 permutable
= is_permutable(node
);
2435 if (permutable
< 0 || permutable
)
2437 if (isl_schedule_node_get_type(node
) == isl_schedule_node_filter
)
2439 permutable
= subtree_has_permutable_bands(node
);
2445 /* Is "node" the outermost node in its branch that can be tiled
2446 * and then mapped to block and thread identifiers?
2447 * If there are no such nodes in the subtree at "node" and
2448 * if "node" is not a filter node, then it is accepted too.
2450 static int is_outer_tilable(__isl_keep isl_schedule_node
*node
)
2453 isl_schedule_node
*ancestor
;
2455 tilable
= is_candidate(node
);
2462 ancestor
= isl_schedule_node_copy(node
);
2463 while (isl_schedule_node_has_parent(ancestor
)) {
2464 ancestor
= isl_schedule_node_parent(ancestor
);
2466 tilable
= is_candidate(ancestor
);
2467 if (tilable
< 0 || tilable
)
2471 isl_schedule_node_free(ancestor
);
2472 return tilable
< 0 ? -1 : !tilable
;
2475 /* Collect the references to all writes in "group".
2476 * Each reference is represented by a universe set in a space
2480 * with S[i,j] the statement instance space and R[] the array reference.
2482 static __isl_give isl_union_set
*group_tagged_writes(
2483 struct gpu_array_ref_group
*group
)
2487 isl_union_set
*writes
;
2489 space
= isl_map_get_space(group
->access
);
2490 writes
= isl_union_set_empty(space
);
2491 for (i
= 0; i
< group
->n_ref
; ++i
) {
2495 if (!group
->refs
[i
]->write
)
2498 space
= isl_map_get_space(group
->refs
[i
]->tagged_access
);
2499 space
= isl_space_domain(space
);
2500 writes_i
= isl_set_universe(space
);
2501 writes
= isl_union_set_add_set(writes
, writes_i
);
2507 /* Is there any write access in "group" that requires synchronization
2508 * on a write to global memory?
2509 * We currently take into account all writes that would require
2510 * synchronization at the thread level depth, but if the copying
2511 * for this group is performed at an outer level, then we do not
2512 * actually need to take into account dependences at intermediate levels.
2514 static int any_sync_writes_in_group(struct ppcg_kernel
*kernel
,
2515 struct gpu_array_ref_group
*group
)
2517 isl_union_set
*writes
;
2518 int empty
, disjoint
;
2520 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2526 writes
= group_tagged_writes(group
);
2527 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2528 isl_union_set_free(writes
);
2530 return disjoint
< 0 ? -1 : !disjoint
;
2533 /* Collect the references to all writes in "kernel" that write directly
2534 * to global or shared memory, i.e., that are not mapped to private memory.
2535 * Each reference is represented by a universe set in a space
2539 * with S[i,j] the statement instance space and R[] the array reference.
2541 static __isl_give isl_union_set
*collect_non_private_tagged_writes(
2542 struct ppcg_kernel
*kernel
)
2544 isl_union_set
*writes
;
2547 writes
= isl_union_set_empty(isl_union_set_get_space(kernel
->arrays
));
2549 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2550 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
2552 for (j
= 0; j
< array
->n_group
; ++j
) {
2553 struct gpu_array_ref_group
*group
= array
->groups
[j
];
2554 enum ppcg_group_access_type type
;
2555 isl_union_set
*writes_ij
;
2559 type
= gpu_array_ref_group_type(group
);
2560 if (type
== ppcg_access_private
)
2562 writes_ij
= group_tagged_writes(group
);
2563 writes
= isl_union_set_union(writes
, writes_ij
);
2570 /* Are there any direct writes to global memory that require
2573 static int any_global_or_shared_sync_writes(struct ppcg_kernel
*kernel
)
2575 isl_union_set
*writes
;
2576 int empty
, disjoint
;
2578 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2584 writes
= collect_non_private_tagged_writes(kernel
);
2585 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2586 isl_union_set_free(writes
);
2588 return disjoint
< 0 ? -1 : !disjoint
;
2591 /* Construct an isl_multi_val for use as tile sizes for tiling "node"
2592 * from the elements in "tile_size".
2594 static __isl_give isl_multi_val
*construct_band_tiles_sizes(
2595 __isl_keep isl_schedule_node
*node
, int *tile_size
)
2605 ctx
= isl_schedule_node_get_ctx(node
);
2606 space
= isl_schedule_node_band_get_space(node
);
2607 n
= isl_schedule_node_band_n_member(node
);
2608 mv
= isl_multi_val_zero(space
);
2609 for (i
= 0; i
< n
; ++i
) {
2612 v
= isl_val_int_from_si(ctx
, tile_size
[i
]);
2613 mv
= isl_multi_val_set_val(mv
, i
, v
);
2619 /* Replace the partial schedule S of the band node "node" by
2627 * if scale_tile_loops is set, with f the integers in "factor".
2628 * The list that "factor" points to is assumed to contain at least
2629 * as many elements as the number of members in the band.
2631 static __isl_give isl_schedule_node
*snap_band_to_sizes(
2632 __isl_take isl_schedule_node
*node
, int *factor
,
2633 struct ppcg_options
*options
)
2637 mv
= construct_band_tiles_sizes(node
, factor
);
2638 node
= isl_schedule_node_band_scale_down(node
, isl_multi_val_copy(mv
));
2639 if (options
->scale_tile_loops
)
2640 node
= isl_schedule_node_band_scale(node
,
2641 isl_multi_val_copy(mv
));
2642 isl_multi_val_free(mv
);
2647 /* Tile "band" with tile size specified by "sizes".
2649 * Since the tile loops will be mapped to block ids, we forcibly
2650 * turn off tile loop scaling. We may want to enable tile loop scaling
2651 * at some later point, but then we would have to support the detection
2652 * of strides during the mapping to block ids.
2653 * Similarly, since the point loops will be mapped to thread ids,
2654 * we forcibly shift the point loops so that they start at zero.
2656 static __isl_give isl_schedule_node
*tile_band(
2657 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2659 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
2663 scale_tile
= isl_options_get_tile_scale_tile_loops(ctx
);
2664 isl_options_set_tile_scale_tile_loops(ctx
, 0);
2665 shift_point
= isl_options_get_tile_shift_point_loops(ctx
);
2666 isl_options_set_tile_shift_point_loops(ctx
, 1);
2668 node
= isl_schedule_node_band_tile(node
, sizes
);
2670 isl_options_set_tile_scale_tile_loops(ctx
, scale_tile
);
2671 isl_options_set_tile_shift_point_loops(ctx
, shift_point
);
2676 /* Extract the set of parameter values and outer schedule dimensions
2677 * for which any statement instance
2678 * in the kernel inserted at "node" needs to be executed.
2679 * Intersect the set of parameter values derived from the host schedule
2680 * relation with the context of "prog".
2682 static __isl_give isl_set
*extract_context(__isl_keep isl_schedule_node
*node
,
2683 struct gpu_prog
*prog
)
2685 isl_union_map
*schedule
;
2686 isl_union_set
*schedule_domain
;
2690 schedule
= isl_schedule_node_get_prefix_schedule_relation(node
);
2691 schedule_domain
= isl_union_map_range(schedule
);
2692 empty
= isl_union_set_is_empty(schedule_domain
);
2694 isl_union_set_free(schedule_domain
);
2701 space
= isl_union_set_get_space(schedule_domain
);
2702 isl_union_set_free(schedule_domain
);
2703 space
= isl_space_set_from_params(space
);
2704 depth
= isl_schedule_node_get_schedule_depth(node
);
2705 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
2706 context
= isl_set_empty(space
);
2708 context
= isl_set_from_union_set(schedule_domain
);
2710 context
= isl_set_intersect_params(context
,
2711 isl_set_copy(prog
->context
));
2716 /* Return the set of outer array elements accessed by
2717 * by the statement instance in "domain" in "prog".
2719 static __isl_give isl_union_set
*accessed_by_domain(
2720 __isl_take isl_union_set
*domain
, struct gpu_prog
*prog
)
2722 isl_union_map
*access
;
2723 isl_union_set
*arrays
;
2725 access
= isl_union_map_union(isl_union_map_copy(prog
->read
),
2726 isl_union_map_copy(prog
->may_write
));
2727 access
= isl_union_map_intersect_domain(access
, domain
);
2728 arrays
= isl_union_map_range(access
);
2729 arrays
= isl_union_set_apply(arrays
,
2730 isl_union_map_copy(prog
->to_outer
));
2735 /* Return the number of outer band members of the band node "node"
2736 * that are marked coincident.
2738 static int n_outer_coincidence(__isl_keep isl_schedule_node
*node
)
2742 n
= isl_schedule_node_band_n_member(node
);
2744 for (i
= 0; i
< n
; ++i
)
2745 if (!isl_schedule_node_band_member_get_coincident(node
, i
))
2751 /* If the band node "node" has more than "n" members, then split off
2752 * the first "n" of them.
2754 static __isl_give isl_schedule_node
*split_band(
2755 __isl_take isl_schedule_node
*node
, int n
)
2759 dim
= isl_schedule_node_band_n_member(node
);
2761 node
= isl_schedule_node_band_split(node
, n
);
2766 /* Scale a band node that may have been split by split_band.
2767 * "sizes" are the scaling factors for the original node.
2768 * "node" either points to the original band node, or the outer
2769 * of the two pieces after splitting.
2771 * If the number of elements in "node" is smaller than the number of
2772 * elements in "sizes", then some splitting has occurred and we split
2773 * "sizes" in the same way.
2775 static __isl_give isl_schedule_node
*scale_band(
2776 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2780 n
= isl_multi_val_dim(sizes
, isl_dim_set
);
2781 dim
= isl_schedule_node_band_n_member(node
);
2783 isl_multi_val
*sizes2
;
2785 sizes2
= isl_multi_val_copy(sizes
);
2786 sizes
= isl_multi_val_drop_dims(sizes
,
2787 isl_dim_set
, dim
, n
- dim
);
2788 sizes2
= isl_multi_val_drop_dims(sizes2
, isl_dim_set
, 0, dim
);
2789 node
= isl_schedule_node_child(node
, 0);
2790 node
= isl_schedule_node_band_scale(node
, sizes2
);
2791 node
= isl_schedule_node_parent(node
);
2794 return isl_schedule_node_band_scale(node
, sizes
);
2797 /* Return an isl_multi_aff, with as elements the parameters in "space"
2798 * that have the names specified by the elements in "names".
2799 * If (some of) these parameters do not already appear in "space",
2800 * then they are added first.
2802 static __isl_give isl_multi_aff
*parameter_vector(__isl_take isl_space
*space
,
2803 __isl_keep isl_id_list
*names
)
2806 isl_local_space
*ls
;
2810 space
= isl_space_free(space
);
2812 n
= isl_id_list_n_id(names
);
2813 for (i
= 0; i
< n
; ++i
) {
2817 id
= isl_id_list_get_id(names
, i
);
2818 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2823 pos
= isl_space_dim(space
, isl_dim_param
);
2824 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2825 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2827 ma
= isl_multi_aff_zero(isl_space_copy(space
));
2828 ls
= isl_local_space_from_space(isl_space_domain(space
));
2829 for (i
= 0; i
< n
; ++i
) {
2834 id
= isl_id_list_get_id(names
, i
);
2835 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2837 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
2838 isl_dim_param
, pos
);
2839 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2841 isl_local_space_free(ls
);
2846 /* Return constraints on the domain elements that equate a sequence of
2847 * parameters called "names", to the partial schedule
2848 * of "node" modulo the integers in "size".
2849 * The number of elements in the array "size" should be equal
2850 * to the number of elements in "names".
2851 * The number of members of the band node "node" should be smaller
2852 * than or equal to this number. If it is smaller, then the first
2853 * elements of "names" are equated to zero.
2855 static __isl_give isl_union_set
*set_schedule_modulo(
2856 __isl_keep isl_schedule_node
*node
, __isl_keep isl_id_list
*names
,
2862 isl_multi_union_pw_aff
*mupa
, *mupa2
;
2864 isl_union_set
*domain
;
2868 n
= isl_id_list_n_id(names
);
2870 return isl_schedule_node_get_universe_domain(node
);
2871 n_zero
= n
- isl_schedule_node_band_n_member(node
);
2873 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
2874 mv
= construct_band_tiles_sizes(node
, size
+ n_zero
);
2875 mupa
= isl_multi_union_pw_aff_mod_multi_val(mupa
, mv
);
2877 space
= isl_multi_union_pw_aff_get_space(mupa
);
2878 space
= isl_space_params(space
);
2879 space
= isl_space_set_from_params(space
);
2880 space
= isl_space_add_dims(space
, isl_dim_set
, n_zero
);
2881 ma
= isl_multi_aff_zero(space
);
2883 domain
= isl_schedule_node_get_universe_domain(node
);
2884 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(
2885 isl_union_set_copy(domain
), ma
);
2886 mupa
= isl_multi_union_pw_aff_range_product(mupa2
, mupa
);
2888 space
= isl_multi_union_pw_aff_get_space(mupa
);
2889 ma
= parameter_vector(space
, names
);
2891 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(domain
, ma
);
2892 mupa
= isl_multi_union_pw_aff_sub(mupa
, mupa2
);
2894 return isl_multi_union_pw_aff_zero_union_set(mupa
);
2897 /* Insert a context node at "node" introducing the block and thread
2898 * identifiers along with their bounds, which are stored in kernel->grid_size
2899 * and kernel->block_dim.
2900 * Note that the bounds on the block identifiers may implicitly impose
2901 * constraints on the parameters. A guard needs to be inserted
2902 * in the schedule tree to ensure that those bounds hold at "node".
2903 * This guard is inserted in insert_guard.
2905 static __isl_give isl_schedule_node
*insert_context(struct ppcg_kernel
*kernel
,
2906 __isl_take isl_schedule_node
*node
)
2910 context
= isl_set_universe(isl_set_get_space(kernel
->context
));
2912 context
= add_bounded_parameters_dynamic(context
,
2913 kernel
->grid_size
, kernel
->block_ids
);
2914 context
= add_bounded_parameters(context
,
2915 kernel
->block_dim
, kernel
->thread_ids
);
2917 node
= isl_schedule_node_insert_context(node
, context
);
2922 /* Insert a guard that eliminates kernel launches where the kernel
2923 * obviously does not have any work to do.
2925 * In particular, eliminate kernel launches where there are obviously
2927 * Use the same block size constraints that are used to create the context
2928 * to ensure that all constraints implicit in the constructed context
2929 * are imposed by the guard.
2931 * Additionally, add other constraints that are valid
2932 * for each executed instance ("context"), as long as this does not result
2935 static __isl_give isl_schedule_node
*insert_guard(
2936 __isl_take isl_schedule_node
*node
, __isl_keep isl_set
*context
,
2937 __isl_keep isl_multi_pw_aff
*size
, struct ppcg_scop
*scop
)
2943 guard
= isl_set_copy(context
);
2944 guard
= isl_set_compute_divs(guard
);
2945 guard
= isl_set_from_basic_set(isl_set_simple_hull(guard
));
2947 nparam
= isl_set_dim(guard
, isl_dim_param
);
2948 n
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
2949 ids
= ppcg_scop_generate_names(scop
, n
, "__ppcg_tmp");
2950 guard
= add_bounded_parameters_dynamic(guard
, size
, ids
);
2951 isl_id_list_free(ids
);
2952 guard
= isl_set_project_out(guard
, isl_dim_param
, nparam
, n
);
2954 node
= isl_schedule_node_insert_guard(node
, guard
);
2959 /* Does any array reference group mapping require the band that is mapped
2960 * to threads to be unrolled?
2962 static int kernel_requires_unroll(struct ppcg_kernel
*kernel
)
2966 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2967 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
2969 for (j
= 0; j
< array
->n_group
; ++j
) {
2970 struct gpu_array_ref_group
*group
= array
->groups
[j
];
2971 if (gpu_array_ref_group_requires_unroll(group
))
2979 /* Mark the given band node "node" for unrolling by the AST generator and
2980 * then sink it to the leaves of the schedule tree.
2981 * All dimensions of "node" are assumed to be coincident, such that this
2982 * sinking is a valid operation.
2984 static __isl_give isl_schedule_node
*unroll(__isl_take isl_schedule_node
*node
)
2986 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
2988 node
= isl_schedule_node_band_sink(node
);
2993 /* Insert a synchronization node in the schedule tree of "node"
2994 * after the core computation of "kernel" at the level of the band
2995 * that is mapped to threads, except if that level is equal to
2996 * that of the band that is mapped to blocks or if there are no writes
2997 * to global or shared memory in the core computation that require
2999 * If there are any writes to shared memory and the shared memory
3000 * copying is performed at the same level, then synchronization
3001 * is needed between the core and the copying anyway, so we might
3002 * as well add it here. If the copying is performed at a higher
3003 * level, then different iterations of intermediate schedule dimensions
3004 * may have a different mapping from between shared memory elements and
3005 * threads, such that synchronization is required after the core.
3006 * "node" is assumed to point to the kernel node.
3008 static __isl_give isl_schedule_node
*add_sync(struct ppcg_kernel
*kernel
,
3009 __isl_take isl_schedule_node
*node
)
3014 need_sync
= any_global_or_shared_sync_writes(kernel
);
3016 return isl_schedule_node_free(node
);
3020 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3022 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3023 if (kernel_depth
== isl_schedule_node_get_schedule_depth(node
))
3024 return gpu_tree_move_up_to_kernel(node
);
3026 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3028 node
= gpu_tree_move_up_to_kernel(node
);
3033 /* Return a read ("read" is 1) or write access relation for "group"
3034 * with those accesses removed that are only needed to communicate data
3035 * within the subtree of the schedule rooted at "node".
3036 * Furthermore, include the prefix schedule at "node".
3037 * That is, return a relation of the form
3041 * with D the outer schedule dimensions at "node".
3043 static __isl_give isl_union_map
*anchored_non_local_accesses(
3044 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3045 __isl_take isl_schedule_node
*node
, int read
)
3047 isl_union_map
*access
;
3048 isl_union_map
*prefix
;
3050 access
= gpu_array_ref_group_access_relation(group
, read
, !read
);
3051 access
= remove_local_accesses_group(kernel
, group
, access
, node
, read
);
3052 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
3053 access
= isl_union_map_range_product(prefix
, access
);
3058 /* Given an array reference group "group", create a mapping
3060 * read[D -> A] -> [D -> A]
3062 * if "read" is set or
3064 * write[D -> A] -> [D -> A]
3066 * if "read" is not set.
3067 * D corresponds to the outer tile->depth dimensions of
3068 * the kernel schedule.
3070 static __isl_give isl_multi_aff
*create_from_access(isl_ctx
*ctx
,
3071 struct gpu_array_ref_group
*group
, int read
)
3073 struct gpu_array_tile
*tile
;
3077 tile
= gpu_array_ref_group_tile(group
);
3078 space
= isl_space_copy(group
->array
->space
);
3079 space
= isl_space_from_range(space
);
3080 space
= isl_space_add_dims(space
, isl_dim_in
, tile
->depth
);
3081 space
= isl_space_wrap(space
);
3082 space
= isl_space_map_from_set(space
);
3084 id
= isl_id_alloc(ctx
, read
? "read" : "write", group
);
3085 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
3087 return isl_multi_aff_identity(space
);
3090 /* If any writes in "group" require synchronization, then make sure
3091 * that there is a synchronization node for "kernel" after the node
3092 * following "node" in a sequence.
3094 * If "shared" is set and no synchronization is needed for
3095 * the writes to global memory, then add synchronization before
3096 * the kernel to protect shared memory from being overwritten
3097 * by the next iteration of the core computation.
3098 * No additional synchronization is needed to protect against
3099 * the next copy into shared memory because each element of
3100 * the shared memory tile is always copied by the same thread.
3102 static __isl_give isl_schedule_node
*add_group_write_sync(
3103 __isl_take isl_schedule_node
*node
, struct ppcg_kernel
*kernel
,
3104 struct gpu_array_ref_group
*group
, int shared
)
3108 need_sync
= any_sync_writes_in_group(kernel
, group
);
3110 return isl_schedule_node_free(node
);
3112 node
= isl_schedule_node_parent(node
);
3113 node
= isl_schedule_node_next_sibling(node
);
3114 node
= isl_schedule_node_child(node
, 0);
3115 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3116 } else if (shared
) {
3117 struct gpu_array_tile
*tile
;
3119 tile
= gpu_array_ref_group_tile(group
);
3120 node
= isl_schedule_node_parent(node
);
3121 node
= isl_schedule_node_parent(node
);
3122 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
,
3124 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3130 /* Add copy statements to the schedule tree of "node"
3131 * for reading from global memory to private memory (if "read" is set) or
3132 * for writing back from private memory to global memory
3133 * (if "read" is not set) for the array reference group "group" that
3134 * is mapped to private memory.
3135 * On input, "node" points to the kernel node, and it is moved
3136 * back there on output.
3138 * The copies are performed in the order of the array elements.
3139 * The copy statement instances include a reference to the outer
3140 * tile->depth dimensions of the kernel schedule for ease of
3141 * combining them with the group tiling.
3143 * That is, the extra schedule is of the form
3147 * where D corresponds to the outer tile->depth dimensions of
3148 * the kernel schedule and A to the global array.
3149 * This schedule is unrolled because registers are not addressable.
3151 * The copying is inserted in the schedule tree through an extension
3156 * where the extra domain elements type[D -> A] are those accessed
3158 * A filter is inserted on type[D -> A] to ensure that the element
3159 * is read/written by the same thread that needs the element.
3160 * This filter is obtained by applying
3164 * to the thread filter for the core statements.
3166 * The extension is inserted before the core computation in case of a read
3167 * and after the core computation in case of a write.
3168 * In the latter case, we also make sure that there is a synchronization
3169 * node after the write to global memory, unless this write is performed
3170 * at the outer level of the kernel.
3171 * In principle, this synchronization could be inserted higher
3172 * in the schedule tree depending on where the corresponding reads
3173 * from global memory are performed.
3175 static __isl_give isl_schedule_node
*add_copies_group_private(
3176 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3177 __isl_take isl_schedule_node
*node
, int read
)
3179 struct gpu_array_tile
*tile
;
3180 isl_union_map
*access
;
3181 isl_union_map
*prefix
;
3182 isl_union_set
*domain
;
3184 isl_multi_aff
*from_access
;
3185 isl_multi_pw_aff
*mpa
;
3186 isl_multi_union_pw_aff
*mupa
;
3187 isl_schedule_node
*graft
;
3188 isl_union_set
*filter
;
3192 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3193 tile
= gpu_array_ref_group_tile(group
);
3194 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3196 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3197 empty
= isl_union_map_is_empty(access
);
3198 if (empty
< 0 || empty
) {
3199 isl_union_map_free(access
);
3201 return isl_schedule_node_free(node
);
3202 return gpu_tree_move_up_to_kernel(node
);
3205 group
->array
->global
= 1;
3206 group
->local_array
->global
= 1;
3208 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3209 space
= isl_space_domain(isl_multi_aff_get_space(from_access
));
3210 access
= isl_union_map_preimage_range_multi_aff(access
, from_access
);
3212 filter
= isl_union_set_copy(kernel
->thread_filter
);
3213 filter
= isl_union_set_apply(filter
, isl_union_map_copy(access
));
3214 filter
= isl_union_set_detect_equalities(filter
);
3215 filter
= isl_union_set_coalesce(filter
);
3217 domain
= isl_union_map_range(access
);
3218 access
= isl_union_set_wrapped_domain_map(domain
);
3219 access
= isl_union_map_reverse(access
);
3220 access
= isl_union_map_coalesce(access
);
3221 graft
= isl_schedule_node_from_extension(access
);
3223 space
= isl_space_map_from_set(space
);
3224 mpa
= isl_multi_pw_aff_identity(space
);
3225 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
3226 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3228 graft
= isl_schedule_node_child(graft
, 0);
3229 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3230 graft
= unroll(graft
);
3232 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3234 graft
= isl_schedule_node_parent(graft
);
3237 node
= isl_schedule_node_graft_before(node
, graft
);
3239 node
= isl_schedule_node_graft_after(node
, graft
);
3240 if (kernel_depth
< tile
->depth
)
3241 node
= add_group_write_sync(node
, kernel
, group
, 0);
3244 node
= gpu_tree_move_up_to_kernel(node
);
3249 /* Add copy statements to the schedule tree of "node"
3250 * for reading from global memory to shared memory (if "read" is set) or
3251 * for writing back from shared memory to global memory
3252 * (if "read" is not set) for the array reference group "group" that
3253 * is mapped to shared memory.
3254 * On input, "node" points to the kernel node, and it is moved
3255 * back there on output.
3257 * The copies are performed in the order of the corresponding shared
3259 * The copy statement instances include a reference to the outer
3260 * tile->depth dimensions of the kernel schedule for ease of
3261 * combining them with the group tiling.
3263 * If we are performing a read from global memory to shared memory and
3264 * if the array involved is not a scalar, then we copy
3265 * the entire tile to shared memory. This may result in some extra
3266 * elements getting copied, but it should lead to simpler code
3267 * (which means that fewer registers may be needed) and less divergence.
3269 * Otherwise, we only copy the elements that will be read or have been written
3272 * That is, the extra schedule is of the form
3276 * where D corresponds to the outer tile->depth dimensions of
3277 * the kernel schedule, A to the global array and T is the corresponding
3278 * shared memory tile.
3280 * The copying is inserted in the schedule tree through an extension
3285 * where the extra domain elements type[D -> A] are those accessed
3286 * by the group. In the case of read from a non-scalar, this set
3287 * is replaced by the entire shared memory tile.
3289 * A filter is inserted on type[D -> A] to map the copy instances
3290 * to the threads. In particular, the thread identifiers are
3291 * equated to the position inside the shared memory tile (T)
3292 * modulo the block size.
3293 * We try to align the innermost tile dimension with the innermost
3294 * thread identifier (x) as a heuristic to improve coalescing.
3295 * In particular, if the dimension of the tile is greater than
3296 * the dimension of the block, then the schedule mapping to the tile
3297 * is broken up into two pieces and the filter is applied to the inner part.
3298 * If, on the other hand, the dimension of the tile is smaller than
3299 * the dimension of the block, then the initial thread identifiers
3300 * are equated to zero and the remaining thread identifiers are
3301 * matched to the memory tile.
3303 * The extension is inserted before the core computation in case of a read
3304 * and after the core computation in case of a write.
3305 * In the case of a read, we first need to make sure there is some
3306 * synchronization before the core computation such that we can put the read
3307 * from global memory to shared memory before that synchronization.
3308 * This ensures that all threads have finished copying into shared memory
3309 * before the shared memory is used.
3310 * We also need to make sure that there is a synchronization node after
3311 * the core computation to ensure that the next load into shared memory
3312 * only happens after all data has been used. There is no need for
3313 * this synchronization if we are at the outer level since then there
3314 * won't be a next load.
3315 * In the case of a write, we need to make sure there is some synchronization
3316 * after the core computation such taht we can put the write from shared
3317 * memory to global memory after that synchronization.
3318 * Unless we are at the outer level, we also need a synchronization node
3319 * after the write to ensure the data is saved to global memory
3320 * before the next iteration write to the same shared memory.
3321 * It also makes sure the data has arrived in global memory before
3322 * it is read in a subsequent iteration.
3324 static __isl_give isl_schedule_node
*add_copies_group_shared(
3325 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3326 __isl_take isl_schedule_node
*node
, int read
)
3328 struct gpu_array_tile
*tile
;
3329 isl_union_map
*access
;
3330 isl_union_set
*domain
;
3331 isl_union_set
*sync
;
3333 isl_multi_aff
*from_access
;
3334 isl_multi_pw_aff
*mpa
;
3335 isl_multi_union_pw_aff
*mupa
;
3336 isl_schedule_node
*graft
;
3337 isl_union_set
*filter
;
3342 tile
= gpu_array_ref_group_tile(group
);
3343 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3344 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3346 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3347 empty
= isl_union_map_is_empty(access
);
3348 if (empty
< 0 || empty
) {
3349 isl_union_map_free(access
);
3351 return isl_schedule_node_free(node
);
3352 return gpu_tree_move_up_to_kernel(node
);
3355 group
->array
->global
= 1;
3356 group
->local_array
->global
= 1;
3358 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3360 ma
= isl_multi_aff_copy(tile
->tiling
);
3361 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3362 isl_multi_aff_copy(from_access
));
3363 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3364 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3366 domain
= isl_union_map_range(access
);
3368 if (read
&& !gpu_array_is_scalar(group
->array
)) {
3370 isl_union_set_free(domain
);
3371 map
= group_tile(group
);
3372 domain
= isl_union_set_from_set(isl_map_wrap(map
));
3375 domain
= isl_union_set_preimage_multi_aff(domain
, from_access
);
3376 access
= isl_union_set_wrapped_domain_map(domain
);
3377 access
= isl_union_map_reverse(access
);
3378 access
= isl_union_map_coalesce(access
);
3379 graft
= isl_schedule_node_from_extension(access
);
3381 graft
= isl_schedule_node_child(graft
, 0);
3383 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3385 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0) {
3386 graft
= isl_schedule_node_band_split(graft
,
3387 tile
->n
- kernel
->n_block
);
3388 graft
= isl_schedule_node_child(graft
, 0);
3390 if (tile
->n
< kernel
->n_block
)
3391 skip
= kernel
->n_block
- tile
->n
;
3394 filter
= set_schedule_modulo(graft
, kernel
->thread_ids
,
3396 if (!kernel
->options
->wrap
)
3397 graft
= snap_band_to_sizes(graft
, kernel
->block_dim
+ skip
,
3399 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0)
3400 graft
= isl_schedule_node_parent(graft
);
3401 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3403 while (graft
&& isl_schedule_node_has_parent(graft
))
3404 graft
= isl_schedule_node_parent(graft
);
3407 if (kernel_depth
< tile
->depth
)
3408 node
= gpu_tree_ensure_sync_after_core(node
, kernel
);
3409 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3410 node
= isl_schedule_node_graft_before(node
, graft
);
3412 node
= gpu_tree_move_right_to_sync(node
, kernel
);
3413 node
= isl_schedule_node_graft_after(node
, graft
);
3414 if (kernel_depth
< tile
->depth
)
3415 node
= add_group_write_sync(node
, kernel
, group
, 1);
3418 node
= gpu_tree_move_up_to_kernel(node
);
3423 /* Check whether the array reference group "group" is mapped to
3424 * private or shared memory and, if so,
3425 * add copy statements to the schedule tree of "node"
3426 * for reading from global memory to private or shared memory
3427 * (if "read" is set) or for writing back from private or shared memory
3428 * to global memory (if "read" is not set) for this group.
3429 * On input, "node" points to the kernel node, and it is moved
3430 * back there on output.
3432 static __isl_give isl_schedule_node
*add_copies_group(
3433 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3434 __isl_take isl_schedule_node
*node
, int read
)
3436 enum ppcg_group_access_type type
;
3438 type
= gpu_array_ref_group_type(group
);
3439 if (type
== ppcg_access_private
)
3440 return add_copies_group_private(kernel
, group
, node
, read
);
3441 if (type
== ppcg_access_shared
)
3442 return add_copies_group_shared(kernel
, group
, node
, read
);
3446 /* For each array reference group that is mapped to private or shared memory,
3447 * add copy statements to the schedule tree of "node"
3448 * for reading from global memory to private or shared memory
3449 * and for writing back.
3450 * On input, "node" points to the kernel node, and it is moved
3451 * back there on output.
3453 static __isl_give isl_schedule_node
*add_copies(struct ppcg_kernel
*kernel
,
3454 __isl_take isl_schedule_node
*node
)
3458 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3459 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3461 for (j
= 0; j
< array
->n_group
; ++j
) {
3462 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3464 node
= add_copies_group(kernel
, group
, node
, 1);
3467 node
= add_copies_group(kernel
, group
, node
, 0);
3476 /* Mark all dimensions in the current band node atomic.
3478 static __isl_give isl_schedule_node
*atomic(__isl_take isl_schedule_node
*node
)
3480 return ppcg_set_schedule_node_type(node
, isl_ast_loop_atomic
);
3483 /* Mark "node" atomic, if it is a band node.
3484 * Do the same for all ancestors.
3485 * Return a pointer to "node" (in the updated schedule tree).
3487 static __isl_give isl_schedule_node
*atomic_ancestors(
3488 __isl_take isl_schedule_node
*node
)
3494 if (!isl_schedule_node_has_parent(node
))
3497 pos
= isl_schedule_node_get_child_position(node
);
3498 node
= isl_schedule_node_parent(node
);
3499 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
)
3500 node
= atomic(node
);
3501 node
= atomic_ancestors(node
);
3502 node
= isl_schedule_node_child(node
, pos
);
3507 /* Collect all write references that require synchronization.
3508 * "node" is assumed to point to the kernel node.
3509 * Each reference is represented by a universe set in a space
3513 * with S[i,j] the statement instance space and R[] the array reference.
3515 * This function should be called before block and thread filters are added.
3517 * Synchronization is needed after a write if there is a subsequent read
3518 * within the same block that may not be performed by the same thread.
3519 * There should not be any dependences between different blocks,
3520 * so we start with the flow dependences within the same kernel invocation
3521 * and we subtract from these those dependences that are mapped
3522 * to the same iteration of the bands where synchronization is inserted.
3523 * We do not remove pairs of instances that are known to map to
3524 * the same thread across different iterations of the intermediate
3525 * bands because the read may be performed by a different thread
3526 * than the one that needs the value if shared memory is involved.
3528 * We also consider all pairs of possible writes that access the same
3529 * memory location and that may be mapped to the same block but not
3530 * to the same iteration of the intermediate bands.
3531 * In theory, it would be possible for one thread to still be in
3532 * a previous iteration of a loop in these bands.
3533 * A write to global memory in this delayed thread could then overwrite
3534 * a write from another thread that has already moved on to
3535 * the next iteration.
3537 * After computing the above writes paired off with reads or writes
3538 * that depend on them, we project onto the domain writes.
3539 * Sychronization is needed after writes to global memory
3540 * through these references.
3542 static __isl_give isl_union_set
*compute_sync_writes(
3543 struct ppcg_kernel
*kernel
, __isl_keep isl_schedule_node
*node
)
3545 isl_union_map
*local
;
3546 isl_union_map
*may_writes
, *shared_access
;
3547 isl_union_map
*kernel_prefix
, *thread_prefix
;
3548 isl_union_map
*equal
;
3549 isl_union_set
*wrap
;
3550 isl_union_set
*domain
;
3552 domain
= isl_schedule_node_get_universe_domain(node
);
3553 kernel_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3554 node
= isl_schedule_node_copy(node
);
3555 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3556 thread_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3557 isl_schedule_node_free(node
);
3559 may_writes
= isl_union_map_copy(kernel
->prog
->scop
->tagged_may_writes
);
3560 may_writes
= isl_union_map_curry(may_writes
);
3561 may_writes
= isl_union_map_intersect_domain(may_writes
, domain
);
3562 may_writes
= isl_union_map_uncurry(may_writes
);
3563 shared_access
= isl_union_map_copy(may_writes
);
3564 shared_access
= isl_union_map_apply_range(shared_access
,
3565 isl_union_map_reverse(may_writes
));
3567 local
= isl_union_map_copy(kernel
->prog
->scop
->tagged_dep_flow
);
3568 local
= isl_union_map_union(local
, shared_access
);
3569 local
= isl_union_map_zip(local
);
3571 equal
= isl_union_map_apply_range(kernel_prefix
,
3572 isl_union_map_reverse(isl_union_map_copy(kernel_prefix
)));
3573 wrap
= isl_union_map_wrap(equal
);
3574 local
= isl_union_map_intersect_domain(local
, wrap
);
3575 equal
= isl_union_map_apply_range(thread_prefix
,
3576 isl_union_map_reverse(isl_union_map_copy(thread_prefix
)));
3577 wrap
= isl_union_map_wrap(equal
);
3578 local
= isl_union_map_subtract_domain(local
, wrap
);
3580 local
= isl_union_map_zip(local
);
3581 local
= isl_union_map_universe(local
);
3583 return isl_union_map_domain(local
);
3586 /* Group the domain elements into a single space, named kernelX,
3587 * with X the kernel sequence number "kernel_id".
3589 static __isl_give isl_schedule_node
*group_statements(
3590 __isl_take isl_schedule_node
*node
, int kernel_id
)
3598 snprintf(buffer
, sizeof(buffer
), "kernel%d", kernel_id
);
3599 id
= isl_id_alloc(isl_schedule_node_get_ctx(node
), buffer
, NULL
);
3600 return isl_schedule_node_group(node
, id
);
3603 /* Create a ppcg_kernel representing the domain instances that reach "node"
3604 * and insert a mark node pointing to the ppcg_kernel before "node".
3605 * The band that "node" points to is the band that needs to be mapped
3606 * to block identifiers. The band that needs to be mapped to thread
3607 * identifiers should be marked by a "thread" mark by the caller.
3608 * This mark is removed by this function.
3609 * If "scale" is set, then the band that "node" points to is scaled
3612 * Mark all outer band nodes as atomic to ensure each kernel is only
3614 * If the domain elements that reach "node" live in more than one space,
3615 * then group the domain elements into a single space, named kernelX,
3616 * with X the kernel sequence number.
3618 * Insert a guard node governing the kernel node to ensure that
3619 * no kernels with zero blocks are launched.
3621 * Insert a context node describing the block and thread
3622 * identifiers inside the kernel mark.
3623 * The context node needs to be inserted after the effective block size
3624 * has been determined such that the bounds on the thread identifiers
3625 * would reflect the effective block size.
3626 * Insert a filter node inside the context node mapping the statement
3627 * instances to block identifiers. In particular, the block identifiers
3628 * are equated to the partial schedule of band that was marked for mapping
3629 * to blocks modulo the grid size.
3630 * Insert a filter node inside the "thread" mark mapping the statement
3631 * instances to thread identifiers. In particular, the thread identifiers
3632 * are equated to the partial schedule of band that was marked for mapping
3633 * to threads modulo the block size.
3635 * Compute array reference groups for all arrays, set the local
3636 * array bounds based on the set of domain instances that reach
3637 * the kernel node, check the total amount of shared memory used
3638 * and compute all group tilings.
3639 * The array reference groups are computed after the block filter
3640 * has been inserted because it affects the mapping to shared or
3641 * private memory. This computation also requires the thread filter
3642 * (in the ppcg_kernel object), but this thread filter should not
3643 * have been added to the schedule tree yet since the computation
3644 * requires the schedule of the band that needs to be mapped to
3645 * threads before the privatization is applied.
3647 * If any array reference group requires the band mapped to threads
3648 * to be unrolled, then we perform the required unrolling.
3650 * We save a copy of the schedule that may influence the mappings
3651 * to shared or private memory in kernel->copy_schedule.
3653 * Finally, we add synchronization and copy statements to the schedule tree,
3654 * remove the "thread" mark and create representations for the local
3655 * variables in the kernel.
3657 * We keep a copy of the isl_id that points to the kernel to ensure
3658 * that the kernel does not get destroyed if the schedule node
3659 * is freed due to some error condition.
3661 static __isl_give isl_schedule_node
*create_kernel(struct gpu_gen
*gen
,
3662 __isl_take isl_schedule_node
*node
, int scale
,
3663 __isl_keep isl_multi_val
*sizes
)
3665 struct ppcg_kernel
*kernel
;
3667 isl_schedule_node
*node_thread
;
3668 isl_union_map
*host_schedule
;
3669 isl_set
*host_domain
;
3670 isl_union_set
*domain
;
3671 int single_statement
;
3673 kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
3674 kernel
= ppcg_kernel_create_local_arrays(kernel
, gen
->prog
);
3676 return isl_schedule_node_free(node
);
3678 domain
= isl_schedule_node_get_domain(node
);
3679 single_statement
= isl_union_set_n_set(domain
) == 1;
3681 kernel
->ctx
= gen
->ctx
;
3682 kernel
->prog
= gen
->prog
;
3683 kernel
->options
= gen
->options
;
3684 kernel
->context
= extract_context(node
, gen
->prog
);
3685 kernel
->core
= isl_union_set_universe(isl_union_set_copy(domain
));
3686 kernel
->arrays
= accessed_by_domain(isl_union_set_copy(domain
),
3688 kernel
->n_grid
= n_outer_coincidence(node
);
3689 node_thread
= isl_schedule_node_copy(node
);
3690 node_thread
= gpu_tree_move_down_to_thread(node_thread
, kernel
->core
);
3691 node_thread
= isl_schedule_node_child(node_thread
, 0);
3692 kernel
->n_block
= n_outer_coincidence(node_thread
);
3693 isl_schedule_node_free(node_thread
);
3694 kernel
->id
= gen
->kernel_id
++;
3695 read_grid_and_block_sizes(kernel
, gen
);
3697 kernel
->sync_writes
= compute_sync_writes(kernel
, node
);
3699 host_schedule
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3700 host_domain
= isl_set_from_union_set(isl_union_map_range(
3703 node
= atomic_ancestors(node
);
3705 id
= isl_id_alloc(gen
->ctx
, "kernel", kernel
);
3706 id
= isl_id_set_free_user(id
, &ppcg_kernel_free_wrap
);
3707 node
= isl_schedule_node_insert_mark(node
, isl_id_copy(id
));
3709 if (!single_statement
)
3710 node
= group_statements(node
, kernel
->id
);
3712 node
= isl_schedule_node_child(node
, 0);
3713 node
= split_band(node
, kernel
->n_grid
);
3714 kernel
->block_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3715 kernel
->n_grid
, "b");
3716 kernel
->block_filter
= set_schedule_modulo(node
, kernel
->block_ids
,
3718 kernel
->grid_size
= extract_grid_size(kernel
,
3719 isl_union_set_copy(domain
));
3720 if (!kernel
->options
->wrap
)
3721 node
= snap_band_to_sizes(node
, kernel
->grid_dim
,
3724 node
= scale_band(node
, isl_multi_val_copy(sizes
));
3725 node
= isl_schedule_node_parent(node
);
3726 if (!single_statement
)
3727 node
= isl_schedule_node_parent(node
);
3728 node
= insert_guard(node
, kernel
->context
, kernel
->grid_size
,
3730 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3731 node
= isl_schedule_node_child(node
, 0);
3732 node
= split_band(node
, kernel
->n_block
);
3733 kernel
->thread_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3734 kernel
->n_block
, "t");
3735 kernel
->thread_filter
= set_schedule_modulo(node
, kernel
->thread_ids
,
3737 if (extract_block_size(kernel
, domain
) < 0)
3738 node
= isl_schedule_node_free(node
);
3740 node
= gpu_tree_move_up_to_kernel(node
);
3741 node
= isl_schedule_node_child(node
, 0);
3742 node
= insert_context(kernel
, node
);
3743 node
= isl_schedule_node_child(node
, 0);
3744 node
= isl_schedule_node_insert_filter(node
,
3745 isl_union_set_copy(kernel
->block_filter
));
3747 node
= gpu_tree_move_up_to_kernel(node
);
3749 if (gpu_group_references(kernel
, node
) < 0)
3750 node
= isl_schedule_node_free(node
);
3751 localize_bounds(kernel
, host_domain
);
3752 isl_set_free(host_domain
);
3754 check_shared_memory_bound(kernel
);
3755 mark_global_arrays(kernel
);
3756 compute_group_tilings(kernel
);
3758 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3759 node
= isl_schedule_node_child(node
, 0);
3760 if (!kernel
->options
->wrap
)
3761 node
= snap_band_to_sizes(node
, kernel
->block_dim
,
3763 node
= isl_schedule_node_insert_filter(node
,
3764 isl_union_set_copy(kernel
->thread_filter
));
3765 if (kernel_requires_unroll(kernel
)) {
3766 node
= isl_schedule_node_child(node
, 0);
3767 node
= unroll(node
);
3770 node
= gpu_tree_move_up_to_thread(node
);
3771 kernel
->copy_schedule_dim
= isl_schedule_node_get_schedule_depth(node
);
3772 kernel
->copy_schedule
=
3773 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
3775 node
= gpu_tree_move_up_to_kernel(node
);
3777 node
= add_sync(kernel
, node
);
3778 node
= add_copies(kernel
, node
);
3780 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3781 node
= isl_schedule_node_delete(node
);
3783 node
= gpu_tree_move_up_to_kernel(node
);
3785 if (create_kernel_vars(kernel
) < 0)
3786 node
= isl_schedule_node_free(node
);
3788 if (!single_statement
)
3789 node
= isl_schedule_node_parent(node
);
3790 node
= isl_schedule_node_parent(node
);
3796 /* Insert a zero-dimensional permutable band at "node".
3798 static __isl_give isl_schedule_node
*insert_empty_permutable_band(
3799 __isl_take isl_schedule_node
*node
)
3802 isl_schedule
*schedule
;
3803 isl_union_set
*domain
;
3804 isl_multi_union_pw_aff
*mupa
;
3806 schedule
= isl_schedule_node_get_schedule(node
);
3807 domain
= isl_schedule_get_domain(schedule
);
3808 space
= isl_union_set_get_space(domain
);
3809 isl_union_set_free(domain
);
3810 isl_schedule_free(schedule
);
3812 space
= isl_space_set_from_params(space
);
3813 mupa
= isl_multi_union_pw_aff_zero(space
);
3814 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3815 node
= isl_schedule_node_band_set_permutable(node
, 1);
3820 /* If "node" is the outermost permutable band that can be mapped to block and
3821 * thread identifiers in its branch (or the root of a subtree with
3822 * no such outer bands),
3823 * then mark the band as such, attaching a ppcg_kernel to the mark.
3825 * If "node" is the root of a subtree without permutable bands,
3826 * then insert a zero-dimensional permutable band such that
3827 * we can assume that "node" always points to a band node.
3828 * This includes the case where "node" already points to a band node,
3829 * but one without any coincident dimension. In this case,
3830 * the extra node ensures that this original node does not get tiled.
3832 * Tile "node" using user specified tile sizes, after splitting the band
3833 * if the number of specified tile sizes is smaller than the dimension
3834 * of the band. Mark the point band of this tiling as the band that
3835 * needs to be mapped to threads.
3836 * Create a kernel representing the domain instances that reach "node" and
3837 * insert a mark node pointing to the ppcg_kernel before the band node.
3839 static __isl_give isl_schedule_node
*mark_outer_permutable(
3840 __isl_take isl_schedule_node
*node
, void *user
)
3842 struct gpu_gen
*gen
= user
;
3848 isl_multi_val
*sizes
;
3850 outer
= is_outer_tilable(node
);
3852 return isl_schedule_node_free(node
);
3856 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
||
3857 !isl_schedule_node_band_member_get_coincident(node
, 0))
3858 node
= insert_empty_permutable_band(node
);
3860 tile_len
= isl_schedule_node_band_n_member(node
);
3861 tile_size
= read_tile_sizes(gen
, &tile_len
);
3863 return isl_schedule_node_free(node
);
3864 if (tile_len
< isl_schedule_node_band_n_member(node
))
3865 node
= isl_schedule_node_band_split(node
, tile_len
);
3866 sizes
= construct_band_tiles_sizes(node
, tile_size
);
3867 node
= tile_band(node
, isl_multi_val_copy(sizes
));
3868 node
= isl_schedule_node_child(node
, 0);
3869 id
= isl_id_alloc(gen
->ctx
, "thread", NULL
);
3870 node
= isl_schedule_node_insert_mark(node
, id
);
3871 node
= isl_schedule_node_parent(node
);
3873 scale
= gen
->options
->scale_tile_loops
;
3874 node
= create_kernel(gen
, node
, scale
, sizes
);
3875 isl_multi_val_free(sizes
);
3881 /* Given a set or sequence node, return the union the filters of either all
3882 * (if "only_initial" is not set) or the initial (if "only_initial" is set)
3883 * direct subtrees that do not contain any suitably permutable bands
3884 * (according to subtree_has_permutable_bands).
3886 static __isl_give isl_union_set
*get_non_parallel_subtree_filters(
3887 __isl_keep isl_schedule_node
*node
, int only_initial
)
3890 isl_union_set
*filter
;
3893 n
= isl_schedule_node_n_children(node
);
3897 node
= isl_schedule_node_copy(node
);
3898 node
= isl_schedule_node_child(node
, 0);
3899 filter
= isl_schedule_node_filter_get_filter(node
);
3900 node
= isl_schedule_node_parent(node
);
3901 space
= isl_union_set_get_space(filter
);
3902 isl_union_set_free(filter
);
3903 filter
= isl_union_set_empty(space
);
3905 for (i
= 0; i
< n
; ++i
) {
3908 node
= isl_schedule_node_child(node
, i
);
3909 parallelism
= subtree_has_permutable_bands(node
);
3910 if (parallelism
< 0) {
3911 filter
= isl_union_set_free(filter
);
3912 } else if (!parallelism
) {
3913 isl_union_set
*filter_i
;
3914 filter_i
= isl_schedule_node_filter_get_filter(node
);
3915 filter
= isl_union_set_union(filter
, filter_i
);
3916 } else if (only_initial
)
3918 node
= isl_schedule_node_parent(node
);
3921 isl_schedule_node_free(node
);
3926 /* Given a set or sequence node, return the union of the filters of
3927 * the direct subtrees that do not contain any suitably permutable bands
3928 * (according to subtree_has_permutable_bands).
3930 static __isl_give isl_union_set
*get_all_non_parallel_subtree_filters(
3931 __isl_keep isl_schedule_node
*node
)
3933 return get_non_parallel_subtree_filters(node
, 0);
3936 /* Given a set or sequence node, return the union of the filters of
3937 * the initial direct subtrees that do not contain any suitably permutable
3938 * bands (according to subtree_has_permutable_bands).
3940 static __isl_give isl_union_set
*get_initial_non_parallel_subtree_filters(
3941 __isl_keep isl_schedule_node
*node
)
3943 return get_non_parallel_subtree_filters(node
, 1);
3946 /* Mark all variables that are accessed by the statement instances in "domain"
3947 * and that are local to "prog" as requiring a declaration in the host code.
3949 static int declare_accessed_local_variables(struct gpu_prog
*prog
,
3950 __isl_keep isl_union_set
*domain
)
3952 isl_union_set
*arrays
;
3955 if (!ppcg_scop_any_hidden_declarations(prog
->scop
))
3957 arrays
= accessed_by_domain(isl_union_set_copy(domain
), prog
);
3959 for (i
= 0; i
< prog
->n_array
; ++i
) {
3964 if (!prog
->array
[i
].local
)
3966 space
= isl_set_get_space(prog
->array
[i
].extent
);
3967 set
= isl_union_set_extract_set(arrays
, space
);
3968 empty
= isl_set_plain_is_empty(set
);
3973 prog
->array
[i
].declare_local
= 1;
3976 isl_union_set_free(arrays
);
3979 isl_union_set_free(arrays
);
3983 /* If "node" points to a set node, then separate its children
3984 * into subtrees that have suitably permutable bands and
3985 * those that do not.
3986 * Adjust the schedule tree in order to execute the second group
3987 * after the first group and return a pointer to the first group,
3988 * assuming there are any such subtrees.
3989 * If "node" points to a sequence node, then separate the initial
3990 * children that do not have suitably permutable bands and
3991 * return a pointer to the subsequence of children that do have such bands,
3992 * assuming there are any such subtrees.
3994 * In both cases, mark all local variables in "prog" that are accessed by
3995 * the group without permutable bands as requiring a declaration on the host.
3997 static __isl_give isl_schedule_node
*isolate_permutable_subtrees(
3998 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
)
4000 isl_union_set
*filter
;
4001 enum isl_schedule_node_type type
;
4005 type
= isl_schedule_node_get_type(node
);
4006 if (type
== isl_schedule_node_set
) {
4007 filter
= get_all_non_parallel_subtree_filters(node
);
4009 node
= isl_schedule_node_free(node
);
4011 if (declare_accessed_local_variables(prog
, filter
) < 0)
4012 node
= isl_schedule_node_free(node
);
4013 node
= isl_schedule_node_order_after(node
, filter
);
4014 } else if (type
== isl_schedule_node_sequence
) {
4015 filter
= get_initial_non_parallel_subtree_filters(node
);
4017 node
= isl_schedule_node_free(node
);
4019 if (declare_accessed_local_variables(prog
, filter
) < 0)
4020 node
= isl_schedule_node_free(node
);
4021 node
= isl_schedule_node_order_before(node
, filter
);
4027 /* Replace any reference to an array element in the range of "copy"
4028 * by a reference to all array elements (defined by the extent of the array).
4030 static __isl_give isl_union_map
*approximate_copy_out(
4031 __isl_take isl_union_map
*copy
, struct gpu_prog
*prog
)
4036 res
= isl_union_map_empty(isl_union_map_get_space(copy
));
4038 for (i
= 0; i
< prog
->n_array
; ++i
) {
4041 isl_union_map
*copy_i
;
4042 isl_union_set
*extent
, *domain
;
4044 space
= isl_space_copy(prog
->array
[i
].space
);
4045 extent
= isl_union_set_from_set(isl_set_universe(space
));
4046 copy_i
= isl_union_map_copy(copy
);
4047 copy_i
= isl_union_map_intersect_range(copy_i
, extent
);
4048 set
= isl_set_copy(prog
->array
[i
].extent
);
4049 extent
= isl_union_set_from_set(set
);
4050 domain
= isl_union_map_domain(copy_i
);
4051 copy_i
= isl_union_map_from_domain_and_range(domain
, extent
);
4052 res
= isl_union_map_union(res
, copy_i
);
4055 isl_union_map_free(copy
);
4060 /* Insert "kernel" marks that point to a ppcg_kernel structure
4061 * in front of all outermost tilable band that (by construction)
4062 * have at least one parallel loop.
4064 static __isl_give isl_schedule_node
*mark_kernels(struct gpu_gen
*gen
,
4065 __isl_take isl_schedule_node
*node
)
4067 return isl_schedule_node_map_descendant_bottom_up(node
,
4068 &mark_outer_permutable
, gen
);
4071 /* Construct schedule constraints from the dependences in prog->scop and
4072 * the array order dependences in prog->array_order.
4074 * If live range reordering is allowed, then we need to make sure
4075 * that live ranges on arrays are not run in parallel since doing
4076 * so would require array expansion. We therefore add the array
4077 * order dependences to the coincidence dependences. Non-zero array
4078 * order dependences will then prevent a schedule dimension from being
4079 * considered parallel.
4080 * Live ranges derived from scalars are allowed to be run in parallel
4081 * since we force the scalars to be mapped to private memory in
4082 * check_scalar_live_ranges.
4083 * If live range reordering is allowed, then the false dependences
4084 * are not added to the validity constraints as that would prevent
4085 * reordering. Instead, the external false dependences that enforce that reads
4086 * from potentially live-in data precede any later write and
4087 * that writes of potentially live-out data follow any other earlier write
4088 * are added to the validity and the coincidence constraints.
4089 * The false dependences are still added to the proximity constraints
4090 * for consistency with the case where live range reordering is not allowed.
4091 * The coincidence constraints then consist of flow dependences,
4092 * external false dependences and array order dependences.
4093 * The independences can be filtered out from the first two sets.
4094 * They have already been filtered out from the array order dependences
4095 * on a per array basis in collect_order_dependences.
4096 * There is no need for a per array handling of the other two sets
4097 * as there should be no flow or external false dependence on local
4098 * variables that can be filtered out.
4100 static __isl_give isl_schedule_constraints
*construct_schedule_constraints(
4101 struct gpu_prog
*prog
)
4103 isl_union_set
*domain
;
4104 isl_union_map
*dep_raw
, *dep
;
4105 isl_union_map
*validity
, *proximity
, *coincidence
;
4106 isl_schedule_constraints
*sc
;
4108 domain
= isl_union_set_copy(prog
->scop
->domain
);
4109 sc
= isl_schedule_constraints_on_domain(domain
);
4110 sc
= isl_schedule_constraints_set_context(sc
,
4111 isl_set_copy(prog
->scop
->context
));
4112 if (prog
->scop
->options
->live_range_reordering
) {
4113 sc
= isl_schedule_constraints_set_conditional_validity(sc
,
4114 isl_union_map_copy(prog
->scop
->tagged_dep_flow
),
4115 isl_union_map_copy(prog
->scop
->tagged_dep_order
));
4116 proximity
= isl_union_map_copy(prog
->scop
->dep_flow
);
4117 validity
= isl_union_map_copy(proximity
);
4118 validity
= isl_union_map_union(validity
,
4119 isl_union_map_copy(prog
->scop
->dep_forced
));
4120 proximity
= isl_union_map_union(proximity
,
4121 isl_union_map_copy(prog
->scop
->dep_false
));
4122 coincidence
= isl_union_map_copy(validity
);
4123 coincidence
= isl_union_map_subtract(coincidence
,
4124 isl_union_map_copy(prog
->scop
->independence
));
4125 coincidence
= isl_union_map_union(coincidence
,
4126 isl_union_map_copy(prog
->array_order
));
4128 dep_raw
= isl_union_map_copy(prog
->scop
->dep_flow
);
4129 dep
= isl_union_map_copy(prog
->scop
->dep_false
);
4130 dep
= isl_union_map_union(dep
, dep_raw
);
4131 dep
= isl_union_map_coalesce(dep
);
4132 proximity
= isl_union_map_copy(dep
);
4133 coincidence
= isl_union_map_copy(dep
);
4136 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4137 sc
= isl_schedule_constraints_set_coincidence(sc
, coincidence
);
4138 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4140 if (prog
->scop
->options
->debug
->dump_schedule_constraints
)
4141 isl_schedule_constraints_dump(sc
);
4145 /* Compute an appropriate schedule based on the accesses in
4146 * gen->read and gen->write.
4148 * We derive schedule constraints from the dependences in gen->prog->scop
4149 * and then use isl to compute a schedule that has a parallel loop
4150 * in each tilable band.
4152 static __isl_give isl_schedule
*compute_schedule(struct gpu_gen
*gen
)
4154 isl_schedule_constraints
*sc
;
4155 isl_schedule
*schedule
;
4157 sc
= construct_schedule_constraints(gen
->prog
);
4158 schedule
= isl_schedule_constraints_compute_schedule(sc
);
4163 /* If the band node "node" has exactly one member then mark it permutable.
4165 static __isl_give isl_schedule_node
*band_set_permutable(
4166 __isl_take isl_schedule_node
*node
,
4167 __isl_keep isl_schedule_constraints
*sc
)
4169 if (isl_schedule_node_band_n_member(node
) == 1)
4170 node
= isl_schedule_node_band_set_permutable(node
, 1);
4175 /* Return the coincidence constraints between pairs of instances
4176 * that are scheduled together by the ancestors of "node".
4177 * That is, select those coincidence constraints that relate
4178 * pairs of instances that have the same value for the prefix schedule.
4179 * If the schedule depth is zero, then the prefix schedule does not
4180 * contain any information, so we intersect domain and range
4181 * of the schedule constraints with the reaching domain elements instead.
4183 static __isl_give isl_union_map
*get_local_coincidence(
4184 __isl_keep isl_schedule_node
*node
,
4185 __isl_keep isl_schedule_constraints
*sc
)
4187 isl_union_map
*coincidence
;
4188 isl_multi_union_pw_aff
*prefix
;
4189 isl_union_pw_multi_aff
*contraction
;
4191 coincidence
= isl_schedule_constraints_get_coincidence(sc
);
4192 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4193 if (isl_schedule_node_get_schedule_depth(node
) == 0) {
4194 isl_union_set
*domain
;
4196 domain
= isl_schedule_node_get_domain(node
);
4197 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4199 coincidence
= isl_union_map_intersect_domain(coincidence
,
4200 isl_union_set_copy(domain
));
4201 coincidence
= isl_union_map_intersect_range(coincidence
,
4206 prefix
= isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
4207 prefix
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
4209 return isl_union_map_eq_at_multi_union_pw_aff(coincidence
, prefix
);
4212 /* For each member in the band node "node", determine whether
4213 * it is coincident with respect to the outer nodes and mark
4216 * That is, for each coincidence constraint between pairs
4217 * of instances that are scheduled together by the outer nodes,
4218 * check that domain and range are assigned the same value
4219 * by the band member. This test is performed by checking
4220 * that imposing the same value for the band member does not
4221 * remove any elements from the set of coincidence constraints.
4223 static __isl_give isl_schedule_node
*band_set_coincident(
4224 __isl_take isl_schedule_node
*node
,
4225 __isl_keep isl_schedule_constraints
*sc
)
4227 isl_union_map
*coincidence
;
4228 isl_union_pw_multi_aff
*contraction
;
4229 isl_multi_union_pw_aff
*partial
;
4232 coincidence
= get_local_coincidence(node
, sc
);
4234 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4235 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4236 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4238 n
= isl_schedule_node_band_n_member(node
);
4239 for (i
= 0; i
< n
; ++i
) {
4240 isl_union_map
*coincidence_i
;
4241 isl_union_pw_aff
*upa
;
4242 isl_multi_union_pw_aff
*partial_i
;
4245 upa
= isl_multi_union_pw_aff_get_union_pw_aff(partial
, i
);
4246 partial_i
= isl_multi_union_pw_aff_from_union_pw_aff(upa
);
4247 coincidence_i
= isl_union_map_copy(coincidence
);
4248 coincidence_i
= isl_union_map_eq_at_multi_union_pw_aff(
4249 coincidence_i
, partial_i
);
4250 subset
= isl_union_map_is_subset(coincidence
, coincidence_i
);
4251 isl_union_map_free(coincidence_i
);
4255 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
4259 node
= isl_schedule_node_free(node
);
4260 isl_multi_union_pw_aff_free(partial
);
4261 isl_union_map_free(coincidence
);
4266 /* If "node" is a band, then set its properties.
4268 * In particular, if the band has exactly one member, then mark it permutable.
4269 * Mark the band member coincident based on the coincidence constraints
4272 static __isl_give isl_schedule_node
*set_band_properties(
4273 __isl_take isl_schedule_node
*node
, void *user
)
4275 isl_schedule_constraints
*sc
= user
;
4277 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
4279 if (isl_schedule_node_band_n_member(node
) == 0)
4282 node
= band_set_permutable(node
, sc
);
4283 node
= band_set_coincident(node
, sc
);
4288 /* Return the original schedule with all bands marked permutable and
4289 * all band members marked coincident based on the coincidence constraints.
4290 * The bands are explicitly marked permutable so that they will be considered
4291 * by mark_outer_permutable.
4293 static __isl_give isl_schedule
*determine_properties_original_schedule(
4294 struct gpu_gen
*gen
)
4296 isl_schedule
*schedule
;
4297 isl_schedule_constraints
*sc
;
4299 schedule
= isl_schedule_copy(gen
->prog
->scop
->schedule
);
4300 sc
= construct_schedule_constraints(gen
->prog
);
4301 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
4302 &set_band_properties
, sc
);
4303 isl_schedule_constraints_free(sc
);
4308 /* Compute a schedule or determine the properties of the original schedule
4309 * depending on the value of the "reschedule" option.
4311 static __isl_give isl_schedule
*compute_or_set_properties(void *user
)
4313 struct gpu_gen
*gen
= user
;
4315 if (gen
->options
->reschedule
)
4316 return compute_schedule(gen
);
4318 return determine_properties_original_schedule(gen
);
4321 /* Obtain a schedule for the scop, by reading it from
4322 * a file, by computing one or by determining the properties
4323 * of the original schedule.
4325 static __isl_give isl_schedule
*get_schedule(struct gpu_gen
*gen
)
4327 return ppcg_get_schedule(gen
->ctx
, gen
->options
,
4328 &compute_or_set_properties
, gen
);
4331 /* Construct the string "<a>_<b>".
4333 static char *concat(isl_ctx
*ctx
, const char *a
, const char *b
)
4338 p
= isl_printer_to_str(ctx
);
4339 p
= isl_printer_print_str(p
, a
);
4340 p
= isl_printer_print_str(p
, "_");
4341 p
= isl_printer_print_str(p
, b
);
4342 s
= isl_printer_get_str(p
);
4343 isl_printer_free(p
);
4348 /* For each array in "prog" of which an element appears in "accessed" and
4349 * that is not a read only scalar, create a zero-dimensional universe set
4350 * of which the tuple id has name "<prefix>_<name of array>" and a user
4351 * pointer pointing to the array (gpu_array_info).
4353 * If the array is local to "prog", then make sure it will be declared
4356 * Return the list of these universe sets.
4358 static __isl_give isl_union_set_list
*create_copy_filters(struct gpu_prog
*prog
,
4359 const char *prefix
, __isl_take isl_union_set
*accessed
)
4363 isl_union_set_list
*filters
;
4366 filters
= isl_union_set_list_alloc(ctx
, 0);
4367 for (i
= 0; i
< prog
->n_array
; ++i
) {
4368 struct gpu_array_info
*array
= &prog
->array
[i
];
4370 isl_set
*accessed_i
;
4374 isl_union_set
*uset
;
4376 if (gpu_array_is_read_only_scalar(array
))
4379 space
= isl_space_copy(array
->space
);
4380 accessed_i
= isl_union_set_extract_set(accessed
, space
);
4381 empty
= isl_set_plain_is_empty(accessed_i
);
4382 isl_set_free(accessed_i
);
4384 filters
= isl_union_set_list_free(filters
);
4392 array
->declare_local
= 1;
4394 name
= concat(ctx
, prefix
, array
->name
);
4395 id
= name
? isl_id_alloc(ctx
, name
, array
) : NULL
;
4397 space
= isl_space_set_alloc(ctx
, 0, 0);
4398 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4399 uset
= isl_union_set_from_set(isl_set_universe(space
));
4401 filters
= isl_union_set_list_add(filters
, uset
);
4403 isl_union_set_free(accessed
);
4408 /* Make sure that code for the statements in "filters" that
4409 * copy arrays to or from the device is only generated when
4410 * the size of the corresponding array is positive.
4411 * That is, add a set node underneath "graft" with "filters" as children
4412 * and for each child add a guard that the selects the parameter
4413 * values for which the corresponding array has a positive size.
4414 * The array is available in the user pointer of the statement identifier.
4415 * "depth" is the schedule depth of the position where "graft"
4418 static __isl_give isl_schedule_node
*insert_positive_size_guards(
4419 __isl_take isl_schedule_node
*graft
,
4420 __isl_take isl_union_set_list
*filters
, int depth
)
4424 graft
= isl_schedule_node_child(graft
, 0);
4425 graft
= isl_schedule_node_insert_set(graft
, filters
);
4426 n
= isl_schedule_node_n_children(graft
);
4427 for (i
= 0; i
< n
; ++i
) {
4428 isl_union_set
*filter
;
4429 isl_set
*domain
, *guard
;
4431 struct gpu_array_info
*array
;
4433 graft
= isl_schedule_node_child(graft
, i
);
4434 filter
= isl_schedule_node_filter_get_filter(graft
);
4435 domain
= isl_set_from_union_set(filter
);
4436 id
= isl_set_get_tuple_id(domain
);
4437 array
= isl_id_get_user(id
);
4439 isl_set_free(domain
);
4440 guard
= gpu_array_positive_size_guard(array
);
4441 guard
= isl_set_from_params(guard
);
4442 guard
= isl_set_add_dims(guard
, isl_dim_set
, depth
);
4443 graft
= isl_schedule_node_child(graft
, 0);
4444 graft
= isl_schedule_node_insert_guard(graft
, guard
);
4445 graft
= isl_schedule_node_parent(graft
);
4446 graft
= isl_schedule_node_parent(graft
);
4448 graft
= isl_schedule_node_parent(graft
);
4453 /* Create a graft for copying arrays to or from the device,
4454 * whenever the size of the array is strictly positive.
4455 * Each statement is called "<prefix>_<name of array>" and
4456 * the identifier has a user pointer pointing to the array.
4457 * The graft will be added at the position specified by "node".
4458 * "copy" contains the array elements that need to be copied.
4459 * Only arrays of which some elements need to be copied
4460 * will have a corresponding statement in the graph.
4461 * Note though that each such statement will copy the entire array.
4463 static __isl_give isl_schedule_node
*create_copy_device(struct gpu_prog
*prog
,
4464 __isl_keep isl_schedule_node
*node
, const char *prefix
,
4465 __isl_take isl_union_set
*copy
)
4470 isl_union_set
*all
, *domain
;
4471 isl_union_set_list
*filters
;
4472 isl_union_map
*extension
;
4473 isl_schedule_node
*graft
;
4476 depth
= isl_schedule_node_get_schedule_depth(node
);
4477 filters
= create_copy_filters(prog
, prefix
, copy
);
4478 all
= isl_union_set_list_union(isl_union_set_list_copy(filters
));
4480 space
= depth
< 0 ? NULL
: isl_space_set_alloc(ctx
, 0, depth
);
4481 domain
= isl_union_set_from_set(isl_set_universe(space
));
4482 extension
= isl_union_map_from_domain_and_range(domain
, all
);
4483 graft
= isl_schedule_node_from_extension(extension
);
4486 return isl_schedule_node_free(graft
);
4487 if (isl_union_set_list_n_union_set(filters
) == 0) {
4488 isl_union_set_list_free(filters
);
4492 return insert_positive_size_guards(graft
, filters
, depth
);
4495 /* Return (the universe spaces of) the arrays that are declared
4496 * inside the scop corresponding to "prog" and for which all
4497 * potential writes inside the scop form a subset of "domain".
4499 static __isl_give isl_union_set
*extract_local_accesses(struct gpu_prog
*prog
,
4500 __isl_keep isl_union_set
*domain
)
4503 isl_union_set
*local
;
4505 local
= isl_union_set_empty(isl_union_set_get_space(domain
));
4507 for (i
= 0; i
< prog
->n_array
; ++i
) {
4509 isl_union_map
*to_outer
;
4510 isl_union_map
*may_write
;
4511 isl_union_set
*write_domain
;
4512 isl_union_set
*fields
;
4515 if (!prog
->array
[i
].local
)
4518 set
= isl_set_universe(isl_space_copy(prog
->array
[i
].space
));
4519 to_outer
= isl_union_map_copy(prog
->to_outer
);
4520 to_outer
= isl_union_map_intersect_range(to_outer
,
4521 isl_union_set_from_set(isl_set_copy(set
)));
4522 fields
= isl_union_map_domain(to_outer
);
4523 may_write
= isl_union_map_copy(prog
->may_write
);
4524 may_write
= isl_union_map_intersect_range(may_write
, fields
);
4525 write_domain
= isl_union_map_domain(may_write
);
4526 subset
= isl_union_set_is_subset(write_domain
, domain
);
4527 isl_union_set_free(write_domain
);
4531 return isl_union_set_free(local
);
4532 } else if (subset
) {
4533 local
= isl_union_set_add_set(local
, set
);
4542 /* Internal data structure for node_may_persist.
4544 * "tagger" maps tagged iteration domains to the corresponding untagged
4547 * "may_persist_flow" is the set of all tagged dataflow dependences
4548 * with those dependences removed that either precede or follow
4549 * the kernel launch in a sequence.
4550 * "inner_band_flow" is the set of all tagged dataflow dependences
4551 * that are local to a given iteration of the outer band nodes
4552 * with respect to the current node.
4553 * "local_flow" is equal to "inner_band_flow", except that the domain
4554 * and the range have been intersected with intermediate filters
4555 * on children of sets or sequences.
4557 struct ppcg_may_persist_data
{
4558 isl_union_pw_multi_aff
*tagger
;
4560 isl_union_map
*local_flow
;
4561 isl_union_map
*inner_band_flow
;
4562 isl_union_map
*may_persist_flow
;
4565 /* Update the information in "data" based on the band ancestor "node".
4567 * In particular, we restrict the dependences in data->local_flow
4568 * to those dependence where the source and the sink occur in
4569 * the same iteration of the given band node.
4570 * We also update data->inner_band_flow to the new value of
4573 static int update_may_persist_at_band(__isl_keep isl_schedule_node
*node
,
4574 struct ppcg_may_persist_data
*data
)
4576 isl_multi_union_pw_aff
*partial
;
4577 isl_union_pw_multi_aff
*contraction
;
4578 isl_union_map
*flow
;
4580 if (isl_schedule_node_band_n_member(node
) == 0)
4583 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4584 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4585 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4587 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4588 isl_union_pw_multi_aff_copy(data
->tagger
));
4590 flow
= data
->local_flow
;
4591 flow
= isl_union_map_eq_at_multi_union_pw_aff(flow
, partial
);
4592 data
->local_flow
= flow
;
4594 isl_union_map_free(data
->inner_band_flow
);
4595 data
->inner_band_flow
= isl_union_map_copy(data
->local_flow
);
4600 /* Given a set of local reaching domain elements "domain",
4601 * expand them to the corresponding leaf domain elements using "contraction"
4602 * and insert the array references tags using data->tagger.
4604 static __isl_give isl_union_set
*expand_and_tag(
4605 __isl_take isl_union_set
*domain
,
4606 __isl_take isl_union_pw_multi_aff
*contraction
,
4607 struct ppcg_may_persist_data
*data
)
4609 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4611 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4612 isl_union_pw_multi_aff_copy(data
->tagger
));
4616 /* Given a filter node that is the child of a set or sequence node,
4617 * restrict data->local_flow to refer only to those elements
4618 * in the filter of the node.
4619 * "contraction" maps the leaf domain elements of the schedule tree
4620 * to the corresponding domain elements at (the parent of) "node".
4622 static int filter_flow(__isl_keep isl_schedule_node
*node
,
4623 struct ppcg_may_persist_data
*data
,
4624 __isl_take isl_union_pw_multi_aff
*contraction
)
4626 isl_union_set
*filter
;
4627 isl_union_map
*flow
;
4629 flow
= data
->local_flow
;
4630 filter
= isl_schedule_node_filter_get_filter(node
);
4631 filter
= expand_and_tag(filter
, contraction
, data
);
4632 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(filter
));
4633 flow
= isl_union_map_intersect_range(flow
, filter
);
4634 data
->local_flow
= flow
;
4639 /* Given a filter node "node", collect the filters on all preceding siblings
4640 * (which are also filter nodes), add them to "filters" and return the result.
4642 static __isl_give isl_union_set
*add_previous_filters(
4643 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4645 isl_schedule_node
*sibling
;
4647 sibling
= isl_schedule_node_copy(node
);
4648 while (sibling
&& isl_schedule_node_has_previous_sibling(sibling
)) {
4649 isl_union_set
*filter
;
4651 sibling
= isl_schedule_node_previous_sibling(sibling
);
4652 filter
= isl_schedule_node_filter_get_filter(sibling
);
4653 filters
= isl_union_set_union(filters
, filter
);
4655 isl_schedule_node_free(sibling
);
4657 return isl_union_set_free(filters
);
4662 /* Given a filter node "node", collect the filters on all following siblings
4663 * (which are also filter nodes), add them to "filters" and return the result.
4665 static __isl_give isl_union_set
*add_next_filters(
4666 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4668 isl_schedule_node
*sibling
;
4670 sibling
= isl_schedule_node_copy(node
);
4671 while (sibling
&& isl_schedule_node_has_next_sibling(sibling
)) {
4672 isl_union_set
*filter
;
4674 sibling
= isl_schedule_node_next_sibling(sibling
);
4675 filter
= isl_schedule_node_filter_get_filter(sibling
);
4676 filters
= isl_union_set_union(filters
, filter
);
4678 isl_schedule_node_free(sibling
);
4680 return isl_union_set_free(filters
);
4685 /* Remove those flow dependences from data->may_persist_flow
4686 * that flow between elements of "domain" within the same iteration
4687 * of all outer band nodes.
4688 * "contraction" maps the leaf domain elements of the schedule tree
4689 * to the corresponding elements "domain".
4691 static void remove_external_flow(struct ppcg_may_persist_data
*data
,
4692 __isl_take isl_union_set
*domain
,
4693 __isl_keep isl_union_pw_multi_aff
*contraction
)
4695 isl_union_map
*flow
;
4697 contraction
= isl_union_pw_multi_aff_copy(contraction
);
4698 domain
= expand_and_tag(domain
, contraction
, data
);
4699 flow
= isl_union_map_copy(data
->local_flow
);
4700 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(domain
));
4701 flow
= isl_union_map_intersect_range(flow
, domain
);
4703 data
->may_persist_flow
= isl_union_map_subtract(data
->may_persist_flow
,
4707 /* Update the information in "data" based on the filter ancestor "node".
4708 * We only need to modify anything if the filter is the child
4709 * of a set or sequence node.
4711 * In the case of a sequence, we remove the dependences between
4712 * statement instances that are both executed either before or
4713 * after the subtree that will be mapped to a kernel, within
4714 * the same iteration of outer bands.
4716 * In both cases, we restrict data->local_flow to the current child.
4718 static int update_may_persist_at_filter(__isl_keep isl_schedule_node
*node
,
4719 struct ppcg_may_persist_data
*data
)
4721 enum isl_schedule_node_type type
;
4722 isl_schedule_node
*parent
;
4724 isl_union_pw_multi_aff
*contraction
;
4725 isl_union_set
*before
, *after
, *filter
;
4726 isl_union_map
*flow
;
4728 type
= isl_schedule_node_get_parent_type(node
);
4729 if (type
!= isl_schedule_node_sequence
&& type
!= isl_schedule_node_set
)
4732 parent
= isl_schedule_node_copy(node
);
4733 parent
= isl_schedule_node_parent(parent
);
4734 contraction
= isl_schedule_node_get_subtree_contraction(parent
);
4735 isl_schedule_node_free(parent
);
4737 if (type
== isl_schedule_node_set
)
4738 return filter_flow(node
, data
, contraction
);
4740 filter
= isl_schedule_node_filter_get_filter(node
);
4741 space
= isl_union_set_get_space(filter
);
4742 isl_union_set_free(filter
);
4743 before
= isl_union_set_empty(space
);
4744 after
= isl_union_set_copy(before
);
4745 before
= add_previous_filters(before
, node
);
4746 after
= add_next_filters(after
, node
);
4748 remove_external_flow(data
, before
, contraction
);
4749 remove_external_flow(data
, after
, contraction
);
4751 return filter_flow(node
, data
, contraction
);
4754 /* Update the information in "data" based on the ancestor "node".
4756 static isl_stat
update_may_persist_at(__isl_keep isl_schedule_node
*node
,
4759 struct ppcg_may_persist_data
*data
= user
;
4761 switch (isl_schedule_node_get_type(node
)) {
4762 case isl_schedule_node_error
:
4763 return isl_stat_error
;
4764 case isl_schedule_node_context
:
4765 case isl_schedule_node_domain
:
4766 case isl_schedule_node_expansion
:
4767 case isl_schedule_node_extension
:
4768 case isl_schedule_node_guard
:
4769 case isl_schedule_node_leaf
:
4770 case isl_schedule_node_mark
:
4771 case isl_schedule_node_sequence
:
4772 case isl_schedule_node_set
:
4774 case isl_schedule_node_band
:
4775 if (update_may_persist_at_band(node
, data
) < 0)
4776 return isl_stat_error
;
4778 case isl_schedule_node_filter
:
4779 if (update_may_persist_at_filter(node
, data
) < 0)
4780 return isl_stat_error
;
4787 /* Determine the set of array elements that may need to be perserved
4788 * by a kernel constructed from the subtree at "node".
4789 * This includes the set of array elements that may need to be preserved
4790 * by the entire scop (prog->may_persist) and the elements for which
4791 * there is a potential flow dependence that may cross a kernel launch.
4793 * To determine the second set, we start from all flow dependences.
4794 * From this set of dependences, we remove those that cannot possibly
4795 * require data to be preserved by a kernel launch.
4796 * In particular, we consider the following sets of dependences.
4797 * - dependences of which the write occurs inside the kernel.
4798 * If the data is needed outside the kernel, then it will
4799 * be copied out immediately after the kernel launch, so there
4800 * is no need for any special care.
4801 * - dependences of which the read occurs inside the kernel and the
4802 * corresponding write occurs inside the same iteration of the
4803 * outer band nodes. This means that the data is needed in
4804 * the first kernel launch after the write, which is already
4805 * taken care of by the standard copy-in. That is, the data
4806 * do not need to be preserved by any intermediate call to
4808 * - dependences of which the write and the read either both occur
4809 * before the kernel launch or both occur after the kernel launch,
4810 * within the same iteration of the outer band nodes with respect
4811 * to the sequence that determines the ordering of the dependence
4812 * and the kernel launch. Such flow dependences cannot cross
4813 * any kernel launch.
4815 * For the remaining (tagged) dependences, we take the domain
4816 * (i.e., the tagged writes) and apply the tagged access relation
4817 * to obtain the accessed data elements.
4818 * These are then combined with the elements that may need to be
4819 * preserved by the entire scop.
4821 static __isl_give isl_union_set
*node_may_persist(
4822 __isl_keep isl_schedule_node
*node
, struct gpu_prog
*prog
)
4824 struct ppcg_may_persist_data data
;
4825 isl_schedule_node
*root
;
4826 isl_union_pw_multi_aff
*contraction
;
4827 isl_union_set
*domain
;
4828 isl_union_set
*persist
;
4829 isl_union_map
*flow
, *local_flow
;
4831 data
.tagger
= prog
->scop
->tagger
;
4833 flow
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
4834 data
.local_flow
= isl_union_map_copy(flow
);
4835 data
.inner_band_flow
= isl_union_map_copy(flow
);
4836 data
.may_persist_flow
= flow
;
4837 if (isl_schedule_node_foreach_ancestor_top_down(node
,
4838 &update_may_persist_at
, &data
) < 0)
4839 data
.may_persist_flow
=
4840 isl_union_map_free(data
.may_persist_flow
);
4841 flow
= data
.may_persist_flow
;
4842 isl_union_map_free(data
.local_flow
);
4844 domain
= isl_schedule_node_get_domain(node
);
4845 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4846 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4848 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4849 isl_union_pw_multi_aff_copy(data
.tagger
));
4850 flow
= isl_union_map_subtract_domain(flow
, isl_union_set_copy(domain
));
4851 local_flow
= data
.inner_band_flow
;
4852 local_flow
= isl_union_map_intersect_range(local_flow
, domain
);
4853 flow
= isl_union_map_subtract(flow
, local_flow
);
4855 persist
= isl_union_map_domain(flow
);
4856 persist
= isl_union_set_apply(persist
,
4857 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
4858 persist
= isl_union_set_union(persist
,
4859 isl_union_set_copy(prog
->may_persist
));
4864 /* Add nodes for copying outer arrays in and out of the device
4865 * before and after the subtree "node", which contains one or more kernels.
4866 * "domain" contains the original reaching domain elements before
4867 * the kernels were created, i.e., before the contraction that
4868 * may have been performed in creating the kernels has been applied.
4869 * "prefix" contains the prefix schedule at that point, in terms
4870 * of the same original reaching domain elements.
4872 * We first compute the sets of outer array elements that need
4873 * to be copied in and out and then graft in the nodes for
4874 * performing this copying.
4876 * In particular, for each array that is possibly written anywhere in
4877 * the subtree "node" and that may be used after "node"
4878 * or that may be visible outside the corresponding scop,
4879 * we copy out its entire extent.
4881 * Any array elements that is read without first being written inside
4882 * the subtree "node" needs to be copied in.
4883 * Furthermore, if there are any array elements that
4884 * are copied out, but that may not be written inside "node, then
4885 * they also need to be copied in to ensure that the value after execution
4886 * is the same as the value before execution, at least for those array
4887 * elements that may have their values preserved by the scop or that
4888 * may be written before "node" and read after "node".
4889 * In case the array elements are structures, we need to take into
4890 * account that all members of the structures need to be written
4891 * by "node" before we can avoid copying the data structure in.
4893 * Note that the may_write relation is intersected with the domain,
4894 * which has been intersected with the context.
4895 * This helps in those cases where the arrays are declared with a fixed size,
4896 * while the accesses are parametric and the context assigns a fixed value
4897 * to the parameters.
4899 * If an element from a local array is read without first being written,
4900 * then there is no point in copying it in since it cannot have been
4901 * written prior to the scop. Warn about the uninitialized read instead.
4903 static __isl_give isl_schedule_node
*add_to_from_device(
4904 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
4905 __isl_take isl_union_map
*prefix
, struct gpu_prog
*prog
)
4907 isl_union_set
*local
;
4908 isl_union_set
*to_device
, *from_device
, *may_persist
;
4909 isl_union_map
*may_write
, *must_write
, *copy_out
, *not_written
;
4910 isl_union_map
*read
, *copy_in
;
4911 isl_union_map
*tagged
;
4912 isl_union_map
*local_uninitialized
;
4913 isl_schedule_node
*graft
;
4915 tagged
= isl_union_map_copy(prog
->scop
->tagged_reads
);
4916 tagged
= isl_union_map_union(tagged
,
4917 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
4919 may_write
= isl_union_map_copy(prog
->may_write
);
4920 may_write
= isl_union_map_intersect_domain(may_write
,
4921 isl_union_set_copy(domain
));
4922 may_write
= remove_local_accesses(prog
,
4923 isl_union_map_copy(tagged
), may_write
,
4924 isl_union_map_copy(prefix
), 0);
4925 may_write
= isl_union_map_apply_range(may_write
,
4926 isl_union_map_copy(prog
->to_outer
));
4927 may_write
= isl_union_map_apply_domain(may_write
,
4928 isl_union_map_copy(prefix
));
4929 may_write
= approximate_copy_out(may_write
, prog
);
4930 copy_out
= isl_union_map_copy(may_write
);
4931 may_write
= isl_union_map_apply_range(may_write
,
4932 isl_union_map_copy(prog
->to_inner
));
4933 must_write
= isl_union_map_copy(prog
->must_write
);
4934 must_write
= isl_union_map_apply_domain(must_write
,
4935 isl_union_map_copy(prefix
));
4936 may_persist
= node_may_persist(node
, prog
);
4937 may_write
= isl_union_map_intersect_range(may_write
, may_persist
);
4938 not_written
= isl_union_map_subtract(may_write
, must_write
);
4940 local
= extract_local_accesses(prog
, domain
);
4941 read
= isl_union_map_copy(prog
->read
);
4942 read
= isl_union_map_intersect_domain(read
, domain
);
4943 read
= remove_local_accesses(prog
, tagged
, read
,
4944 isl_union_map_copy(prefix
), 1);
4945 local
= isl_union_set_apply(local
, isl_union_map_copy(prog
->to_inner
));
4946 local_uninitialized
= isl_union_map_copy(prog
->scop
->live_in
);
4947 local_uninitialized
= isl_union_map_intersect_range(local_uninitialized
,
4949 local_uninitialized
= isl_union_map_intersect(local_uninitialized
,
4950 isl_union_map_copy(read
));
4951 if (!isl_union_map_is_empty(local_uninitialized
)) {
4953 "possibly uninitialized reads (not copied in):\n");
4954 isl_union_map_dump(local_uninitialized
);
4956 read
= isl_union_map_subtract(read
, local_uninitialized
);
4957 read
= isl_union_map_apply_domain(read
, prefix
);
4958 copy_in
= isl_union_map_union(read
, not_written
);
4959 copy_in
= isl_union_map_apply_range(copy_in
,
4960 isl_union_map_copy(prog
->to_outer
));
4962 graft
= create_copy_device(prog
, node
, "to_device",
4963 isl_union_map_range(copy_in
));
4964 node
= isl_schedule_node_graft_before(node
, graft
);
4965 graft
= create_copy_device(prog
, node
, "from_device",
4966 isl_union_map_range(copy_out
));
4967 node
= isl_schedule_node_graft_after(node
, graft
);
4972 /* Add nodes for initializing ("init_device") and clearing ("clear_device")
4973 * the device before and after "node".
4975 static __isl_give isl_schedule_node
*add_init_clear_device(
4976 __isl_take isl_schedule_node
*node
)
4980 isl_union_set
*domain
;
4981 isl_schedule_node
*graft
;
4983 ctx
= isl_schedule_node_get_ctx(node
);
4985 space
= isl_space_set_alloc(ctx
, 0, 0);
4986 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "init_device");
4987 domain
= isl_union_set_from_set(isl_set_universe(space
));
4988 graft
= isl_schedule_node_from_domain(domain
);
4990 node
= isl_schedule_node_graft_before(node
, graft
);
4992 space
= isl_space_set_alloc(ctx
, 0, 0);
4993 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "clear_device");
4994 domain
= isl_union_set_from_set(isl_set_universe(space
));
4995 graft
= isl_schedule_node_from_domain(domain
);
4997 node
= isl_schedule_node_graft_after(node
, graft
);
5002 /* Update "schedule" for mapping to a GPU device.
5004 * In particular, insert a context node, create kernels for
5005 * each outermost tilable band and introduce nodes for copying arrays
5006 * in and out of the device and for initializing and clearing the device.
5007 * If the child of the initial root points to a set node,
5008 * then children of this node that do not contain any tilable bands
5009 * are separated from the other children and are not mapped to
5012 static __isl_give isl_schedule
*map_to_device(struct gpu_gen
*gen
,
5013 __isl_take isl_schedule
*schedule
)
5015 isl_schedule_node
*node
;
5017 isl_union_set
*domain
;
5018 isl_union_map
*prefix
;
5020 context
= isl_set_copy(gen
->prog
->context
);
5021 context
= isl_set_from_params(context
);
5022 schedule
= isl_schedule_insert_context(schedule
, context
);
5024 node
= isl_schedule_get_root(schedule
);
5025 isl_schedule_free(schedule
);
5026 node
= isl_schedule_node_child(node
, 0);
5027 node
= isl_schedule_node_child(node
, 0);
5028 node
= isolate_permutable_subtrees(node
, gen
->prog
);
5029 domain
= isl_schedule_node_get_domain(node
);
5030 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
5031 node
= mark_kernels(gen
, node
);
5032 node
= add_to_from_device(node
, domain
, prefix
, gen
->prog
);
5033 node
= isl_schedule_node_root(node
);
5034 node
= isl_schedule_node_child(node
, 0);
5035 node
= isl_schedule_node_child(node
, 0);
5036 node
= add_init_clear_device(node
);
5037 schedule
= isl_schedule_node_get_schedule(node
);
5038 isl_schedule_node_free(node
);
5043 /* Internal data structure for extract_access.
5044 * "next_access" points to the end of a linked list that is extended
5045 * by extract_access.
5046 * "single_expression" is set if the access expressions belong to
5047 * an expression statement (i.e., a statement without internal control).
5048 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5050 struct ppcg_extract_access_data
{
5051 struct gpu_stmt_access
**next_access
;
5052 int single_expression
;
5053 isl_union_map
*any_to_outer
;
5056 /* Given a tagged access relation to a single array "tagged", extract it
5057 * as a map, taking into account that the input may be empty.
5058 * If the access relation is empty, then it does not contain
5059 * any space information, so we try to recover it from the index
5061 * The space of the index expression is of the form I -> A,
5062 * with I the statement instances and A the array, or [I -> F] -> A,
5063 * with F the filters corresponding to arguments.
5064 * We first drop F, if present, obtaining I -> A.
5065 * Then we construct I -> R, with R the reference tag,
5066 * combine the two into I -> [R -> A] and uncurry to obtain
5067 * the final result [I -> R] -> A.
5068 * Note that the index expression may have a lower dimension
5069 * than that of the array, but this dimension is not used
5070 * if the access relation is empty.
5072 static __isl_give isl_map
*extract_single_tagged_access(
5073 __isl_take isl_union_map
*tagged
, __isl_keep pet_expr
*expr
)
5077 isl_space
*space
, *space2
;
5078 isl_multi_pw_aff
*index
;
5080 empty
= isl_union_map_is_empty(tagged
);
5084 return isl_map_from_union_map(tagged
);
5085 isl_union_map_free(tagged
);
5087 index
= pet_expr_access_get_index(expr
);
5088 space
= isl_multi_pw_aff_get_space(index
);
5089 isl_multi_pw_aff_free(index
);
5090 if (isl_space_domain_is_wrapping(space
))
5091 space
= isl_space_domain_factor_domain(space
);
5092 space2
= isl_space_copy(space
);
5093 space2
= isl_space_from_domain(isl_space_domain(space
));
5094 id
= pet_expr_access_get_ref_id(expr
);
5095 space2
= isl_space_set_tuple_id(space2
, isl_dim_out
, id
);
5096 space
= isl_space_range_product(space2
, space
);
5097 space
= isl_space_uncurry(space
);
5099 return isl_map_empty(space
);
5101 isl_union_map_free(tagged
);
5105 /* Extract a gpu_stmt_access from "expr", append it to the list
5106 * that ends in *data->next_access and update the end of the list.
5107 * If the access expression performs a write, then it is considered
5108 * exact only if it appears in a single expression statement and
5109 * if its may access relation is equal to its must access relation.
5111 * The combined set of may accesses may be union if member accesses
5112 * are involved, but the entire set is derived from a single reference and
5113 * therefore from a single index expression. These accesses therefore
5114 * all map to the same outer array.
5116 static int extract_access(__isl_keep pet_expr
*expr
, void *user
)
5118 struct ppcg_extract_access_data
*data
= user
;
5119 isl_union_map
*tagged
;
5120 struct gpu_stmt_access
*access
;
5121 isl_ctx
*ctx
= pet_expr_get_ctx(expr
);
5122 isl_multi_pw_aff
*index
;
5124 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
5126 access
->next
= NULL
;
5127 access
->read
= pet_expr_access_is_read(expr
);
5128 access
->write
= pet_expr_access_is_write(expr
);
5129 tagged
= pet_expr_access_get_tagged_may_read(expr
);
5130 tagged
= isl_union_map_union(tagged
,
5131 pet_expr_access_get_tagged_may_write(expr
));
5132 tagged
= isl_union_map_apply_range(tagged
,
5133 isl_union_map_copy(data
->any_to_outer
));
5134 if (!access
->write
) {
5135 access
->exact_write
= 1;
5136 } else if (!data
->single_expression
) {
5137 access
->exact_write
= 0;
5139 isl_union_map
*must
, *may
;
5140 may
= isl_union_map_copy(tagged
);
5141 may
= isl_union_map_domain_factor_domain(may
);
5142 must
= pet_expr_access_get_must_write(expr
);
5143 access
->exact_write
= isl_union_map_is_equal(must
, may
);
5144 isl_union_map_free(must
);
5145 isl_union_map_free(may
);
5147 index
= pet_expr_access_get_index(expr
);
5148 access
->n_index
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5149 isl_multi_pw_aff_free(index
);
5150 access
->ref_id
= pet_expr_access_get_ref_id(expr
);
5151 access
->tagged_access
= extract_single_tagged_access(tagged
, expr
);
5152 access
->access
= isl_map_copy(access
->tagged_access
);
5153 access
->access
= isl_map_domain_factor_domain(access
->access
);
5155 *data
->next_access
= access
;
5156 data
->next_access
= &(*data
->next_access
)->next
;
5158 if (!access
->access
)
5164 /* Construct a linked list of gpu_stmt_access objects,
5165 * one for each access expression in the statement body.
5166 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5168 static int pet_stmt_extract_accesses(struct gpu_stmt
*stmt
,
5169 __isl_keep isl_union_map
*any_to_outer
)
5171 struct ppcg_extract_access_data data
;
5173 stmt
->accesses
= NULL
;
5174 data
.next_access
= &stmt
->accesses
;
5175 data
.single_expression
=
5176 pet_tree_get_type(stmt
->stmt
->body
) == pet_tree_expr
;
5177 data
.any_to_outer
= any_to_outer
;
5178 return pet_tree_foreach_access_expr(stmt
->stmt
->body
,
5179 &extract_access
, &data
);
5182 /* Return an array of gpu_stmt representing the statements in "scop".
5184 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
5185 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*any_to_outer
)
5188 struct gpu_stmt
*stmts
;
5190 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->pet
->n_stmt
);
5194 for (i
= 0; i
< scop
->pet
->n_stmt
; ++i
) {
5195 struct gpu_stmt
*s
= &stmts
[i
];
5197 s
->id
= isl_set_get_tuple_id(scop
->pet
->stmts
[i
]->domain
);
5198 s
->stmt
= scop
->pet
->stmts
[i
];
5199 if (pet_stmt_extract_accesses(s
, any_to_outer
) < 0)
5200 return free_stmts(stmts
, i
+ 1);
5206 /* Callback for ppcg_print_guarded that calls the callback for generate_gpu.
5208 static __isl_give isl_printer
*print_gpu(__isl_take isl_printer
*p
, void *user
)
5210 struct gpu_gen
*gen
= user
;
5212 return gen
->print(p
, gen
->prog
, gen
->tree
, &gen
->types
,
5216 /* Generate CUDA code for "scop" and print it to "p".
5217 * After generating an AST for the transformed scop as explained below,
5218 * we call "gen->print" to print the AST in the desired output format
5221 * If it turns out that it does not make sense to generate GPU code,
5222 * then we generate CPU code instead.
5224 * The GPU code is generated in a context where at least one
5225 * statement instance is executed. The corresponding guard (if any) is printed
5226 * around the entire generated GPU code, except for the declaration
5227 * of the arrays that are visible outside of the scop and that therefore
5228 * cannot be declared inside the body of any possible guard.
5230 * We first compute a schedule that respects the dependences
5231 * of the original program and select the outermost bands
5232 * of tilable dimensions that have at least one parallel loop.
5233 * If the --load-schedule is specified, then the loaded schedule
5234 * is used instead of a computed schedule.
5236 * Each of these bands B is then tiled according to "tile" sizes, resulting
5237 * in two nested bands, with a kernel marker on top
5245 * We then split off at most 2 parallel dimensions from the T band and
5246 * at most 3 parallel dimension from the P band
5259 * A filter is introduced in front of T1 that maps the domain instances
5260 * to block identifiers. Similarly, a filter is introduced in front of P1
5261 * that maps the domain instances to thread identifiers.
5263 * For each iteration of the T2 band and for each array, we compute
5264 * the array elements accessed by that iteration, construct a rectangular
5265 * box around it and shift it to the origin. The result is used
5266 * as shared memory for the array.
5268 * Copying and synchronization statements are added to this schedule tree.
5269 * In principle, these are added in front of the P1 band, but some of
5270 * them may get hoisted up to higher levels.
5272 * The entire AST is then generated from the single resulting schedule tree.
5273 * During the generation the subtrees at kernel nodes (K) are saved
5274 * aside and replaced by kernel calls. The result is printed as host code
5275 * while the saved subtrees are printed as device code.
5277 static __isl_give isl_printer
*generate(__isl_take isl_printer
*p
,
5278 struct gpu_gen
*gen
, struct ppcg_scop
*scop
,
5279 struct ppcg_options
*options
)
5281 struct gpu_prog
*prog
;
5283 isl_set
*context
, *guard
;
5284 isl_schedule
*schedule
;
5288 return isl_printer_free(p
);
5290 ctx
= isl_printer_get_ctx(p
);
5291 prog
= gpu_prog_alloc(ctx
, scop
);
5293 return isl_printer_free(p
);
5295 context
= isl_set_copy(prog
->context
);
5296 guard
= isl_union_set_params(isl_union_set_copy(prog
->scop
->domain
));
5297 prog
->context
= isl_set_intersect(prog
->context
, isl_set_copy(guard
));
5300 schedule
= get_schedule(gen
);
5302 any_permutable
= has_any_permutable_node(schedule
);
5303 if (any_permutable
< 0 || !any_permutable
) {
5304 isl_set_free(context
);
5305 isl_set_free(guard
);
5306 if (any_permutable
< 0)
5307 p
= isl_printer_free(p
);
5309 p
= print_cpu(p
, scop
, options
);
5310 isl_schedule_free(schedule
);
5312 schedule
= map_to_device(gen
, schedule
);
5313 gen
->tree
= generate_code(gen
, schedule
);
5314 p
= ppcg_set_macro_names(p
);
5315 p
= isl_ast_op_type_print_macro(isl_ast_op_fdiv_q
, p
);
5316 p
= ppcg_print_exposed_declarations(p
, prog
->scop
);
5317 p
= ppcg_print_guarded(p
, guard
, context
, &print_gpu
, gen
);
5318 isl_ast_node_free(gen
->tree
);
5321 gpu_prog_free(prog
);
5326 /* Wrapper around generate for use as a ppcg_transform callback.
5328 static __isl_give isl_printer
*generate_wrap(__isl_take isl_printer
*p
,
5329 struct ppcg_scop
*scop
, void *user
)
5331 struct gpu_gen
*gen
= user
;
5333 return generate(p
, gen
, scop
, gen
->options
);
5336 /* Transform the code in the file called "input" by replacing
5337 * all scops by corresponding GPU code and write the results to "out".
5339 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
5340 struct ppcg_options
*options
,
5341 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
5342 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
5343 struct gpu_types
*types
, void *user
), void *user
)
5350 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
5351 gen
.options
= options
;
5354 gen
.print_user
= user
;
5356 gen
.types
.name
= NULL
;
5358 if (options
->debug
->dump_sizes
) {
5359 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
5360 gen
.used_sizes
= isl_union_map_empty(space
);
5363 r
= ppcg_transform(ctx
, input
, out
, options
, &generate_wrap
, &gen
);
5365 if (options
->debug
->dump_sizes
) {
5366 isl_union_map_dump(gen
.used_sizes
);
5367 isl_union_map_free(gen
.used_sizes
);
5370 isl_union_map_free(gen
.sizes
);
5371 for (i
= 0; i
< gen
.types
.n
; ++i
)
5372 free(gen
.types
.name
[i
]);
5373 free(gen
.types
.name
);
5378 /* Compute the set of inner array elements that may have their values
5379 * preserved by "prog". In particular, collect the array elements of
5380 * arrays that are not local to "prog" and remove those elements that
5381 * are definitely killed or definitely written by "prog".
5383 static __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
)
5386 isl_union_set
*may_persist
, *killed
;
5387 isl_union_map
*must_kill
;
5389 may_persist
= isl_union_set_empty(isl_set_get_space(prog
->context
));
5390 for (i
= 0; i
< prog
->n_array
; ++i
) {
5393 if (prog
->array
[i
].local
)
5396 extent
= isl_set_copy(prog
->array
[i
].extent
);
5397 may_persist
= isl_union_set_add_set(may_persist
, extent
);
5400 may_persist
= isl_union_set_intersect_params(may_persist
,
5401 isl_set_copy(prog
->context
));
5402 may_persist
= isl_union_set_apply(may_persist
,
5403 isl_union_map_copy(prog
->to_inner
));
5404 must_kill
= isl_union_map_copy(prog
->tagged_must_kill
);
5405 killed
= isl_union_map_range(must_kill
);
5406 must_kill
= isl_union_map_copy(prog
->must_write
);
5407 killed
= isl_union_set_union(killed
, isl_union_map_range(must_kill
));
5409 may_persist
= isl_union_set_subtract(may_persist
, killed
);
5413 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
5415 struct gpu_prog
*prog
;
5422 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
5427 prog
->context
= isl_set_copy(scop
->context
);
5428 prog
->n_stmts
= scop
->pet
->n_stmt
;
5429 prog
->any_to_outer
= pet_scop_compute_outer_to_any(scop
->pet
);
5430 prog
->any_to_outer
= isl_union_map_reverse(prog
->any_to_outer
);
5431 space
= isl_union_map_get_space(prog
->any_to_outer
);
5432 space
= isl_space_set_from_params(space
);
5433 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
5434 space
= isl_space_map_from_set(space
);
5435 id
= isl_map_identity(space
);
5436 prog
->any_to_outer
= isl_union_map_add_map(prog
->any_to_outer
, id
);
5437 prog
->stmts
= extract_stmts(ctx
, scop
,
5438 prog
->context
, prog
->any_to_outer
);
5439 prog
->read
= isl_union_map_copy(scop
->reads
);
5440 prog
->may_write
= isl_union_map_copy(scop
->may_writes
);
5441 prog
->must_write
= isl_union_map_copy(scop
->must_writes
);
5442 prog
->tagged_must_kill
= isl_union_map_copy(scop
->tagged_must_kills
);
5443 prog
->to_inner
= pet_scop_compute_outer_to_inner(scop
->pet
);
5444 prog
->to_outer
= isl_union_map_copy(prog
->to_inner
);
5445 prog
->to_outer
= isl_union_map_reverse(prog
->to_outer
);
5448 return gpu_prog_free(prog
);
5450 if (collect_array_info(prog
) < 0)
5451 return gpu_prog_free(prog
);
5452 prog
->may_persist
= compute_may_persist(prog
);
5457 void *gpu_prog_free(struct gpu_prog
*prog
)
5461 free_array_info(prog
);
5462 free_stmts(prog
->stmts
, prog
->n_stmts
);
5463 isl_union_map_free(prog
->any_to_outer
);
5464 isl_union_map_free(prog
->to_outer
);
5465 isl_union_map_free(prog
->to_inner
);
5466 isl_union_map_free(prog
->read
);
5467 isl_union_map_free(prog
->may_write
);
5468 isl_union_map_free(prog
->must_write
);
5469 isl_union_map_free(prog
->tagged_must_kill
);
5470 isl_union_map_free(prog
->array_order
);
5471 isl_union_set_free(prog
->may_persist
);
5472 isl_set_free(prog
->context
);