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
)
175 isl_multi_pw_aff
*bounds
;
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
);
181 info
->space
= isl_set_get_space(pa
->extent
);
182 info
->name
= strdup(name
);
183 info
->n_index
= n_index
;
184 info
->linearize
= prog
->scop
->options
->linearize_device_arrays
;
186 info
->type
= strdup(pa
->element_type
);
187 info
->size
= pa
->element_size
;
188 info
->local
= pa
->declared
&& !pa
->exposed
;
189 info
->has_compound_element
= pa
->element_is_record
;
190 info
->read_only_scalar
= is_read_only_scalar(info
, prog
);
192 accessed
= isl_union_set_extract_set(arrays
,
193 isl_space_copy(info
->space
));
194 empty
= isl_set_is_empty(accessed
);
195 extent
= compute_extent(pa
, accessed
);
196 isl_set_free(accessed
);
197 info
->extent
= extent
;
200 info
->accessed
= !empty
;
201 bounds
= ppcg_size_from_extent(isl_set_copy(extent
));
202 bounds
= isl_multi_pw_aff_gist(bounds
, isl_set_copy(prog
->context
));
205 if (!isl_multi_pw_aff_is_cst(bounds
))
207 info
->bound
= bounds
;
209 collect_references(prog
, info
);
214 /* Remove independence from the order constraints "order" on array "array".
215 * Since the pairs of iterations in the filter relation of an independence
216 * are guaranteed to be completely independent by the user, there is
217 * no need to ensure that live ranges are ordered along thong pairs.
218 * We make an exception for local variables, though, as the independence
219 * guarantee does not apply to those.
221 * The order constraints are used in two places.
222 * Those on scalars are used in check_scalar_live_ranges to check if
223 * we need to force the scalar to be private. Any non-local scalar
224 * should not be forced scalar if it only appears in independent loops.
225 * Those on non-scalars are added to the coincidence constraints
226 * in compute_schedule because we do not support any array expansion.
227 * Accesses to non-local arrays should not prevent a loop from being
228 * considered coincident so we should indeed remove those constraints
229 * from the order constraints.
231 static __isl_give isl_union_map
*remove_independences(struct gpu_prog
*prog
,
232 struct gpu_array_info
*array
, __isl_take isl_union_map
*order
)
236 for (i
= 0; i
< prog
->scop
->pet
->n_independence
; ++i
) {
237 struct pet_independence
*pi
= prog
->scop
->pet
->independences
[i
];
238 if (isl_union_set_contains(pi
->local
, array
->space
))
241 order
= isl_union_map_subtract(order
,
242 isl_union_map_copy(pi
->filter
));
248 /* For each array in "prog", store the (untagged) order dependences
249 * derived from the array in array->dep_order.
250 * In particular, consider all references that access the given array
251 * and take the order dependences that have one of these references
252 * as source. (Since an order dependence relates two references to
253 * the same array, the target of these order dependences will also
254 * be one of these references.)
255 * Additionally, store the union of these array->dep_order relations
256 * for all non-scalar arrays in prog->array_order.
258 void collect_order_dependences(struct gpu_prog
*prog
)
262 isl_union_map
*accesses
;
264 space
= isl_union_map_get_space(prog
->read
);
265 prog
->array_order
= isl_union_map_empty(space
);
267 accesses
= isl_union_map_copy(prog
->scop
->tagged_reads
);
268 accesses
= isl_union_map_union(accesses
,
269 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
270 accesses
= isl_union_map_universe(accesses
);
271 accesses
= isl_union_map_apply_range(accesses
,
272 isl_union_map_copy(prog
->to_outer
));
274 for (i
= 0; i
< prog
->n_array
; ++i
) {
275 struct gpu_array_info
*array
= &prog
->array
[i
];
278 isl_union_map
*order
;
280 set
= isl_set_universe(isl_space_copy(array
->space
));
281 uset
= isl_union_set_from_set(set
);
282 uset
= isl_union_map_domain(
283 isl_union_map_intersect_range(isl_union_map_copy(accesses
),
285 order
= isl_union_map_copy(prog
->scop
->tagged_dep_order
);
286 order
= isl_union_map_intersect_domain(order
, uset
);
287 order
= isl_union_map_zip(order
);
288 order
= isl_union_set_unwrap(isl_union_map_domain(order
));
289 order
= remove_independences(prog
, array
, order
);
290 array
->dep_order
= order
;
292 if (gpu_array_is_scalar(array
) && !array
->has_compound_element
)
295 prog
->array_order
= isl_union_map_union(prog
->array_order
,
296 isl_union_map_copy(array
->dep_order
));
299 isl_union_map_free(accesses
);
302 /* Construct a gpu_array_info for each array referenced by prog->scop and
303 * collect them in prog->array.
305 * The sizes are based on the extents and the set of possibly accessed
306 * elements by "prog".
307 * If there are any member accesses involved, then they are first mapped
308 * to the outer arrays of structs.
310 * If we are allowing live range reordering, then also set
311 * the dep_order field. Otherwise leave it NULL.
313 static int collect_array_info(struct gpu_prog
*prog
)
317 isl_union_set
*arrays
;
319 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
320 arrays
= isl_union_set_union(arrays
,
321 isl_union_map_range(isl_union_map_copy(prog
->may_write
)));
323 arrays
= isl_union_set_apply(arrays
,
324 isl_union_map_copy(prog
->to_outer
));
326 arrays
= isl_union_set_coalesce(arrays
);
328 prog
->n_array
= prog
->scop
->pet
->n_array
;
329 prog
->array
= isl_calloc_array(prog
->ctx
,
330 struct gpu_array_info
, prog
->n_array
);
332 for (i
= 0; i
< prog
->scop
->pet
->n_array
; ++i
)
333 if (extract_array_info(prog
, &prog
->array
[i
],
334 prog
->scop
->pet
->arrays
[i
], arrays
) < 0)
337 isl_union_set_free(arrays
);
339 if (prog
->scop
->options
->live_range_reordering
)
340 collect_order_dependences(prog
);
345 static void free_array_info(struct gpu_prog
*prog
)
349 for (i
= 0; i
< prog
->n_array
; ++i
) {
350 free(prog
->array
[i
].type
);
351 free(prog
->array
[i
].name
);
352 isl_multi_pw_aff_free(prog
->array
[i
].bound
);
353 isl_ast_expr_free(prog
->array
[i
].bound_expr
);
354 isl_space_free(prog
->array
[i
].space
);
355 isl_set_free(prog
->array
[i
].extent
);
356 isl_ast_expr_free(prog
->array
[i
].declared_size
);
357 free(prog
->array
[i
].refs
);
358 isl_union_map_free(prog
->array
[i
].dep_order
);
363 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
364 * as an array or through a pointer reference, but as a single data element.
365 * At the moment, scalars are represented as zero-dimensional arrays.
366 * Note that the single data element may be an entire structure.
368 int gpu_array_is_scalar(struct gpu_array_info
*array
)
370 return array
->n_index
== 0;
373 /* Is "array" a read-only scalar?
375 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
377 return array
->read_only_scalar
;
380 /* Does "array" need to be allocated on the device?
381 * If it is a read-only scalar, then it will be passed as an argument
382 * to the kernel and therefore does not require any allocation.
383 * If this device memory is not accessed at all, then it does not
384 * need to be allocated either.
386 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
)
388 if (gpu_array_is_read_only_scalar(array
))
395 /* Return the set of parameter values for which the array has a positive
396 * size in all dimensions.
397 * If the sizes are only valid for some parameter values, then those
398 * constraints are also taken into account.
400 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
)
409 space
= isl_space_params(isl_space_copy(array
->space
));
410 guard
= isl_set_universe(space
);
412 for (i
= 0; i
< array
->n_index
; ++i
) {
414 isl_set
*guard_i
, *zero
;
416 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
417 guard_i
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound
));
418 zero
= isl_pw_aff_zero_set(bound
);
419 guard_i
= isl_set_subtract(guard_i
, zero
);
420 guard
= isl_set_intersect(guard
, guard_i
);
426 /* Internal data structure for extract_size_of_type.
427 * "type" specifies the name of the space that we want to extract.
428 * "res" is used to store the subset of that space.
430 struct ppcg_extract_size_data
{
435 /* This function is called for each set in a union_set.
436 * If the name of the set matches data->type, we store the
439 static isl_stat
extract_size_of_type(__isl_take isl_set
*size
, void *user
)
441 struct ppcg_extract_size_data
*data
= user
;
444 name
= isl_set_get_tuple_name(size
);
445 if (name
&& !strcmp(name
, data
->type
)) {
447 return isl_stat_error
;
454 /* Given a union map { kernel[i] -> *[...] },
455 * return the range in the space called "type" for the kernel with
456 * sequence number "id".
458 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
459 const char *type
, int id
)
463 isl_union_set
*local_sizes
;
464 struct ppcg_extract_size_data data
= { type
, NULL
};
469 space
= isl_union_map_get_space(sizes
);
470 space
= isl_space_set_from_params(space
);
471 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
472 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
473 dom
= isl_set_universe(space
);
474 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
476 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
477 isl_union_map_copy(sizes
));
478 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
479 isl_union_set_free(local_sizes
);
483 /* Given a singleton set, extract the first (at most *len) elements
484 * of the single integer tuple into *sizes and update *len if needed.
486 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
494 dim
= isl_set_dim(set
, isl_dim_set
);
498 for (i
= 0; i
< *len
; ++i
) {
501 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
504 sizes
[i
] = isl_val_get_num_si(v
);
511 /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
512 * if the option debug->dump_sizes is set.
514 static void set_used_sizes(struct gpu_gen
*gen
, const char *type
, int id
,
521 if (!gen
->options
->debug
->dump_sizes
)
524 space
= isl_union_map_get_space(gen
->used_sizes
);
525 space
= isl_space_set_from_params(space
);
526 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
527 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
528 space
= isl_space_from_domain(space
);
529 space
= isl_space_add_dims(space
, isl_dim_out
, len
);
530 space
= isl_space_set_tuple_name(space
, isl_dim_out
, type
);
532 map
= isl_map_universe(space
);
533 map
= isl_map_fix_si(map
, isl_dim_in
, 0, id
);
534 for (i
= 0; i
< len
; ++i
)
535 map
= isl_map_fix_si(map
, isl_dim_out
, i
, sizes
[i
]);
537 gen
->used_sizes
= isl_union_map_add_map(gen
->used_sizes
, map
);
540 /* Extract user specified "tile" sizes from the "sizes" command line option,
541 * defaulting to option->tile_size in each dimension.
542 * *tile_len contains the maximum number of tile sizes needed.
543 * Update *tile_len to the number of specified tile sizes, if any, and
544 * return a pointer to the tile sizes (or NULL on error).
545 * Add the effectively used sizes to gen->used_sizes.
547 static int *read_tile_sizes(struct gpu_gen
*gen
, int *tile_len
)
553 tile_size
= isl_alloc_array(gen
->ctx
, int, *tile_len
);
556 for (n
= 0; n
< *tile_len
; ++n
)
557 tile_size
[n
] = gen
->options
->tile_size
;
559 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
560 read_sizes_from_set(size
, tile_size
, tile_len
);
561 set_used_sizes(gen
, "tile", gen
->kernel_id
, tile_size
, *tile_len
);
566 /* Extract user specified "block" sizes from the "sizes" command line option,
567 * after filling in some potentially useful defaults.
569 static void read_block_sizes(struct ppcg_kernel
*kernel
,
570 __isl_keep isl_union_map
*sizes
)
574 if (kernel
->n_block
> 3)
576 switch (kernel
->n_block
) {
578 kernel
->block_dim
[0] = 512;
581 kernel
->block_dim
[0] = 32;
582 kernel
->block_dim
[1] = 16;
585 kernel
->block_dim
[0] = 32;
586 kernel
->block_dim
[1] = 4;
587 kernel
->block_dim
[2] = 4;
591 size
= extract_sizes(sizes
, "block", kernel
->id
);
592 read_sizes_from_set(size
, kernel
->block_dim
, &kernel
->n_block
);
595 /* Extract user specified "grid" sizes from the "sizes" command line option,
596 * after filling in some potentially useful defaults.
598 static void read_grid_sizes(struct ppcg_kernel
*kernel
,
599 __isl_keep isl_union_map
*sizes
)
603 if (kernel
->n_grid
> 2)
605 switch (kernel
->n_grid
) {
607 kernel
->grid_dim
[0] = 32768;
610 kernel
->grid_dim
[0] = 256;
611 kernel
->grid_dim
[1] = 256;
615 size
= extract_sizes(sizes
, "grid", kernel
->id
);
616 read_sizes_from_set(size
, kernel
->grid_dim
, &kernel
->n_grid
);
619 /* Extract user specified grid and block sizes from the gen->sizes
620 * command line option after filling in some potentially useful defaults.
621 * Store the extracted sizes in "kernel".
622 * Add the effectively used sizes to gen->used_sizes.
624 static void read_grid_and_block_sizes(struct ppcg_kernel
*kernel
,
627 read_block_sizes(kernel
, gen
->sizes
);
628 read_grid_sizes(kernel
, gen
->sizes
);
629 set_used_sizes(gen
, "block", kernel
->id
,
630 kernel
->block_dim
, kernel
->n_block
);
631 set_used_sizes(gen
, "grid", kernel
->id
,
632 kernel
->grid_dim
, kernel
->n_grid
);
635 static void *free_stmts(struct gpu_stmt
*stmts
, int n
)
642 for (i
= 0; i
< n
; ++i
) {
643 struct gpu_stmt_access
*access
, *next
;
645 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
647 isl_id_free(access
->ref_id
);
648 isl_map_free(access
->access
);
649 isl_map_free(access
->tagged_access
);
653 isl_id_free(stmts
[i
].id
);
660 /* Add parameters p[i] with identifiers "ids" to "set",
661 * with bounds to 0 <= p[i] < size[i].
663 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
664 int *size
, __isl_keep isl_id_list
*ids
)
669 len
= isl_id_list_n_id(ids
);
670 nparam
= isl_set_dim(set
, isl_dim_param
);
671 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
673 for (i
= 0; i
< len
; ++i
) {
676 id
= isl_id_list_get_id(ids
, i
);
677 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
678 set
= isl_set_lower_bound_si(set
, isl_dim_param
, nparam
+ i
, 0);
679 set
= isl_set_upper_bound_si(set
, isl_dim_param
,
680 nparam
+ i
, size
[i
] - 1);
686 /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
689 * { : 0 <= p[i] < size[i] }
691 * or an overapproximation.
693 static __isl_give isl_set
*add_bounded_parameters_dynamic(
694 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
695 __isl_keep isl_id_list
*ids
)
702 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
703 nparam
= isl_set_dim(set
, isl_dim_param
);
704 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
706 for (i
= 0; i
< len
; ++i
) {
709 id
= isl_id_list_get_id(ids
, i
);
710 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
713 space
= isl_space_params(isl_set_get_space(set
));
714 ls
= isl_local_space_from_space(space
);
715 for (i
= 0; i
< len
; ++i
) {
716 isl_pw_aff
*param
, *size_i
, *zero
;
719 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
720 isl_dim_param
, nparam
+ i
);
722 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
723 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
724 bound
= isl_set_from_basic_set(isl_set_simple_hull(bound
));
725 set
= isl_set_intersect_params(set
, bound
);
727 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
728 bound
= isl_pw_aff_ge_set(param
, zero
);
729 set
= isl_set_intersect_params(set
, bound
);
731 isl_local_space_free(ls
);
736 /* Return the union of all tagged access relations in the group.
738 static __isl_give isl_union_map
*group_tagged_access_relation(
739 struct gpu_array_ref_group
*group
)
742 isl_union_map
*access
;
744 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
745 for (i
= 0; i
< group
->n_ref
; ++i
) {
748 map_i
= isl_map_copy(group
->refs
[i
]->tagged_access
);
749 access
= isl_union_map_union(access
,
750 isl_union_map_from_map(map_i
));
756 /* Return the extent of "array", recomputed from the bounds.
757 * The recomputed extent may be simpler than the original extent.
759 static __isl_give isl_set
*array_extent(struct gpu_array_info
*array
)
767 id
= isl_set_get_tuple_id(array
->extent
);
768 space
= isl_set_get_space(array
->extent
);
769 extent
= isl_set_universe(isl_space_copy(space
));
770 ls
= isl_local_space_from_space(space
);
771 for (i
= 0; i
< array
->n_index
; ++i
) {
777 extent
= isl_set_lower_bound_si(extent
, isl_dim_set
, i
, 0);
779 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
781 index
= isl_pw_aff_from_aff(aff
);
782 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
783 bound
= isl_pw_aff_from_range(bound
);
784 bound
= isl_pw_aff_add_dims(bound
, isl_dim_in
, array
->n_index
);
785 bound
= isl_pw_aff_set_tuple_id(bound
, isl_dim_in
,
787 lt
= isl_pw_aff_lt_set(index
, bound
);
788 extent
= isl_set_intersect(extent
, lt
);
790 isl_local_space_free(ls
);
796 /* Return a map from the first group->shared_tile->depth dimensions
797 * of the computed schedule to the array tile in
798 * global memory that corresponds to the shared memory copy.
800 * In particular, return a map
806 * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
810 * 0 <= a <= array_size - 1 (2)
812 * Note that if some stride has been detected (i.e., when
813 * group->shared_tile->bound[i].shift is set), then a in (1) refers
814 * to the shifted and scaled down version.
816 * Constraints (1) are obtained by mapping the size constraints on the
817 * shared/private memory tile back to the access relation.
818 * Constraints (2) are obtained from the (recomputed) extent.
820 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
823 int n_index
= group
->array
->n_index
;
829 space
= isl_multi_aff_get_space(group
->shared_tile
->tiling
);
830 space
= isl_space_range(space
);
831 local
= isl_set_universe(space
);
832 for (i
= 0; i
< n_index
; ++i
) {
835 local
= isl_set_lower_bound_si(local
, isl_dim_set
, i
, 0);
836 bound
= isl_val_copy(group
->shared_tile
->bound
[i
].size
);
837 bound
= isl_val_sub_ui(bound
, 1);
838 local
= isl_set_upper_bound_val(local
, isl_dim_set
, i
, bound
);
840 local
= isl_set_preimage_multi_aff(local
,
841 isl_multi_aff_copy(group
->shared_tile
->tiling
));
842 tile
= isl_set_unwrap(local
);
843 extent
= array_extent(group
->array
);
844 tile
= isl_map_intersect_range(tile
, extent
);
849 /* Given a mapping "iterator_map" from the AST schedule to a domain,
850 * return the corresponding mapping from the AST schedule to
851 * to the outer kernel->copy_schedule_dim dimensions of
852 * the schedule computed by PPCG for this kernel.
854 * Note that kernel->copy_schedule_dim is at least as large as
855 * the largest depth of any array reference group associated to the kernel.
856 * This is needed as the returned schedule is used to extract a mapping
857 * to the outer tile->depth dimensions in transform_index.
859 static __isl_give isl_pw_multi_aff
*compute_sched_to_copy(
860 struct ppcg_kernel
*kernel
, __isl_take isl_pw_multi_aff
*iterator_map
)
862 isl_union_pw_multi_aff
*upma
;
863 isl_pw_multi_aff
*pma
;
866 space
= isl_space_range(isl_pw_multi_aff_get_space(iterator_map
));
867 space
= isl_space_from_domain(space
);
868 space
= isl_space_add_dims(space
, isl_dim_out
,
869 kernel
->copy_schedule_dim
);
871 upma
= isl_union_pw_multi_aff_copy(kernel
->copy_schedule
);
872 pma
= isl_union_pw_multi_aff_extract_pw_multi_aff(upma
, space
);
873 isl_union_pw_multi_aff_free(upma
);
875 return isl_pw_multi_aff_pullback_pw_multi_aff(pma
, iterator_map
);
878 /* If max_shared_memory is not set to infinity (-1), then make
879 * sure that the total amount of shared memory required by the
880 * array reference groups mapped to shared memory by "kernel"
881 * is no larger than this maximum.
883 * We apply a greedy approach and discard (keep in global memory)
884 * those groups that would result in a total memory size that
885 * is larger than the maximum.
887 * This function should be called after any function that may
888 * affect the decision on whether to place a reference group
889 * in private, shared or global memory.
891 static void check_shared_memory_bound(struct ppcg_kernel
*kernel
)
894 isl_val
*left
, *size
;
896 if (kernel
->options
->max_shared_memory
< 0)
899 left
= isl_val_int_from_si(kernel
->ctx
,
900 kernel
->options
->max_shared_memory
);
902 for (i
= 0; i
< kernel
->n_array
; ++i
) {
903 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
905 for (j
= 0; j
< local
->n_group
; ++j
) {
906 struct gpu_array_ref_group
*group
;
907 enum ppcg_group_access_type type
;
909 group
= local
->groups
[j
];
910 type
= gpu_array_ref_group_type(group
);
911 if (type
!= ppcg_access_shared
)
914 size
= gpu_array_tile_size(group
->shared_tile
);
915 size
= isl_val_mul_ui(size
, local
->array
->size
);
917 if (isl_val_le(size
, left
)) {
918 left
= isl_val_sub(left
, size
);
924 gpu_array_tile_free(group
->shared_tile
);
931 /* Mark all arrays of "kernel" that have an array reference group
932 * that is not mapped to private or shared memory as
933 * accessing the corresponding global device memory.
935 static void mark_global_arrays(struct ppcg_kernel
*kernel
)
939 for (i
= 0; i
< kernel
->n_array
; ++i
) {
940 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
944 for (j
= 0; j
< local
->n_group
; ++j
) {
945 if (gpu_array_ref_group_tile(local
->groups
[j
]))
949 local
->array
->global
= 1;
955 /* Compute a tiling for all the array reference groups in "kernel".
957 static void compute_group_tilings(struct ppcg_kernel
*kernel
)
961 for (i
= 0; i
< kernel
->n_array
; ++i
) {
962 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
964 for (j
= 0; j
< array
->n_group
; ++j
)
965 gpu_array_ref_group_compute_tiling(array
->groups
[j
]);
969 /* Compute the effective grid size as a list of the sizes in each dimension.
971 * The grid size specified by the user or set by default
972 * in read_grid_sizes() and applied by the block filter,
973 * may be too large for the given code in the sense that
974 * it may contain blocks that don't need to execute anything.
975 * We therefore don't return this grid size, but instead the
976 * smallest grid size that ensures that all blocks that actually
977 * execute code are included in the grid.
979 * We first extract a description of the grid, i.e., the possible values
980 * of the block ids, from the domain elements in "domain" and
981 * kernel->block_filter.
982 * The block ids are parameters in kernel->block_filter.
983 * We simply need to change them into set dimensions.
985 * Then, for each block dimension, we compute the maximal value of the block id
988 static __isl_give isl_multi_pw_aff
*extract_grid_size(
989 struct ppcg_kernel
*kernel
, __isl_take isl_union_set
*domain
)
994 isl_multi_pw_aff
*size
;
996 domain
= isl_union_set_intersect(domain
,
997 isl_union_set_copy(kernel
->block_filter
));
998 grid
= isl_union_set_params(domain
);
999 grid
= isl_set_from_params(grid
);
1000 grid
= isl_set_add_dims(grid
, isl_dim_set
, kernel
->n_grid
);
1001 for (i
= 0; i
< kernel
->n_grid
; ++i
) {
1005 id
= isl_id_list_get_id(kernel
->block_ids
, i
);
1006 pos
= isl_set_find_dim_by_id(grid
, isl_dim_param
, id
);
1009 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
1010 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
1013 grid
= isl_set_coalesce(grid
);
1014 size
= ppcg_size_from_extent(grid
);
1015 context
= isl_set_params(isl_set_copy(kernel
->context
));
1016 return isl_multi_pw_aff_gist(size
, context
);
1019 /* Compute the size of a fixed bounding box around the origin and "set",
1020 * where "set" is assumed to contain only non-negative elements,
1021 * and store the results in "size".
1022 * In particular, compute the maximal value of "set" in each direction
1025 static void extract_fixed_size(__isl_take isl_set
*set
, int *size
)
1028 isl_local_space
*ls
;
1031 n
= isl_set_dim(set
, isl_dim_set
);
1032 ls
= isl_local_space_from_space(isl_set_get_space(set
));
1033 obj
= isl_aff_zero_on_domain(ls
);
1034 for (i
= 0; i
< n
; ++i
) {
1037 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 1);
1038 max
= isl_set_max_val(set
, obj
);
1039 size
[i
] = isl_val_get_num_si(max
) + 1;
1041 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 0);
1047 /* Compute the effective block size as a list of the sizes in each dimension
1048 * and store the sizes in kernel->block_dim.
1050 * The block size specified by the user or set by default
1051 * in read_block_sizes() and applied by the thread filter,
1052 * may be too large for the given code in the sense that
1053 * it may contain threads that don't need to execute anything.
1054 * We therefore update this block size in kernel->block_dim
1055 * to the smallest block size that ensures that all threads
1056 * that actually execute code are included in the block.
1058 * The possible values of the thread ids is obtained from
1059 * the domain elements "domain" and kernel->thread_filter.
1060 * The current implementation eliminates all parameters, ensuring
1061 * that the size is a fixed constant in each dimension.
1062 * In principle we could also compute parametric sizes.
1063 * We would have to make sure to project out all b%d and t%d parameters,
1066 static isl_stat
extract_block_size(struct ppcg_kernel
*kernel
,
1067 __isl_take isl_union_set
*domain
)
1073 domain
= isl_union_set_intersect(domain
,
1074 isl_union_set_copy(kernel
->thread_filter
));
1075 block
= isl_union_set_params(domain
);
1076 block
= isl_set_from_params(block
);
1077 block
= isl_set_add_dims(block
, isl_dim_set
, kernel
->n_block
);
1078 for (i
= 0; i
< kernel
->n_block
; ++i
) {
1083 return isl_stat_error
;
1085 id
= isl_id_list_get_id(kernel
->thread_ids
, i
);
1086 pos
= isl_set_find_dim_by_id(block
, isl_dim_param
, id
);
1089 isl_die(isl_set_get_ctx(block
), isl_error_internal
,
1090 "missing constraints on thread identifier",
1091 block
= isl_set_free(block
));
1092 block
= isl_set_equate(block
, isl_dim_param
, pos
,
1095 nparam
= isl_set_dim(block
, isl_dim_param
);
1096 block
= isl_set_project_out(block
, isl_dim_param
, 0, nparam
);
1099 return isl_stat_error
;
1101 extract_fixed_size(block
, kernel
->block_dim
);
1106 struct ppcg_kernel
*ppcg_kernel_free(struct ppcg_kernel
*kernel
)
1113 isl_id_list_free(kernel
->block_ids
);
1114 isl_id_list_free(kernel
->thread_ids
);
1115 isl_multi_pw_aff_free(kernel
->grid_size
);
1116 isl_ast_expr_free(kernel
->grid_size_expr
);
1117 isl_set_free(kernel
->context
);
1118 isl_union_set_free(kernel
->core
);
1119 isl_union_set_free(kernel
->arrays
);
1120 isl_space_free(kernel
->space
);
1121 isl_ast_node_free(kernel
->tree
);
1122 isl_union_set_free(kernel
->block_filter
);
1123 isl_union_set_free(kernel
->thread_filter
);
1124 isl_union_pw_multi_aff_free(kernel
->copy_schedule
);
1125 isl_union_set_free(kernel
->sync_writes
);
1127 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1128 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1130 for (j
= 0; j
< array
->n_group
; ++j
)
1131 gpu_array_ref_group_free(array
->groups
[j
]);
1132 free(array
->groups
);
1134 isl_multi_pw_aff_free(array
->bound
);
1135 isl_ast_expr_free(array
->bound_expr
);
1137 free(kernel
->array
);
1139 for (i
= 0; i
< kernel
->n_var
; ++i
) {
1140 free(kernel
->var
[i
].name
);
1141 isl_vec_free(kernel
->var
[i
].size
);
1150 /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
1152 static void ppcg_kernel_free_wrap(void *user
)
1154 struct ppcg_kernel
*kernel
= user
;
1156 ppcg_kernel_free(kernel
);
1159 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
1160 struct ppcg_kernel_var
*var
)
1163 struct gpu_array_tile
*tile
;
1166 var
->array
= group
->array
;
1168 var
->type
= gpu_array_ref_group_type(group
);
1169 tile
= gpu_array_ref_group_tile(group
);
1171 p
= isl_printer_to_str(ctx
);
1172 p
= gpu_array_ref_group_print_name(group
, p
);
1173 var
->name
= isl_printer_get_str(p
);
1174 isl_printer_free(p
);
1176 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
1178 for (j
= 0; j
< group
->array
->n_index
; ++j
)
1179 var
->size
= isl_vec_set_element_val(var
->size
, j
,
1180 isl_val_copy(tile
->bound
[j
].size
));
1183 static int create_kernel_vars(struct ppcg_kernel
*kernel
)
1188 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1189 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1191 for (j
= 0; j
< array
->n_group
; ++j
) {
1192 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1193 enum ppcg_group_access_type type
;
1195 type
= gpu_array_ref_group_type(group
);
1196 if (type
!= ppcg_access_global
)
1202 kernel
->var
= isl_calloc_array(kernel
->ctx
, struct ppcg_kernel_var
, n
);
1207 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1208 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1210 for (j
= 0; j
< array
->n_group
; ++j
) {
1211 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1212 enum ppcg_group_access_type type
;
1214 type
= gpu_array_ref_group_type(group
);
1215 if (type
== ppcg_access_global
)
1217 create_kernel_var(kernel
->ctx
, group
, &kernel
->var
[n
]);
1225 /* Replace "pa" by the zero function defined over the universe domain
1226 * in the space of "pa".
1228 static __isl_give isl_pw_aff
*set_universally_zero(__isl_take isl_pw_aff
*pa
)
1233 space
= isl_space_domain(isl_pw_aff_get_space(pa
));
1234 isl_pw_aff_free(pa
);
1235 zero
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1237 return isl_pw_aff_from_aff(zero
);
1240 /* The sizes of the arrays on the host that have been computed by
1241 * extract_array_info may depend on the parameters. Use the extra
1242 * constraints on the parameters that are valid at "host_domain"
1243 * to simplify these expressions and store the results in kernel->array.
1245 * We only need these localized bounds for arrays that are accessed
1246 * by the current kernel. If we have found at least one reference group
1247 * then the array is accessed by the kernel.
1249 * The resulting sizes may be functions that are nowhere defined
1250 * in case the access function cannot possibly access anything inside
1251 * the kernel for some reason. If so, they are replaced by the zero
1252 * function. Since the access function cannot actually access anything,
1253 * there is no harm in printing the array sizes as zero.
1255 static void localize_bounds(struct ppcg_kernel
*kernel
,
1256 __isl_keep isl_set
*host_domain
)
1261 context
= isl_set_copy(host_domain
);
1262 context
= isl_set_params(context
);
1264 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1265 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1266 isl_multi_pw_aff
*bound
;
1269 if (local
->n_group
== 0)
1272 n_index
= local
->array
->n_index
;
1273 bound
= isl_multi_pw_aff_copy(local
->array
->bound
);
1275 for (j
= 0; j
< n_index
; ++j
) {
1279 pwaff
= isl_multi_pw_aff_get_pw_aff(bound
, j
);
1280 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
1281 empty
= isl_pw_aff_is_empty(pwaff
);
1283 pwaff
= isl_pw_aff_free(pwaff
);
1285 pwaff
= set_universally_zero(pwaff
);
1286 bound
= isl_multi_pw_aff_set_pw_aff(bound
, j
, pwaff
);
1289 local
->n_index
= n_index
;
1290 local
->bound
= bound
;
1292 isl_set_free(context
);
1295 /* Create the array of gpu_local_array_info structures "array"
1296 * inside "kernel". The number of elements in this array is
1297 * the same as the number of arrays in "prog".
1298 * Initialize the "array" field of each local array to point
1299 * to the corresponding array in "prog".
1301 static struct ppcg_kernel
*ppcg_kernel_create_local_arrays(
1302 struct ppcg_kernel
*kernel
, struct gpu_prog
*prog
)
1307 ctx
= isl_set_get_ctx(prog
->context
);
1308 kernel
->array
= isl_calloc_array(ctx
,
1309 struct gpu_local_array_info
, prog
->n_array
);
1311 return ppcg_kernel_free(kernel
);
1312 kernel
->n_array
= prog
->n_array
;
1314 for (i
= 0; i
< prog
->n_array
; ++i
)
1315 kernel
->array
[i
].array
= &prog
->array
[i
];
1320 /* Does "kernel" need to be passed an argument corresponding to array "i"?
1322 * The argument is only needed if the kernel accesses this device memory.
1324 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
)
1326 return kernel
->array
[i
].global
;
1329 /* Find the element in gen->stmt that has the given "id".
1330 * Return NULL if no such gpu_stmt can be found.
1332 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
1336 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
1337 if (id
== prog
->stmts
[i
].id
)
1341 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
1344 void ppcg_kernel_stmt_free(void *user
)
1346 struct ppcg_kernel_stmt
*stmt
= user
;
1351 switch (stmt
->type
) {
1352 case ppcg_kernel_copy
:
1353 isl_ast_expr_free(stmt
->u
.c
.index
);
1354 isl_ast_expr_free(stmt
->u
.c
.local_index
);
1356 case ppcg_kernel_domain
:
1357 isl_id_to_ast_expr_free(stmt
->u
.d
.ref2expr
);
1359 case ppcg_kernel_sync
:
1366 /* Return the gpu_stmt_access in the list "accesses" that corresponds
1369 static struct gpu_stmt_access
*find_access(struct gpu_stmt_access
*accesses
,
1370 __isl_keep isl_id
*ref_id
)
1372 struct gpu_stmt_access
*access
;
1374 for (access
= accesses
; access
; access
= access
->next
)
1375 if (access
->ref_id
== ref_id
)
1381 /* Return the index of the array called "name" in the list of arrays.
1383 static int find_array_index(struct ppcg_kernel
*kernel
, const char *name
)
1387 for (i
= 0; i
< kernel
->n_array
; ++i
)
1388 if (!strcmp(name
, kernel
->array
[i
].array
->name
))
1394 /* Internal data structure for the index and AST expression transformation
1395 * callbacks for pet_stmt_build_ast_exprs.
1397 * "kernel" is the kernel for which are computing AST expressions and
1398 * may be NULL if we are not inside a kernel.
1399 * "accesses" is the list of gpu_stmt_access in the statement.
1400 * "iterator_map" expresses the statement iterators in terms of
1401 * the AST loop iterators.
1402 * "sched2copy" expresses the outer copy_schedule_dim dimensions of
1403 * the kernel schedule in terms of the AST loop iterators and
1404 * may be NULL if we are not inside a kernel.
1406 * The following fields are set in transform_index and used in transform_expr.
1407 * "array" is the array that is being accessed.
1408 * "global" is set if the global array is accessed (rather than
1409 * shared/private memory).
1410 * "local_array" refers to information on the array specialized
1411 * to the current kernel.
1413 struct ppcg_transform_data
{
1414 struct ppcg_kernel
*kernel
;
1415 struct gpu_stmt_access
*accesses
;
1416 isl_pw_multi_aff
*iterator_map
;
1417 isl_pw_multi_aff
*sched2copy
;
1419 struct gpu_array_info
*array
;
1421 struct gpu_local_array_info
*local_array
;
1424 /* Return a pointer to the gpu_array_ref_group in "local"
1425 * that contains the reference "access".
1426 * Return NULL if no such group can be found.
1428 static struct gpu_array_ref_group
*find_ref_group(
1429 struct gpu_local_array_info
*local
, struct gpu_stmt_access
*access
)
1433 for (i
= 0; i
< local
->n_group
; ++i
) {
1434 struct gpu_array_ref_group
*group
= local
->groups
[i
];
1436 for (j
= 0; j
< group
->n_ref
; ++j
)
1437 if (group
->refs
[j
] == access
)
1444 /* Index transformation callback for pet_stmt_build_ast_exprs.
1446 * "index" expresses the array indices in terms of statement iterators
1448 * We first reformulate "index" in terms of the AST loop iterators.
1449 * Then we check if we are accessing the global array or
1450 * a shared/private copy. In particular, if we are not inside a kernel
1451 * then we must be accessing a global array.
1452 * In the former case, we simply return
1453 * the updated index. If "index" is an affine expression rather
1454 * than an array access, then we also return the updated index here.
1456 * If no reference groups have been computed for the array,
1457 * then we can only be accessing the global array.
1459 * Otherwise, we apply the tiling to the index.
1460 * This tiling is of the form
1464 * where D corresponds to the outer tile->depth dimensions of
1465 * the kernel schedule.
1466 * The index is of the form
1470 * We update the tiling to refer to the AST loop iterators
1474 * and modify index to keep track of those iterators
1478 * Combining these two yields a tiled index expression in terms
1479 * of the AST loop iterators
1483 static __isl_give isl_multi_pw_aff
*transform_index(
1484 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*ref_id
,
1487 struct ppcg_transform_data
*data
= user
;
1488 struct gpu_stmt_access
*access
;
1489 struct gpu_array_ref_group
*group
;
1490 struct gpu_array_tile
*tile
;
1491 isl_pw_multi_aff
*iterator_map
;
1496 isl_multi_pw_aff
*tiling
;
1497 isl_pw_multi_aff
*pma
;
1498 isl_multi_pw_aff
*mpa
;
1499 isl_pw_multi_aff
*sched2depth
;
1503 iterator_map
= isl_pw_multi_aff_copy(data
->iterator_map
);
1504 index
= isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
1509 access
= find_access(data
->accesses
, ref_id
);
1512 if (!isl_map_has_tuple_name(access
->access
, isl_dim_out
))
1515 name
= get_outer_array_name(access
->access
);
1516 i
= find_array_index(data
->kernel
, name
);
1518 isl_die(isl_multi_pw_aff_get_ctx(index
), isl_error_internal
,
1519 "cannot find array",
1520 return isl_multi_pw_aff_free(index
));
1521 data
->local_array
= &data
->kernel
->array
[i
];
1522 data
->array
= data
->local_array
->array
;
1524 group
= find_ref_group(data
->local_array
, access
);
1530 tile
= gpu_array_ref_group_tile(group
);
1531 data
->global
= !tile
;
1535 space
= isl_space_range(isl_multi_pw_aff_get_space(index
));
1536 space
= isl_space_map_from_set(space
);
1537 pma
= isl_pw_multi_aff_identity(space
);
1538 sched2depth
= isl_pw_multi_aff_copy(data
->sched2copy
);
1539 dim
= isl_pw_multi_aff_dim(sched2depth
, isl_dim_out
);
1540 sched2depth
= isl_pw_multi_aff_drop_dims(sched2depth
, isl_dim_out
,
1541 tile
->depth
, dim
- tile
->depth
);
1542 pma
= isl_pw_multi_aff_product(sched2depth
, pma
);
1543 tiling
= isl_multi_pw_aff_from_multi_aff(
1544 isl_multi_aff_copy(tile
->tiling
));
1545 tiling
= isl_multi_pw_aff_pullback_pw_multi_aff(tiling
, pma
);
1547 space
= isl_space_domain(isl_multi_pw_aff_get_space(index
));
1548 space
= isl_space_map_from_set(space
);
1549 mpa
= isl_multi_pw_aff_identity(space
);
1550 index
= isl_multi_pw_aff_range_product(mpa
, index
);
1551 index
= isl_multi_pw_aff_pullback_multi_pw_aff(tiling
, index
);
1556 /* Dereference "expr" by adding an index [0].
1557 * The original "expr" is assumed not to have any indices.
1559 * If "expr" is a member access, then the dereferencing needs
1560 * to be applied to the structure argument of this member access.
1562 static __isl_give isl_ast_expr
*dereference(__isl_take isl_ast_expr
*expr
)
1565 isl_ast_expr
*arg0
, *res
;
1566 isl_ast_expr_list
*list
;
1568 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1570 return isl_ast_expr_free(expr
);
1571 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1572 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1575 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1576 arg
= dereference(arg
);
1577 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1578 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1582 isl_ast_expr_free(arg0
);
1584 ctx
= isl_ast_expr_get_ctx(expr
);
1585 res
= isl_ast_expr_from_val(isl_val_zero(ctx
));
1586 list
= isl_ast_expr_list_from_ast_expr(res
);
1587 res
= isl_ast_expr_get_op_arg(expr
, 0);
1588 res
= isl_ast_expr_access(res
, list
);
1589 isl_ast_expr_free(expr
);
1594 /* Linearize the index expression "expr" based on the array bounds
1597 * That is, transform expression
1599 * A[i_0][i_1]...[i_n]
1603 * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
1605 * where b_0, b_1, ..., b_n are the bounds on the array.
1607 * If the base of "expr" is a member access, then the linearization needs
1608 * to be applied to the structure argument of this member access.
1610 * In the base case, if "expr" has no arguments (other than the name of
1611 * the array), then we are passing an entire array to a function.
1612 * In this case, there is nothing to linearize.
1613 * Note that at this point an expression with no arguments can
1614 * only be an entire array because the scalar case and
1615 * the case of single struct are handled by the caller.
1617 * If the number of specified index expressions in "expr"
1618 * is smaller than the dimension of the accessed array,
1619 * then the missing i_j also do not appear in the linearized expression.
1620 * Furthermore, since such an expression does not refer to a single
1621 * element while the default linearized expression would refer to
1622 * a single element, we return the expression
1624 * A + (..((i_0 * b_1 + i_1) ... ) * b_l + i_l)
1626 * instead. Note that because of the special case handling above,
1627 * we can assume here that there is at least one index expression.
1629 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
1630 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
)
1635 isl_ast_expr_list
*list
;
1637 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1638 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1639 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1642 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1643 arg
= gpu_local_array_info_linearize_index(array
, arg
);
1644 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1645 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1649 isl_ast_expr_free(arg0
);
1651 if (isl_ast_expr_get_op_n_arg(expr
) == 1)
1654 n
= isl_ast_expr_get_op_n_arg(expr
);
1655 res
= isl_ast_expr_get_op_arg(expr
, 1);
1656 for (i
= 1; i
< array
->n_index
; ++i
) {
1657 isl_ast_expr
*expr_i
;
1659 expr_i
= isl_ast_expr_get_op_arg(array
->bound_expr
, 1 + i
);
1660 res
= isl_ast_expr_mul(res
, expr_i
);
1664 expr_i
= isl_ast_expr_get_op_arg(expr
, i
+ 1);
1665 res
= isl_ast_expr_add(res
, expr_i
);
1668 if (1 + array
->n_index
> n
) {
1669 res
= isl_ast_expr_add(isl_ast_expr_get_op_arg(expr
, 0), res
);
1671 list
= isl_ast_expr_list_from_ast_expr(res
);
1672 res
= isl_ast_expr_get_op_arg(expr
, 0);
1673 res
= isl_ast_expr_access(res
, list
);
1676 isl_ast_expr_free(expr
);
1681 /* AST expression transformation callback for pet_stmt_build_ast_exprs.
1683 * If the AST expression refers to an array that is not accessed
1684 * at all, then this means the value of the expression is not used,
1685 * so we might as well print zero (NULL pointer) instead.
1687 * If the AST expression refers to a global scalar that is not
1688 * a read-only scalar, then its address was passed to the kernel and
1689 * we need to dereference it.
1691 * If the AST expression refers to an access to a global array,
1692 * then we linearize the access exploiting the bounds in data->local_array.
1694 static __isl_give isl_ast_expr
*transform_expr(__isl_take isl_ast_expr
*expr
,
1695 __isl_keep isl_id
*id
, void *user
)
1697 struct ppcg_transform_data
*data
= user
;
1701 if (!data
->array
->accessed
) {
1704 ctx
= isl_ast_expr_get_ctx(expr
);
1705 isl_ast_expr_free(expr
);
1706 return isl_ast_expr_from_val(isl_val_zero(ctx
));
1708 if (gpu_array_is_read_only_scalar(data
->array
))
1712 if (data
->array
->n_index
== 0)
1713 return dereference(expr
);
1714 if (!data
->array
->linearize
)
1717 return gpu_local_array_info_linearize_index(data
->local_array
, expr
);
1720 /* This function is called for each instance of a user statement
1721 * in the kernel "kernel", identified by "gpu_stmt".
1722 * "kernel" may be NULL if we are not inside a kernel.
1724 * We attach a struct ppcg_kernel_stmt to the "node", containing
1725 * a computed AST expression for each access, through an annotation
1727 * These AST expressions are computed from iterator_map,
1728 * which expresses the domain
1729 * elements in terms of the generated loops, and sched2copy,
1730 * which expresses the outer copy_schedule_dim dimensions of
1731 * the kernel schedule computed by PPCG in terms of the generated loops.
1733 static __isl_give isl_ast_node
*create_domain_leaf(
1734 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1735 __isl_keep isl_ast_build
*build
, struct gpu_stmt
*gpu_stmt
)
1737 struct ppcg_transform_data data
;
1738 struct ppcg_kernel_stmt
*stmt
;
1741 isl_pw_multi_aff
*sched2copy
;
1743 isl_pw_multi_aff
*iterator_map
;
1744 isl_union_map
*schedule
;
1748 ctx
= isl_ast_node_get_ctx(node
);
1750 stmt
= isl_calloc_type(ctx
, struct ppcg_kernel_stmt
);
1752 return isl_ast_node_free(node
);
1754 schedule
= isl_ast_build_get_schedule(build
);
1755 map
= isl_map_reverse(isl_map_from_union_map(schedule
));
1756 iterator_map
= isl_pw_multi_aff_from_map(map
);
1758 sched2copy
= compute_sched_to_copy(kernel
,
1759 isl_pw_multi_aff_copy(iterator_map
));
1763 stmt
->type
= ppcg_kernel_domain
;
1764 stmt
->u
.d
.stmt
= gpu_stmt
;
1766 data
.kernel
= kernel
;
1767 data
.accesses
= stmt
->u
.d
.stmt
->accesses
;
1768 data
.iterator_map
= iterator_map
;
1769 data
.sched2copy
= sched2copy
;
1770 stmt
->u
.d
.ref2expr
= pet_stmt_build_ast_exprs(stmt
->u
.d
.stmt
->stmt
,
1771 build
, &transform_index
, &data
,
1772 &transform_expr
, &data
);
1774 isl_pw_multi_aff_free(iterator_map
);
1775 isl_pw_multi_aff_free(sched2copy
);
1777 id
= isl_id_alloc(ctx
, "user", stmt
);
1778 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1779 return isl_ast_node_set_annotation(node
, id
);
1782 /* This function is called for each statement node in the AST
1783 * for copying to or from shared/private memory.
1784 * Attach a pointer to a ppcg_kernel_stmt representing the copy
1785 * statement to the node.
1786 * The statement name is "read" or "write", depending on whether we are
1787 * reading from global memory or writing to global memory.
1789 * The schedule is of the form
1793 * where D corresponds to the outer tile->depth dimensions of
1794 * the kernel schedule, A to the global array and L to the outer
1795 * generated AST schedule.
1796 * We compute the inverse and strip off the type, resulting in
1800 * We combine this mapping with on the one hand the projection
1804 * and on the other hand the group tiling
1812 * and store the corresponding expressions in stmt->index and stmt->local_index,
1813 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
1814 * stmt->index is linearized if the global memory array is linearized.
1816 static __isl_give isl_ast_node
*create_access_leaf(struct ppcg_kernel
*kernel
,
1817 struct gpu_array_ref_group
*group
, __isl_take isl_ast_node
*node
,
1818 __isl_keep isl_ast_build
*build
)
1820 struct ppcg_kernel_stmt
*stmt
;
1821 struct gpu_array_tile
*tile
;
1826 isl_pw_multi_aff
*pma
, *pma2
;
1829 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1831 return isl_ast_node_free(node
);
1833 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
1834 type
= isl_map_get_tuple_name(access
, isl_dim_in
);
1835 stmt
->u
.c
.read
= !strcmp(type
, "read");
1836 access
= isl_map_reverse(access
);
1837 pma
= isl_pw_multi_aff_from_map(access
);
1838 pma
= isl_pw_multi_aff_reset_tuple_id(pma
, isl_dim_out
);
1840 space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
1841 space
= isl_space_unwrap(space
);
1842 pma2
= isl_pw_multi_aff_range_map(space
);
1843 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
,
1844 isl_pw_multi_aff_copy(pma
));
1845 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1846 if (group
->array
->linearize
)
1847 expr
= gpu_local_array_info_linearize_index(group
->local_array
,
1849 stmt
->u
.c
.index
= expr
;
1851 tile
= gpu_array_ref_group_tile(group
);
1852 pma2
= isl_pw_multi_aff_from_multi_aff(
1853 isl_multi_aff_copy(tile
->tiling
));
1854 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
, pma
);
1855 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1856 stmt
->u
.c
.local_index
= expr
;
1858 stmt
->u
.c
.array
= group
->array
;
1859 stmt
->u
.c
.local_array
= group
->local_array
;
1860 stmt
->type
= ppcg_kernel_copy
;
1862 id
= isl_id_alloc(kernel
->ctx
, "copy", stmt
);
1863 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1864 return isl_ast_node_set_annotation(node
, id
);
1867 /* Create a synchronization ppcg_kernel_stmt and
1868 * attach it to the node "node" representing the synchronization.
1870 static __isl_give isl_ast_node
*create_sync_leaf(
1871 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1872 __isl_keep isl_ast_build
*build
)
1874 struct ppcg_kernel_stmt
*stmt
;
1877 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1879 return isl_ast_node_free(node
);
1881 stmt
->type
= ppcg_kernel_sync
;
1882 id
= isl_id_alloc(kernel
->ctx
, "sync", stmt
);
1883 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1884 return isl_ast_node_set_annotation(node
, id
);
1887 /* Build AST expressions for the device array sizes of all arrays in "prog"
1888 * that require allocation on the device using "build", as well as
1889 * for the original array sizes of all arrays that need to be declared
1891 * "node" is freed in case of error.
1893 static __isl_give isl_ast_node
*build_array_bounds(
1894 __isl_take isl_ast_node
*node
, struct gpu_prog
*prog
,
1895 __isl_keep isl_ast_build
*build
)
1899 for (i
= 0; i
< prog
->n_array
; ++i
) {
1900 struct gpu_array_info
*array
= &prog
->array
[i
];
1901 isl_multi_pw_aff
*size
;
1904 if (!gpu_array_requires_device_allocation(array
))
1907 size
= isl_multi_pw_aff_copy(array
->bound
);
1908 expr
= ppcg_build_size_expr(size
, build
);
1909 array
->bound_expr
= expr
;
1911 return isl_ast_node_free(node
);
1914 for (i
= 0; i
< prog
->n_array
; ++i
) {
1915 struct gpu_array_info
*array
= &prog
->array
[i
];
1916 struct pet_array
*pet_array
= prog
->scop
->pet
->arrays
[i
];
1917 isl_multi_pw_aff
*size
;
1920 if (!array
->declare_local
)
1922 size
= ppcg_size_from_extent(isl_set_copy(pet_array
->extent
));
1923 expr
= ppcg_build_size_expr(size
, build
);
1924 array
->declared_size
= expr
;
1926 return isl_ast_node_free(node
);
1932 /* Internal data structure for at_domain.
1934 * "prog" represents the entire scop.
1935 * "kernel" points to the kernel to which the current schedule node
1936 * belongs. It is set by before_mark and reset by after_mark.
1937 * It may be NULL if we are outside any kernel.
1939 struct ppcg_at_domain_data
{
1940 struct gpu_prog
*prog
;
1941 struct ppcg_kernel
*kernel
;
1944 /* This function is called for each instance of a user statement
1945 * in the kernel. This may be one of the original user statements
1946 * or a statement introduced by PPCG.
1948 * We first check if the statement id corresponds to a gpu statement,
1949 * which indicates the statement is an original user statement. Any statement
1950 * that is not an original user statement has been introduced by PPCG and
1951 * requires special handling.
1953 * If the user statement is one of the original user statements, then we call
1954 * create_domain_leaf. If it is "init_device", then we call
1955 * build_array_bounds. Otherwise, we check if it is a copy or synchronization
1956 * statement and call the appropriate functions. Statements that copy an array
1957 * to/from the device do not need any further treatment.
1958 * Neither does "clear_device".
1960 static __isl_give isl_ast_node
*at_domain(__isl_take isl_ast_node
*node
,
1961 __isl_keep isl_ast_build
*build
, void *user
)
1963 struct ppcg_at_domain_data
*data
= user
;
1964 struct gpu_stmt
*gpu_stmt
;
1965 isl_ast_expr
*expr
, *arg
;
1971 expr
= isl_ast_node_user_get_expr(node
);
1972 arg
= isl_ast_expr_get_op_arg(expr
, 0);
1973 id
= isl_ast_expr_get_id(arg
);
1974 name
= isl_id_get_name(id
);
1975 p
= isl_id_get_user(id
);
1976 isl_ast_expr_free(expr
);
1977 isl_ast_expr_free(arg
);
1979 gpu_stmt
= find_stmt(data
->prog
, id
);
1980 is_sync
= gpu_tree_id_is_sync(id
, data
->kernel
);
1984 return create_domain_leaf(data
->kernel
, node
, build
, gpu_stmt
);
1986 if (!prefixcmp(name
, "to_device_") || !prefixcmp(name
, "from_device_"))
1988 if (!strcmp(name
, "init_device"))
1989 return build_array_bounds(node
, data
->prog
, build
);
1990 if (!strcmp(name
, "clear_device"))
1993 return isl_ast_node_free(node
);
1994 if (!strcmp(name
, "read") || !strcmp(name
, "write")) {
1995 struct gpu_array_ref_group
*group
= p
;
1996 return create_access_leaf(data
->kernel
, group
, node
, build
);
1999 isl_die(data
->prog
->ctx
, isl_error_internal
,
2000 "unknown statement type",
2001 return isl_ast_node_free(node
));
2002 return create_sync_leaf(data
->kernel
, node
, build
);
2005 /* Given a set of wrapped references "ref", return the corresponding
2006 * access relations based on the tagged access relations "tagged".
2008 * The elements of "ref" are of the form
2012 * with D an iteration domains and R a reference.
2013 * The elements of "tagged" are of the form
2019 * Extend "tagged" to include the iteration domain in the range, i.e.,
2021 * [D -> R] -> [D -> A]
2023 * apply the result to "ref" and then unwrap the resulting set
2024 * to obtain relations of the form
2028 static __isl_give isl_union_map
*wrapped_reference_to_access(
2029 __isl_take isl_union_set
*ref
, __isl_take isl_union_map
*tagged
)
2031 isl_union_map
*tag2access
;
2033 tag2access
= isl_union_map_copy(tagged
);
2034 tag2access
= isl_union_map_universe(tag2access
);
2035 tag2access
= isl_union_set_unwrap(isl_union_map_domain(tag2access
));
2036 tag2access
= isl_union_map_domain_map(tag2access
);
2037 tag2access
= isl_union_map_range_product(tag2access
, tagged
);
2039 ref
= isl_union_set_coalesce(ref
);
2040 ref
= isl_union_set_apply(ref
, tag2access
);
2042 return isl_union_set_unwrap(ref
);
2045 /* Given an access relation "access" from one or more array reference groups,
2046 * remove those reads if ("read" is 1) or writes (if "read" is 0)
2047 * that are only needed to communicate data within
2048 * the same iteration of "sched".
2049 * "tagged" contains all tagged access relations to all
2050 * the array reference groups accessed by "access" from statement
2051 * instances scheduled by "sched".
2053 * If the access is a read then it is either an element of
2055 * live_in union (range flow)
2057 * where live_in and flow may be overapproximations, or
2058 * it reads an uninitialized value (that is not live-in because
2059 * there is an intermediate kill) or it reads a value that was
2060 * written within the same (compound) statement instance.
2061 * If the access is a write then it is either an element of
2063 * live_out union (domain flow)
2065 * or it writes a value that is never read (and is not live-out
2066 * because of an intermediate kill) or only
2067 * within the same (compound) statement instance.
2068 * In both cases, the access relation is also a subset of
2069 * the group access relation.
2071 * The cases where an uninitialized value is read or a value is written
2072 * that is never read or where the dataflow occurs within a statement
2073 * instance are also considered local and may also be removed.
2075 * Essentially, we compute the intersection of "access" with either
2077 * live_in union (range non-local-flow)
2081 * live_out union (domain non-local-flow)
2083 * We first construct a relation "local"
2085 * [[D -> R] -> [D' -> R']]
2087 * of pairs of domain iterations accessing the reference group
2088 * and references in the group that are coscheduled by "sched".
2090 * If this relation does not intersect the dataflow dependences,
2091 * then there is nothing we can possibly remove, unless the dataflow
2092 * dependences themselves only relate a subset of the accesses.
2093 * In particular, the accesses may not be involved in any dataflow
2094 * dependences, either because they are uninitialized reads/dead writes
2095 * or because the dataflow occurs inside a statement instance.
2097 * Since the computation below may break up the access relation
2098 * into smaller pieces, we only perform the intersection with
2099 * the non-local dependent accesses if the local pairs
2100 * intersect the dataflow dependences. Otherwise, we intersect
2101 * with the universe of the non-local dependent accesses.
2102 * This should at least remove accesses from statements that
2103 * do not participate in any dependences.
2105 * In particular, we remove the "local" dataflow dependences from
2106 * the set of all dataflow dependences, or at least those
2107 * that may contribute to a domain/range that intersects
2108 * the domain of "access".
2109 * Note that if the potential dataflow dependences are an overapproximation
2110 * of the actual dataflow dependences, then the result remains an
2111 * overapproximation of the non-local dataflow dependences.
2112 * Copying to/from global memory is only needed for the references
2113 * in the domain/range of the result or for accesses that are live out/in
2114 * for the entire scop.
2116 * We therefore map the domain/range of the "external" relation
2117 * to the corresponding access relation and take the union with
2118 * the live out/in relation.
2120 static __isl_give isl_union_map
*remove_local_accesses(
2121 struct gpu_prog
*prog
, __isl_take isl_union_map
*tagged
,
2122 __isl_take isl_union_map
*access
, __isl_take isl_union_map
*sched
,
2126 isl_union_pw_multi_aff
*tagger
;
2127 isl_union_set
*domain
, *access_domain
;
2128 isl_union_map
*local
, *external
, *universe
;
2129 isl_union_set
*tag_set
;
2131 if (isl_union_map_is_empty(access
)) {
2132 isl_union_map_free(sched
);
2133 isl_union_map_free(tagged
);
2137 tagger
= isl_union_pw_multi_aff_copy(prog
->scop
->tagger
);
2138 domain
= isl_union_map_domain(isl_union_map_copy(tagged
));
2139 tagger
= isl_union_pw_multi_aff_intersect_domain(tagger
,
2140 isl_union_set_copy(domain
));
2141 sched
= isl_union_map_preimage_domain_union_pw_multi_aff(sched
, tagger
);
2143 local
= isl_union_map_apply_range(sched
,
2144 isl_union_map_reverse(isl_union_map_copy(sched
)));
2145 local
= isl_union_map_intersect(local
,
2146 isl_union_map_copy(prog
->scop
->tagged_dep_flow
));
2148 empty
= isl_union_map_is_empty(local
);
2150 external
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
2151 universe
= isl_union_map_universe(isl_union_map_copy(access
));
2152 access_domain
= isl_union_map_domain(universe
);
2153 domain
= isl_union_set_universe(domain
);
2154 universe
= isl_union_set_unwrap(domain
);
2155 universe
= isl_union_map_intersect_domain(universe
, access_domain
);
2156 domain
= isl_union_map_wrap(universe
);
2158 external
= isl_union_map_intersect_range(external
, domain
);
2160 external
= isl_union_map_intersect_domain(external
, domain
);
2161 external
= isl_union_map_intersect_params(external
,
2162 isl_set_copy(prog
->scop
->context
));
2163 external
= isl_union_map_subtract(external
, local
);
2166 tag_set
= isl_union_map_range(external
);
2167 external
= wrapped_reference_to_access(tag_set
, tagged
);
2168 external
= isl_union_map_union(external
,
2169 isl_union_map_copy(prog
->scop
->live_in
));
2171 tag_set
= isl_union_map_domain(external
);
2172 external
= wrapped_reference_to_access(tag_set
, tagged
);
2173 external
= isl_union_map_union(external
,
2174 isl_union_map_copy(prog
->scop
->live_out
));
2178 external
= isl_union_map_free(external
);
2180 external
= isl_union_map_universe(external
);
2182 access
= isl_union_map_intersect(access
, external
);
2187 /* Given an access relation "access" from "group", remove those reads
2188 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2189 * communicate data within the same iteration of the schedule at the
2190 * position where the copying of the group is inserted.
2191 * "node" points to this position, i.e., the depth at "node"
2192 * is equal to tile->depth.
2194 * We extract a schedule that picks out the iterations of the outer
2195 * tile->depth dimensions and call remove_local_accesses.
2197 static __isl_give isl_union_map
*remove_local_accesses_group(
2198 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
2199 __isl_take isl_union_map
*access
, __isl_keep isl_schedule_node
*node
,
2202 isl_union_map
*sched
, *tagged
;
2204 if (isl_union_map_is_empty(access
))
2207 tagged
= group_tagged_access_relation(group
);
2208 sched
= isl_schedule_node_get_prefix_schedule_relation(node
);
2210 return remove_local_accesses(kernel
->prog
, tagged
, access
, sched
, read
);
2213 /* Build an access AST expression for the effective grid size using "build".
2214 * Store the result in kernel->grid_size_expr.
2216 static isl_stat
build_grid_size(struct ppcg_kernel
*kernel
,
2217 __isl_keep isl_ast_build
*build
)
2219 isl_multi_pw_aff
*size
;
2221 size
= isl_multi_pw_aff_copy(kernel
->grid_size
);
2222 size
= isl_multi_pw_aff_set_tuple_name(size
, isl_dim_out
, "grid");
2223 kernel
->grid_size_expr
= ppcg_build_size_expr(size
, build
);
2225 if (!kernel
->grid_size_expr
)
2226 return isl_stat_error
;
2230 /* Build access AST expressions for the localized array sizes using "build".
2231 * Store the result in local->bound_expr.
2232 * Only do this for arrays for which localized bounds have been computed.
2234 static isl_stat
build_local_array_sizes(struct ppcg_kernel
*kernel
,
2235 __isl_keep isl_ast_build
*build
)
2239 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2240 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
2241 isl_multi_pw_aff
*size
;
2243 if (local
->n_group
== 0)
2245 size
= isl_multi_pw_aff_copy(local
->bound
);
2246 local
->bound_expr
= ppcg_build_size_expr(size
, build
);
2247 if (!local
->bound_expr
)
2248 return isl_stat_error
;
2254 /* Build access AST expressions for the effective grid size and
2255 * the localized array sizes using "build".
2257 static isl_stat
build_grid_and_local_array_sizes(struct ppcg_kernel
*kernel
,
2258 __isl_keep isl_ast_build
*build
)
2260 if (build_grid_size(kernel
, build
) < 0)
2261 return isl_stat_error
;
2262 if (build_local_array_sizes(kernel
, build
) < 0)
2263 return isl_stat_error
;
2267 /* This function is called before the AST generator starts traversing
2268 * the schedule subtree of a node with mark "mark".
2270 * If the mark is called "kernel", store the kernel pointer in data->kernel
2271 * for use in at_domain and build AST expressions for the grid size and
2272 * the localized array sizes.
2274 static isl_stat
before_mark(__isl_keep isl_id
*mark
,
2275 __isl_keep isl_ast_build
*build
, void *user
)
2277 struct ppcg_at_domain_data
*data
= user
;
2280 return isl_stat_error
;
2281 if (!strcmp(isl_id_get_name(mark
), "kernel")) {
2282 data
->kernel
= isl_id_get_user(mark
);
2283 if (build_grid_and_local_array_sizes(data
->kernel
, build
) < 0)
2284 return isl_stat_error
;
2289 /* This function is called after the AST generator has finished traversing
2290 * the schedule subtree of a mark node. "node" points to the corresponding
2293 * If the mark is called "kernel", then replace "node" by a user node
2294 * that "calls" the kernel, representing the launch of the kernel.
2295 * The original "node" is stored inside the kernel object so that
2296 * it can be used to print the device code.
2297 * Note that this assumes that a kernel is only launched once.
2298 * Also clear data->kernel.
2300 static __isl_give isl_ast_node
*after_mark(__isl_take isl_ast_node
*node
,
2301 __isl_keep isl_ast_build
*build
, void *user
)
2306 isl_ast_expr_list
*list
;
2307 struct ppcg_kernel
*kernel
;
2308 struct ppcg_at_domain_data
*data
= user
;
2310 ctx
= isl_ast_node_get_ctx(node
);
2311 id
= isl_ast_node_mark_get_id(node
);
2313 return isl_ast_node_free(node
);
2314 if (strcmp(isl_id_get_name(id
), "kernel") || !data
->kernel
) {
2318 kernel
= data
->kernel
;
2319 data
->kernel
= NULL
;
2320 kernel
->space
= isl_ast_build_get_schedule_space(build
);
2321 kernel
->tree
= isl_ast_node_mark_get_node(node
);
2322 isl_ast_node_free(node
);
2324 expr
= isl_ast_expr_from_id(isl_id_copy(id
));
2325 list
= isl_ast_expr_list_alloc(ctx
, 0);
2326 expr
= isl_ast_expr_call(expr
, list
);
2327 node
= isl_ast_node_alloc_user(expr
);
2328 node
= isl_ast_node_set_annotation(node
, id
);
2333 static isl_bool
update_depth(__isl_keep isl_schedule_node
*node
, void *user
)
2338 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2339 return isl_bool_true
;
2340 node_depth
= isl_schedule_node_get_schedule_depth(node
);
2341 if (node_depth
> *depth
)
2342 *depth
= node_depth
;
2344 return isl_bool_false
;
2347 /* Use isl to generate code for both the host and the device
2349 * The device code is marked by "kernel" mark nodes in the schedule tree,
2350 * containing a pointer to a ppcg_kernel object.
2351 * The returned AST only contains the AST for the host code.
2352 * The ASTs for the device code are embedded in ppcg_kernel objects
2353 * attached to the leaf nodes that call "kernel".
2355 static __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
,
2356 __isl_take isl_schedule
*schedule
)
2358 struct ppcg_at_domain_data data
;
2359 isl_ast_build
*build
;
2361 isl_id_list
*iterators
;
2364 data
.prog
= gen
->prog
;
2368 if (isl_schedule_foreach_schedule_node_top_down(schedule
, &update_depth
,
2371 build
= isl_ast_build_alloc(gen
->prog
->ctx
);
2372 iterators
= ppcg_scop_generate_names(gen
->prog
->scop
, depth
, "c");
2373 build
= isl_ast_build_set_iterators(build
, iterators
);
2374 build
= isl_ast_build_set_at_each_domain(build
, &at_domain
, &data
);
2375 build
= isl_ast_build_set_before_each_mark(build
, &before_mark
, &data
);
2376 build
= isl_ast_build_set_after_each_mark(build
, &after_mark
, &data
);
2377 if (gen
->prog
->scop
->options
->debug
->dump_final_schedule
)
2378 isl_schedule_dump(schedule
);
2379 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2380 isl_ast_build_free(build
);
2385 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
2389 return isl_union_map_read_from_str(ctx
, str
);
2392 /* Can "node" be tiled and then mapped to block and thread identifiers?
2393 * That is, is it permutable with at least one coincident dimension?
2395 static int is_permutable(__isl_keep isl_schedule_node
*node
)
2400 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
2402 if (!isl_schedule_node_band_get_permutable(node
))
2404 if (isl_schedule_node_band_n_member(node
) < 1)
2406 if (!isl_schedule_node_band_member_get_coincident(node
, 0))
2412 /* A isl_schedule_foreach_schedule_node_top_down callback
2413 * for setting *any_permutable and aborting the search
2414 * if "node" is a permutable band with coincident dimensions.
2415 * Otherwise, continue searching.
2417 static isl_bool
set_permutable(__isl_keep isl_schedule_node
*node
, void *user
)
2419 int *any_permutable
= user
;
2422 permutable
= is_permutable(node
);
2424 return isl_bool_error
;
2426 return isl_bool_true
;
2428 *any_permutable
= 1;
2430 return isl_bool_error
;
2433 /* Does the subtree rooted at "node" have any suitably permutable band nodes?
2434 * That is, does it have any nodes that are permutable and that
2435 * have a least one coincident dimension?
2437 static int subtree_has_permutable_bands(__isl_keep isl_schedule_node
*node
)
2439 int any_parallelism
= 0;
2441 if (isl_schedule_node_foreach_descendant_top_down(node
, &set_permutable
,
2442 &any_parallelism
) < 0 &&
2446 return any_parallelism
;
2449 /* Does "schedule" contain any permutable band with at least one coincident
2452 static int has_any_permutable_node(__isl_keep isl_schedule
*schedule
)
2454 isl_schedule_node
*root
;
2457 root
= isl_schedule_get_root(schedule
);
2458 any_permutable
= subtree_has_permutable_bands(root
);
2459 isl_schedule_node_free(root
);
2461 return any_permutable
;
2464 /* Is "node" a candidate for mapping to block and thread identifiers?
2465 * In particular, is it permutable with at least one coincident dimension?
2466 * Alternatively, does the subtree rooted at "node" not contain
2467 * any such permutable node? Filter nodes are skipped in this case,
2468 * because a band node will be inserted in front of the returned
2469 * node and this is not possible for filter nodes that are children
2470 * of set or sequence nodes.
2472 static int is_candidate(__isl_keep isl_schedule_node
*node
)
2476 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
)
2478 permutable
= is_permutable(node
);
2479 if (permutable
< 0 || permutable
)
2481 if (isl_schedule_node_get_type(node
) == isl_schedule_node_filter
)
2483 permutable
= subtree_has_permutable_bands(node
);
2489 /* Is "node" the outermost node in its branch that can be tiled
2490 * and then mapped to block and thread identifiers?
2491 * If there are no such nodes in the subtree at "node" and
2492 * if "node" is not a filter node, then it is accepted too.
2494 static int is_outer_tilable(__isl_keep isl_schedule_node
*node
)
2497 isl_schedule_node
*ancestor
;
2499 tilable
= is_candidate(node
);
2506 ancestor
= isl_schedule_node_copy(node
);
2507 while (isl_schedule_node_has_parent(ancestor
)) {
2508 ancestor
= isl_schedule_node_parent(ancestor
);
2510 tilable
= is_candidate(ancestor
);
2511 if (tilable
< 0 || tilable
)
2515 isl_schedule_node_free(ancestor
);
2516 return tilable
< 0 ? -1 : !tilable
;
2519 /* Collect the references to all writes in "group".
2520 * Each reference is represented by a universe set in a space
2524 * with S[i,j] the statement instance space and R[] the array reference.
2526 static __isl_give isl_union_set
*group_tagged_writes(
2527 struct gpu_array_ref_group
*group
)
2531 isl_union_set
*writes
;
2533 space
= isl_map_get_space(group
->access
);
2534 writes
= isl_union_set_empty(space
);
2535 for (i
= 0; i
< group
->n_ref
; ++i
) {
2539 if (!group
->refs
[i
]->write
)
2542 space
= isl_map_get_space(group
->refs
[i
]->tagged_access
);
2543 space
= isl_space_domain(space
);
2544 writes_i
= isl_set_universe(space
);
2545 writes
= isl_union_set_add_set(writes
, writes_i
);
2551 /* Is there any write access in "group" that requires synchronization
2552 * on a write to global memory?
2553 * We currently take into account all writes that would require
2554 * synchronization at the thread level depth, but if the copying
2555 * for this group is performed at an outer level, then we do not
2556 * actually need to take into account dependences at intermediate levels.
2558 static int any_sync_writes_in_group(struct ppcg_kernel
*kernel
,
2559 struct gpu_array_ref_group
*group
)
2561 isl_union_set
*writes
;
2562 int empty
, disjoint
;
2564 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2570 writes
= group_tagged_writes(group
);
2571 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2572 isl_union_set_free(writes
);
2574 return disjoint
< 0 ? -1 : !disjoint
;
2577 /* Collect the references to all writes in "kernel" that write directly
2578 * to global or shared memory, i.e., that are not mapped to private memory.
2579 * Each reference is represented by a universe set in a space
2583 * with S[i,j] the statement instance space and R[] the array reference.
2585 static __isl_give isl_union_set
*collect_non_private_tagged_writes(
2586 struct ppcg_kernel
*kernel
)
2588 isl_union_set
*writes
;
2591 writes
= isl_union_set_empty(isl_union_set_get_space(kernel
->arrays
));
2593 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2594 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
2596 for (j
= 0; j
< array
->n_group
; ++j
) {
2597 struct gpu_array_ref_group
*group
= array
->groups
[j
];
2598 enum ppcg_group_access_type type
;
2599 isl_union_set
*writes_ij
;
2603 type
= gpu_array_ref_group_type(group
);
2604 if (type
== ppcg_access_private
)
2606 writes_ij
= group_tagged_writes(group
);
2607 writes
= isl_union_set_union(writes
, writes_ij
);
2614 /* Are there any direct writes to global memory that require
2617 static int any_global_or_shared_sync_writes(struct ppcg_kernel
*kernel
)
2619 isl_union_set
*writes
;
2620 int empty
, disjoint
;
2622 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2628 writes
= collect_non_private_tagged_writes(kernel
);
2629 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2630 isl_union_set_free(writes
);
2632 return disjoint
< 0 ? -1 : !disjoint
;
2635 /* Construct an isl_multi_val for use as tile sizes for tiling "node"
2636 * from the elements in "tile_size".
2638 static __isl_give isl_multi_val
*construct_band_tiles_sizes(
2639 __isl_keep isl_schedule_node
*node
, int *tile_size
)
2649 ctx
= isl_schedule_node_get_ctx(node
);
2650 space
= isl_schedule_node_band_get_space(node
);
2651 n
= isl_schedule_node_band_n_member(node
);
2652 mv
= isl_multi_val_zero(space
);
2653 for (i
= 0; i
< n
; ++i
) {
2656 v
= isl_val_int_from_si(ctx
, tile_size
[i
]);
2657 mv
= isl_multi_val_set_val(mv
, i
, v
);
2663 /* Replace the partial schedule S of the band node "node" by
2671 * if scale_tile_loops is set, with f the integers in "factor".
2672 * The list that "factor" points to is assumed to contain at least
2673 * as many elements as the number of members in the band.
2675 static __isl_give isl_schedule_node
*snap_band_to_sizes(
2676 __isl_take isl_schedule_node
*node
, int *factor
,
2677 struct ppcg_options
*options
)
2681 mv
= construct_band_tiles_sizes(node
, factor
);
2682 node
= isl_schedule_node_band_scale_down(node
, isl_multi_val_copy(mv
));
2683 if (options
->scale_tile_loops
)
2684 node
= isl_schedule_node_band_scale(node
,
2685 isl_multi_val_copy(mv
));
2686 isl_multi_val_free(mv
);
2691 /* Tile "band" with tile size specified by "sizes".
2693 * Since the tile loops will be mapped to block ids, we forcibly
2694 * turn off tile loop scaling. We may want to enable tile loop scaling
2695 * at some later point, but then we would have to support the detection
2696 * of strides during the mapping to block ids.
2697 * Similarly, since the point loops will be mapped to thread ids,
2698 * we forcibly shift the point loops so that they start at zero.
2700 static __isl_give isl_schedule_node
*tile_band(
2701 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2703 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
2707 scale_tile
= isl_options_get_tile_scale_tile_loops(ctx
);
2708 isl_options_set_tile_scale_tile_loops(ctx
, 0);
2709 shift_point
= isl_options_get_tile_shift_point_loops(ctx
);
2710 isl_options_set_tile_shift_point_loops(ctx
, 1);
2712 node
= isl_schedule_node_band_tile(node
, sizes
);
2714 isl_options_set_tile_scale_tile_loops(ctx
, scale_tile
);
2715 isl_options_set_tile_shift_point_loops(ctx
, shift_point
);
2720 /* Extract the set of parameter values and outer schedule dimensions
2721 * for which any statement instance
2722 * in the kernel inserted at "node" needs to be executed.
2723 * Intersect the set of parameter values derived from the host schedule
2724 * relation with the context of "prog".
2726 static __isl_give isl_set
*extract_context(__isl_keep isl_schedule_node
*node
,
2727 struct gpu_prog
*prog
)
2729 isl_union_map
*schedule
;
2730 isl_union_set
*schedule_domain
;
2734 schedule
= isl_schedule_node_get_prefix_schedule_relation(node
);
2735 schedule_domain
= isl_union_map_range(schedule
);
2736 empty
= isl_union_set_is_empty(schedule_domain
);
2738 isl_union_set_free(schedule_domain
);
2745 space
= isl_union_set_get_space(schedule_domain
);
2746 isl_union_set_free(schedule_domain
);
2747 space
= isl_space_set_from_params(space
);
2748 depth
= isl_schedule_node_get_schedule_depth(node
);
2749 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
2750 context
= isl_set_empty(space
);
2752 context
= isl_set_from_union_set(schedule_domain
);
2754 context
= isl_set_intersect_params(context
,
2755 isl_set_copy(prog
->context
));
2760 /* Return the set of outer array elements accessed by
2761 * by the statement instance in "domain" in "prog".
2763 static __isl_give isl_union_set
*accessed_by_domain(
2764 __isl_take isl_union_set
*domain
, struct gpu_prog
*prog
)
2766 isl_union_map
*access
;
2767 isl_union_set
*arrays
;
2769 access
= isl_union_map_union(isl_union_map_copy(prog
->read
),
2770 isl_union_map_copy(prog
->may_write
));
2771 access
= isl_union_map_intersect_domain(access
, domain
);
2772 arrays
= isl_union_map_range(access
);
2773 arrays
= isl_union_set_apply(arrays
,
2774 isl_union_map_copy(prog
->to_outer
));
2779 /* Return the number of outer band members of the band node "node"
2780 * that are marked coincident.
2782 static int n_outer_coincidence(__isl_keep isl_schedule_node
*node
)
2786 n
= isl_schedule_node_band_n_member(node
);
2788 for (i
= 0; i
< n
; ++i
)
2789 if (!isl_schedule_node_band_member_get_coincident(node
, i
))
2795 /* If the band node "node" has more than "n" members, then split off
2796 * the first "n" of them.
2798 static __isl_give isl_schedule_node
*split_band(
2799 __isl_take isl_schedule_node
*node
, int n
)
2803 dim
= isl_schedule_node_band_n_member(node
);
2805 node
= isl_schedule_node_band_split(node
, n
);
2810 /* Scale a band node that may have been split by split_band.
2811 * "sizes" are the scaling factors for the original node.
2812 * "node" either points to the original band node, or the outer
2813 * of the two pieces after splitting.
2815 * If the number of elements in "node" is smaller than the number of
2816 * elements in "sizes", then some splitting has occurred and we split
2817 * "sizes" in the same way.
2819 static __isl_give isl_schedule_node
*scale_band(
2820 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2824 n
= isl_multi_val_dim(sizes
, isl_dim_set
);
2825 dim
= isl_schedule_node_band_n_member(node
);
2827 isl_multi_val
*sizes2
;
2829 sizes2
= isl_multi_val_copy(sizes
);
2830 sizes
= isl_multi_val_drop_dims(sizes
,
2831 isl_dim_set
, dim
, n
- dim
);
2832 sizes2
= isl_multi_val_drop_dims(sizes2
, isl_dim_set
, 0, dim
);
2833 node
= isl_schedule_node_child(node
, 0);
2834 node
= isl_schedule_node_band_scale(node
, sizes2
);
2835 node
= isl_schedule_node_parent(node
);
2838 return isl_schedule_node_band_scale(node
, sizes
);
2841 /* Return an isl_multi_aff, with as elements the parameters in "space"
2842 * that have the names specified by the elements in "names".
2843 * If (some of) these parameters do not already appear in "space",
2844 * then they are added first.
2846 static __isl_give isl_multi_aff
*parameter_vector(__isl_take isl_space
*space
,
2847 __isl_keep isl_id_list
*names
)
2850 isl_local_space
*ls
;
2854 space
= isl_space_free(space
);
2856 n
= isl_id_list_n_id(names
);
2857 for (i
= 0; i
< n
; ++i
) {
2861 id
= isl_id_list_get_id(names
, i
);
2862 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2867 pos
= isl_space_dim(space
, isl_dim_param
);
2868 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2869 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2871 ma
= isl_multi_aff_zero(isl_space_copy(space
));
2872 ls
= isl_local_space_from_space(isl_space_domain(space
));
2873 for (i
= 0; i
< n
; ++i
) {
2878 id
= isl_id_list_get_id(names
, i
);
2879 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2881 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
2882 isl_dim_param
, pos
);
2883 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2885 isl_local_space_free(ls
);
2890 /* Return constraints on the domain elements that equate a sequence of
2891 * parameters called "names", to the partial schedule
2892 * of "node" modulo the integers in "size".
2893 * The number of elements in the array "size" should be equal
2894 * to the number of elements in "names".
2895 * The number of members of the band node "node" should be smaller
2896 * than or equal to this number. If it is smaller, then the first
2897 * elements of "names" are equated to zero.
2899 static __isl_give isl_union_set
*set_schedule_modulo(
2900 __isl_keep isl_schedule_node
*node
, __isl_keep isl_id_list
*names
,
2906 isl_multi_union_pw_aff
*mupa
, *mupa2
;
2908 isl_union_set
*domain
;
2912 n
= isl_id_list_n_id(names
);
2914 return isl_schedule_node_get_universe_domain(node
);
2915 n_zero
= n
- isl_schedule_node_band_n_member(node
);
2917 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
2918 mv
= construct_band_tiles_sizes(node
, size
+ n_zero
);
2919 mupa
= isl_multi_union_pw_aff_mod_multi_val(mupa
, mv
);
2921 space
= isl_multi_union_pw_aff_get_space(mupa
);
2922 space
= isl_space_params(space
);
2923 space
= isl_space_set_from_params(space
);
2924 space
= isl_space_add_dims(space
, isl_dim_set
, n_zero
);
2925 ma
= isl_multi_aff_zero(space
);
2927 domain
= isl_schedule_node_get_universe_domain(node
);
2928 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(
2929 isl_union_set_copy(domain
), ma
);
2930 mupa
= isl_multi_union_pw_aff_range_product(mupa2
, mupa
);
2932 space
= isl_multi_union_pw_aff_get_space(mupa
);
2933 ma
= parameter_vector(space
, names
);
2935 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(domain
, ma
);
2936 mupa
= isl_multi_union_pw_aff_sub(mupa
, mupa2
);
2938 return isl_multi_union_pw_aff_zero_union_set(mupa
);
2941 /* Insert a context node at "node" introducing the block and thread
2942 * identifiers along with their bounds, which are stored in kernel->grid_size
2943 * and kernel->block_dim.
2944 * Note that the bounds on the block identifiers may implicitly impose
2945 * constraints on the parameters. A guard needs to be inserted
2946 * in the schedule tree to ensure that those bounds hold at "node".
2947 * This guard is inserted in insert_guard.
2949 static __isl_give isl_schedule_node
*insert_context(struct ppcg_kernel
*kernel
,
2950 __isl_take isl_schedule_node
*node
)
2954 context
= isl_set_universe(isl_set_get_space(kernel
->context
));
2956 context
= add_bounded_parameters_dynamic(context
,
2957 kernel
->grid_size
, kernel
->block_ids
);
2958 context
= add_bounded_parameters(context
,
2959 kernel
->block_dim
, kernel
->thread_ids
);
2961 node
= isl_schedule_node_insert_context(node
, context
);
2966 /* Insert a guard that eliminates kernel launches where the kernel
2967 * obviously does not have any work to do.
2969 * In particular, eliminate kernel launches where there are obviously
2971 * Use the same block size constraints that are used to create the context
2972 * to ensure that all constraints implicit in the constructed context
2973 * are imposed by the guard.
2975 * Additionally, add other constraints that are valid
2976 * for each executed instance ("context"), as long as this does not result
2979 static __isl_give isl_schedule_node
*insert_guard(
2980 __isl_take isl_schedule_node
*node
, __isl_keep isl_set
*context
,
2981 __isl_keep isl_multi_pw_aff
*size
, struct ppcg_scop
*scop
)
2987 guard
= isl_set_copy(context
);
2988 guard
= isl_set_compute_divs(guard
);
2989 guard
= isl_set_from_basic_set(isl_set_simple_hull(guard
));
2991 nparam
= isl_set_dim(guard
, isl_dim_param
);
2992 n
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
2993 ids
= ppcg_scop_generate_names(scop
, n
, "__ppcg_tmp");
2994 guard
= add_bounded_parameters_dynamic(guard
, size
, ids
);
2995 isl_id_list_free(ids
);
2996 guard
= isl_set_project_out(guard
, isl_dim_param
, nparam
, n
);
2998 node
= isl_schedule_node_insert_guard(node
, guard
);
3003 /* Does any array reference group mapping require the band that is mapped
3004 * to threads to be unrolled?
3006 static int kernel_requires_unroll(struct ppcg_kernel
*kernel
)
3010 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3011 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3013 for (j
= 0; j
< array
->n_group
; ++j
) {
3014 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3015 if (gpu_array_ref_group_requires_unroll(group
))
3023 /* Mark the given band node "node" for unrolling by the AST generator and
3024 * then sink it to the leaves of the schedule tree.
3025 * All dimensions of "node" are assumed to be coincident, such that this
3026 * sinking is a valid operation.
3028 static __isl_give isl_schedule_node
*unroll(__isl_take isl_schedule_node
*node
)
3030 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
3032 node
= isl_schedule_node_band_sink(node
);
3037 /* Insert a synchronization node in the schedule tree of "node"
3038 * after the core computation of "kernel" at the level of the band
3039 * that is mapped to threads, except if that level is equal to
3040 * that of the band that is mapped to blocks or if there are no writes
3041 * to global or shared memory in the core computation that require
3043 * If there are any writes to shared memory and the shared memory
3044 * copying is performed at the same level, then synchronization
3045 * is needed between the core and the copying anyway, so we might
3046 * as well add it here. If the copying is performed at a higher
3047 * level, then different iterations of intermediate schedule dimensions
3048 * may have a different mapping from between shared memory elements and
3049 * threads, such that synchronization is required after the core.
3050 * "node" is assumed to point to the kernel node.
3052 static __isl_give isl_schedule_node
*add_sync(struct ppcg_kernel
*kernel
,
3053 __isl_take isl_schedule_node
*node
)
3058 need_sync
= any_global_or_shared_sync_writes(kernel
);
3060 return isl_schedule_node_free(node
);
3064 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3066 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3067 if (kernel_depth
== isl_schedule_node_get_schedule_depth(node
))
3068 return gpu_tree_move_up_to_kernel(node
);
3070 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3072 node
= gpu_tree_move_up_to_kernel(node
);
3077 /* Return a read ("read" is 1) or write access relation for "group"
3078 * with those accesses removed that are only needed to communicate data
3079 * within the subtree of the schedule rooted at "node".
3080 * Furthermore, include the prefix schedule at "node".
3081 * That is, return a relation of the form
3085 * with D the outer schedule dimensions at "node".
3087 static __isl_give isl_union_map
*anchored_non_local_accesses(
3088 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3089 __isl_take isl_schedule_node
*node
, int read
)
3091 isl_union_map
*access
;
3092 isl_union_map
*prefix
;
3094 access
= gpu_array_ref_group_access_relation(group
, read
, !read
);
3095 access
= remove_local_accesses_group(kernel
, group
, access
, node
, read
);
3096 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
3097 access
= isl_union_map_range_product(prefix
, access
);
3102 /* Given an array reference group "group", create a mapping
3104 * read[D -> A] -> [D -> A]
3106 * if "read" is set or
3108 * write[D -> A] -> [D -> A]
3110 * if "read" is not set.
3111 * D corresponds to the outer tile->depth dimensions of
3112 * the kernel schedule.
3114 static __isl_give isl_multi_aff
*create_from_access(isl_ctx
*ctx
,
3115 struct gpu_array_ref_group
*group
, int read
)
3117 struct gpu_array_tile
*tile
;
3121 tile
= gpu_array_ref_group_tile(group
);
3122 space
= isl_space_copy(group
->array
->space
);
3123 space
= isl_space_from_range(space
);
3124 space
= isl_space_add_dims(space
, isl_dim_in
, tile
->depth
);
3125 space
= isl_space_wrap(space
);
3126 space
= isl_space_map_from_set(space
);
3128 id
= isl_id_alloc(ctx
, read
? "read" : "write", group
);
3129 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
3131 return isl_multi_aff_identity(space
);
3134 /* If any writes in "group" require synchronization, then make sure
3135 * that there is a synchronization node for "kernel" after the node
3136 * following "node" in a sequence.
3138 * If "shared" is set and no synchronization is needed for
3139 * the writes to global memory, then add synchronization before
3140 * the kernel to protect shared memory from being overwritten
3141 * by the next iteration of the core computation.
3142 * No additional synchronization is needed to protect against
3143 * the next copy into shared memory because each element of
3144 * the shared memory tile is always copied by the same thread.
3146 static __isl_give isl_schedule_node
*add_group_write_sync(
3147 __isl_take isl_schedule_node
*node
, struct ppcg_kernel
*kernel
,
3148 struct gpu_array_ref_group
*group
, int shared
)
3152 need_sync
= any_sync_writes_in_group(kernel
, group
);
3154 return isl_schedule_node_free(node
);
3156 node
= isl_schedule_node_parent(node
);
3157 node
= isl_schedule_node_next_sibling(node
);
3158 node
= isl_schedule_node_child(node
, 0);
3159 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3160 } else if (shared
) {
3161 struct gpu_array_tile
*tile
;
3163 tile
= gpu_array_ref_group_tile(group
);
3164 node
= isl_schedule_node_parent(node
);
3165 node
= isl_schedule_node_parent(node
);
3166 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
,
3168 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3174 /* Add copy statements to the schedule tree of "node"
3175 * for reading from global memory to private memory (if "read" is set) or
3176 * for writing back from private memory to global memory
3177 * (if "read" is not set) for the array reference group "group" that
3178 * is mapped to private memory.
3179 * On input, "node" points to the kernel node, and it is moved
3180 * back there on output.
3182 * The copies are performed in the order of the array elements.
3183 * The copy statement instances include a reference to the outer
3184 * tile->depth dimensions of the kernel schedule for ease of
3185 * combining them with the group tiling.
3187 * That is, the extra schedule is of the form
3191 * where D corresponds to the outer tile->depth dimensions of
3192 * the kernel schedule and A to the global array.
3193 * This schedule is unrolled because registers are not addressable.
3195 * The copying is inserted in the schedule tree through an extension
3200 * where the extra domain elements type[D -> A] are those accessed
3202 * A filter is inserted on type[D -> A] to ensure that the element
3203 * is read/written by the same thread that needs the element.
3204 * This filter is obtained by applying
3208 * to the thread filter for the core statements.
3210 * The extension is inserted before the core computation in case of a read
3211 * and after the core computation in case of a write.
3212 * In the latter case, we also make sure that there is a synchronization
3213 * node after the write to global memory, unless this write is performed
3214 * at the outer level of the kernel.
3215 * In principle, this synchronization could be inserted higher
3216 * in the schedule tree depending on where the corresponding reads
3217 * from global memory are performed.
3219 static __isl_give isl_schedule_node
*add_copies_group_private(
3220 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3221 __isl_take isl_schedule_node
*node
, int read
)
3223 struct gpu_array_tile
*tile
;
3224 isl_union_map
*access
;
3225 isl_union_set
*domain
;
3227 isl_multi_aff
*from_access
;
3228 isl_multi_pw_aff
*mpa
;
3229 isl_multi_union_pw_aff
*mupa
;
3230 isl_schedule_node
*graft
;
3231 isl_union_set
*filter
;
3235 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3236 tile
= gpu_array_ref_group_tile(group
);
3237 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3239 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3240 empty
= isl_union_map_is_empty(access
);
3241 if (empty
< 0 || empty
) {
3242 isl_union_map_free(access
);
3244 return isl_schedule_node_free(node
);
3245 return gpu_tree_move_up_to_kernel(node
);
3248 group
->array
->global
= 1;
3249 group
->local_array
->global
= 1;
3251 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3252 space
= isl_space_domain(isl_multi_aff_get_space(from_access
));
3253 access
= isl_union_map_preimage_range_multi_aff(access
, from_access
);
3255 filter
= isl_union_set_copy(kernel
->thread_filter
);
3256 filter
= isl_union_set_apply(filter
, isl_union_map_copy(access
));
3257 filter
= isl_union_set_detect_equalities(filter
);
3258 filter
= isl_union_set_coalesce(filter
);
3260 domain
= isl_union_map_range(access
);
3261 access
= isl_union_set_wrapped_domain_map(domain
);
3262 access
= isl_union_map_reverse(access
);
3263 access
= isl_union_map_coalesce(access
);
3264 graft
= isl_schedule_node_from_extension(access
);
3266 space
= isl_space_map_from_set(space
);
3267 mpa
= isl_multi_pw_aff_identity(space
);
3268 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
3269 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3271 graft
= isl_schedule_node_child(graft
, 0);
3272 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3273 graft
= unroll(graft
);
3275 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3277 graft
= isl_schedule_node_parent(graft
);
3280 node
= isl_schedule_node_graft_before(node
, graft
);
3282 node
= isl_schedule_node_graft_after(node
, graft
);
3283 if (kernel_depth
< tile
->depth
)
3284 node
= add_group_write_sync(node
, kernel
, group
, 0);
3287 node
= gpu_tree_move_up_to_kernel(node
);
3292 /* Add copy statements to the schedule tree of "node"
3293 * for reading from global memory to shared memory (if "read" is set) or
3294 * for writing back from shared memory to global memory
3295 * (if "read" is not set) for the array reference group "group" that
3296 * is mapped to shared memory.
3297 * On input, "node" points to the kernel node, and it is moved
3298 * back there on output.
3300 * The copies are performed in the order of the corresponding shared
3302 * The copy statement instances include a reference to the outer
3303 * tile->depth dimensions of the kernel schedule for ease of
3304 * combining them with the group tiling.
3306 * If we are performing a read from global memory to shared memory and
3307 * if the array involved is not a scalar, then we copy
3308 * the entire tile to shared memory. This may result in some extra
3309 * elements getting copied, but it should lead to simpler code
3310 * (which means that fewer registers may be needed) and less divergence.
3312 * Otherwise, we only copy the elements that will be read or have been written
3315 * That is, the extra schedule is of the form
3319 * where D corresponds to the outer tile->depth dimensions of
3320 * the kernel schedule, A to the global array and T is the corresponding
3321 * shared memory tile.
3323 * The copying is inserted in the schedule tree through an extension
3328 * where the extra domain elements type[D -> A] are those accessed
3329 * by the group. In the case of read from a non-scalar, this set
3330 * is replaced by the entire shared memory tile.
3332 * A filter is inserted on type[D -> A] to map the copy instances
3333 * to the threads. In particular, the thread identifiers are
3334 * equated to the position inside the shared memory tile (T)
3335 * modulo the block size.
3336 * We try to align the innermost tile dimension with the innermost
3337 * thread identifier (x) as a heuristic to improve coalescing.
3338 * In particular, if the dimension of the tile is greater than
3339 * the dimension of the block, then the schedule mapping to the tile
3340 * is broken up into two pieces and the filter is applied to the inner part.
3341 * If, on the other hand, the dimension of the tile is smaller than
3342 * the dimension of the block, then the initial thread identifiers
3343 * are equated to zero and the remaining thread identifiers are
3344 * matched to the memory tile.
3346 * The extension is inserted before the core computation in case of a read
3347 * and after the core computation in case of a write.
3348 * In the case of a read, we first need to make sure there is some
3349 * synchronization before the core computation such that we can put the read
3350 * from global memory to shared memory before that synchronization.
3351 * This ensures that all threads have finished copying into shared memory
3352 * before the shared memory is used.
3353 * We also need to make sure that there is a synchronization node after
3354 * the core computation to ensure that the next load into shared memory
3355 * only happens after all data has been used. There is no need for
3356 * this synchronization if we are at the outer level since then there
3357 * won't be a next load.
3358 * In the case of a write, we need to make sure there is some synchronization
3359 * after the core computation such taht we can put the write from shared
3360 * memory to global memory after that synchronization.
3361 * Unless we are at the outer level, we also need a synchronization node
3362 * after the write to ensure the data is saved to global memory
3363 * before the next iteration write to the same shared memory.
3364 * It also makes sure the data has arrived in global memory before
3365 * it is read in a subsequent iteration.
3367 static __isl_give isl_schedule_node
*add_copies_group_shared(
3368 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3369 __isl_take isl_schedule_node
*node
, int read
)
3371 struct gpu_array_tile
*tile
;
3372 isl_union_map
*access
;
3373 isl_union_set
*domain
;
3375 isl_multi_aff
*from_access
;
3376 isl_multi_pw_aff
*mpa
;
3377 isl_multi_union_pw_aff
*mupa
;
3378 isl_schedule_node
*graft
;
3379 isl_union_set
*filter
;
3384 tile
= gpu_array_ref_group_tile(group
);
3385 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3386 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3388 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3389 empty
= isl_union_map_is_empty(access
);
3390 if (empty
< 0 || empty
) {
3391 isl_union_map_free(access
);
3393 return isl_schedule_node_free(node
);
3394 return gpu_tree_move_up_to_kernel(node
);
3397 group
->array
->global
= 1;
3398 group
->local_array
->global
= 1;
3400 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3402 ma
= isl_multi_aff_copy(tile
->tiling
);
3403 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3404 isl_multi_aff_copy(from_access
));
3405 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3406 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3408 domain
= isl_union_map_range(access
);
3410 if (read
&& !gpu_array_is_scalar(group
->array
)) {
3412 isl_union_set_free(domain
);
3413 map
= group_tile(group
);
3414 domain
= isl_union_set_from_set(isl_map_wrap(map
));
3417 domain
= isl_union_set_preimage_multi_aff(domain
, from_access
);
3418 access
= isl_union_set_wrapped_domain_map(domain
);
3419 access
= isl_union_map_reverse(access
);
3420 access
= isl_union_map_coalesce(access
);
3421 graft
= isl_schedule_node_from_extension(access
);
3423 graft
= isl_schedule_node_child(graft
, 0);
3425 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3427 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0) {
3428 graft
= isl_schedule_node_band_split(graft
,
3429 tile
->n
- kernel
->n_block
);
3430 graft
= isl_schedule_node_child(graft
, 0);
3432 if (tile
->n
< kernel
->n_block
)
3433 skip
= kernel
->n_block
- tile
->n
;
3436 filter
= set_schedule_modulo(graft
, kernel
->thread_ids
,
3438 if (!kernel
->options
->wrap
)
3439 graft
= snap_band_to_sizes(graft
, kernel
->block_dim
+ skip
,
3441 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0)
3442 graft
= isl_schedule_node_parent(graft
);
3443 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3445 while (graft
&& isl_schedule_node_has_parent(graft
))
3446 graft
= isl_schedule_node_parent(graft
);
3449 if (kernel_depth
< tile
->depth
)
3450 node
= gpu_tree_ensure_sync_after_core(node
, kernel
);
3451 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3452 node
= isl_schedule_node_graft_before(node
, graft
);
3454 node
= gpu_tree_move_right_to_sync(node
, kernel
);
3455 node
= isl_schedule_node_graft_after(node
, graft
);
3456 if (kernel_depth
< tile
->depth
)
3457 node
= add_group_write_sync(node
, kernel
, group
, 1);
3460 node
= gpu_tree_move_up_to_kernel(node
);
3465 /* Check whether the array reference group "group" is mapped to
3466 * private or shared memory and, if so,
3467 * add copy statements to the schedule tree of "node"
3468 * for reading from global memory to private or shared memory
3469 * (if "read" is set) or for writing back from private or shared memory
3470 * to global memory (if "read" is not set) for this group.
3471 * On input, "node" points to the kernel node, and it is moved
3472 * back there on output.
3474 static __isl_give isl_schedule_node
*add_copies_group(
3475 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3476 __isl_take isl_schedule_node
*node
, int read
)
3478 enum ppcg_group_access_type type
;
3480 type
= gpu_array_ref_group_type(group
);
3481 if (type
== ppcg_access_private
)
3482 return add_copies_group_private(kernel
, group
, node
, read
);
3483 if (type
== ppcg_access_shared
)
3484 return add_copies_group_shared(kernel
, group
, node
, read
);
3488 /* For each array reference group that is mapped to private or shared memory,
3489 * add copy statements to the schedule tree of "node"
3490 * for reading from global memory to private or shared memory
3491 * and for writing back.
3492 * On input, "node" points to the kernel node, and it is moved
3493 * back there on output.
3495 static __isl_give isl_schedule_node
*add_copies(struct ppcg_kernel
*kernel
,
3496 __isl_take isl_schedule_node
*node
)
3500 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3501 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3503 for (j
= 0; j
< array
->n_group
; ++j
) {
3504 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3506 node
= add_copies_group(kernel
, group
, node
, 1);
3509 node
= add_copies_group(kernel
, group
, node
, 0);
3518 /* Mark all dimensions in the current band node atomic.
3520 static __isl_give isl_schedule_node
*atomic(__isl_take isl_schedule_node
*node
)
3522 return ppcg_set_schedule_node_type(node
, isl_ast_loop_atomic
);
3525 /* Mark "node" atomic, if it is a band node.
3526 * Do the same for all ancestors.
3527 * Return a pointer to "node" (in the updated schedule tree).
3529 static __isl_give isl_schedule_node
*atomic_ancestors(
3530 __isl_take isl_schedule_node
*node
)
3536 if (!isl_schedule_node_has_parent(node
))
3539 pos
= isl_schedule_node_get_child_position(node
);
3540 node
= isl_schedule_node_parent(node
);
3541 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
)
3542 node
= atomic(node
);
3543 node
= atomic_ancestors(node
);
3544 node
= isl_schedule_node_child(node
, pos
);
3549 /* Collect all write references that require synchronization.
3550 * "node" is assumed to point to the kernel node.
3551 * Each reference is represented by a universe set in a space
3555 * with S[i,j] the statement instance space and R[] the array reference.
3557 * This function should be called before block and thread filters are added.
3559 * Synchronization is needed after a write if there is a subsequent read
3560 * within the same block that may not be performed by the same thread.
3561 * There should not be any dependences between different blocks,
3562 * so we start with the flow dependences within the same kernel invocation
3563 * and we subtract from these those dependences that are mapped
3564 * to the same iteration of the bands where synchronization is inserted.
3565 * We do not remove pairs of instances that are known to map to
3566 * the same thread across different iterations of the intermediate
3567 * bands because the read may be performed by a different thread
3568 * than the one that needs the value if shared memory is involved.
3570 * We also consider all pairs of possible writes that access the same
3571 * memory location and that may be mapped to the same block but not
3572 * to the same iteration of the intermediate bands.
3573 * In theory, it would be possible for one thread to still be in
3574 * a previous iteration of a loop in these bands.
3575 * A write to global memory in this delayed thread could then overwrite
3576 * a write from another thread that has already moved on to
3577 * the next iteration.
3579 * After computing the above writes paired off with reads or writes
3580 * that depend on them, we project onto the domain writes.
3581 * Sychronization is needed after writes to global memory
3582 * through these references.
3584 static __isl_give isl_union_set
*compute_sync_writes(
3585 struct ppcg_kernel
*kernel
, __isl_keep isl_schedule_node
*node
)
3587 isl_union_map
*local
;
3588 isl_union_map
*may_writes
, *shared_access
;
3589 isl_union_map
*kernel_prefix
, *thread_prefix
;
3590 isl_union_map
*equal
;
3591 isl_union_set
*wrap
;
3592 isl_union_set
*domain
;
3594 domain
= isl_schedule_node_get_universe_domain(node
);
3595 kernel_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3596 node
= isl_schedule_node_copy(node
);
3597 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3598 thread_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3599 isl_schedule_node_free(node
);
3601 may_writes
= isl_union_map_copy(kernel
->prog
->scop
->tagged_may_writes
);
3602 may_writes
= isl_union_map_curry(may_writes
);
3603 may_writes
= isl_union_map_intersect_domain(may_writes
, domain
);
3604 may_writes
= isl_union_map_uncurry(may_writes
);
3605 shared_access
= isl_union_map_copy(may_writes
);
3606 shared_access
= isl_union_map_apply_range(shared_access
,
3607 isl_union_map_reverse(may_writes
));
3609 local
= isl_union_map_copy(kernel
->prog
->scop
->tagged_dep_flow
);
3610 local
= isl_union_map_union(local
, shared_access
);
3611 local
= isl_union_map_zip(local
);
3613 equal
= isl_union_map_apply_range(kernel_prefix
,
3614 isl_union_map_reverse(isl_union_map_copy(kernel_prefix
)));
3615 wrap
= isl_union_map_wrap(equal
);
3616 local
= isl_union_map_intersect_domain(local
, wrap
);
3617 equal
= isl_union_map_apply_range(thread_prefix
,
3618 isl_union_map_reverse(isl_union_map_copy(thread_prefix
)));
3619 wrap
= isl_union_map_wrap(equal
);
3620 local
= isl_union_map_subtract_domain(local
, wrap
);
3622 local
= isl_union_map_zip(local
);
3623 local
= isl_union_map_universe(local
);
3625 return isl_union_map_domain(local
);
3628 /* Group the domain elements into a single space, named kernelX,
3629 * with X the kernel sequence number "kernel_id".
3631 static __isl_give isl_schedule_node
*group_statements(
3632 __isl_take isl_schedule_node
*node
, int kernel_id
)
3640 snprintf(buffer
, sizeof(buffer
), "kernel%d", kernel_id
);
3641 id
= isl_id_alloc(isl_schedule_node_get_ctx(node
), buffer
, NULL
);
3642 return isl_schedule_node_group(node
, id
);
3645 /* Create a ppcg_kernel representing the domain instances that reach "node"
3646 * and insert a mark node pointing to the ppcg_kernel before "node".
3647 * The band that "node" points to is the band that needs to be mapped
3648 * to block identifiers. The band that needs to be mapped to thread
3649 * identifiers should be marked by a "thread" mark by the caller.
3650 * This mark is removed by this function.
3651 * If "scale" is set, then the band that "node" points to is scaled
3654 * Mark all outer band nodes as atomic to ensure each kernel is only
3656 * If the domain elements that reach "node" live in more than one space,
3657 * then group the domain elements into a single space, named kernelX,
3658 * with X the kernel sequence number.
3660 * Insert a guard node governing the kernel node to ensure that
3661 * no kernels with zero blocks are launched.
3663 * Insert a context node describing the block and thread
3664 * identifiers inside the kernel mark.
3665 * The context node needs to be inserted after the effective block size
3666 * has been determined such that the bounds on the thread identifiers
3667 * would reflect the effective block size.
3668 * Insert a filter node inside the context node mapping the statement
3669 * instances to block identifiers. In particular, the block identifiers
3670 * are equated to the partial schedule of band that was marked for mapping
3671 * to blocks modulo the grid size.
3672 * Insert a filter node inside the "thread" mark mapping the statement
3673 * instances to thread identifiers. In particular, the thread identifiers
3674 * are equated to the partial schedule of band that was marked for mapping
3675 * to threads modulo the block size.
3677 * Compute array reference groups for all arrays, set the local
3678 * array bounds based on the set of domain instances that reach
3679 * the kernel node, check the total amount of shared memory used
3680 * and compute all group tilings.
3681 * The array reference groups are computed after the block filter
3682 * has been inserted because it affects the mapping to shared or
3683 * private memory. This computation also requires the thread filter
3684 * (in the ppcg_kernel object), but this thread filter should not
3685 * have been added to the schedule tree yet since the computation
3686 * requires the schedule of the band that needs to be mapped to
3687 * threads before the privatization is applied.
3689 * If any array reference group requires the band mapped to threads
3690 * to be unrolled, then we perform the required unrolling.
3692 * We save a copy of the schedule that may influence the mappings
3693 * to shared or private memory in kernel->copy_schedule.
3695 * Finally, we add synchronization and copy statements to the schedule tree,
3696 * remove the "thread" mark and create representations for the local
3697 * variables in the kernel.
3699 * We keep a copy of the isl_id that points to the kernel to ensure
3700 * that the kernel does not get destroyed if the schedule node
3701 * is freed due to some error condition.
3703 static __isl_give isl_schedule_node
*create_kernel(struct gpu_gen
*gen
,
3704 __isl_take isl_schedule_node
*node
, int scale
,
3705 __isl_keep isl_multi_val
*sizes
)
3707 struct ppcg_kernel
*kernel
;
3709 isl_schedule_node
*node_thread
;
3710 isl_union_map
*host_schedule
;
3711 isl_set
*host_domain
;
3712 isl_union_set
*domain
;
3713 int single_statement
;
3715 kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
3716 kernel
= ppcg_kernel_create_local_arrays(kernel
, gen
->prog
);
3718 return isl_schedule_node_free(node
);
3720 domain
= isl_schedule_node_get_domain(node
);
3721 single_statement
= isl_union_set_n_set(domain
) == 1;
3723 kernel
->ctx
= gen
->ctx
;
3724 kernel
->prog
= gen
->prog
;
3725 kernel
->options
= gen
->options
;
3726 kernel
->context
= extract_context(node
, gen
->prog
);
3727 kernel
->core
= isl_union_set_universe(isl_union_set_copy(domain
));
3728 kernel
->arrays
= accessed_by_domain(isl_union_set_copy(domain
),
3730 kernel
->n_grid
= n_outer_coincidence(node
);
3731 node_thread
= isl_schedule_node_copy(node
);
3732 node_thread
= gpu_tree_move_down_to_thread(node_thread
, kernel
->core
);
3733 node_thread
= isl_schedule_node_child(node_thread
, 0);
3734 kernel
->n_block
= n_outer_coincidence(node_thread
);
3735 isl_schedule_node_free(node_thread
);
3736 kernel
->id
= gen
->kernel_id
++;
3737 read_grid_and_block_sizes(kernel
, gen
);
3739 kernel
->sync_writes
= compute_sync_writes(kernel
, node
);
3741 host_schedule
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3742 host_domain
= isl_set_from_union_set(isl_union_map_range(
3745 node
= atomic_ancestors(node
);
3747 id
= isl_id_alloc(gen
->ctx
, "kernel", kernel
);
3748 id
= isl_id_set_free_user(id
, &ppcg_kernel_free_wrap
);
3749 node
= isl_schedule_node_insert_mark(node
, isl_id_copy(id
));
3751 if (!single_statement
)
3752 node
= group_statements(node
, kernel
->id
);
3754 node
= isl_schedule_node_child(node
, 0);
3755 node
= split_band(node
, kernel
->n_grid
);
3756 kernel
->block_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3757 kernel
->n_grid
, "b");
3758 kernel
->block_filter
= set_schedule_modulo(node
, kernel
->block_ids
,
3760 kernel
->grid_size
= extract_grid_size(kernel
,
3761 isl_union_set_copy(domain
));
3762 if (!kernel
->options
->wrap
)
3763 node
= snap_band_to_sizes(node
, kernel
->grid_dim
,
3766 node
= scale_band(node
, isl_multi_val_copy(sizes
));
3767 node
= isl_schedule_node_parent(node
);
3768 if (!single_statement
)
3769 node
= isl_schedule_node_parent(node
);
3770 node
= insert_guard(node
, kernel
->context
, kernel
->grid_size
,
3772 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3773 node
= isl_schedule_node_child(node
, 0);
3774 node
= split_band(node
, kernel
->n_block
);
3775 kernel
->thread_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3776 kernel
->n_block
, "t");
3777 kernel
->thread_filter
= set_schedule_modulo(node
, kernel
->thread_ids
,
3779 if (extract_block_size(kernel
, domain
) < 0)
3780 node
= isl_schedule_node_free(node
);
3782 node
= gpu_tree_move_up_to_kernel(node
);
3783 node
= isl_schedule_node_child(node
, 0);
3784 node
= insert_context(kernel
, node
);
3785 node
= isl_schedule_node_child(node
, 0);
3786 node
= isl_schedule_node_insert_filter(node
,
3787 isl_union_set_copy(kernel
->block_filter
));
3789 node
= gpu_tree_move_up_to_kernel(node
);
3791 if (gpu_group_references(kernel
, node
) < 0)
3792 node
= isl_schedule_node_free(node
);
3793 localize_bounds(kernel
, host_domain
);
3794 isl_set_free(host_domain
);
3796 check_shared_memory_bound(kernel
);
3797 mark_global_arrays(kernel
);
3798 compute_group_tilings(kernel
);
3800 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3801 node
= isl_schedule_node_child(node
, 0);
3802 if (!kernel
->options
->wrap
)
3803 node
= snap_band_to_sizes(node
, kernel
->block_dim
,
3805 node
= isl_schedule_node_insert_filter(node
,
3806 isl_union_set_copy(kernel
->thread_filter
));
3807 if (kernel_requires_unroll(kernel
)) {
3808 node
= isl_schedule_node_child(node
, 0);
3809 node
= unroll(node
);
3812 node
= gpu_tree_move_up_to_thread(node
);
3813 kernel
->copy_schedule_dim
= isl_schedule_node_get_schedule_depth(node
);
3814 kernel
->copy_schedule
=
3815 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
3817 node
= gpu_tree_move_up_to_kernel(node
);
3819 node
= add_sync(kernel
, node
);
3820 node
= add_copies(kernel
, node
);
3822 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3823 node
= isl_schedule_node_delete(node
);
3825 node
= gpu_tree_move_up_to_kernel(node
);
3827 if (create_kernel_vars(kernel
) < 0)
3828 node
= isl_schedule_node_free(node
);
3830 if (!single_statement
)
3831 node
= isl_schedule_node_parent(node
);
3832 node
= isl_schedule_node_parent(node
);
3838 /* Insert a zero-dimensional permutable band at "node".
3840 static __isl_give isl_schedule_node
*insert_empty_permutable_band(
3841 __isl_take isl_schedule_node
*node
)
3844 isl_schedule
*schedule
;
3845 isl_union_set
*domain
;
3846 isl_multi_union_pw_aff
*mupa
;
3848 schedule
= isl_schedule_node_get_schedule(node
);
3849 domain
= isl_schedule_get_domain(schedule
);
3850 space
= isl_union_set_get_space(domain
);
3851 isl_union_set_free(domain
);
3852 isl_schedule_free(schedule
);
3854 space
= isl_space_set_from_params(space
);
3855 mupa
= isl_multi_union_pw_aff_zero(space
);
3856 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3857 node
= isl_schedule_node_band_set_permutable(node
, 1);
3862 /* If "node" is the outermost permutable band that can be mapped to block and
3863 * thread identifiers in its branch (or the root of a subtree with
3864 * no such outer bands),
3865 * then mark the band as such, attaching a ppcg_kernel to the mark.
3867 * If "node" is the root of a subtree without permutable bands,
3868 * then insert a zero-dimensional permutable band such that
3869 * we can assume that "node" always points to a band node.
3870 * This includes the case where "node" already points to a band node,
3871 * but one without any coincident dimension. In this case,
3872 * the extra node ensures that this original node does not get tiled.
3874 * Tile "node" using user specified tile sizes, after splitting the band
3875 * if the number of specified tile sizes is smaller than the dimension
3876 * of the band. Mark the point band of this tiling as the band that
3877 * needs to be mapped to threads.
3878 * Create a kernel representing the domain instances that reach "node" and
3879 * insert a mark node pointing to the ppcg_kernel before the band node.
3881 static __isl_give isl_schedule_node
*mark_outer_permutable(
3882 __isl_take isl_schedule_node
*node
, void *user
)
3884 struct gpu_gen
*gen
= user
;
3890 isl_multi_val
*sizes
;
3892 outer
= is_outer_tilable(node
);
3894 return isl_schedule_node_free(node
);
3898 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
||
3899 !isl_schedule_node_band_member_get_coincident(node
, 0))
3900 node
= insert_empty_permutable_band(node
);
3902 tile_len
= isl_schedule_node_band_n_member(node
);
3903 tile_size
= read_tile_sizes(gen
, &tile_len
);
3905 return isl_schedule_node_free(node
);
3906 if (tile_len
< isl_schedule_node_band_n_member(node
))
3907 node
= isl_schedule_node_band_split(node
, tile_len
);
3908 sizes
= construct_band_tiles_sizes(node
, tile_size
);
3909 node
= tile_band(node
, isl_multi_val_copy(sizes
));
3910 node
= isl_schedule_node_child(node
, 0);
3911 id
= isl_id_alloc(gen
->ctx
, "thread", NULL
);
3912 node
= isl_schedule_node_insert_mark(node
, id
);
3913 node
= isl_schedule_node_parent(node
);
3915 scale
= gen
->options
->scale_tile_loops
;
3916 node
= create_kernel(gen
, node
, scale
, sizes
);
3917 isl_multi_val_free(sizes
);
3923 /* Given a set or sequence node, return the union the filters of either all
3924 * (if "only_initial" is not set) or the initial (if "only_initial" is set)
3925 * direct subtrees that do not contain any suitably permutable bands
3926 * (according to subtree_has_permutable_bands).
3928 static __isl_give isl_union_set
*get_non_parallel_subtree_filters(
3929 __isl_keep isl_schedule_node
*node
, int only_initial
)
3932 isl_union_set
*filter
;
3935 n
= isl_schedule_node_n_children(node
);
3939 node
= isl_schedule_node_copy(node
);
3940 node
= isl_schedule_node_child(node
, 0);
3941 filter
= isl_schedule_node_filter_get_filter(node
);
3942 node
= isl_schedule_node_parent(node
);
3943 space
= isl_union_set_get_space(filter
);
3944 isl_union_set_free(filter
);
3945 filter
= isl_union_set_empty(space
);
3947 for (i
= 0; i
< n
; ++i
) {
3950 node
= isl_schedule_node_child(node
, i
);
3951 parallelism
= subtree_has_permutable_bands(node
);
3952 if (parallelism
< 0) {
3953 filter
= isl_union_set_free(filter
);
3954 } else if (!parallelism
) {
3955 isl_union_set
*filter_i
;
3956 filter_i
= isl_schedule_node_filter_get_filter(node
);
3957 filter
= isl_union_set_union(filter
, filter_i
);
3958 } else if (only_initial
)
3960 node
= isl_schedule_node_parent(node
);
3963 isl_schedule_node_free(node
);
3968 /* Given a set or sequence node, return the union of the filters of
3969 * the direct subtrees that do not contain any suitably permutable bands
3970 * (according to subtree_has_permutable_bands).
3972 static __isl_give isl_union_set
*get_all_non_parallel_subtree_filters(
3973 __isl_keep isl_schedule_node
*node
)
3975 return get_non_parallel_subtree_filters(node
, 0);
3978 /* Given a set or sequence node, return the union of the filters of
3979 * the initial direct subtrees that do not contain any suitably permutable
3980 * bands (according to subtree_has_permutable_bands).
3982 static __isl_give isl_union_set
*get_initial_non_parallel_subtree_filters(
3983 __isl_keep isl_schedule_node
*node
)
3985 return get_non_parallel_subtree_filters(node
, 1);
3988 /* Mark all variables that are accessed by the statement instances in "domain"
3989 * and that are local to "prog" as requiring a declaration in the host code.
3991 static int declare_accessed_local_variables(struct gpu_prog
*prog
,
3992 __isl_keep isl_union_set
*domain
)
3994 isl_union_set
*arrays
;
3997 if (!ppcg_scop_any_hidden_declarations(prog
->scop
))
3999 arrays
= accessed_by_domain(isl_union_set_copy(domain
), prog
);
4001 for (i
= 0; i
< prog
->n_array
; ++i
) {
4006 if (!prog
->array
[i
].local
)
4008 space
= isl_set_get_space(prog
->array
[i
].extent
);
4009 set
= isl_union_set_extract_set(arrays
, space
);
4010 empty
= isl_set_plain_is_empty(set
);
4015 prog
->array
[i
].declare_local
= 1;
4018 isl_union_set_free(arrays
);
4021 isl_union_set_free(arrays
);
4025 /* If "node" points to a set node, then separate its children
4026 * into subtrees that have suitably permutable bands and
4027 * those that do not.
4028 * Adjust the schedule tree in order to execute the second group
4029 * after the first group and return a pointer to the first group,
4030 * assuming there are any such subtrees.
4031 * If "node" points to a sequence node, then separate the initial
4032 * children that do not have suitably permutable bands and
4033 * return a pointer to the subsequence of children that do have such bands,
4034 * assuming there are any such subtrees.
4036 * In both cases, mark all local variables in "prog" that are accessed by
4037 * the group without permutable bands as requiring a declaration on the host.
4039 static __isl_give isl_schedule_node
*isolate_permutable_subtrees(
4040 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
)
4042 isl_union_set
*filter
;
4043 enum isl_schedule_node_type type
;
4047 type
= isl_schedule_node_get_type(node
);
4048 if (type
== isl_schedule_node_set
) {
4049 filter
= get_all_non_parallel_subtree_filters(node
);
4051 node
= isl_schedule_node_free(node
);
4053 if (declare_accessed_local_variables(prog
, filter
) < 0)
4054 node
= isl_schedule_node_free(node
);
4055 node
= isl_schedule_node_order_after(node
, filter
);
4056 } else if (type
== isl_schedule_node_sequence
) {
4057 filter
= get_initial_non_parallel_subtree_filters(node
);
4059 node
= isl_schedule_node_free(node
);
4061 if (declare_accessed_local_variables(prog
, filter
) < 0)
4062 node
= isl_schedule_node_free(node
);
4063 node
= isl_schedule_node_order_before(node
, filter
);
4069 /* Replace any reference to an array element in the range of "copy"
4070 * by a reference to all array elements (defined by the extent of the array).
4072 static __isl_give isl_union_map
*approximate_copy_out(
4073 __isl_take isl_union_map
*copy
, struct gpu_prog
*prog
)
4078 res
= isl_union_map_empty(isl_union_map_get_space(copy
));
4080 for (i
= 0; i
< prog
->n_array
; ++i
) {
4083 isl_union_map
*copy_i
;
4084 isl_union_set
*extent
, *domain
;
4086 space
= isl_space_copy(prog
->array
[i
].space
);
4087 extent
= isl_union_set_from_set(isl_set_universe(space
));
4088 copy_i
= isl_union_map_copy(copy
);
4089 copy_i
= isl_union_map_intersect_range(copy_i
, extent
);
4090 set
= isl_set_copy(prog
->array
[i
].extent
);
4091 extent
= isl_union_set_from_set(set
);
4092 domain
= isl_union_map_domain(copy_i
);
4093 copy_i
= isl_union_map_from_domain_and_range(domain
, extent
);
4094 res
= isl_union_map_union(res
, copy_i
);
4097 isl_union_map_free(copy
);
4102 /* Insert "kernel" marks that point to a ppcg_kernel structure
4103 * in front of all outermost tilable band that (by construction)
4104 * have at least one parallel loop.
4106 static __isl_give isl_schedule_node
*mark_kernels(struct gpu_gen
*gen
,
4107 __isl_take isl_schedule_node
*node
)
4109 return isl_schedule_node_map_descendant_bottom_up(node
,
4110 &mark_outer_permutable
, gen
);
4113 /* Construct schedule constraints from the dependences in prog->scop and
4114 * the array order dependences in prog->array_order.
4116 * If live range reordering is allowed, then we need to make sure
4117 * that live ranges on arrays are not run in parallel since doing
4118 * so would require array expansion. We therefore add the array
4119 * order dependences to the coincidence dependences. Non-zero array
4120 * order dependences will then prevent a schedule dimension from being
4121 * considered parallel.
4122 * Live ranges derived from scalars are allowed to be run in parallel
4123 * since we force the scalars to be mapped to private memory in
4124 * check_scalar_live_ranges.
4125 * If live range reordering is allowed, then the false dependences
4126 * are not added to the validity constraints as that would prevent
4127 * reordering. Instead, the external false dependences that enforce that reads
4128 * from potentially live-in data precede any later write and
4129 * that writes of potentially live-out data follow any other earlier write
4130 * are added to the validity and the coincidence constraints.
4131 * The false dependences are still added to the proximity constraints
4132 * for consistency with the case where live range reordering is not allowed.
4133 * The coincidence constraints then consist of flow dependences,
4134 * external false dependences and array order dependences.
4135 * The independences can be filtered out from the first two sets.
4136 * They have already been filtered out from the array order dependences
4137 * on a per array basis in collect_order_dependences.
4138 * There is no need for a per array handling of the other two sets
4139 * as there should be no flow or external false dependence on local
4140 * variables that can be filtered out.
4142 static __isl_give isl_schedule_constraints
*construct_schedule_constraints(
4143 struct gpu_prog
*prog
)
4145 isl_union_set
*domain
;
4146 isl_union_map
*dep_raw
, *dep
;
4147 isl_union_map
*validity
, *proximity
, *coincidence
;
4148 isl_schedule_constraints
*sc
;
4150 domain
= isl_union_set_copy(prog
->scop
->domain
);
4151 sc
= isl_schedule_constraints_on_domain(domain
);
4152 sc
= isl_schedule_constraints_set_context(sc
,
4153 isl_set_copy(prog
->scop
->context
));
4154 if (prog
->scop
->options
->live_range_reordering
) {
4155 sc
= isl_schedule_constraints_set_conditional_validity(sc
,
4156 isl_union_map_copy(prog
->scop
->tagged_dep_flow
),
4157 isl_union_map_copy(prog
->scop
->tagged_dep_order
));
4158 proximity
= isl_union_map_copy(prog
->scop
->dep_flow
);
4159 validity
= isl_union_map_copy(proximity
);
4160 validity
= isl_union_map_union(validity
,
4161 isl_union_map_copy(prog
->scop
->dep_forced
));
4162 proximity
= isl_union_map_union(proximity
,
4163 isl_union_map_copy(prog
->scop
->dep_false
));
4164 coincidence
= isl_union_map_copy(validity
);
4165 coincidence
= isl_union_map_subtract(coincidence
,
4166 isl_union_map_copy(prog
->scop
->independence
));
4167 coincidence
= isl_union_map_union(coincidence
,
4168 isl_union_map_copy(prog
->array_order
));
4170 dep_raw
= isl_union_map_copy(prog
->scop
->dep_flow
);
4171 dep
= isl_union_map_copy(prog
->scop
->dep_false
);
4172 dep
= isl_union_map_union(dep
, dep_raw
);
4173 dep
= isl_union_map_coalesce(dep
);
4174 proximity
= isl_union_map_copy(dep
);
4175 coincidence
= isl_union_map_copy(dep
);
4178 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4179 sc
= isl_schedule_constraints_set_coincidence(sc
, coincidence
);
4180 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4182 if (prog
->scop
->options
->debug
->dump_schedule_constraints
)
4183 isl_schedule_constraints_dump(sc
);
4187 /* Compute an appropriate schedule based on the accesses in
4188 * gen->read and gen->write.
4190 * We derive schedule constraints from the dependences in gen->prog->scop
4191 * and then use isl to compute a schedule that has a parallel loop
4192 * in each tilable band.
4194 static __isl_give isl_schedule
*compute_schedule(struct gpu_gen
*gen
)
4196 isl_schedule_constraints
*sc
;
4197 isl_schedule
*schedule
;
4199 sc
= construct_schedule_constraints(gen
->prog
);
4200 schedule
= isl_schedule_constraints_compute_schedule(sc
);
4205 /* If the band node "node" has exactly one member then mark it permutable.
4207 static __isl_give isl_schedule_node
*band_set_permutable(
4208 __isl_take isl_schedule_node
*node
,
4209 __isl_keep isl_schedule_constraints
*sc
)
4211 if (isl_schedule_node_band_n_member(node
) == 1)
4212 node
= isl_schedule_node_band_set_permutable(node
, 1);
4217 /* Return the coincidence constraints between pairs of instances
4218 * that are scheduled together by the ancestors of "node".
4219 * That is, select those coincidence constraints that relate
4220 * pairs of instances that have the same value for the prefix schedule.
4221 * If the schedule depth is zero, then the prefix schedule does not
4222 * contain any information, so we intersect domain and range
4223 * of the schedule constraints with the reaching domain elements instead.
4225 static __isl_give isl_union_map
*get_local_coincidence(
4226 __isl_keep isl_schedule_node
*node
,
4227 __isl_keep isl_schedule_constraints
*sc
)
4229 isl_union_map
*coincidence
;
4230 isl_multi_union_pw_aff
*prefix
;
4231 isl_union_pw_multi_aff
*contraction
;
4233 coincidence
= isl_schedule_constraints_get_coincidence(sc
);
4234 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4235 if (isl_schedule_node_get_schedule_depth(node
) == 0) {
4236 isl_union_set
*domain
;
4238 domain
= isl_schedule_node_get_domain(node
);
4239 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4241 coincidence
= isl_union_map_intersect_domain(coincidence
,
4242 isl_union_set_copy(domain
));
4243 coincidence
= isl_union_map_intersect_range(coincidence
,
4248 prefix
= isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
4249 prefix
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
4251 return isl_union_map_eq_at_multi_union_pw_aff(coincidence
, prefix
);
4254 /* For each member in the band node "node", determine whether
4255 * it is coincident with respect to the outer nodes and mark
4258 * That is, for each coincidence constraint between pairs
4259 * of instances that are scheduled together by the outer nodes,
4260 * check that domain and range are assigned the same value
4261 * by the band member. This test is performed by checking
4262 * that imposing the same value for the band member does not
4263 * remove any elements from the set of coincidence constraints.
4265 static __isl_give isl_schedule_node
*band_set_coincident(
4266 __isl_take isl_schedule_node
*node
,
4267 __isl_keep isl_schedule_constraints
*sc
)
4269 isl_union_map
*coincidence
;
4270 isl_union_pw_multi_aff
*contraction
;
4271 isl_multi_union_pw_aff
*partial
;
4274 coincidence
= get_local_coincidence(node
, sc
);
4276 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4277 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4278 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4280 n
= isl_schedule_node_band_n_member(node
);
4281 for (i
= 0; i
< n
; ++i
) {
4282 isl_union_map
*coincidence_i
;
4283 isl_union_pw_aff
*upa
;
4284 isl_multi_union_pw_aff
*partial_i
;
4287 upa
= isl_multi_union_pw_aff_get_union_pw_aff(partial
, i
);
4288 partial_i
= isl_multi_union_pw_aff_from_union_pw_aff(upa
);
4289 coincidence_i
= isl_union_map_copy(coincidence
);
4290 coincidence_i
= isl_union_map_eq_at_multi_union_pw_aff(
4291 coincidence_i
, partial_i
);
4292 subset
= isl_union_map_is_subset(coincidence
, coincidence_i
);
4293 isl_union_map_free(coincidence_i
);
4297 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
4301 node
= isl_schedule_node_free(node
);
4302 isl_multi_union_pw_aff_free(partial
);
4303 isl_union_map_free(coincidence
);
4308 /* If "node" is a band, then set its properties.
4310 * In particular, if the band has exactly one member, then mark it permutable.
4311 * Mark the band member coincident based on the coincidence constraints
4314 static __isl_give isl_schedule_node
*set_band_properties(
4315 __isl_take isl_schedule_node
*node
, void *user
)
4317 isl_schedule_constraints
*sc
= user
;
4319 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
4321 if (isl_schedule_node_band_n_member(node
) == 0)
4324 node
= band_set_permutable(node
, sc
);
4325 node
= band_set_coincident(node
, sc
);
4330 /* Return the original schedule with all bands marked permutable and
4331 * all band members marked coincident based on the coincidence constraints.
4332 * The bands are explicitly marked permutable so that they will be considered
4333 * by mark_outer_permutable.
4335 static __isl_give isl_schedule
*determine_properties_original_schedule(
4336 struct gpu_gen
*gen
)
4338 isl_schedule
*schedule
;
4339 isl_schedule_constraints
*sc
;
4341 schedule
= isl_schedule_copy(gen
->prog
->scop
->schedule
);
4342 sc
= construct_schedule_constraints(gen
->prog
);
4343 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
4344 &set_band_properties
, sc
);
4345 isl_schedule_constraints_free(sc
);
4350 /* Compute a schedule or determine the properties of the original schedule
4351 * depending on the value of the "reschedule" option.
4353 static __isl_give isl_schedule
*compute_or_set_properties(void *user
)
4355 struct gpu_gen
*gen
= user
;
4357 if (gen
->options
->reschedule
)
4358 return compute_schedule(gen
);
4360 return determine_properties_original_schedule(gen
);
4363 /* Obtain a schedule for the scop, by reading it from
4364 * a file, by computing one or by determining the properties
4365 * of the original schedule.
4367 static __isl_give isl_schedule
*get_schedule(struct gpu_gen
*gen
)
4369 return ppcg_get_schedule(gen
->ctx
, gen
->options
,
4370 &compute_or_set_properties
, gen
);
4373 /* Construct the string "<a>_<b>".
4375 static char *concat(isl_ctx
*ctx
, const char *a
, const char *b
)
4380 p
= isl_printer_to_str(ctx
);
4381 p
= isl_printer_print_str(p
, a
);
4382 p
= isl_printer_print_str(p
, "_");
4383 p
= isl_printer_print_str(p
, b
);
4384 s
= isl_printer_get_str(p
);
4385 isl_printer_free(p
);
4390 /* For each array in "prog" of which an element appears in "accessed" and
4391 * that is not a read only scalar, create a zero-dimensional universe set
4392 * of which the tuple id has name "<prefix>_<name of array>" and a user
4393 * pointer pointing to the array (gpu_array_info).
4395 * If the array is local to "prog", then make sure it will be declared
4398 * Return the list of these universe sets.
4400 static __isl_give isl_union_set_list
*create_copy_filters(struct gpu_prog
*prog
,
4401 const char *prefix
, __isl_take isl_union_set
*accessed
)
4405 isl_union_set_list
*filters
;
4408 filters
= isl_union_set_list_alloc(ctx
, 0);
4409 for (i
= 0; i
< prog
->n_array
; ++i
) {
4410 struct gpu_array_info
*array
= &prog
->array
[i
];
4412 isl_set
*accessed_i
;
4416 isl_union_set
*uset
;
4418 if (gpu_array_is_read_only_scalar(array
))
4421 space
= isl_space_copy(array
->space
);
4422 accessed_i
= isl_union_set_extract_set(accessed
, space
);
4423 empty
= isl_set_plain_is_empty(accessed_i
);
4424 isl_set_free(accessed_i
);
4426 filters
= isl_union_set_list_free(filters
);
4434 array
->declare_local
= 1;
4436 name
= concat(ctx
, prefix
, array
->name
);
4437 id
= name
? isl_id_alloc(ctx
, name
, array
) : NULL
;
4439 space
= isl_space_set_alloc(ctx
, 0, 0);
4440 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4441 uset
= isl_union_set_from_set(isl_set_universe(space
));
4443 filters
= isl_union_set_list_add(filters
, uset
);
4445 isl_union_set_free(accessed
);
4450 /* Make sure that code for the statements in "filters" that
4451 * copy arrays to or from the device is only generated when
4452 * the size of the corresponding array is positive.
4453 * That is, add a set node underneath "graft" with "filters" as children
4454 * and for each child add a guard that the selects the parameter
4455 * values for which the corresponding array has a positive size.
4456 * The array is available in the user pointer of the statement identifier.
4457 * "depth" is the schedule depth of the position where "graft"
4460 static __isl_give isl_schedule_node
*insert_positive_size_guards(
4461 __isl_take isl_schedule_node
*graft
,
4462 __isl_take isl_union_set_list
*filters
, int depth
)
4466 graft
= isl_schedule_node_child(graft
, 0);
4467 graft
= isl_schedule_node_insert_set(graft
, filters
);
4468 n
= isl_schedule_node_n_children(graft
);
4469 for (i
= 0; i
< n
; ++i
) {
4470 isl_union_set
*filter
;
4471 isl_set
*domain
, *guard
;
4473 struct gpu_array_info
*array
;
4475 graft
= isl_schedule_node_child(graft
, i
);
4476 filter
= isl_schedule_node_filter_get_filter(graft
);
4477 domain
= isl_set_from_union_set(filter
);
4478 id
= isl_set_get_tuple_id(domain
);
4479 array
= isl_id_get_user(id
);
4481 isl_set_free(domain
);
4482 guard
= gpu_array_positive_size_guard(array
);
4483 guard
= isl_set_from_params(guard
);
4484 guard
= isl_set_add_dims(guard
, isl_dim_set
, depth
);
4485 graft
= isl_schedule_node_child(graft
, 0);
4486 graft
= isl_schedule_node_insert_guard(graft
, guard
);
4487 graft
= isl_schedule_node_parent(graft
);
4488 graft
= isl_schedule_node_parent(graft
);
4490 graft
= isl_schedule_node_parent(graft
);
4495 /* Create a graft for copying arrays to or from the device,
4496 * whenever the size of the array is strictly positive.
4497 * Each statement is called "<prefix>_<name of array>" and
4498 * the identifier has a user pointer pointing to the array.
4499 * The graft will be added at the position specified by "node".
4500 * "copy" contains the array elements that need to be copied.
4501 * Only arrays of which some elements need to be copied
4502 * will have a corresponding statement in the graph.
4503 * Note though that each such statement will copy the entire array.
4505 static __isl_give isl_schedule_node
*create_copy_device(struct gpu_prog
*prog
,
4506 __isl_keep isl_schedule_node
*node
, const char *prefix
,
4507 __isl_take isl_union_set
*copy
)
4512 isl_union_set
*all
, *domain
;
4513 isl_union_set_list
*filters
;
4514 isl_union_map
*extension
;
4515 isl_schedule_node
*graft
;
4518 depth
= isl_schedule_node_get_schedule_depth(node
);
4519 filters
= create_copy_filters(prog
, prefix
, copy
);
4520 all
= isl_union_set_list_union(isl_union_set_list_copy(filters
));
4522 space
= depth
< 0 ? NULL
: isl_space_set_alloc(ctx
, 0, depth
);
4523 domain
= isl_union_set_from_set(isl_set_universe(space
));
4524 extension
= isl_union_map_from_domain_and_range(domain
, all
);
4525 graft
= isl_schedule_node_from_extension(extension
);
4528 return isl_schedule_node_free(graft
);
4529 if (isl_union_set_list_n_union_set(filters
) == 0) {
4530 isl_union_set_list_free(filters
);
4534 return insert_positive_size_guards(graft
, filters
, depth
);
4537 /* Return (the universe spaces of) the arrays that are declared
4538 * inside the scop corresponding to "prog" and for which all
4539 * potential writes inside the scop form a subset of "domain".
4541 static __isl_give isl_union_set
*extract_local_accesses(struct gpu_prog
*prog
,
4542 __isl_keep isl_union_set
*domain
)
4545 isl_union_set
*local
;
4547 local
= isl_union_set_empty(isl_union_set_get_space(domain
));
4549 for (i
= 0; i
< prog
->n_array
; ++i
) {
4551 isl_union_map
*to_outer
;
4552 isl_union_map
*may_write
;
4553 isl_union_set
*write_domain
;
4554 isl_union_set
*fields
;
4557 if (!prog
->array
[i
].local
)
4560 set
= isl_set_universe(isl_space_copy(prog
->array
[i
].space
));
4561 to_outer
= isl_union_map_copy(prog
->to_outer
);
4562 to_outer
= isl_union_map_intersect_range(to_outer
,
4563 isl_union_set_from_set(isl_set_copy(set
)));
4564 fields
= isl_union_map_domain(to_outer
);
4565 may_write
= isl_union_map_copy(prog
->may_write
);
4566 may_write
= isl_union_map_intersect_range(may_write
, fields
);
4567 write_domain
= isl_union_map_domain(may_write
);
4568 subset
= isl_union_set_is_subset(write_domain
, domain
);
4569 isl_union_set_free(write_domain
);
4573 return isl_union_set_free(local
);
4574 } else if (subset
) {
4575 local
= isl_union_set_add_set(local
, set
);
4584 /* Internal data structure for node_may_persist.
4586 * "tagger" maps tagged iteration domains to the corresponding untagged
4589 * "may_persist_flow" is the set of all tagged dataflow dependences
4590 * with those dependences removed that either precede or follow
4591 * the kernel launch in a sequence.
4592 * "inner_band_flow" is the set of all tagged dataflow dependences
4593 * that are local to a given iteration of the outer band nodes
4594 * with respect to the current node.
4595 * "local_flow" is equal to "inner_band_flow", except that the domain
4596 * and the range have been intersected with intermediate filters
4597 * on children of sets or sequences.
4599 struct ppcg_may_persist_data
{
4600 isl_union_pw_multi_aff
*tagger
;
4602 isl_union_map
*local_flow
;
4603 isl_union_map
*inner_band_flow
;
4604 isl_union_map
*may_persist_flow
;
4607 /* Update the information in "data" based on the band ancestor "node".
4609 * In particular, we restrict the dependences in data->local_flow
4610 * to those dependence where the source and the sink occur in
4611 * the same iteration of the given band node.
4612 * We also update data->inner_band_flow to the new value of
4615 static int update_may_persist_at_band(__isl_keep isl_schedule_node
*node
,
4616 struct ppcg_may_persist_data
*data
)
4618 isl_multi_union_pw_aff
*partial
;
4619 isl_union_pw_multi_aff
*contraction
;
4620 isl_union_map
*flow
;
4622 if (isl_schedule_node_band_n_member(node
) == 0)
4625 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4626 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4627 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4629 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4630 isl_union_pw_multi_aff_copy(data
->tagger
));
4632 flow
= data
->local_flow
;
4633 flow
= isl_union_map_eq_at_multi_union_pw_aff(flow
, partial
);
4634 data
->local_flow
= flow
;
4636 isl_union_map_free(data
->inner_band_flow
);
4637 data
->inner_band_flow
= isl_union_map_copy(data
->local_flow
);
4642 /* Given a set of local reaching domain elements "domain",
4643 * expand them to the corresponding leaf domain elements using "contraction"
4644 * and insert the array references tags using data->tagger.
4646 static __isl_give isl_union_set
*expand_and_tag(
4647 __isl_take isl_union_set
*domain
,
4648 __isl_take isl_union_pw_multi_aff
*contraction
,
4649 struct ppcg_may_persist_data
*data
)
4651 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4653 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4654 isl_union_pw_multi_aff_copy(data
->tagger
));
4658 /* Given a filter node that is the child of a set or sequence node,
4659 * restrict data->local_flow to refer only to those elements
4660 * in the filter of the node.
4661 * "contraction" maps the leaf domain elements of the schedule tree
4662 * to the corresponding domain elements at (the parent of) "node".
4664 static int filter_flow(__isl_keep isl_schedule_node
*node
,
4665 struct ppcg_may_persist_data
*data
,
4666 __isl_take isl_union_pw_multi_aff
*contraction
)
4668 isl_union_set
*filter
;
4669 isl_union_map
*flow
;
4671 flow
= data
->local_flow
;
4672 filter
= isl_schedule_node_filter_get_filter(node
);
4673 filter
= expand_and_tag(filter
, contraction
, data
);
4674 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(filter
));
4675 flow
= isl_union_map_intersect_range(flow
, filter
);
4676 data
->local_flow
= flow
;
4681 /* Given a filter node "node", collect the filters on all preceding siblings
4682 * (which are also filter nodes), add them to "filters" and return the result.
4684 static __isl_give isl_union_set
*add_previous_filters(
4685 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4687 isl_schedule_node
*sibling
;
4689 sibling
= isl_schedule_node_copy(node
);
4690 while (sibling
&& isl_schedule_node_has_previous_sibling(sibling
)) {
4691 isl_union_set
*filter
;
4693 sibling
= isl_schedule_node_previous_sibling(sibling
);
4694 filter
= isl_schedule_node_filter_get_filter(sibling
);
4695 filters
= isl_union_set_union(filters
, filter
);
4697 isl_schedule_node_free(sibling
);
4699 return isl_union_set_free(filters
);
4704 /* Given a filter node "node", collect the filters on all following siblings
4705 * (which are also filter nodes), add them to "filters" and return the result.
4707 static __isl_give isl_union_set
*add_next_filters(
4708 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4710 isl_schedule_node
*sibling
;
4712 sibling
= isl_schedule_node_copy(node
);
4713 while (sibling
&& isl_schedule_node_has_next_sibling(sibling
)) {
4714 isl_union_set
*filter
;
4716 sibling
= isl_schedule_node_next_sibling(sibling
);
4717 filter
= isl_schedule_node_filter_get_filter(sibling
);
4718 filters
= isl_union_set_union(filters
, filter
);
4720 isl_schedule_node_free(sibling
);
4722 return isl_union_set_free(filters
);
4727 /* Remove those flow dependences from data->may_persist_flow
4728 * that flow between elements of "domain" within the same iteration
4729 * of all outer band nodes.
4730 * "contraction" maps the leaf domain elements of the schedule tree
4731 * to the corresponding elements "domain".
4733 static void remove_external_flow(struct ppcg_may_persist_data
*data
,
4734 __isl_take isl_union_set
*domain
,
4735 __isl_keep isl_union_pw_multi_aff
*contraction
)
4737 isl_union_map
*flow
;
4739 contraction
= isl_union_pw_multi_aff_copy(contraction
);
4740 domain
= expand_and_tag(domain
, contraction
, data
);
4741 flow
= isl_union_map_copy(data
->local_flow
);
4742 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(domain
));
4743 flow
= isl_union_map_intersect_range(flow
, domain
);
4745 data
->may_persist_flow
= isl_union_map_subtract(data
->may_persist_flow
,
4749 /* Update the information in "data" based on the filter ancestor "node".
4750 * We only need to modify anything if the filter is the child
4751 * of a set or sequence node.
4753 * In the case of a sequence, we remove the dependences between
4754 * statement instances that are both executed either before or
4755 * after the subtree that will be mapped to a kernel, within
4756 * the same iteration of outer bands.
4758 * In both cases, we restrict data->local_flow to the current child.
4760 static int update_may_persist_at_filter(__isl_keep isl_schedule_node
*node
,
4761 struct ppcg_may_persist_data
*data
)
4763 enum isl_schedule_node_type type
;
4764 isl_schedule_node
*parent
;
4766 isl_union_pw_multi_aff
*contraction
;
4767 isl_union_set
*before
, *after
, *filter
;
4769 type
= isl_schedule_node_get_parent_type(node
);
4770 if (type
!= isl_schedule_node_sequence
&& type
!= isl_schedule_node_set
)
4773 parent
= isl_schedule_node_copy(node
);
4774 parent
= isl_schedule_node_parent(parent
);
4775 contraction
= isl_schedule_node_get_subtree_contraction(parent
);
4776 isl_schedule_node_free(parent
);
4778 if (type
== isl_schedule_node_set
)
4779 return filter_flow(node
, data
, contraction
);
4781 filter
= isl_schedule_node_filter_get_filter(node
);
4782 space
= isl_union_set_get_space(filter
);
4783 isl_union_set_free(filter
);
4784 before
= isl_union_set_empty(space
);
4785 after
= isl_union_set_copy(before
);
4786 before
= add_previous_filters(before
, node
);
4787 after
= add_next_filters(after
, node
);
4789 remove_external_flow(data
, before
, contraction
);
4790 remove_external_flow(data
, after
, contraction
);
4792 return filter_flow(node
, data
, contraction
);
4795 /* Update the information in "data" based on the ancestor "node".
4797 static isl_stat
update_may_persist_at(__isl_keep isl_schedule_node
*node
,
4800 struct ppcg_may_persist_data
*data
= user
;
4802 switch (isl_schedule_node_get_type(node
)) {
4803 case isl_schedule_node_error
:
4804 return isl_stat_error
;
4805 case isl_schedule_node_context
:
4806 case isl_schedule_node_domain
:
4807 case isl_schedule_node_expansion
:
4808 case isl_schedule_node_extension
:
4809 case isl_schedule_node_guard
:
4810 case isl_schedule_node_leaf
:
4811 case isl_schedule_node_mark
:
4812 case isl_schedule_node_sequence
:
4813 case isl_schedule_node_set
:
4815 case isl_schedule_node_band
:
4816 if (update_may_persist_at_band(node
, data
) < 0)
4817 return isl_stat_error
;
4819 case isl_schedule_node_filter
:
4820 if (update_may_persist_at_filter(node
, data
) < 0)
4821 return isl_stat_error
;
4828 /* Determine the set of array elements that may need to be perserved
4829 * by a kernel constructed from the subtree at "node".
4830 * This includes the set of array elements that may need to be preserved
4831 * by the entire scop (prog->may_persist) and the elements for which
4832 * there is a potential flow dependence that may cross a kernel launch.
4834 * To determine the second set, we start from all flow dependences.
4835 * From this set of dependences, we remove those that cannot possibly
4836 * require data to be preserved by a kernel launch.
4837 * In particular, we consider the following sets of dependences.
4838 * - dependences of which the write occurs inside the kernel.
4839 * If the data is needed outside the kernel, then it will
4840 * be copied out immediately after the kernel launch, so there
4841 * is no need for any special care.
4842 * - dependences of which the read occurs inside the kernel and the
4843 * corresponding write occurs inside the same iteration of the
4844 * outer band nodes. This means that the data is needed in
4845 * the first kernel launch after the write, which is already
4846 * taken care of by the standard copy-in. That is, the data
4847 * do not need to be preserved by any intermediate call to
4849 * - dependences of which the write and the read either both occur
4850 * before the kernel launch or both occur after the kernel launch,
4851 * within the same iteration of the outer band nodes with respect
4852 * to the sequence that determines the ordering of the dependence
4853 * and the kernel launch. Such flow dependences cannot cross
4854 * any kernel launch.
4856 * For the remaining (tagged) dependences, we take the domain
4857 * (i.e., the tagged writes) and apply the tagged access relation
4858 * to obtain the accessed data elements.
4859 * These are then combined with the elements that may need to be
4860 * preserved by the entire scop.
4862 static __isl_give isl_union_set
*node_may_persist(
4863 __isl_keep isl_schedule_node
*node
, struct gpu_prog
*prog
)
4865 struct ppcg_may_persist_data data
;
4866 isl_union_pw_multi_aff
*contraction
;
4867 isl_union_set
*domain
;
4868 isl_union_set
*persist
;
4869 isl_union_map
*flow
, *local_flow
;
4871 data
.tagger
= prog
->scop
->tagger
;
4873 flow
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
4874 data
.local_flow
= isl_union_map_copy(flow
);
4875 data
.inner_band_flow
= isl_union_map_copy(flow
);
4876 data
.may_persist_flow
= flow
;
4877 if (isl_schedule_node_foreach_ancestor_top_down(node
,
4878 &update_may_persist_at
, &data
) < 0)
4879 data
.may_persist_flow
=
4880 isl_union_map_free(data
.may_persist_flow
);
4881 flow
= data
.may_persist_flow
;
4882 isl_union_map_free(data
.local_flow
);
4884 domain
= isl_schedule_node_get_domain(node
);
4885 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4886 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4888 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4889 isl_union_pw_multi_aff_copy(data
.tagger
));
4890 flow
= isl_union_map_subtract_domain(flow
, isl_union_set_copy(domain
));
4891 local_flow
= data
.inner_band_flow
;
4892 local_flow
= isl_union_map_intersect_range(local_flow
, domain
);
4893 flow
= isl_union_map_subtract(flow
, local_flow
);
4895 persist
= isl_union_map_domain(flow
);
4896 persist
= isl_union_set_apply(persist
,
4897 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
4898 persist
= isl_union_set_union(persist
,
4899 isl_union_set_copy(prog
->may_persist
));
4904 /* Add nodes for copying outer arrays in and out of the device
4905 * before and after the subtree "node", which contains one or more kernels.
4906 * "domain" contains the original reaching domain elements before
4907 * the kernels were created, i.e., before the contraction that
4908 * may have been performed in creating the kernels has been applied.
4909 * "prefix" contains the prefix schedule at that point, in terms
4910 * of the same original reaching domain elements.
4912 * We first compute the sets of outer array elements that need
4913 * to be copied in and out and then graft in the nodes for
4914 * performing this copying.
4916 * In particular, for each array that is possibly written anywhere in
4917 * the subtree "node" and that may be used after "node"
4918 * or that may be visible outside the corresponding scop,
4919 * we copy out its entire extent.
4921 * Any array elements that is read without first being written inside
4922 * the subtree "node" needs to be copied in.
4923 * Furthermore, if there are any array elements that
4924 * are copied out, but that may not be written inside "node, then
4925 * they also need to be copied in to ensure that the value after execution
4926 * is the same as the value before execution, at least for those array
4927 * elements that may have their values preserved by the scop or that
4928 * may be written before "node" and read after "node".
4929 * In case the array elements are structures, we need to take into
4930 * account that all members of the structures need to be written
4931 * by "node" before we can avoid copying the data structure in.
4933 * Note that the may_write relation is intersected with the domain,
4934 * which has been intersected with the context.
4935 * This helps in those cases where the arrays are declared with a fixed size,
4936 * while the accesses are parametric and the context assigns a fixed value
4937 * to the parameters.
4939 * If an element from a local array is read without first being written,
4940 * then there is no point in copying it in since it cannot have been
4941 * written prior to the scop. Warn about the uninitialized read instead.
4943 static __isl_give isl_schedule_node
*add_to_from_device(
4944 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
4945 __isl_take isl_union_map
*prefix
, struct gpu_prog
*prog
)
4947 isl_union_set
*local
;
4948 isl_union_set
*may_persist
;
4949 isl_union_map
*may_write
, *must_write
, *copy_out
, *not_written
;
4950 isl_union_map
*read
, *copy_in
;
4951 isl_union_map
*tagged
;
4952 isl_union_map
*local_uninitialized
;
4953 isl_schedule_node
*graft
;
4955 tagged
= isl_union_map_copy(prog
->scop
->tagged_reads
);
4956 tagged
= isl_union_map_union(tagged
,
4957 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
4959 may_write
= isl_union_map_copy(prog
->may_write
);
4960 may_write
= isl_union_map_intersect_domain(may_write
,
4961 isl_union_set_copy(domain
));
4962 may_write
= remove_local_accesses(prog
,
4963 isl_union_map_copy(tagged
), may_write
,
4964 isl_union_map_copy(prefix
), 0);
4965 may_write
= isl_union_map_apply_range(may_write
,
4966 isl_union_map_copy(prog
->to_outer
));
4967 may_write
= isl_union_map_apply_domain(may_write
,
4968 isl_union_map_copy(prefix
));
4969 may_write
= approximate_copy_out(may_write
, prog
);
4970 copy_out
= isl_union_map_copy(may_write
);
4971 may_write
= isl_union_map_apply_range(may_write
,
4972 isl_union_map_copy(prog
->to_inner
));
4973 must_write
= isl_union_map_copy(prog
->must_write
);
4974 must_write
= isl_union_map_apply_domain(must_write
,
4975 isl_union_map_copy(prefix
));
4976 may_persist
= node_may_persist(node
, prog
);
4977 may_write
= isl_union_map_intersect_range(may_write
, may_persist
);
4978 not_written
= isl_union_map_subtract(may_write
, must_write
);
4980 local
= extract_local_accesses(prog
, domain
);
4981 read
= isl_union_map_copy(prog
->read
);
4982 read
= isl_union_map_intersect_domain(read
, domain
);
4983 read
= remove_local_accesses(prog
, tagged
, read
,
4984 isl_union_map_copy(prefix
), 1);
4985 local
= isl_union_set_apply(local
, isl_union_map_copy(prog
->to_inner
));
4986 local_uninitialized
= isl_union_map_copy(prog
->scop
->live_in
);
4987 local_uninitialized
= isl_union_map_intersect_range(local_uninitialized
,
4989 local_uninitialized
= isl_union_map_intersect(local_uninitialized
,
4990 isl_union_map_copy(read
));
4991 if (!isl_union_map_is_empty(local_uninitialized
)) {
4993 "possibly uninitialized reads (not copied in):\n");
4994 isl_union_map_dump(local_uninitialized
);
4996 read
= isl_union_map_subtract(read
, local_uninitialized
);
4997 read
= isl_union_map_apply_domain(read
, prefix
);
4998 copy_in
= isl_union_map_union(read
, not_written
);
4999 copy_in
= isl_union_map_apply_range(copy_in
,
5000 isl_union_map_copy(prog
->to_outer
));
5002 graft
= create_copy_device(prog
, node
, "to_device",
5003 isl_union_map_range(copy_in
));
5004 node
= isl_schedule_node_graft_before(node
, graft
);
5005 graft
= create_copy_device(prog
, node
, "from_device",
5006 isl_union_map_range(copy_out
));
5007 node
= isl_schedule_node_graft_after(node
, graft
);
5012 /* Add nodes for initializing ("init_device") and clearing ("clear_device")
5013 * the device before and after "node".
5015 static __isl_give isl_schedule_node
*add_init_clear_device(
5016 __isl_take isl_schedule_node
*node
)
5020 isl_union_set
*domain
;
5021 isl_schedule_node
*graft
;
5023 ctx
= isl_schedule_node_get_ctx(node
);
5025 space
= isl_space_set_alloc(ctx
, 0, 0);
5026 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "init_device");
5027 domain
= isl_union_set_from_set(isl_set_universe(space
));
5028 graft
= isl_schedule_node_from_domain(domain
);
5030 node
= isl_schedule_node_graft_before(node
, graft
);
5032 space
= isl_space_set_alloc(ctx
, 0, 0);
5033 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "clear_device");
5034 domain
= isl_union_set_from_set(isl_set_universe(space
));
5035 graft
= isl_schedule_node_from_domain(domain
);
5037 node
= isl_schedule_node_graft_after(node
, graft
);
5042 /* Update "schedule" for mapping to a GPU device.
5044 * In particular, insert a context node, create kernels for
5045 * each outermost tilable band and introduce nodes for copying arrays
5046 * in and out of the device and for initializing and clearing the device.
5047 * If the child of the initial root points to a set node,
5048 * then children of this node that do not contain any tilable bands
5049 * are separated from the other children and are not mapped to
5052 * The GPU code is generated in a context where at least one
5053 * statement instance is executed. The corresponding guard is inserted
5054 * around the entire schedule.
5056 static __isl_give isl_schedule
*map_to_device(struct gpu_gen
*gen
,
5057 __isl_take isl_schedule
*schedule
)
5059 isl_schedule_node
*node
;
5062 isl_union_set
*domain
;
5063 isl_union_map
*prefix
;
5064 struct gpu_prog
*prog
;
5066 context
= isl_set_copy(gen
->prog
->context
);
5067 context
= isl_set_from_params(context
);
5068 schedule
= isl_schedule_insert_context(schedule
, context
);
5071 guard
= isl_union_set_params(isl_union_set_copy(prog
->scop
->domain
));
5072 prog
->context
= isl_set_intersect(prog
->context
, isl_set_copy(guard
));
5073 guard
= isl_set_from_params(guard
);
5075 node
= isl_schedule_get_root(schedule
);
5076 isl_schedule_free(schedule
);
5077 node
= isl_schedule_node_child(node
, 0);
5078 node
= isl_schedule_node_child(node
, 0);
5079 node
= isolate_permutable_subtrees(node
, gen
->prog
);
5080 domain
= isl_schedule_node_get_domain(node
);
5081 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
5082 node
= mark_kernels(gen
, node
);
5083 node
= add_to_from_device(node
, domain
, prefix
, gen
->prog
);
5084 node
= isl_schedule_node_root(node
);
5085 node
= isl_schedule_node_child(node
, 0);
5086 node
= isl_schedule_node_child(node
, 0);
5087 node
= isl_schedule_node_insert_guard(node
, guard
);
5088 node
= isl_schedule_node_child(node
, 0);
5089 node
= add_init_clear_device(node
);
5090 schedule
= isl_schedule_node_get_schedule(node
);
5091 isl_schedule_node_free(node
);
5096 /* Internal data structure for extract_access.
5097 * "next_access" points to the end of a linked list that is extended
5098 * by extract_access.
5099 * "single_expression" is set if the access expressions belong to
5100 * an expression statement (i.e., a statement without internal control).
5101 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5103 struct ppcg_extract_access_data
{
5104 struct gpu_stmt_access
**next_access
;
5105 int single_expression
;
5106 isl_union_map
*any_to_outer
;
5109 /* Given a tagged access relation to a single array "tagged", extract it
5110 * as a map, taking into account that the input may be empty.
5111 * If the access relation is empty, then it does not contain
5112 * any space information, so we try to recover it from the index
5114 * The space of the index expression is of the form I -> A,
5115 * with I the statement instances and A the array, or [I -> F] -> A,
5116 * with F the filters corresponding to arguments.
5117 * We first drop F, if present, obtaining I -> A.
5118 * Then we construct I -> R, with R the reference tag,
5119 * combine the two into I -> [R -> A] and uncurry to obtain
5120 * the final result [I -> R] -> A.
5121 * Note that the index expression may have a lower dimension
5122 * than that of the array, but this dimension is not used
5123 * if the access relation is empty.
5125 static __isl_give isl_map
*extract_single_tagged_access(
5126 __isl_take isl_union_map
*tagged
, __isl_keep pet_expr
*expr
)
5130 isl_space
*space
, *space2
;
5131 isl_multi_pw_aff
*index
;
5133 empty
= isl_union_map_is_empty(tagged
);
5137 return isl_map_from_union_map(tagged
);
5138 isl_union_map_free(tagged
);
5140 index
= pet_expr_access_get_index(expr
);
5141 space
= isl_multi_pw_aff_get_space(index
);
5142 isl_multi_pw_aff_free(index
);
5143 if (isl_space_domain_is_wrapping(space
))
5144 space
= isl_space_domain_factor_domain(space
);
5145 space2
= isl_space_copy(space
);
5146 space2
= isl_space_from_domain(isl_space_domain(space
));
5147 id
= pet_expr_access_get_ref_id(expr
);
5148 space2
= isl_space_set_tuple_id(space2
, isl_dim_out
, id
);
5149 space
= isl_space_range_product(space2
, space
);
5150 space
= isl_space_uncurry(space
);
5152 return isl_map_empty(space
);
5154 isl_union_map_free(tagged
);
5158 /* Extract a gpu_stmt_access from "expr", append it to the list
5159 * that ends in *data->next_access and update the end of the list.
5160 * If the access expression performs a write, then it is considered
5161 * exact only if it appears in a single expression statement and
5162 * if its may access relation is equal to its must access relation.
5164 * The combined set of may accesses may be union if member accesses
5165 * are involved, but the entire set is derived from a single reference and
5166 * therefore from a single index expression. These accesses therefore
5167 * all map to the same outer array.
5169 static int extract_access(__isl_keep pet_expr
*expr
, void *user
)
5171 struct ppcg_extract_access_data
*data
= user
;
5172 isl_union_map
*tagged
;
5173 struct gpu_stmt_access
*access
;
5174 isl_ctx
*ctx
= pet_expr_get_ctx(expr
);
5175 isl_multi_pw_aff
*index
;
5177 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
5179 access
->next
= NULL
;
5180 access
->read
= pet_expr_access_is_read(expr
);
5181 access
->write
= pet_expr_access_is_write(expr
);
5182 tagged
= pet_expr_access_get_tagged_may_read(expr
);
5183 tagged
= isl_union_map_union(tagged
,
5184 pet_expr_access_get_tagged_may_write(expr
));
5185 tagged
= isl_union_map_apply_range(tagged
,
5186 isl_union_map_copy(data
->any_to_outer
));
5187 if (!access
->write
) {
5188 access
->exact_write
= 1;
5189 } else if (!data
->single_expression
) {
5190 access
->exact_write
= 0;
5192 isl_union_map
*must
, *may
;
5193 may
= isl_union_map_copy(tagged
);
5194 may
= isl_union_map_domain_factor_domain(may
);
5195 must
= pet_expr_access_get_must_write(expr
);
5196 access
->exact_write
= isl_union_map_is_equal(must
, may
);
5197 isl_union_map_free(must
);
5198 isl_union_map_free(may
);
5200 index
= pet_expr_access_get_index(expr
);
5201 access
->n_index
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5202 isl_multi_pw_aff_free(index
);
5203 access
->ref_id
= pet_expr_access_get_ref_id(expr
);
5204 access
->tagged_access
= extract_single_tagged_access(tagged
, expr
);
5205 access
->access
= isl_map_copy(access
->tagged_access
);
5206 access
->access
= isl_map_domain_factor_domain(access
->access
);
5208 *data
->next_access
= access
;
5209 data
->next_access
= &(*data
->next_access
)->next
;
5211 if (!access
->access
)
5217 /* Construct a linked list of gpu_stmt_access objects,
5218 * one for each access expression in the statement body.
5219 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5221 static int pet_stmt_extract_accesses(struct gpu_stmt
*stmt
,
5222 __isl_keep isl_union_map
*any_to_outer
)
5224 struct ppcg_extract_access_data data
;
5226 stmt
->accesses
= NULL
;
5227 data
.next_access
= &stmt
->accesses
;
5228 data
.single_expression
=
5229 pet_tree_get_type(stmt
->stmt
->body
) == pet_tree_expr
;
5230 data
.any_to_outer
= any_to_outer
;
5231 return pet_tree_foreach_access_expr(stmt
->stmt
->body
,
5232 &extract_access
, &data
);
5235 /* Return an array of gpu_stmt representing the statements in "scop".
5237 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
5238 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*any_to_outer
)
5241 struct gpu_stmt
*stmts
;
5243 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->pet
->n_stmt
);
5247 for (i
= 0; i
< scop
->pet
->n_stmt
; ++i
) {
5248 struct gpu_stmt
*s
= &stmts
[i
];
5250 s
->id
= isl_set_get_tuple_id(scop
->pet
->stmts
[i
]->domain
);
5251 s
->stmt
= scop
->pet
->stmts
[i
];
5252 if (pet_stmt_extract_accesses(s
, any_to_outer
) < 0)
5253 return free_stmts(stmts
, i
+ 1);
5259 /* Generate CUDA code for "scop" and print it to "p".
5260 * After generating an AST for the transformed scop as explained below,
5261 * we call "gen->print" to print the AST in the desired output format
5264 * If it turns out that it does not make sense to generate GPU code,
5265 * then we generate CPU code instead.
5267 * The declarations of the arrays that are visible outside of the scop
5268 * are printed outside of the code generated from the schedule,
5269 * because the generated code may involve a guard around the entire code.
5271 * We first compute a schedule that respects the dependences
5272 * of the original program and select the outermost bands
5273 * of tilable dimensions that have at least one parallel loop.
5274 * If the --load-schedule is specified, then the loaded schedule
5275 * is used instead of a computed schedule.
5277 * Each of these bands B is then tiled according to "tile" sizes, resulting
5278 * in two nested bands, with a kernel marker on top
5286 * We then split off at most 2 parallel dimensions from the T band and
5287 * at most 3 parallel dimension from the P band
5300 * A filter is introduced in front of T1 that maps the domain instances
5301 * to block identifiers. Similarly, a filter is introduced in front of P1
5302 * that maps the domain instances to thread identifiers.
5304 * For each iteration of the T2 band and for each array, we compute
5305 * the array elements accessed by that iteration, construct a rectangular
5306 * box around it and shift it to the origin. The result is used
5307 * as shared memory for the array.
5309 * Copying and synchronization statements are added to this schedule tree.
5310 * In principle, these are added in front of the P1 band, but some of
5311 * them may get hoisted up to higher levels.
5313 * The entire AST is then generated from the single resulting schedule tree.
5314 * During the generation the subtrees at kernel nodes (K) are saved
5315 * aside and replaced by kernel calls. The result is printed as host code
5316 * while the saved subtrees are printed as device code.
5318 static __isl_give isl_printer
*generate(__isl_take isl_printer
*p
,
5319 struct gpu_gen
*gen
, struct ppcg_scop
*scop
,
5320 struct ppcg_options
*options
)
5322 struct gpu_prog
*prog
;
5324 isl_schedule
*schedule
;
5328 return isl_printer_free(p
);
5330 ctx
= isl_printer_get_ctx(p
);
5331 prog
= gpu_prog_alloc(ctx
, scop
);
5333 return isl_printer_free(p
);
5336 schedule
= get_schedule(gen
);
5338 any_permutable
= has_any_permutable_node(schedule
);
5339 if (any_permutable
< 0 || !any_permutable
) {
5340 if (any_permutable
< 0)
5341 p
= isl_printer_free(p
);
5343 p
= print_cpu(p
, scop
, options
);
5344 isl_schedule_free(schedule
);
5346 schedule
= map_to_device(gen
, schedule
);
5347 gen
->tree
= generate_code(gen
, schedule
);
5348 p
= ppcg_set_macro_names(p
);
5349 p
= ppcg_print_exposed_declarations(p
, prog
->scop
);
5350 p
= gen
->print(p
, gen
->prog
, gen
->tree
, &gen
->types
,
5352 isl_ast_node_free(gen
->tree
);
5355 gpu_prog_free(prog
);
5360 /* Wrapper around generate for use as a ppcg_transform callback.
5362 static __isl_give isl_printer
*generate_wrap(__isl_take isl_printer
*p
,
5363 struct ppcg_scop
*scop
, void *user
)
5365 struct gpu_gen
*gen
= user
;
5367 return generate(p
, gen
, scop
, gen
->options
);
5370 /* Transform the code in the file called "input" by replacing
5371 * all scops by corresponding GPU code and write the results to "out".
5373 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
5374 struct ppcg_options
*options
,
5375 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
5376 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
5377 struct gpu_types
*types
, void *user
), void *user
)
5384 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
5385 gen
.options
= options
;
5388 gen
.print_user
= user
;
5390 gen
.types
.name
= NULL
;
5392 if (options
->debug
->dump_sizes
) {
5393 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
5394 gen
.used_sizes
= isl_union_map_empty(space
);
5397 r
= ppcg_transform(ctx
, input
, out
, options
, &generate_wrap
, &gen
);
5399 if (options
->debug
->dump_sizes
) {
5400 isl_union_map_dump(gen
.used_sizes
);
5401 isl_union_map_free(gen
.used_sizes
);
5404 isl_union_map_free(gen
.sizes
);
5405 for (i
= 0; i
< gen
.types
.n
; ++i
)
5406 free(gen
.types
.name
[i
]);
5407 free(gen
.types
.name
);
5412 /* Compute the set of inner array elements that may have their values
5413 * preserved by "prog". In particular, collect the array elements of
5414 * arrays that are not local to "prog" and remove those elements that
5415 * are definitely killed or definitely written by "prog".
5417 static __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
)
5420 isl_union_set
*may_persist
, *killed
;
5421 isl_union_map
*must_kill
;
5423 may_persist
= isl_union_set_empty(isl_set_get_space(prog
->context
));
5424 for (i
= 0; i
< prog
->n_array
; ++i
) {
5427 if (prog
->array
[i
].local
)
5430 extent
= isl_set_copy(prog
->array
[i
].extent
);
5431 may_persist
= isl_union_set_add_set(may_persist
, extent
);
5434 may_persist
= isl_union_set_intersect_params(may_persist
,
5435 isl_set_copy(prog
->context
));
5436 may_persist
= isl_union_set_apply(may_persist
,
5437 isl_union_map_copy(prog
->to_inner
));
5438 must_kill
= isl_union_map_copy(prog
->tagged_must_kill
);
5439 killed
= isl_union_map_range(must_kill
);
5440 must_kill
= isl_union_map_copy(prog
->must_write
);
5441 killed
= isl_union_set_union(killed
, isl_union_map_range(must_kill
));
5443 may_persist
= isl_union_set_subtract(may_persist
, killed
);
5447 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
5449 struct gpu_prog
*prog
;
5456 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
5461 prog
->context
= isl_set_copy(scop
->context
);
5462 prog
->n_stmts
= scop
->pet
->n_stmt
;
5463 prog
->any_to_outer
= pet_scop_compute_outer_to_any(scop
->pet
);
5464 prog
->any_to_outer
= isl_union_map_reverse(prog
->any_to_outer
);
5465 space
= isl_union_map_get_space(prog
->any_to_outer
);
5466 space
= isl_space_set_from_params(space
);
5467 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
5468 space
= isl_space_map_from_set(space
);
5469 id
= isl_map_identity(space
);
5470 prog
->any_to_outer
= isl_union_map_add_map(prog
->any_to_outer
, id
);
5471 prog
->stmts
= extract_stmts(ctx
, scop
,
5472 prog
->context
, prog
->any_to_outer
);
5473 prog
->read
= isl_union_map_copy(scop
->reads
);
5474 prog
->may_write
= isl_union_map_copy(scop
->may_writes
);
5475 prog
->must_write
= isl_union_map_copy(scop
->must_writes
);
5476 prog
->tagged_must_kill
= isl_union_map_copy(scop
->tagged_must_kills
);
5477 prog
->to_inner
= pet_scop_compute_outer_to_inner(scop
->pet
);
5478 prog
->to_outer
= isl_union_map_copy(prog
->to_inner
);
5479 prog
->to_outer
= isl_union_map_reverse(prog
->to_outer
);
5482 return gpu_prog_free(prog
);
5484 if (collect_array_info(prog
) < 0)
5485 return gpu_prog_free(prog
);
5486 prog
->may_persist
= compute_may_persist(prog
);
5491 void *gpu_prog_free(struct gpu_prog
*prog
)
5495 free_array_info(prog
);
5496 free_stmts(prog
->stmts
, prog
->n_stmts
);
5497 isl_union_map_free(prog
->any_to_outer
);
5498 isl_union_map_free(prog
->to_outer
);
5499 isl_union_map_free(prog
->to_inner
);
5500 isl_union_map_free(prog
->read
);
5501 isl_union_map_free(prog
->may_write
);
5502 isl_union_map_free(prog
->must_write
);
5503 isl_union_map_free(prog
->tagged_must_kill
);
5504 isl_union_map_free(prog
->array_order
);
5505 isl_union_set_free(prog
->may_persist
);
5506 isl_set_free(prog
->context
);