2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl/polynomial.h>
19 #include <isl/union_set.h>
23 #include <isl/schedule.h>
24 #include <isl/schedule_node.h>
25 #include <isl/options.h>
26 #include <isl/ast_build.h>
30 #include "gpu_array_tile.h"
31 #include "gpu_group.h"
32 #include "gpu_hybrid.h"
36 #include "ppcg_options.h"
40 struct gpu_array_info
;
42 /* Return the name of the outer array (of structs) accessed by "access".
44 static const char *get_outer_array_name(__isl_keep isl_map
*access
)
49 space
= isl_space_range(isl_map_get_space(access
));
50 while (space
&& isl_space_is_wrapping(space
))
51 space
= isl_space_domain(isl_space_unwrap(space
));
52 name
= isl_space_get_tuple_name(space
, isl_dim_set
);
53 isl_space_free(space
);
58 /* Collect all references to the given array and store pointers to them
61 static void collect_references(struct gpu_prog
*prog
,
62 struct gpu_array_info
*array
)
68 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
69 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
70 struct gpu_stmt_access
*access
;
72 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
74 name
= get_outer_array_name(access
->access
);
75 if (name
&& !strcmp(array
->name
, name
))
81 array
->refs
= isl_alloc_array(prog
->ctx
, struct gpu_stmt_access
*, n
);
85 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
86 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
87 struct gpu_stmt_access
*access
;
89 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
91 name
= get_outer_array_name(access
->access
);
92 if (!name
|| strcmp(array
->name
, name
))
95 array
->refs
[n
++] = access
;
100 /* Compute and return the extent of "array", taking into account the set of
103 * In particular, the extent in the outer dimension is taken
104 * from "accessed", while the extents in the remaining dimensions
105 * are taken from array->extent.
107 * The extent in the outer dimension cannot be taken from array->extent
108 * because that may be unbounded. Furthermore, even if it is bounded,
109 * it may be larger than the piece of the array that is being accessed.
111 static __isl_give isl_set
*compute_extent(struct pet_array
*array
,
112 __isl_keep isl_set
*accessed
)
119 extent
= isl_set_copy(array
->extent
);
121 n_index
= isl_set_dim(accessed
, isl_dim_set
);
125 extent
= isl_set_project_out(extent
, isl_dim_set
, 0, 1);
126 outer
= isl_set_copy(accessed
);
127 outer
= isl_set_project_out(outer
, isl_dim_set
, 1, n_index
- 1);
128 extent
= isl_set_flat_product(outer
, extent
);
129 id
= isl_set_get_tuple_id(accessed
);
130 extent
= isl_set_set_tuple_id(extent
, id
);
135 /* Is the array "array" being extracted a read-only scalar?
137 * That is, is "array" a scalar that is never possibly written to.
138 * An array containing structures is never considered to be a scalar.
140 static int is_read_only_scalar(struct gpu_array_info
*array
,
141 struct gpu_prog
*prog
)
144 isl_union_map
*write
;
147 if (array
->has_compound_element
)
149 if (array
->n_index
!= 0)
152 write
= isl_union_map_copy(prog
->may_write
);
153 space
= isl_set_universe(isl_space_copy(array
->space
));
154 write
= isl_union_map_intersect_range(write
,
155 isl_union_set_from_set(space
));
156 empty
= isl_union_map_is_empty(write
);
157 isl_union_map_free(write
);
162 /* Is "array" only accessed as individual, fixed elements?
163 * That is, does each access to "array" access a single, fixed element?
165 static isl_bool
only_fixed_element_accessed(struct gpu_array_info
*array
)
169 for (i
= 0; i
< array
->n_ref
; ++i
)
170 if (!array
->refs
[i
]->fixed_element
)
171 return isl_bool_false
;
173 return isl_bool_true
;
176 /* Compute bounds on the host array "pa" based on the corresponding
177 * accessed elements in "arrays"
178 * and collect all references to the array.
179 * Store the results in "info".
181 * If the array is zero-dimensional and does not contain structures,
182 * i.e., if the array is a scalar, we check whether it is read-only.
183 * We also check whether the array is accessed at all.
185 static int extract_array_info(struct gpu_prog
*prog
,
186 struct gpu_array_info
*info
, struct pet_array
*pa
,
187 __isl_keep isl_union_set
*arrays
)
192 isl_multi_pw_aff
*bounds
;
193 isl_set
*accessed
, *extent
;
195 n_index
= isl_set_dim(pa
->extent
, isl_dim_set
);
196 name
= isl_set_get_tuple_name(pa
->extent
);
198 info
->space
= isl_set_get_space(pa
->extent
);
199 info
->name
= strdup(name
);
200 info
->n_index
= n_index
;
201 info
->linearize
= prog
->scop
->options
->linearize_device_arrays
;
203 info
->type
= strdup(pa
->element_type
);
204 info
->size
= pa
->element_size
;
205 info
->local
= pa
->declared
&& !pa
->exposed
;
206 info
->has_compound_element
= pa
->element_is_record
;
207 info
->read_only_scalar
= is_read_only_scalar(info
, prog
);
209 info
->declared_extent
= isl_set_copy(pa
->extent
);
210 accessed
= isl_union_set_extract_set(arrays
,
211 isl_space_copy(info
->space
));
212 empty
= isl_set_is_empty(accessed
);
213 extent
= compute_extent(pa
, accessed
);
214 isl_set_free(accessed
);
215 info
->extent
= extent
;
218 info
->accessed
= !empty
;
219 bounds
= ppcg_size_from_extent(isl_set_copy(extent
));
220 bounds
= isl_multi_pw_aff_gist(bounds
, isl_set_copy(prog
->context
));
223 if (!isl_multi_pw_aff_is_cst(bounds
))
225 info
->bound
= bounds
;
227 collect_references(prog
, info
);
228 info
->only_fixed_element
= only_fixed_element_accessed(info
);
233 /* Remove independence from the order constraints "order" on array "array".
234 * Since the pairs of iterations in the filter relation of an independence
235 * are guaranteed to be completely independent by the user, there is
236 * no need to ensure that live ranges are ordered along those pairs.
237 * We make an exception for local variables, though, as the independence
238 * guarantee does not apply to those.
240 * The order constraints are used in two places.
241 * Those on scalars are used in check_scalar_live_ranges to check if
242 * we need to force the scalar to be private. Any non-local scalar
243 * should not be forced scalar if it only appears in independent loops.
244 * Those on non-scalars are added to the coincidence constraints
245 * in compute_schedule because we do not support any array expansion.
246 * Accesses to non-local arrays should not prevent a loop from being
247 * considered coincident so we should indeed remove those constraints
248 * from the order constraints.
250 static __isl_give isl_union_map
*remove_independences(struct gpu_prog
*prog
,
251 struct gpu_array_info
*array
, __isl_take isl_union_map
*order
)
255 for (i
= 0; i
< prog
->scop
->pet
->n_independence
; ++i
) {
256 struct pet_independence
*pi
= prog
->scop
->pet
->independences
[i
];
257 if (isl_union_set_contains(pi
->local
, array
->space
))
260 order
= isl_union_map_subtract(order
,
261 isl_union_map_copy(pi
->filter
));
267 /* For each array in "prog", store the (untagged) order dependences
268 * derived from the array in array->dep_order.
269 * In particular, consider all references that access the given array
270 * and take the order dependences that have one of these references
271 * as source. (Since an order dependence relates two references to
272 * the same array, the target of these order dependences will also
273 * be one of these references.)
274 * Additionally, store the union of these array->dep_order relations
275 * for all arrays that cannot be mapped to private memory in prog->array_order.
277 void collect_order_dependences(struct gpu_prog
*prog
)
281 isl_union_map
*accesses
;
283 space
= isl_union_map_get_space(prog
->read
);
284 prog
->array_order
= isl_union_map_empty(space
);
286 accesses
= isl_union_map_copy(prog
->scop
->tagged_reads
);
287 accesses
= isl_union_map_union(accesses
,
288 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
289 accesses
= isl_union_map_universe(accesses
);
290 accesses
= isl_union_map_apply_range(accesses
,
291 isl_union_map_copy(prog
->to_outer
));
293 for (i
= 0; i
< prog
->n_array
; ++i
) {
294 struct gpu_array_info
*array
= &prog
->array
[i
];
297 isl_union_map
*order
;
299 set
= isl_set_universe(isl_space_copy(array
->space
));
300 uset
= isl_union_set_from_set(set
);
301 uset
= isl_union_map_domain(
302 isl_union_map_intersect_range(isl_union_map_copy(accesses
),
304 order
= isl_union_map_copy(prog
->scop
->tagged_dep_order
);
305 order
= isl_union_map_intersect_domain(order
, uset
);
306 order
= isl_union_map_zip(order
);
307 order
= isl_union_set_unwrap(isl_union_map_domain(order
));
308 order
= remove_independences(prog
, array
, order
);
309 array
->dep_order
= order
;
311 if (gpu_array_can_be_private(array
))
314 prog
->array_order
= isl_union_map_union(prog
->array_order
,
315 isl_union_map_copy(array
->dep_order
));
318 isl_union_map_free(accesses
);
321 /* Construct a gpu_array_info for each array referenced by prog->scop and
322 * collect them in prog->array.
324 * The sizes are based on the extents and the set of possibly accessed
325 * elements by "prog".
326 * If there are any member accesses involved, then they are first mapped
327 * to the outer arrays of structs.
328 * Only extract gpu_array_info entries for these outer arrays.
330 * If we are allowing live range reordering, then also set
331 * the dep_order field. Otherwise leave it NULL.
333 static int collect_array_info(struct gpu_prog
*prog
)
337 isl_union_set
*arrays
;
339 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
340 arrays
= isl_union_set_union(arrays
,
341 isl_union_map_range(isl_union_map_copy(prog
->may_write
)));
343 arrays
= isl_union_set_apply(arrays
,
344 isl_union_map_copy(prog
->to_outer
));
346 arrays
= isl_union_set_coalesce(arrays
);
348 prog
->n_array
= prog
->scop
->pet
->n_array
;
349 prog
->array
= isl_calloc_array(prog
->ctx
,
350 struct gpu_array_info
, prog
->n_array
);
353 for (i
= 0; i
< prog
->scop
->pet
->n_array
; ++i
) {
356 field
= isl_set_is_wrapping(prog
->scop
->pet
->arrays
[i
]->extent
);
361 if (extract_array_info(prog
, &prog
->array
[prog
->n_array
++],
362 prog
->scop
->pet
->arrays
[i
], arrays
) < 0)
365 if (i
< prog
->scop
->pet
->n_array
)
368 isl_union_set_free(arrays
);
370 if (prog
->scop
->options
->live_range_reordering
)
371 collect_order_dependences(prog
);
376 static void free_array_info(struct gpu_prog
*prog
)
380 for (i
= 0; i
< prog
->n_array
; ++i
) {
381 free(prog
->array
[i
].type
);
382 free(prog
->array
[i
].name
);
383 isl_multi_pw_aff_free(prog
->array
[i
].bound
);
384 isl_ast_expr_free(prog
->array
[i
].bound_expr
);
385 isl_space_free(prog
->array
[i
].space
);
386 isl_set_free(prog
->array
[i
].declared_extent
);
387 isl_set_free(prog
->array
[i
].extent
);
388 isl_ast_expr_free(prog
->array
[i
].declared_size
);
389 free(prog
->array
[i
].refs
);
390 isl_union_map_free(prog
->array
[i
].dep_order
);
395 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
396 * as an array or through a pointer reference, but as a single data element.
397 * At the moment, scalars are represented as zero-dimensional arrays.
398 * Note that the single data element may be an entire structure.
400 int gpu_array_is_scalar(struct gpu_array_info
*array
)
402 return array
->n_index
== 0;
405 /* Can "array" be mapped to private memory?
406 * That is, is it only accessed as individual elements with
407 * constant index expressions?
409 isl_bool
gpu_array_can_be_private(struct gpu_array_info
*array
)
412 return isl_bool_error
;
413 return array
->only_fixed_element
;
416 /* Is "array" a read-only scalar?
418 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
420 return array
->read_only_scalar
;
423 /* Does "array" need to be allocated on the device?
424 * If it is a read-only scalar, then it will be passed as an argument
425 * to the kernel and therefore does not require any allocation.
426 * If this device memory is not accessed at all, then it does not
427 * need to be allocated either.
429 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
)
431 if (gpu_array_is_read_only_scalar(array
))
438 /* Return the set of parameter values for which the array has a positive
439 * size in all dimensions.
440 * If the sizes are only valid for some parameter values, then those
441 * constraints are also taken into account.
443 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
)
452 space
= isl_space_params(isl_space_copy(array
->space
));
453 guard
= isl_set_universe(space
);
455 for (i
= 0; i
< array
->n_index
; ++i
) {
457 isl_set
*guard_i
, *zero
;
459 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
460 guard_i
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound
));
461 zero
= isl_pw_aff_zero_set(bound
);
462 guard_i
= isl_set_subtract(guard_i
, zero
);
463 guard
= isl_set_intersect(guard
, guard_i
);
469 /* Internal data structure for extract_size_of_type.
470 * "type" specifies the name of the space that we want to extract.
471 * "res" is used to store the subset of that space.
473 struct ppcg_extract_size_data
{
478 /* This function is called for each set in a union_set.
479 * If the name of the set matches data->type, we store the
482 static isl_stat
extract_size_of_type(__isl_take isl_set
*size
, void *user
)
484 struct ppcg_extract_size_data
*data
= user
;
487 name
= isl_set_get_tuple_name(size
);
488 if (name
&& !strcmp(name
, data
->type
)) {
490 return isl_stat_error
;
497 /* Given a union map { kernel[i] -> *[...] },
498 * return the range in the space called "type" for the kernel with
499 * sequence number "id".
501 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
502 const char *type
, int id
)
506 isl_union_set
*local_sizes
;
507 struct ppcg_extract_size_data data
= { type
, NULL
};
512 space
= isl_union_map_get_space(sizes
);
513 space
= isl_space_set_from_params(space
);
514 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
515 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
516 dom
= isl_set_universe(space
);
517 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
519 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
520 isl_union_map_copy(sizes
));
521 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
522 isl_union_set_free(local_sizes
);
526 /* Given a singleton set, extract the first (at most *len) elements
527 * of the single integer tuple into *sizes and update *len if needed.
529 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
537 dim
= isl_set_dim(set
, isl_dim_set
);
541 for (i
= 0; i
< *len
; ++i
) {
544 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
547 sizes
[i
] = isl_val_get_num_si(v
);
554 /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
555 * if the option debug->dump_sizes is set.
557 static void set_used_sizes(struct gpu_gen
*gen
, const char *type
, int id
,
564 if (!gen
->options
->debug
->dump_sizes
)
567 space
= isl_union_map_get_space(gen
->used_sizes
);
568 space
= isl_space_set_from_params(space
);
569 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
570 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
571 space
= isl_space_from_domain(space
);
572 space
= isl_space_add_dims(space
, isl_dim_out
, len
);
573 space
= isl_space_set_tuple_name(space
, isl_dim_out
, type
);
575 map
= isl_map_universe(space
);
576 map
= isl_map_fix_si(map
, isl_dim_in
, 0, id
);
577 for (i
= 0; i
< len
; ++i
)
578 map
= isl_map_fix_si(map
, isl_dim_out
, i
, sizes
[i
]);
580 gen
->used_sizes
= isl_union_map_add_map(gen
->used_sizes
, map
);
583 /* Extract user specified "tile" sizes from the "sizes" command line option,
584 * defaulting to option->tile_size in each dimension.
585 * *tile_len contains the maximum number of tile sizes needed.
586 * Update *tile_len to the number of specified tile sizes, if any, and
587 * return a pointer to the tile sizes (or NULL on error).
588 * Add the effectively used sizes to gen->used_sizes.
590 static int *read_tile_sizes(struct gpu_gen
*gen
, int *tile_len
)
596 tile_size
= isl_alloc_array(gen
->ctx
, int, *tile_len
);
599 for (n
= 0; n
< *tile_len
; ++n
)
600 tile_size
[n
] = gen
->options
->tile_size
;
602 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
603 read_sizes_from_set(size
, tile_size
, tile_len
);
604 set_used_sizes(gen
, "tile", gen
->kernel_id
, tile_size
, *tile_len
);
609 /* Extract user specified "block" sizes from the "sizes" command line option,
610 * after filling in some potentially useful defaults.
612 static void read_block_sizes(struct ppcg_kernel
*kernel
,
613 __isl_keep isl_union_map
*sizes
)
617 if (kernel
->n_block
> 3)
619 switch (kernel
->n_block
) {
621 kernel
->block_dim
[0] = 512;
624 kernel
->block_dim
[0] = 32;
625 kernel
->block_dim
[1] = 16;
628 kernel
->block_dim
[0] = 32;
629 kernel
->block_dim
[1] = 4;
630 kernel
->block_dim
[2] = 4;
634 size
= extract_sizes(sizes
, "block", kernel
->id
);
635 read_sizes_from_set(size
, kernel
->block_dim
, &kernel
->n_block
);
638 /* Extract user specified "grid" sizes from the "sizes" command line option,
639 * after filling in some potentially useful defaults.
641 static void read_grid_sizes(struct ppcg_kernel
*kernel
,
642 __isl_keep isl_union_map
*sizes
)
646 if (kernel
->n_grid
> 2)
648 switch (kernel
->n_grid
) {
650 kernel
->grid_dim
[0] = 32768;
653 kernel
->grid_dim
[0] = 256;
654 kernel
->grid_dim
[1] = 256;
658 size
= extract_sizes(sizes
, "grid", kernel
->id
);
659 read_sizes_from_set(size
, kernel
->grid_dim
, &kernel
->n_grid
);
662 /* Extract user specified grid and block sizes from the gen->sizes
663 * command line option after filling in some potentially useful defaults.
664 * Store the extracted sizes in "kernel".
665 * Add the effectively used sizes to gen->used_sizes.
667 static void read_grid_and_block_sizes(struct ppcg_kernel
*kernel
,
670 read_block_sizes(kernel
, gen
->sizes
);
671 read_grid_sizes(kernel
, gen
->sizes
);
672 set_used_sizes(gen
, "block", kernel
->id
,
673 kernel
->block_dim
, kernel
->n_block
);
674 set_used_sizes(gen
, "grid", kernel
->id
,
675 kernel
->grid_dim
, kernel
->n_grid
);
678 static void *free_stmts(struct gpu_stmt
*stmts
, int n
)
685 for (i
= 0; i
< n
; ++i
) {
686 struct gpu_stmt_access
*access
, *next
;
688 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
690 isl_id_free(access
->ref_id
);
691 isl_map_free(access
->access
);
692 isl_map_free(access
->tagged_access
);
696 isl_id_free(stmts
[i
].id
);
703 /* Add parameters p[i] with identifiers "ids" to "set",
704 * with bounds to 0 <= p[i] < size[i].
706 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
707 int *size
, __isl_keep isl_id_list
*ids
)
712 len
= isl_id_list_n_id(ids
);
713 nparam
= isl_set_dim(set
, isl_dim_param
);
714 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
716 for (i
= 0; i
< len
; ++i
) {
719 id
= isl_id_list_get_id(ids
, i
);
720 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
721 set
= isl_set_lower_bound_si(set
, isl_dim_param
, nparam
+ i
, 0);
722 set
= isl_set_upper_bound_si(set
, isl_dim_param
,
723 nparam
+ i
, size
[i
] - 1);
729 /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
732 * { : 0 <= p[i] < size[i] }
734 * or an overapproximation.
736 static __isl_give isl_set
*add_bounded_parameters_dynamic(
737 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
738 __isl_keep isl_id_list
*ids
)
745 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
746 nparam
= isl_set_dim(set
, isl_dim_param
);
747 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
749 for (i
= 0; i
< len
; ++i
) {
752 id
= isl_id_list_get_id(ids
, i
);
753 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
756 space
= isl_space_params(isl_set_get_space(set
));
757 ls
= isl_local_space_from_space(space
);
758 for (i
= 0; i
< len
; ++i
) {
759 isl_pw_aff
*param
, *size_i
, *zero
;
762 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
763 isl_dim_param
, nparam
+ i
);
765 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
766 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
767 bound
= isl_set_from_basic_set(isl_set_simple_hull(bound
));
768 set
= isl_set_intersect_params(set
, bound
);
770 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
771 bound
= isl_pw_aff_ge_set(param
, zero
);
772 set
= isl_set_intersect_params(set
, bound
);
774 isl_local_space_free(ls
);
779 /* Return the union of all tagged access relations in the group.
781 static __isl_give isl_union_map
*group_tagged_access_relation(
782 struct gpu_array_ref_group
*group
)
785 isl_union_map
*access
;
787 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
788 for (i
= 0; i
< group
->n_ref
; ++i
) {
791 map_i
= isl_map_copy(group
->refs
[i
]->tagged_access
);
792 access
= isl_union_map_union(access
,
793 isl_union_map_from_map(map_i
));
799 /* Return the extent of "array", recomputed from the bounds.
800 * The recomputed extent may be simpler than the original extent.
802 static __isl_give isl_set
*array_extent(struct gpu_array_info
*array
)
810 id
= isl_set_get_tuple_id(array
->extent
);
811 space
= isl_set_get_space(array
->extent
);
812 extent
= isl_set_universe(isl_space_copy(space
));
813 ls
= isl_local_space_from_space(space
);
814 for (i
= 0; i
< array
->n_index
; ++i
) {
820 extent
= isl_set_lower_bound_si(extent
, isl_dim_set
, i
, 0);
822 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
824 index
= isl_pw_aff_from_aff(aff
);
825 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
826 bound
= isl_pw_aff_from_range(bound
);
827 bound
= isl_pw_aff_add_dims(bound
, isl_dim_in
, array
->n_index
);
828 bound
= isl_pw_aff_set_tuple_id(bound
, isl_dim_in
,
830 lt
= isl_pw_aff_lt_set(index
, bound
);
831 extent
= isl_set_intersect(extent
, lt
);
833 isl_local_space_free(ls
);
839 /* Return a map from the first group->shared_tile->depth dimensions
840 * of the computed schedule to the array tile in
841 * global memory that corresponds to the shared memory copy.
843 * In particular, return a map
849 * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
853 * 0 <= a <= array_size - 1 (2)
855 * Note that if some stride has been detected (i.e., when
856 * group->shared_tile->bound[i].shift is set), then a in (1) refers
857 * to the shifted and scaled down version.
859 * Constraints (1) are obtained by mapping the size constraints on the
860 * shared/private memory tile back to the access relation.
861 * Constraints (2) are obtained from the (recomputed) extent.
863 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
866 int n_index
= group
->array
->n_index
;
872 space
= isl_multi_aff_get_space(group
->shared_tile
->tiling
);
873 space
= isl_space_range(space
);
874 local
= isl_set_universe(space
);
875 for (i
= 0; i
< n_index
; ++i
) {
878 local
= isl_set_lower_bound_si(local
, isl_dim_set
, i
, 0);
879 bound
= isl_val_copy(group
->shared_tile
->bound
[i
].size
);
880 bound
= isl_val_sub_ui(bound
, 1);
881 local
= isl_set_upper_bound_val(local
, isl_dim_set
, i
, bound
);
883 local
= isl_set_preimage_multi_aff(local
,
884 isl_multi_aff_copy(group
->shared_tile
->tiling
));
885 tile
= isl_set_unwrap(local
);
886 extent
= array_extent(group
->array
);
887 tile
= isl_map_intersect_range(tile
, extent
);
892 /* Given a mapping "iterator_map" from the AST schedule to a domain,
893 * return the corresponding mapping from the AST schedule to
894 * to the outer kernel->copy_schedule_dim dimensions of
895 * the schedule computed by PPCG for this kernel.
897 * Note that kernel->copy_schedule_dim is at least as large as
898 * the largest depth of any array reference group associated to the kernel.
899 * This is needed as the returned schedule is used to extract a mapping
900 * to the outer tile->depth dimensions in transform_index.
902 static __isl_give isl_pw_multi_aff
*compute_sched_to_copy(
903 struct ppcg_kernel
*kernel
, __isl_take isl_pw_multi_aff
*iterator_map
)
905 isl_union_pw_multi_aff
*upma
;
906 isl_pw_multi_aff
*pma
;
909 space
= isl_space_range(isl_pw_multi_aff_get_space(iterator_map
));
910 space
= isl_space_from_domain(space
);
911 space
= isl_space_add_dims(space
, isl_dim_out
,
912 kernel
->copy_schedule_dim
);
914 upma
= isl_union_pw_multi_aff_copy(kernel
->copy_schedule
);
915 pma
= isl_union_pw_multi_aff_extract_pw_multi_aff(upma
, space
);
916 isl_union_pw_multi_aff_free(upma
);
918 return isl_pw_multi_aff_pullback_pw_multi_aff(pma
, iterator_map
);
921 /* If max_shared_memory is not set to infinity (-1), then make
922 * sure that the total amount of shared memory required by the
923 * array reference groups mapped to shared memory by "kernel"
924 * is no larger than this maximum.
926 * We apply a greedy approach and discard (keep in global memory)
927 * those groups that would result in a total memory size that
928 * is larger than the maximum.
930 * This function should be called after any function that may
931 * affect the decision on whether to place a reference group
932 * in private, shared or global memory.
934 static void check_shared_memory_bound(struct ppcg_kernel
*kernel
)
937 isl_val
*left
, *size
;
939 if (kernel
->options
->max_shared_memory
< 0)
942 left
= isl_val_int_from_si(kernel
->ctx
,
943 kernel
->options
->max_shared_memory
);
945 for (i
= 0; i
< kernel
->n_array
; ++i
) {
946 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
948 for (j
= 0; j
< local
->n_group
; ++j
) {
949 struct gpu_array_ref_group
*group
;
950 enum ppcg_group_access_type type
;
952 group
= local
->groups
[j
];
953 type
= gpu_array_ref_group_type(group
);
954 if (type
!= ppcg_access_shared
)
957 size
= gpu_array_tile_size(group
->shared_tile
);
958 size
= isl_val_mul_ui(size
, local
->array
->size
);
960 if (isl_val_le(size
, left
)) {
961 left
= isl_val_sub(left
, size
);
967 gpu_array_tile_free(group
->shared_tile
);
974 /* Mark all arrays of "kernel" that have an array reference group
975 * that is not mapped to private or shared memory as
976 * accessing the corresponding global device memory.
978 static void mark_global_arrays(struct ppcg_kernel
*kernel
)
982 for (i
= 0; i
< kernel
->n_array
; ++i
) {
983 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
987 for (j
= 0; j
< local
->n_group
; ++j
) {
988 if (gpu_array_ref_group_tile(local
->groups
[j
]))
992 local
->array
->global
= 1;
998 /* Compute a tiling for all the array reference groups in "kernel".
1000 static void compute_group_tilings(struct ppcg_kernel
*kernel
)
1004 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1005 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1007 for (j
= 0; j
< array
->n_group
; ++j
)
1008 gpu_array_ref_group_compute_tiling(array
->groups
[j
]);
1012 /* Compute the effective grid size as a list of the sizes in each dimension.
1014 * The grid size specified by the user or set by default
1015 * in read_grid_sizes() and applied by the block filter,
1016 * may be too large for the given code in the sense that
1017 * it may contain blocks that don't need to execute anything.
1018 * We therefore don't return this grid size, but instead the
1019 * smallest grid size that ensures that all blocks that actually
1020 * execute code are included in the grid.
1022 * We first extract a description of the grid, i.e., the possible values
1023 * of the block ids, from the domain elements in "domain" and
1024 * kernel->block_filter.
1025 * The block ids are parameters in kernel->block_filter.
1026 * We simply need to change them into set dimensions.
1028 * Then, for each block dimension, we compute the maximal value of the block id
1031 static __isl_give isl_multi_pw_aff
*extract_grid_size(
1032 struct ppcg_kernel
*kernel
, __isl_take isl_union_set
*domain
)
1037 isl_multi_pw_aff
*size
;
1039 domain
= isl_union_set_intersect(domain
,
1040 isl_union_set_copy(kernel
->block_filter
));
1041 grid
= isl_union_set_params(domain
);
1042 grid
= isl_set_from_params(grid
);
1043 grid
= isl_set_add_dims(grid
, isl_dim_set
, kernel
->n_grid
);
1044 for (i
= 0; i
< kernel
->n_grid
; ++i
) {
1048 id
= isl_id_list_get_id(kernel
->block_ids
, i
);
1049 pos
= isl_set_find_dim_by_id(grid
, isl_dim_param
, id
);
1052 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
1053 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
1056 grid
= isl_set_coalesce(grid
);
1057 size
= ppcg_size_from_extent(grid
);
1058 context
= isl_set_params(isl_set_copy(kernel
->context
));
1059 return isl_multi_pw_aff_gist(size
, context
);
1062 /* Compute the size of a fixed bounding box around the origin and "set",
1063 * where "set" is assumed to contain only non-negative elements,
1064 * and store the results in "size".
1065 * In particular, compute the maximal value of "set" in each direction
1068 static void extract_fixed_size(__isl_take isl_set
*set
, int *size
)
1071 isl_local_space
*ls
;
1074 n
= isl_set_dim(set
, isl_dim_set
);
1075 ls
= isl_local_space_from_space(isl_set_get_space(set
));
1076 obj
= isl_aff_zero_on_domain(ls
);
1077 for (i
= 0; i
< n
; ++i
) {
1080 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 1);
1081 max
= isl_set_max_val(set
, obj
);
1082 size
[i
] = isl_val_get_num_si(max
) + 1;
1084 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 0);
1090 /* Compute the effective block size as a list of the sizes in each dimension
1091 * and store the sizes in kernel->block_dim.
1093 * The block size specified by the user or set by default
1094 * in read_block_sizes() and applied by the thread filter,
1095 * may be too large for the given code in the sense that
1096 * it may contain threads that don't need to execute anything.
1097 * We therefore update this block size in kernel->block_dim
1098 * to the smallest block size that ensures that all threads
1099 * that actually execute code are included in the block.
1101 * The set of possible values of the thread ids is obtained from
1102 * the domain elements "domain" and kernel->thread_filter.
1103 * The current implementation eliminates all parameters, ensuring
1104 * that the size is a fixed constant in each dimension.
1105 * In principle we could also compute parametric sizes.
1106 * We would have to make sure to project out all b%d and t%d parameters,
1109 static isl_stat
extract_block_size(struct ppcg_kernel
*kernel
,
1110 __isl_take isl_union_set
*domain
)
1116 domain
= isl_union_set_intersect(domain
,
1117 isl_union_set_copy(kernel
->thread_filter
));
1118 block
= isl_union_set_params(domain
);
1119 block
= isl_set_from_params(block
);
1120 block
= isl_set_add_dims(block
, isl_dim_set
, kernel
->n_block
);
1121 for (i
= 0; i
< kernel
->n_block
; ++i
) {
1126 return isl_stat_error
;
1128 id
= isl_id_list_get_id(kernel
->thread_ids
, i
);
1129 pos
= isl_set_find_dim_by_id(block
, isl_dim_param
, id
);
1132 isl_die(isl_set_get_ctx(block
), isl_error_internal
,
1133 "missing constraints on thread identifier",
1134 block
= isl_set_free(block
));
1135 block
= isl_set_equate(block
, isl_dim_param
, pos
,
1138 nparam
= isl_set_dim(block
, isl_dim_param
);
1139 block
= isl_set_project_out(block
, isl_dim_param
, 0, nparam
);
1142 return isl_stat_error
;
1144 extract_fixed_size(block
, kernel
->block_dim
);
1149 struct ppcg_kernel
*ppcg_kernel_free(struct ppcg_kernel
*kernel
)
1156 isl_id_list_free(kernel
->block_ids
);
1157 isl_id_list_free(kernel
->thread_ids
);
1158 isl_multi_pw_aff_free(kernel
->grid_size
);
1159 isl_ast_expr_free(kernel
->grid_size_expr
);
1160 isl_set_free(kernel
->context
);
1161 isl_union_set_free(kernel
->core
);
1162 isl_union_set_free(kernel
->arrays
);
1163 isl_union_pw_multi_aff_free(kernel
->contraction
);
1164 isl_union_set_free(kernel
->expanded_domain
);
1165 isl_space_free(kernel
->space
);
1166 isl_ast_node_free(kernel
->tree
);
1167 isl_union_set_free(kernel
->block_filter
);
1168 isl_union_set_free(kernel
->thread_filter
);
1169 isl_union_pw_multi_aff_free(kernel
->copy_schedule
);
1170 isl_union_set_free(kernel
->sync_writes
);
1172 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1173 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1175 for (j
= 0; j
< array
->n_group
; ++j
)
1176 gpu_array_ref_group_free(array
->groups
[j
]);
1177 free(array
->groups
);
1179 isl_multi_pw_aff_free(array
->bound
);
1180 isl_ast_expr_free(array
->bound_expr
);
1182 free(kernel
->array
);
1184 for (i
= 0; i
< kernel
->n_var
; ++i
) {
1185 free(kernel
->var
[i
].name
);
1186 isl_vec_free(kernel
->var
[i
].size
);
1195 /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
1197 static void ppcg_kernel_free_wrap(void *user
)
1199 struct ppcg_kernel
*kernel
= user
;
1201 ppcg_kernel_free(kernel
);
1204 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
1205 struct ppcg_kernel_var
*var
)
1208 struct gpu_array_tile
*tile
;
1211 var
->array
= group
->array
;
1213 var
->type
= gpu_array_ref_group_type(group
);
1214 tile
= gpu_array_ref_group_tile(group
);
1216 p
= isl_printer_to_str(ctx
);
1217 p
= gpu_array_ref_group_print_name(group
, p
);
1218 var
->name
= isl_printer_get_str(p
);
1219 isl_printer_free(p
);
1221 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
1223 for (j
= 0; j
< group
->array
->n_index
; ++j
)
1224 var
->size
= isl_vec_set_element_val(var
->size
, j
,
1225 isl_val_copy(tile
->bound
[j
].size
));
1228 static isl_stat
create_kernel_vars(struct ppcg_kernel
*kernel
)
1233 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1234 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1236 for (j
= 0; j
< array
->n_group
; ++j
) {
1237 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1238 enum ppcg_group_access_type type
;
1240 type
= gpu_array_ref_group_type(group
);
1241 if (type
!= ppcg_access_global
)
1246 kernel
->var
= isl_calloc_array(kernel
->ctx
, struct ppcg_kernel_var
, n
);
1248 return isl_stat_error
;
1252 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1253 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1255 for (j
= 0; j
< array
->n_group
; ++j
) {
1256 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1257 enum ppcg_group_access_type type
;
1259 type
= gpu_array_ref_group_type(group
);
1260 if (type
== ppcg_access_global
)
1262 create_kernel_var(kernel
->ctx
, group
, &kernel
->var
[n
]);
1270 /* Replace "pa" by the zero function defined over the universe domain
1271 * in the space of "pa".
1273 static __isl_give isl_pw_aff
*set_universally_zero(__isl_take isl_pw_aff
*pa
)
1278 space
= isl_space_domain(isl_pw_aff_get_space(pa
));
1279 isl_pw_aff_free(pa
);
1280 zero
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1282 return isl_pw_aff_from_aff(zero
);
1285 /* The sizes of the arrays on the host that have been computed by
1286 * extract_array_info may depend on the parameters. Use the extra
1287 * constraints on the parameters that are valid at "host_domain"
1288 * to simplify these expressions and store the results in kernel->array.
1290 * We only need these localized bounds for arrays that are accessed
1291 * by the current kernel. If we have found at least one reference group
1292 * then the array is accessed by the kernel.
1294 * The resulting sizes may be functions that are nowhere defined
1295 * in case the access function cannot possibly access anything inside
1296 * the kernel for some reason. If so, they are replaced by the zero
1297 * function. Since the access function cannot actually access anything,
1298 * there is no harm in printing the array sizes as zero.
1300 static void localize_bounds(struct ppcg_kernel
*kernel
,
1301 __isl_keep isl_set
*host_domain
)
1306 context
= isl_set_copy(host_domain
);
1307 context
= isl_set_params(context
);
1309 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1310 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1311 isl_multi_pw_aff
*bound
;
1314 if (local
->n_group
== 0)
1317 n_index
= local
->array
->n_index
;
1318 bound
= isl_multi_pw_aff_copy(local
->array
->bound
);
1320 for (j
= 0; j
< n_index
; ++j
) {
1324 pwaff
= isl_multi_pw_aff_get_pw_aff(bound
, j
);
1325 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
1326 empty
= isl_pw_aff_is_empty(pwaff
);
1328 pwaff
= isl_pw_aff_free(pwaff
);
1330 pwaff
= set_universally_zero(pwaff
);
1331 bound
= isl_multi_pw_aff_set_pw_aff(bound
, j
, pwaff
);
1334 local
->n_index
= n_index
;
1335 local
->bound
= bound
;
1337 isl_set_free(context
);
1340 /* Create the array of gpu_local_array_info structures "array"
1341 * inside "kernel". The number of elements in this array is
1342 * the same as the number of arrays in "prog".
1343 * Initialize the "array" field of each local array to point
1344 * to the corresponding array in "prog".
1346 static struct ppcg_kernel
*ppcg_kernel_create_local_arrays(
1347 struct ppcg_kernel
*kernel
, struct gpu_prog
*prog
)
1355 ctx
= isl_set_get_ctx(prog
->context
);
1356 kernel
->array
= isl_calloc_array(ctx
,
1357 struct gpu_local_array_info
, prog
->n_array
);
1359 return ppcg_kernel_free(kernel
);
1360 kernel
->n_array
= prog
->n_array
;
1362 for (i
= 0; i
< prog
->n_array
; ++i
)
1363 kernel
->array
[i
].array
= &prog
->array
[i
];
1368 /* Does "kernel" need to be passed an argument corresponding to array "i"?
1370 * The argument is only needed if the kernel accesses this device memory.
1372 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
)
1374 return kernel
->array
[i
].global
;
1377 /* Find the element in gen->stmt that has the given "id".
1378 * Return NULL if no such gpu_stmt can be found.
1380 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
1384 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
1385 if (id
== prog
->stmts
[i
].id
)
1389 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
1392 void ppcg_kernel_stmt_free(void *user
)
1394 struct ppcg_kernel_stmt
*stmt
= user
;
1399 switch (stmt
->type
) {
1400 case ppcg_kernel_copy
:
1401 isl_ast_expr_free(stmt
->u
.c
.index
);
1402 isl_ast_expr_free(stmt
->u
.c
.local_index
);
1404 case ppcg_kernel_domain
:
1405 isl_id_to_ast_expr_free(stmt
->u
.d
.ref2expr
);
1407 case ppcg_kernel_sync
:
1414 /* Return the gpu_stmt_access in the list "accesses" that corresponds
1417 static struct gpu_stmt_access
*find_access(struct gpu_stmt_access
*accesses
,
1418 __isl_keep isl_id
*ref_id
)
1420 struct gpu_stmt_access
*access
;
1422 for (access
= accesses
; access
; access
= access
->next
)
1423 if (access
->ref_id
== ref_id
)
1429 /* Return the index of the array called "name" in the list of arrays.
1431 static int find_array_index(struct ppcg_kernel
*kernel
, const char *name
)
1435 for (i
= 0; i
< kernel
->n_array
; ++i
)
1436 if (!strcmp(name
, kernel
->array
[i
].array
->name
))
1442 /* Internal data structure for the index and AST expression transformation
1443 * callbacks for pet_stmt_build_ast_exprs.
1445 * "kernel" is the kernel for which are computing AST expressions and
1446 * may be NULL if we are not inside a kernel.
1447 * "accesses" is the list of gpu_stmt_access in the statement.
1448 * "iterator_map" expresses the statement iterators in terms of
1449 * the AST loop iterators.
1450 * "sched2copy" expresses the outer copy_schedule_dim dimensions of
1451 * the kernel schedule in terms of the AST loop iterators and
1452 * may be NULL if we are not inside a kernel.
1454 * The following fields are set in transform_index and used in transform_expr.
1455 * "array" is the array that is being accessed.
1456 * "global" is set if the global array is accessed (rather than
1457 * shared/private memory).
1458 * "local_array" refers to information on the array specialized
1459 * to the current kernel.
1461 struct ppcg_transform_data
{
1462 struct ppcg_kernel
*kernel
;
1463 struct gpu_stmt_access
*accesses
;
1464 isl_pw_multi_aff
*iterator_map
;
1465 isl_pw_multi_aff
*sched2copy
;
1467 struct gpu_array_info
*array
;
1469 struct gpu_local_array_info
*local_array
;
1472 /* Return a pointer to the gpu_array_ref_group in "local"
1473 * that contains the reference "access".
1474 * Return NULL if no such group can be found.
1476 static struct gpu_array_ref_group
*find_ref_group(
1477 struct gpu_local_array_info
*local
, struct gpu_stmt_access
*access
)
1481 for (i
= 0; i
< local
->n_group
; ++i
) {
1482 struct gpu_array_ref_group
*group
= local
->groups
[i
];
1484 for (j
= 0; j
< group
->n_ref
; ++j
)
1485 if (group
->refs
[j
] == access
)
1492 /* Given an index expression "index" of the form
1496 * with F(A) either A or some subfield of A and L the AST loop iterators,
1497 * and a tiling "tiling" of the form
1501 * apply the tiling to the outer array in the index expression to obtain
1505 * If F(A) is some subfield of A, then separate the member access
1506 * into the base index expression and the field index expression,
1507 * apply the tiling to the base index expression and combine the result
1508 * with the field index expression.
1510 * If F(A) is A, then modify index to keep track of the iterators
1514 * and combine the result with the tiling to obtain a tiled index expression
1515 * in terms of the AST loop iterators
1519 static __isl_give isl_multi_pw_aff
*tile_outer(
1520 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_multi_pw_aff
*tiling
)
1522 isl_bool is_wrapping
;
1524 isl_multi_pw_aff
*mpa
;
1526 is_wrapping
= isl_multi_pw_aff_range_is_wrapping(index
);
1527 if (is_wrapping
< 0)
1530 isl_multi_pw_aff
*field
;
1532 field
= isl_multi_pw_aff_copy(index
);
1533 field
= isl_multi_pw_aff_range_factor_range(field
);
1534 index
= isl_multi_pw_aff_range_factor_domain(index
);
1535 index
= tile_outer(index
, tiling
);
1536 return isl_multi_pw_aff_range_product(index
, field
);
1539 space
= isl_space_domain(isl_multi_pw_aff_get_space(index
));
1540 space
= isl_space_map_from_set(space
);
1541 mpa
= isl_multi_pw_aff_identity(space
);
1542 index
= isl_multi_pw_aff_range_product(mpa
, index
);
1543 index
= isl_multi_pw_aff_pullback_multi_pw_aff(tiling
, index
);
1547 isl_multi_pw_aff_free(index
);
1548 isl_multi_pw_aff_free(tiling
);
1552 /* Index transformation callback for pet_stmt_build_ast_exprs.
1554 * "index" expresses the array indices in terms of statement iterators
1556 * We first reformulate "index" in terms of the AST loop iterators.
1557 * Then we check if we are accessing the global array or
1558 * a shared/private copy. In particular, if we are not inside a kernel
1559 * then we must be accessing a global array.
1560 * In the former case, we simply return
1561 * the updated index. If "index" is an affine expression rather
1562 * than an array access, then we also return the updated index here.
1564 * If no reference groups have been computed for the array,
1565 * then we can only be accessing the global array.
1567 * Otherwise, we apply the tiling to the index.
1568 * This tiling is of the form
1572 * where D corresponds to the outer tile->depth dimensions of
1573 * the kernel schedule.
1574 * The index is of the form
1578 * We update the tiling to refer to the AST loop iterators
1582 * and combine it with the index to obtain a tiled index expression in terms
1583 * of the AST loop iterators
1587 * Note that while the tiling applies directly to an outer array.
1588 * the index may refer to some subfield of this outer array.
1589 * In such cases, the result will refer to the same subfield of the tile.
1590 * That is, an index expression of the form L -> F(A) will be transformed
1591 * into an index expression of the form L -> F(T).
1593 static __isl_give isl_multi_pw_aff
*transform_index(
1594 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*ref_id
,
1597 struct ppcg_transform_data
*data
= user
;
1598 struct gpu_stmt_access
*access
;
1599 struct gpu_array_ref_group
*group
;
1600 struct gpu_array_tile
*tile
;
1601 isl_pw_multi_aff
*iterator_map
;
1606 isl_multi_pw_aff
*tiling
;
1607 isl_pw_multi_aff
*pma
;
1608 isl_pw_multi_aff
*sched2depth
;
1612 iterator_map
= isl_pw_multi_aff_copy(data
->iterator_map
);
1613 index
= isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
1618 access
= find_access(data
->accesses
, ref_id
);
1621 if (!isl_map_has_tuple_name(access
->access
, isl_dim_out
))
1624 name
= get_outer_array_name(access
->access
);
1626 return isl_multi_pw_aff_free(index
);
1627 i
= find_array_index(data
->kernel
, name
);
1629 isl_die(isl_multi_pw_aff_get_ctx(index
), isl_error_internal
,
1630 "cannot find array",
1631 return isl_multi_pw_aff_free(index
));
1632 data
->local_array
= &data
->kernel
->array
[i
];
1633 data
->array
= data
->local_array
->array
;
1635 group
= find_ref_group(data
->local_array
, access
);
1641 tile
= gpu_array_ref_group_tile(group
);
1642 data
->global
= !tile
;
1646 space
= isl_space_domain(isl_multi_aff_get_space(tile
->tiling
));
1647 space
= isl_space_range(isl_space_unwrap(space
));
1648 space
= isl_space_map_from_set(space
);
1649 pma
= isl_pw_multi_aff_identity(space
);
1650 sched2depth
= isl_pw_multi_aff_copy(data
->sched2copy
);
1651 dim
= isl_pw_multi_aff_dim(sched2depth
, isl_dim_out
);
1652 sched2depth
= isl_pw_multi_aff_drop_dims(sched2depth
, isl_dim_out
,
1653 tile
->depth
, dim
- tile
->depth
);
1654 pma
= isl_pw_multi_aff_product(sched2depth
, pma
);
1655 tiling
= isl_multi_pw_aff_from_multi_aff(
1656 isl_multi_aff_copy(tile
->tiling
));
1657 tiling
= isl_multi_pw_aff_pullback_pw_multi_aff(tiling
, pma
);
1659 index
= tile_outer(index
, tiling
);
1664 /* Dereference "expr" by adding an index [0].
1665 * The original "expr" is assumed not to have any indices.
1667 * If "expr" is a member access, then the dereferencing needs
1668 * to be applied to the structure argument of this member access.
1670 static __isl_give isl_ast_expr
*dereference(__isl_take isl_ast_expr
*expr
)
1673 isl_ast_expr
*arg0
, *res
;
1674 isl_ast_expr_list
*list
;
1676 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1678 return isl_ast_expr_free(expr
);
1679 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1680 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1683 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1684 arg
= dereference(arg
);
1685 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1686 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1690 isl_ast_expr_free(arg0
);
1692 ctx
= isl_ast_expr_get_ctx(expr
);
1693 res
= isl_ast_expr_from_val(isl_val_zero(ctx
));
1694 list
= isl_ast_expr_list_from_ast_expr(res
);
1695 res
= isl_ast_expr_get_op_arg(expr
, 0);
1696 res
= isl_ast_expr_access(res
, list
);
1697 isl_ast_expr_free(expr
);
1702 /* Linearize the index expression "expr" based on the array bounds
1705 * That is, transform expression
1707 * A[i_0][i_1]...[i_n]
1711 * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
1713 * where b_0, b_1, ..., b_n are the bounds on the array.
1715 * If the base of "expr" is a member access, then the linearization needs
1716 * to be applied to the structure argument of this member access.
1718 * In the base case, if "expr" has no arguments (other than the name of
1719 * the array), then we are passing an entire array to a function.
1720 * In this case, there is nothing to linearize.
1721 * Note that at this point an expression with no arguments can
1722 * only be an entire array because the scalar case and
1723 * the case of single struct are handled by the caller.
1725 * If the number of specified index expressions in "expr"
1726 * is smaller than the dimension of the accessed array,
1727 * then the missing i_j also do not appear in the linearized expression.
1728 * Furthermore, since such an expression does not refer to a single
1729 * element while the default linearized expression would refer to
1730 * a single element, we return the expression
1732 * A + (..((i_0 * b_1 + i_1) ... ) * b_l + i_l)
1734 * instead. Note that because of the special case handling above,
1735 * we can assume here that there is at least one index expression.
1737 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
1738 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
)
1743 isl_ast_expr_list
*list
;
1745 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1746 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1747 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1750 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1751 arg
= gpu_local_array_info_linearize_index(array
, arg
);
1752 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1753 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1757 isl_ast_expr_free(arg0
);
1759 if (isl_ast_expr_get_op_n_arg(expr
) == 1)
1762 n
= isl_ast_expr_get_op_n_arg(expr
);
1763 res
= isl_ast_expr_get_op_arg(expr
, 1);
1764 for (i
= 1; i
< array
->n_index
; ++i
) {
1765 isl_ast_expr
*expr_i
;
1767 expr_i
= isl_ast_expr_get_op_arg(array
->bound_expr
, 1 + i
);
1768 res
= isl_ast_expr_mul(res
, expr_i
);
1772 expr_i
= isl_ast_expr_get_op_arg(expr
, i
+ 1);
1773 res
= isl_ast_expr_add(res
, expr_i
);
1776 if (1 + array
->n_index
> n
) {
1777 res
= isl_ast_expr_add(isl_ast_expr_get_op_arg(expr
, 0), res
);
1779 list
= isl_ast_expr_list_from_ast_expr(res
);
1780 res
= isl_ast_expr_get_op_arg(expr
, 0);
1781 res
= isl_ast_expr_access(res
, list
);
1784 isl_ast_expr_free(expr
);
1789 /* AST expression transformation callback for pet_stmt_build_ast_exprs.
1791 * If the AST expression refers to an array that is not accessed
1792 * at all, then this means the value of the expression is not used,
1793 * so we might as well print zero (NULL pointer) instead.
1795 * If the AST expression refers to a global scalar that is not
1796 * a read-only scalar, then its address was passed to the kernel and
1797 * we need to dereference it.
1799 * If the AST expression refers to an access to a global array,
1800 * then we linearize the access exploiting the bounds in data->local_array.
1802 static __isl_give isl_ast_expr
*transform_expr(__isl_take isl_ast_expr
*expr
,
1803 __isl_keep isl_id
*id
, void *user
)
1805 struct ppcg_transform_data
*data
= user
;
1809 if (!data
->array
->accessed
) {
1812 ctx
= isl_ast_expr_get_ctx(expr
);
1813 isl_ast_expr_free(expr
);
1814 return isl_ast_expr_from_val(isl_val_zero(ctx
));
1816 if (gpu_array_is_read_only_scalar(data
->array
))
1820 if (data
->array
->n_index
== 0)
1821 return dereference(expr
);
1822 if (!data
->array
->linearize
)
1825 return gpu_local_array_info_linearize_index(data
->local_array
, expr
);
1828 /* This function is called for each instance of a user statement
1829 * in the kernel "kernel", identified by "gpu_stmt".
1830 * "kernel" may be NULL if we are not inside a kernel.
1832 * We attach a struct ppcg_kernel_stmt to the "node", containing
1833 * a computed AST expression for each access, through an annotation
1835 * These AST expressions are computed from iterator_map,
1836 * which expresses the domain
1837 * elements in terms of the generated loops, and sched2copy,
1838 * which expresses the outer copy_schedule_dim dimensions of
1839 * the kernel schedule computed by PPCG in terms of the generated loops.
1841 static __isl_give isl_ast_node
*create_domain_leaf(
1842 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1843 __isl_keep isl_ast_build
*build
, struct gpu_stmt
*gpu_stmt
)
1845 struct ppcg_transform_data data
;
1846 struct ppcg_kernel_stmt
*stmt
;
1849 isl_pw_multi_aff
*sched2copy
;
1851 isl_pw_multi_aff
*iterator_map
;
1852 isl_union_map
*schedule
;
1856 ctx
= isl_ast_node_get_ctx(node
);
1858 stmt
= isl_calloc_type(ctx
, struct ppcg_kernel_stmt
);
1860 return isl_ast_node_free(node
);
1862 schedule
= isl_ast_build_get_schedule(build
);
1863 map
= isl_map_reverse(isl_map_from_union_map(schedule
));
1864 iterator_map
= isl_pw_multi_aff_from_map(map
);
1866 sched2copy
= compute_sched_to_copy(kernel
,
1867 isl_pw_multi_aff_copy(iterator_map
));
1871 stmt
->type
= ppcg_kernel_domain
;
1872 stmt
->u
.d
.stmt
= gpu_stmt
;
1874 data
.kernel
= kernel
;
1875 data
.accesses
= stmt
->u
.d
.stmt
->accesses
;
1876 data
.iterator_map
= iterator_map
;
1877 data
.sched2copy
= sched2copy
;
1878 stmt
->u
.d
.ref2expr
= pet_stmt_build_ast_exprs(stmt
->u
.d
.stmt
->stmt
,
1879 build
, &transform_index
, &data
,
1880 &transform_expr
, &data
);
1882 isl_pw_multi_aff_free(iterator_map
);
1883 isl_pw_multi_aff_free(sched2copy
);
1885 id
= isl_id_alloc(ctx
, "user", stmt
);
1886 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1888 ppcg_kernel_stmt_free(stmt
);
1889 return isl_ast_node_set_annotation(node
, id
);
1892 /* This function is called for each statement node in the AST
1893 * for copying to or from shared/private memory.
1894 * Attach a pointer to a ppcg_kernel_stmt representing the copy
1895 * statement to the node.
1896 * The statement name is "read" or "write", depending on whether we are
1897 * reading from global memory or writing to global memory.
1899 * The schedule is of the form
1903 * where D corresponds to the outer tile->depth dimensions of
1904 * the kernel schedule, A to the global array and L to the outer
1905 * generated AST schedule.
1906 * We compute the inverse and strip off the type, resulting in
1910 * We combine this mapping with on the one hand the projection
1914 * and on the other hand the group tiling
1922 * and store the corresponding expressions in stmt->index and stmt->local_index,
1923 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
1924 * stmt->index is linearized if the global memory array is linearized.
1926 static __isl_give isl_ast_node
*create_access_leaf(struct ppcg_kernel
*kernel
,
1927 struct gpu_array_ref_group
*group
, __isl_take isl_ast_node
*node
,
1928 __isl_keep isl_ast_build
*build
)
1930 struct ppcg_kernel_stmt
*stmt
;
1931 struct gpu_array_tile
*tile
;
1936 isl_pw_multi_aff
*pma
, *pma2
;
1939 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1941 return isl_ast_node_free(node
);
1943 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
1944 type
= isl_map_get_tuple_name(access
, isl_dim_in
);
1945 stmt
->u
.c
.read
= type
&& !strcmp(type
, "read");
1946 access
= isl_map_reverse(access
);
1947 pma
= isl_pw_multi_aff_from_map(access
);
1948 pma
= isl_pw_multi_aff_reset_tuple_id(pma
, isl_dim_out
);
1950 space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
1951 space
= isl_space_unwrap(space
);
1952 pma2
= isl_pw_multi_aff_range_map(space
);
1953 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
,
1954 isl_pw_multi_aff_copy(pma
));
1955 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1956 if (group
->array
->linearize
)
1957 expr
= gpu_local_array_info_linearize_index(group
->local_array
,
1959 stmt
->u
.c
.index
= expr
;
1961 tile
= gpu_array_ref_group_tile(group
);
1962 pma2
= isl_pw_multi_aff_from_multi_aff(
1963 isl_multi_aff_copy(tile
->tiling
));
1964 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
, pma
);
1965 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1966 stmt
->u
.c
.local_index
= expr
;
1968 stmt
->u
.c
.array
= group
->array
;
1969 stmt
->u
.c
.local_array
= group
->local_array
;
1970 stmt
->type
= ppcg_kernel_copy
;
1972 id
= isl_id_alloc(kernel
->ctx
, "copy", stmt
);
1973 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1975 ppcg_kernel_stmt_free(stmt
);
1976 return isl_ast_node_set_annotation(node
, id
);
1979 /* Create a synchronization ppcg_kernel_stmt and
1980 * attach it to the node "node" representing the synchronization.
1982 static __isl_give isl_ast_node
*create_sync_leaf(
1983 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1984 __isl_keep isl_ast_build
*build
)
1986 struct ppcg_kernel_stmt
*stmt
;
1989 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1991 return isl_ast_node_free(node
);
1993 stmt
->type
= ppcg_kernel_sync
;
1994 id
= isl_id_alloc(kernel
->ctx
, "sync", stmt
);
1995 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1997 ppcg_kernel_stmt_free(stmt
);
1998 return isl_ast_node_set_annotation(node
, id
);
2001 /* Build AST expressions for the device array sizes of all arrays in "prog"
2002 * that require allocation on the device using "build", as well as
2003 * for the original array sizes of all arrays that need to be declared
2005 * "node" is freed in case of error.
2007 static __isl_give isl_ast_node
*build_array_bounds(
2008 __isl_take isl_ast_node
*node
, struct gpu_prog
*prog
,
2009 __isl_keep isl_ast_build
*build
)
2013 for (i
= 0; i
< prog
->n_array
; ++i
) {
2014 struct gpu_array_info
*array
= &prog
->array
[i
];
2015 isl_multi_pw_aff
*size
;
2018 if (!gpu_array_requires_device_allocation(array
))
2021 size
= isl_multi_pw_aff_copy(array
->bound
);
2022 expr
= ppcg_build_size_expr(size
, build
);
2023 array
->bound_expr
= expr
;
2025 return isl_ast_node_free(node
);
2028 for (i
= 0; i
< prog
->n_array
; ++i
) {
2029 struct gpu_array_info
*array
= &prog
->array
[i
];
2031 isl_multi_pw_aff
*size
;
2034 if (!array
->declare_local
)
2036 extent
= isl_set_copy(array
->declared_extent
);
2037 size
= ppcg_size_from_extent(extent
);
2038 expr
= ppcg_build_size_expr(size
, build
);
2039 array
->declared_size
= expr
;
2041 return isl_ast_node_free(node
);
2047 /* Internal data structure for at_domain.
2049 * "prog" represents the entire scop.
2050 * "kernel" points to the kernel to which the current schedule node
2051 * belongs. It is set by before_mark and reset by after_mark.
2052 * It may be NULL if we are outside any kernel.
2054 struct ppcg_at_domain_data
{
2055 struct gpu_prog
*prog
;
2056 struct ppcg_kernel
*kernel
;
2059 /* This function is called for each instance of a user statement
2060 * in the kernel. This may be one of the original user statements
2061 * or a statement introduced by PPCG.
2063 * We first check if the statement id corresponds to a gpu statement,
2064 * which indicates the statement is an original user statement. Any statement
2065 * that is not an original user statement has been introduced by PPCG and
2066 * requires special handling.
2068 * If the user statement is one of the original user statements, then we call
2069 * create_domain_leaf. If it is "init_device", then we call
2070 * build_array_bounds. Otherwise, we check if it is a copy or synchronization
2071 * statement and call the appropriate functions. Statements that copy an array
2072 * to/from the device do not need any further treatment.
2073 * Neither does "clear_device".
2075 static __isl_give isl_ast_node
*at_domain(__isl_take isl_ast_node
*node
,
2076 __isl_keep isl_ast_build
*build
, void *user
)
2078 struct ppcg_at_domain_data
*data
= user
;
2079 struct gpu_stmt
*gpu_stmt
;
2080 isl_ast_expr
*expr
, *arg
;
2086 expr
= isl_ast_node_user_get_expr(node
);
2087 arg
= isl_ast_expr_get_op_arg(expr
, 0);
2088 id
= isl_ast_expr_get_id(arg
);
2089 name
= isl_id_get_name(id
);
2090 p
= isl_id_get_user(id
);
2091 isl_ast_expr_free(expr
);
2092 isl_ast_expr_free(arg
);
2094 gpu_stmt
= find_stmt(data
->prog
, id
);
2095 is_sync
= gpu_tree_id_is_sync(id
, data
->kernel
);
2099 return create_domain_leaf(data
->kernel
, node
, build
, gpu_stmt
);
2101 if (!prefixcmp(name
, "to_device_") || !prefixcmp(name
, "from_device_"))
2103 if (!strcmp(name
, "init_device"))
2104 return build_array_bounds(node
, data
->prog
, build
);
2105 if (!strcmp(name
, "clear_device"))
2108 return isl_ast_node_free(node
);
2109 if (!strcmp(name
, "read") || !strcmp(name
, "write")) {
2110 struct gpu_array_ref_group
*group
= p
;
2111 return create_access_leaf(data
->kernel
, group
, node
, build
);
2114 isl_die(data
->prog
->ctx
, isl_error_internal
,
2115 "unknown statement type",
2116 return isl_ast_node_free(node
));
2117 return create_sync_leaf(data
->kernel
, node
, build
);
2120 /* Given a set of wrapped references "ref", return the corresponding
2121 * access relations based on the tagged access relations "tagged".
2123 * The elements of "ref" are of the form
2127 * with D an iteration domains and R a reference.
2128 * The elements of "tagged" are of the form
2134 * Extend "tagged" to include the iteration domain in the range, i.e.,
2136 * [D -> R] -> [D -> A]
2138 * apply the result to "ref" and then unwrap the resulting set
2139 * to obtain relations of the form
2143 static __isl_give isl_union_map
*wrapped_reference_to_access(
2144 __isl_take isl_union_set
*ref
, __isl_take isl_union_map
*tagged
)
2146 isl_union_map
*tag2access
;
2148 tag2access
= isl_union_map_copy(tagged
);
2149 tag2access
= isl_union_map_universe(tag2access
);
2150 tag2access
= isl_union_set_unwrap(isl_union_map_domain(tag2access
));
2151 tag2access
= isl_union_map_domain_map(tag2access
);
2152 tag2access
= isl_union_map_range_product(tag2access
, tagged
);
2154 ref
= isl_union_set_coalesce(ref
);
2155 ref
= isl_union_set_apply(ref
, tag2access
);
2157 return isl_union_set_unwrap(ref
);
2160 /* Given an access relation "access" from one or more array reference groups,
2161 * remove those reads if ("read" is 1) or writes (if "read" is 0)
2162 * that are only needed to communicate data within
2163 * the same iteration of "sched".
2164 * The domain of "sched" corresponds to the original statement instances,
2165 * i.e., those that appear in the domains of the access relations.
2166 * "tagged" contains all tagged access relations to all
2167 * the array reference groups accessed by "access" from statement
2168 * instances scheduled by "sched".
2170 * If the access is a read then it is either an element of
2172 * live_in union (range flow)
2174 * where live_in and flow may be overapproximations, or
2175 * it reads an uninitialized value (that is not live-in because
2176 * there is an intermediate kill) or it reads a value that was
2177 * written within the same (compound) statement instance.
2178 * If the access is a write then it is either an element of
2180 * live_out union (domain flow)
2182 * or it writes a value that is never read (and is not live-out
2183 * because of an intermediate kill) or only
2184 * within the same (compound) statement instance.
2185 * In both cases, the access relation is also a subset of
2186 * the group access relation.
2188 * The cases where an uninitialized value is read or a value is written
2189 * that is never read or where the dataflow occurs within a statement
2190 * instance are also considered local and may also be removed.
2192 * Essentially, we compute the intersection of "access" with either
2194 * live_in union (range non-local-flow)
2198 * live_out union (domain non-local-flow)
2200 * We first construct a relation "local"
2202 * [[D -> R] -> [D' -> R']]
2204 * of pairs of domain iterations accessing the reference group
2205 * and references in the group that are coscheduled by "sched".
2207 * If this relation does not intersect the dataflow dependences,
2208 * then there is nothing we can possibly remove, unless the dataflow
2209 * dependences themselves only relate a subset of the accesses.
2210 * In particular, the accesses may not be involved in any dataflow
2211 * dependences, either because they are uninitialized reads/dead writes
2212 * or because the dataflow occurs inside a statement instance.
2214 * Since the computation below may break up the access relation
2215 * into smaller pieces, we only perform the intersection with
2216 * the non-local dependent accesses if the local pairs
2217 * intersect the dataflow dependences. Otherwise, we intersect
2218 * with the universe of the non-local dependent accesses.
2219 * This should at least remove accesses from statements that
2220 * do not participate in any dependences.
2222 * In particular, we remove the "local" dataflow dependences from
2223 * the set of all dataflow dependences, or at least those
2224 * that may contribute to a domain/range that intersects
2225 * the domain of "access".
2226 * Note that if the potential dataflow dependences are an overapproximation
2227 * of the actual dataflow dependences, then the result remains an
2228 * overapproximation of the non-local dataflow dependences.
2229 * Copying to/from global memory is only needed for the references
2230 * in the domain/range of the result or for accesses that are live out/in
2231 * for the entire scop.
2233 * We therefore map the domain/range of the "external" relation
2234 * to the corresponding access relation and take the union with
2235 * the live out/in relation.
2237 static __isl_give isl_union_map
*remove_local_accesses(
2238 struct gpu_prog
*prog
, __isl_take isl_union_map
*tagged
,
2239 __isl_take isl_union_map
*access
, __isl_take isl_union_map
*sched
,
2243 isl_union_pw_multi_aff
*tagger
;
2244 isl_union_set
*domain
, *access_domain
;
2245 isl_union_map
*local
, *external
, *universe
;
2246 isl_union_set
*tag_set
;
2248 if (isl_union_map_is_empty(access
)) {
2249 isl_union_map_free(sched
);
2250 isl_union_map_free(tagged
);
2254 tagger
= isl_union_pw_multi_aff_copy(prog
->scop
->tagger
);
2255 domain
= isl_union_map_domain(isl_union_map_copy(tagged
));
2256 tagger
= isl_union_pw_multi_aff_intersect_domain(tagger
,
2257 isl_union_set_copy(domain
));
2258 sched
= isl_union_map_preimage_domain_union_pw_multi_aff(sched
, tagger
);
2260 local
= isl_union_map_apply_range(sched
,
2261 isl_union_map_reverse(isl_union_map_copy(sched
)));
2262 local
= isl_union_map_intersect(local
,
2263 isl_union_map_copy(prog
->scop
->tagged_dep_flow
));
2265 empty
= isl_union_map_is_empty(local
);
2267 external
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
2268 universe
= isl_union_map_universe(isl_union_map_copy(access
));
2269 access_domain
= isl_union_map_domain(universe
);
2270 domain
= isl_union_set_universe(domain
);
2271 universe
= isl_union_set_unwrap(domain
);
2272 universe
= isl_union_map_intersect_domain(universe
, access_domain
);
2273 domain
= isl_union_map_wrap(universe
);
2275 external
= isl_union_map_intersect_range(external
, domain
);
2277 external
= isl_union_map_intersect_domain(external
, domain
);
2278 external
= isl_union_map_intersect_params(external
,
2279 isl_set_copy(prog
->scop
->context
));
2280 external
= isl_union_map_subtract(external
, local
);
2283 tag_set
= isl_union_map_range(external
);
2284 external
= wrapped_reference_to_access(tag_set
, tagged
);
2285 external
= isl_union_map_union(external
,
2286 isl_union_map_copy(prog
->scop
->live_in
));
2288 tag_set
= isl_union_map_domain(external
);
2289 external
= wrapped_reference_to_access(tag_set
, tagged
);
2290 external
= isl_union_map_union(external
,
2291 isl_union_map_copy(prog
->scop
->live_out
));
2295 external
= isl_union_map_free(external
);
2297 external
= isl_union_map_universe(external
);
2299 access
= isl_union_map_intersect(access
, external
);
2304 /* Given an access relation "access" from "group", remove those reads
2305 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2306 * communicate data within the same iteration of the schedule "prefix"
2307 * at the position where the copying of the group is inserted.
2308 * That is, the output dimension of "prefix"
2309 * is equal to tile->depth.
2310 * The domain of "prefix" corresponds to the original statement instances,
2311 * i.e., those that appear in the domains of the access relations.
2313 * Extract the tagged access relation of "group" and
2314 * then call remove_local_accesses.
2316 static __isl_give isl_union_map
*remove_local_accesses_group(
2317 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
2318 __isl_take isl_union_map
*access
, __isl_keep isl_union_map
*prefix
,
2321 isl_union_map
*sched
, *tagged
;
2323 if (isl_union_map_is_empty(access
))
2326 tagged
= group_tagged_access_relation(group
);
2327 sched
= isl_union_map_copy(prefix
);
2329 return remove_local_accesses(kernel
->prog
, tagged
, access
, sched
, read
);
2332 /* Build an access AST expression for the effective grid size using "build".
2333 * Store the result in kernel->grid_size_expr.
2335 static isl_stat
build_grid_size(struct ppcg_kernel
*kernel
,
2336 __isl_keep isl_ast_build
*build
)
2338 isl_multi_pw_aff
*size
;
2340 size
= isl_multi_pw_aff_copy(kernel
->grid_size
);
2341 size
= isl_multi_pw_aff_set_tuple_name(size
, isl_dim_out
, "grid");
2342 kernel
->grid_size_expr
= ppcg_build_size_expr(size
, build
);
2344 if (!kernel
->grid_size_expr
)
2345 return isl_stat_error
;
2349 /* Build access AST expressions for the localized array sizes using "build".
2350 * Store the result in local->bound_expr.
2351 * Only do this for arrays for which localized bounds have been computed.
2353 static isl_stat
build_local_array_sizes(struct ppcg_kernel
*kernel
,
2354 __isl_keep isl_ast_build
*build
)
2358 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2359 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
2360 isl_multi_pw_aff
*size
;
2362 if (local
->n_group
== 0)
2364 size
= isl_multi_pw_aff_copy(local
->bound
);
2365 local
->bound_expr
= ppcg_build_size_expr(size
, build
);
2366 if (!local
->bound_expr
)
2367 return isl_stat_error
;
2373 /* Build access AST expressions for the effective grid size and
2374 * the localized array sizes using "build".
2376 static isl_stat
build_grid_and_local_array_sizes(struct ppcg_kernel
*kernel
,
2377 __isl_keep isl_ast_build
*build
)
2379 if (build_grid_size(kernel
, build
) < 0)
2380 return isl_stat_error
;
2381 if (build_local_array_sizes(kernel
, build
) < 0)
2382 return isl_stat_error
;
2386 /* This function is called before the AST generator starts traversing
2387 * the schedule subtree of a node with mark "mark".
2389 * If the mark is called "kernel", store the kernel pointer in data->kernel
2390 * for use in at_domain and build AST expressions for the grid size and
2391 * the localized array sizes.
2393 static isl_stat
before_mark(__isl_keep isl_id
*mark
,
2394 __isl_keep isl_ast_build
*build
, void *user
)
2396 struct ppcg_at_domain_data
*data
= user
;
2399 return isl_stat_error
;
2400 if (!strcmp(isl_id_get_name(mark
), "kernel")) {
2401 data
->kernel
= isl_id_get_user(mark
);
2402 if (build_grid_and_local_array_sizes(data
->kernel
, build
) < 0)
2403 return isl_stat_error
;
2408 /* This function is called after the AST generator has finished traversing
2409 * the schedule subtree of a mark node. "node" points to the corresponding
2412 * If the mark is called "kernel", then replace "node" by a user node
2413 * that "calls" the kernel, representing the launch of the kernel.
2414 * The original "node" is stored inside the kernel object so that
2415 * it can be used to print the device code.
2416 * Note that this assumes that a kernel is only launched once.
2417 * Also clear data->kernel.
2419 static __isl_give isl_ast_node
*after_mark(__isl_take isl_ast_node
*node
,
2420 __isl_keep isl_ast_build
*build
, void *user
)
2425 isl_ast_expr_list
*list
;
2426 struct ppcg_kernel
*kernel
;
2427 struct ppcg_at_domain_data
*data
= user
;
2429 ctx
= isl_ast_node_get_ctx(node
);
2430 id
= isl_ast_node_mark_get_id(node
);
2432 return isl_ast_node_free(node
);
2433 if (strcmp(isl_id_get_name(id
), "kernel") || !data
->kernel
) {
2437 kernel
= data
->kernel
;
2438 data
->kernel
= NULL
;
2439 kernel
->space
= isl_ast_build_get_schedule_space(build
);
2440 kernel
->tree
= isl_ast_node_mark_get_node(node
);
2441 isl_ast_node_free(node
);
2443 expr
= isl_ast_expr_from_id(isl_id_copy(id
));
2444 list
= isl_ast_expr_list_alloc(ctx
, 0);
2445 expr
= isl_ast_expr_call(expr
, list
);
2446 node
= isl_ast_node_alloc_user(expr
);
2447 node
= isl_ast_node_set_annotation(node
, id
);
2452 static isl_bool
update_depth(__isl_keep isl_schedule_node
*node
, void *user
)
2457 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2458 return isl_bool_true
;
2459 node_depth
= isl_schedule_node_get_schedule_depth(node
);
2460 if (node_depth
> *depth
)
2461 *depth
= node_depth
;
2463 return isl_bool_false
;
2466 /* Use isl to generate code for both the host and the device
2468 * The device code is marked by "kernel" mark nodes in the schedule tree,
2469 * containing a pointer to a ppcg_kernel object.
2470 * The returned AST only contains the AST for the host code.
2471 * The ASTs for the device code are embedded in ppcg_kernel objects
2472 * attached to the leaf nodes that call "kernel".
2474 static __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
,
2475 __isl_take isl_schedule
*schedule
)
2477 struct ppcg_at_domain_data data
;
2478 isl_ast_build
*build
;
2480 isl_id_list
*iterators
;
2483 data
.prog
= gen
->prog
;
2487 if (isl_schedule_foreach_schedule_node_top_down(schedule
, &update_depth
,
2490 build
= isl_ast_build_alloc(gen
->prog
->ctx
);
2491 iterators
= ppcg_scop_generate_names(gen
->prog
->scop
, depth
, "c");
2492 build
= isl_ast_build_set_iterators(build
, iterators
);
2493 build
= isl_ast_build_set_at_each_domain(build
, &at_domain
, &data
);
2494 build
= isl_ast_build_set_before_each_mark(build
, &before_mark
, &data
);
2495 build
= isl_ast_build_set_after_each_mark(build
, &after_mark
, &data
);
2496 if (gen
->prog
->scop
->options
->debug
->dump_final_schedule
)
2497 isl_schedule_dump(schedule
);
2498 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2499 isl_ast_build_free(build
);
2504 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
2508 return isl_union_map_read_from_str(ctx
, str
);
2511 /* Can "node" be tiled and then mapped to block and thread identifiers?
2512 * That is, is it permutable with at least one coincident dimension?
2514 static isl_bool
is_permutable(__isl_keep isl_schedule_node
*node
)
2517 return isl_bool_error
;
2519 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
2520 return isl_bool_false
;
2521 if (!isl_schedule_node_band_get_permutable(node
))
2522 return isl_bool_false
;
2523 if (isl_schedule_node_band_n_member(node
) < 1)
2524 return isl_bool_false
;
2525 if (!isl_schedule_node_band_member_get_coincident(node
, 0))
2526 return isl_bool_false
;
2528 return isl_bool_true
;
2531 /* Is "node" not a suitably permutable band?
2533 static isl_bool
not_permutable(__isl_keep isl_schedule_node
*node
, void *user
)
2535 return isl_bool_not(is_permutable(node
));
2538 /* Does the subtree rooted at "node" have any suitably permutable band nodes?
2539 * That is, does it have any nodes that are permutable and that
2540 * have a least one coincident dimension?
2542 static isl_bool
subtree_has_permutable_bands(__isl_keep isl_schedule_node
*node
)
2544 isl_bool all_non_permutable
;
2546 all_non_permutable
= isl_schedule_node_every_descendant(node
,
2547 ¬_permutable
, NULL
);
2548 return isl_bool_not(all_non_permutable
);
2551 /* Does "schedule" contain any permutable band with at least one coincident
2554 static isl_bool
has_any_permutable_node(__isl_keep isl_schedule
*schedule
)
2556 isl_schedule_node
*root
;
2557 isl_bool any_permutable
;
2559 root
= isl_schedule_get_root(schedule
);
2560 any_permutable
= subtree_has_permutable_bands(root
);
2561 isl_schedule_node_free(root
);
2563 return any_permutable
;
2566 /* Is "node" a candidate for mapping to block and thread identifiers?
2567 * In particular, is it permutable with at least one coincident dimension?
2568 * Alternatively, does the subtree rooted at "node" not contain
2569 * any such permutable node? Filter nodes are skipped in this case,
2570 * because a band node will be inserted in front of the returned
2571 * node and this is not possible for filter nodes that are children
2572 * of set or sequence nodes.
2574 static int is_candidate(__isl_keep isl_schedule_node
*node
)
2576 isl_bool permutable
;
2578 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
)
2580 permutable
= is_permutable(node
);
2581 if (permutable
< 0 || permutable
)
2583 if (isl_schedule_node_get_type(node
) == isl_schedule_node_filter
)
2585 permutable
= subtree_has_permutable_bands(node
);
2591 /* Is "node" the outermost node in its branch that can be tiled
2592 * and then mapped to block and thread identifiers?
2593 * If there are no such nodes in the subtree at "node" and
2594 * if "node" is not a filter node, then it is accepted too.
2596 static int is_outer_tilable(__isl_keep isl_schedule_node
*node
)
2599 isl_schedule_node
*ancestor
;
2601 tilable
= is_candidate(node
);
2608 ancestor
= isl_schedule_node_copy(node
);
2609 while (isl_schedule_node_has_parent(ancestor
)) {
2610 ancestor
= isl_schedule_node_parent(ancestor
);
2612 tilable
= is_candidate(ancestor
);
2613 if (tilable
< 0 || tilable
)
2617 isl_schedule_node_free(ancestor
);
2618 return tilable
< 0 ? -1 : !tilable
;
2621 /* Collect the references to all writes in "group".
2622 * Each reference is represented by a universe set in a space
2626 * with S[i,j] the statement instance space and R[] the array reference.
2628 static __isl_give isl_union_set
*group_tagged_writes(
2629 struct gpu_array_ref_group
*group
)
2633 isl_union_set
*writes
;
2635 space
= isl_map_get_space(group
->access
);
2636 writes
= isl_union_set_empty(space
);
2637 for (i
= 0; i
< group
->n_ref
; ++i
) {
2641 if (!group
->refs
[i
]->write
)
2644 space
= isl_map_get_space(group
->refs
[i
]->tagged_access
);
2645 space
= isl_space_domain(space
);
2646 writes_i
= isl_set_universe(space
);
2647 writes
= isl_union_set_add_set(writes
, writes_i
);
2653 /* Is there any write access in "group" that requires synchronization
2654 * on a write to global memory?
2655 * We currently take into account all writes that would require
2656 * synchronization at the thread level depth, but if the copying
2657 * for this group is performed at an outer level, then we do not
2658 * actually need to take into account dependences at intermediate levels.
2660 static int any_sync_writes_in_group(struct ppcg_kernel
*kernel
,
2661 struct gpu_array_ref_group
*group
)
2663 isl_union_set
*writes
;
2664 int empty
, disjoint
;
2666 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2672 writes
= group_tagged_writes(group
);
2673 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2674 isl_union_set_free(writes
);
2676 return disjoint
< 0 ? -1 : !disjoint
;
2679 /* Collect the references to all writes in "kernel" that write directly
2680 * to global or shared memory, i.e., that are not mapped to private memory.
2681 * Each reference is represented by a universe set in a space
2685 * with S[i,j] the statement instance space and R[] the array reference.
2687 static __isl_give isl_union_set
*collect_non_private_tagged_writes(
2688 struct ppcg_kernel
*kernel
)
2690 isl_union_set
*writes
;
2693 writes
= isl_union_set_empty(isl_union_set_get_space(kernel
->arrays
));
2695 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2696 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
2698 for (j
= 0; j
< array
->n_group
; ++j
) {
2699 struct gpu_array_ref_group
*group
= array
->groups
[j
];
2700 enum ppcg_group_access_type type
;
2701 isl_union_set
*writes_ij
;
2705 type
= gpu_array_ref_group_type(group
);
2706 if (type
== ppcg_access_private
)
2708 writes_ij
= group_tagged_writes(group
);
2709 writes
= isl_union_set_union(writes
, writes_ij
);
2716 /* Are there any direct writes to global memory that require
2719 static int any_global_or_shared_sync_writes(struct ppcg_kernel
*kernel
)
2721 isl_union_set
*writes
;
2722 int empty
, disjoint
;
2724 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2730 writes
= collect_non_private_tagged_writes(kernel
);
2731 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2732 isl_union_set_free(writes
);
2734 return disjoint
< 0 ? -1 : !disjoint
;
2737 /* Construct an isl_multi_val for use as tile sizes for tiling "node"
2738 * from the elements in "tile_size".
2740 static __isl_give isl_multi_val
*construct_band_tiles_sizes(
2741 __isl_keep isl_schedule_node
*node
, int *tile_size
)
2748 space
= isl_schedule_node_band_get_space(node
);
2749 return ppcg_multi_val_from_int_list(space
, tile_size
);
2752 /* Replace the partial schedule S of the band node "node" by
2760 * if scale_tile_loops is set, with f the integers in "factor".
2761 * The list that "factor" points to is assumed to contain at least
2762 * as many elements as the number of members in the band.
2764 static __isl_give isl_schedule_node
*snap_band_to_sizes(
2765 __isl_take isl_schedule_node
*node
, int *factor
,
2766 struct ppcg_options
*options
)
2770 mv
= construct_band_tiles_sizes(node
, factor
);
2771 node
= isl_schedule_node_band_scale_down(node
, isl_multi_val_copy(mv
));
2772 if (options
->scale_tile_loops
)
2773 node
= isl_schedule_node_band_scale(node
,
2774 isl_multi_val_copy(mv
));
2775 isl_multi_val_free(mv
);
2780 /* Tile "band" with tile size specified by "sizes".
2782 * Since the tile loops will be mapped to block ids, we forcibly
2783 * turn off tile loop scaling. We may want to enable tile loop scaling
2784 * at some later point, but then we would have to support the detection
2785 * of strides during the mapping to block ids.
2786 * Similarly, since the point loops will be mapped to thread ids,
2787 * we forcibly shift the point loops so that they start at zero.
2789 static __isl_give isl_schedule_node
*tile_band(
2790 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2792 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
2796 scale_tile
= isl_options_get_tile_scale_tile_loops(ctx
);
2797 isl_options_set_tile_scale_tile_loops(ctx
, 0);
2798 shift_point
= isl_options_get_tile_shift_point_loops(ctx
);
2799 isl_options_set_tile_shift_point_loops(ctx
, 1);
2801 node
= isl_schedule_node_band_tile(node
, sizes
);
2803 isl_options_set_tile_scale_tile_loops(ctx
, scale_tile
);
2804 isl_options_set_tile_shift_point_loops(ctx
, shift_point
);
2809 /* Extract the set of parameter values and outer schedule dimensions
2810 * for which any statement instance
2811 * in the kernel inserted at "node" needs to be executed.
2812 * Intersect the set of parameter values derived from the host schedule
2813 * relation with the context of "prog".
2815 static __isl_give isl_set
*extract_context(__isl_keep isl_schedule_node
*node
,
2816 struct gpu_prog
*prog
)
2818 isl_union_map
*schedule
;
2819 isl_union_set
*schedule_domain
;
2823 schedule
= isl_schedule_node_get_prefix_schedule_relation(node
);
2824 schedule_domain
= isl_union_map_range(schedule
);
2825 empty
= isl_union_set_is_empty(schedule_domain
);
2827 isl_union_set_free(schedule_domain
);
2834 space
= isl_union_set_get_space(schedule_domain
);
2835 isl_union_set_free(schedule_domain
);
2836 space
= isl_space_set_from_params(space
);
2837 depth
= isl_schedule_node_get_schedule_depth(node
);
2838 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
2839 context
= isl_set_empty(space
);
2841 context
= isl_set_from_union_set(schedule_domain
);
2843 context
= isl_set_intersect_params(context
,
2844 isl_set_copy(prog
->context
));
2849 /* Return the set of outer array elements accessed by
2850 * by the statement instances in "domain" in "prog".
2851 * The instances in "domain" are those that appear
2852 * in the domains of the access relations in "prog".
2854 static __isl_give isl_union_set
*accessed_by_domain(
2855 __isl_take isl_union_set
*domain
, struct gpu_prog
*prog
)
2857 isl_union_map
*access
;
2858 isl_union_set
*arrays
;
2860 access
= isl_union_map_union(isl_union_map_copy(prog
->read
),
2861 isl_union_map_copy(prog
->may_write
));
2862 access
= isl_union_map_intersect_domain(access
, domain
);
2863 arrays
= isl_union_map_range(access
);
2864 arrays
= isl_union_set_apply(arrays
,
2865 isl_union_map_copy(prog
->to_outer
));
2870 /* Return the number of outer band members of the band node "node"
2871 * that are marked coincident.
2873 static int n_outer_coincidence(__isl_keep isl_schedule_node
*node
)
2877 n
= isl_schedule_node_band_n_member(node
);
2879 for (i
= 0; i
< n
; ++i
)
2880 if (!isl_schedule_node_band_member_get_coincident(node
, i
))
2886 /* If the band node "node" has more than "n" members, then split off
2887 * the first "n" of them.
2889 static __isl_give isl_schedule_node
*split_band(
2890 __isl_take isl_schedule_node
*node
, int n
)
2894 dim
= isl_schedule_node_band_n_member(node
);
2896 node
= isl_schedule_node_band_split(node
, n
);
2901 /* Scale a band node that may have been split by split_band.
2902 * "sizes" are the scaling factors for the original node.
2903 * "node" either points to the original band node, or the outer
2904 * of the two pieces after splitting.
2906 * If the number of elements in "node" is smaller than the number of
2907 * elements in "sizes", then some splitting has occurred and we split
2908 * "sizes" in the same way.
2910 static __isl_give isl_schedule_node
*scale_band(
2911 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2915 n
= isl_multi_val_dim(sizes
, isl_dim_set
);
2916 dim
= isl_schedule_node_band_n_member(node
);
2918 isl_multi_val
*sizes2
;
2920 sizes2
= isl_multi_val_copy(sizes
);
2921 sizes
= isl_multi_val_drop_dims(sizes
,
2922 isl_dim_set
, dim
, n
- dim
);
2923 sizes2
= isl_multi_val_drop_dims(sizes2
, isl_dim_set
, 0, dim
);
2924 node
= isl_schedule_node_child(node
, 0);
2925 node
= isl_schedule_node_band_scale(node
, sizes2
);
2926 node
= isl_schedule_node_parent(node
);
2929 return isl_schedule_node_band_scale(node
, sizes
);
2932 /* Return an isl_multi_aff, with as elements the parameters in "space"
2933 * that have the names specified by the elements in "names".
2934 * If (some of) these parameters do not already appear in "space",
2935 * then they are added first.
2937 static __isl_give isl_multi_aff
*parameter_vector(__isl_take isl_space
*space
,
2938 __isl_keep isl_id_list
*names
)
2941 isl_local_space
*ls
;
2945 space
= isl_space_free(space
);
2947 n
= isl_id_list_n_id(names
);
2948 for (i
= 0; i
< n
; ++i
) {
2952 id
= isl_id_list_get_id(names
, i
);
2953 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2958 pos
= isl_space_dim(space
, isl_dim_param
);
2959 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2960 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2962 ma
= isl_multi_aff_zero(isl_space_copy(space
));
2963 ls
= isl_local_space_from_space(isl_space_domain(space
));
2964 for (i
= 0; i
< n
; ++i
) {
2969 id
= isl_id_list_get_id(names
, i
);
2970 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2972 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
2973 isl_dim_param
, pos
);
2974 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2976 isl_local_space_free(ls
);
2981 /* Return constraints on the domain elements that equate a sequence of
2982 * parameters called "names", to the partial schedule
2983 * of "node" modulo the integers in "size".
2984 * The number of elements in the array "size" should be equal
2985 * to the number of elements in "names".
2986 * The number of members of the band node "node" should be smaller
2987 * than or equal to this number. If it is smaller, then the first
2988 * elements of "names" are equated to zero.
2990 static __isl_give isl_union_set
*set_schedule_modulo(
2991 __isl_keep isl_schedule_node
*node
, __isl_keep isl_id_list
*names
,
2997 isl_multi_union_pw_aff
*mupa
, *mupa2
;
2999 isl_union_set
*domain
;
3003 n
= isl_id_list_n_id(names
);
3005 return isl_schedule_node_get_universe_domain(node
);
3006 n_zero
= n
- isl_schedule_node_band_n_member(node
);
3008 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
3009 mv
= construct_band_tiles_sizes(node
, size
+ n_zero
);
3010 mupa
= isl_multi_union_pw_aff_mod_multi_val(mupa
, mv
);
3012 space
= isl_multi_union_pw_aff_get_space(mupa
);
3013 space
= isl_space_params(space
);
3014 space
= isl_space_set_from_params(space
);
3015 space
= isl_space_add_dims(space
, isl_dim_set
, n_zero
);
3016 ma
= isl_multi_aff_zero(space
);
3018 domain
= isl_schedule_node_get_universe_domain(node
);
3019 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(
3020 isl_union_set_copy(domain
), ma
);
3021 mupa
= isl_multi_union_pw_aff_range_product(mupa2
, mupa
);
3023 space
= isl_multi_union_pw_aff_get_space(mupa
);
3024 ma
= parameter_vector(space
, names
);
3026 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(domain
, ma
);
3027 mupa
= isl_multi_union_pw_aff_sub(mupa
, mupa2
);
3029 return isl_multi_union_pw_aff_zero_union_set(mupa
);
3032 /* Insert a context node at "node" introducing the block and thread
3033 * identifiers along with their bounds, which are stored in kernel->grid_size
3034 * and kernel->block_dim.
3035 * Note that the bounds on the block identifiers may implicitly impose
3036 * constraints on the parameters. A guard needs to be inserted
3037 * in the schedule tree to ensure that those bounds hold at "node".
3038 * This guard is inserted in insert_guard.
3040 static __isl_give isl_schedule_node
*insert_context(struct ppcg_kernel
*kernel
,
3041 __isl_take isl_schedule_node
*node
)
3045 context
= isl_set_universe(isl_set_get_space(kernel
->context
));
3047 context
= add_bounded_parameters_dynamic(context
,
3048 kernel
->grid_size
, kernel
->block_ids
);
3049 context
= add_bounded_parameters(context
,
3050 kernel
->block_dim
, kernel
->thread_ids
);
3052 node
= isl_schedule_node_insert_context(node
, context
);
3057 /* Insert a guard that eliminates kernel launches where the kernel
3058 * obviously does not have any work to do.
3060 * In particular, eliminate kernel launches where there are obviously
3062 * Use the same block size constraints that are used to create the context
3063 * to ensure that all constraints implicit in the constructed context
3064 * are imposed by the guard.
3066 * Additionally, add other constraints that are valid
3067 * for each executed instance ("context"), as long as this does not result
3070 static __isl_give isl_schedule_node
*insert_guard(
3071 __isl_take isl_schedule_node
*node
, __isl_keep isl_set
*context
,
3072 __isl_keep isl_multi_pw_aff
*size
, struct ppcg_scop
*scop
)
3078 guard
= isl_set_copy(context
);
3079 guard
= isl_set_compute_divs(guard
);
3080 guard
= isl_set_from_basic_set(isl_set_simple_hull(guard
));
3082 nparam
= isl_set_dim(guard
, isl_dim_param
);
3083 n
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
3084 ids
= ppcg_scop_generate_names(scop
, n
, "__ppcg_tmp");
3085 guard
= add_bounded_parameters_dynamic(guard
, size
, ids
);
3086 isl_id_list_free(ids
);
3087 guard
= isl_set_project_out(guard
, isl_dim_param
, nparam
, n
);
3089 node
= isl_schedule_node_insert_guard(node
, guard
);
3094 /* Does any array reference group mapping require the band that is mapped
3095 * to threads to be unrolled?
3097 static int kernel_requires_unroll(struct ppcg_kernel
*kernel
)
3101 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3102 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3104 for (j
= 0; j
< array
->n_group
; ++j
) {
3105 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3106 if (gpu_array_ref_group_requires_unroll(group
))
3114 /* Mark the given band node "node" for unrolling by the AST generator and
3115 * then sink it to the leaves of the schedule tree.
3116 * All dimensions of "node" are assumed to be coincident, such that this
3117 * sinking is a valid operation.
3119 static __isl_give isl_schedule_node
*unroll(__isl_take isl_schedule_node
*node
)
3121 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
3123 node
= isl_schedule_node_band_sink(node
);
3128 /* Insert a synchronization node in the schedule tree of "node"
3129 * after the core computation of "kernel" at the level of the band
3130 * that is mapped to threads, except if that level is equal to
3131 * that of the band that is mapped to blocks or if there are no writes
3132 * to global or shared memory in the core computation that require
3134 * If there are any writes to shared memory and the shared memory
3135 * copying is performed at the same level, then synchronization
3136 * is needed between the core and the copying anyway, so we might
3137 * as well add it here. If the copying is performed at a higher
3138 * level, then different iterations of intermediate schedule dimensions
3139 * may have a different mapping from between shared memory elements and
3140 * threads, such that synchronization is required after the core.
3141 * "node" is assumed to point to the kernel node.
3143 * If the shared and the thread mark point to the same node, then make
3144 * sure the synchronization is inserted outside of the shared mark.
3146 static __isl_give isl_schedule_node
*add_sync(struct ppcg_kernel
*kernel
,
3147 __isl_take isl_schedule_node
*node
)
3152 need_sync
= any_global_or_shared_sync_writes(kernel
);
3154 return isl_schedule_node_free(node
);
3158 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3159 depth
= isl_schedule_node_get_schedule_depth(node
);
3160 node
= gpu_tree_move_up_to_kernel(node
);
3161 if (depth
== isl_schedule_node_get_schedule_depth(node
))
3164 node
= gpu_tree_move_down_to_depth(node
, depth
, kernel
->core
);
3165 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3167 node
= gpu_tree_move_up_to_kernel(node
);
3172 /* Return a read ("read" is 1) or write access relation for "group"
3173 * with those accesses removed that are only needed to communicate data
3174 * within the subtree of the schedule rooted at "node".
3175 * Furthermore, include the prefix schedule at "node".
3176 * That is, return a relation of the form
3180 * with D the outer schedule dimensions at "node".
3182 static __isl_give isl_union_map
*anchored_non_local_accesses(
3183 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3184 __isl_take isl_schedule_node
*node
, int read
)
3186 isl_union_map
*access
;
3187 isl_union_map
*prefix
;
3189 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
3190 prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(prefix
,
3191 isl_union_pw_multi_aff_copy(kernel
->contraction
));
3192 access
= gpu_array_ref_group_access_relation(group
, read
, !read
);
3193 access
= remove_local_accesses_group(kernel
, group
, access
, prefix
,
3195 access
= isl_union_map_range_product(prefix
, access
);
3200 /* Given an array reference group "group", create a mapping
3202 * read[D -> A] -> [D -> A]
3204 * if "read" is set or
3206 * write[D -> A] -> [D -> A]
3208 * if "read" is not set.
3209 * D corresponds to the outer tile->depth dimensions of
3210 * the kernel schedule.
3212 static __isl_give isl_multi_aff
*create_from_access(isl_ctx
*ctx
,
3213 struct gpu_array_ref_group
*group
, int read
)
3215 struct gpu_array_tile
*tile
;
3219 tile
= gpu_array_ref_group_tile(group
);
3220 space
= isl_space_copy(group
->array
->space
);
3221 space
= isl_space_from_range(space
);
3222 space
= isl_space_add_dims(space
, isl_dim_in
, tile
->depth
);
3223 space
= isl_space_wrap(space
);
3224 space
= isl_space_map_from_set(space
);
3226 id
= isl_id_alloc(ctx
, read
? "read" : "write", group
);
3227 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
3229 return isl_multi_aff_identity(space
);
3232 /* If any writes in "group" require synchronization, then make sure
3233 * that there is a synchronization node for "kernel" after the node
3234 * following "node" in a sequence.
3236 * If "shared" is set and no synchronization is needed for
3237 * the writes to global memory, then add synchronization before
3238 * the kernel to protect shared memory from being overwritten
3239 * by the next iteration of the core computation.
3240 * No additional synchronization is needed to protect against
3241 * the next copy into shared memory because each element of
3242 * the shared memory tile is always copied by the same thread.
3244 static __isl_give isl_schedule_node
*add_group_write_sync(
3245 __isl_take isl_schedule_node
*node
, struct ppcg_kernel
*kernel
,
3246 struct gpu_array_ref_group
*group
, int shared
)
3250 need_sync
= any_sync_writes_in_group(kernel
, group
);
3252 return isl_schedule_node_free(node
);
3254 node
= isl_schedule_node_parent(node
);
3255 node
= isl_schedule_node_next_sibling(node
);
3256 node
= isl_schedule_node_child(node
, 0);
3257 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3258 } else if (shared
) {
3259 struct gpu_array_tile
*tile
;
3261 tile
= gpu_array_ref_group_tile(group
);
3262 node
= isl_schedule_node_parent(node
);
3263 node
= isl_schedule_node_parent(node
);
3264 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
,
3266 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3272 /* Add copy statements to the schedule tree of "node"
3273 * for reading from global memory to private memory (if "read" is set) or
3274 * for writing back from private memory to global memory
3275 * (if "read" is not set) for the array reference group "group" that
3276 * is mapped to private memory.
3277 * On input, "node" points to the kernel node, and it is moved
3278 * back there on output.
3280 * The copies are performed in the order of the array elements.
3281 * The copy statement instances include a reference to the outer
3282 * tile->depth dimensions of the kernel schedule for ease of
3283 * combining them with the group tiling.
3285 * That is, the extra schedule is of the form
3289 * where D corresponds to the outer tile->depth dimensions of
3290 * the kernel schedule and A to the global array.
3291 * This schedule is unrolled because registers are not addressable.
3293 * The copying is inserted in the schedule tree through an extension
3298 * where the extra domain elements type[D -> A] are those accessed
3300 * A filter is inserted on type[D -> A] to ensure that the element
3301 * is read/written by the same thread that needs the element.
3302 * This filter is obtained by applying
3306 * to the thread filter for the core statements.
3308 * The extension is inserted before the core computation in case of a read
3309 * and after the core computation in case of a write.
3310 * In the latter case, we also make sure that there is a synchronization
3311 * node after the write to global memory, unless this write is performed
3312 * at the outer level of the kernel.
3313 * In principle, this synchronization could be inserted higher
3314 * in the schedule tree depending on where the corresponding reads
3315 * from global memory are performed.
3317 static __isl_give isl_schedule_node
*add_copies_group_private(
3318 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3319 __isl_take isl_schedule_node
*node
, int read
)
3321 struct gpu_array_tile
*tile
;
3322 isl_union_map
*access
;
3323 isl_union_set
*domain
;
3325 isl_multi_aff
*from_access
;
3326 isl_multi_pw_aff
*mpa
;
3327 isl_multi_union_pw_aff
*mupa
;
3328 isl_union_pw_multi_aff
*contraction
;
3329 isl_schedule_node
*graft
;
3330 isl_union_set
*filter
;
3334 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3335 tile
= gpu_array_ref_group_tile(group
);
3336 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3338 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3339 empty
= isl_union_map_is_empty(access
);
3340 if (empty
< 0 || empty
) {
3341 isl_union_map_free(access
);
3343 return isl_schedule_node_free(node
);
3344 return gpu_tree_move_up_to_kernel(node
);
3347 group
->array
->global
= 1;
3348 group
->local_array
->global
= 1;
3350 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3351 space
= isl_space_domain(isl_multi_aff_get_space(from_access
));
3352 access
= isl_union_map_preimage_range_multi_aff(access
, from_access
);
3354 filter
= isl_union_set_copy(kernel
->thread_filter
);
3355 contraction
= isl_union_pw_multi_aff_copy(kernel
->contraction
);
3356 filter
= isl_union_set_preimage_union_pw_multi_aff(filter
, contraction
);
3357 filter
= isl_union_set_apply(filter
, isl_union_map_copy(access
));
3358 filter
= isl_union_set_detect_equalities(filter
);
3359 filter
= isl_union_set_coalesce(filter
);
3361 domain
= isl_union_map_range(access
);
3362 access
= isl_union_set_wrapped_domain_map(domain
);
3363 access
= isl_union_map_reverse(access
);
3364 access
= isl_union_map_coalesce(access
);
3365 graft
= isl_schedule_node_from_extension(access
);
3367 space
= isl_space_map_from_set(space
);
3368 mpa
= isl_multi_pw_aff_identity(space
);
3369 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
3370 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3372 graft
= isl_schedule_node_child(graft
, 0);
3373 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3374 graft
= unroll(graft
);
3376 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3378 graft
= isl_schedule_node_parent(graft
);
3381 node
= isl_schedule_node_graft_before(node
, graft
);
3383 node
= isl_schedule_node_graft_after(node
, graft
);
3384 if (kernel_depth
< tile
->depth
)
3385 node
= add_group_write_sync(node
, kernel
, group
, 0);
3388 node
= gpu_tree_move_up_to_kernel(node
);
3393 /* Add copy statements to the schedule tree of "node"
3394 * for reading from global memory to shared memory (if "read" is set) or
3395 * for writing back from shared memory to global memory
3396 * (if "read" is not set) for the array reference group "group" that
3397 * is mapped to shared memory.
3398 * On input, "node" points to the kernel node, and it is moved
3399 * back there on output.
3401 * The copies are performed in the order of the corresponding shared
3403 * The copy statement instances include a reference to the outer
3404 * tile->depth dimensions of the kernel schedule for ease of
3405 * combining them with the group tiling.
3407 * If we are performing a read from global memory to shared memory and
3408 * if the array involved is not a scalar, then we copy
3409 * the entire tile to shared memory. This may result in some extra
3410 * elements getting copied, but it should lead to simpler code
3411 * (which means that fewer registers may be needed) and less divergence.
3413 * Otherwise, we only copy the elements that will be read or have been written
3416 * That is, the extra schedule is of the form
3420 * where D corresponds to the outer tile->depth dimensions of
3421 * the kernel schedule, A to the global array and T is the corresponding
3422 * shared memory tile.
3424 * The copying is inserted in the schedule tree through an extension
3429 * where the extra domain elements type[D -> A] are those accessed
3430 * by the group. In the case of read from a non-scalar, this set
3431 * is replaced by the entire shared memory tile.
3433 * If the "unroll_copy_shared" option is set, then the AST generator
3434 * is instructed to unroll the copying code.
3436 * A filter is inserted on type[D -> A] to map the copy instances
3437 * to the threads. In particular, the thread identifiers are
3438 * equated to the position inside the shared memory tile (T)
3439 * modulo the block size.
3440 * We try to align the innermost tile dimension with the innermost
3441 * thread identifier (x) as a heuristic to improve coalescing.
3442 * In particular, if the dimension of the tile is greater than
3443 * the dimension of the block, then the schedule mapping to the tile
3444 * is broken up into two pieces and the filter is applied to the inner part.
3445 * If, on the other hand, the dimension of the tile is smaller than
3446 * the dimension of the block, then the initial thread identifiers
3447 * are equated to zero and the remaining thread identifiers are
3448 * matched to the memory tile.
3450 * The extension is inserted before the core computation in case of a read
3451 * and after the core computation in case of a write.
3452 * In the case of a read, we first need to make sure there is some
3453 * synchronization before the core computation such that we can put the read
3454 * from global memory to shared memory before that synchronization.
3455 * This ensures that all threads have finished copying into shared memory
3456 * before the shared memory is used.
3457 * We also need to make sure that there is a synchronization node after
3458 * the core computation to ensure that the next load into shared memory
3459 * only happens after all data has been used. There is no need for
3460 * this synchronization if we are at the outer level since then there
3461 * won't be a next load.
3462 * In the case of a write, we need to make sure there is some synchronization
3463 * after the core computation such that we can put the write from shared
3464 * memory to global memory after that synchronization.
3465 * Unless we are at the outer level, we also need a synchronization node
3466 * after the write to ensure the data is saved to global memory
3467 * before the next iteration writes to the same shared memory.
3468 * It also makes sure the data has arrived in global memory before
3469 * it is read in a subsequent iteration.
3471 static __isl_give isl_schedule_node
*add_copies_group_shared(
3472 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3473 __isl_take isl_schedule_node
*node
, int read
)
3475 struct gpu_array_tile
*tile
;
3476 isl_union_map
*access
;
3477 isl_union_set
*domain
;
3479 isl_multi_aff
*from_access
;
3480 isl_multi_pw_aff
*mpa
;
3481 isl_multi_union_pw_aff
*mupa
;
3482 isl_schedule_node
*graft
;
3483 isl_union_set
*filter
;
3488 tile
= gpu_array_ref_group_tile(group
);
3489 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3490 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3492 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3493 empty
= isl_union_map_is_empty(access
);
3494 if (empty
< 0 || empty
) {
3495 isl_union_map_free(access
);
3497 return isl_schedule_node_free(node
);
3498 return gpu_tree_move_up_to_kernel(node
);
3501 group
->array
->global
= 1;
3502 group
->local_array
->global
= 1;
3504 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3506 ma
= isl_multi_aff_copy(tile
->tiling
);
3507 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3508 isl_multi_aff_copy(from_access
));
3509 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3510 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3512 domain
= isl_union_map_range(access
);
3514 if (read
&& !gpu_array_is_scalar(group
->array
)) {
3516 isl_union_set_free(domain
);
3517 map
= group_tile(group
);
3518 domain
= isl_union_set_from_set(isl_map_wrap(map
));
3521 domain
= isl_union_set_preimage_multi_aff(domain
, from_access
);
3522 access
= isl_union_set_wrapped_domain_map(domain
);
3523 access
= isl_union_map_reverse(access
);
3524 access
= isl_union_map_coalesce(access
);
3525 graft
= isl_schedule_node_from_extension(access
);
3527 graft
= isl_schedule_node_child(graft
, 0);
3529 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3530 if (kernel
->options
->unroll_copy_shared
)
3531 graft
= ppcg_set_schedule_node_type(graft
, isl_ast_loop_unroll
);
3533 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0) {
3534 graft
= isl_schedule_node_band_split(graft
,
3535 tile
->n
- kernel
->n_block
);
3536 graft
= isl_schedule_node_child(graft
, 0);
3538 if (tile
->n
< kernel
->n_block
)
3539 skip
= kernel
->n_block
- tile
->n
;
3542 filter
= set_schedule_modulo(graft
, kernel
->thread_ids
,
3544 if (!kernel
->options
->wrap
)
3545 graft
= snap_band_to_sizes(graft
, kernel
->block_dim
+ skip
,
3547 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0)
3548 graft
= isl_schedule_node_parent(graft
);
3549 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3551 while (graft
&& isl_schedule_node_has_parent(graft
))
3552 graft
= isl_schedule_node_parent(graft
);
3555 if (kernel_depth
< tile
->depth
)
3556 node
= gpu_tree_ensure_sync_after_core(node
, kernel
);
3557 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3558 node
= isl_schedule_node_graft_before(node
, graft
);
3560 node
= gpu_tree_move_right_to_sync(node
, kernel
);
3561 node
= isl_schedule_node_graft_after(node
, graft
);
3562 if (kernel_depth
< tile
->depth
)
3563 node
= add_group_write_sync(node
, kernel
, group
, 1);
3566 node
= gpu_tree_move_up_to_kernel(node
);
3571 /* Check whether the array reference group "group" is mapped to
3572 * private or shared memory and, if so,
3573 * add copy statements to the schedule tree of "node"
3574 * for reading from global memory to private or shared memory
3575 * (if "read" is set) or for writing back from private or shared memory
3576 * to global memory (if "read" is not set) for this group.
3577 * On input, "node" points to the kernel node, and it is moved
3578 * back there on output.
3580 static __isl_give isl_schedule_node
*add_copies_group(
3581 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3582 __isl_take isl_schedule_node
*node
, int read
)
3584 enum ppcg_group_access_type type
;
3586 type
= gpu_array_ref_group_type(group
);
3587 if (type
== ppcg_access_private
)
3588 return add_copies_group_private(kernel
, group
, node
, read
);
3589 if (type
== ppcg_access_shared
)
3590 return add_copies_group_shared(kernel
, group
, node
, read
);
3594 /* For each array reference group that is mapped to private or shared memory,
3595 * add copy statements to the schedule tree of "node"
3596 * for reading from global memory to private or shared memory
3597 * and for writing back.
3598 * On input, "node" points to the kernel node, and it is moved
3599 * back there on output.
3601 static __isl_give isl_schedule_node
*add_copies(struct ppcg_kernel
*kernel
,
3602 __isl_take isl_schedule_node
*node
)
3606 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3607 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3609 for (j
= 0; j
< array
->n_group
; ++j
) {
3610 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3612 node
= add_copies_group(kernel
, group
, node
, 1);
3615 node
= add_copies_group(kernel
, group
, node
, 0);
3624 /* Mark all dimensions in the current band node atomic.
3626 static __isl_give isl_schedule_node
*atomic(__isl_take isl_schedule_node
*node
)
3628 return ppcg_set_schedule_node_type(node
, isl_ast_loop_atomic
);
3631 /* Mark "node" atomic, if it is a band node.
3632 * Do the same for all ancestors.
3633 * Return a pointer to "node" (in the updated schedule tree).
3635 static __isl_give isl_schedule_node
*atomic_ancestors(
3636 __isl_take isl_schedule_node
*node
)
3642 if (!isl_schedule_node_has_parent(node
))
3645 pos
= isl_schedule_node_get_child_position(node
);
3646 node
= isl_schedule_node_parent(node
);
3647 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
)
3648 node
= atomic(node
);
3649 node
= atomic_ancestors(node
);
3650 node
= isl_schedule_node_child(node
, pos
);
3655 /* Collect all write references that require synchronization.
3656 * "node" is assumed to point to the kernel node.
3657 * Each reference is represented by a universe set in a space
3661 * with S[i,j] the statement instance space and R[] the array reference.
3663 * This function should be called before block and thread filters are added.
3665 * Synchronization is needed after a write if there is a subsequent read
3666 * within the same block that may not be performed by the same thread.
3667 * There should not be any dependences between different blocks,
3668 * so we start with the flow dependences within the same kernel invocation
3669 * and we subtract from these those dependences that are mapped
3670 * to the same iteration of the bands where synchronization is inserted.
3671 * We do not remove pairs of instances that are known to map to
3672 * the same thread across different iterations of the intermediate
3673 * bands because the read may be performed by a different thread
3674 * than the one that needs the value if shared memory is involved.
3676 * We also consider all pairs of possible writes that access the same
3677 * memory location and that may be mapped to the same block but not
3678 * to the same iteration of the intermediate bands.
3679 * In theory, it would be possible for one thread to still be in
3680 * a previous iteration of a loop in these bands.
3681 * A write to global memory in this delayed thread could then overwrite
3682 * a write from another thread that has already moved on to
3683 * the next iteration.
3685 * After computing the above writes paired off with reads or writes
3686 * that depend on them, we project onto the domain writes.
3687 * Sychronization is needed after writes to global memory
3688 * through these references.
3690 static __isl_give isl_union_set
*compute_sync_writes(
3691 struct ppcg_kernel
*kernel
, __isl_keep isl_schedule_node
*node
)
3693 isl_union_map
*local
;
3694 isl_union_map
*may_writes
, *shared_access
;
3695 isl_union_map
*kernel_prefix
, *thread_prefix
;
3696 isl_union_map
*equal
;
3697 isl_union_set
*wrap
;
3698 isl_union_set
*domain
;
3699 isl_union_pw_multi_aff
*contraction
;
3701 kernel_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3702 node
= isl_schedule_node_copy(node
);
3703 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3704 thread_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3705 isl_schedule_node_free(node
);
3707 contraction
= kernel
->contraction
;
3708 kernel_prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(
3709 kernel_prefix
, isl_union_pw_multi_aff_copy(contraction
));
3710 thread_prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(
3711 thread_prefix
, isl_union_pw_multi_aff_copy(contraction
));
3712 domain
= isl_union_set_copy(kernel
->expanded_domain
);
3713 domain
= isl_union_set_universe(domain
);
3715 may_writes
= isl_union_map_copy(kernel
->prog
->scop
->tagged_may_writes
);
3716 may_writes
= isl_union_map_curry(may_writes
);
3717 may_writes
= isl_union_map_intersect_domain(may_writes
, domain
);
3718 may_writes
= isl_union_map_uncurry(may_writes
);
3719 shared_access
= isl_union_map_copy(may_writes
);
3720 shared_access
= isl_union_map_apply_range(shared_access
,
3721 isl_union_map_reverse(may_writes
));
3723 local
= isl_union_map_copy(kernel
->prog
->scop
->tagged_dep_flow
);
3724 local
= isl_union_map_union(local
, shared_access
);
3725 local
= isl_union_map_zip(local
);
3727 equal
= isl_union_map_apply_range(kernel_prefix
,
3728 isl_union_map_reverse(isl_union_map_copy(kernel_prefix
)));
3729 wrap
= isl_union_map_wrap(equal
);
3730 local
= isl_union_map_intersect_domain(local
, wrap
);
3731 equal
= isl_union_map_apply_range(thread_prefix
,
3732 isl_union_map_reverse(isl_union_map_copy(thread_prefix
)));
3733 wrap
= isl_union_map_wrap(equal
);
3734 local
= isl_union_map_subtract_domain(local
, wrap
);
3736 local
= isl_union_map_zip(local
);
3737 local
= isl_union_map_universe(local
);
3739 return isl_union_map_domain(local
);
3742 /* Group the domain elements into a single space, named kernelX,
3743 * with X the kernel sequence number "kernel_id".
3745 static __isl_give isl_schedule_node
*group_statements(
3746 __isl_take isl_schedule_node
*node
, int kernel_id
)
3754 snprintf(buffer
, sizeof(buffer
), "kernel%d", kernel_id
);
3755 id
= isl_id_alloc(isl_schedule_node_get_ctx(node
), buffer
, NULL
);
3756 return isl_schedule_node_group(node
, id
);
3759 /* Create a ppcg_kernel representing the domain instances that reach "node"
3760 * and insert a mark node pointing to the ppcg_kernel before "node".
3761 * The band that "node" points to is the band that needs to be mapped
3762 * to block identifiers. The band that needs to be mapped to thread
3763 * identifiers should be marked by a "thread" mark by the caller.
3764 * The linear branch between the current node and the "thread" mark
3765 * may also have a "shared" mark. If present, the mapping to shared
3766 * memory is computed at that point.
3767 * Both marks are removed by this function.
3768 * If "scale" is set, then the band that "node" points to is scaled
3771 * Mark all outer band nodes as atomic to ensure each kernel is only
3773 * If the domain elements that reach "node" live in more than one space,
3774 * then group the domain elements into a single space, named kernelX,
3775 * with X the kernel sequence number.
3777 * Insert a guard node governing the kernel node to ensure that
3778 * no kernels with zero blocks are launched.
3780 * Insert a context node describing the block and thread
3781 * identifiers inside the kernel mark.
3782 * The context node needs to be inserted after the effective block size
3783 * has been determined such that the bounds on the thread identifiers
3784 * would reflect the effective block size.
3785 * Insert a filter node inside the context node mapping the statement
3786 * instances to block identifiers. In particular, the block identifiers
3787 * are equated to the partial schedule of band that was marked for mapping
3788 * to blocks modulo the grid size.
3789 * Insert a filter node inside the "thread" mark mapping the statement
3790 * instances to thread identifiers. In particular, the thread identifiers
3791 * are equated to the partial schedule of band that was marked for mapping
3792 * to threads modulo the block size.
3794 * Compute array reference groups for all arrays, set the local
3795 * array bounds based on the set of domain instances that reach
3796 * the kernel node, check the total amount of shared memory used
3797 * and compute all group tilings.
3798 * The array reference groups are computed after the block filter
3799 * has been inserted because it affects the mapping to shared or
3800 * private memory. This computation also requires the thread filter
3801 * (in the ppcg_kernel object), but this thread filter should not
3802 * have been added to the schedule tree yet since the computation
3803 * requires the schedule of the band that needs to be mapped to
3804 * threads before the privatization is applied.
3806 * If any array reference group requires the band mapped to threads
3807 * to be unrolled, then we perform the required unrolling.
3809 * We save a copy of the schedule that may influence the mappings
3810 * to shared or private memory in kernel->copy_schedule.
3812 * Finally, we add synchronization and copy statements to the schedule tree,
3813 * remove the "thread" mark and create representations for the local
3814 * variables in the kernel.
3816 * We keep a copy of the isl_id that points to the kernel to ensure
3817 * that the kernel does not get destroyed if the schedule node
3818 * is freed due to some error condition.
3820 __isl_give isl_schedule_node
*gpu_create_kernel(struct gpu_gen
*gen
,
3821 __isl_take isl_schedule_node
*node
, int scale
,
3822 __isl_keep isl_multi_val
*sizes
)
3824 struct ppcg_kernel
*kernel
;
3826 isl_schedule_node
*node_thread
;
3827 isl_union_map
*host_schedule
;
3828 isl_union_pw_multi_aff
*contraction
;
3829 isl_set
*host_domain
;
3830 isl_union_set
*domain
, *expanded
;
3831 int single_statement
;
3833 node
= gpu_tree_insert_shared_before_thread(node
);
3837 kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
3838 kernel
= ppcg_kernel_create_local_arrays(kernel
, gen
->prog
);
3840 return isl_schedule_node_free(node
);
3842 domain
= isl_schedule_node_get_domain(node
);
3843 single_statement
= isl_union_set_n_set(domain
) == 1;
3845 kernel
->ctx
= gen
->ctx
;
3846 kernel
->prog
= gen
->prog
;
3847 kernel
->options
= gen
->options
;
3848 kernel
->context
= extract_context(node
, gen
->prog
);
3849 kernel
->core
= isl_union_set_universe(isl_union_set_copy(domain
));
3850 contraction
= isl_schedule_node_get_subtree_contraction(node
);
3851 kernel
->contraction
= isl_union_pw_multi_aff_copy(contraction
);
3852 expanded
= isl_union_set_copy(domain
);
3853 expanded
= isl_union_set_preimage_union_pw_multi_aff(expanded
,
3855 kernel
->expanded_domain
= isl_union_set_copy(expanded
);
3856 kernel
->arrays
= accessed_by_domain(expanded
, gen
->prog
);
3857 kernel
->n_grid
= n_outer_coincidence(node
);
3858 node_thread
= isl_schedule_node_copy(node
);
3859 node_thread
= gpu_tree_move_down_to_thread(node_thread
, kernel
->core
);
3860 node_thread
= isl_schedule_node_child(node_thread
, 0);
3861 kernel
->n_block
= n_outer_coincidence(node_thread
);
3862 isl_schedule_node_free(node_thread
);
3863 kernel
->id
= gen
->kernel_id
++;
3864 read_grid_and_block_sizes(kernel
, gen
);
3866 kernel
->sync_writes
= compute_sync_writes(kernel
, node
);
3868 host_schedule
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3869 host_domain
= isl_set_from_union_set(isl_union_map_range(
3872 node
= atomic_ancestors(node
);
3874 id
= isl_id_alloc(gen
->ctx
, "kernel", kernel
);
3875 id
= isl_id_set_free_user(id
, &ppcg_kernel_free_wrap
);
3876 node
= isl_schedule_node_insert_mark(node
, isl_id_copy(id
));
3878 if (!single_statement
)
3879 node
= group_statements(node
, kernel
->id
);
3881 node
= isl_schedule_node_child(node
, 0);
3882 node
= split_band(node
, kernel
->n_grid
);
3883 kernel
->block_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3884 kernel
->n_grid
, "b");
3885 kernel
->block_filter
= set_schedule_modulo(node
, kernel
->block_ids
,
3887 kernel
->grid_size
= extract_grid_size(kernel
,
3888 isl_union_set_copy(domain
));
3889 if (!kernel
->options
->wrap
)
3890 node
= snap_band_to_sizes(node
, kernel
->grid_dim
,
3893 node
= scale_band(node
, isl_multi_val_copy(sizes
));
3894 node
= isl_schedule_node_parent(node
);
3895 if (!single_statement
)
3896 node
= isl_schedule_node_parent(node
);
3897 node
= insert_guard(node
, kernel
->context
, kernel
->grid_size
,
3899 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3900 node
= isl_schedule_node_child(node
, 0);
3901 node
= split_band(node
, kernel
->n_block
);
3902 kernel
->thread_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3903 kernel
->n_block
, "t");
3904 kernel
->thread_filter
= set_schedule_modulo(node
, kernel
->thread_ids
,
3906 if (extract_block_size(kernel
, domain
) < 0)
3907 node
= isl_schedule_node_free(node
);
3909 node
= gpu_tree_move_up_to_kernel(node
);
3910 node
= isl_schedule_node_child(node
, 0);
3911 node
= insert_context(kernel
, node
);
3912 node
= isl_schedule_node_child(node
, 0);
3913 node
= isl_schedule_node_insert_filter(node
,
3914 isl_union_set_copy(kernel
->block_filter
));
3916 node
= gpu_tree_move_up_to_kernel(node
);
3918 if (gpu_group_references(kernel
, node
) < 0)
3919 node
= isl_schedule_node_free(node
);
3920 localize_bounds(kernel
, host_domain
);
3921 isl_set_free(host_domain
);
3923 check_shared_memory_bound(kernel
);
3924 mark_global_arrays(kernel
);
3925 compute_group_tilings(kernel
);
3927 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3928 node
= isl_schedule_node_child(node
, 0);
3929 if (!kernel
->options
->wrap
)
3930 node
= snap_band_to_sizes(node
, kernel
->block_dim
,
3932 node
= isl_schedule_node_insert_filter(node
,
3933 isl_union_set_copy(kernel
->thread_filter
));
3934 if (kernel_requires_unroll(kernel
)) {
3935 node
= isl_schedule_node_child(node
, 0);
3936 node
= unroll(node
);
3939 node
= gpu_tree_move_up_to_thread(node
);
3940 kernel
->copy_schedule_dim
= isl_schedule_node_get_schedule_depth(node
);
3941 kernel
->copy_schedule
=
3942 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
3943 contraction
= isl_union_pw_multi_aff_copy(kernel
->contraction
);
3944 kernel
->copy_schedule
=
3945 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3946 kernel
->copy_schedule
, contraction
);
3948 node
= gpu_tree_move_up_to_kernel(node
);
3950 node
= add_sync(kernel
, node
);
3951 node
= add_copies(kernel
, node
);
3953 node
= gpu_tree_move_down_to_shared(node
, kernel
->core
);
3954 node
= isl_schedule_node_delete(node
);
3956 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3957 node
= isl_schedule_node_delete(node
);
3959 node
= gpu_tree_move_up_to_kernel(node
);
3961 if (create_kernel_vars(kernel
) < 0)
3962 node
= isl_schedule_node_free(node
);
3964 if (!single_statement
)
3965 node
= isl_schedule_node_parent(node
);
3966 node
= isl_schedule_node_parent(node
);
3970 ppcg_kernel_free(kernel
);
3974 /* Insert a zero-dimensional permutable band at "node".
3976 static __isl_give isl_schedule_node
*insert_empty_permutable_band(
3977 __isl_take isl_schedule_node
*node
)
3980 isl_schedule
*schedule
;
3981 isl_union_set
*domain
;
3982 isl_multi_union_pw_aff
*mupa
;
3984 schedule
= isl_schedule_node_get_schedule(node
);
3985 domain
= isl_schedule_get_domain(schedule
);
3986 space
= isl_union_set_get_space(domain
);
3987 isl_union_set_free(domain
);
3988 isl_schedule_free(schedule
);
3990 space
= isl_space_set_from_params(space
);
3991 mupa
= isl_multi_union_pw_aff_zero(space
);
3992 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3993 node
= isl_schedule_node_band_set_permutable(node
, 1);
3998 /* See if hybrid tiling can be performed on "node" and its parent.
3999 * If so, apply hybrid tiling and return the updated schedule tree.
4000 * If not, return the original schedule tree.
4001 * Return NULL on error.
4003 * First check if "node", together with its parent, meets
4004 * the basic requirements for hybrid tiling.
4005 * If so, compute the relative dependence distances of "node"
4006 * with respect to its parent and check if they are sufficiently bounded.
4007 * If so, apply hybrid tiling using user specified tile sizes.
4009 * The tile sizes are read before the dependence distance bounds are
4010 * computed, because the user may have specified fewer dimensions
4011 * than are available. In this case, the remaining schedule dimensions
4012 * are split off and the dependence distances should be computed
4013 * after these dimensions have been split off.
4015 static __isl_give isl_schedule_node
*try_hybrid_tile(struct gpu_gen
*gen
,
4016 __isl_take isl_schedule_node
*node
)
4021 isl_schedule_node
*orig
= node
;
4022 ppcg_ht_bounds
*bounds
;
4024 ok
= ppcg_ht_parent_has_input_pattern(node
);
4026 return isl_schedule_node_free(node
);
4030 tile_len
= 1 + isl_schedule_node_band_n_member(node
);
4031 tile_size
= read_tile_sizes(gen
, &tile_len
);
4033 return isl_schedule_node_free(node
);
4035 node
= isl_schedule_node_copy(node
);
4036 node
= split_band(node
, tile_len
- 1);
4037 node
= isl_schedule_node_parent(node
);
4038 bounds
= ppcg_ht_compute_bounds(gen
->prog
->scop
, node
);
4039 node
= isl_schedule_node_child(node
, 0);
4041 ok
= ppcg_ht_bounds_is_valid(bounds
);
4043 node
= gpu_hybrid_tile(gen
, node
, bounds
, tile_size
);
4045 ppcg_ht_bounds_free(bounds
);
4048 if (ok
>= 0 && !ok
) {
4049 isl_schedule_node_free(node
);
4052 isl_schedule_node_free(orig
);
4054 return isl_schedule_node_free(node
);
4058 /* If "node" is the outermost permutable band that can be mapped to block and
4059 * thread identifiers in its branch (or the root of a subtree with
4060 * no such outer bands),
4061 * then mark the band as such, attaching a ppcg_kernel to the mark.
4063 * If hybrid tiling is allowed, then first try and apply it
4064 * to "node" and its parent.
4066 * If "node" is the root of a subtree without permutable bands,
4067 * then insert a zero-dimensional permutable band such that
4068 * we can assume that "node" always points to a band node.
4069 * This includes the case where "node" already points to a band node,
4070 * but one without any coincident dimension. In this case,
4071 * the extra node ensures that this original node does not get tiled.
4073 * Tile "node" using user specified tile sizes, after splitting the band
4074 * if the number of specified tile sizes is smaller than the dimension
4075 * of the band. Mark the point band of this tiling as the band that
4076 * needs to be mapped to threads and instruct the AST generator to unroll
4077 * the band if the "unroll_gpu_tile" option is set.
4078 * Create a kernel representing the domain instances that reach "node" and
4079 * insert a mark node pointing to the ppcg_kernel before the band node.
4081 static __isl_give isl_schedule_node
*mark_outer_permutable(
4082 __isl_take isl_schedule_node
*node
, void *user
)
4084 struct gpu_gen
*gen
= user
;
4090 isl_multi_val
*sizes
;
4092 outer
= is_outer_tilable(node
);
4094 return isl_schedule_node_free(node
);
4098 if (gen
->options
->hybrid
) {
4099 isl_schedule_node
*saved
= isl_schedule_node_copy(node
);
4100 node
= try_hybrid_tile(gen
, node
);
4101 isl_schedule_node_free(saved
);
4106 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
||
4107 !isl_schedule_node_band_member_get_coincident(node
, 0))
4108 node
= insert_empty_permutable_band(node
);
4110 tile_len
= isl_schedule_node_band_n_member(node
);
4111 tile_size
= read_tile_sizes(gen
, &tile_len
);
4113 return isl_schedule_node_free(node
);
4114 if (tile_len
< isl_schedule_node_band_n_member(node
))
4115 node
= isl_schedule_node_band_split(node
, tile_len
);
4116 sizes
= construct_band_tiles_sizes(node
, tile_size
);
4117 node
= tile_band(node
, isl_multi_val_copy(sizes
));
4118 node
= isl_schedule_node_child(node
, 0);
4119 if (gen
->options
->unroll_gpu_tile
)
4120 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
4121 id
= isl_id_alloc(gen
->ctx
, "thread", NULL
);
4122 node
= isl_schedule_node_insert_mark(node
, id
);
4123 node
= isl_schedule_node_parent(node
);
4125 scale
= gen
->options
->scale_tile_loops
;
4126 node
= gpu_create_kernel(gen
, node
, scale
, sizes
);
4127 isl_multi_val_free(sizes
);
4133 /* Given a set or sequence node, return the union the filters of either all
4134 * (if "only_initial" is not set) or the initial (if "only_initial" is set)
4135 * direct subtrees that do not contain any suitably permutable bands
4136 * (according to subtree_has_permutable_bands).
4138 static __isl_give isl_union_set
*get_non_parallel_subtree_filters(
4139 __isl_keep isl_schedule_node
*node
, int only_initial
)
4142 isl_union_set
*filter
;
4145 n
= isl_schedule_node_n_children(node
);
4149 node
= isl_schedule_node_copy(node
);
4150 node
= isl_schedule_node_child(node
, 0);
4151 filter
= isl_schedule_node_filter_get_filter(node
);
4152 node
= isl_schedule_node_parent(node
);
4153 space
= isl_union_set_get_space(filter
);
4154 isl_union_set_free(filter
);
4155 filter
= isl_union_set_empty(space
);
4157 for (i
= 0; i
< n
; ++i
) {
4160 node
= isl_schedule_node_child(node
, i
);
4161 parallelism
= subtree_has_permutable_bands(node
);
4162 if (parallelism
< 0) {
4163 filter
= isl_union_set_free(filter
);
4164 } else if (!parallelism
) {
4165 isl_union_set
*filter_i
;
4166 filter_i
= isl_schedule_node_filter_get_filter(node
);
4167 filter
= isl_union_set_union(filter
, filter_i
);
4168 } else if (only_initial
)
4170 node
= isl_schedule_node_parent(node
);
4173 isl_schedule_node_free(node
);
4178 /* Given a set or sequence node, return the union of the filters of
4179 * the direct subtrees that do not contain any suitably permutable bands
4180 * (according to subtree_has_permutable_bands).
4182 static __isl_give isl_union_set
*get_all_non_parallel_subtree_filters(
4183 __isl_keep isl_schedule_node
*node
)
4185 return get_non_parallel_subtree_filters(node
, 0);
4188 /* Given a set or sequence node, return the union of the filters of
4189 * the initial direct subtrees that do not contain any suitably permutable
4190 * bands (according to subtree_has_permutable_bands).
4192 static __isl_give isl_union_set
*get_initial_non_parallel_subtree_filters(
4193 __isl_keep isl_schedule_node
*node
)
4195 return get_non_parallel_subtree_filters(node
, 1);
4198 /* Mark all variables that are accessed by the statement instances in "domain"
4199 * and that are local to "prog" as requiring a declaration in the host code.
4200 * The statement instances in "domain" correspond to (a subset of)
4201 * the active instances at "node".
4202 * "node" is not modified by this function, except that NULL is returned
4205 static __isl_give isl_schedule_node
*declare_accessed_local_variables(
4206 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
,
4207 __isl_keep isl_union_set
*domain
)
4209 isl_union_pw_multi_aff
*contraction
;
4210 isl_union_set
*arrays
;
4213 if (!ppcg_scop_any_hidden_declarations(prog
->scop
))
4215 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4216 domain
= isl_union_set_copy(domain
);
4217 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, contraction
);
4218 arrays
= accessed_by_domain(domain
, prog
);
4220 for (i
= 0; i
< prog
->n_array
; ++i
) {
4225 if (!prog
->array
[i
].local
)
4227 space
= isl_set_get_space(prog
->array
[i
].extent
);
4228 set
= isl_union_set_extract_set(arrays
, space
);
4229 empty
= isl_set_plain_is_empty(set
);
4234 prog
->array
[i
].declare_local
= 1;
4237 isl_union_set_free(arrays
);
4240 isl_union_set_free(arrays
);
4241 return isl_schedule_node_free(node
);
4244 /* If "node" points to a set node, then separate its children
4245 * into subtrees that have suitably permutable bands and
4246 * those that do not.
4247 * Adjust the schedule tree in order to execute the second group
4248 * after the first group and return a pointer to the first group,
4249 * assuming there are any such subtrees.
4250 * If "node" points to a sequence node, then separate the initial
4251 * children that do not have suitably permutable bands and
4252 * return a pointer to the subsequence of children that do have such bands,
4253 * assuming there are any such subtrees.
4255 * In both cases, mark all local variables in "prog" that are accessed by
4256 * the group without permutable bands as requiring a declaration on the host.
4258 static __isl_give isl_schedule_node
*isolate_permutable_subtrees(
4259 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
)
4261 isl_union_set
*filter
;
4262 enum isl_schedule_node_type type
;
4266 type
= isl_schedule_node_get_type(node
);
4267 if (type
== isl_schedule_node_set
) {
4268 filter
= get_all_non_parallel_subtree_filters(node
);
4269 node
= declare_accessed_local_variables(node
, prog
, filter
);
4270 node
= isl_schedule_node_order_after(node
, filter
);
4271 } else if (type
== isl_schedule_node_sequence
) {
4272 filter
= get_initial_non_parallel_subtree_filters(node
);
4273 node
= declare_accessed_local_variables(node
, prog
, filter
);
4274 node
= isl_schedule_node_order_before(node
, filter
);
4280 /* Replace any reference to an array element in the range of "copy"
4281 * by a reference to all array elements (defined by the extent of the array).
4283 static __isl_give isl_union_map
*approximate_copy_out(
4284 __isl_take isl_union_map
*copy
, struct gpu_prog
*prog
)
4289 res
= isl_union_map_empty(isl_union_map_get_space(copy
));
4291 for (i
= 0; i
< prog
->n_array
; ++i
) {
4294 isl_union_map
*copy_i
;
4295 isl_union_set
*extent
, *domain
;
4297 space
= isl_space_copy(prog
->array
[i
].space
);
4298 extent
= isl_union_set_from_set(isl_set_universe(space
));
4299 copy_i
= isl_union_map_copy(copy
);
4300 copy_i
= isl_union_map_intersect_range(copy_i
, extent
);
4301 set
= isl_set_copy(prog
->array
[i
].extent
);
4302 extent
= isl_union_set_from_set(set
);
4303 domain
= isl_union_map_domain(copy_i
);
4304 copy_i
= isl_union_map_from_domain_and_range(domain
, extent
);
4305 res
= isl_union_map_union(res
, copy_i
);
4308 isl_union_map_free(copy
);
4313 /* Insert "kernel" marks that point to a ppcg_kernel structure
4314 * in front of all outermost tilable band that (by construction)
4315 * have at least one parallel loop.
4317 static __isl_give isl_schedule_node
*mark_kernels(struct gpu_gen
*gen
,
4318 __isl_take isl_schedule_node
*node
)
4320 return isl_schedule_node_map_descendant_bottom_up(node
,
4321 &mark_outer_permutable
, gen
);
4324 /* Construct schedule constraints from the dependences in prog->scop and
4325 * the array order dependences in prog->array_order.
4327 * If live range reordering is allowed, then we need to make sure
4328 * that live ranges on arrays are not run in parallel since doing
4329 * so would require array expansion. We therefore add the array
4330 * order dependences to the coincidence dependences. Non-zero array
4331 * order dependences will then prevent a schedule dimension from being
4332 * considered parallel.
4333 * Live ranges derived from scalars are allowed to be run in parallel
4334 * since we force the scalars to be mapped to private memory in
4335 * check_scalar_live_ranges.
4336 * If live range reordering is allowed, then the false dependences
4337 * are not added to the validity constraints as that would prevent
4338 * reordering. Instead, the external false dependences that enforce that reads
4339 * from potentially live-in data precede any later write and
4340 * that writes of potentially live-out data follow any other earlier write
4341 * are added to the validity and the coincidence constraints.
4342 * The false dependences are still added to the proximity constraints
4343 * for consistency with the case where live range reordering is not allowed.
4344 * The coincidence constraints then consist of flow dependences,
4345 * external false dependences and array order dependences.
4346 * The independences can be filtered out from the first two sets.
4347 * They have already been filtered out from the array order dependences
4348 * on a per array basis in collect_order_dependences.
4349 * There is no need for a per array handling of the other two sets
4350 * as there should be no flow or external false dependence on local
4351 * variables that can be filtered out.
4353 static __isl_give isl_schedule_constraints
*construct_schedule_constraints(
4354 struct gpu_prog
*prog
)
4356 isl_union_set
*domain
;
4357 isl_union_map
*dep_raw
, *dep
;
4358 isl_union_map
*validity
, *proximity
, *coincidence
;
4359 isl_schedule_constraints
*sc
;
4361 domain
= isl_union_set_copy(prog
->scop
->domain
);
4362 sc
= isl_schedule_constraints_on_domain(domain
);
4363 sc
= isl_schedule_constraints_set_context(sc
,
4364 isl_set_copy(prog
->scop
->context
));
4365 if (prog
->scop
->options
->live_range_reordering
) {
4366 sc
= isl_schedule_constraints_set_conditional_validity(sc
,
4367 isl_union_map_copy(prog
->scop
->tagged_dep_flow
),
4368 isl_union_map_copy(prog
->scop
->tagged_dep_order
));
4369 proximity
= isl_union_map_copy(prog
->scop
->dep_flow
);
4370 validity
= isl_union_map_copy(proximity
);
4371 validity
= isl_union_map_union(validity
,
4372 isl_union_map_copy(prog
->scop
->dep_forced
));
4373 proximity
= isl_union_map_union(proximity
,
4374 isl_union_map_copy(prog
->scop
->dep_false
));
4375 coincidence
= isl_union_map_copy(validity
);
4376 coincidence
= isl_union_map_subtract(coincidence
,
4377 isl_union_map_copy(prog
->scop
->independence
));
4378 coincidence
= isl_union_map_union(coincidence
,
4379 isl_union_map_copy(prog
->array_order
));
4381 dep_raw
= isl_union_map_copy(prog
->scop
->dep_flow
);
4382 dep
= isl_union_map_copy(prog
->scop
->dep_false
);
4383 dep
= isl_union_map_union(dep
, dep_raw
);
4384 dep
= isl_union_map_coalesce(dep
);
4385 proximity
= isl_union_map_copy(dep
);
4386 coincidence
= isl_union_map_copy(dep
);
4389 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4390 sc
= isl_schedule_constraints_set_coincidence(sc
, coincidence
);
4391 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4396 /* Compute an appropriate schedule based on the accesses in
4397 * gen->read and gen->write.
4399 * We derive schedule constraints from the dependences in gen->prog->scop
4400 * and then use isl to compute a schedule that has a parallel loop
4401 * in each tilable band.
4402 * During the schedule construction, some statement instances
4403 * may be grouped first based on the input schedule.
4405 static __isl_give isl_schedule
*compute_schedule(struct gpu_gen
*gen
)
4407 isl_schedule_constraints
*sc
;
4408 isl_schedule
*schedule
;
4410 sc
= construct_schedule_constraints(gen
->prog
);
4411 schedule
= gen
->prog
->scop
->schedule
;
4412 schedule
= ppcg_compute_schedule(sc
, schedule
, gen
->options
);
4417 /* If the band node "node" has exactly one member then mark it permutable.
4419 static __isl_give isl_schedule_node
*band_set_permutable(
4420 __isl_take isl_schedule_node
*node
,
4421 __isl_keep isl_schedule_constraints
*sc
)
4423 if (isl_schedule_node_band_n_member(node
) == 1)
4424 node
= isl_schedule_node_band_set_permutable(node
, 1);
4429 /* Return the coincidence constraints between pairs of instances
4430 * that are scheduled together by the ancestors of "node".
4431 * That is, select those coincidence constraints that relate
4432 * pairs of instances that have the same value for the prefix schedule.
4433 * If the schedule depth is zero, then the prefix schedule does not
4434 * contain any information, so we intersect domain and range
4435 * of the schedule constraints with the reaching domain elements instead.
4437 static __isl_give isl_union_map
*get_local_coincidence(
4438 __isl_keep isl_schedule_node
*node
,
4439 __isl_keep isl_schedule_constraints
*sc
)
4441 isl_union_map
*coincidence
;
4442 isl_multi_union_pw_aff
*prefix
;
4443 isl_union_pw_multi_aff
*contraction
;
4445 coincidence
= isl_schedule_constraints_get_coincidence(sc
);
4446 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4447 if (isl_schedule_node_get_schedule_depth(node
) == 0) {
4448 isl_union_set
*domain
;
4450 domain
= isl_schedule_node_get_domain(node
);
4451 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4453 coincidence
= isl_union_map_intersect_domain(coincidence
,
4454 isl_union_set_copy(domain
));
4455 coincidence
= isl_union_map_intersect_range(coincidence
,
4460 prefix
= isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
4461 prefix
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
4463 return isl_union_map_eq_at_multi_union_pw_aff(coincidence
, prefix
);
4466 /* For each member in the band node "node", determine whether
4467 * it is coincident with respect to the outer nodes and mark
4470 * That is, for each coincidence constraint between pairs
4471 * of instances that are scheduled together by the outer nodes,
4472 * check that domain and range are assigned the same value
4473 * by the band member. This test is performed by checking
4474 * that imposing the same value for the band member does not
4475 * remove any elements from the set of coincidence constraints.
4477 static __isl_give isl_schedule_node
*band_set_coincident(
4478 __isl_take isl_schedule_node
*node
,
4479 __isl_keep isl_schedule_constraints
*sc
)
4481 isl_union_map
*coincidence
;
4482 isl_union_pw_multi_aff
*contraction
;
4483 isl_multi_union_pw_aff
*partial
;
4486 coincidence
= get_local_coincidence(node
, sc
);
4488 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4489 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4490 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4492 n
= isl_schedule_node_band_n_member(node
);
4493 for (i
= 0; i
< n
; ++i
) {
4494 isl_union_map
*coincidence_i
;
4495 isl_union_pw_aff
*upa
;
4496 isl_multi_union_pw_aff
*partial_i
;
4499 upa
= isl_multi_union_pw_aff_get_union_pw_aff(partial
, i
);
4500 partial_i
= isl_multi_union_pw_aff_from_union_pw_aff(upa
);
4501 coincidence_i
= isl_union_map_copy(coincidence
);
4502 coincidence_i
= isl_union_map_eq_at_multi_union_pw_aff(
4503 coincidence_i
, partial_i
);
4504 subset
= isl_union_map_is_subset(coincidence
, coincidence_i
);
4505 isl_union_map_free(coincidence_i
);
4509 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
4513 node
= isl_schedule_node_free(node
);
4514 isl_multi_union_pw_aff_free(partial
);
4515 isl_union_map_free(coincidence
);
4520 /* If "node" is a band, then set its properties.
4522 * In particular, if the band has exactly one member, then mark it permutable.
4523 * Mark the band members coincident based on the coincidence constraints
4526 static __isl_give isl_schedule_node
*set_band_properties(
4527 __isl_take isl_schedule_node
*node
, void *user
)
4529 isl_schedule_constraints
*sc
= user
;
4531 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
4533 if (isl_schedule_node_band_n_member(node
) == 0)
4536 node
= band_set_permutable(node
, sc
);
4537 node
= band_set_coincident(node
, sc
);
4542 /* Return the original schedule with all bands marked permutable and
4543 * all band members marked coincident based on the coincidence constraints.
4544 * The bands are explicitly marked permutable so that they will be considered
4545 * by mark_outer_permutable.
4547 static __isl_give isl_schedule
*determine_properties_original_schedule(
4548 struct gpu_gen
*gen
)
4550 isl_schedule
*schedule
;
4551 isl_schedule_constraints
*sc
;
4553 schedule
= isl_schedule_copy(gen
->prog
->scop
->schedule
);
4554 sc
= construct_schedule_constraints(gen
->prog
);
4555 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
4556 &set_band_properties
, sc
);
4557 isl_schedule_constraints_free(sc
);
4562 /* Compute a schedule or determine the properties of the original schedule
4563 * depending on the value of the "reschedule" option.
4565 static __isl_give isl_schedule
*compute_or_set_properties(void *user
)
4567 struct gpu_gen
*gen
= user
;
4569 if (gen
->options
->reschedule
)
4570 return compute_schedule(gen
);
4572 return determine_properties_original_schedule(gen
);
4575 /* Obtain a schedule for the scop, by reading it from
4576 * a file, by computing one or by determining the properties
4577 * of the original schedule.
4579 static __isl_give isl_schedule
*get_schedule(struct gpu_gen
*gen
)
4581 return ppcg_get_schedule(gen
->ctx
, gen
->options
,
4582 &compute_or_set_properties
, gen
);
4585 /* Construct the string "<a>_<b>".
4587 static char *concat(isl_ctx
*ctx
, const char *a
, const char *b
)
4592 p
= isl_printer_to_str(ctx
);
4593 p
= isl_printer_print_str(p
, a
);
4594 p
= isl_printer_print_str(p
, "_");
4595 p
= isl_printer_print_str(p
, b
);
4596 s
= isl_printer_get_str(p
);
4597 isl_printer_free(p
);
4602 /* For each array in "prog" of which an element appears in "accessed" and
4603 * that is not a read only scalar, create a zero-dimensional universe set
4604 * of which the tuple id has name "<prefix>_<name of array>" and a user
4605 * pointer pointing to the array (gpu_array_info).
4607 * If the array is local to "prog", then make sure it will be declared
4610 * Return the list of these universe sets.
4612 static __isl_give isl_union_set_list
*create_copy_filters(struct gpu_prog
*prog
,
4613 const char *prefix
, __isl_take isl_union_set
*accessed
)
4617 isl_union_set_list
*filters
;
4620 filters
= isl_union_set_list_alloc(ctx
, 0);
4621 for (i
= 0; i
< prog
->n_array
; ++i
) {
4622 struct gpu_array_info
*array
= &prog
->array
[i
];
4624 isl_set
*accessed_i
;
4628 isl_union_set
*uset
;
4630 if (gpu_array_is_read_only_scalar(array
))
4633 space
= isl_space_copy(array
->space
);
4634 accessed_i
= isl_union_set_extract_set(accessed
, space
);
4635 empty
= isl_set_plain_is_empty(accessed_i
);
4636 isl_set_free(accessed_i
);
4638 filters
= isl_union_set_list_free(filters
);
4646 array
->declare_local
= 1;
4648 name
= concat(ctx
, prefix
, array
->name
);
4649 id
= name
? isl_id_alloc(ctx
, name
, array
) : NULL
;
4651 space
= isl_space_set_alloc(ctx
, 0, 0);
4652 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4653 uset
= isl_union_set_from_set(isl_set_universe(space
));
4655 filters
= isl_union_set_list_add(filters
, uset
);
4657 isl_union_set_free(accessed
);
4662 /* Make sure that code for the statements in "filters" that
4663 * copy arrays to or from the device is only generated when
4664 * the size of the corresponding array is positive.
4665 * That is, add a set node underneath "graft" with "filters" as children
4666 * and for each child add a guard that the selects the parameter
4667 * values for which the corresponding array has a positive size.
4668 * The array is available in the user pointer of the statement identifier.
4669 * "depth" is the schedule depth of the position where "graft"
4672 static __isl_give isl_schedule_node
*insert_positive_size_guards(
4673 __isl_take isl_schedule_node
*graft
,
4674 __isl_take isl_union_set_list
*filters
, int depth
)
4678 graft
= isl_schedule_node_child(graft
, 0);
4679 graft
= isl_schedule_node_insert_set(graft
, filters
);
4680 n
= isl_schedule_node_n_children(graft
);
4681 for (i
= 0; i
< n
; ++i
) {
4682 isl_union_set
*filter
;
4683 isl_set
*domain
, *guard
;
4685 struct gpu_array_info
*array
;
4687 graft
= isl_schedule_node_child(graft
, i
);
4688 filter
= isl_schedule_node_filter_get_filter(graft
);
4689 domain
= isl_set_from_union_set(filter
);
4690 id
= isl_set_get_tuple_id(domain
);
4691 array
= isl_id_get_user(id
);
4693 isl_set_free(domain
);
4694 guard
= gpu_array_positive_size_guard(array
);
4695 guard
= isl_set_from_params(guard
);
4696 guard
= isl_set_add_dims(guard
, isl_dim_set
, depth
);
4697 graft
= isl_schedule_node_child(graft
, 0);
4698 graft
= isl_schedule_node_insert_guard(graft
, guard
);
4699 graft
= isl_schedule_node_parent(graft
);
4700 graft
= isl_schedule_node_parent(graft
);
4702 graft
= isl_schedule_node_parent(graft
);
4707 /* Create a graft for copying arrays to or from the device,
4708 * whenever the size of the array is strictly positive.
4709 * Each statement is called "<prefix>_<name of array>" and
4710 * the identifier has a user pointer pointing to the array.
4711 * The graft will be added at the position specified by "node".
4712 * "copy" contains the array elements that need to be copied.
4713 * Only arrays of which some elements need to be copied
4714 * will have a corresponding statement in the graph.
4715 * Note though that each such statement will copy the entire array.
4717 static __isl_give isl_schedule_node
*create_copy_device(struct gpu_prog
*prog
,
4718 __isl_keep isl_schedule_node
*node
, const char *prefix
,
4719 __isl_take isl_union_set
*copy
)
4724 isl_union_set
*all
, *domain
;
4725 isl_union_set_list
*filters
;
4726 isl_union_map
*extension
;
4727 isl_schedule_node
*graft
;
4730 depth
= isl_schedule_node_get_schedule_depth(node
);
4731 filters
= create_copy_filters(prog
, prefix
, copy
);
4732 all
= isl_union_set_list_union(isl_union_set_list_copy(filters
));
4734 space
= depth
< 0 ? NULL
: isl_space_set_alloc(ctx
, 0, depth
);
4735 domain
= isl_union_set_from_set(isl_set_universe(space
));
4736 extension
= isl_union_map_from_domain_and_range(domain
, all
);
4737 graft
= isl_schedule_node_from_extension(extension
);
4740 return isl_schedule_node_free(graft
);
4741 if (isl_union_set_list_n_union_set(filters
) == 0) {
4742 isl_union_set_list_free(filters
);
4746 return insert_positive_size_guards(graft
, filters
, depth
);
4749 /* Return (the universe spaces of) the arrays that are declared
4750 * inside the scop corresponding to "prog" and for which all
4751 * potential writes inside the scop form a subset of "domain".
4753 static __isl_give isl_union_set
*extract_local_accesses(struct gpu_prog
*prog
,
4754 __isl_keep isl_union_set
*domain
)
4757 isl_union_set
*local
;
4759 local
= isl_union_set_empty(isl_union_set_get_space(domain
));
4761 for (i
= 0; i
< prog
->n_array
; ++i
) {
4763 isl_union_map
*to_outer
;
4764 isl_union_map
*may_write
;
4765 isl_union_set
*write_domain
;
4766 isl_union_set
*fields
;
4769 if (!prog
->array
[i
].local
)
4772 set
= isl_set_universe(isl_space_copy(prog
->array
[i
].space
));
4773 to_outer
= isl_union_map_copy(prog
->to_outer
);
4774 to_outer
= isl_union_map_intersect_range(to_outer
,
4775 isl_union_set_from_set(isl_set_copy(set
)));
4776 fields
= isl_union_map_domain(to_outer
);
4777 may_write
= isl_union_map_copy(prog
->may_write
);
4778 may_write
= isl_union_map_intersect_range(may_write
, fields
);
4779 write_domain
= isl_union_map_domain(may_write
);
4780 subset
= isl_union_set_is_subset(write_domain
, domain
);
4781 isl_union_set_free(write_domain
);
4785 return isl_union_set_free(local
);
4786 } else if (subset
) {
4787 local
= isl_union_set_add_set(local
, set
);
4796 /* Internal data structure for node_may_persist.
4798 * "tagger" maps tagged iteration domains to the corresponding untagged
4801 * "may_persist_flow" is the set of all tagged dataflow dependences
4802 * with those dependences removed that either precede or follow
4803 * the kernel launch in a sequence.
4804 * "inner_band_flow" is the set of all tagged dataflow dependences
4805 * that are local to a given iteration of the outer band nodes
4806 * with respect to the current node.
4807 * "local_flow" is equal to "inner_band_flow", except that the domain
4808 * and the range have been intersected with intermediate filters
4809 * on children of sets or sequences.
4811 struct ppcg_may_persist_data
{
4812 isl_union_pw_multi_aff
*tagger
;
4814 isl_union_map
*local_flow
;
4815 isl_union_map
*inner_band_flow
;
4816 isl_union_map
*may_persist_flow
;
4819 /* Update the information in "data" based on the band ancestor "node".
4821 * In particular, we restrict the dependences in data->local_flow
4822 * to those dependence where the source and the sink occur in
4823 * the same iteration of the given band node.
4824 * We also update data->inner_band_flow to the new value of
4827 static int update_may_persist_at_band(__isl_keep isl_schedule_node
*node
,
4828 struct ppcg_may_persist_data
*data
)
4830 isl_multi_union_pw_aff
*partial
;
4831 isl_union_pw_multi_aff
*contraction
;
4832 isl_union_map
*flow
;
4834 if (isl_schedule_node_band_n_member(node
) == 0)
4837 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4838 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4839 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4841 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4842 isl_union_pw_multi_aff_copy(data
->tagger
));
4844 flow
= data
->local_flow
;
4845 flow
= isl_union_map_eq_at_multi_union_pw_aff(flow
, partial
);
4846 data
->local_flow
= flow
;
4848 isl_union_map_free(data
->inner_band_flow
);
4849 data
->inner_band_flow
= isl_union_map_copy(data
->local_flow
);
4854 /* Given a set of local reaching domain elements "domain",
4855 * expand them to the corresponding leaf domain elements using "contraction"
4856 * and insert the array references tags using data->tagger.
4858 static __isl_give isl_union_set
*expand_and_tag(
4859 __isl_take isl_union_set
*domain
,
4860 __isl_take isl_union_pw_multi_aff
*contraction
,
4861 struct ppcg_may_persist_data
*data
)
4863 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4865 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4866 isl_union_pw_multi_aff_copy(data
->tagger
));
4870 /* Given a filter node that is the child of a set or sequence node,
4871 * restrict data->local_flow to refer only to those elements
4872 * in the filter of the node.
4873 * "contraction" maps the leaf domain elements of the schedule tree
4874 * to the corresponding domain elements at (the parent of) "node".
4876 static int filter_flow(__isl_keep isl_schedule_node
*node
,
4877 struct ppcg_may_persist_data
*data
,
4878 __isl_take isl_union_pw_multi_aff
*contraction
)
4880 isl_union_set
*filter
;
4881 isl_union_map
*flow
;
4883 flow
= data
->local_flow
;
4884 filter
= isl_schedule_node_filter_get_filter(node
);
4885 filter
= expand_and_tag(filter
, contraction
, data
);
4886 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(filter
));
4887 flow
= isl_union_map_intersect_range(flow
, filter
);
4888 data
->local_flow
= flow
;
4893 /* Given a filter node "node", collect the filters on all preceding siblings
4894 * (which are also filter nodes), add them to "filters" and return the result.
4896 static __isl_give isl_union_set
*add_previous_filters(
4897 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4899 isl_schedule_node
*sibling
;
4901 sibling
= isl_schedule_node_copy(node
);
4902 while (sibling
&& isl_schedule_node_has_previous_sibling(sibling
)) {
4903 isl_union_set
*filter
;
4905 sibling
= isl_schedule_node_previous_sibling(sibling
);
4906 filter
= isl_schedule_node_filter_get_filter(sibling
);
4907 filters
= isl_union_set_union(filters
, filter
);
4909 isl_schedule_node_free(sibling
);
4911 return isl_union_set_free(filters
);
4916 /* Given a filter node "node", collect the filters on all following siblings
4917 * (which are also filter nodes), add them to "filters" and return the result.
4919 static __isl_give isl_union_set
*add_next_filters(
4920 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4922 isl_schedule_node
*sibling
;
4924 sibling
= isl_schedule_node_copy(node
);
4925 while (sibling
&& isl_schedule_node_has_next_sibling(sibling
)) {
4926 isl_union_set
*filter
;
4928 sibling
= isl_schedule_node_next_sibling(sibling
);
4929 filter
= isl_schedule_node_filter_get_filter(sibling
);
4930 filters
= isl_union_set_union(filters
, filter
);
4932 isl_schedule_node_free(sibling
);
4934 return isl_union_set_free(filters
);
4939 /* Remove those flow dependences from data->may_persist_flow
4940 * that flow between elements of "domain" within the same iteration
4941 * of all outer band nodes.
4942 * "contraction" maps the leaf domain elements of the schedule tree
4943 * to the corresponding elements "domain".
4945 static void remove_external_flow(struct ppcg_may_persist_data
*data
,
4946 __isl_take isl_union_set
*domain
,
4947 __isl_keep isl_union_pw_multi_aff
*contraction
)
4949 isl_union_map
*flow
;
4951 contraction
= isl_union_pw_multi_aff_copy(contraction
);
4952 domain
= expand_and_tag(domain
, contraction
, data
);
4953 flow
= isl_union_map_copy(data
->local_flow
);
4954 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(domain
));
4955 flow
= isl_union_map_intersect_range(flow
, domain
);
4957 data
->may_persist_flow
= isl_union_map_subtract(data
->may_persist_flow
,
4961 /* Update the information in "data" based on the filter ancestor "node".
4962 * We only need to modify anything if the filter is the child
4963 * of a set or sequence node.
4965 * In the case of a sequence, we remove the dependences between
4966 * statement instances that are both executed either before or
4967 * after the subtree that will be mapped to a kernel, within
4968 * the same iteration of outer bands.
4970 * In both cases, we restrict data->local_flow to the current child.
4972 static int update_may_persist_at_filter(__isl_keep isl_schedule_node
*node
,
4973 struct ppcg_may_persist_data
*data
)
4975 enum isl_schedule_node_type type
;
4976 isl_schedule_node
*parent
;
4978 isl_union_pw_multi_aff
*contraction
;
4979 isl_union_set
*before
, *after
, *filter
;
4981 type
= isl_schedule_node_get_parent_type(node
);
4982 if (type
!= isl_schedule_node_sequence
&& type
!= isl_schedule_node_set
)
4985 parent
= isl_schedule_node_copy(node
);
4986 parent
= isl_schedule_node_parent(parent
);
4987 contraction
= isl_schedule_node_get_subtree_contraction(parent
);
4988 isl_schedule_node_free(parent
);
4990 if (type
== isl_schedule_node_set
)
4991 return filter_flow(node
, data
, contraction
);
4993 filter
= isl_schedule_node_filter_get_filter(node
);
4994 space
= isl_union_set_get_space(filter
);
4995 isl_union_set_free(filter
);
4996 before
= isl_union_set_empty(space
);
4997 after
= isl_union_set_copy(before
);
4998 before
= add_previous_filters(before
, node
);
4999 after
= add_next_filters(after
, node
);
5001 remove_external_flow(data
, before
, contraction
);
5002 remove_external_flow(data
, after
, contraction
);
5004 return filter_flow(node
, data
, contraction
);
5007 /* Update the information in "data" based on the ancestor "node".
5009 static isl_stat
update_may_persist_at(__isl_keep isl_schedule_node
*node
,
5012 struct ppcg_may_persist_data
*data
= user
;
5014 switch (isl_schedule_node_get_type(node
)) {
5015 case isl_schedule_node_error
:
5016 return isl_stat_error
;
5017 case isl_schedule_node_context
:
5018 case isl_schedule_node_domain
:
5019 case isl_schedule_node_expansion
:
5020 case isl_schedule_node_extension
:
5021 case isl_schedule_node_guard
:
5022 case isl_schedule_node_leaf
:
5023 case isl_schedule_node_mark
:
5024 case isl_schedule_node_sequence
:
5025 case isl_schedule_node_set
:
5027 case isl_schedule_node_band
:
5028 if (update_may_persist_at_band(node
, data
) < 0)
5029 return isl_stat_error
;
5031 case isl_schedule_node_filter
:
5032 if (update_may_persist_at_filter(node
, data
) < 0)
5033 return isl_stat_error
;
5040 /* Determine the set of array elements that may need to be perserved
5041 * by a kernel constructed from the subtree at "node".
5042 * This includes the set of array elements that may need to be preserved
5043 * by the entire scop (prog->may_persist) and the elements for which
5044 * there is a potential flow dependence that may cross a kernel launch.
5046 * To determine the second set, we start from all flow dependences.
5047 * From this set of dependences, we remove those that cannot possibly
5048 * require data to be preserved by a kernel launch.
5049 * In particular, we consider the following sets of dependences.
5050 * - dependences of which the write occurs inside the kernel.
5051 * If the data is needed outside the kernel, then it will
5052 * be copied out immediately after the kernel launch, so there
5053 * is no need for any special care.
5054 * - dependences of which the read occurs inside the kernel and the
5055 * corresponding write occurs inside the same iteration of the
5056 * outer band nodes. This means that the data is needed in
5057 * the first kernel launch after the write, which is already
5058 * taken care of by the standard copy-in. That is, the data
5059 * do not need to be preserved by any intermediate call to
5061 * - dependences of which the write and the read either both occur
5062 * before the kernel launch or both occur after the kernel launch,
5063 * within the same iteration of the outer band nodes with respect
5064 * to the sequence that determines the ordering of the dependence
5065 * and the kernel launch. Such flow dependences cannot cross
5066 * any kernel launch.
5068 * For the remaining (tagged) dependences, we take the domain
5069 * (i.e., the tagged writes) and apply the tagged access relation
5070 * to obtain the accessed data elements.
5071 * These are then combined with the elements that may need to be
5072 * preserved by the entire scop.
5074 static __isl_give isl_union_set
*node_may_persist(
5075 __isl_keep isl_schedule_node
*node
, struct gpu_prog
*prog
)
5077 struct ppcg_may_persist_data data
;
5078 isl_union_pw_multi_aff
*contraction
;
5079 isl_union_set
*domain
;
5080 isl_union_set
*persist
;
5081 isl_union_map
*flow
, *local_flow
;
5083 data
.tagger
= prog
->scop
->tagger
;
5085 flow
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
5086 data
.local_flow
= isl_union_map_copy(flow
);
5087 data
.inner_band_flow
= isl_union_map_copy(flow
);
5088 data
.may_persist_flow
= flow
;
5089 if (isl_schedule_node_foreach_ancestor_top_down(node
,
5090 &update_may_persist_at
, &data
) < 0)
5091 data
.may_persist_flow
=
5092 isl_union_map_free(data
.may_persist_flow
);
5093 flow
= data
.may_persist_flow
;
5094 isl_union_map_free(data
.local_flow
);
5096 domain
= isl_schedule_node_get_domain(node
);
5097 contraction
= isl_schedule_node_get_subtree_contraction(node
);
5098 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5100 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5101 isl_union_pw_multi_aff_copy(data
.tagger
));
5102 flow
= isl_union_map_subtract_domain(flow
, isl_union_set_copy(domain
));
5103 local_flow
= data
.inner_band_flow
;
5104 local_flow
= isl_union_map_intersect_range(local_flow
, domain
);
5105 flow
= isl_union_map_subtract(flow
, local_flow
);
5107 persist
= isl_union_map_domain(flow
);
5108 persist
= isl_union_set_apply(persist
,
5109 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5110 persist
= isl_union_set_union(persist
,
5111 isl_union_set_copy(prog
->may_persist
));
5116 /* Add nodes for copying outer arrays in and out of the device
5117 * before and after the subtree "node", which contains one or more kernels.
5118 * "domain" contains the original statement instances, i.e.,
5119 * those that correspond to the domains of the access relations in "prog".
5120 * In particular, the domain has not been contracted in any way.
5121 * "prefix" contains the prefix schedule at that point, in terms
5122 * of the same original statement instances.
5124 * We first compute the sets of outer array elements that need
5125 * to be copied in and out and then graft in the nodes for
5126 * performing this copying.
5128 * In particular, for each array that is possibly written anywhere in
5129 * the subtree "node" and that may be used after "node"
5130 * or that may be visible outside the corresponding scop,
5131 * we copy out its entire extent.
5133 * Any array elements that is read without first being written inside
5134 * the subtree "node" needs to be copied in.
5135 * Furthermore, if there are any array elements that
5136 * are copied out, but that may not be written inside "node, then
5137 * they also need to be copied in to ensure that the value after execution
5138 * is the same as the value before execution, at least for those array
5139 * elements that may have their values preserved by the scop or that
5140 * may be written before "node" and read after "node".
5141 * In case the array elements are structures, we need to take into
5142 * account that all members of the structures need to be written
5143 * by "node" before we can avoid copying the data structure in.
5145 * Note that the may_write relation is intersected with the domain,
5146 * which has been intersected with the context.
5147 * This helps in those cases where the arrays are declared with a fixed size,
5148 * while the accesses are parametric and the context assigns a fixed value
5149 * to the parameters.
5151 * If an element from a local array is read without first being written,
5152 * then there is no point in copying it in since it cannot have been
5153 * written prior to the scop. Warn about the uninitialized read instead.
5155 static __isl_give isl_schedule_node
*add_to_from_device(
5156 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
5157 __isl_take isl_union_map
*prefix
, struct gpu_prog
*prog
)
5159 isl_union_set
*local
;
5160 isl_union_set
*may_persist
;
5161 isl_union_map
*may_write
, *must_write
, *copy_out
, *not_written
;
5162 isl_union_map
*read
, *copy_in
;
5163 isl_union_map
*tagged
;
5164 isl_union_map
*local_uninitialized
;
5165 isl_schedule_node
*graft
;
5167 tagged
= isl_union_map_copy(prog
->scop
->tagged_reads
);
5168 tagged
= isl_union_map_union(tagged
,
5169 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5171 may_write
= isl_union_map_copy(prog
->may_write
);
5172 may_write
= isl_union_map_intersect_domain(may_write
,
5173 isl_union_set_copy(domain
));
5174 may_write
= remove_local_accesses(prog
,
5175 isl_union_map_copy(tagged
), may_write
,
5176 isl_union_map_copy(prefix
), 0);
5177 may_write
= isl_union_map_apply_range(may_write
,
5178 isl_union_map_copy(prog
->to_outer
));
5179 may_write
= isl_union_map_apply_domain(may_write
,
5180 isl_union_map_copy(prefix
));
5181 may_write
= approximate_copy_out(may_write
, prog
);
5182 copy_out
= isl_union_map_copy(may_write
);
5183 may_write
= isl_union_map_apply_range(may_write
,
5184 isl_union_map_copy(prog
->to_inner
));
5185 must_write
= isl_union_map_copy(prog
->must_write
);
5186 must_write
= isl_union_map_apply_domain(must_write
,
5187 isl_union_map_copy(prefix
));
5188 may_persist
= node_may_persist(node
, prog
);
5189 may_write
= isl_union_map_intersect_range(may_write
, may_persist
);
5190 not_written
= isl_union_map_subtract(may_write
, must_write
);
5192 local
= extract_local_accesses(prog
, domain
);
5193 read
= isl_union_map_copy(prog
->read
);
5194 read
= isl_union_map_intersect_domain(read
, domain
);
5195 read
= remove_local_accesses(prog
, tagged
, read
,
5196 isl_union_map_copy(prefix
), 1);
5197 local
= isl_union_set_apply(local
, isl_union_map_copy(prog
->to_inner
));
5198 local_uninitialized
= isl_union_map_copy(prog
->scop
->live_in
);
5199 local_uninitialized
= isl_union_map_intersect_range(local_uninitialized
,
5201 local_uninitialized
= isl_union_map_intersect(local_uninitialized
,
5202 isl_union_map_copy(read
));
5203 if (!isl_union_map_is_empty(local_uninitialized
)) {
5205 "possibly uninitialized reads (not copied in):\n");
5206 isl_union_map_dump(local_uninitialized
);
5208 read
= isl_union_map_subtract(read
, local_uninitialized
);
5209 read
= isl_union_map_apply_domain(read
, prefix
);
5210 copy_in
= isl_union_map_union(read
, not_written
);
5211 copy_in
= isl_union_map_apply_range(copy_in
,
5212 isl_union_map_copy(prog
->to_outer
));
5214 graft
= create_copy_device(prog
, node
, "to_device",
5215 isl_union_map_range(copy_in
));
5216 node
= isl_schedule_node_graft_before(node
, graft
);
5217 graft
= create_copy_device(prog
, node
, "from_device",
5218 isl_union_map_range(copy_out
));
5219 node
= isl_schedule_node_graft_after(node
, graft
);
5224 /* Add nodes for initializing ("init_device") and clearing ("clear_device")
5225 * the device before and after "node".
5227 static __isl_give isl_schedule_node
*add_init_clear_device(
5228 __isl_take isl_schedule_node
*node
)
5232 isl_union_set
*domain
;
5233 isl_schedule_node
*graft
;
5235 ctx
= isl_schedule_node_get_ctx(node
);
5237 space
= isl_space_set_alloc(ctx
, 0, 0);
5238 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "init_device");
5239 domain
= isl_union_set_from_set(isl_set_universe(space
));
5240 graft
= isl_schedule_node_from_domain(domain
);
5242 node
= isl_schedule_node_graft_before(node
, graft
);
5244 space
= isl_space_set_alloc(ctx
, 0, 0);
5245 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "clear_device");
5246 domain
= isl_union_set_from_set(isl_set_universe(space
));
5247 graft
= isl_schedule_node_from_domain(domain
);
5249 node
= isl_schedule_node_graft_after(node
, graft
);
5254 /* Update "schedule" for mapping to a GPU device.
5256 * In particular, insert a context node, create kernels for
5257 * each outermost tilable band and introduce nodes for copying arrays
5258 * in and out of the device and for initializing and clearing the device.
5259 * If the child of the initial root points to a set node,
5260 * then children of this node that do not contain any tilable bands
5261 * are separated from the other children and are not mapped to
5264 * The GPU code is generated in a context where at least one
5265 * statement instance is executed. The corresponding guard is inserted
5266 * around the entire schedule.
5268 static __isl_give isl_schedule
*map_to_device(struct gpu_gen
*gen
,
5269 __isl_take isl_schedule
*schedule
)
5271 isl_schedule_node
*node
;
5274 isl_union_set
*domain
;
5275 isl_union_map
*prefix
;
5276 isl_union_pw_multi_aff
*contraction
;
5277 struct gpu_prog
*prog
;
5279 context
= isl_set_copy(gen
->prog
->context
);
5280 context
= isl_set_from_params(context
);
5281 schedule
= isl_schedule_insert_context(schedule
, context
);
5284 guard
= isl_union_set_params(isl_union_set_copy(prog
->scop
->domain
));
5285 prog
->context
= isl_set_intersect(prog
->context
, isl_set_copy(guard
));
5286 guard
= isl_set_from_params(guard
);
5288 node
= isl_schedule_get_root(schedule
);
5289 isl_schedule_free(schedule
);
5290 node
= isl_schedule_node_child(node
, 0);
5291 node
= isl_schedule_node_child(node
, 0);
5292 node
= isolate_permutable_subtrees(node
, gen
->prog
);
5293 domain
= isl_schedule_node_get_domain(node
);
5294 contraction
= isl_schedule_node_get_subtree_contraction(node
);
5295 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5296 isl_union_pw_multi_aff_copy(contraction
));
5297 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
5298 prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(prefix
,
5300 node
= mark_kernels(gen
, node
);
5301 node
= add_to_from_device(node
, domain
, prefix
, gen
->prog
);
5302 node
= isl_schedule_node_root(node
);
5303 node
= isl_schedule_node_child(node
, 0);
5304 node
= isl_schedule_node_child(node
, 0);
5305 node
= isl_schedule_node_insert_guard(node
, guard
);
5306 node
= isl_schedule_node_child(node
, 0);
5307 node
= add_init_clear_device(node
);
5308 schedule
= isl_schedule_node_get_schedule(node
);
5309 isl_schedule_node_free(node
);
5314 /* Internal data structure for extract_access.
5315 * "next_access" points to the end of a linked list that is extended
5316 * by extract_access.
5317 * "single_expression" is set if the access expressions belong to
5318 * an expression statement (i.e., a statement without internal control).
5319 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5321 struct ppcg_extract_access_data
{
5322 struct gpu_stmt_access
**next_access
;
5323 int single_expression
;
5324 isl_union_map
*any_to_outer
;
5327 /* Given a tagged access relation to a single array "tagged", extract it
5328 * as a map, taking into account that the input may be empty.
5329 * If the access relation is empty, then it does not contain
5330 * any space information, so we try to recover it from the index
5332 * The space of the index expression is of the form I -> A,
5333 * with I the statement instances and A the array, or [I -> F] -> A,
5334 * with F the filters corresponding to arguments.
5335 * We first drop F, if present, obtaining I -> A.
5336 * Then we construct I -> R, with R the reference tag,
5337 * combine the two into I -> [R -> A] and uncurry to obtain
5338 * the final result [I -> R] -> A.
5339 * Note that the index expression may have a lower dimension
5340 * than that of the array, but this dimension is not used
5341 * if the access relation is empty.
5343 static __isl_give isl_map
*extract_single_tagged_access(
5344 __isl_take isl_union_map
*tagged
, __isl_keep pet_expr
*expr
)
5348 isl_space
*space
, *space2
;
5349 isl_multi_pw_aff
*index
;
5351 empty
= isl_union_map_is_empty(tagged
);
5355 return isl_map_from_union_map(tagged
);
5356 isl_union_map_free(tagged
);
5358 index
= pet_expr_access_get_index(expr
);
5359 space
= isl_multi_pw_aff_get_space(index
);
5360 isl_multi_pw_aff_free(index
);
5361 if (isl_space_domain_is_wrapping(space
))
5362 space
= isl_space_domain_factor_domain(space
);
5363 space2
= isl_space_copy(space
);
5364 space2
= isl_space_from_domain(isl_space_domain(space
));
5365 id
= pet_expr_access_get_ref_id(expr
);
5366 space2
= isl_space_set_tuple_id(space2
, isl_dim_out
, id
);
5367 space
= isl_space_range_product(space2
, space
);
5368 space
= isl_space_uncurry(space
);
5370 return isl_map_empty(space
);
5372 isl_union_map_free(tagged
);
5376 /* Does the index expression "index" of "expr" represent an access
5377 * to a single element?
5378 * That is, is "index" completely specified?
5380 * If "expr" accesses elements from different spaces (i.e., fields
5381 * of a structure), then it does not access a single element.
5382 * Otherwise, if the single space of the access matches the space
5383 * of "index", then the index expression is completely specified
5384 * (no pointer to a lower-dimensional slice of the accessed array)
5385 * and a single element is being accessed.
5387 static isl_bool
complete_index(__isl_keep pet_expr
*expr
,
5388 __isl_keep isl_multi_pw_aff
*index
)
5390 isl_union_map
*read
, *write
, *all
;
5392 isl_space
*space1
, *space2
;
5395 read
= pet_expr_access_get_may_read(expr
);
5396 write
= pet_expr_access_get_may_write(expr
);
5397 all
= isl_union_map_union(read
, write
);
5399 return isl_bool_error
;
5400 if (isl_union_map_n_map(all
) != 1) {
5401 isl_union_map_free(all
);
5402 return isl_bool_false
;
5404 map
= isl_map_from_union_map(all
);
5405 space1
= isl_map_get_space(map
);
5407 space2
= isl_multi_pw_aff_get_space(index
);
5408 complete
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
5409 space2
, isl_dim_out
);
5410 isl_space_free(space1
);
5411 isl_space_free(space2
);
5416 /* Does "expr" access a single, fixed element (independently of the statement
5418 * That is, does it have a completely specified constant index expression?
5420 * Note that it is not sufficient for the index expression to be
5421 * piecewise constant. isl_multi_pw_aff_is_cst can therefore not be used.
5423 static isl_bool
accesses_fixed_element(__isl_keep pet_expr
*expr
)
5426 isl_multi_pw_aff
*index
;
5427 isl_bool fixed
= isl_bool_true
;
5429 index
= pet_expr_access_get_index(expr
);
5431 return isl_bool_error
;
5432 n
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5433 for (i
= 0; i
< n
; ++i
) {
5436 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
5437 fixed
= isl_pw_aff_n_piece(pa
) == 1;
5439 fixed
= isl_pw_aff_is_cst(pa
);
5440 isl_pw_aff_free(pa
);
5441 if (fixed
< 0 || !fixed
)
5444 if (fixed
>= 0 && fixed
)
5445 fixed
= complete_index(expr
, index
);
5446 isl_multi_pw_aff_free(index
);
5451 /* Extract a gpu_stmt_access from "expr", append it to the list
5452 * that ends in *data->next_access and update the end of the list.
5453 * If the access expression performs a write, then it is considered
5454 * exact only if it appears in a single expression statement and
5455 * if its may access relation is equal to its must access relation.
5457 * The combined set of may accesses may be a union if member accesses
5458 * are involved, but the entire set is derived from a single reference and
5459 * therefore from a single index expression. These accesses therefore
5460 * all map to the same outer array.
5462 static int extract_access(__isl_keep pet_expr
*expr
, void *user
)
5464 struct ppcg_extract_access_data
*data
= user
;
5465 isl_union_map
*tagged
;
5466 struct gpu_stmt_access
*access
;
5467 isl_ctx
*ctx
= pet_expr_get_ctx(expr
);
5468 isl_multi_pw_aff
*index
;
5470 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
5472 access
->next
= NULL
;
5473 access
->read
= pet_expr_access_is_read(expr
);
5474 access
->write
= pet_expr_access_is_write(expr
);
5475 tagged
= pet_expr_access_get_tagged_may_read(expr
);
5476 tagged
= isl_union_map_union(tagged
,
5477 pet_expr_access_get_tagged_may_write(expr
));
5478 tagged
= isl_union_map_apply_range(tagged
,
5479 isl_union_map_copy(data
->any_to_outer
));
5480 if (!access
->write
) {
5481 access
->exact_write
= 1;
5482 } else if (!data
->single_expression
) {
5483 access
->exact_write
= 0;
5485 isl_union_map
*must
, *may
;
5486 may
= isl_union_map_copy(tagged
);
5487 may
= isl_union_map_domain_factor_domain(may
);
5488 must
= pet_expr_access_get_must_write(expr
);
5489 access
->exact_write
= isl_union_map_is_equal(must
, may
);
5490 isl_union_map_free(must
);
5491 isl_union_map_free(may
);
5493 index
= pet_expr_access_get_index(expr
);
5494 access
->n_index
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5495 isl_multi_pw_aff_free(index
);
5496 access
->ref_id
= pet_expr_access_get_ref_id(expr
);
5497 access
->tagged_access
= extract_single_tagged_access(tagged
, expr
);
5498 access
->access
= isl_map_copy(access
->tagged_access
);
5499 access
->access
= isl_map_domain_factor_domain(access
->access
);
5500 access
->fixed_element
= accesses_fixed_element(expr
);
5502 *data
->next_access
= access
;
5503 data
->next_access
= &(*data
->next_access
)->next
;
5505 if (!access
->access
|| access
->fixed_element
< 0)
5511 /* Construct a linked list of gpu_stmt_access objects,
5512 * one for each access expression in the statement body.
5513 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5515 static int pet_stmt_extract_accesses(struct gpu_stmt
*stmt
,
5516 __isl_keep isl_union_map
*any_to_outer
)
5518 struct ppcg_extract_access_data data
;
5520 stmt
->accesses
= NULL
;
5521 data
.next_access
= &stmt
->accesses
;
5522 data
.single_expression
=
5523 pet_tree_get_type(stmt
->stmt
->body
) == pet_tree_expr
;
5524 data
.any_to_outer
= any_to_outer
;
5525 return pet_tree_foreach_access_expr(stmt
->stmt
->body
,
5526 &extract_access
, &data
);
5529 /* Has statement "stmt" been killed from "scop"?
5530 * That is, is the instance set of "scop" free from any
5531 * instances of "stmt"?
5533 static isl_bool
is_stmt_killed(struct ppcg_scop
*scop
, struct pet_stmt
*stmt
)
5540 return isl_bool_error
;
5541 space
= isl_set_get_space(stmt
->domain
);
5542 left
= isl_union_set_extract_set(scop
->domain
, space
);
5543 empty
= isl_set_plain_is_empty(left
);
5549 /* Return an array of gpu_stmt representing the statements in "scop".
5550 * Do not collect array accesses for statements that have been killed.
5552 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
5553 __isl_keep isl_union_map
*any_to_outer
)
5556 struct gpu_stmt
*stmts
;
5558 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->pet
->n_stmt
);
5562 for (i
= 0; i
< scop
->pet
->n_stmt
; ++i
) {
5563 struct gpu_stmt
*s
= &stmts
[i
];
5566 s
->id
= isl_set_get_tuple_id(scop
->pet
->stmts
[i
]->domain
);
5567 s
->stmt
= scop
->pet
->stmts
[i
];
5568 killed
= is_stmt_killed(scop
, scop
->pet
->stmts
[i
]);
5570 return free_stmts(stmts
, i
+ 1);
5573 if (pet_stmt_extract_accesses(s
, any_to_outer
) < 0)
5574 return free_stmts(stmts
, i
+ 1);
5580 /* Generate CUDA code for "scop" and print it to "p".
5581 * After generating an AST for the transformed scop as explained below,
5582 * we call "gen->print" to print the AST in the desired output format
5585 * If it turns out that it does not make sense to generate GPU code,
5586 * then we generate CPU code instead.
5588 * The declarations of the arrays that are visible outside of the scop
5589 * are printed outside of the code generated from the schedule,
5590 * because the generated code may involve a guard around the entire code.
5592 * We first compute a schedule that respects the dependences
5593 * of the original program and select the outermost bands
5594 * of tilable dimensions that have at least one parallel loop.
5595 * If the --load-schedule is specified, then the loaded schedule
5596 * is used instead of a computed schedule.
5598 * Each of these bands B is then tiled according to "tile" sizes, resulting
5599 * in two nested bands, with a kernel marker on top
5607 * We then split off at most 2 parallel dimensions from the T band and
5608 * at most 3 parallel dimension from the P band
5621 * A filter is introduced in front of T1 that maps the domain instances
5622 * to block identifiers. Similarly, a filter is introduced in front of P1
5623 * that maps the domain instances to thread identifiers.
5625 * For each iteration of the T2 band and for each array, we compute
5626 * the array elements accessed by that iteration, construct a rectangular
5627 * box around it and shift it to the origin. The result is used
5628 * as shared memory for the array.
5630 * Copying and synchronization statements are added to this schedule tree.
5631 * In principle, these are added in front of the P1 band, but some of
5632 * them may get hoisted up to higher levels.
5634 * The entire AST is then generated from the single resulting schedule tree.
5635 * During the generation the subtrees at kernel nodes (K) are saved
5636 * aside and replaced by kernel calls. The result is printed as host code
5637 * while the saved subtrees are printed as device code.
5639 static __isl_give isl_printer
*generate(__isl_take isl_printer
*p
,
5640 struct gpu_gen
*gen
, struct ppcg_scop
*scop
,
5641 struct ppcg_options
*options
)
5643 struct gpu_prog
*prog
;
5645 isl_schedule
*schedule
;
5646 isl_bool any_permutable
;
5649 return isl_printer_free(p
);
5651 ctx
= isl_printer_get_ctx(p
);
5652 prog
= gpu_prog_alloc(ctx
, scop
);
5654 return isl_printer_free(p
);
5657 schedule
= get_schedule(gen
);
5659 any_permutable
= has_any_permutable_node(schedule
);
5660 if (any_permutable
< 0 || !any_permutable
) {
5661 if (any_permutable
< 0)
5662 p
= isl_printer_free(p
);
5664 p
= print_cpu(p
, scop
, options
);
5665 isl_schedule_free(schedule
);
5667 schedule
= map_to_device(gen
, schedule
);
5668 gen
->tree
= generate_code(gen
, schedule
);
5669 p
= ppcg_set_macro_names(p
);
5670 p
= ppcg_print_exposed_declarations(p
, prog
->scop
);
5671 p
= gen
->print(p
, gen
->prog
, gen
->tree
, &gen
->types
,
5673 isl_ast_node_free(gen
->tree
);
5676 gpu_prog_free(prog
);
5681 /* Wrapper around generate for use as a ppcg_transform callback.
5683 static __isl_give isl_printer
*generate_wrap(__isl_take isl_printer
*p
,
5684 struct ppcg_scop
*scop
, void *user
)
5686 struct gpu_gen
*gen
= user
;
5688 return generate(p
, gen
, scop
, gen
->options
);
5691 /* Transform the code in the file called "input" by replacing
5692 * all scops by corresponding GPU code and write the results to "out".
5694 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
5695 struct ppcg_options
*options
,
5696 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
5697 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
5698 struct gpu_types
*types
, void *user
), void *user
)
5705 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
5706 gen
.options
= options
;
5709 gen
.print_user
= user
;
5711 gen
.types
.name
= NULL
;
5713 if (options
->debug
->dump_sizes
) {
5714 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
5715 gen
.used_sizes
= isl_union_map_empty(space
);
5718 r
= ppcg_transform(ctx
, input
, out
, options
, &generate_wrap
, &gen
);
5720 if (options
->debug
->dump_sizes
) {
5721 isl_union_map_dump(gen
.used_sizes
);
5722 isl_union_map_free(gen
.used_sizes
);
5725 isl_union_map_free(gen
.sizes
);
5726 for (i
= 0; i
< gen
.types
.n
; ++i
)
5727 free(gen
.types
.name
[i
]);
5728 free(gen
.types
.name
);
5733 /* Compute the set of inner array elements that may have their values
5734 * preserved by "prog". In particular, collect the array elements of
5735 * arrays that are not local to "prog" and remove those elements that
5736 * are definitely killed or definitely written by "prog".
5738 static __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
)
5741 isl_union_set
*may_persist
, *killed
;
5742 isl_union_map
*must_kill
;
5744 may_persist
= isl_union_set_empty(isl_set_get_space(prog
->context
));
5745 for (i
= 0; i
< prog
->n_array
; ++i
) {
5748 if (prog
->array
[i
].local
)
5751 extent
= isl_set_copy(prog
->array
[i
].extent
);
5752 may_persist
= isl_union_set_add_set(may_persist
, extent
);
5755 may_persist
= isl_union_set_intersect_params(may_persist
,
5756 isl_set_copy(prog
->context
));
5757 may_persist
= isl_union_set_apply(may_persist
,
5758 isl_union_map_copy(prog
->to_inner
));
5759 must_kill
= isl_union_map_copy(prog
->tagged_must_kill
);
5760 killed
= isl_union_map_range(must_kill
);
5761 must_kill
= isl_union_map_copy(prog
->must_write
);
5762 killed
= isl_union_set_union(killed
, isl_union_map_range(must_kill
));
5764 may_persist
= isl_union_set_subtract(may_persist
, killed
);
5768 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
5770 struct gpu_prog
*prog
;
5777 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
5782 prog
->context
= isl_set_copy(scop
->context
);
5783 prog
->n_stmts
= scop
->pet
->n_stmt
;
5784 prog
->any_to_outer
= pet_scop_compute_outer_to_any(scop
->pet
);
5785 prog
->any_to_outer
= isl_union_map_reverse(prog
->any_to_outer
);
5786 space
= isl_union_map_get_space(prog
->any_to_outer
);
5787 space
= isl_space_set_from_params(space
);
5788 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
5789 space
= isl_space_map_from_set(space
);
5790 id
= isl_map_identity(space
);
5791 prog
->any_to_outer
= isl_union_map_add_map(prog
->any_to_outer
, id
);
5792 prog
->stmts
= extract_stmts(ctx
, scop
, prog
->any_to_outer
);
5793 prog
->read
= isl_union_map_copy(scop
->reads
);
5794 prog
->may_write
= isl_union_map_copy(scop
->may_writes
);
5795 prog
->must_write
= isl_union_map_copy(scop
->must_writes
);
5796 prog
->tagged_must_kill
= isl_union_map_copy(scop
->tagged_must_kills
);
5797 prog
->to_inner
= pet_scop_compute_outer_to_inner(scop
->pet
);
5798 prog
->to_outer
= isl_union_map_copy(prog
->to_inner
);
5799 prog
->to_outer
= isl_union_map_reverse(prog
->to_outer
);
5802 return gpu_prog_free(prog
);
5804 if (collect_array_info(prog
) < 0)
5805 return gpu_prog_free(prog
);
5806 prog
->may_persist
= compute_may_persist(prog
);
5811 void *gpu_prog_free(struct gpu_prog
*prog
)
5815 free_array_info(prog
);
5816 free_stmts(prog
->stmts
, prog
->n_stmts
);
5817 isl_union_map_free(prog
->any_to_outer
);
5818 isl_union_map_free(prog
->to_outer
);
5819 isl_union_map_free(prog
->to_inner
);
5820 isl_union_map_free(prog
->read
);
5821 isl_union_map_free(prog
->may_write
);
5822 isl_union_map_free(prog
->must_write
);
5823 isl_union_map_free(prog
->tagged_must_kill
);
5824 isl_union_map_free(prog
->array_order
);
5825 isl_union_set_free(prog
->may_persist
);
5826 isl_set_free(prog
->context
);