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 isl_stat
collect_array_info(struct gpu_prog
*prog
)
336 isl_stat r
= isl_stat_ok
;
337 isl_union_set
*arrays
;
340 prog
->array
= isl_calloc_array(prog
->ctx
,
341 struct gpu_array_info
, prog
->scop
->pet
->n_array
);
343 return isl_stat_error
;
345 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
346 arrays
= isl_union_set_union(arrays
,
347 isl_union_map_range(isl_union_map_copy(prog
->may_write
)));
349 arrays
= isl_union_set_apply(arrays
,
350 isl_union_map_copy(prog
->to_outer
));
352 arrays
= isl_union_set_coalesce(arrays
);
354 for (i
= 0; i
< prog
->scop
->pet
->n_array
; ++i
) {
357 field
= isl_set_is_wrapping(prog
->scop
->pet
->arrays
[i
]->extent
);
362 if (extract_array_info(prog
, &prog
->array
[prog
->n_array
++],
363 prog
->scop
->pet
->arrays
[i
], arrays
) < 0)
366 if (i
< prog
->scop
->pet
->n_array
)
369 isl_union_set_free(arrays
);
371 if (prog
->scop
->options
->live_range_reordering
)
372 collect_order_dependences(prog
);
377 static void free_array_info(struct gpu_prog
*prog
)
381 for (i
= 0; i
< prog
->n_array
; ++i
) {
382 free(prog
->array
[i
].type
);
383 free(prog
->array
[i
].name
);
384 isl_multi_pw_aff_free(prog
->array
[i
].bound
);
385 isl_ast_expr_free(prog
->array
[i
].bound_expr
);
386 isl_space_free(prog
->array
[i
].space
);
387 isl_set_free(prog
->array
[i
].declared_extent
);
388 isl_set_free(prog
->array
[i
].extent
);
389 isl_ast_expr_free(prog
->array
[i
].declared_size
);
390 free(prog
->array
[i
].refs
);
391 isl_union_map_free(prog
->array
[i
].dep_order
);
396 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
397 * as an array or through a pointer reference, but as a single data element.
398 * At the moment, scalars are represented as zero-dimensional arrays.
399 * Note that the single data element may be an entire structure.
401 int gpu_array_is_scalar(struct gpu_array_info
*array
)
403 return array
->n_index
== 0;
406 /* Can "array" be mapped to private memory?
407 * That is, is it only accessed as individual elements with
408 * constant index expressions?
410 isl_bool
gpu_array_can_be_private(struct gpu_array_info
*array
)
413 return isl_bool_error
;
414 return array
->only_fixed_element
;
417 /* Is "array" a read-only scalar?
419 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
421 return array
->read_only_scalar
;
424 /* Does "array" need to be allocated on the device?
425 * If it is a read-only scalar, then it will be passed as an argument
426 * to the kernel and therefore does not require any allocation.
427 * If this device memory is not accessed at all, then it does not
428 * need to be allocated either.
430 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
)
432 if (gpu_array_is_read_only_scalar(array
))
439 /* Return the set of parameter values for which the array has a positive
440 * size in all dimensions.
441 * If the sizes are only valid for some parameter values, then those
442 * constraints are also taken into account.
444 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
)
453 space
= isl_space_params(isl_space_copy(array
->space
));
454 guard
= isl_set_universe(space
);
456 for (i
= 0; i
< array
->n_index
; ++i
) {
458 isl_set
*guard_i
, *zero
;
460 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
461 guard_i
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound
));
462 zero
= isl_pw_aff_zero_set(bound
);
463 guard_i
= isl_set_subtract(guard_i
, zero
);
464 guard
= isl_set_intersect(guard
, guard_i
);
470 /* Internal data structure for extract_size_of_type.
471 * "type" specifies the name of the space that we want to extract.
472 * "res" is used to store the subset of that space.
474 struct ppcg_extract_size_data
{
479 /* This function is called for each set in a union_set.
480 * If the name of the set matches data->type, we store the
483 static isl_stat
extract_size_of_type(__isl_take isl_set
*size
, void *user
)
485 struct ppcg_extract_size_data
*data
= user
;
488 name
= isl_set_get_tuple_name(size
);
489 if (name
&& !strcmp(name
, data
->type
)) {
491 return isl_stat_error
;
498 /* Given a union map { kernel[i] -> *[...] },
499 * return the range in the space called "type" for the kernel with
500 * sequence number "id".
502 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
503 const char *type
, int id
)
507 isl_union_set
*local_sizes
;
508 struct ppcg_extract_size_data data
= { type
, NULL
};
513 space
= isl_union_map_get_space(sizes
);
514 space
= isl_space_set_from_params(space
);
515 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
516 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
517 dom
= isl_set_universe(space
);
518 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
520 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
521 isl_union_map_copy(sizes
));
522 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
523 isl_union_set_free(local_sizes
);
527 /* Given a singleton set, extract the first (at most *len) elements
528 * of the single integer tuple into *sizes and update *len if needed.
530 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
538 dim
= isl_set_dim(set
, isl_dim_set
);
542 for (i
= 0; i
< *len
; ++i
) {
545 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
548 sizes
[i
] = isl_val_get_num_si(v
);
555 /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
556 * if the option debug->dump_sizes is set.
558 static void set_used_sizes(struct gpu_gen
*gen
, const char *type
, int id
,
565 if (!gen
->options
->debug
->dump_sizes
)
568 space
= isl_union_map_get_space(gen
->used_sizes
);
569 space
= isl_space_set_from_params(space
);
570 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
571 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
572 space
= isl_space_from_domain(space
);
573 space
= isl_space_add_dims(space
, isl_dim_out
, len
);
574 space
= isl_space_set_tuple_name(space
, isl_dim_out
, type
);
576 map
= isl_map_universe(space
);
577 map
= isl_map_fix_si(map
, isl_dim_in
, 0, id
);
578 for (i
= 0; i
< len
; ++i
)
579 map
= isl_map_fix_si(map
, isl_dim_out
, i
, sizes
[i
]);
581 gen
->used_sizes
= isl_union_map_add_map(gen
->used_sizes
, map
);
584 /* Extract user specified "tile" sizes from the "sizes" command line option,
585 * defaulting to option->tile_size in each dimension.
586 * *tile_len contains the maximum number of tile sizes needed.
587 * Update *tile_len to the number of specified tile sizes, if any, and
588 * return a pointer to the tile sizes (or NULL on error).
589 * Add the effectively used sizes to gen->used_sizes.
591 static int *read_tile_sizes(struct gpu_gen
*gen
, int *tile_len
)
597 tile_size
= isl_alloc_array(gen
->ctx
, int, *tile_len
);
600 for (n
= 0; n
< *tile_len
; ++n
)
601 tile_size
[n
] = gen
->options
->tile_size
;
603 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
604 read_sizes_from_set(size
, tile_size
, tile_len
);
605 set_used_sizes(gen
, "tile", gen
->kernel_id
, tile_size
, *tile_len
);
610 /* Extract user specified "block" sizes from the "sizes" command line option,
611 * after filling in some potentially useful defaults.
613 static void read_block_sizes(struct ppcg_kernel
*kernel
,
614 __isl_keep isl_union_map
*sizes
)
618 if (kernel
->n_block
> 3)
620 switch (kernel
->n_block
) {
622 kernel
->block_dim
[0] = 512;
625 kernel
->block_dim
[0] = 32;
626 kernel
->block_dim
[1] = 16;
629 kernel
->block_dim
[0] = 32;
630 kernel
->block_dim
[1] = 4;
631 kernel
->block_dim
[2] = 4;
635 size
= extract_sizes(sizes
, "block", kernel
->id
);
636 read_sizes_from_set(size
, kernel
->block_dim
, &kernel
->n_block
);
639 /* Extract user specified "grid" sizes from the "sizes" command line option,
640 * after filling in some potentially useful defaults.
642 static void read_grid_sizes(struct ppcg_kernel
*kernel
,
643 __isl_keep isl_union_map
*sizes
)
647 if (kernel
->n_grid
> 2)
649 switch (kernel
->n_grid
) {
651 kernel
->grid_dim
[0] = 32768;
654 kernel
->grid_dim
[0] = 256;
655 kernel
->grid_dim
[1] = 256;
659 size
= extract_sizes(sizes
, "grid", kernel
->id
);
660 read_sizes_from_set(size
, kernel
->grid_dim
, &kernel
->n_grid
);
663 /* Extract user specified grid and block sizes from the gen->sizes
664 * command line option after filling in some potentially useful defaults.
665 * Store the extracted sizes in "kernel".
666 * Add the effectively used sizes to gen->used_sizes.
668 static void read_grid_and_block_sizes(struct ppcg_kernel
*kernel
,
671 read_block_sizes(kernel
, gen
->sizes
);
672 read_grid_sizes(kernel
, gen
->sizes
);
673 set_used_sizes(gen
, "block", kernel
->id
,
674 kernel
->block_dim
, kernel
->n_block
);
675 set_used_sizes(gen
, "grid", kernel
->id
,
676 kernel
->grid_dim
, kernel
->n_grid
);
679 static void *free_stmts(struct gpu_stmt
*stmts
, int n
)
686 for (i
= 0; i
< n
; ++i
) {
687 struct gpu_stmt_access
*access
, *next
;
689 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
691 isl_id_free(access
->ref_id
);
692 isl_map_free(access
->access
);
693 isl_map_free(access
->tagged_access
);
697 isl_id_free(stmts
[i
].id
);
704 /* Add parameters p[i] with identifiers "ids" to "set",
705 * with bounds to 0 <= p[i] < size[i].
707 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
708 int *size
, __isl_keep isl_id_list
*ids
)
713 len
= isl_id_list_n_id(ids
);
714 nparam
= isl_set_dim(set
, isl_dim_param
);
715 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
717 for (i
= 0; i
< len
; ++i
) {
720 id
= isl_id_list_get_id(ids
, i
);
721 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
722 set
= isl_set_lower_bound_si(set
, isl_dim_param
, nparam
+ i
, 0);
723 set
= isl_set_upper_bound_si(set
, isl_dim_param
,
724 nparam
+ i
, size
[i
] - 1);
730 /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
733 * { : 0 <= p[i] < size[i] }
735 * or an overapproximation.
737 static __isl_give isl_set
*add_bounded_parameters_dynamic(
738 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
739 __isl_keep isl_id_list
*ids
)
746 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
747 nparam
= isl_set_dim(set
, isl_dim_param
);
748 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
750 for (i
= 0; i
< len
; ++i
) {
753 id
= isl_id_list_get_id(ids
, i
);
754 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
757 space
= isl_space_params(isl_set_get_space(set
));
758 ls
= isl_local_space_from_space(space
);
759 for (i
= 0; i
< len
; ++i
) {
760 isl_pw_aff
*param
, *size_i
, *zero
;
763 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
764 isl_dim_param
, nparam
+ i
);
766 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
767 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
768 bound
= isl_set_from_basic_set(isl_set_simple_hull(bound
));
769 set
= isl_set_intersect_params(set
, bound
);
771 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
772 bound
= isl_pw_aff_ge_set(param
, zero
);
773 set
= isl_set_intersect_params(set
, bound
);
775 isl_local_space_free(ls
);
780 /* Return the union of all tagged access relations in the group.
782 static __isl_give isl_union_map
*group_tagged_access_relation(
783 struct gpu_array_ref_group
*group
)
786 isl_union_map
*access
;
788 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
789 for (i
= 0; i
< group
->n_ref
; ++i
) {
792 map_i
= isl_map_copy(group
->refs
[i
]->tagged_access
);
793 access
= isl_union_map_union(access
,
794 isl_union_map_from_map(map_i
));
800 /* Return the extent of "array", recomputed from the bounds.
801 * The recomputed extent may be simpler than the original extent.
803 static __isl_give isl_set
*array_extent(struct gpu_array_info
*array
)
811 id
= isl_set_get_tuple_id(array
->extent
);
812 space
= isl_set_get_space(array
->extent
);
813 extent
= isl_set_universe(isl_space_copy(space
));
814 ls
= isl_local_space_from_space(space
);
815 for (i
= 0; i
< array
->n_index
; ++i
) {
821 extent
= isl_set_lower_bound_si(extent
, isl_dim_set
, i
, 0);
823 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
825 index
= isl_pw_aff_from_aff(aff
);
826 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
827 bound
= isl_pw_aff_from_range(bound
);
828 bound
= isl_pw_aff_add_dims(bound
, isl_dim_in
, array
->n_index
);
829 bound
= isl_pw_aff_set_tuple_id(bound
, isl_dim_in
,
831 lt
= isl_pw_aff_lt_set(index
, bound
);
832 extent
= isl_set_intersect(extent
, lt
);
834 isl_local_space_free(ls
);
840 /* Return a map from the first group->shared_tile->depth dimensions
841 * of the computed schedule to the array tile in
842 * global memory that corresponds to the shared memory copy.
844 * In particular, return a map
850 * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
854 * 0 <= a <= array_size - 1 (2)
856 * Note that if some stride has been detected (i.e., when
857 * group->shared_tile->bound[i].shift is set), then a in (1) refers
858 * to the shifted and scaled down version.
860 * Constraints (1) are obtained by mapping the size constraints on the
861 * shared/private memory tile back to the access relation.
862 * Constraints (2) are obtained from the (recomputed) extent.
864 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
867 int n_index
= group
->array
->n_index
;
873 space
= isl_multi_aff_get_space(group
->shared_tile
->tiling
);
874 space
= isl_space_range(space
);
875 local
= isl_set_universe(space
);
876 for (i
= 0; i
< n_index
; ++i
) {
879 local
= isl_set_lower_bound_si(local
, isl_dim_set
, i
, 0);
880 bound
= isl_val_copy(group
->shared_tile
->bound
[i
].size
);
881 bound
= isl_val_sub_ui(bound
, 1);
882 local
= isl_set_upper_bound_val(local
, isl_dim_set
, i
, bound
);
884 local
= isl_set_preimage_multi_aff(local
,
885 isl_multi_aff_copy(group
->shared_tile
->tiling
));
886 tile
= isl_set_unwrap(local
);
887 extent
= array_extent(group
->array
);
888 tile
= isl_map_intersect_range(tile
, extent
);
893 /* Given a mapping "iterator_map" from the AST schedule to a domain,
894 * return the corresponding mapping from the AST schedule to
895 * to the outer kernel->copy_schedule_dim dimensions of
896 * the schedule computed by PPCG for this kernel.
898 * Note that kernel->copy_schedule_dim is at least as large as
899 * the largest depth of any array reference group associated to the kernel.
900 * This is needed as the returned schedule is used to extract a mapping
901 * to the outer tile->depth dimensions in transform_index.
903 static __isl_give isl_pw_multi_aff
*compute_sched_to_copy(
904 struct ppcg_kernel
*kernel
, __isl_take isl_pw_multi_aff
*iterator_map
)
906 isl_union_pw_multi_aff
*upma
;
907 isl_pw_multi_aff
*pma
;
910 space
= isl_space_range(isl_pw_multi_aff_get_space(iterator_map
));
911 space
= isl_space_from_domain(space
);
912 space
= isl_space_add_dims(space
, isl_dim_out
,
913 kernel
->copy_schedule_dim
);
915 upma
= isl_union_pw_multi_aff_copy(kernel
->copy_schedule
);
916 pma
= isl_union_pw_multi_aff_extract_pw_multi_aff(upma
, space
);
917 isl_union_pw_multi_aff_free(upma
);
919 return isl_pw_multi_aff_pullback_pw_multi_aff(pma
, iterator_map
);
922 /* If max_shared_memory is not set to infinity (-1), then make
923 * sure that the total amount of shared memory required by the
924 * array reference groups mapped to shared memory by "kernel"
925 * is no larger than this maximum.
927 * We apply a greedy approach and discard (keep in global memory)
928 * those groups that would result in a total memory size that
929 * is larger than the maximum.
931 * This function should be called after any function that may
932 * affect the decision on whether to place a reference group
933 * in private, shared or global memory.
935 static void check_shared_memory_bound(struct ppcg_kernel
*kernel
)
938 isl_val
*left
, *size
;
940 if (kernel
->options
->max_shared_memory
< 0)
943 left
= isl_val_int_from_si(kernel
->ctx
,
944 kernel
->options
->max_shared_memory
);
946 for (i
= 0; i
< kernel
->n_array
; ++i
) {
947 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
949 for (j
= 0; j
< local
->n_group
; ++j
) {
950 struct gpu_array_ref_group
*group
;
951 enum ppcg_group_access_type type
;
953 group
= local
->groups
[j
];
954 type
= gpu_array_ref_group_type(group
);
955 if (type
!= ppcg_access_shared
)
958 size
= gpu_array_tile_size(group
->shared_tile
);
959 size
= isl_val_mul_ui(size
, local
->array
->size
);
961 if (isl_val_le(size
, left
)) {
962 left
= isl_val_sub(left
, size
);
968 gpu_array_tile_free(group
->shared_tile
);
975 /* Mark all arrays of "kernel" that have an array reference group
976 * that is not mapped to private or shared memory as
977 * accessing the corresponding global device memory.
979 static void mark_global_arrays(struct ppcg_kernel
*kernel
)
983 for (i
= 0; i
< kernel
->n_array
; ++i
) {
984 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
988 for (j
= 0; j
< local
->n_group
; ++j
) {
989 if (gpu_array_ref_group_tile(local
->groups
[j
]))
993 local
->array
->global
= 1;
999 /* Compute a tiling for all the array reference groups in "kernel".
1001 static void compute_group_tilings(struct ppcg_kernel
*kernel
)
1005 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1006 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1008 for (j
= 0; j
< array
->n_group
; ++j
)
1009 gpu_array_ref_group_compute_tiling(array
->groups
[j
]);
1013 /* Compute the effective grid size as a list of the sizes in each dimension.
1015 * The grid size specified by the user or set by default
1016 * in read_grid_sizes() and applied by the block filter,
1017 * may be too large for the given code in the sense that
1018 * it may contain blocks that don't need to execute anything.
1019 * We therefore don't return this grid size, but instead the
1020 * smallest grid size that ensures that all blocks that actually
1021 * execute code are included in the grid.
1023 * We first extract a description of the grid, i.e., the possible values
1024 * of the block ids, from the domain elements in "domain" and
1025 * kernel->block_filter.
1026 * The block ids are parameters in kernel->block_filter.
1027 * We simply need to change them into set dimensions.
1029 * Then, for each block dimension, we compute the maximal value of the block id
1032 static __isl_give isl_multi_pw_aff
*extract_grid_size(
1033 struct ppcg_kernel
*kernel
, __isl_take isl_union_set
*domain
)
1038 isl_multi_pw_aff
*size
;
1040 domain
= isl_union_set_intersect(domain
,
1041 isl_union_set_copy(kernel
->block_filter
));
1042 grid
= isl_union_set_params(domain
);
1043 grid
= isl_set_from_params(grid
);
1044 grid
= isl_set_add_dims(grid
, isl_dim_set
, kernel
->n_grid
);
1045 for (i
= 0; i
< kernel
->n_grid
; ++i
) {
1049 id
= isl_id_list_get_id(kernel
->block_ids
, i
);
1050 pos
= isl_set_find_dim_by_id(grid
, isl_dim_param
, id
);
1053 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
1054 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
1057 grid
= isl_set_coalesce(grid
);
1058 size
= ppcg_size_from_extent(grid
);
1059 context
= isl_set_params(isl_set_copy(kernel
->context
));
1060 return isl_multi_pw_aff_gist(size
, context
);
1063 /* Compute the size of a fixed bounding box around the origin and "set",
1064 * where "set" is assumed to contain only non-negative elements,
1065 * and store the results in "size".
1066 * In particular, compute the maximal value of "set" in each direction
1069 static void extract_fixed_size(__isl_take isl_set
*set
, int *size
)
1072 isl_local_space
*ls
;
1075 n
= isl_set_dim(set
, isl_dim_set
);
1076 ls
= isl_local_space_from_space(isl_set_get_space(set
));
1077 obj
= isl_aff_zero_on_domain(ls
);
1078 for (i
= 0; i
< n
; ++i
) {
1081 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 1);
1082 max
= isl_set_max_val(set
, obj
);
1083 size
[i
] = isl_val_get_num_si(max
) + 1;
1085 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 0);
1091 /* Compute the effective block size as a list of the sizes in each dimension
1092 * and store the sizes in kernel->block_dim.
1094 * The block size specified by the user or set by default
1095 * in read_block_sizes() and applied by the thread filter,
1096 * may be too large for the given code in the sense that
1097 * it may contain threads that don't need to execute anything.
1098 * We therefore update this block size in kernel->block_dim
1099 * to the smallest block size that ensures that all threads
1100 * that actually execute code are included in the block.
1102 * The set of possible values of the thread ids is obtained from
1103 * the domain elements "domain" and kernel->thread_filter.
1104 * The current implementation eliminates all parameters, ensuring
1105 * that the size is a fixed constant in each dimension.
1106 * In principle we could also compute parametric sizes.
1107 * We would have to make sure to project out all b%d and t%d parameters,
1110 static isl_stat
extract_block_size(struct ppcg_kernel
*kernel
,
1111 __isl_take isl_union_set
*domain
)
1117 domain
= isl_union_set_intersect(domain
,
1118 isl_union_set_copy(kernel
->thread_filter
));
1119 block
= isl_union_set_params(domain
);
1120 block
= isl_set_from_params(block
);
1121 block
= isl_set_add_dims(block
, isl_dim_set
, kernel
->n_block
);
1122 for (i
= 0; i
< kernel
->n_block
; ++i
) {
1127 return isl_stat_error
;
1129 id
= isl_id_list_get_id(kernel
->thread_ids
, i
);
1130 pos
= isl_set_find_dim_by_id(block
, isl_dim_param
, id
);
1133 isl_die(isl_set_get_ctx(block
), isl_error_internal
,
1134 "missing constraints on thread identifier",
1135 block
= isl_set_free(block
));
1136 block
= isl_set_equate(block
, isl_dim_param
, pos
,
1139 nparam
= isl_set_dim(block
, isl_dim_param
);
1140 block
= isl_set_project_out(block
, isl_dim_param
, 0, nparam
);
1143 return isl_stat_error
;
1145 extract_fixed_size(block
, kernel
->block_dim
);
1150 struct ppcg_kernel
*ppcg_kernel_free(struct ppcg_kernel
*kernel
)
1157 isl_id_list_free(kernel
->block_ids
);
1158 isl_id_list_free(kernel
->thread_ids
);
1159 isl_multi_pw_aff_free(kernel
->grid_size
);
1160 isl_ast_expr_free(kernel
->grid_size_expr
);
1161 isl_set_free(kernel
->context
);
1162 isl_union_set_free(kernel
->core
);
1163 isl_union_set_free(kernel
->arrays
);
1164 isl_union_pw_multi_aff_free(kernel
->contraction
);
1165 isl_union_set_free(kernel
->expanded_domain
);
1166 isl_space_free(kernel
->space
);
1167 isl_ast_node_free(kernel
->tree
);
1168 isl_union_set_free(kernel
->block_filter
);
1169 isl_union_set_free(kernel
->thread_filter
);
1170 isl_union_pw_multi_aff_free(kernel
->copy_schedule
);
1171 isl_union_set_free(kernel
->sync_writes
);
1173 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1174 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1176 for (j
= 0; j
< array
->n_group
; ++j
)
1177 gpu_array_ref_group_free(array
->groups
[j
]);
1178 free(array
->groups
);
1180 isl_multi_pw_aff_free(array
->bound
);
1181 isl_ast_expr_free(array
->bound_expr
);
1183 free(kernel
->array
);
1185 for (i
= 0; i
< kernel
->n_var
; ++i
) {
1186 free(kernel
->var
[i
].name
);
1187 isl_vec_free(kernel
->var
[i
].size
);
1196 /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
1198 static void ppcg_kernel_free_wrap(void *user
)
1200 struct ppcg_kernel
*kernel
= user
;
1202 ppcg_kernel_free(kernel
);
1205 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
1206 struct ppcg_kernel_var
*var
)
1209 struct gpu_array_tile
*tile
;
1212 var
->array
= group
->array
;
1214 var
->type
= gpu_array_ref_group_type(group
);
1215 tile
= gpu_array_ref_group_tile(group
);
1217 p
= isl_printer_to_str(ctx
);
1218 p
= gpu_array_ref_group_print_name(group
, p
);
1219 var
->name
= isl_printer_get_str(p
);
1220 isl_printer_free(p
);
1222 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
1224 for (j
= 0; j
< group
->array
->n_index
; ++j
)
1225 var
->size
= isl_vec_set_element_val(var
->size
, j
,
1226 isl_val_copy(tile
->bound
[j
].size
));
1229 static isl_stat
create_kernel_vars(struct ppcg_kernel
*kernel
)
1234 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1235 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1237 for (j
= 0; j
< array
->n_group
; ++j
) {
1238 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1239 enum ppcg_group_access_type type
;
1241 type
= gpu_array_ref_group_type(group
);
1242 if (type
!= ppcg_access_global
)
1247 kernel
->var
= isl_calloc_array(kernel
->ctx
, struct ppcg_kernel_var
, n
);
1249 return isl_stat_error
;
1253 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1254 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1256 for (j
= 0; j
< array
->n_group
; ++j
) {
1257 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1258 enum ppcg_group_access_type type
;
1260 type
= gpu_array_ref_group_type(group
);
1261 if (type
== ppcg_access_global
)
1263 create_kernel_var(kernel
->ctx
, group
, &kernel
->var
[n
]);
1271 /* Replace "pa" by the zero function defined over the universe domain
1272 * in the space of "pa".
1274 static __isl_give isl_pw_aff
*set_universally_zero(__isl_take isl_pw_aff
*pa
)
1279 space
= isl_space_domain(isl_pw_aff_get_space(pa
));
1280 isl_pw_aff_free(pa
);
1281 zero
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1283 return isl_pw_aff_from_aff(zero
);
1286 /* The sizes of the arrays on the host that have been computed by
1287 * extract_array_info may depend on the parameters. Use the extra
1288 * constraints on the parameters that are valid at "host_domain"
1289 * to simplify these expressions and store the results in kernel->array.
1291 * We only need these localized bounds for arrays that are accessed
1292 * by the current kernel. If we have found at least one reference group
1293 * then the array is accessed by the kernel.
1295 * The resulting sizes may be functions that are nowhere defined
1296 * in case the access function cannot possibly access anything inside
1297 * the kernel for some reason. If so, they are replaced by the zero
1298 * function. Since the access function cannot actually access anything,
1299 * there is no harm in printing the array sizes as zero.
1301 static void localize_bounds(struct ppcg_kernel
*kernel
,
1302 __isl_keep isl_set
*host_domain
)
1307 context
= isl_set_copy(host_domain
);
1308 context
= isl_set_params(context
);
1310 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1311 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1312 isl_multi_pw_aff
*bound
;
1315 if (local
->n_group
== 0)
1318 n_index
= local
->array
->n_index
;
1319 bound
= isl_multi_pw_aff_copy(local
->array
->bound
);
1321 for (j
= 0; j
< n_index
; ++j
) {
1325 pwaff
= isl_multi_pw_aff_get_pw_aff(bound
, j
);
1326 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
1327 empty
= isl_pw_aff_is_empty(pwaff
);
1329 pwaff
= isl_pw_aff_free(pwaff
);
1331 pwaff
= set_universally_zero(pwaff
);
1332 bound
= isl_multi_pw_aff_set_pw_aff(bound
, j
, pwaff
);
1335 local
->n_index
= n_index
;
1336 local
->bound
= bound
;
1338 isl_set_free(context
);
1341 /* Create the array of gpu_local_array_info structures "array"
1342 * inside "kernel". The number of elements in this array is
1343 * the same as the number of arrays in "prog".
1344 * Initialize the "array" field of each local array to point
1345 * to the corresponding array in "prog".
1347 static struct ppcg_kernel
*ppcg_kernel_create_local_arrays(
1348 struct ppcg_kernel
*kernel
, struct gpu_prog
*prog
)
1356 ctx
= isl_set_get_ctx(prog
->context
);
1357 kernel
->array
= isl_calloc_array(ctx
,
1358 struct gpu_local_array_info
, prog
->n_array
);
1360 return ppcg_kernel_free(kernel
);
1361 kernel
->n_array
= prog
->n_array
;
1363 for (i
= 0; i
< prog
->n_array
; ++i
)
1364 kernel
->array
[i
].array
= &prog
->array
[i
];
1369 /* Does "kernel" need to be passed an argument corresponding to array "i"?
1371 * The argument is only needed if the kernel accesses this device memory.
1373 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
)
1375 return kernel
->array
[i
].global
;
1378 /* Find the element in gen->stmt that has the given "id".
1379 * Return NULL if no such gpu_stmt can be found.
1381 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
1385 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
1386 if (id
== prog
->stmts
[i
].id
)
1390 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
1393 void ppcg_kernel_stmt_free(void *user
)
1395 struct ppcg_kernel_stmt
*stmt
= user
;
1400 switch (stmt
->type
) {
1401 case ppcg_kernel_copy
:
1402 isl_ast_expr_free(stmt
->u
.c
.index
);
1403 isl_ast_expr_free(stmt
->u
.c
.local_index
);
1405 case ppcg_kernel_domain
:
1406 isl_id_to_ast_expr_free(stmt
->u
.d
.ref2expr
);
1408 case ppcg_kernel_sync
:
1415 /* Return the gpu_stmt_access in the list "accesses" that corresponds
1418 static struct gpu_stmt_access
*find_access(struct gpu_stmt_access
*accesses
,
1419 __isl_keep isl_id
*ref_id
)
1421 struct gpu_stmt_access
*access
;
1423 for (access
= accesses
; access
; access
= access
->next
)
1424 if (access
->ref_id
== ref_id
)
1430 /* Return the index of the array called "name" in the list of arrays.
1432 static int find_array_index(struct ppcg_kernel
*kernel
, const char *name
)
1436 for (i
= 0; i
< kernel
->n_array
; ++i
)
1437 if (!strcmp(name
, kernel
->array
[i
].array
->name
))
1443 /* Internal data structure for the index and AST expression transformation
1444 * callbacks for pet_stmt_build_ast_exprs.
1446 * "kernel" is the kernel for which are computing AST expressions and
1447 * may be NULL if we are not inside a kernel.
1448 * "accesses" is the list of gpu_stmt_access in the statement.
1449 * "iterator_map" expresses the statement iterators in terms of
1450 * the AST loop iterators.
1451 * "sched2copy" expresses the outer copy_schedule_dim dimensions of
1452 * the kernel schedule in terms of the AST loop iterators and
1453 * may be NULL if we are not inside a kernel.
1455 * The following fields are set in transform_index and used in transform_expr.
1456 * "array" is the array that is being accessed.
1457 * "global" is set if the global array is accessed (rather than
1458 * shared/private memory).
1459 * "local_array" refers to information on the array specialized
1460 * to the current kernel.
1462 struct ppcg_transform_data
{
1463 struct ppcg_kernel
*kernel
;
1464 struct gpu_stmt_access
*accesses
;
1465 isl_pw_multi_aff
*iterator_map
;
1466 isl_pw_multi_aff
*sched2copy
;
1468 struct gpu_array_info
*array
;
1470 struct gpu_local_array_info
*local_array
;
1473 /* Return a pointer to the gpu_array_ref_group in "local"
1474 * that contains the reference "access".
1475 * Return NULL if no such group can be found.
1477 static struct gpu_array_ref_group
*find_ref_group(
1478 struct gpu_local_array_info
*local
, struct gpu_stmt_access
*access
)
1482 for (i
= 0; i
< local
->n_group
; ++i
) {
1483 struct gpu_array_ref_group
*group
= local
->groups
[i
];
1485 for (j
= 0; j
< group
->n_ref
; ++j
)
1486 if (group
->refs
[j
] == access
)
1493 /* Given an index expression "index" of the form
1497 * with F(A) either A or some subfield of A and L the AST loop iterators,
1498 * and a tiling "tiling" of the form
1502 * apply the tiling to the outer array in the index expression to obtain
1506 * If F(A) is some subfield of A, then separate the member access
1507 * into the base index expression and the field index expression,
1508 * apply the tiling to the base index expression and combine the result
1509 * with the field index expression.
1511 * If F(A) is A, then modify index to keep track of the iterators
1515 * and combine the result with the tiling to obtain a tiled index expression
1516 * in terms of the AST loop iterators
1520 static __isl_give isl_multi_pw_aff
*tile_outer(
1521 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_multi_pw_aff
*tiling
)
1523 isl_bool is_wrapping
;
1525 isl_multi_pw_aff
*mpa
;
1527 is_wrapping
= isl_multi_pw_aff_range_is_wrapping(index
);
1528 if (is_wrapping
< 0)
1531 isl_multi_pw_aff
*field
;
1533 field
= isl_multi_pw_aff_copy(index
);
1534 field
= isl_multi_pw_aff_range_factor_range(field
);
1535 index
= isl_multi_pw_aff_range_factor_domain(index
);
1536 index
= tile_outer(index
, tiling
);
1537 return isl_multi_pw_aff_range_product(index
, field
);
1540 space
= isl_space_domain(isl_multi_pw_aff_get_space(index
));
1541 space
= isl_space_map_from_set(space
);
1542 mpa
= isl_multi_pw_aff_identity(space
);
1543 index
= isl_multi_pw_aff_range_product(mpa
, index
);
1544 index
= isl_multi_pw_aff_pullback_multi_pw_aff(tiling
, index
);
1548 isl_multi_pw_aff_free(index
);
1549 isl_multi_pw_aff_free(tiling
);
1553 /* Index transformation callback for pet_stmt_build_ast_exprs.
1555 * "index" expresses the array indices in terms of statement iterators
1557 * We first reformulate "index" in terms of the AST loop iterators.
1558 * Then we check if we are accessing the global array or
1559 * a shared/private copy. In particular, if we are not inside a kernel
1560 * then we must be accessing a global array.
1561 * In the former case, we simply return
1562 * the updated index. If "index" is an affine expression rather
1563 * than an array access, then we also return the updated index here.
1565 * If no reference groups have been computed for the array,
1566 * then we can only be accessing the global array.
1568 * Otherwise, we apply the tiling to the index.
1569 * This tiling is of the form
1573 * where D corresponds to the outer tile->depth dimensions of
1574 * the kernel schedule.
1575 * The index is of the form
1579 * We update the tiling to refer to the AST loop iterators
1583 * and combine it with the index to obtain a tiled index expression in terms
1584 * of the AST loop iterators
1588 * Note that while the tiling applies directly to an outer array.
1589 * the index may refer to some subfield of this outer array.
1590 * In such cases, the result will refer to the same subfield of the tile.
1591 * That is, an index expression of the form L -> F(A) will be transformed
1592 * into an index expression of the form L -> F(T).
1594 static __isl_give isl_multi_pw_aff
*transform_index(
1595 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*ref_id
,
1598 struct ppcg_transform_data
*data
= user
;
1599 struct gpu_stmt_access
*access
;
1600 struct gpu_array_ref_group
*group
;
1601 struct gpu_array_tile
*tile
;
1602 isl_pw_multi_aff
*iterator_map
;
1607 isl_multi_pw_aff
*tiling
;
1608 isl_pw_multi_aff
*pma
;
1609 isl_pw_multi_aff
*sched2depth
;
1613 iterator_map
= isl_pw_multi_aff_copy(data
->iterator_map
);
1614 index
= isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
1619 access
= find_access(data
->accesses
, ref_id
);
1622 if (!isl_map_has_tuple_name(access
->access
, isl_dim_out
))
1625 name
= get_outer_array_name(access
->access
);
1627 return isl_multi_pw_aff_free(index
);
1628 i
= find_array_index(data
->kernel
, name
);
1630 isl_die(isl_multi_pw_aff_get_ctx(index
), isl_error_internal
,
1631 "cannot find array",
1632 return isl_multi_pw_aff_free(index
));
1633 data
->local_array
= &data
->kernel
->array
[i
];
1634 data
->array
= data
->local_array
->array
;
1636 group
= find_ref_group(data
->local_array
, access
);
1642 tile
= gpu_array_ref_group_tile(group
);
1643 data
->global
= !tile
;
1647 space
= isl_space_domain(isl_multi_aff_get_space(tile
->tiling
));
1648 space
= isl_space_range(isl_space_unwrap(space
));
1649 space
= isl_space_map_from_set(space
);
1650 pma
= isl_pw_multi_aff_identity(space
);
1651 sched2depth
= isl_pw_multi_aff_copy(data
->sched2copy
);
1652 dim
= isl_pw_multi_aff_dim(sched2depth
, isl_dim_out
);
1653 sched2depth
= isl_pw_multi_aff_drop_dims(sched2depth
, isl_dim_out
,
1654 tile
->depth
, dim
- tile
->depth
);
1655 pma
= isl_pw_multi_aff_product(sched2depth
, pma
);
1656 tiling
= isl_multi_pw_aff_from_multi_aff(
1657 isl_multi_aff_copy(tile
->tiling
));
1658 tiling
= isl_multi_pw_aff_pullback_pw_multi_aff(tiling
, pma
);
1660 index
= tile_outer(index
, tiling
);
1665 /* Dereference "expr" by adding an index [0].
1666 * The original "expr" is assumed not to have any indices.
1668 * If "expr" is a member access, then the dereferencing needs
1669 * to be applied to the structure argument of this member access.
1671 static __isl_give isl_ast_expr
*dereference(__isl_take isl_ast_expr
*expr
)
1674 isl_ast_expr
*arg0
, *res
;
1675 isl_ast_expr_list
*list
;
1677 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1679 return isl_ast_expr_free(expr
);
1680 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1681 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1684 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1685 arg
= dereference(arg
);
1686 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1687 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1691 isl_ast_expr_free(arg0
);
1693 ctx
= isl_ast_expr_get_ctx(expr
);
1694 res
= isl_ast_expr_from_val(isl_val_zero(ctx
));
1695 list
= isl_ast_expr_list_from_ast_expr(res
);
1696 res
= isl_ast_expr_get_op_arg(expr
, 0);
1697 res
= isl_ast_expr_access(res
, list
);
1698 isl_ast_expr_free(expr
);
1703 /* Linearize the index expression "expr" based on the array bounds
1706 * That is, transform expression
1708 * A[i_0][i_1]...[i_n]
1712 * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
1714 * where b_0, b_1, ..., b_n are the bounds on the array.
1716 * If the base of "expr" is a member access, then the linearization needs
1717 * to be applied to the structure argument of this member access.
1719 * In the base case, if "expr" has no arguments (other than the name of
1720 * the array), then we are passing an entire array to a function.
1721 * In this case, there is nothing to linearize.
1722 * Note that at this point an expression with no arguments can
1723 * only be an entire array because the scalar case and
1724 * the case of single struct are handled by the caller.
1726 * If the number of specified index expressions in "expr"
1727 * is smaller than the dimension of the accessed array,
1728 * then the missing i_j also do not appear in the linearized expression.
1729 * Furthermore, since such an expression does not refer to a single
1730 * element while the default linearized expression would refer to
1731 * a single element, we return the expression
1733 * A + (..((i_0 * b_1 + i_1) ... ) * b_l + i_l)
1735 * instead. Note that because of the special case handling above,
1736 * we can assume here that there is at least one index expression.
1738 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
1739 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
)
1744 isl_ast_expr_list
*list
;
1746 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1747 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1748 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1751 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1752 arg
= gpu_local_array_info_linearize_index(array
, arg
);
1753 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1754 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1758 isl_ast_expr_free(arg0
);
1760 if (isl_ast_expr_get_op_n_arg(expr
) == 1)
1763 n
= isl_ast_expr_get_op_n_arg(expr
);
1764 res
= isl_ast_expr_get_op_arg(expr
, 1);
1765 for (i
= 1; i
< array
->n_index
; ++i
) {
1766 isl_ast_expr
*expr_i
;
1768 expr_i
= isl_ast_expr_get_op_arg(array
->bound_expr
, 1 + i
);
1769 res
= isl_ast_expr_mul(res
, expr_i
);
1773 expr_i
= isl_ast_expr_get_op_arg(expr
, i
+ 1);
1774 res
= isl_ast_expr_add(res
, expr_i
);
1777 if (1 + array
->n_index
> n
) {
1778 res
= isl_ast_expr_add(isl_ast_expr_get_op_arg(expr
, 0), res
);
1780 list
= isl_ast_expr_list_from_ast_expr(res
);
1781 res
= isl_ast_expr_get_op_arg(expr
, 0);
1782 res
= isl_ast_expr_access(res
, list
);
1785 isl_ast_expr_free(expr
);
1790 /* AST expression transformation callback for pet_stmt_build_ast_exprs.
1792 * If the AST expression refers to an array that is not accessed
1793 * at all, then this means the value of the expression is not used,
1794 * so we might as well print zero (NULL pointer) instead.
1796 * If the AST expression refers to a global scalar that is not
1797 * a read-only scalar, then its address was passed to the kernel and
1798 * we need to dereference it.
1800 * If the AST expression refers to an access to a global array,
1801 * then we linearize the access exploiting the bounds in data->local_array.
1803 static __isl_give isl_ast_expr
*transform_expr(__isl_take isl_ast_expr
*expr
,
1804 __isl_keep isl_id
*id
, void *user
)
1806 struct ppcg_transform_data
*data
= user
;
1810 if (!data
->array
->accessed
) {
1813 ctx
= isl_ast_expr_get_ctx(expr
);
1814 isl_ast_expr_free(expr
);
1815 return isl_ast_expr_from_val(isl_val_zero(ctx
));
1817 if (gpu_array_is_read_only_scalar(data
->array
))
1821 if (data
->array
->n_index
== 0)
1822 return dereference(expr
);
1823 if (!data
->array
->linearize
)
1826 return gpu_local_array_info_linearize_index(data
->local_array
, expr
);
1829 /* This function is called for each instance of a user statement
1830 * in the kernel "kernel", identified by "gpu_stmt".
1831 * "kernel" may be NULL if we are not inside a kernel.
1833 * We attach a struct ppcg_kernel_stmt to the "node", containing
1834 * a computed AST expression for each access, through an annotation
1836 * These AST expressions are computed from iterator_map,
1837 * which expresses the domain
1838 * elements in terms of the generated loops, and sched2copy,
1839 * which expresses the outer copy_schedule_dim dimensions of
1840 * the kernel schedule computed by PPCG in terms of the generated loops.
1842 static __isl_give isl_ast_node
*create_domain_leaf(
1843 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1844 __isl_keep isl_ast_build
*build
, struct gpu_stmt
*gpu_stmt
)
1846 struct ppcg_transform_data data
;
1847 struct ppcg_kernel_stmt
*stmt
;
1850 isl_pw_multi_aff
*sched2copy
;
1852 isl_pw_multi_aff
*iterator_map
;
1853 isl_union_map
*schedule
;
1857 ctx
= isl_ast_node_get_ctx(node
);
1859 stmt
= isl_calloc_type(ctx
, struct ppcg_kernel_stmt
);
1861 return isl_ast_node_free(node
);
1863 schedule
= isl_ast_build_get_schedule(build
);
1864 map
= isl_map_reverse(isl_map_from_union_map(schedule
));
1865 iterator_map
= isl_pw_multi_aff_from_map(map
);
1867 sched2copy
= compute_sched_to_copy(kernel
,
1868 isl_pw_multi_aff_copy(iterator_map
));
1872 stmt
->type
= ppcg_kernel_domain
;
1873 stmt
->u
.d
.stmt
= gpu_stmt
;
1875 data
.kernel
= kernel
;
1876 data
.accesses
= stmt
->u
.d
.stmt
->accesses
;
1877 data
.iterator_map
= iterator_map
;
1878 data
.sched2copy
= sched2copy
;
1879 stmt
->u
.d
.ref2expr
= pet_stmt_build_ast_exprs(stmt
->u
.d
.stmt
->stmt
,
1880 build
, &transform_index
, &data
,
1881 &transform_expr
, &data
);
1883 isl_pw_multi_aff_free(iterator_map
);
1884 isl_pw_multi_aff_free(sched2copy
);
1886 id
= isl_id_alloc(ctx
, "user", stmt
);
1887 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1889 ppcg_kernel_stmt_free(stmt
);
1890 return isl_ast_node_set_annotation(node
, id
);
1893 /* This function is called for each statement node in the AST
1894 * for copying to or from shared/private memory.
1895 * Attach a pointer to a ppcg_kernel_stmt representing the copy
1896 * statement to the node.
1897 * The statement name is "read" or "write", depending on whether we are
1898 * reading from global memory or writing to global memory.
1900 * The schedule is of the form
1904 * where D corresponds to the outer tile->depth dimensions of
1905 * the kernel schedule, A to the global array and L to the outer
1906 * generated AST schedule.
1907 * We compute the inverse and strip off the type, resulting in
1911 * We combine this mapping with on the one hand the projection
1915 * and on the other hand the group tiling
1923 * and store the corresponding expressions in stmt->index and stmt->local_index,
1924 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
1925 * stmt->index is linearized if the global memory array is linearized.
1927 static __isl_give isl_ast_node
*create_access_leaf(struct ppcg_kernel
*kernel
,
1928 struct gpu_array_ref_group
*group
, __isl_take isl_ast_node
*node
,
1929 __isl_keep isl_ast_build
*build
)
1931 struct ppcg_kernel_stmt
*stmt
;
1932 struct gpu_array_tile
*tile
;
1937 isl_pw_multi_aff
*pma
, *pma2
;
1940 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1942 return isl_ast_node_free(node
);
1944 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
1945 type
= isl_map_get_tuple_name(access
, isl_dim_in
);
1946 stmt
->u
.c
.read
= type
&& !strcmp(type
, "read");
1947 access
= isl_map_reverse(access
);
1948 pma
= isl_pw_multi_aff_from_map(access
);
1949 pma
= isl_pw_multi_aff_reset_tuple_id(pma
, isl_dim_out
);
1951 space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
1952 space
= isl_space_unwrap(space
);
1953 pma2
= isl_pw_multi_aff_range_map(space
);
1954 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
,
1955 isl_pw_multi_aff_copy(pma
));
1956 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1957 if (group
->array
->linearize
)
1958 expr
= gpu_local_array_info_linearize_index(group
->local_array
,
1960 stmt
->u
.c
.index
= expr
;
1962 tile
= gpu_array_ref_group_tile(group
);
1963 pma2
= isl_pw_multi_aff_from_multi_aff(
1964 isl_multi_aff_copy(tile
->tiling
));
1965 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
, pma
);
1966 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1967 stmt
->u
.c
.local_index
= expr
;
1969 stmt
->u
.c
.array
= group
->array
;
1970 stmt
->u
.c
.local_array
= group
->local_array
;
1971 stmt
->type
= ppcg_kernel_copy
;
1973 id
= isl_id_alloc(kernel
->ctx
, "copy", stmt
);
1974 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1976 ppcg_kernel_stmt_free(stmt
);
1977 return isl_ast_node_set_annotation(node
, id
);
1980 /* Create a synchronization ppcg_kernel_stmt and
1981 * attach it to the node "node" representing the synchronization.
1983 static __isl_give isl_ast_node
*create_sync_leaf(
1984 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1985 __isl_keep isl_ast_build
*build
)
1987 struct ppcg_kernel_stmt
*stmt
;
1990 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1992 return isl_ast_node_free(node
);
1994 stmt
->type
= ppcg_kernel_sync
;
1995 id
= isl_id_alloc(kernel
->ctx
, "sync", stmt
);
1996 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1998 ppcg_kernel_stmt_free(stmt
);
1999 return isl_ast_node_set_annotation(node
, id
);
2002 /* Build AST expressions for the device array sizes of all arrays in "prog"
2003 * that require allocation on the device using "build", as well as
2004 * for the original array sizes of all arrays that need to be declared
2006 * "node" is freed in case of error.
2008 static __isl_give isl_ast_node
*build_array_bounds(
2009 __isl_take isl_ast_node
*node
, struct gpu_prog
*prog
,
2010 __isl_keep isl_ast_build
*build
)
2014 for (i
= 0; i
< prog
->n_array
; ++i
) {
2015 struct gpu_array_info
*array
= &prog
->array
[i
];
2016 isl_multi_pw_aff
*size
;
2019 if (!gpu_array_requires_device_allocation(array
))
2022 size
= isl_multi_pw_aff_copy(array
->bound
);
2023 expr
= ppcg_build_size_expr(size
, build
);
2024 array
->bound_expr
= expr
;
2026 return isl_ast_node_free(node
);
2029 for (i
= 0; i
< prog
->n_array
; ++i
) {
2030 struct gpu_array_info
*array
= &prog
->array
[i
];
2032 isl_multi_pw_aff
*size
;
2035 if (!array
->declare_local
)
2037 extent
= isl_set_copy(array
->declared_extent
);
2038 size
= ppcg_size_from_extent(extent
);
2039 expr
= ppcg_build_size_expr(size
, build
);
2040 array
->declared_size
= expr
;
2042 return isl_ast_node_free(node
);
2048 /* Internal data structure for at_domain.
2050 * "prog" represents the entire scop.
2051 * "kernel" points to the kernel to which the current schedule node
2052 * belongs. It is set by before_mark and reset by after_mark.
2053 * It may be NULL if we are outside any kernel.
2055 struct ppcg_at_domain_data
{
2056 struct gpu_prog
*prog
;
2057 struct ppcg_kernel
*kernel
;
2060 /* This function is called for each instance of a user statement
2061 * in the kernel. This may be one of the original user statements
2062 * or a statement introduced by PPCG.
2064 * We first check if the statement id corresponds to a gpu statement,
2065 * which indicates the statement is an original user statement. Any statement
2066 * that is not an original user statement has been introduced by PPCG and
2067 * requires special handling.
2069 * If the user statement is one of the original user statements, then we call
2070 * create_domain_leaf. If it is "init_device", then we call
2071 * build_array_bounds. Otherwise, we check if it is a copy or synchronization
2072 * statement and call the appropriate functions. Statements that copy an array
2073 * to/from the device do not need any further treatment.
2074 * Neither does "clear_device".
2076 static __isl_give isl_ast_node
*at_domain(__isl_take isl_ast_node
*node
,
2077 __isl_keep isl_ast_build
*build
, void *user
)
2079 struct ppcg_at_domain_data
*data
= user
;
2080 struct gpu_stmt
*gpu_stmt
;
2081 isl_ast_expr
*expr
, *arg
;
2087 expr
= isl_ast_node_user_get_expr(node
);
2088 arg
= isl_ast_expr_get_op_arg(expr
, 0);
2089 id
= isl_ast_expr_get_id(arg
);
2090 name
= isl_id_get_name(id
);
2091 p
= isl_id_get_user(id
);
2092 isl_ast_expr_free(expr
);
2093 isl_ast_expr_free(arg
);
2095 gpu_stmt
= find_stmt(data
->prog
, id
);
2096 is_sync
= gpu_tree_id_is_sync(id
, data
->kernel
);
2100 return create_domain_leaf(data
->kernel
, node
, build
, gpu_stmt
);
2102 if (!prefixcmp(name
, "to_device_") || !prefixcmp(name
, "from_device_"))
2104 if (!strcmp(name
, "init_device"))
2105 return build_array_bounds(node
, data
->prog
, build
);
2106 if (!strcmp(name
, "clear_device"))
2109 return isl_ast_node_free(node
);
2110 if (!strcmp(name
, "read") || !strcmp(name
, "write")) {
2111 struct gpu_array_ref_group
*group
= p
;
2112 return create_access_leaf(data
->kernel
, group
, node
, build
);
2115 isl_die(data
->prog
->ctx
, isl_error_internal
,
2116 "unknown statement type",
2117 return isl_ast_node_free(node
));
2118 return create_sync_leaf(data
->kernel
, node
, build
);
2121 /* Given a set of wrapped references "ref", return the corresponding
2122 * access relations based on the tagged access relations "tagged".
2124 * The elements of "ref" are of the form
2128 * with D an iteration domains and R a reference.
2129 * The elements of "tagged" are of the form
2135 * Extend "tagged" to include the iteration domain in the range, i.e.,
2137 * [D -> R] -> [D -> A]
2139 * apply the result to "ref" and then unwrap the resulting set
2140 * to obtain relations of the form
2144 static __isl_give isl_union_map
*wrapped_reference_to_access(
2145 __isl_take isl_union_set
*ref
, __isl_take isl_union_map
*tagged
)
2147 isl_union_map
*tag2access
;
2149 tag2access
= isl_union_map_copy(tagged
);
2150 tag2access
= isl_union_map_universe(tag2access
);
2151 tag2access
= isl_union_set_unwrap(isl_union_map_domain(tag2access
));
2152 tag2access
= isl_union_map_domain_map(tag2access
);
2153 tag2access
= isl_union_map_range_product(tag2access
, tagged
);
2155 ref
= isl_union_set_coalesce(ref
);
2156 ref
= isl_union_set_apply(ref
, tag2access
);
2158 return isl_union_set_unwrap(ref
);
2161 /* Given an access relation "access" from one or more array reference groups,
2162 * remove those reads if ("read" is 1) or writes (if "read" is 0)
2163 * that are only needed to communicate data within
2164 * the same iteration of "sched".
2165 * The domain of "sched" corresponds to the original statement instances,
2166 * i.e., those that appear in the domains of the access relations.
2167 * "tagged" contains all tagged access relations to all
2168 * the array reference groups accessed by "access" from statement
2169 * instances scheduled by "sched".
2171 * If the access is a read then it is either an element of
2173 * live_in union (range flow)
2175 * where live_in and flow may be overapproximations, or
2176 * it reads an uninitialized value (that is not live-in because
2177 * there is an intermediate kill) or it reads a value that was
2178 * written within the same (compound) statement instance.
2179 * If the access is a write then it is either an element of
2181 * live_out union (domain flow)
2183 * or it writes a value that is never read (and is not live-out
2184 * because of an intermediate kill) or only
2185 * within the same (compound) statement instance.
2186 * In both cases, the access relation is also a subset of
2187 * the group access relation.
2189 * The cases where an uninitialized value is read or a value is written
2190 * that is never read or where the dataflow occurs within a statement
2191 * instance are also considered local and may also be removed.
2193 * Essentially, we compute the intersection of "access" with either
2195 * live_in union (range non-local-flow)
2199 * live_out union (domain non-local-flow)
2201 * We first construct a relation "local"
2203 * [[D -> R] -> [D' -> R']]
2205 * of pairs of domain iterations accessing the reference group
2206 * and references in the group that are coscheduled by "sched".
2208 * If this relation does not intersect the dataflow dependences,
2209 * then there is nothing we can possibly remove, unless the dataflow
2210 * dependences themselves only relate a subset of the accesses.
2211 * In particular, the accesses may not be involved in any dataflow
2212 * dependences, either because they are uninitialized reads/dead writes
2213 * or because the dataflow occurs inside a statement instance.
2215 * Since the computation below may break up the access relation
2216 * into smaller pieces, we only perform the intersection with
2217 * the non-local dependent accesses if the local pairs
2218 * intersect the dataflow dependences. Otherwise, we intersect
2219 * with the universe of the non-local dependent accesses.
2220 * This should at least remove accesses from statements that
2221 * do not participate in any dependences.
2223 * In particular, we remove the "local" dataflow dependences from
2224 * the set of all dataflow dependences, or at least those
2225 * that may contribute to a domain/range that intersects
2226 * the domain of "access".
2227 * Note that if the potential dataflow dependences are an overapproximation
2228 * of the actual dataflow dependences, then the result remains an
2229 * overapproximation of the non-local dataflow dependences.
2230 * Copying to/from global memory is only needed for the references
2231 * in the domain/range of the result or for accesses that are live out/in
2232 * for the entire scop.
2234 * We therefore map the domain/range of the "external" relation
2235 * to the corresponding access relation and take the union with
2236 * the live out/in relation.
2238 static __isl_give isl_union_map
*remove_local_accesses(
2239 struct gpu_prog
*prog
, __isl_take isl_union_map
*tagged
,
2240 __isl_take isl_union_map
*access
, __isl_take isl_union_map
*sched
,
2244 isl_union_pw_multi_aff
*tagger
;
2245 isl_union_set
*domain
, *access_domain
;
2246 isl_union_map
*local
, *external
, *universe
;
2247 isl_union_set
*tag_set
;
2249 if (isl_union_map_is_empty(access
)) {
2250 isl_union_map_free(sched
);
2251 isl_union_map_free(tagged
);
2255 tagger
= isl_union_pw_multi_aff_copy(prog
->scop
->tagger
);
2256 domain
= isl_union_map_domain(isl_union_map_copy(tagged
));
2257 tagger
= isl_union_pw_multi_aff_intersect_domain(tagger
,
2258 isl_union_set_copy(domain
));
2259 sched
= isl_union_map_preimage_domain_union_pw_multi_aff(sched
, tagger
);
2261 local
= isl_union_map_apply_range(sched
,
2262 isl_union_map_reverse(isl_union_map_copy(sched
)));
2263 local
= isl_union_map_intersect(local
,
2264 isl_union_map_copy(prog
->scop
->tagged_dep_flow
));
2266 empty
= isl_union_map_is_empty(local
);
2268 external
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
2269 universe
= isl_union_map_universe(isl_union_map_copy(access
));
2270 access_domain
= isl_union_map_domain(universe
);
2271 domain
= isl_union_set_universe(domain
);
2272 universe
= isl_union_set_unwrap(domain
);
2273 universe
= isl_union_map_intersect_domain(universe
, access_domain
);
2274 domain
= isl_union_map_wrap(universe
);
2276 external
= isl_union_map_intersect_range(external
, domain
);
2278 external
= isl_union_map_intersect_domain(external
, domain
);
2279 external
= isl_union_map_intersect_params(external
,
2280 isl_set_copy(prog
->scop
->context
));
2281 external
= isl_union_map_subtract(external
, local
);
2284 tag_set
= isl_union_map_range(external
);
2285 external
= wrapped_reference_to_access(tag_set
, tagged
);
2286 external
= isl_union_map_union(external
,
2287 isl_union_map_copy(prog
->scop
->live_in
));
2289 tag_set
= isl_union_map_domain(external
);
2290 external
= wrapped_reference_to_access(tag_set
, tagged
);
2291 external
= isl_union_map_union(external
,
2292 isl_union_map_copy(prog
->scop
->live_out
));
2296 external
= isl_union_map_free(external
);
2298 external
= isl_union_map_universe(external
);
2300 access
= isl_union_map_intersect(access
, external
);
2305 /* Given an access relation "access" from "group", remove those reads
2306 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2307 * communicate data within the same iteration of the schedule "prefix"
2308 * at the position where the copying of the group is inserted.
2309 * That is, the output dimension of "prefix"
2310 * is equal to tile->depth.
2311 * The domain of "prefix" corresponds to the original statement instances,
2312 * i.e., those that appear in the domains of the access relations.
2314 * Extract the tagged access relation of "group" and
2315 * then call remove_local_accesses.
2317 static __isl_give isl_union_map
*remove_local_accesses_group(
2318 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
2319 __isl_take isl_union_map
*access
, __isl_keep isl_union_map
*prefix
,
2322 isl_union_map
*sched
, *tagged
;
2324 if (isl_union_map_is_empty(access
))
2327 tagged
= group_tagged_access_relation(group
);
2328 sched
= isl_union_map_copy(prefix
);
2330 return remove_local_accesses(kernel
->prog
, tagged
, access
, sched
, read
);
2333 /* Build an access AST expression for the effective grid size using "build".
2334 * Store the result in kernel->grid_size_expr.
2336 static isl_stat
build_grid_size(struct ppcg_kernel
*kernel
,
2337 __isl_keep isl_ast_build
*build
)
2339 isl_multi_pw_aff
*size
;
2341 size
= isl_multi_pw_aff_copy(kernel
->grid_size
);
2342 size
= isl_multi_pw_aff_set_tuple_name(size
, isl_dim_out
, "grid");
2343 kernel
->grid_size_expr
= ppcg_build_size_expr(size
, build
);
2345 if (!kernel
->grid_size_expr
)
2346 return isl_stat_error
;
2350 /* Build access AST expressions for the localized array sizes using "build".
2351 * Store the result in local->bound_expr.
2352 * Only do this for arrays for which localized bounds have been computed.
2354 static isl_stat
build_local_array_sizes(struct ppcg_kernel
*kernel
,
2355 __isl_keep isl_ast_build
*build
)
2359 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2360 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
2361 isl_multi_pw_aff
*size
;
2363 if (local
->n_group
== 0)
2365 size
= isl_multi_pw_aff_copy(local
->bound
);
2366 local
->bound_expr
= ppcg_build_size_expr(size
, build
);
2367 if (!local
->bound_expr
)
2368 return isl_stat_error
;
2374 /* Build access AST expressions for the effective grid size and
2375 * the localized array sizes using "build".
2377 static isl_stat
build_grid_and_local_array_sizes(struct ppcg_kernel
*kernel
,
2378 __isl_keep isl_ast_build
*build
)
2380 if (build_grid_size(kernel
, build
) < 0)
2381 return isl_stat_error
;
2382 if (build_local_array_sizes(kernel
, build
) < 0)
2383 return isl_stat_error
;
2387 /* This function is called before the AST generator starts traversing
2388 * the schedule subtree of a node with mark "mark".
2390 * If the mark is called "kernel", store the kernel pointer in data->kernel
2391 * for use in at_domain and build AST expressions for the grid size and
2392 * the localized array sizes.
2394 static isl_stat
before_mark(__isl_keep isl_id
*mark
,
2395 __isl_keep isl_ast_build
*build
, void *user
)
2397 struct ppcg_at_domain_data
*data
= user
;
2400 return isl_stat_error
;
2401 if (!strcmp(isl_id_get_name(mark
), "kernel")) {
2402 data
->kernel
= isl_id_get_user(mark
);
2403 if (build_grid_and_local_array_sizes(data
->kernel
, build
) < 0)
2404 return isl_stat_error
;
2409 /* This function is called after the AST generator has finished traversing
2410 * the schedule subtree of a mark node. "node" points to the corresponding
2413 * If the mark is called "kernel", then replace "node" by a user node
2414 * that "calls" the kernel, representing the launch of the kernel.
2415 * The original "node" is stored inside the kernel object so that
2416 * it can be used to print the device code.
2417 * Note that this assumes that a kernel is only launched once.
2418 * Also clear data->kernel.
2420 static __isl_give isl_ast_node
*after_mark(__isl_take isl_ast_node
*node
,
2421 __isl_keep isl_ast_build
*build
, void *user
)
2426 isl_ast_expr_list
*list
;
2427 struct ppcg_kernel
*kernel
;
2428 struct ppcg_at_domain_data
*data
= user
;
2430 ctx
= isl_ast_node_get_ctx(node
);
2431 id
= isl_ast_node_mark_get_id(node
);
2433 return isl_ast_node_free(node
);
2434 if (strcmp(isl_id_get_name(id
), "kernel") || !data
->kernel
) {
2438 kernel
= data
->kernel
;
2439 data
->kernel
= NULL
;
2440 kernel
->space
= isl_ast_build_get_schedule_space(build
);
2441 kernel
->tree
= isl_ast_node_mark_get_node(node
);
2442 isl_ast_node_free(node
);
2444 expr
= isl_ast_expr_from_id(isl_id_copy(id
));
2445 list
= isl_ast_expr_list_alloc(ctx
, 0);
2446 expr
= isl_ast_expr_call(expr
, list
);
2447 node
= isl_ast_node_alloc_user(expr
);
2448 node
= isl_ast_node_set_annotation(node
, id
);
2453 static isl_bool
update_depth(__isl_keep isl_schedule_node
*node
, void *user
)
2458 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2459 return isl_bool_true
;
2460 node_depth
= isl_schedule_node_get_schedule_depth(node
);
2461 if (node_depth
> *depth
)
2462 *depth
= node_depth
;
2464 return isl_bool_false
;
2467 /* Use isl to generate code for both the host and the device
2469 * The device code is marked by "kernel" mark nodes in the schedule tree,
2470 * containing a pointer to a ppcg_kernel object.
2471 * The returned AST only contains the AST for the host code.
2472 * The ASTs for the device code are embedded in ppcg_kernel objects
2473 * attached to the leaf nodes that call "kernel".
2475 static __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
,
2476 __isl_take isl_schedule
*schedule
)
2478 struct ppcg_at_domain_data data
;
2479 isl_ast_build
*build
;
2481 isl_id_list
*iterators
;
2484 data
.prog
= gen
->prog
;
2488 if (isl_schedule_foreach_schedule_node_top_down(schedule
, &update_depth
,
2490 schedule
= isl_schedule_free(schedule
);
2491 build
= isl_ast_build_alloc(gen
->prog
->ctx
);
2492 iterators
= ppcg_scop_generate_names(gen
->prog
->scop
, depth
, "c");
2493 build
= isl_ast_build_set_iterators(build
, iterators
);
2494 build
= isl_ast_build_set_at_each_domain(build
, &at_domain
, &data
);
2495 build
= isl_ast_build_set_before_each_mark(build
, &before_mark
, &data
);
2496 build
= isl_ast_build_set_after_each_mark(build
, &after_mark
, &data
);
2497 if (gen
->prog
->scop
->options
->debug
->dump_final_schedule
)
2498 isl_schedule_dump(schedule
);
2499 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2500 isl_ast_build_free(build
);
2505 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
2509 return isl_union_map_read_from_str(ctx
, str
);
2512 /* Can "node" be tiled and then mapped to block and thread identifiers?
2513 * That is, is it permutable with at least one coincident dimension?
2515 static isl_bool
is_permutable(__isl_keep isl_schedule_node
*node
)
2518 return isl_bool_error
;
2520 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
2521 return isl_bool_false
;
2522 if (!isl_schedule_node_band_get_permutable(node
))
2523 return isl_bool_false
;
2524 if (isl_schedule_node_band_n_member(node
) < 1)
2525 return isl_bool_false
;
2526 if (!isl_schedule_node_band_member_get_coincident(node
, 0))
2527 return isl_bool_false
;
2529 return isl_bool_true
;
2532 /* Is "node" not a suitably permutable band?
2534 static isl_bool
not_permutable(__isl_keep isl_schedule_node
*node
, void *user
)
2536 return isl_bool_not(is_permutable(node
));
2539 /* Does the subtree rooted at "node" have any suitably permutable band nodes?
2540 * That is, does it have any nodes that are permutable and that
2541 * have a least one coincident dimension?
2543 static isl_bool
subtree_has_permutable_bands(__isl_keep isl_schedule_node
*node
)
2545 isl_bool all_non_permutable
;
2547 all_non_permutable
= isl_schedule_node_every_descendant(node
,
2548 ¬_permutable
, NULL
);
2549 return isl_bool_not(all_non_permutable
);
2552 /* Does "schedule" contain any permutable band with at least one coincident
2555 static isl_bool
has_any_permutable_node(__isl_keep isl_schedule
*schedule
)
2557 isl_schedule_node
*root
;
2558 isl_bool any_permutable
;
2560 root
= isl_schedule_get_root(schedule
);
2561 any_permutable
= subtree_has_permutable_bands(root
);
2562 isl_schedule_node_free(root
);
2564 return any_permutable
;
2567 /* Is "node" a candidate for mapping to block and thread identifiers?
2568 * In particular, is it permutable with at least one coincident dimension?
2569 * Alternatively, does the subtree rooted at "node" not contain
2570 * any such permutable node? Filter nodes are skipped in this case,
2571 * because a band node will be inserted in front of the returned
2572 * node and this is not possible for filter nodes that are children
2573 * of set or sequence nodes.
2575 static int is_candidate(__isl_keep isl_schedule_node
*node
)
2577 isl_bool permutable
;
2579 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
)
2581 permutable
= is_permutable(node
);
2582 if (permutable
< 0 || permutable
)
2584 if (isl_schedule_node_get_type(node
) == isl_schedule_node_filter
)
2586 permutable
= subtree_has_permutable_bands(node
);
2592 /* Is "node" the outermost node in its branch that can be tiled
2593 * and then mapped to block and thread identifiers?
2594 * If there are no such nodes in the subtree at "node" and
2595 * if "node" is not a filter node, then it is accepted too.
2597 static int is_outer_tilable(__isl_keep isl_schedule_node
*node
)
2600 isl_schedule_node
*ancestor
;
2602 tilable
= is_candidate(node
);
2609 ancestor
= isl_schedule_node_copy(node
);
2610 while (isl_schedule_node_has_parent(ancestor
)) {
2611 ancestor
= isl_schedule_node_parent(ancestor
);
2613 tilable
= is_candidate(ancestor
);
2614 if (tilable
< 0 || tilable
)
2618 isl_schedule_node_free(ancestor
);
2619 return tilable
< 0 ? -1 : !tilable
;
2622 /* Collect the references to all writes in "group".
2623 * Each reference is represented by a universe set in a space
2627 * with S[i,j] the statement instance space and R[] the array reference.
2629 static __isl_give isl_union_set
*group_tagged_writes(
2630 struct gpu_array_ref_group
*group
)
2634 isl_union_set
*writes
;
2636 space
= isl_map_get_space(group
->access
);
2637 writes
= isl_union_set_empty(space
);
2638 for (i
= 0; i
< group
->n_ref
; ++i
) {
2642 if (!group
->refs
[i
]->write
)
2645 space
= isl_map_get_space(group
->refs
[i
]->tagged_access
);
2646 space
= isl_space_domain(space
);
2647 writes_i
= isl_set_universe(space
);
2648 writes
= isl_union_set_add_set(writes
, writes_i
);
2654 /* Is there any write access in "group" that requires synchronization
2655 * on a write to global memory?
2656 * We currently take into account all writes that would require
2657 * synchronization at the thread level depth, but if the copying
2658 * for this group is performed at an outer level, then we do not
2659 * actually need to take into account dependences at intermediate levels.
2661 static int any_sync_writes_in_group(struct ppcg_kernel
*kernel
,
2662 struct gpu_array_ref_group
*group
)
2664 isl_union_set
*writes
;
2665 int empty
, disjoint
;
2667 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2673 writes
= group_tagged_writes(group
);
2674 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2675 isl_union_set_free(writes
);
2677 return disjoint
< 0 ? -1 : !disjoint
;
2680 /* Collect the references to all writes in "kernel" that write directly
2681 * to global or shared memory, i.e., that are not mapped to private memory.
2682 * Each reference is represented by a universe set in a space
2686 * with S[i,j] the statement instance space and R[] the array reference.
2688 static __isl_give isl_union_set
*collect_non_private_tagged_writes(
2689 struct ppcg_kernel
*kernel
)
2691 isl_union_set
*writes
;
2694 writes
= isl_union_set_empty(isl_union_set_get_space(kernel
->arrays
));
2696 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2697 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
2699 for (j
= 0; j
< array
->n_group
; ++j
) {
2700 struct gpu_array_ref_group
*group
= array
->groups
[j
];
2701 enum ppcg_group_access_type type
;
2702 isl_union_set
*writes_ij
;
2706 type
= gpu_array_ref_group_type(group
);
2707 if (type
== ppcg_access_private
)
2709 writes_ij
= group_tagged_writes(group
);
2710 writes
= isl_union_set_union(writes
, writes_ij
);
2717 /* Are there any direct writes to global memory that require
2720 static int any_global_or_shared_sync_writes(struct ppcg_kernel
*kernel
)
2722 isl_union_set
*writes
;
2723 int empty
, disjoint
;
2725 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2731 writes
= collect_non_private_tagged_writes(kernel
);
2732 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2733 isl_union_set_free(writes
);
2735 return disjoint
< 0 ? -1 : !disjoint
;
2738 /* Construct an isl_multi_val for use as tile sizes for tiling "node"
2739 * from the elements in "tile_size".
2741 static __isl_give isl_multi_val
*construct_band_tiles_sizes(
2742 __isl_keep isl_schedule_node
*node
, int *tile_size
)
2749 space
= isl_schedule_node_band_get_space(node
);
2750 return ppcg_multi_val_from_int_list(space
, tile_size
);
2753 /* Replace the partial schedule S of the band node "node" by
2761 * if scale_tile_loops is set, with f the integers in "factor".
2762 * The list that "factor" points to is assumed to contain at least
2763 * as many elements as the number of members in the band.
2765 static __isl_give isl_schedule_node
*snap_band_to_sizes(
2766 __isl_take isl_schedule_node
*node
, int *factor
,
2767 struct ppcg_options
*options
)
2771 mv
= construct_band_tiles_sizes(node
, factor
);
2772 node
= isl_schedule_node_band_scale_down(node
, isl_multi_val_copy(mv
));
2773 if (options
->scale_tile_loops
)
2774 node
= isl_schedule_node_band_scale(node
,
2775 isl_multi_val_copy(mv
));
2776 isl_multi_val_free(mv
);
2781 /* Tile "band" with tile size specified by "sizes".
2783 * Since the tile loops will be mapped to block ids, we forcibly
2784 * turn off tile loop scaling. We may want to enable tile loop scaling
2785 * at some later point, but then we would have to support the detection
2786 * of strides during the mapping to block ids.
2787 * Similarly, since the point loops will be mapped to thread ids,
2788 * we forcibly shift the point loops so that they start at zero.
2790 static __isl_give isl_schedule_node
*tile_band(
2791 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2793 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
2797 scale_tile
= isl_options_get_tile_scale_tile_loops(ctx
);
2798 isl_options_set_tile_scale_tile_loops(ctx
, 0);
2799 shift_point
= isl_options_get_tile_shift_point_loops(ctx
);
2800 isl_options_set_tile_shift_point_loops(ctx
, 1);
2802 node
= isl_schedule_node_band_tile(node
, sizes
);
2804 isl_options_set_tile_scale_tile_loops(ctx
, scale_tile
);
2805 isl_options_set_tile_shift_point_loops(ctx
, shift_point
);
2810 /* Extract the set of parameter values and outer schedule dimensions
2811 * for which any statement instance
2812 * in the kernel inserted at "node" needs to be executed.
2813 * Intersect the set of parameter values derived from the host schedule
2814 * relation with the context of "prog".
2816 static __isl_give isl_set
*extract_context(__isl_keep isl_schedule_node
*node
,
2817 struct gpu_prog
*prog
)
2819 isl_union_map
*schedule
;
2820 isl_union_set
*schedule_domain
;
2824 schedule
= isl_schedule_node_get_prefix_schedule_relation(node
);
2825 schedule_domain
= isl_union_map_range(schedule
);
2826 empty
= isl_union_set_is_empty(schedule_domain
);
2828 isl_union_set_free(schedule_domain
);
2835 space
= isl_union_set_get_space(schedule_domain
);
2836 isl_union_set_free(schedule_domain
);
2837 space
= isl_space_set_from_params(space
);
2838 depth
= isl_schedule_node_get_schedule_depth(node
);
2839 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
2840 context
= isl_set_empty(space
);
2842 context
= isl_set_from_union_set(schedule_domain
);
2844 context
= isl_set_intersect_params(context
,
2845 isl_set_copy(prog
->context
));
2850 /* Return the set of outer array elements accessed by
2851 * by the statement instances in "domain" in "prog".
2852 * The instances in "domain" are those that appear
2853 * in the domains of the access relations in "prog".
2855 static __isl_give isl_union_set
*accessed_by_domain(
2856 __isl_take isl_union_set
*domain
, struct gpu_prog
*prog
)
2858 isl_union_map
*access
;
2859 isl_union_set
*arrays
;
2861 access
= isl_union_map_union(isl_union_map_copy(prog
->read
),
2862 isl_union_map_copy(prog
->may_write
));
2863 access
= isl_union_map_intersect_domain(access
, domain
);
2864 arrays
= isl_union_map_range(access
);
2865 arrays
= isl_union_set_apply(arrays
,
2866 isl_union_map_copy(prog
->to_outer
));
2871 /* Return the number of outer band members of the band node "node"
2872 * that are marked coincident.
2874 static int n_outer_coincidence(__isl_keep isl_schedule_node
*node
)
2878 n
= isl_schedule_node_band_n_member(node
);
2880 for (i
= 0; i
< n
; ++i
)
2881 if (!isl_schedule_node_band_member_get_coincident(node
, i
))
2887 /* If the band node "node" has more than "n" members, then split off
2888 * the first "n" of them.
2890 static __isl_give isl_schedule_node
*split_band(
2891 __isl_take isl_schedule_node
*node
, int n
)
2895 dim
= isl_schedule_node_band_n_member(node
);
2897 node
= isl_schedule_node_band_split(node
, n
);
2902 /* Scale a band node that may have been split by split_band.
2903 * "sizes" are the scaling factors for the original node.
2904 * "node" either points to the original band node, or the outer
2905 * of the two pieces after splitting.
2907 * If the number of elements in "node" is smaller than the number of
2908 * elements in "sizes", then some splitting has occurred and we split
2909 * "sizes" in the same way.
2911 static __isl_give isl_schedule_node
*scale_band(
2912 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2916 n
= isl_multi_val_dim(sizes
, isl_dim_set
);
2917 dim
= isl_schedule_node_band_n_member(node
);
2919 isl_multi_val
*sizes2
;
2921 sizes2
= isl_multi_val_copy(sizes
);
2922 sizes
= isl_multi_val_drop_dims(sizes
,
2923 isl_dim_set
, dim
, n
- dim
);
2924 sizes2
= isl_multi_val_drop_dims(sizes2
, isl_dim_set
, 0, dim
);
2925 node
= isl_schedule_node_child(node
, 0);
2926 node
= isl_schedule_node_band_scale(node
, sizes2
);
2927 node
= isl_schedule_node_parent(node
);
2930 return isl_schedule_node_band_scale(node
, sizes
);
2933 /* Return an isl_multi_aff, with as elements the parameters in "space"
2934 * that have the names specified by the elements in "names".
2935 * If (some of) these parameters do not already appear in "space",
2936 * then they are added first.
2938 static __isl_give isl_multi_aff
*parameter_vector(__isl_take isl_space
*space
,
2939 __isl_keep isl_id_list
*names
)
2942 isl_local_space
*ls
;
2946 space
= isl_space_free(space
);
2948 n
= isl_id_list_n_id(names
);
2949 for (i
= 0; i
< n
; ++i
) {
2953 id
= isl_id_list_get_id(names
, i
);
2954 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2959 pos
= isl_space_dim(space
, isl_dim_param
);
2960 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2961 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2963 ma
= isl_multi_aff_zero(isl_space_copy(space
));
2964 ls
= isl_local_space_from_space(isl_space_domain(space
));
2965 for (i
= 0; i
< n
; ++i
) {
2970 id
= isl_id_list_get_id(names
, i
);
2971 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2973 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
2974 isl_dim_param
, pos
);
2975 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2977 isl_local_space_free(ls
);
2982 /* Return constraints on the domain elements that equate a sequence of
2983 * parameters called "names", to the partial schedule
2984 * of "node" modulo the integers in "size".
2985 * The number of elements in the array "size" should be equal
2986 * to the number of elements in "names".
2987 * The number of members of the band node "node" should be smaller
2988 * than or equal to this number. If it is smaller, then the first
2989 * elements of "names" are equated to zero.
2991 static __isl_give isl_union_set
*set_schedule_modulo(
2992 __isl_keep isl_schedule_node
*node
, __isl_keep isl_id_list
*names
,
2998 isl_multi_union_pw_aff
*mupa
, *mupa2
;
3000 isl_union_set
*domain
;
3004 n
= isl_id_list_n_id(names
);
3006 return isl_schedule_node_get_universe_domain(node
);
3007 n_zero
= n
- isl_schedule_node_band_n_member(node
);
3009 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
3010 mv
= construct_band_tiles_sizes(node
, size
+ n_zero
);
3011 mupa
= isl_multi_union_pw_aff_mod_multi_val(mupa
, mv
);
3013 space
= isl_multi_union_pw_aff_get_space(mupa
);
3014 space
= isl_space_params(space
);
3015 space
= isl_space_set_from_params(space
);
3016 space
= isl_space_add_dims(space
, isl_dim_set
, n_zero
);
3017 ma
= isl_multi_aff_zero(space
);
3019 domain
= isl_schedule_node_get_universe_domain(node
);
3020 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(
3021 isl_union_set_copy(domain
), ma
);
3022 mupa
= isl_multi_union_pw_aff_range_product(mupa2
, mupa
);
3024 space
= isl_multi_union_pw_aff_get_space(mupa
);
3025 ma
= parameter_vector(space
, names
);
3027 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(domain
, ma
);
3028 mupa
= isl_multi_union_pw_aff_sub(mupa
, mupa2
);
3030 return isl_multi_union_pw_aff_zero_union_set(mupa
);
3033 /* Insert a context node at "node" introducing the block and thread
3034 * identifiers along with their bounds, which are stored in kernel->grid_size
3035 * and kernel->block_dim.
3036 * Note that the bounds on the block identifiers may implicitly impose
3037 * constraints on the parameters. A guard needs to be inserted
3038 * in the schedule tree to ensure that those bounds hold at "node".
3039 * This guard is inserted in insert_guard.
3041 static __isl_give isl_schedule_node
*insert_context(struct ppcg_kernel
*kernel
,
3042 __isl_take isl_schedule_node
*node
)
3046 context
= isl_set_universe(isl_set_get_space(kernel
->context
));
3048 context
= add_bounded_parameters_dynamic(context
,
3049 kernel
->grid_size
, kernel
->block_ids
);
3050 context
= add_bounded_parameters(context
,
3051 kernel
->block_dim
, kernel
->thread_ids
);
3053 node
= isl_schedule_node_insert_context(node
, context
);
3058 /* Insert a guard that eliminates kernel launches where the kernel
3059 * obviously does not have any work to do.
3061 * In particular, eliminate kernel launches where there are obviously
3063 * Use the same block size constraints that are used to create the context
3064 * to ensure that all constraints implicit in the constructed context
3065 * are imposed by the guard.
3067 * Additionally, add other constraints that are valid
3068 * for each executed instance ("context"), as long as this does not result
3071 static __isl_give isl_schedule_node
*insert_guard(
3072 __isl_take isl_schedule_node
*node
, __isl_keep isl_set
*context
,
3073 __isl_keep isl_multi_pw_aff
*size
, struct ppcg_scop
*scop
)
3079 guard
= isl_set_copy(context
);
3080 guard
= isl_set_compute_divs(guard
);
3081 guard
= isl_set_from_basic_set(isl_set_simple_hull(guard
));
3083 nparam
= isl_set_dim(guard
, isl_dim_param
);
3084 n
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
3085 ids
= ppcg_scop_generate_names(scop
, n
, "__ppcg_tmp");
3086 guard
= add_bounded_parameters_dynamic(guard
, size
, ids
);
3087 isl_id_list_free(ids
);
3088 guard
= isl_set_project_out(guard
, isl_dim_param
, nparam
, n
);
3090 node
= isl_schedule_node_insert_guard(node
, guard
);
3095 /* Does any array reference group mapping require the band that is mapped
3096 * to threads to be unrolled?
3098 static int kernel_requires_unroll(struct ppcg_kernel
*kernel
)
3102 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3103 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3105 for (j
= 0; j
< array
->n_group
; ++j
) {
3106 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3107 if (gpu_array_ref_group_requires_unroll(group
))
3115 /* Mark the given band node "node" for unrolling by the AST generator and
3116 * then sink it to the leaves of the schedule tree.
3117 * All dimensions of "node" are assumed to be coincident, such that this
3118 * sinking is a valid operation.
3120 static __isl_give isl_schedule_node
*unroll(__isl_take isl_schedule_node
*node
)
3122 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
3124 node
= isl_schedule_node_band_sink(node
);
3129 /* Insert a synchronization node in the schedule tree of "node"
3130 * after the core computation of "kernel" at the level of the band
3131 * that is mapped to threads, except if that level is equal to
3132 * that of the band that is mapped to blocks or if there are no writes
3133 * to global or shared memory in the core computation that require
3135 * If there are any writes to shared memory and the shared memory
3136 * copying is performed at the same level, then synchronization
3137 * is needed between the core and the copying anyway, so we might
3138 * as well add it here. If the copying is performed at a higher
3139 * level, then different iterations of intermediate schedule dimensions
3140 * may have a different mapping from between shared memory elements and
3141 * threads, such that synchronization is required after the core.
3142 * "node" is assumed to point to the kernel node.
3144 * If the shared and the thread mark point to the same node, then make
3145 * sure the synchronization is inserted outside of the shared mark.
3147 static __isl_give isl_schedule_node
*add_sync(struct ppcg_kernel
*kernel
,
3148 __isl_take isl_schedule_node
*node
)
3153 need_sync
= any_global_or_shared_sync_writes(kernel
);
3155 return isl_schedule_node_free(node
);
3159 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3160 depth
= isl_schedule_node_get_schedule_depth(node
);
3161 node
= gpu_tree_move_up_to_kernel(node
);
3162 if (depth
== isl_schedule_node_get_schedule_depth(node
))
3165 node
= gpu_tree_move_down_to_depth(node
, depth
, kernel
->core
);
3166 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3168 node
= gpu_tree_move_up_to_kernel(node
);
3173 /* Return a read ("read" is 1) or write access relation for "group"
3174 * with those accesses removed that are only needed to communicate data
3175 * within the subtree of the schedule rooted at "node".
3176 * Furthermore, include the prefix schedule at "node".
3177 * That is, return a relation of the form
3181 * with D the outer schedule dimensions at "node".
3183 static __isl_give isl_union_map
*anchored_non_local_accesses(
3184 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3185 __isl_take isl_schedule_node
*node
, int read
)
3187 isl_union_map
*access
;
3188 isl_union_map
*prefix
;
3190 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
3191 prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(prefix
,
3192 isl_union_pw_multi_aff_copy(kernel
->contraction
));
3193 access
= gpu_array_ref_group_access_relation(group
, read
, !read
);
3194 access
= remove_local_accesses_group(kernel
, group
, access
, prefix
,
3196 access
= isl_union_map_range_product(prefix
, access
);
3201 /* Given an array reference group "group", create a mapping
3203 * read[D -> A] -> [D -> A]
3205 * if "read" is set or
3207 * write[D -> A] -> [D -> A]
3209 * if "read" is not set.
3210 * D corresponds to the outer tile->depth dimensions of
3211 * the kernel schedule.
3213 static __isl_give isl_multi_aff
*create_from_access(isl_ctx
*ctx
,
3214 struct gpu_array_ref_group
*group
, int read
)
3216 struct gpu_array_tile
*tile
;
3220 tile
= gpu_array_ref_group_tile(group
);
3221 space
= isl_space_copy(group
->array
->space
);
3222 space
= isl_space_from_range(space
);
3223 space
= isl_space_add_dims(space
, isl_dim_in
, tile
->depth
);
3224 space
= isl_space_wrap(space
);
3225 space
= isl_space_map_from_set(space
);
3227 id
= isl_id_alloc(ctx
, read
? "read" : "write", group
);
3228 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
3230 return isl_multi_aff_identity(space
);
3233 /* If any writes in "group" require synchronization, then make sure
3234 * that there is a synchronization node for "kernel" after the node
3235 * following "node" in a sequence.
3237 * If "shared" is set and no synchronization is needed for
3238 * the writes to global memory, then add synchronization before
3239 * the kernel to protect shared memory from being overwritten
3240 * by the next iteration of the core computation.
3241 * No additional synchronization is needed to protect against
3242 * the next copy into shared memory because each element of
3243 * the shared memory tile is always copied by the same thread.
3245 static __isl_give isl_schedule_node
*add_group_write_sync(
3246 __isl_take isl_schedule_node
*node
, struct ppcg_kernel
*kernel
,
3247 struct gpu_array_ref_group
*group
, int shared
)
3251 need_sync
= any_sync_writes_in_group(kernel
, group
);
3253 return isl_schedule_node_free(node
);
3255 node
= isl_schedule_node_parent(node
);
3256 node
= isl_schedule_node_next_sibling(node
);
3257 node
= isl_schedule_node_child(node
, 0);
3258 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3259 } else if (shared
) {
3260 struct gpu_array_tile
*tile
;
3262 tile
= gpu_array_ref_group_tile(group
);
3263 node
= isl_schedule_node_parent(node
);
3264 node
= isl_schedule_node_parent(node
);
3265 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
,
3267 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3273 /* Add copy statements to the schedule tree of "node"
3274 * for reading from global memory to private memory (if "read" is set) or
3275 * for writing back from private memory to global memory
3276 * (if "read" is not set) for the array reference group "group" that
3277 * is mapped to private memory.
3278 * On input, "node" points to the kernel node, and it is moved
3279 * back there on output.
3281 * The copies are performed in the order of the array elements.
3282 * The copy statement instances include a reference to the outer
3283 * tile->depth dimensions of the kernel schedule for ease of
3284 * combining them with the group tiling.
3286 * That is, the extra schedule is of the form
3290 * where D corresponds to the outer tile->depth dimensions of
3291 * the kernel schedule and A to the global array.
3292 * This schedule is unrolled because registers are not addressable.
3294 * The copying is inserted in the schedule tree through an extension
3299 * where the extra domain elements type[D -> A] are those accessed
3301 * A filter is inserted on type[D -> A] to ensure that the element
3302 * is read/written by the same thread that needs the element.
3303 * This filter is obtained by applying
3307 * to the thread filter for the core statements.
3309 * The extension is inserted before the core computation in case of a read
3310 * and after the core computation in case of a write.
3311 * In the latter case, we also make sure that there is a synchronization
3312 * node after the write to global memory, unless this write is performed
3313 * at the outer level of the kernel.
3314 * In principle, this synchronization could be inserted higher
3315 * in the schedule tree depending on where the corresponding reads
3316 * from global memory are performed.
3318 static __isl_give isl_schedule_node
*add_copies_group_private(
3319 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3320 __isl_take isl_schedule_node
*node
, int read
)
3322 struct gpu_array_tile
*tile
;
3323 isl_union_map
*access
;
3324 isl_union_set
*domain
;
3326 isl_multi_aff
*from_access
;
3327 isl_multi_pw_aff
*mpa
;
3328 isl_multi_union_pw_aff
*mupa
;
3329 isl_union_pw_multi_aff
*contraction
;
3330 isl_schedule_node
*graft
;
3331 isl_union_set
*filter
;
3335 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3336 tile
= gpu_array_ref_group_tile(group
);
3337 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3339 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3340 empty
= isl_union_map_is_empty(access
);
3341 if (empty
< 0 || empty
) {
3342 isl_union_map_free(access
);
3344 return isl_schedule_node_free(node
);
3345 return gpu_tree_move_up_to_kernel(node
);
3348 group
->array
->global
= 1;
3349 group
->local_array
->global
= 1;
3351 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3352 space
= isl_space_domain(isl_multi_aff_get_space(from_access
));
3353 access
= isl_union_map_preimage_range_multi_aff(access
, from_access
);
3355 filter
= isl_union_set_copy(kernel
->thread_filter
);
3356 contraction
= isl_union_pw_multi_aff_copy(kernel
->contraction
);
3357 filter
= isl_union_set_preimage_union_pw_multi_aff(filter
, contraction
);
3358 filter
= isl_union_set_apply(filter
, isl_union_map_copy(access
));
3359 filter
= isl_union_set_detect_equalities(filter
);
3360 filter
= isl_union_set_coalesce(filter
);
3362 domain
= isl_union_map_range(access
);
3363 access
= isl_union_set_wrapped_domain_map(domain
);
3364 access
= isl_union_map_reverse(access
);
3365 access
= isl_union_map_coalesce(access
);
3366 graft
= isl_schedule_node_from_extension(access
);
3368 space
= isl_space_map_from_set(space
);
3369 mpa
= isl_multi_pw_aff_identity(space
);
3370 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
3371 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3373 graft
= isl_schedule_node_child(graft
, 0);
3374 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3375 graft
= unroll(graft
);
3377 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3379 graft
= isl_schedule_node_parent(graft
);
3382 node
= isl_schedule_node_graft_before(node
, graft
);
3384 node
= isl_schedule_node_graft_after(node
, graft
);
3385 if (kernel_depth
< tile
->depth
)
3386 node
= add_group_write_sync(node
, kernel
, group
, 0);
3389 node
= gpu_tree_move_up_to_kernel(node
);
3394 /* Add copy statements to the schedule tree of "node"
3395 * for reading from global memory to shared memory (if "read" is set) or
3396 * for writing back from shared memory to global memory
3397 * (if "read" is not set) for the array reference group "group" that
3398 * is mapped to shared memory.
3399 * On input, "node" points to the kernel node, and it is moved
3400 * back there on output.
3402 * The copies are performed in the order of the corresponding shared
3404 * The copy statement instances include a reference to the outer
3405 * tile->depth dimensions of the kernel schedule for ease of
3406 * combining them with the group tiling.
3408 * If we are performing a read from global memory to shared memory and
3409 * if the array involved is not a scalar, then we copy
3410 * the entire tile to shared memory. This may result in some extra
3411 * elements getting copied, but it should lead to simpler code
3412 * (which means that fewer registers may be needed) and less divergence.
3414 * Otherwise, we only copy the elements that will be read or have been written
3417 * That is, the extra schedule is of the form
3421 * where D corresponds to the outer tile->depth dimensions of
3422 * the kernel schedule, A to the global array and T is the corresponding
3423 * shared memory tile.
3425 * The copying is inserted in the schedule tree through an extension
3430 * where the extra domain elements type[D -> A] are those accessed
3431 * by the group. In the case of read from a non-scalar, this set
3432 * is replaced by the entire shared memory tile.
3434 * If the "unroll_copy_shared" option is set, then the AST generator
3435 * is instructed to unroll the copying code.
3437 * A filter is inserted on type[D -> A] to map the copy instances
3438 * to the threads. In particular, the thread identifiers are
3439 * equated to the position inside the shared memory tile (T)
3440 * modulo the block size.
3441 * We try to align the innermost tile dimension with the innermost
3442 * thread identifier (x) as a heuristic to improve coalescing.
3443 * In particular, if the dimension of the tile is greater than
3444 * the dimension of the block, then the schedule mapping to the tile
3445 * is broken up into two pieces and the filter is applied to the inner part.
3446 * If, on the other hand, the dimension of the tile is smaller than
3447 * the dimension of the block, then the initial thread identifiers
3448 * are equated to zero and the remaining thread identifiers are
3449 * matched to the memory tile.
3451 * The extension is inserted before the core computation in case of a read
3452 * and after the core computation in case of a write.
3453 * In the case of a read, we first need to make sure there is some
3454 * synchronization before the core computation such that we can put the read
3455 * from global memory to shared memory before that synchronization.
3456 * This ensures that all threads have finished copying into shared memory
3457 * before the shared memory is used.
3458 * We also need to make sure that there is a synchronization node after
3459 * the core computation to ensure that the next load into shared memory
3460 * only happens after all data has been used. There is no need for
3461 * this synchronization if we are at the outer level since then there
3462 * won't be a next load.
3463 * In the case of a write, we need to make sure there is some synchronization
3464 * after the core computation such that we can put the write from shared
3465 * memory to global memory after that synchronization.
3466 * Unless we are at the outer level, we also need a synchronization node
3467 * after the write to ensure the data is saved to global memory
3468 * before the next iteration writes to the same shared memory.
3469 * It also makes sure the data has arrived in global memory before
3470 * it is read in a subsequent iteration.
3472 static __isl_give isl_schedule_node
*add_copies_group_shared(
3473 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3474 __isl_take isl_schedule_node
*node
, int read
)
3476 struct gpu_array_tile
*tile
;
3477 isl_union_map
*access
;
3478 isl_union_set
*domain
;
3480 isl_multi_aff
*from_access
;
3481 isl_multi_pw_aff
*mpa
;
3482 isl_multi_union_pw_aff
*mupa
;
3483 isl_schedule_node
*graft
;
3484 isl_union_set
*filter
;
3489 tile
= gpu_array_ref_group_tile(group
);
3490 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3491 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3493 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3494 empty
= isl_union_map_is_empty(access
);
3495 if (empty
< 0 || empty
) {
3496 isl_union_map_free(access
);
3498 return isl_schedule_node_free(node
);
3499 return gpu_tree_move_up_to_kernel(node
);
3502 group
->array
->global
= 1;
3503 group
->local_array
->global
= 1;
3505 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3507 ma
= isl_multi_aff_copy(tile
->tiling
);
3508 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3509 isl_multi_aff_copy(from_access
));
3510 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3511 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3513 domain
= isl_union_map_range(access
);
3515 if (read
&& !gpu_array_is_scalar(group
->array
)) {
3517 isl_union_set_free(domain
);
3518 map
= group_tile(group
);
3519 domain
= isl_union_set_from_set(isl_map_wrap(map
));
3522 domain
= isl_union_set_preimage_multi_aff(domain
, from_access
);
3523 access
= isl_union_set_wrapped_domain_map(domain
);
3524 access
= isl_union_map_reverse(access
);
3525 access
= isl_union_map_coalesce(access
);
3526 graft
= isl_schedule_node_from_extension(access
);
3528 graft
= isl_schedule_node_child(graft
, 0);
3530 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3531 if (kernel
->options
->unroll_copy_shared
)
3532 graft
= ppcg_set_schedule_node_type(graft
, isl_ast_loop_unroll
);
3534 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0) {
3535 graft
= isl_schedule_node_band_split(graft
,
3536 tile
->n
- kernel
->n_block
);
3537 graft
= isl_schedule_node_child(graft
, 0);
3539 if (tile
->n
< kernel
->n_block
)
3540 skip
= kernel
->n_block
- tile
->n
;
3543 filter
= set_schedule_modulo(graft
, kernel
->thread_ids
,
3545 if (!kernel
->options
->wrap
)
3546 graft
= snap_band_to_sizes(graft
, kernel
->block_dim
+ skip
,
3548 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0)
3549 graft
= isl_schedule_node_parent(graft
);
3550 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3552 while (graft
&& isl_schedule_node_has_parent(graft
))
3553 graft
= isl_schedule_node_parent(graft
);
3556 if (kernel_depth
< tile
->depth
)
3557 node
= gpu_tree_ensure_sync_after_core(node
, kernel
);
3558 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3559 node
= isl_schedule_node_graft_before(node
, graft
);
3561 node
= gpu_tree_move_right_to_sync(node
, kernel
);
3562 node
= isl_schedule_node_graft_after(node
, graft
);
3563 if (kernel_depth
< tile
->depth
)
3564 node
= add_group_write_sync(node
, kernel
, group
, 1);
3567 node
= gpu_tree_move_up_to_kernel(node
);
3572 /* Check whether the array reference group "group" is mapped to
3573 * private or shared memory and, if so,
3574 * add copy statements to the schedule tree of "node"
3575 * for reading from global memory to private or shared memory
3576 * (if "read" is set) or for writing back from private or shared memory
3577 * to global memory (if "read" is not set) for this group.
3578 * On input, "node" points to the kernel node, and it is moved
3579 * back there on output.
3581 static __isl_give isl_schedule_node
*add_copies_group(
3582 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3583 __isl_take isl_schedule_node
*node
, int read
)
3585 enum ppcg_group_access_type type
;
3587 type
= gpu_array_ref_group_type(group
);
3588 if (type
== ppcg_access_private
)
3589 return add_copies_group_private(kernel
, group
, node
, read
);
3590 if (type
== ppcg_access_shared
)
3591 return add_copies_group_shared(kernel
, group
, node
, read
);
3595 /* For each array reference group that is mapped to private or shared memory,
3596 * add copy statements to the schedule tree of "node"
3597 * for reading from global memory to private or shared memory
3598 * and for writing back.
3599 * On input, "node" points to the kernel node, and it is moved
3600 * back there on output.
3602 static __isl_give isl_schedule_node
*add_copies(struct ppcg_kernel
*kernel
,
3603 __isl_take isl_schedule_node
*node
)
3607 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3608 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3610 for (j
= 0; j
< array
->n_group
; ++j
) {
3611 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3613 node
= add_copies_group(kernel
, group
, node
, 1);
3616 node
= add_copies_group(kernel
, group
, node
, 0);
3625 /* Mark all dimensions in the current band node atomic.
3627 static __isl_give isl_schedule_node
*atomic(__isl_take isl_schedule_node
*node
)
3629 return ppcg_set_schedule_node_type(node
, isl_ast_loop_atomic
);
3632 /* Mark "node" atomic, if it is a band node.
3633 * Do the same for all ancestors.
3634 * Return a pointer to "node" (in the updated schedule tree).
3636 static __isl_give isl_schedule_node
*atomic_ancestors(
3637 __isl_take isl_schedule_node
*node
)
3643 if (!isl_schedule_node_has_parent(node
))
3646 pos
= isl_schedule_node_get_child_position(node
);
3647 node
= isl_schedule_node_parent(node
);
3648 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
)
3649 node
= atomic(node
);
3650 node
= atomic_ancestors(node
);
3651 node
= isl_schedule_node_child(node
, pos
);
3656 /* Collect all write references that require synchronization.
3657 * "node" is assumed to point to the kernel node.
3658 * Each reference is represented by a universe set in a space
3662 * with S[i,j] the statement instance space and R[] the array reference.
3664 * This function should be called before block and thread filters are added.
3666 * Synchronization is needed after a write if there is a subsequent read
3667 * within the same block that may not be performed by the same thread.
3668 * There should not be any dependences between different blocks,
3669 * so we start with the flow dependences within the same kernel invocation
3670 * and we subtract from these those dependences that are mapped
3671 * to the same iteration of the bands where synchronization is inserted.
3672 * We do not remove pairs of instances that are known to map to
3673 * the same thread across different iterations of the intermediate
3674 * bands because the read may be performed by a different thread
3675 * than the one that needs the value if shared memory is involved.
3677 * We also consider all pairs of possible writes that access the same
3678 * memory location and that may be mapped to the same block but not
3679 * to the same iteration of the intermediate bands.
3680 * In theory, it would be possible for one thread to still be in
3681 * a previous iteration of a loop in these bands.
3682 * A write to global memory in this delayed thread could then overwrite
3683 * a write from another thread that has already moved on to
3684 * the next iteration.
3686 * After computing the above writes paired off with reads or writes
3687 * that depend on them, we project onto the domain writes.
3688 * Sychronization is needed after writes to global memory
3689 * through these references.
3691 static __isl_give isl_union_set
*compute_sync_writes(
3692 struct ppcg_kernel
*kernel
, __isl_keep isl_schedule_node
*node
)
3694 isl_union_map
*local
;
3695 isl_union_map
*may_writes
, *shared_access
;
3696 isl_union_map
*kernel_prefix
, *thread_prefix
;
3697 isl_union_map
*equal
;
3698 isl_union_set
*wrap
;
3699 isl_union_set
*domain
;
3700 isl_union_pw_multi_aff
*contraction
;
3702 kernel_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3703 node
= isl_schedule_node_copy(node
);
3704 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3705 thread_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3706 isl_schedule_node_free(node
);
3708 contraction
= kernel
->contraction
;
3709 kernel_prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(
3710 kernel_prefix
, isl_union_pw_multi_aff_copy(contraction
));
3711 thread_prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(
3712 thread_prefix
, isl_union_pw_multi_aff_copy(contraction
));
3713 domain
= isl_union_set_copy(kernel
->expanded_domain
);
3714 domain
= isl_union_set_universe(domain
);
3716 may_writes
= isl_union_map_copy(kernel
->prog
->scop
->tagged_may_writes
);
3717 may_writes
= isl_union_map_curry(may_writes
);
3718 may_writes
= isl_union_map_intersect_domain(may_writes
, domain
);
3719 may_writes
= isl_union_map_uncurry(may_writes
);
3720 shared_access
= isl_union_map_copy(may_writes
);
3721 shared_access
= isl_union_map_apply_range(shared_access
,
3722 isl_union_map_reverse(may_writes
));
3724 local
= isl_union_map_copy(kernel
->prog
->scop
->tagged_dep_flow
);
3725 local
= isl_union_map_union(local
, shared_access
);
3726 local
= isl_union_map_zip(local
);
3728 equal
= isl_union_map_apply_range(kernel_prefix
,
3729 isl_union_map_reverse(isl_union_map_copy(kernel_prefix
)));
3730 wrap
= isl_union_map_wrap(equal
);
3731 local
= isl_union_map_intersect_domain(local
, wrap
);
3732 equal
= isl_union_map_apply_range(thread_prefix
,
3733 isl_union_map_reverse(isl_union_map_copy(thread_prefix
)));
3734 wrap
= isl_union_map_wrap(equal
);
3735 local
= isl_union_map_subtract_domain(local
, wrap
);
3737 local
= isl_union_map_zip(local
);
3738 local
= isl_union_map_universe(local
);
3740 return isl_union_map_domain(local
);
3743 /* Group the domain elements into a single space, named kernelX,
3744 * with X the kernel sequence number "kernel_id".
3746 static __isl_give isl_schedule_node
*group_statements(
3747 __isl_take isl_schedule_node
*node
, int kernel_id
)
3755 snprintf(buffer
, sizeof(buffer
), "kernel%d", kernel_id
);
3756 id
= isl_id_alloc(isl_schedule_node_get_ctx(node
), buffer
, NULL
);
3757 return isl_schedule_node_group(node
, id
);
3760 /* Create a ppcg_kernel representing the domain instances that reach "node"
3761 * and insert a mark node pointing to the ppcg_kernel before "node".
3762 * The band that "node" points to is the band that needs to be mapped
3763 * to block identifiers. The band that needs to be mapped to thread
3764 * identifiers should be marked by a "thread" mark by the caller.
3765 * The linear branch between the current node and the "thread" mark
3766 * may also have a "shared" mark. If present, the mapping to shared
3767 * memory is computed at that point.
3768 * Both marks are removed by this function.
3769 * If "scale" is set, then the band that "node" points to is scaled
3772 * Mark all outer band nodes as atomic to ensure each kernel is only
3774 * If the domain elements that reach "node" live in more than one space,
3775 * then group the domain elements into a single space, named kernelX,
3776 * with X the kernel sequence number.
3778 * Insert a guard node governing the kernel node to ensure that
3779 * no kernels with zero blocks are launched.
3781 * Insert a context node describing the block and thread
3782 * identifiers inside the kernel mark.
3783 * The context node needs to be inserted after the effective block size
3784 * has been determined such that the bounds on the thread identifiers
3785 * would reflect the effective block size.
3786 * Insert a filter node inside the context node mapping the statement
3787 * instances to block identifiers. In particular, the block identifiers
3788 * are equated to the partial schedule of band that was marked for mapping
3789 * to blocks modulo the grid size.
3790 * Insert a filter node inside the "thread" mark mapping the statement
3791 * instances to thread identifiers. In particular, the thread identifiers
3792 * are equated to the partial schedule of band that was marked for mapping
3793 * to threads modulo the block size.
3795 * Compute array reference groups for all arrays, set the local
3796 * array bounds based on the set of domain instances that reach
3797 * the kernel node, check the total amount of shared memory used
3798 * and compute all group tilings.
3799 * The array reference groups are computed after the block filter
3800 * has been inserted because it affects the mapping to shared or
3801 * private memory. This computation also requires the thread filter
3802 * (in the ppcg_kernel object), but this thread filter should not
3803 * have been added to the schedule tree yet since the computation
3804 * requires the schedule of the band that needs to be mapped to
3805 * threads before the privatization is applied.
3807 * If any array reference group requires the band mapped to threads
3808 * to be unrolled, then we perform the required unrolling.
3810 * We save a copy of the schedule that may influence the mappings
3811 * to shared or private memory in kernel->copy_schedule.
3813 * Finally, we add synchronization and copy statements to the schedule tree,
3814 * remove the "thread" mark and create representations for the local
3815 * variables in the kernel.
3817 * We keep a copy of the isl_id that points to the kernel to ensure
3818 * that the kernel does not get destroyed if the schedule node
3819 * is freed due to some error condition.
3821 __isl_give isl_schedule_node
*gpu_create_kernel(struct gpu_gen
*gen
,
3822 __isl_take isl_schedule_node
*node
, int scale
,
3823 __isl_keep isl_multi_val
*sizes
)
3825 struct ppcg_kernel
*kernel
;
3827 isl_schedule_node
*node_thread
;
3828 isl_union_map
*host_schedule
;
3829 isl_union_pw_multi_aff
*contraction
;
3830 isl_set
*host_domain
;
3831 isl_union_set
*domain
, *expanded
;
3832 int single_statement
;
3834 node
= gpu_tree_insert_shared_before_thread(node
);
3838 kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
3839 kernel
= ppcg_kernel_create_local_arrays(kernel
, gen
->prog
);
3841 return isl_schedule_node_free(node
);
3843 domain
= isl_schedule_node_get_domain(node
);
3844 single_statement
= isl_union_set_n_set(domain
) == 1;
3846 kernel
->ctx
= gen
->ctx
;
3847 kernel
->prog
= gen
->prog
;
3848 kernel
->options
= gen
->options
;
3849 kernel
->context
= extract_context(node
, gen
->prog
);
3850 kernel
->core
= isl_union_set_universe(isl_union_set_copy(domain
));
3851 contraction
= isl_schedule_node_get_subtree_contraction(node
);
3852 kernel
->contraction
= isl_union_pw_multi_aff_copy(contraction
);
3853 expanded
= isl_union_set_copy(domain
);
3854 expanded
= isl_union_set_preimage_union_pw_multi_aff(expanded
,
3856 kernel
->expanded_domain
= isl_union_set_copy(expanded
);
3857 kernel
->arrays
= accessed_by_domain(expanded
, gen
->prog
);
3858 kernel
->n_grid
= n_outer_coincidence(node
);
3859 node_thread
= isl_schedule_node_copy(node
);
3860 node_thread
= gpu_tree_move_down_to_thread(node_thread
, kernel
->core
);
3861 node_thread
= isl_schedule_node_child(node_thread
, 0);
3862 kernel
->n_block
= n_outer_coincidence(node_thread
);
3863 isl_schedule_node_free(node_thread
);
3864 kernel
->id
= gen
->kernel_id
++;
3865 read_grid_and_block_sizes(kernel
, gen
);
3867 kernel
->sync_writes
= compute_sync_writes(kernel
, node
);
3869 host_schedule
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3870 host_domain
= isl_set_from_union_set(isl_union_map_range(
3873 node
= atomic_ancestors(node
);
3875 id
= isl_id_alloc(gen
->ctx
, "kernel", kernel
);
3876 id
= isl_id_set_free_user(id
, &ppcg_kernel_free_wrap
);
3877 node
= isl_schedule_node_insert_mark(node
, isl_id_copy(id
));
3879 if (!single_statement
)
3880 node
= group_statements(node
, kernel
->id
);
3882 node
= isl_schedule_node_child(node
, 0);
3883 node
= split_band(node
, kernel
->n_grid
);
3884 kernel
->block_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3885 kernel
->n_grid
, "b");
3886 kernel
->block_filter
= set_schedule_modulo(node
, kernel
->block_ids
,
3888 kernel
->grid_size
= extract_grid_size(kernel
,
3889 isl_union_set_copy(domain
));
3890 if (!kernel
->options
->wrap
)
3891 node
= snap_band_to_sizes(node
, kernel
->grid_dim
,
3894 node
= scale_band(node
, isl_multi_val_copy(sizes
));
3895 node
= isl_schedule_node_parent(node
);
3896 if (!single_statement
)
3897 node
= isl_schedule_node_parent(node
);
3898 node
= insert_guard(node
, kernel
->context
, kernel
->grid_size
,
3900 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3901 node
= isl_schedule_node_child(node
, 0);
3902 node
= split_band(node
, kernel
->n_block
);
3903 kernel
->thread_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3904 kernel
->n_block
, "t");
3905 kernel
->thread_filter
= set_schedule_modulo(node
, kernel
->thread_ids
,
3907 if (extract_block_size(kernel
, domain
) < 0)
3908 node
= isl_schedule_node_free(node
);
3910 node
= gpu_tree_move_up_to_kernel(node
);
3911 node
= isl_schedule_node_child(node
, 0);
3912 node
= insert_context(kernel
, node
);
3913 node
= isl_schedule_node_child(node
, 0);
3914 node
= isl_schedule_node_insert_filter(node
,
3915 isl_union_set_copy(kernel
->block_filter
));
3917 node
= gpu_tree_move_up_to_kernel(node
);
3919 if (gpu_group_references(kernel
, node
) < 0)
3920 node
= isl_schedule_node_free(node
);
3921 localize_bounds(kernel
, host_domain
);
3922 isl_set_free(host_domain
);
3924 check_shared_memory_bound(kernel
);
3925 mark_global_arrays(kernel
);
3926 compute_group_tilings(kernel
);
3928 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3929 node
= isl_schedule_node_child(node
, 0);
3930 if (!kernel
->options
->wrap
)
3931 node
= snap_band_to_sizes(node
, kernel
->block_dim
,
3933 node
= isl_schedule_node_insert_filter(node
,
3934 isl_union_set_copy(kernel
->thread_filter
));
3935 if (kernel_requires_unroll(kernel
)) {
3936 node
= isl_schedule_node_child(node
, 0);
3937 node
= unroll(node
);
3940 node
= gpu_tree_move_up_to_thread(node
);
3941 kernel
->copy_schedule_dim
= isl_schedule_node_get_schedule_depth(node
);
3942 kernel
->copy_schedule
=
3943 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
3944 contraction
= isl_union_pw_multi_aff_copy(kernel
->contraction
);
3945 kernel
->copy_schedule
=
3946 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3947 kernel
->copy_schedule
, contraction
);
3949 node
= gpu_tree_move_up_to_kernel(node
);
3951 node
= add_sync(kernel
, node
);
3952 node
= add_copies(kernel
, node
);
3954 node
= gpu_tree_move_down_to_shared(node
, kernel
->core
);
3955 node
= isl_schedule_node_delete(node
);
3957 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3958 node
= isl_schedule_node_delete(node
);
3960 node
= gpu_tree_move_up_to_kernel(node
);
3962 if (create_kernel_vars(kernel
) < 0)
3963 node
= isl_schedule_node_free(node
);
3965 if (!single_statement
)
3966 node
= isl_schedule_node_parent(node
);
3967 node
= isl_schedule_node_parent(node
);
3971 ppcg_kernel_free(kernel
);
3975 /* Insert a zero-dimensional permutable band at "node".
3977 static __isl_give isl_schedule_node
*insert_empty_permutable_band(
3978 __isl_take isl_schedule_node
*node
)
3981 isl_schedule
*schedule
;
3982 isl_union_set
*domain
;
3983 isl_multi_union_pw_aff
*mupa
;
3985 schedule
= isl_schedule_node_get_schedule(node
);
3986 domain
= isl_schedule_get_domain(schedule
);
3987 space
= isl_union_set_get_space(domain
);
3988 isl_union_set_free(domain
);
3989 isl_schedule_free(schedule
);
3991 space
= isl_space_set_from_params(space
);
3992 mupa
= isl_multi_union_pw_aff_zero(space
);
3993 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3994 node
= isl_schedule_node_band_set_permutable(node
, 1);
3999 /* See if hybrid tiling can be performed on "node" and its parent.
4000 * If so, apply hybrid tiling and return the updated schedule tree.
4001 * If not, return the original schedule tree.
4002 * Return NULL on error.
4004 * First check if "node", together with its parent, meets
4005 * the basic requirements for hybrid tiling.
4006 * If so, compute the relative dependence distances of "node"
4007 * with respect to its parent and check if they are sufficiently bounded.
4008 * If so, apply hybrid tiling using user specified tile sizes.
4010 * The tile sizes are read before the dependence distance bounds are
4011 * computed, because the user may have specified fewer dimensions
4012 * than are available. In this case, the remaining schedule dimensions
4013 * are split off and the dependence distances should be computed
4014 * after these dimensions have been split off.
4016 static __isl_give isl_schedule_node
*try_hybrid_tile(struct gpu_gen
*gen
,
4017 __isl_take isl_schedule_node
*node
)
4022 isl_schedule_node
*orig
= node
;
4023 ppcg_ht_bounds
*bounds
;
4025 ok
= ppcg_ht_parent_has_input_pattern(node
);
4027 return isl_schedule_node_free(node
);
4031 tile_len
= 1 + isl_schedule_node_band_n_member(node
);
4032 tile_size
= read_tile_sizes(gen
, &tile_len
);
4034 return isl_schedule_node_free(node
);
4036 node
= isl_schedule_node_copy(node
);
4037 node
= split_band(node
, tile_len
- 1);
4038 node
= isl_schedule_node_parent(node
);
4039 bounds
= ppcg_ht_compute_bounds(gen
->prog
->scop
, node
);
4040 node
= isl_schedule_node_child(node
, 0);
4042 ok
= ppcg_ht_bounds_is_valid(bounds
);
4044 node
= gpu_hybrid_tile(gen
, node
, bounds
, tile_size
);
4046 ppcg_ht_bounds_free(bounds
);
4049 if (ok
>= 0 && !ok
) {
4050 isl_schedule_node_free(node
);
4053 isl_schedule_node_free(orig
);
4055 return isl_schedule_node_free(node
);
4059 /* If "node" is the outermost permutable band that can be mapped to block and
4060 * thread identifiers in its branch (or the root of a subtree with
4061 * no such outer bands),
4062 * then mark the band as such, attaching a ppcg_kernel to the mark.
4064 * If hybrid tiling is allowed, then first try and apply it
4065 * to "node" and its parent.
4067 * If "node" is the root of a subtree without permutable bands,
4068 * then insert a zero-dimensional permutable band such that
4069 * we can assume that "node" always points to a band node.
4070 * This includes the case where "node" already points to a band node,
4071 * but one without any coincident dimension. In this case,
4072 * the extra node ensures that this original node does not get tiled.
4074 * Tile "node" using user specified tile sizes, after splitting the band
4075 * if the number of specified tile sizes is smaller than the dimension
4076 * of the band. Mark the point band of this tiling as the band that
4077 * needs to be mapped to threads and instruct the AST generator to unroll
4078 * the band if the "unroll_gpu_tile" option is set.
4079 * Create a kernel representing the domain instances that reach "node" and
4080 * insert a mark node pointing to the ppcg_kernel before the band node.
4082 static __isl_give isl_schedule_node
*mark_outer_permutable(
4083 __isl_take isl_schedule_node
*node
, void *user
)
4085 struct gpu_gen
*gen
= user
;
4091 isl_multi_val
*sizes
;
4093 outer
= is_outer_tilable(node
);
4095 return isl_schedule_node_free(node
);
4099 if (gen
->options
->hybrid
) {
4100 isl_schedule_node
*saved
= isl_schedule_node_copy(node
);
4101 node
= try_hybrid_tile(gen
, node
);
4102 isl_schedule_node_free(saved
);
4107 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
||
4108 !isl_schedule_node_band_member_get_coincident(node
, 0))
4109 node
= insert_empty_permutable_band(node
);
4111 tile_len
= isl_schedule_node_band_n_member(node
);
4112 tile_size
= read_tile_sizes(gen
, &tile_len
);
4114 return isl_schedule_node_free(node
);
4115 if (tile_len
< isl_schedule_node_band_n_member(node
))
4116 node
= isl_schedule_node_band_split(node
, tile_len
);
4117 sizes
= construct_band_tiles_sizes(node
, tile_size
);
4118 node
= tile_band(node
, isl_multi_val_copy(sizes
));
4119 node
= isl_schedule_node_child(node
, 0);
4120 if (gen
->options
->unroll_gpu_tile
)
4121 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
4122 id
= isl_id_alloc(gen
->ctx
, "thread", NULL
);
4123 node
= isl_schedule_node_insert_mark(node
, id
);
4124 node
= isl_schedule_node_parent(node
);
4126 scale
= gen
->options
->scale_tile_loops
;
4127 node
= gpu_create_kernel(gen
, node
, scale
, sizes
);
4128 isl_multi_val_free(sizes
);
4134 /* Given a set or sequence node, return the union the filters of either all
4135 * (if "only_initial" is not set) or the initial (if "only_initial" is set)
4136 * direct subtrees that do not contain any suitably permutable bands
4137 * (according to subtree_has_permutable_bands).
4139 static __isl_give isl_union_set
*get_non_parallel_subtree_filters(
4140 __isl_keep isl_schedule_node
*node
, int only_initial
)
4143 isl_union_set
*filter
;
4146 n
= isl_schedule_node_n_children(node
);
4150 node
= isl_schedule_node_copy(node
);
4151 node
= isl_schedule_node_child(node
, 0);
4152 filter
= isl_schedule_node_filter_get_filter(node
);
4153 node
= isl_schedule_node_parent(node
);
4154 space
= isl_union_set_get_space(filter
);
4155 isl_union_set_free(filter
);
4156 filter
= isl_union_set_empty(space
);
4158 for (i
= 0; i
< n
; ++i
) {
4161 node
= isl_schedule_node_child(node
, i
);
4162 parallelism
= subtree_has_permutable_bands(node
);
4163 if (parallelism
< 0) {
4164 filter
= isl_union_set_free(filter
);
4165 } else if (!parallelism
) {
4166 isl_union_set
*filter_i
;
4167 filter_i
= isl_schedule_node_filter_get_filter(node
);
4168 filter
= isl_union_set_union(filter
, filter_i
);
4169 } else if (only_initial
)
4171 node
= isl_schedule_node_parent(node
);
4174 isl_schedule_node_free(node
);
4179 /* Given a set or sequence node, return the union of the filters of
4180 * the direct subtrees that do not contain any suitably permutable bands
4181 * (according to subtree_has_permutable_bands).
4183 static __isl_give isl_union_set
*get_all_non_parallel_subtree_filters(
4184 __isl_keep isl_schedule_node
*node
)
4186 return get_non_parallel_subtree_filters(node
, 0);
4189 /* Given a set or sequence node, return the union of the filters of
4190 * the initial direct subtrees that do not contain any suitably permutable
4191 * bands (according to subtree_has_permutable_bands).
4193 static __isl_give isl_union_set
*get_initial_non_parallel_subtree_filters(
4194 __isl_keep isl_schedule_node
*node
)
4196 return get_non_parallel_subtree_filters(node
, 1);
4199 /* Mark all variables that are accessed by the statement instances in "domain"
4200 * and that are local to "prog" as requiring a declaration in the host code.
4201 * The statement instances in "domain" correspond to (a subset of)
4202 * the active instances at "node".
4203 * "node" is not modified by this function, except that NULL is returned
4206 static __isl_give isl_schedule_node
*declare_accessed_local_variables(
4207 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
,
4208 __isl_keep isl_union_set
*domain
)
4210 isl_union_pw_multi_aff
*contraction
;
4211 isl_union_set
*arrays
;
4214 if (!ppcg_scop_any_hidden_declarations(prog
->scop
))
4216 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4217 domain
= isl_union_set_copy(domain
);
4218 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, contraction
);
4219 arrays
= accessed_by_domain(domain
, prog
);
4221 for (i
= 0; i
< prog
->n_array
; ++i
) {
4226 if (!prog
->array
[i
].local
)
4228 space
= isl_set_get_space(prog
->array
[i
].extent
);
4229 set
= isl_union_set_extract_set(arrays
, space
);
4230 empty
= isl_set_plain_is_empty(set
);
4235 prog
->array
[i
].declare_local
= 1;
4238 isl_union_set_free(arrays
);
4241 isl_union_set_free(arrays
);
4242 return isl_schedule_node_free(node
);
4245 /* If "node" points to a set node, then separate its children
4246 * into subtrees that have suitably permutable bands and
4247 * those that do not.
4248 * Adjust the schedule tree in order to execute the second group
4249 * after the first group and return a pointer to the first group,
4250 * assuming there are any such subtrees.
4251 * If "node" points to a sequence node, then separate the initial
4252 * children that do not have suitably permutable bands and
4253 * return a pointer to the subsequence of children that do have such bands,
4254 * assuming there are any such subtrees.
4256 * In both cases, mark all local variables in "prog" that are accessed by
4257 * the group without permutable bands as requiring a declaration on the host.
4259 static __isl_give isl_schedule_node
*isolate_permutable_subtrees(
4260 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
)
4262 isl_union_set
*filter
;
4263 enum isl_schedule_node_type type
;
4267 type
= isl_schedule_node_get_type(node
);
4268 if (type
== isl_schedule_node_set
) {
4269 filter
= get_all_non_parallel_subtree_filters(node
);
4270 node
= declare_accessed_local_variables(node
, prog
, filter
);
4271 node
= isl_schedule_node_order_after(node
, filter
);
4272 } else if (type
== isl_schedule_node_sequence
) {
4273 filter
= get_initial_non_parallel_subtree_filters(node
);
4274 node
= declare_accessed_local_variables(node
, prog
, filter
);
4275 node
= isl_schedule_node_order_before(node
, filter
);
4281 /* Replace any reference to an array element in the range of "copy"
4282 * by a reference to all array elements (defined by the extent of the array).
4284 static __isl_give isl_union_map
*approximate_copy_out(
4285 __isl_take isl_union_map
*copy
, struct gpu_prog
*prog
)
4290 res
= isl_union_map_empty(isl_union_map_get_space(copy
));
4292 for (i
= 0; i
< prog
->n_array
; ++i
) {
4295 isl_union_map
*copy_i
;
4296 isl_union_set
*extent
, *domain
;
4298 space
= isl_space_copy(prog
->array
[i
].space
);
4299 extent
= isl_union_set_from_set(isl_set_universe(space
));
4300 copy_i
= isl_union_map_copy(copy
);
4301 copy_i
= isl_union_map_intersect_range(copy_i
, extent
);
4302 set
= isl_set_copy(prog
->array
[i
].extent
);
4303 extent
= isl_union_set_from_set(set
);
4304 domain
= isl_union_map_domain(copy_i
);
4305 copy_i
= isl_union_map_from_domain_and_range(domain
, extent
);
4306 res
= isl_union_map_union(res
, copy_i
);
4309 isl_union_map_free(copy
);
4314 /* Insert "kernel" marks that point to a ppcg_kernel structure
4315 * in front of all outermost tilable band that (by construction)
4316 * have at least one parallel loop.
4318 static __isl_give isl_schedule_node
*mark_kernels(struct gpu_gen
*gen
,
4319 __isl_take isl_schedule_node
*node
)
4321 return isl_schedule_node_map_descendant_bottom_up(node
,
4322 &mark_outer_permutable
, gen
);
4325 /* Construct schedule constraints from the dependences in prog->scop and
4326 * the array order dependences in prog->array_order.
4328 * If live range reordering is allowed, then we need to make sure
4329 * that live ranges on arrays are not run in parallel since doing
4330 * so would require array expansion. We therefore add the array
4331 * order dependences to the coincidence dependences. Non-zero array
4332 * order dependences will then prevent a schedule dimension from being
4333 * considered parallel.
4334 * Live ranges derived from scalars are allowed to be run in parallel
4335 * since we force the scalars to be mapped to private memory in
4336 * check_scalar_live_ranges.
4337 * If live range reordering is allowed, then the false dependences
4338 * are not added to the validity constraints as that would prevent
4339 * reordering. Instead, the external false dependences that enforce that reads
4340 * from potentially live-in data precede any later write and
4341 * that writes of potentially live-out data follow any other earlier write
4342 * are added to the validity and the coincidence constraints.
4343 * The false dependences are still added to the proximity constraints
4344 * for consistency with the case where live range reordering is not allowed.
4345 * The coincidence constraints then consist of flow dependences,
4346 * external false dependences and array order dependences.
4347 * The independences can be filtered out from the first two sets.
4348 * They have already been filtered out from the array order dependences
4349 * on a per array basis in collect_order_dependences.
4350 * There is no need for a per array handling of the other two sets
4351 * as there should be no flow or external false dependence on local
4352 * variables that can be filtered out.
4354 static __isl_give isl_schedule_constraints
*construct_schedule_constraints(
4355 struct gpu_prog
*prog
)
4357 isl_union_set
*domain
;
4358 isl_union_map
*dep_raw
, *dep
;
4359 isl_union_map
*validity
, *proximity
, *coincidence
;
4360 isl_schedule_constraints
*sc
;
4362 domain
= isl_union_set_copy(prog
->scop
->domain
);
4363 sc
= isl_schedule_constraints_on_domain(domain
);
4364 sc
= isl_schedule_constraints_set_context(sc
,
4365 isl_set_copy(prog
->scop
->context
));
4366 if (prog
->scop
->options
->live_range_reordering
) {
4367 sc
= isl_schedule_constraints_set_conditional_validity(sc
,
4368 isl_union_map_copy(prog
->scop
->tagged_dep_flow
),
4369 isl_union_map_copy(prog
->scop
->tagged_dep_order
));
4370 proximity
= isl_union_map_copy(prog
->scop
->dep_flow
);
4371 validity
= isl_union_map_copy(proximity
);
4372 validity
= isl_union_map_union(validity
,
4373 isl_union_map_copy(prog
->scop
->dep_forced
));
4374 proximity
= isl_union_map_union(proximity
,
4375 isl_union_map_copy(prog
->scop
->dep_false
));
4376 coincidence
= isl_union_map_copy(validity
);
4377 coincidence
= isl_union_map_subtract(coincidence
,
4378 isl_union_map_copy(prog
->scop
->independence
));
4379 coincidence
= isl_union_map_union(coincidence
,
4380 isl_union_map_copy(prog
->array_order
));
4382 dep_raw
= isl_union_map_copy(prog
->scop
->dep_flow
);
4383 dep
= isl_union_map_copy(prog
->scop
->dep_false
);
4384 dep
= isl_union_map_union(dep
, dep_raw
);
4385 dep
= isl_union_map_coalesce(dep
);
4386 proximity
= isl_union_map_copy(dep
);
4387 coincidence
= isl_union_map_copy(dep
);
4390 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4391 sc
= isl_schedule_constraints_set_coincidence(sc
, coincidence
);
4392 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4397 /* Compute an appropriate schedule based on the accesses in
4398 * gen->read and gen->write.
4400 * We derive schedule constraints from the dependences in gen->prog->scop
4401 * and then use isl to compute a schedule that has a parallel loop
4402 * in each tilable band.
4403 * During the schedule construction, some statement instances
4404 * may be grouped first based on the input schedule.
4406 static __isl_give isl_schedule
*compute_schedule(struct gpu_gen
*gen
)
4408 isl_schedule_constraints
*sc
;
4409 isl_schedule
*schedule
;
4411 sc
= construct_schedule_constraints(gen
->prog
);
4412 schedule
= gen
->prog
->scop
->schedule
;
4413 schedule
= ppcg_compute_schedule(sc
, schedule
, gen
->options
);
4418 /* If the band node "node" has exactly one member then mark it permutable.
4420 static __isl_give isl_schedule_node
*band_set_permutable(
4421 __isl_take isl_schedule_node
*node
,
4422 __isl_keep isl_schedule_constraints
*sc
)
4424 if (isl_schedule_node_band_n_member(node
) == 1)
4425 node
= isl_schedule_node_band_set_permutable(node
, 1);
4430 /* Return the coincidence constraints between pairs of instances
4431 * that are scheduled together by the ancestors of "node".
4432 * That is, select those coincidence constraints that relate
4433 * pairs of instances that have the same value for the prefix schedule.
4434 * If the schedule depth is zero, then the prefix schedule does not
4435 * contain any information, so we intersect domain and range
4436 * of the schedule constraints with the reaching domain elements instead.
4438 static __isl_give isl_union_map
*get_local_coincidence(
4439 __isl_keep isl_schedule_node
*node
,
4440 __isl_keep isl_schedule_constraints
*sc
)
4442 isl_union_map
*coincidence
;
4443 isl_multi_union_pw_aff
*prefix
;
4444 isl_union_pw_multi_aff
*contraction
;
4446 coincidence
= isl_schedule_constraints_get_coincidence(sc
);
4447 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4448 if (isl_schedule_node_get_schedule_depth(node
) == 0) {
4449 isl_union_set
*domain
;
4451 domain
= isl_schedule_node_get_domain(node
);
4452 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4454 coincidence
= isl_union_map_intersect_domain(coincidence
,
4455 isl_union_set_copy(domain
));
4456 coincidence
= isl_union_map_intersect_range(coincidence
,
4461 prefix
= isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
4462 prefix
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
4464 return isl_union_map_eq_at_multi_union_pw_aff(coincidence
, prefix
);
4467 /* For each member in the band node "node", determine whether
4468 * it is coincident with respect to the outer nodes and mark
4471 * That is, for each coincidence constraint between pairs
4472 * of instances that are scheduled together by the outer nodes,
4473 * check that domain and range are assigned the same value
4474 * by the band member. This test is performed by checking
4475 * that imposing the same value for the band member does not
4476 * remove any elements from the set of coincidence constraints.
4478 static __isl_give isl_schedule_node
*band_set_coincident(
4479 __isl_take isl_schedule_node
*node
,
4480 __isl_keep isl_schedule_constraints
*sc
)
4482 isl_union_map
*coincidence
;
4483 isl_union_pw_multi_aff
*contraction
;
4484 isl_multi_union_pw_aff
*partial
;
4487 coincidence
= get_local_coincidence(node
, sc
);
4489 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4490 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4491 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4493 n
= isl_schedule_node_band_n_member(node
);
4494 for (i
= 0; i
< n
; ++i
) {
4495 isl_union_map
*coincidence_i
;
4496 isl_union_pw_aff
*upa
;
4497 isl_multi_union_pw_aff
*partial_i
;
4500 upa
= isl_multi_union_pw_aff_get_union_pw_aff(partial
, i
);
4501 partial_i
= isl_multi_union_pw_aff_from_union_pw_aff(upa
);
4502 coincidence_i
= isl_union_map_copy(coincidence
);
4503 coincidence_i
= isl_union_map_eq_at_multi_union_pw_aff(
4504 coincidence_i
, partial_i
);
4505 subset
= isl_union_map_is_subset(coincidence
, coincidence_i
);
4506 isl_union_map_free(coincidence_i
);
4510 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
4514 node
= isl_schedule_node_free(node
);
4515 isl_multi_union_pw_aff_free(partial
);
4516 isl_union_map_free(coincidence
);
4521 /* If "node" is a band, then set its properties.
4523 * In particular, if the band has exactly one member, then mark it permutable.
4524 * Mark the band members coincident based on the coincidence constraints
4527 static __isl_give isl_schedule_node
*set_band_properties(
4528 __isl_take isl_schedule_node
*node
, void *user
)
4530 isl_schedule_constraints
*sc
= user
;
4532 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
4534 if (isl_schedule_node_band_n_member(node
) == 0)
4537 node
= band_set_permutable(node
, sc
);
4538 node
= band_set_coincident(node
, sc
);
4543 /* Return the original schedule with all bands marked permutable and
4544 * all band members marked coincident based on the coincidence constraints.
4545 * The bands are explicitly marked permutable so that they will be considered
4546 * by mark_outer_permutable.
4548 static __isl_give isl_schedule
*determine_properties_original_schedule(
4549 struct gpu_gen
*gen
)
4551 isl_schedule
*schedule
;
4552 isl_schedule_constraints
*sc
;
4554 schedule
= isl_schedule_copy(gen
->prog
->scop
->schedule
);
4555 sc
= construct_schedule_constraints(gen
->prog
);
4556 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
4557 &set_band_properties
, sc
);
4558 isl_schedule_constraints_free(sc
);
4563 /* Compute a schedule or determine the properties of the original schedule
4564 * depending on the value of the "reschedule" option.
4566 static __isl_give isl_schedule
*compute_or_set_properties(void *user
)
4568 struct gpu_gen
*gen
= user
;
4570 if (gen
->options
->reschedule
)
4571 return compute_schedule(gen
);
4573 return determine_properties_original_schedule(gen
);
4576 /* Obtain a schedule for the scop, by reading it from
4577 * a file, by computing one or by determining the properties
4578 * of the original schedule.
4580 static __isl_give isl_schedule
*get_schedule(struct gpu_gen
*gen
)
4582 return ppcg_get_schedule(gen
->ctx
, gen
->options
,
4583 &compute_or_set_properties
, gen
);
4586 /* Construct the string "<a>_<b>".
4588 static char *concat(isl_ctx
*ctx
, const char *a
, const char *b
)
4593 p
= isl_printer_to_str(ctx
);
4594 p
= isl_printer_print_str(p
, a
);
4595 p
= isl_printer_print_str(p
, "_");
4596 p
= isl_printer_print_str(p
, b
);
4597 s
= isl_printer_get_str(p
);
4598 isl_printer_free(p
);
4603 /* For each array in "prog" of which an element appears in "accessed" and
4604 * that is not a read only scalar, create a zero-dimensional universe set
4605 * of which the tuple id has name "<prefix>_<name of array>" and a user
4606 * pointer pointing to the array (gpu_array_info).
4608 * If the array is local to "prog", then make sure it will be declared
4611 * Return the list of these universe sets.
4613 static __isl_give isl_union_set_list
*create_copy_filters(struct gpu_prog
*prog
,
4614 const char *prefix
, __isl_take isl_union_set
*accessed
)
4618 isl_union_set_list
*filters
;
4621 filters
= isl_union_set_list_alloc(ctx
, 0);
4622 for (i
= 0; i
< prog
->n_array
; ++i
) {
4623 struct gpu_array_info
*array
= &prog
->array
[i
];
4625 isl_set
*accessed_i
;
4629 isl_union_set
*uset
;
4631 if (gpu_array_is_read_only_scalar(array
))
4634 space
= isl_space_copy(array
->space
);
4635 accessed_i
= isl_union_set_extract_set(accessed
, space
);
4636 empty
= isl_set_plain_is_empty(accessed_i
);
4637 isl_set_free(accessed_i
);
4639 filters
= isl_union_set_list_free(filters
);
4647 array
->declare_local
= 1;
4649 name
= concat(ctx
, prefix
, array
->name
);
4650 id
= name
? isl_id_alloc(ctx
, name
, array
) : NULL
;
4652 space
= isl_space_set_alloc(ctx
, 0, 0);
4653 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4654 uset
= isl_union_set_from_set(isl_set_universe(space
));
4656 filters
= isl_union_set_list_add(filters
, uset
);
4658 isl_union_set_free(accessed
);
4663 /* Make sure that code for the statements in "filters" that
4664 * copy arrays to or from the device is only generated when
4665 * the size of the corresponding array is positive.
4666 * That is, add a set node underneath "graft" with "filters" as children
4667 * and for each child add a guard that the selects the parameter
4668 * values for which the corresponding array has a positive size.
4669 * The array is available in the user pointer of the statement identifier.
4670 * "depth" is the schedule depth of the position where "graft"
4673 static __isl_give isl_schedule_node
*insert_positive_size_guards(
4674 __isl_take isl_schedule_node
*graft
,
4675 __isl_take isl_union_set_list
*filters
, int depth
)
4679 graft
= isl_schedule_node_child(graft
, 0);
4680 graft
= isl_schedule_node_insert_set(graft
, filters
);
4681 n
= isl_schedule_node_n_children(graft
);
4682 for (i
= 0; i
< n
; ++i
) {
4683 isl_union_set
*filter
;
4684 isl_set
*domain
, *guard
;
4686 struct gpu_array_info
*array
;
4688 graft
= isl_schedule_node_child(graft
, i
);
4689 filter
= isl_schedule_node_filter_get_filter(graft
);
4690 domain
= isl_set_from_union_set(filter
);
4691 id
= isl_set_get_tuple_id(domain
);
4692 array
= isl_id_get_user(id
);
4694 isl_set_free(domain
);
4695 guard
= gpu_array_positive_size_guard(array
);
4696 guard
= isl_set_from_params(guard
);
4697 guard
= isl_set_add_dims(guard
, isl_dim_set
, depth
);
4698 graft
= isl_schedule_node_child(graft
, 0);
4699 graft
= isl_schedule_node_insert_guard(graft
, guard
);
4700 graft
= isl_schedule_node_parent(graft
);
4701 graft
= isl_schedule_node_parent(graft
);
4703 graft
= isl_schedule_node_parent(graft
);
4708 /* Create a graft for copying arrays to or from the device,
4709 * whenever the size of the array is strictly positive.
4710 * Each statement is called "<prefix>_<name of array>" and
4711 * the identifier has a user pointer pointing to the array.
4712 * The graft will be added at the position specified by "node".
4713 * "copy" contains the array elements that need to be copied.
4714 * Only arrays of which some elements need to be copied
4715 * will have a corresponding statement in the graph.
4716 * Note though that each such statement will copy the entire array.
4718 static __isl_give isl_schedule_node
*create_copy_device(struct gpu_prog
*prog
,
4719 __isl_keep isl_schedule_node
*node
, const char *prefix
,
4720 __isl_take isl_union_set
*copy
)
4725 isl_union_set
*all
, *domain
;
4726 isl_union_set_list
*filters
;
4727 isl_union_map
*extension
;
4728 isl_schedule_node
*graft
;
4731 depth
= isl_schedule_node_get_schedule_depth(node
);
4732 filters
= create_copy_filters(prog
, prefix
, copy
);
4733 all
= isl_union_set_list_union(isl_union_set_list_copy(filters
));
4735 space
= depth
< 0 ? NULL
: isl_space_set_alloc(ctx
, 0, depth
);
4736 domain
= isl_union_set_from_set(isl_set_universe(space
));
4737 extension
= isl_union_map_from_domain_and_range(domain
, all
);
4738 graft
= isl_schedule_node_from_extension(extension
);
4741 return isl_schedule_node_free(graft
);
4742 if (isl_union_set_list_n_union_set(filters
) == 0) {
4743 isl_union_set_list_free(filters
);
4747 return insert_positive_size_guards(graft
, filters
, depth
);
4750 /* Return (the universe spaces of) the arrays that are declared
4751 * inside the scop corresponding to "prog" and for which all
4752 * potential writes inside the scop form a subset of "domain".
4754 static __isl_give isl_union_set
*extract_local_accesses(struct gpu_prog
*prog
,
4755 __isl_keep isl_union_set
*domain
)
4758 isl_union_set
*local
;
4760 local
= isl_union_set_empty(isl_union_set_get_space(domain
));
4762 for (i
= 0; i
< prog
->n_array
; ++i
) {
4764 isl_union_map
*to_outer
;
4765 isl_union_map
*may_write
;
4766 isl_union_set
*write_domain
;
4767 isl_union_set
*fields
;
4770 if (!prog
->array
[i
].local
)
4773 set
= isl_set_universe(isl_space_copy(prog
->array
[i
].space
));
4774 to_outer
= isl_union_map_copy(prog
->to_outer
);
4775 to_outer
= isl_union_map_intersect_range(to_outer
,
4776 isl_union_set_from_set(isl_set_copy(set
)));
4777 fields
= isl_union_map_domain(to_outer
);
4778 may_write
= isl_union_map_copy(prog
->may_write
);
4779 may_write
= isl_union_map_intersect_range(may_write
, fields
);
4780 write_domain
= isl_union_map_domain(may_write
);
4781 subset
= isl_union_set_is_subset(write_domain
, domain
);
4782 isl_union_set_free(write_domain
);
4786 return isl_union_set_free(local
);
4787 } else if (subset
) {
4788 local
= isl_union_set_add_set(local
, set
);
4797 /* Internal data structure for node_may_persist.
4799 * "tagger" maps tagged iteration domains to the corresponding untagged
4802 * "may_persist_flow" is the set of all tagged dataflow dependences
4803 * with those dependences removed that either precede or follow
4804 * the kernel launch in a sequence.
4805 * "inner_band_flow" is the set of all tagged dataflow dependences
4806 * that are local to a given iteration of the outer band nodes
4807 * with respect to the current node.
4808 * "local_flow" is equal to "inner_band_flow", except that the domain
4809 * and the range have been intersected with intermediate filters
4810 * on children of sets or sequences.
4812 struct ppcg_may_persist_data
{
4813 isl_union_pw_multi_aff
*tagger
;
4815 isl_union_map
*local_flow
;
4816 isl_union_map
*inner_band_flow
;
4817 isl_union_map
*may_persist_flow
;
4820 /* Update the information in "data" based on the band ancestor "node".
4822 * In particular, we restrict the dependences in data->local_flow
4823 * to those dependence where the source and the sink occur in
4824 * the same iteration of the given band node.
4825 * We also update data->inner_band_flow to the new value of
4828 static int update_may_persist_at_band(__isl_keep isl_schedule_node
*node
,
4829 struct ppcg_may_persist_data
*data
)
4831 isl_multi_union_pw_aff
*partial
;
4832 isl_union_pw_multi_aff
*contraction
;
4833 isl_union_map
*flow
;
4835 if (isl_schedule_node_band_n_member(node
) == 0)
4838 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4839 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4840 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4842 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4843 isl_union_pw_multi_aff_copy(data
->tagger
));
4845 flow
= data
->local_flow
;
4846 flow
= isl_union_map_eq_at_multi_union_pw_aff(flow
, partial
);
4847 data
->local_flow
= flow
;
4849 isl_union_map_free(data
->inner_band_flow
);
4850 data
->inner_band_flow
= isl_union_map_copy(data
->local_flow
);
4855 /* Given a set of local reaching domain elements "domain",
4856 * expand them to the corresponding leaf domain elements using "contraction"
4857 * and insert the array references tags using data->tagger.
4859 static __isl_give isl_union_set
*expand_and_tag(
4860 __isl_take isl_union_set
*domain
,
4861 __isl_take isl_union_pw_multi_aff
*contraction
,
4862 struct ppcg_may_persist_data
*data
)
4864 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4866 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4867 isl_union_pw_multi_aff_copy(data
->tagger
));
4871 /* Given a filter node that is the child of a set or sequence node,
4872 * restrict data->local_flow to refer only to those elements
4873 * in the filter of the node.
4874 * "contraction" maps the leaf domain elements of the schedule tree
4875 * to the corresponding domain elements at (the parent of) "node".
4877 static int filter_flow(__isl_keep isl_schedule_node
*node
,
4878 struct ppcg_may_persist_data
*data
,
4879 __isl_take isl_union_pw_multi_aff
*contraction
)
4881 isl_union_set
*filter
;
4882 isl_union_map
*flow
;
4884 flow
= data
->local_flow
;
4885 filter
= isl_schedule_node_filter_get_filter(node
);
4886 filter
= expand_and_tag(filter
, contraction
, data
);
4887 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(filter
));
4888 flow
= isl_union_map_intersect_range(flow
, filter
);
4889 data
->local_flow
= flow
;
4894 /* Given a filter node "node", collect the filters on all preceding siblings
4895 * (which are also filter nodes), add them to "filters" and return the result.
4897 static __isl_give isl_union_set
*add_previous_filters(
4898 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4900 isl_schedule_node
*sibling
;
4902 sibling
= isl_schedule_node_copy(node
);
4903 while (sibling
&& isl_schedule_node_has_previous_sibling(sibling
)) {
4904 isl_union_set
*filter
;
4906 sibling
= isl_schedule_node_previous_sibling(sibling
);
4907 filter
= isl_schedule_node_filter_get_filter(sibling
);
4908 filters
= isl_union_set_union(filters
, filter
);
4910 isl_schedule_node_free(sibling
);
4912 return isl_union_set_free(filters
);
4917 /* Given a filter node "node", collect the filters on all following siblings
4918 * (which are also filter nodes), add them to "filters" and return the result.
4920 static __isl_give isl_union_set
*add_next_filters(
4921 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4923 isl_schedule_node
*sibling
;
4925 sibling
= isl_schedule_node_copy(node
);
4926 while (sibling
&& isl_schedule_node_has_next_sibling(sibling
)) {
4927 isl_union_set
*filter
;
4929 sibling
= isl_schedule_node_next_sibling(sibling
);
4930 filter
= isl_schedule_node_filter_get_filter(sibling
);
4931 filters
= isl_union_set_union(filters
, filter
);
4933 isl_schedule_node_free(sibling
);
4935 return isl_union_set_free(filters
);
4940 /* Remove those flow dependences from data->may_persist_flow
4941 * that flow between elements of "domain" within the same iteration
4942 * of all outer band nodes.
4943 * "contraction" maps the leaf domain elements of the schedule tree
4944 * to the corresponding elements "domain".
4946 static void remove_external_flow(struct ppcg_may_persist_data
*data
,
4947 __isl_take isl_union_set
*domain
,
4948 __isl_keep isl_union_pw_multi_aff
*contraction
)
4950 isl_union_map
*flow
;
4952 contraction
= isl_union_pw_multi_aff_copy(contraction
);
4953 domain
= expand_and_tag(domain
, contraction
, data
);
4954 flow
= isl_union_map_copy(data
->local_flow
);
4955 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(domain
));
4956 flow
= isl_union_map_intersect_range(flow
, domain
);
4958 data
->may_persist_flow
= isl_union_map_subtract(data
->may_persist_flow
,
4962 /* Update the information in "data" based on the filter ancestor "node".
4963 * We only need to modify anything if the filter is the child
4964 * of a set or sequence node.
4966 * In the case of a sequence, we remove the dependences between
4967 * statement instances that are both executed either before or
4968 * after the subtree that will be mapped to a kernel, within
4969 * the same iteration of outer bands.
4971 * In both cases, we restrict data->local_flow to the current child.
4973 static int update_may_persist_at_filter(__isl_keep isl_schedule_node
*node
,
4974 struct ppcg_may_persist_data
*data
)
4976 enum isl_schedule_node_type type
;
4977 isl_schedule_node
*parent
;
4979 isl_union_pw_multi_aff
*contraction
;
4980 isl_union_set
*before
, *after
, *filter
;
4982 type
= isl_schedule_node_get_parent_type(node
);
4983 if (type
!= isl_schedule_node_sequence
&& type
!= isl_schedule_node_set
)
4986 parent
= isl_schedule_node_copy(node
);
4987 parent
= isl_schedule_node_parent(parent
);
4988 contraction
= isl_schedule_node_get_subtree_contraction(parent
);
4989 isl_schedule_node_free(parent
);
4991 if (type
== isl_schedule_node_set
)
4992 return filter_flow(node
, data
, contraction
);
4994 filter
= isl_schedule_node_filter_get_filter(node
);
4995 space
= isl_union_set_get_space(filter
);
4996 isl_union_set_free(filter
);
4997 before
= isl_union_set_empty(space
);
4998 after
= isl_union_set_copy(before
);
4999 before
= add_previous_filters(before
, node
);
5000 after
= add_next_filters(after
, node
);
5002 remove_external_flow(data
, before
, contraction
);
5003 remove_external_flow(data
, after
, contraction
);
5005 return filter_flow(node
, data
, contraction
);
5008 /* Update the information in "data" based on the ancestor "node".
5010 static isl_stat
update_may_persist_at(__isl_keep isl_schedule_node
*node
,
5013 struct ppcg_may_persist_data
*data
= user
;
5015 switch (isl_schedule_node_get_type(node
)) {
5016 case isl_schedule_node_error
:
5017 return isl_stat_error
;
5018 case isl_schedule_node_context
:
5019 case isl_schedule_node_domain
:
5020 case isl_schedule_node_expansion
:
5021 case isl_schedule_node_extension
:
5022 case isl_schedule_node_guard
:
5023 case isl_schedule_node_leaf
:
5024 case isl_schedule_node_mark
:
5025 case isl_schedule_node_sequence
:
5026 case isl_schedule_node_set
:
5028 case isl_schedule_node_band
:
5029 if (update_may_persist_at_band(node
, data
) < 0)
5030 return isl_stat_error
;
5032 case isl_schedule_node_filter
:
5033 if (update_may_persist_at_filter(node
, data
) < 0)
5034 return isl_stat_error
;
5041 /* Determine the set of array elements that may need to be perserved
5042 * by a kernel constructed from the subtree at "node".
5043 * This includes the set of array elements that may need to be preserved
5044 * by the entire scop (prog->may_persist) and the elements for which
5045 * there is a potential flow dependence that may cross a kernel launch.
5047 * To determine the second set, we start from all flow dependences.
5048 * From this set of dependences, we remove those that cannot possibly
5049 * require data to be preserved by a kernel launch.
5050 * In particular, we consider the following sets of dependences.
5051 * - dependences of which the write occurs inside the kernel.
5052 * If the data is needed outside the kernel, then it will
5053 * be copied out immediately after the kernel launch, so there
5054 * is no need for any special care.
5055 * - dependences of which the read occurs inside the kernel and the
5056 * corresponding write occurs inside the same iteration of the
5057 * outer band nodes. This means that the data is needed in
5058 * the first kernel launch after the write, which is already
5059 * taken care of by the standard copy-in. That is, the data
5060 * do not need to be preserved by any intermediate call to
5062 * - dependences of which the write and the read either both occur
5063 * before the kernel launch or both occur after the kernel launch,
5064 * within the same iteration of the outer band nodes with respect
5065 * to the sequence that determines the ordering of the dependence
5066 * and the kernel launch. Such flow dependences cannot cross
5067 * any kernel launch.
5069 * For the remaining (tagged) dependences, we take the domain
5070 * (i.e., the tagged writes) and apply the tagged access relation
5071 * to obtain the accessed data elements.
5072 * These are then combined with the elements that may need to be
5073 * preserved by the entire scop.
5075 static __isl_give isl_union_set
*node_may_persist(
5076 __isl_keep isl_schedule_node
*node
, struct gpu_prog
*prog
)
5078 struct ppcg_may_persist_data data
;
5079 isl_union_pw_multi_aff
*contraction
;
5080 isl_union_set
*domain
;
5081 isl_union_set
*persist
;
5082 isl_union_map
*flow
, *local_flow
;
5084 data
.tagger
= prog
->scop
->tagger
;
5086 flow
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
5087 data
.local_flow
= isl_union_map_copy(flow
);
5088 data
.inner_band_flow
= isl_union_map_copy(flow
);
5089 data
.may_persist_flow
= flow
;
5090 if (isl_schedule_node_foreach_ancestor_top_down(node
,
5091 &update_may_persist_at
, &data
) < 0)
5092 data
.may_persist_flow
=
5093 isl_union_map_free(data
.may_persist_flow
);
5094 flow
= data
.may_persist_flow
;
5095 isl_union_map_free(data
.local_flow
);
5097 domain
= isl_schedule_node_get_domain(node
);
5098 contraction
= isl_schedule_node_get_subtree_contraction(node
);
5099 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5101 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5102 isl_union_pw_multi_aff_copy(data
.tagger
));
5103 flow
= isl_union_map_subtract_domain(flow
, isl_union_set_copy(domain
));
5104 local_flow
= data
.inner_band_flow
;
5105 local_flow
= isl_union_map_intersect_range(local_flow
, domain
);
5106 flow
= isl_union_map_subtract(flow
, local_flow
);
5108 persist
= isl_union_map_domain(flow
);
5109 persist
= isl_union_set_apply(persist
,
5110 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5111 persist
= isl_union_set_union(persist
,
5112 isl_union_set_copy(prog
->may_persist
));
5117 /* Add nodes for copying outer arrays in and out of the device
5118 * before and after the subtree "node", which contains one or more kernels.
5119 * "domain" contains the original statement instances, i.e.,
5120 * those that correspond to the domains of the access relations in "prog".
5121 * In particular, the domain has not been contracted in any way.
5122 * "prefix" contains the prefix schedule at that point, in terms
5123 * of the same original statement instances.
5125 * We first compute the sets of outer array elements that need
5126 * to be copied in and out and then graft in the nodes for
5127 * performing this copying.
5129 * In particular, for each array that is possibly written anywhere in
5130 * the subtree "node" and that may be used after "node"
5131 * or that may be visible outside the corresponding scop,
5132 * we copy out its entire extent.
5134 * Any array elements that is read without first being written inside
5135 * the subtree "node" needs to be copied in.
5136 * Furthermore, if there are any array elements that
5137 * are copied out, but that may not be written inside "node, then
5138 * they also need to be copied in to ensure that the value after execution
5139 * is the same as the value before execution, at least for those array
5140 * elements that may have their values preserved by the scop or that
5141 * may be written before "node" and read after "node".
5142 * In case the array elements are structures, we need to take into
5143 * account that all members of the structures need to be written
5144 * by "node" before we can avoid copying the data structure in.
5146 * Note that the may_write relation is intersected with the domain,
5147 * which has been intersected with the context.
5148 * This helps in those cases where the arrays are declared with a fixed size,
5149 * while the accesses are parametric and the context assigns a fixed value
5150 * to the parameters.
5152 * If an element from a local array is read without first being written,
5153 * then there is no point in copying it in since it cannot have been
5154 * written prior to the scop. Warn about the uninitialized read instead.
5156 static __isl_give isl_schedule_node
*add_to_from_device(
5157 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
5158 __isl_take isl_union_map
*prefix
, struct gpu_prog
*prog
)
5160 isl_union_set
*local
;
5161 isl_union_set
*may_persist
;
5162 isl_union_map
*may_write
, *must_write
, *copy_out
, *not_written
;
5163 isl_union_map
*read
, *copy_in
;
5164 isl_union_map
*tagged
;
5165 isl_union_map
*local_uninitialized
;
5166 isl_schedule_node
*graft
;
5168 tagged
= isl_union_map_copy(prog
->scop
->tagged_reads
);
5169 tagged
= isl_union_map_union(tagged
,
5170 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5172 may_write
= isl_union_map_copy(prog
->may_write
);
5173 may_write
= isl_union_map_intersect_domain(may_write
,
5174 isl_union_set_copy(domain
));
5175 may_write
= remove_local_accesses(prog
,
5176 isl_union_map_copy(tagged
), may_write
,
5177 isl_union_map_copy(prefix
), 0);
5178 may_write
= isl_union_map_apply_range(may_write
,
5179 isl_union_map_copy(prog
->to_outer
));
5180 may_write
= isl_union_map_apply_domain(may_write
,
5181 isl_union_map_copy(prefix
));
5182 may_write
= approximate_copy_out(may_write
, prog
);
5183 copy_out
= isl_union_map_copy(may_write
);
5184 may_write
= isl_union_map_apply_range(may_write
,
5185 isl_union_map_copy(prog
->to_inner
));
5186 must_write
= isl_union_map_copy(prog
->must_write
);
5187 must_write
= isl_union_map_apply_domain(must_write
,
5188 isl_union_map_copy(prefix
));
5189 may_persist
= node_may_persist(node
, prog
);
5190 may_write
= isl_union_map_intersect_range(may_write
, may_persist
);
5191 not_written
= isl_union_map_subtract(may_write
, must_write
);
5193 local
= extract_local_accesses(prog
, domain
);
5194 read
= isl_union_map_copy(prog
->read
);
5195 read
= isl_union_map_intersect_domain(read
, domain
);
5196 read
= remove_local_accesses(prog
, tagged
, read
,
5197 isl_union_map_copy(prefix
), 1);
5198 local
= isl_union_set_apply(local
, isl_union_map_copy(prog
->to_inner
));
5199 local_uninitialized
= isl_union_map_copy(prog
->scop
->live_in
);
5200 local_uninitialized
= isl_union_map_intersect_range(local_uninitialized
,
5202 local_uninitialized
= isl_union_map_intersect(local_uninitialized
,
5203 isl_union_map_copy(read
));
5204 if (!isl_union_map_is_empty(local_uninitialized
)) {
5206 "possibly uninitialized reads (not copied in):\n");
5207 isl_union_map_dump(local_uninitialized
);
5209 read
= isl_union_map_subtract(read
, local_uninitialized
);
5210 read
= isl_union_map_apply_domain(read
, prefix
);
5211 copy_in
= isl_union_map_union(read
, not_written
);
5212 copy_in
= isl_union_map_apply_range(copy_in
,
5213 isl_union_map_copy(prog
->to_outer
));
5215 graft
= create_copy_device(prog
, node
, "to_device",
5216 isl_union_map_range(copy_in
));
5217 node
= isl_schedule_node_graft_before(node
, graft
);
5218 graft
= create_copy_device(prog
, node
, "from_device",
5219 isl_union_map_range(copy_out
));
5220 node
= isl_schedule_node_graft_after(node
, graft
);
5225 /* Add nodes for initializing ("init_device") and clearing ("clear_device")
5226 * the device before and after "node".
5228 static __isl_give isl_schedule_node
*add_init_clear_device(
5229 __isl_take isl_schedule_node
*node
)
5233 isl_union_set
*domain
;
5234 isl_schedule_node
*graft
;
5236 ctx
= isl_schedule_node_get_ctx(node
);
5238 space
= isl_space_set_alloc(ctx
, 0, 0);
5239 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "init_device");
5240 domain
= isl_union_set_from_set(isl_set_universe(space
));
5241 graft
= isl_schedule_node_from_domain(domain
);
5243 node
= isl_schedule_node_graft_before(node
, graft
);
5245 space
= isl_space_set_alloc(ctx
, 0, 0);
5246 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "clear_device");
5247 domain
= isl_union_set_from_set(isl_set_universe(space
));
5248 graft
= isl_schedule_node_from_domain(domain
);
5250 node
= isl_schedule_node_graft_after(node
, graft
);
5255 /* Update "schedule" for mapping to a GPU device.
5257 * In particular, insert a context node, create kernels for
5258 * each outermost tilable band and introduce nodes for copying arrays
5259 * in and out of the device and for initializing and clearing the device.
5260 * If the child of the initial root points to a set node,
5261 * then children of this node that do not contain any tilable bands
5262 * are separated from the other children and are not mapped to
5265 * The GPU code is generated in a context where at least one
5266 * statement instance is executed. The corresponding guard is inserted
5267 * around the entire schedule.
5269 static __isl_give isl_schedule
*map_to_device(struct gpu_gen
*gen
,
5270 __isl_take isl_schedule
*schedule
)
5272 isl_schedule_node
*node
;
5275 isl_union_set
*domain
;
5276 isl_union_map
*prefix
;
5277 isl_union_pw_multi_aff
*contraction
;
5278 struct gpu_prog
*prog
;
5280 context
= isl_set_copy(gen
->prog
->context
);
5281 context
= isl_set_from_params(context
);
5282 schedule
= isl_schedule_insert_context(schedule
, context
);
5285 guard
= isl_union_set_params(isl_union_set_copy(prog
->scop
->domain
));
5286 prog
->context
= isl_set_intersect(prog
->context
, isl_set_copy(guard
));
5287 guard
= isl_set_from_params(guard
);
5289 node
= isl_schedule_get_root(schedule
);
5290 isl_schedule_free(schedule
);
5291 node
= isl_schedule_node_child(node
, 0);
5292 node
= isl_schedule_node_child(node
, 0);
5293 node
= isolate_permutable_subtrees(node
, gen
->prog
);
5294 domain
= isl_schedule_node_get_domain(node
);
5295 contraction
= isl_schedule_node_get_subtree_contraction(node
);
5296 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5297 isl_union_pw_multi_aff_copy(contraction
));
5298 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
5299 prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(prefix
,
5301 node
= mark_kernels(gen
, node
);
5302 node
= add_to_from_device(node
, domain
, prefix
, gen
->prog
);
5303 node
= isl_schedule_node_root(node
);
5304 node
= isl_schedule_node_child(node
, 0);
5305 node
= isl_schedule_node_child(node
, 0);
5306 node
= isl_schedule_node_insert_guard(node
, guard
);
5307 node
= isl_schedule_node_child(node
, 0);
5308 node
= add_init_clear_device(node
);
5309 schedule
= isl_schedule_node_get_schedule(node
);
5310 isl_schedule_node_free(node
);
5315 /* Internal data structure for extract_access.
5316 * "next_access" points to the end of a linked list that is extended
5317 * by extract_access.
5318 * "single_expression" is set if the access expressions belong to
5319 * an expression statement (i.e., a statement without internal control).
5320 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5322 struct ppcg_extract_access_data
{
5323 struct gpu_stmt_access
**next_access
;
5324 int single_expression
;
5325 isl_union_map
*any_to_outer
;
5328 /* Given a tagged access relation to a single array "tagged", extract it
5329 * as a map, taking into account that the input may be empty.
5330 * If the access relation is empty, then it does not contain
5331 * any space information, so we try to recover it from the index
5333 * The space of the index expression is of the form I -> A,
5334 * with I the statement instances and A the array, or [I -> F] -> A,
5335 * with F the filters corresponding to arguments.
5336 * We first drop F, if present, obtaining I -> A.
5337 * Then we construct I -> R, with R the reference tag,
5338 * combine the two into I -> [R -> A] and uncurry to obtain
5339 * the final result [I -> R] -> A.
5340 * Note that the index expression may have a lower dimension
5341 * than that of the array, but this dimension is not used
5342 * if the access relation is empty.
5344 static __isl_give isl_map
*extract_single_tagged_access(
5345 __isl_take isl_union_map
*tagged
, __isl_keep pet_expr
*expr
)
5349 isl_space
*space
, *space2
;
5350 isl_multi_pw_aff
*index
;
5352 empty
= isl_union_map_is_empty(tagged
);
5356 return isl_map_from_union_map(tagged
);
5357 isl_union_map_free(tagged
);
5359 index
= pet_expr_access_get_index(expr
);
5360 space
= isl_multi_pw_aff_get_space(index
);
5361 isl_multi_pw_aff_free(index
);
5362 if (isl_space_domain_is_wrapping(space
))
5363 space
= isl_space_domain_factor_domain(space
);
5364 space2
= isl_space_copy(space
);
5365 space2
= isl_space_from_domain(isl_space_domain(space
));
5366 id
= pet_expr_access_get_ref_id(expr
);
5367 space2
= isl_space_set_tuple_id(space2
, isl_dim_out
, id
);
5368 space
= isl_space_range_product(space2
, space
);
5369 space
= isl_space_uncurry(space
);
5371 return isl_map_empty(space
);
5373 isl_union_map_free(tagged
);
5377 /* Does the index expression "index" of "expr" represent an access
5378 * to a single element?
5379 * That is, is "index" completely specified?
5381 * If "expr" accesses elements from different spaces (i.e., fields
5382 * of a structure), then it does not access a single element.
5383 * Otherwise, if the single space of the access matches the space
5384 * of "index", then the index expression is completely specified
5385 * (no pointer to a lower-dimensional slice of the accessed array)
5386 * and a single element is being accessed.
5388 static isl_bool
complete_index(__isl_keep pet_expr
*expr
,
5389 __isl_keep isl_multi_pw_aff
*index
)
5391 isl_union_map
*read
, *write
, *all
;
5393 isl_space
*space1
, *space2
;
5396 read
= pet_expr_access_get_may_read(expr
);
5397 write
= pet_expr_access_get_may_write(expr
);
5398 all
= isl_union_map_union(read
, write
);
5400 return isl_bool_error
;
5401 if (isl_union_map_n_map(all
) != 1) {
5402 isl_union_map_free(all
);
5403 return isl_bool_false
;
5405 map
= isl_map_from_union_map(all
);
5406 space1
= isl_map_get_space(map
);
5408 space2
= isl_multi_pw_aff_get_space(index
);
5409 complete
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
5410 space2
, isl_dim_out
);
5411 isl_space_free(space1
);
5412 isl_space_free(space2
);
5417 /* Does "expr" access a single, fixed element (independently of the statement
5419 * That is, does it have a completely specified constant index expression?
5421 * Note that it is not sufficient for the index expression to be
5422 * piecewise constant. isl_multi_pw_aff_is_cst can therefore not be used.
5424 static isl_bool
accesses_fixed_element(__isl_keep pet_expr
*expr
)
5427 isl_multi_pw_aff
*index
;
5428 isl_bool fixed
= isl_bool_true
;
5430 index
= pet_expr_access_get_index(expr
);
5432 return isl_bool_error
;
5433 n
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5434 for (i
= 0; i
< n
; ++i
) {
5437 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
5438 fixed
= isl_pw_aff_n_piece(pa
) == 1;
5440 fixed
= isl_pw_aff_is_cst(pa
);
5441 isl_pw_aff_free(pa
);
5442 if (fixed
< 0 || !fixed
)
5445 if (fixed
>= 0 && fixed
)
5446 fixed
= complete_index(expr
, index
);
5447 isl_multi_pw_aff_free(index
);
5452 /* Extract a gpu_stmt_access from "expr", append it to the list
5453 * that ends in *data->next_access and update the end of the list.
5454 * If the access expression performs a write, then it is considered
5455 * exact only if it appears in a single expression statement and
5456 * if its may access relation is equal to its must access relation.
5458 * The combined set of may accesses may be a union if member accesses
5459 * are involved, but the entire set is derived from a single reference and
5460 * therefore from a single index expression. These accesses therefore
5461 * all map to the same outer array.
5463 static int extract_access(__isl_keep pet_expr
*expr
, void *user
)
5465 struct ppcg_extract_access_data
*data
= user
;
5466 isl_union_map
*tagged
;
5467 struct gpu_stmt_access
*access
;
5468 isl_ctx
*ctx
= pet_expr_get_ctx(expr
);
5469 isl_multi_pw_aff
*index
;
5471 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
5473 access
->next
= NULL
;
5474 access
->read
= pet_expr_access_is_read(expr
);
5475 access
->write
= pet_expr_access_is_write(expr
);
5476 tagged
= pet_expr_access_get_tagged_may_read(expr
);
5477 tagged
= isl_union_map_union(tagged
,
5478 pet_expr_access_get_tagged_may_write(expr
));
5479 tagged
= isl_union_map_apply_range(tagged
,
5480 isl_union_map_copy(data
->any_to_outer
));
5481 if (!access
->write
) {
5482 access
->exact_write
= 1;
5483 } else if (!data
->single_expression
) {
5484 access
->exact_write
= 0;
5486 isl_union_map
*must
, *may
;
5487 may
= isl_union_map_copy(tagged
);
5488 may
= isl_union_map_domain_factor_domain(may
);
5489 must
= pet_expr_access_get_must_write(expr
);
5490 access
->exact_write
= isl_union_map_is_equal(must
, may
);
5491 isl_union_map_free(must
);
5492 isl_union_map_free(may
);
5494 index
= pet_expr_access_get_index(expr
);
5495 access
->n_index
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5496 isl_multi_pw_aff_free(index
);
5497 access
->ref_id
= pet_expr_access_get_ref_id(expr
);
5498 access
->tagged_access
= extract_single_tagged_access(tagged
, expr
);
5499 access
->access
= isl_map_copy(access
->tagged_access
);
5500 access
->access
= isl_map_domain_factor_domain(access
->access
);
5501 access
->fixed_element
= accesses_fixed_element(expr
);
5503 *data
->next_access
= access
;
5504 data
->next_access
= &(*data
->next_access
)->next
;
5506 if (!access
->access
|| access
->fixed_element
< 0)
5512 /* Construct a linked list of gpu_stmt_access objects,
5513 * one for each access expression in the statement body.
5514 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5516 static int pet_stmt_extract_accesses(struct gpu_stmt
*stmt
,
5517 __isl_keep isl_union_map
*any_to_outer
)
5519 struct ppcg_extract_access_data data
;
5521 stmt
->accesses
= NULL
;
5522 data
.next_access
= &stmt
->accesses
;
5523 data
.single_expression
=
5524 pet_tree_get_type(stmt
->stmt
->body
) == pet_tree_expr
;
5525 data
.any_to_outer
= any_to_outer
;
5526 return pet_tree_foreach_access_expr(stmt
->stmt
->body
,
5527 &extract_access
, &data
);
5530 /* Has statement "stmt" been killed from "scop"?
5531 * That is, is the instance set of "scop" free from any
5532 * instances of "stmt"?
5534 static isl_bool
is_stmt_killed(struct ppcg_scop
*scop
, struct pet_stmt
*stmt
)
5541 return isl_bool_error
;
5542 space
= isl_set_get_space(stmt
->domain
);
5543 left
= isl_union_set_extract_set(scop
->domain
, space
);
5544 empty
= isl_set_plain_is_empty(left
);
5550 /* Return an array of gpu_stmt representing the statements in "scop".
5551 * Do not collect array accesses for statements that have been killed.
5553 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
5554 __isl_keep isl_union_map
*any_to_outer
)
5557 struct gpu_stmt
*stmts
;
5559 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->pet
->n_stmt
);
5563 for (i
= 0; i
< scop
->pet
->n_stmt
; ++i
) {
5564 struct gpu_stmt
*s
= &stmts
[i
];
5567 s
->id
= isl_set_get_tuple_id(scop
->pet
->stmts
[i
]->domain
);
5568 s
->stmt
= scop
->pet
->stmts
[i
];
5569 killed
= is_stmt_killed(scop
, scop
->pet
->stmts
[i
]);
5571 return free_stmts(stmts
, i
+ 1);
5574 if (pet_stmt_extract_accesses(s
, any_to_outer
) < 0)
5575 return free_stmts(stmts
, i
+ 1);
5581 /* Generate CUDA code for "scop" and print it to "p".
5582 * After generating an AST for the transformed scop as explained below,
5583 * we call "gen->print" to print the AST in the desired output format
5586 * If it turns out that it does not make sense to generate GPU code,
5587 * then we generate CPU code instead.
5589 * The declarations of the arrays that are visible outside of the scop
5590 * are printed outside of the code generated from the schedule,
5591 * because the generated code may involve a guard around the entire code.
5593 * We first compute a schedule that respects the dependences
5594 * of the original program and select the outermost bands
5595 * of tilable dimensions that have at least one parallel loop.
5596 * If the --load-schedule is specified, then the loaded schedule
5597 * is used instead of a computed schedule.
5599 * Each of these bands B is then tiled according to "tile" sizes, resulting
5600 * in two nested bands, with a kernel marker on top
5608 * We then split off at most 2 parallel dimensions from the T band and
5609 * at most 3 parallel dimension from the P band
5622 * A filter is introduced in front of T1 that maps the domain instances
5623 * to block identifiers. Similarly, a filter is introduced in front of P1
5624 * that maps the domain instances to thread identifiers.
5626 * For each iteration of the T2 band and for each array, we compute
5627 * the array elements accessed by that iteration, construct a rectangular
5628 * box around it and shift it to the origin. The result is used
5629 * as shared memory for the array.
5631 * Copying and synchronization statements are added to this schedule tree.
5632 * In principle, these are added in front of the P1 band, but some of
5633 * them may get hoisted up to higher levels.
5635 * The entire AST is then generated from the single resulting schedule tree.
5636 * During the generation the subtrees at kernel nodes (K) are saved
5637 * aside and replaced by kernel calls. The result is printed as host code
5638 * while the saved subtrees are printed as device code.
5640 static __isl_give isl_printer
*generate(__isl_take isl_printer
*p
,
5641 struct gpu_gen
*gen
, struct ppcg_scop
*scop
,
5642 struct ppcg_options
*options
)
5644 struct gpu_prog
*prog
;
5646 isl_schedule
*schedule
;
5647 isl_bool any_permutable
;
5650 return isl_printer_free(p
);
5652 ctx
= isl_printer_get_ctx(p
);
5653 prog
= gpu_prog_alloc(ctx
, scop
);
5655 return isl_printer_free(p
);
5658 schedule
= get_schedule(gen
);
5660 any_permutable
= has_any_permutable_node(schedule
);
5661 if (any_permutable
< 0 || !any_permutable
) {
5662 if (any_permutable
< 0)
5663 p
= isl_printer_free(p
);
5665 p
= print_cpu(p
, scop
, options
);
5666 isl_schedule_free(schedule
);
5668 schedule
= map_to_device(gen
, schedule
);
5669 gen
->tree
= generate_code(gen
, schedule
);
5670 p
= ppcg_set_macro_names(p
);
5671 p
= ppcg_print_exposed_declarations(p
, prog
->scop
);
5672 p
= gen
->print(p
, gen
->prog
, gen
->tree
, &gen
->types
,
5674 isl_ast_node_free(gen
->tree
);
5677 gpu_prog_free(prog
);
5682 /* Wrapper around generate for use as a ppcg_transform callback.
5684 static __isl_give isl_printer
*generate_wrap(__isl_take isl_printer
*p
,
5685 struct ppcg_scop
*scop
, void *user
)
5687 struct gpu_gen
*gen
= user
;
5689 return generate(p
, gen
, scop
, gen
->options
);
5692 /* Transform the code in the file called "input" by replacing
5693 * all scops by corresponding GPU code and write the results to "out".
5695 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
5696 struct ppcg_options
*options
,
5697 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
5698 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
5699 struct gpu_types
*types
, void *user
), void *user
)
5706 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
5707 gen
.options
= options
;
5710 gen
.print_user
= user
;
5712 gen
.types
.name
= NULL
;
5714 if (options
->debug
->dump_sizes
) {
5715 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
5716 gen
.used_sizes
= isl_union_map_empty(space
);
5719 r
= ppcg_transform(ctx
, input
, out
, options
, &generate_wrap
, &gen
);
5721 if (options
->debug
->dump_sizes
) {
5722 isl_union_map_dump(gen
.used_sizes
);
5723 isl_union_map_free(gen
.used_sizes
);
5726 isl_union_map_free(gen
.sizes
);
5727 for (i
= 0; i
< gen
.types
.n
; ++i
)
5728 free(gen
.types
.name
[i
]);
5729 free(gen
.types
.name
);
5734 /* Compute the set of inner array elements that may have their values
5735 * preserved by "prog". In particular, collect the array elements of
5736 * arrays that are not local to "prog" and remove those elements that
5737 * are definitely killed or definitely written by "prog".
5739 static __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
)
5742 isl_union_set
*may_persist
, *killed
;
5743 isl_union_map
*must_kill
;
5745 may_persist
= isl_union_set_empty(isl_set_get_space(prog
->context
));
5746 for (i
= 0; i
< prog
->n_array
; ++i
) {
5749 if (prog
->array
[i
].local
)
5752 extent
= isl_set_copy(prog
->array
[i
].extent
);
5753 may_persist
= isl_union_set_add_set(may_persist
, extent
);
5756 may_persist
= isl_union_set_intersect_params(may_persist
,
5757 isl_set_copy(prog
->context
));
5758 may_persist
= isl_union_set_apply(may_persist
,
5759 isl_union_map_copy(prog
->to_inner
));
5760 must_kill
= isl_union_map_copy(prog
->tagged_must_kill
);
5761 killed
= isl_union_map_range(must_kill
);
5762 must_kill
= isl_union_map_copy(prog
->must_write
);
5763 killed
= isl_union_set_union(killed
, isl_union_map_range(must_kill
));
5765 may_persist
= isl_union_set_subtract(may_persist
, killed
);
5769 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
5771 struct gpu_prog
*prog
;
5778 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
5783 prog
->context
= isl_set_copy(scop
->context
);
5784 prog
->n_stmts
= scop
->pet
->n_stmt
;
5785 prog
->any_to_outer
= pet_scop_compute_outer_to_any(scop
->pet
);
5786 prog
->any_to_outer
= isl_union_map_reverse(prog
->any_to_outer
);
5787 space
= isl_union_map_get_space(prog
->any_to_outer
);
5788 space
= isl_space_set_from_params(space
);
5789 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
5790 space
= isl_space_map_from_set(space
);
5791 id
= isl_map_identity(space
);
5792 prog
->any_to_outer
= isl_union_map_add_map(prog
->any_to_outer
, id
);
5793 prog
->stmts
= extract_stmts(ctx
, scop
, prog
->any_to_outer
);
5794 prog
->read
= isl_union_map_copy(scop
->reads
);
5795 prog
->may_write
= isl_union_map_copy(scop
->may_writes
);
5796 prog
->must_write
= isl_union_map_copy(scop
->must_writes
);
5797 prog
->tagged_must_kill
= isl_union_map_copy(scop
->tagged_must_kills
);
5798 prog
->to_inner
= pet_scop_compute_outer_to_inner(scop
->pet
);
5799 prog
->to_outer
= isl_union_map_copy(prog
->to_inner
);
5800 prog
->to_outer
= isl_union_map_reverse(prog
->to_outer
);
5803 return gpu_prog_free(prog
);
5805 if (collect_array_info(prog
) < 0)
5806 return gpu_prog_free(prog
);
5807 prog
->may_persist
= compute_may_persist(prog
);
5812 void *gpu_prog_free(struct gpu_prog
*prog
)
5816 free_array_info(prog
);
5817 free_stmts(prog
->stmts
, prog
->n_stmts
);
5818 isl_union_map_free(prog
->any_to_outer
);
5819 isl_union_map_free(prog
->to_outer
);
5820 isl_union_map_free(prog
->to_inner
);
5821 isl_union_map_free(prog
->read
);
5822 isl_union_map_free(prog
->may_write
);
5823 isl_union_map_free(prog
->must_write
);
5824 isl_union_map_free(prog
->tagged_must_kill
);
5825 isl_union_map_free(prog
->array_order
);
5826 isl_union_set_free(prog
->may_persist
);
5827 isl_set_free(prog
->context
);