2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 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"
34 #include "ppcg_options.h"
38 struct gpu_array_info
;
40 /* Return the name of the outer array (of structs) accessed by "access".
42 static const char *get_outer_array_name(__isl_keep isl_map
*access
)
47 space
= isl_space_range(isl_map_get_space(access
));
48 while (space
&& isl_space_is_wrapping(space
))
49 space
= isl_space_domain(isl_space_unwrap(space
));
50 name
= isl_space_get_tuple_name(space
, isl_dim_set
);
51 isl_space_free(space
);
56 /* Collect all references to the given array and store pointers to them
59 static void collect_references(struct gpu_prog
*prog
,
60 struct gpu_array_info
*array
)
66 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
67 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
68 struct gpu_stmt_access
*access
;
70 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
72 name
= get_outer_array_name(access
->access
);
73 if (name
&& !strcmp(array
->name
, name
))
79 array
->refs
= isl_alloc_array(prog
->ctx
, struct gpu_stmt_access
*, n
);
83 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
84 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
85 struct gpu_stmt_access
*access
;
87 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
89 name
= get_outer_array_name(access
->access
);
90 if (!name
|| strcmp(array
->name
, name
))
93 array
->refs
[n
++] = access
;
98 /* Compute and return the extent of "array", taking into account the set of
101 * In particular, the extent in the outer dimension is taken
102 * from "accessed", while the extents in the remaining dimensions
103 * are taken from array->extent.
105 * The extent in the outer dimension cannot be taken from array->extent
106 * because that may be unbounded. Furthermore, even if it is bounded,
107 * it may be larger than the piece of the array that is being accessed.
109 static __isl_give isl_set
*compute_extent(struct pet_array
*array
,
110 __isl_keep isl_set
*accessed
)
117 extent
= isl_set_copy(array
->extent
);
119 n_index
= isl_set_dim(accessed
, isl_dim_set
);
123 extent
= isl_set_project_out(extent
, isl_dim_set
, 0, 1);
124 outer
= isl_set_copy(accessed
);
125 outer
= isl_set_project_out(outer
, isl_dim_set
, 1, n_index
- 1);
126 extent
= isl_set_flat_product(outer
, extent
);
127 id
= isl_set_get_tuple_id(accessed
);
128 extent
= isl_set_set_tuple_id(extent
, id
);
133 /* Is the array "array" being extracted a read-only scalar?
135 * That is, is "array" a scalar that is never possibly written to.
136 * An array containing structures is never considered to be a scalar.
138 static int is_read_only_scalar(struct gpu_array_info
*array
,
139 struct gpu_prog
*prog
)
142 isl_union_map
*write
;
145 if (array
->has_compound_element
)
147 if (array
->n_index
!= 0)
150 write
= isl_union_map_copy(prog
->may_write
);
151 space
= isl_set_universe(isl_space_copy(array
->space
));
152 write
= isl_union_map_intersect_range(write
,
153 isl_union_set_from_set(space
));
154 empty
= isl_union_map_is_empty(write
);
155 isl_union_map_free(write
);
160 /* Is "array" only accessed as individual, fixed elements?
161 * That is, does each access to "array" access a single, fixed element?
163 static isl_bool
only_fixed_element_accessed(struct gpu_array_info
*array
)
167 for (i
= 0; i
< array
->n_ref
; ++i
)
168 if (!array
->refs
[i
]->fixed_element
)
169 return isl_bool_false
;
171 return isl_bool_true
;
174 /* Compute bounds on the host array "pa" based on the corresponding
175 * accessed elements in "arrays"
176 * and collect all references to the array.
177 * Store the results in "info".
179 * If the array is zero-dimensional and does not contain structures,
180 * i.e., if the array is a scalar, we check whether it is read-only.
181 * We also check whether the array is accessed at all.
183 static int extract_array_info(struct gpu_prog
*prog
,
184 struct gpu_array_info
*info
, struct pet_array
*pa
,
185 __isl_keep isl_union_set
*arrays
)
190 isl_multi_pw_aff
*bounds
;
191 isl_set
*accessed
, *extent
;
193 n_index
= isl_set_dim(pa
->extent
, isl_dim_set
);
194 name
= isl_set_get_tuple_name(pa
->extent
);
196 info
->space
= isl_set_get_space(pa
->extent
);
197 info
->name
= strdup(name
);
198 info
->n_index
= n_index
;
199 info
->linearize
= prog
->scop
->options
->linearize_device_arrays
;
201 info
->type
= strdup(pa
->element_type
);
202 info
->size
= pa
->element_size
;
203 info
->local
= pa
->declared
&& !pa
->exposed
;
204 info
->has_compound_element
= pa
->element_is_record
;
205 info
->read_only_scalar
= is_read_only_scalar(info
, prog
);
207 info
->declared_extent
= isl_set_copy(pa
->extent
);
208 accessed
= isl_union_set_extract_set(arrays
,
209 isl_space_copy(info
->space
));
210 empty
= isl_set_is_empty(accessed
);
211 extent
= compute_extent(pa
, accessed
);
212 isl_set_free(accessed
);
213 info
->extent
= extent
;
216 info
->accessed
= !empty
;
217 bounds
= ppcg_size_from_extent(isl_set_copy(extent
));
218 bounds
= isl_multi_pw_aff_gist(bounds
, isl_set_copy(prog
->context
));
221 if (!isl_multi_pw_aff_is_cst(bounds
))
223 info
->bound
= bounds
;
225 collect_references(prog
, info
);
226 info
->only_fixed_element
= only_fixed_element_accessed(info
);
231 /* Remove independence from the order constraints "order" on array "array".
232 * Since the pairs of iterations in the filter relation of an independence
233 * are guaranteed to be completely independent by the user, there is
234 * no need to ensure that live ranges are ordered along thong pairs.
235 * We make an exception for local variables, though, as the independence
236 * guarantee does not apply to those.
238 * The order constraints are used in two places.
239 * Those on scalars are used in check_scalar_live_ranges to check if
240 * we need to force the scalar to be private. Any non-local scalar
241 * should not be forced scalar if it only appears in independent loops.
242 * Those on non-scalars are added to the coincidence constraints
243 * in compute_schedule because we do not support any array expansion.
244 * Accesses to non-local arrays should not prevent a loop from being
245 * considered coincident so we should indeed remove those constraints
246 * from the order constraints.
248 static __isl_give isl_union_map
*remove_independences(struct gpu_prog
*prog
,
249 struct gpu_array_info
*array
, __isl_take isl_union_map
*order
)
253 for (i
= 0; i
< prog
->scop
->pet
->n_independence
; ++i
) {
254 struct pet_independence
*pi
= prog
->scop
->pet
->independences
[i
];
255 if (isl_union_set_contains(pi
->local
, array
->space
))
258 order
= isl_union_map_subtract(order
,
259 isl_union_map_copy(pi
->filter
));
265 /* For each array in "prog", store the (untagged) order dependences
266 * derived from the array in array->dep_order.
267 * In particular, consider all references that access the given array
268 * and take the order dependences that have one of these references
269 * as source. (Since an order dependence relates two references to
270 * the same array, the target of these order dependences will also
271 * be one of these references.)
272 * Additionally, store the union of these array->dep_order relations
273 * for all arrays that cannot be mapped to private memory in prog->array_order.
275 void collect_order_dependences(struct gpu_prog
*prog
)
279 isl_union_map
*accesses
;
281 space
= isl_union_map_get_space(prog
->read
);
282 prog
->array_order
= isl_union_map_empty(space
);
284 accesses
= isl_union_map_copy(prog
->scop
->tagged_reads
);
285 accesses
= isl_union_map_union(accesses
,
286 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
287 accesses
= isl_union_map_universe(accesses
);
288 accesses
= isl_union_map_apply_range(accesses
,
289 isl_union_map_copy(prog
->to_outer
));
291 for (i
= 0; i
< prog
->n_array
; ++i
) {
292 struct gpu_array_info
*array
= &prog
->array
[i
];
295 isl_union_map
*order
;
297 set
= isl_set_universe(isl_space_copy(array
->space
));
298 uset
= isl_union_set_from_set(set
);
299 uset
= isl_union_map_domain(
300 isl_union_map_intersect_range(isl_union_map_copy(accesses
),
302 order
= isl_union_map_copy(prog
->scop
->tagged_dep_order
);
303 order
= isl_union_map_intersect_domain(order
, uset
);
304 order
= isl_union_map_zip(order
);
305 order
= isl_union_set_unwrap(isl_union_map_domain(order
));
306 order
= remove_independences(prog
, array
, order
);
307 array
->dep_order
= order
;
309 if (gpu_array_can_be_private(array
))
312 prog
->array_order
= isl_union_map_union(prog
->array_order
,
313 isl_union_map_copy(array
->dep_order
));
316 isl_union_map_free(accesses
);
319 /* Construct a gpu_array_info for each array referenced by prog->scop and
320 * collect them in prog->array.
322 * The sizes are based on the extents and the set of possibly accessed
323 * elements by "prog".
324 * If there are any member accesses involved, then they are first mapped
325 * to the outer arrays of structs.
326 * Only extract gpu_array_info entries for these outer arrays.
328 * If we are allowing live range reordering, then also set
329 * the dep_order field. Otherwise leave it NULL.
331 static int collect_array_info(struct gpu_prog
*prog
)
335 isl_union_set
*arrays
;
337 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
338 arrays
= isl_union_set_union(arrays
,
339 isl_union_map_range(isl_union_map_copy(prog
->may_write
)));
341 arrays
= isl_union_set_apply(arrays
,
342 isl_union_map_copy(prog
->to_outer
));
344 arrays
= isl_union_set_coalesce(arrays
);
346 prog
->n_array
= prog
->scop
->pet
->n_array
;
347 prog
->array
= isl_calloc_array(prog
->ctx
,
348 struct gpu_array_info
, prog
->n_array
);
351 for (i
= 0; i
< prog
->scop
->pet
->n_array
; ++i
) {
354 field
= isl_set_is_wrapping(prog
->scop
->pet
->arrays
[i
]->extent
);
359 if (extract_array_info(prog
, &prog
->array
[prog
->n_array
++],
360 prog
->scop
->pet
->arrays
[i
], arrays
) < 0)
363 if (i
< prog
->scop
->pet
->n_array
)
366 isl_union_set_free(arrays
);
368 if (prog
->scop
->options
->live_range_reordering
)
369 collect_order_dependences(prog
);
374 static void free_array_info(struct gpu_prog
*prog
)
378 for (i
= 0; i
< prog
->n_array
; ++i
) {
379 free(prog
->array
[i
].type
);
380 free(prog
->array
[i
].name
);
381 isl_multi_pw_aff_free(prog
->array
[i
].bound
);
382 isl_ast_expr_free(prog
->array
[i
].bound_expr
);
383 isl_space_free(prog
->array
[i
].space
);
384 isl_set_free(prog
->array
[i
].declared_extent
);
385 isl_set_free(prog
->array
[i
].extent
);
386 isl_ast_expr_free(prog
->array
[i
].declared_size
);
387 free(prog
->array
[i
].refs
);
388 isl_union_map_free(prog
->array
[i
].dep_order
);
393 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
394 * as an array or through a pointer reference, but as a single data element.
395 * At the moment, scalars are represented as zero-dimensional arrays.
396 * Note that the single data element may be an entire structure.
398 int gpu_array_is_scalar(struct gpu_array_info
*array
)
400 return array
->n_index
== 0;
403 /* Can "array" be mapped to private memory?
404 * That is, is it only accessed as individual elements with
405 * constant index expressions?
407 isl_bool
gpu_array_can_be_private(struct gpu_array_info
*array
)
410 return isl_bool_error
;
411 return array
->only_fixed_element
;
414 /* Is "array" a read-only scalar?
416 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
418 return array
->read_only_scalar
;
421 /* Does "array" need to be allocated on the device?
422 * If it is a read-only scalar, then it will be passed as an argument
423 * to the kernel and therefore does not require any allocation.
424 * If this device memory is not accessed at all, then it does not
425 * need to be allocated either.
427 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
)
429 if (gpu_array_is_read_only_scalar(array
))
436 /* Return the set of parameter values for which the array has a positive
437 * size in all dimensions.
438 * If the sizes are only valid for some parameter values, then those
439 * constraints are also taken into account.
441 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
)
450 space
= isl_space_params(isl_space_copy(array
->space
));
451 guard
= isl_set_universe(space
);
453 for (i
= 0; i
< array
->n_index
; ++i
) {
455 isl_set
*guard_i
, *zero
;
457 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
458 guard_i
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound
));
459 zero
= isl_pw_aff_zero_set(bound
);
460 guard_i
= isl_set_subtract(guard_i
, zero
);
461 guard
= isl_set_intersect(guard
, guard_i
);
467 /* Internal data structure for extract_size_of_type.
468 * "type" specifies the name of the space that we want to extract.
469 * "res" is used to store the subset of that space.
471 struct ppcg_extract_size_data
{
476 /* This function is called for each set in a union_set.
477 * If the name of the set matches data->type, we store the
480 static isl_stat
extract_size_of_type(__isl_take isl_set
*size
, void *user
)
482 struct ppcg_extract_size_data
*data
= user
;
485 name
= isl_set_get_tuple_name(size
);
486 if (name
&& !strcmp(name
, data
->type
)) {
488 return isl_stat_error
;
495 /* Given a union map { kernel[i] -> *[...] },
496 * return the range in the space called "type" for the kernel with
497 * sequence number "id".
499 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
500 const char *type
, int id
)
504 isl_union_set
*local_sizes
;
505 struct ppcg_extract_size_data data
= { type
, NULL
};
510 space
= isl_union_map_get_space(sizes
);
511 space
= isl_space_set_from_params(space
);
512 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
513 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
514 dom
= isl_set_universe(space
);
515 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
517 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
518 isl_union_map_copy(sizes
));
519 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
520 isl_union_set_free(local_sizes
);
524 /* Given a singleton set, extract the first (at most *len) elements
525 * of the single integer tuple into *sizes and update *len if needed.
527 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
535 dim
= isl_set_dim(set
, isl_dim_set
);
539 for (i
= 0; i
< *len
; ++i
) {
542 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
545 sizes
[i
] = isl_val_get_num_si(v
);
552 /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
553 * if the option debug->dump_sizes is set.
555 static void set_used_sizes(struct gpu_gen
*gen
, const char *type
, int id
,
562 if (!gen
->options
->debug
->dump_sizes
)
565 space
= isl_union_map_get_space(gen
->used_sizes
);
566 space
= isl_space_set_from_params(space
);
567 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
568 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
569 space
= isl_space_from_domain(space
);
570 space
= isl_space_add_dims(space
, isl_dim_out
, len
);
571 space
= isl_space_set_tuple_name(space
, isl_dim_out
, type
);
573 map
= isl_map_universe(space
);
574 map
= isl_map_fix_si(map
, isl_dim_in
, 0, id
);
575 for (i
= 0; i
< len
; ++i
)
576 map
= isl_map_fix_si(map
, isl_dim_out
, i
, sizes
[i
]);
578 gen
->used_sizes
= isl_union_map_add_map(gen
->used_sizes
, map
);
581 /* Extract user specified "tile" sizes from the "sizes" command line option,
582 * defaulting to option->tile_size in each dimension.
583 * *tile_len contains the maximum number of tile sizes needed.
584 * Update *tile_len to the number of specified tile sizes, if any, and
585 * return a pointer to the tile sizes (or NULL on error).
586 * Add the effectively used sizes to gen->used_sizes.
588 static int *read_tile_sizes(struct gpu_gen
*gen
, int *tile_len
)
594 tile_size
= isl_alloc_array(gen
->ctx
, int, *tile_len
);
597 for (n
= 0; n
< *tile_len
; ++n
)
598 tile_size
[n
] = gen
->options
->tile_size
;
600 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
601 read_sizes_from_set(size
, tile_size
, tile_len
);
602 set_used_sizes(gen
, "tile", gen
->kernel_id
, tile_size
, *tile_len
);
607 /* Extract user specified "block" sizes from the "sizes" command line option,
608 * after filling in some potentially useful defaults.
610 static void read_block_sizes(struct ppcg_kernel
*kernel
,
611 __isl_keep isl_union_map
*sizes
)
615 if (kernel
->n_block
> 3)
617 switch (kernel
->n_block
) {
619 kernel
->block_dim
[0] = 512;
622 kernel
->block_dim
[0] = 32;
623 kernel
->block_dim
[1] = 16;
626 kernel
->block_dim
[0] = 32;
627 kernel
->block_dim
[1] = 4;
628 kernel
->block_dim
[2] = 4;
632 size
= extract_sizes(sizes
, "block", kernel
->id
);
633 read_sizes_from_set(size
, kernel
->block_dim
, &kernel
->n_block
);
636 /* Extract user specified "grid" sizes from the "sizes" command line option,
637 * after filling in some potentially useful defaults.
639 static void read_grid_sizes(struct ppcg_kernel
*kernel
,
640 __isl_keep isl_union_map
*sizes
)
644 if (kernel
->n_grid
> 2)
646 switch (kernel
->n_grid
) {
648 kernel
->grid_dim
[0] = 32768;
651 kernel
->grid_dim
[0] = 256;
652 kernel
->grid_dim
[1] = 256;
656 size
= extract_sizes(sizes
, "grid", kernel
->id
);
657 read_sizes_from_set(size
, kernel
->grid_dim
, &kernel
->n_grid
);
660 /* Extract user specified grid and block sizes from the gen->sizes
661 * command line option after filling in some potentially useful defaults.
662 * Store the extracted sizes in "kernel".
663 * Add the effectively used sizes to gen->used_sizes.
665 static void read_grid_and_block_sizes(struct ppcg_kernel
*kernel
,
668 read_block_sizes(kernel
, gen
->sizes
);
669 read_grid_sizes(kernel
, gen
->sizes
);
670 set_used_sizes(gen
, "block", kernel
->id
,
671 kernel
->block_dim
, kernel
->n_block
);
672 set_used_sizes(gen
, "grid", kernel
->id
,
673 kernel
->grid_dim
, kernel
->n_grid
);
676 static void *free_stmts(struct gpu_stmt
*stmts
, int n
)
683 for (i
= 0; i
< n
; ++i
) {
684 struct gpu_stmt_access
*access
, *next
;
686 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
688 isl_id_free(access
->ref_id
);
689 isl_map_free(access
->access
);
690 isl_map_free(access
->tagged_access
);
694 isl_id_free(stmts
[i
].id
);
701 /* Add parameters p[i] with identifiers "ids" to "set",
702 * with bounds to 0 <= p[i] < size[i].
704 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
705 int *size
, __isl_keep isl_id_list
*ids
)
710 len
= isl_id_list_n_id(ids
);
711 nparam
= isl_set_dim(set
, isl_dim_param
);
712 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
714 for (i
= 0; i
< len
; ++i
) {
717 id
= isl_id_list_get_id(ids
, i
);
718 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
719 set
= isl_set_lower_bound_si(set
, isl_dim_param
, nparam
+ i
, 0);
720 set
= isl_set_upper_bound_si(set
, isl_dim_param
,
721 nparam
+ i
, size
[i
] - 1);
727 /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
730 * { : 0 <= p[i] < size[i] }
732 * or an overapproximation.
734 static __isl_give isl_set
*add_bounded_parameters_dynamic(
735 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
736 __isl_keep isl_id_list
*ids
)
743 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
744 nparam
= isl_set_dim(set
, isl_dim_param
);
745 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
747 for (i
= 0; i
< len
; ++i
) {
750 id
= isl_id_list_get_id(ids
, i
);
751 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
754 space
= isl_space_params(isl_set_get_space(set
));
755 ls
= isl_local_space_from_space(space
);
756 for (i
= 0; i
< len
; ++i
) {
757 isl_pw_aff
*param
, *size_i
, *zero
;
760 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
761 isl_dim_param
, nparam
+ i
);
763 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
764 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
765 bound
= isl_set_from_basic_set(isl_set_simple_hull(bound
));
766 set
= isl_set_intersect_params(set
, bound
);
768 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
769 bound
= isl_pw_aff_ge_set(param
, zero
);
770 set
= isl_set_intersect_params(set
, bound
);
772 isl_local_space_free(ls
);
777 /* Return the union of all tagged access relations in the group.
779 static __isl_give isl_union_map
*group_tagged_access_relation(
780 struct gpu_array_ref_group
*group
)
783 isl_union_map
*access
;
785 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
786 for (i
= 0; i
< group
->n_ref
; ++i
) {
789 map_i
= isl_map_copy(group
->refs
[i
]->tagged_access
);
790 access
= isl_union_map_union(access
,
791 isl_union_map_from_map(map_i
));
797 /* Return the extent of "array", recomputed from the bounds.
798 * The recomputed extent may be simpler than the original extent.
800 static __isl_give isl_set
*array_extent(struct gpu_array_info
*array
)
808 id
= isl_set_get_tuple_id(array
->extent
);
809 space
= isl_set_get_space(array
->extent
);
810 extent
= isl_set_universe(isl_space_copy(space
));
811 ls
= isl_local_space_from_space(space
);
812 for (i
= 0; i
< array
->n_index
; ++i
) {
818 extent
= isl_set_lower_bound_si(extent
, isl_dim_set
, i
, 0);
820 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
822 index
= isl_pw_aff_from_aff(aff
);
823 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
824 bound
= isl_pw_aff_from_range(bound
);
825 bound
= isl_pw_aff_add_dims(bound
, isl_dim_in
, array
->n_index
);
826 bound
= isl_pw_aff_set_tuple_id(bound
, isl_dim_in
,
828 lt
= isl_pw_aff_lt_set(index
, bound
);
829 extent
= isl_set_intersect(extent
, lt
);
831 isl_local_space_free(ls
);
837 /* Return a map from the first group->shared_tile->depth dimensions
838 * of the computed schedule to the array tile in
839 * global memory that corresponds to the shared memory copy.
841 * In particular, return a map
847 * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
851 * 0 <= a <= array_size - 1 (2)
853 * Note that if some stride has been detected (i.e., when
854 * group->shared_tile->bound[i].shift is set), then a in (1) refers
855 * to the shifted and scaled down version.
857 * Constraints (1) are obtained by mapping the size constraints on the
858 * shared/private memory tile back to the access relation.
859 * Constraints (2) are obtained from the (recomputed) extent.
861 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
864 int n_index
= group
->array
->n_index
;
870 space
= isl_multi_aff_get_space(group
->shared_tile
->tiling
);
871 space
= isl_space_range(space
);
872 local
= isl_set_universe(space
);
873 for (i
= 0; i
< n_index
; ++i
) {
876 local
= isl_set_lower_bound_si(local
, isl_dim_set
, i
, 0);
877 bound
= isl_val_copy(group
->shared_tile
->bound
[i
].size
);
878 bound
= isl_val_sub_ui(bound
, 1);
879 local
= isl_set_upper_bound_val(local
, isl_dim_set
, i
, bound
);
881 local
= isl_set_preimage_multi_aff(local
,
882 isl_multi_aff_copy(group
->shared_tile
->tiling
));
883 tile
= isl_set_unwrap(local
);
884 extent
= array_extent(group
->array
);
885 tile
= isl_map_intersect_range(tile
, extent
);
890 /* Given a mapping "iterator_map" from the AST schedule to a domain,
891 * return the corresponding mapping from the AST schedule to
892 * to the outer kernel->copy_schedule_dim dimensions of
893 * the schedule computed by PPCG for this kernel.
895 * Note that kernel->copy_schedule_dim is at least as large as
896 * the largest depth of any array reference group associated to the kernel.
897 * This is needed as the returned schedule is used to extract a mapping
898 * to the outer tile->depth dimensions in transform_index.
900 static __isl_give isl_pw_multi_aff
*compute_sched_to_copy(
901 struct ppcg_kernel
*kernel
, __isl_take isl_pw_multi_aff
*iterator_map
)
903 isl_union_pw_multi_aff
*upma
;
904 isl_pw_multi_aff
*pma
;
907 space
= isl_space_range(isl_pw_multi_aff_get_space(iterator_map
));
908 space
= isl_space_from_domain(space
);
909 space
= isl_space_add_dims(space
, isl_dim_out
,
910 kernel
->copy_schedule_dim
);
912 upma
= isl_union_pw_multi_aff_copy(kernel
->copy_schedule
);
913 pma
= isl_union_pw_multi_aff_extract_pw_multi_aff(upma
, space
);
914 isl_union_pw_multi_aff_free(upma
);
916 return isl_pw_multi_aff_pullback_pw_multi_aff(pma
, iterator_map
);
919 /* If max_shared_memory is not set to infinity (-1), then make
920 * sure that the total amount of shared memory required by the
921 * array reference groups mapped to shared memory by "kernel"
922 * is no larger than this maximum.
924 * We apply a greedy approach and discard (keep in global memory)
925 * those groups that would result in a total memory size that
926 * is larger than the maximum.
928 * This function should be called after any function that may
929 * affect the decision on whether to place a reference group
930 * in private, shared or global memory.
932 static void check_shared_memory_bound(struct ppcg_kernel
*kernel
)
935 isl_val
*left
, *size
;
937 if (kernel
->options
->max_shared_memory
< 0)
940 left
= isl_val_int_from_si(kernel
->ctx
,
941 kernel
->options
->max_shared_memory
);
943 for (i
= 0; i
< kernel
->n_array
; ++i
) {
944 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
946 for (j
= 0; j
< local
->n_group
; ++j
) {
947 struct gpu_array_ref_group
*group
;
948 enum ppcg_group_access_type type
;
950 group
= local
->groups
[j
];
951 type
= gpu_array_ref_group_type(group
);
952 if (type
!= ppcg_access_shared
)
955 size
= gpu_array_tile_size(group
->shared_tile
);
956 size
= isl_val_mul_ui(size
, local
->array
->size
);
958 if (isl_val_le(size
, left
)) {
959 left
= isl_val_sub(left
, size
);
965 gpu_array_tile_free(group
->shared_tile
);
972 /* Mark all arrays of "kernel" that have an array reference group
973 * that is not mapped to private or shared memory as
974 * accessing the corresponding global device memory.
976 static void mark_global_arrays(struct ppcg_kernel
*kernel
)
980 for (i
= 0; i
< kernel
->n_array
; ++i
) {
981 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
985 for (j
= 0; j
< local
->n_group
; ++j
) {
986 if (gpu_array_ref_group_tile(local
->groups
[j
]))
990 local
->array
->global
= 1;
996 /* Compute a tiling for all the array reference groups in "kernel".
998 static void compute_group_tilings(struct ppcg_kernel
*kernel
)
1002 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1003 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1005 for (j
= 0; j
< array
->n_group
; ++j
)
1006 gpu_array_ref_group_compute_tiling(array
->groups
[j
]);
1010 /* Compute the effective grid size as a list of the sizes in each dimension.
1012 * The grid size specified by the user or set by default
1013 * in read_grid_sizes() and applied by the block filter,
1014 * may be too large for the given code in the sense that
1015 * it may contain blocks that don't need to execute anything.
1016 * We therefore don't return this grid size, but instead the
1017 * smallest grid size that ensures that all blocks that actually
1018 * execute code are included in the grid.
1020 * We first extract a description of the grid, i.e., the possible values
1021 * of the block ids, from the domain elements in "domain" and
1022 * kernel->block_filter.
1023 * The block ids are parameters in kernel->block_filter.
1024 * We simply need to change them into set dimensions.
1026 * Then, for each block dimension, we compute the maximal value of the block id
1029 static __isl_give isl_multi_pw_aff
*extract_grid_size(
1030 struct ppcg_kernel
*kernel
, __isl_take isl_union_set
*domain
)
1035 isl_multi_pw_aff
*size
;
1037 domain
= isl_union_set_intersect(domain
,
1038 isl_union_set_copy(kernel
->block_filter
));
1039 grid
= isl_union_set_params(domain
);
1040 grid
= isl_set_from_params(grid
);
1041 grid
= isl_set_add_dims(grid
, isl_dim_set
, kernel
->n_grid
);
1042 for (i
= 0; i
< kernel
->n_grid
; ++i
) {
1046 id
= isl_id_list_get_id(kernel
->block_ids
, i
);
1047 pos
= isl_set_find_dim_by_id(grid
, isl_dim_param
, id
);
1050 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
1051 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
1054 grid
= isl_set_coalesce(grid
);
1055 size
= ppcg_size_from_extent(grid
);
1056 context
= isl_set_params(isl_set_copy(kernel
->context
));
1057 return isl_multi_pw_aff_gist(size
, context
);
1060 /* Compute the size of a fixed bounding box around the origin and "set",
1061 * where "set" is assumed to contain only non-negative elements,
1062 * and store the results in "size".
1063 * In particular, compute the maximal value of "set" in each direction
1066 static void extract_fixed_size(__isl_take isl_set
*set
, int *size
)
1069 isl_local_space
*ls
;
1072 n
= isl_set_dim(set
, isl_dim_set
);
1073 ls
= isl_local_space_from_space(isl_set_get_space(set
));
1074 obj
= isl_aff_zero_on_domain(ls
);
1075 for (i
= 0; i
< n
; ++i
) {
1078 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 1);
1079 max
= isl_set_max_val(set
, obj
);
1080 size
[i
] = isl_val_get_num_si(max
) + 1;
1082 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 0);
1088 /* Compute the effective block size as a list of the sizes in each dimension
1089 * and store the sizes in kernel->block_dim.
1091 * The block size specified by the user or set by default
1092 * in read_block_sizes() and applied by the thread filter,
1093 * may be too large for the given code in the sense that
1094 * it may contain threads that don't need to execute anything.
1095 * We therefore update this block size in kernel->block_dim
1096 * to the smallest block size that ensures that all threads
1097 * that actually execute code are included in the block.
1099 * The set of possible values of the thread ids is obtained from
1100 * the domain elements "domain" and kernel->thread_filter.
1101 * The current implementation eliminates all parameters, ensuring
1102 * that the size is a fixed constant in each dimension.
1103 * In principle we could also compute parametric sizes.
1104 * We would have to make sure to project out all b%d and t%d parameters,
1107 static isl_stat
extract_block_size(struct ppcg_kernel
*kernel
,
1108 __isl_take isl_union_set
*domain
)
1114 domain
= isl_union_set_intersect(domain
,
1115 isl_union_set_copy(kernel
->thread_filter
));
1116 block
= isl_union_set_params(domain
);
1117 block
= isl_set_from_params(block
);
1118 block
= isl_set_add_dims(block
, isl_dim_set
, kernel
->n_block
);
1119 for (i
= 0; i
< kernel
->n_block
; ++i
) {
1124 return isl_stat_error
;
1126 id
= isl_id_list_get_id(kernel
->thread_ids
, i
);
1127 pos
= isl_set_find_dim_by_id(block
, isl_dim_param
, id
);
1130 isl_die(isl_set_get_ctx(block
), isl_error_internal
,
1131 "missing constraints on thread identifier",
1132 block
= isl_set_free(block
));
1133 block
= isl_set_equate(block
, isl_dim_param
, pos
,
1136 nparam
= isl_set_dim(block
, isl_dim_param
);
1137 block
= isl_set_project_out(block
, isl_dim_param
, 0, nparam
);
1140 return isl_stat_error
;
1142 extract_fixed_size(block
, kernel
->block_dim
);
1147 struct ppcg_kernel
*ppcg_kernel_free(struct ppcg_kernel
*kernel
)
1154 isl_id_list_free(kernel
->block_ids
);
1155 isl_id_list_free(kernel
->thread_ids
);
1156 isl_multi_pw_aff_free(kernel
->grid_size
);
1157 isl_ast_expr_free(kernel
->grid_size_expr
);
1158 isl_set_free(kernel
->context
);
1159 isl_union_set_free(kernel
->core
);
1160 isl_union_set_free(kernel
->arrays
);
1161 isl_union_pw_multi_aff_free(kernel
->contraction
);
1162 isl_union_set_free(kernel
->expanded_domain
);
1163 isl_space_free(kernel
->space
);
1164 isl_ast_node_free(kernel
->tree
);
1165 isl_union_set_free(kernel
->block_filter
);
1166 isl_union_set_free(kernel
->thread_filter
);
1167 isl_union_pw_multi_aff_free(kernel
->copy_schedule
);
1168 isl_union_set_free(kernel
->sync_writes
);
1170 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1171 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1173 for (j
= 0; j
< array
->n_group
; ++j
)
1174 gpu_array_ref_group_free(array
->groups
[j
]);
1175 free(array
->groups
);
1177 isl_multi_pw_aff_free(array
->bound
);
1178 isl_ast_expr_free(array
->bound_expr
);
1180 free(kernel
->array
);
1182 for (i
= 0; i
< kernel
->n_var
; ++i
) {
1183 free(kernel
->var
[i
].name
);
1184 isl_vec_free(kernel
->var
[i
].size
);
1193 /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
1195 static void ppcg_kernel_free_wrap(void *user
)
1197 struct ppcg_kernel
*kernel
= user
;
1199 ppcg_kernel_free(kernel
);
1202 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
1203 struct ppcg_kernel_var
*var
)
1206 struct gpu_array_tile
*tile
;
1209 var
->array
= group
->array
;
1211 var
->type
= gpu_array_ref_group_type(group
);
1212 tile
= gpu_array_ref_group_tile(group
);
1214 p
= isl_printer_to_str(ctx
);
1215 p
= gpu_array_ref_group_print_name(group
, p
);
1216 var
->name
= isl_printer_get_str(p
);
1217 isl_printer_free(p
);
1219 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
1221 for (j
= 0; j
< group
->array
->n_index
; ++j
)
1222 var
->size
= isl_vec_set_element_val(var
->size
, j
,
1223 isl_val_copy(tile
->bound
[j
].size
));
1226 static int create_kernel_vars(struct ppcg_kernel
*kernel
)
1231 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1232 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1234 for (j
= 0; j
< array
->n_group
; ++j
) {
1235 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1236 enum ppcg_group_access_type type
;
1238 type
= gpu_array_ref_group_type(group
);
1239 if (type
!= ppcg_access_global
)
1245 kernel
->var
= isl_calloc_array(kernel
->ctx
, struct ppcg_kernel_var
, n
);
1250 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1251 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1253 for (j
= 0; j
< array
->n_group
; ++j
) {
1254 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1255 enum ppcg_group_access_type type
;
1257 type
= gpu_array_ref_group_type(group
);
1258 if (type
== ppcg_access_global
)
1260 create_kernel_var(kernel
->ctx
, group
, &kernel
->var
[n
]);
1268 /* Replace "pa" by the zero function defined over the universe domain
1269 * in the space of "pa".
1271 static __isl_give isl_pw_aff
*set_universally_zero(__isl_take isl_pw_aff
*pa
)
1276 space
= isl_space_domain(isl_pw_aff_get_space(pa
));
1277 isl_pw_aff_free(pa
);
1278 zero
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1280 return isl_pw_aff_from_aff(zero
);
1283 /* The sizes of the arrays on the host that have been computed by
1284 * extract_array_info may depend on the parameters. Use the extra
1285 * constraints on the parameters that are valid at "host_domain"
1286 * to simplify these expressions and store the results in kernel->array.
1288 * We only need these localized bounds for arrays that are accessed
1289 * by the current kernel. If we have found at least one reference group
1290 * then the array is accessed by the kernel.
1292 * The resulting sizes may be functions that are nowhere defined
1293 * in case the access function cannot possibly access anything inside
1294 * the kernel for some reason. If so, they are replaced by the zero
1295 * function. Since the access function cannot actually access anything,
1296 * there is no harm in printing the array sizes as zero.
1298 static void localize_bounds(struct ppcg_kernel
*kernel
,
1299 __isl_keep isl_set
*host_domain
)
1304 context
= isl_set_copy(host_domain
);
1305 context
= isl_set_params(context
);
1307 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1308 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1309 isl_multi_pw_aff
*bound
;
1312 if (local
->n_group
== 0)
1315 n_index
= local
->array
->n_index
;
1316 bound
= isl_multi_pw_aff_copy(local
->array
->bound
);
1318 for (j
= 0; j
< n_index
; ++j
) {
1322 pwaff
= isl_multi_pw_aff_get_pw_aff(bound
, j
);
1323 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
1324 empty
= isl_pw_aff_is_empty(pwaff
);
1326 pwaff
= isl_pw_aff_free(pwaff
);
1328 pwaff
= set_universally_zero(pwaff
);
1329 bound
= isl_multi_pw_aff_set_pw_aff(bound
, j
, pwaff
);
1332 local
->n_index
= n_index
;
1333 local
->bound
= bound
;
1335 isl_set_free(context
);
1338 /* Create the array of gpu_local_array_info structures "array"
1339 * inside "kernel". The number of elements in this array is
1340 * the same as the number of arrays in "prog".
1341 * Initialize the "array" field of each local array to point
1342 * to the corresponding array in "prog".
1344 static struct ppcg_kernel
*ppcg_kernel_create_local_arrays(
1345 struct ppcg_kernel
*kernel
, struct gpu_prog
*prog
)
1350 ctx
= isl_set_get_ctx(prog
->context
);
1351 kernel
->array
= isl_calloc_array(ctx
,
1352 struct gpu_local_array_info
, prog
->n_array
);
1354 return ppcg_kernel_free(kernel
);
1355 kernel
->n_array
= prog
->n_array
;
1357 for (i
= 0; i
< prog
->n_array
; ++i
)
1358 kernel
->array
[i
].array
= &prog
->array
[i
];
1363 /* Does "kernel" need to be passed an argument corresponding to array "i"?
1365 * The argument is only needed if the kernel accesses this device memory.
1367 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
)
1369 return kernel
->array
[i
].global
;
1372 /* Find the element in gen->stmt that has the given "id".
1373 * Return NULL if no such gpu_stmt can be found.
1375 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
1379 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
1380 if (id
== prog
->stmts
[i
].id
)
1384 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
1387 void ppcg_kernel_stmt_free(void *user
)
1389 struct ppcg_kernel_stmt
*stmt
= user
;
1394 switch (stmt
->type
) {
1395 case ppcg_kernel_copy
:
1396 isl_ast_expr_free(stmt
->u
.c
.index
);
1397 isl_ast_expr_free(stmt
->u
.c
.local_index
);
1399 case ppcg_kernel_domain
:
1400 isl_id_to_ast_expr_free(stmt
->u
.d
.ref2expr
);
1402 case ppcg_kernel_sync
:
1409 /* Return the gpu_stmt_access in the list "accesses" that corresponds
1412 static struct gpu_stmt_access
*find_access(struct gpu_stmt_access
*accesses
,
1413 __isl_keep isl_id
*ref_id
)
1415 struct gpu_stmt_access
*access
;
1417 for (access
= accesses
; access
; access
= access
->next
)
1418 if (access
->ref_id
== ref_id
)
1424 /* Return the index of the array called "name" in the list of arrays.
1426 static int find_array_index(struct ppcg_kernel
*kernel
, const char *name
)
1430 for (i
= 0; i
< kernel
->n_array
; ++i
)
1431 if (!strcmp(name
, kernel
->array
[i
].array
->name
))
1437 /* Internal data structure for the index and AST expression transformation
1438 * callbacks for pet_stmt_build_ast_exprs.
1440 * "kernel" is the kernel for which are computing AST expressions and
1441 * may be NULL if we are not inside a kernel.
1442 * "accesses" is the list of gpu_stmt_access in the statement.
1443 * "iterator_map" expresses the statement iterators in terms of
1444 * the AST loop iterators.
1445 * "sched2copy" expresses the outer copy_schedule_dim dimensions of
1446 * the kernel schedule in terms of the AST loop iterators and
1447 * may be NULL if we are not inside a kernel.
1449 * The following fields are set in transform_index and used in transform_expr.
1450 * "array" is the array that is being accessed.
1451 * "global" is set if the global array is accessed (rather than
1452 * shared/private memory).
1453 * "local_array" refers to information on the array specialized
1454 * to the current kernel.
1456 struct ppcg_transform_data
{
1457 struct ppcg_kernel
*kernel
;
1458 struct gpu_stmt_access
*accesses
;
1459 isl_pw_multi_aff
*iterator_map
;
1460 isl_pw_multi_aff
*sched2copy
;
1462 struct gpu_array_info
*array
;
1464 struct gpu_local_array_info
*local_array
;
1467 /* Return a pointer to the gpu_array_ref_group in "local"
1468 * that contains the reference "access".
1469 * Return NULL if no such group can be found.
1471 static struct gpu_array_ref_group
*find_ref_group(
1472 struct gpu_local_array_info
*local
, struct gpu_stmt_access
*access
)
1476 for (i
= 0; i
< local
->n_group
; ++i
) {
1477 struct gpu_array_ref_group
*group
= local
->groups
[i
];
1479 for (j
= 0; j
< group
->n_ref
; ++j
)
1480 if (group
->refs
[j
] == access
)
1487 /* Given an index expression "index" of the form
1491 * with F(A) either A or some subfield of A and L the AST loop iterators,
1492 * and a tiling "tiling" of the form
1496 * apply the tiling to the outer array in the index expression to obtain
1500 * If F(A) is some subfield of A, then separate the member access
1501 * into the base index expression and the field index expression,
1502 * apply the tiling to the base index expression and combine the result
1503 * with the field index expression.
1505 * If F(A) is A, then modify index to keep track of the iterators
1509 * and combine the result with the tiling to obtain a tiled index expression
1510 * in terms of the AST loop iterators
1514 static __isl_give isl_multi_pw_aff
*tile_outer(
1515 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_multi_pw_aff
*tiling
)
1517 isl_bool is_wrapping
;
1519 isl_multi_pw_aff
*mpa
;
1521 is_wrapping
= isl_multi_pw_aff_range_is_wrapping(index
);
1522 if (is_wrapping
< 0)
1525 isl_multi_pw_aff
*field
;
1527 field
= isl_multi_pw_aff_copy(index
);
1528 field
= isl_multi_pw_aff_range_factor_range(field
);
1529 index
= isl_multi_pw_aff_range_factor_domain(index
);
1530 index
= tile_outer(index
, tiling
);
1531 return isl_multi_pw_aff_range_product(index
, field
);
1534 space
= isl_space_domain(isl_multi_pw_aff_get_space(index
));
1535 space
= isl_space_map_from_set(space
);
1536 mpa
= isl_multi_pw_aff_identity(space
);
1537 index
= isl_multi_pw_aff_range_product(mpa
, index
);
1538 index
= isl_multi_pw_aff_pullback_multi_pw_aff(tiling
, index
);
1542 isl_multi_pw_aff_free(index
);
1543 isl_multi_pw_aff_free(tiling
);
1547 /* Index transformation callback for pet_stmt_build_ast_exprs.
1549 * "index" expresses the array indices in terms of statement iterators
1551 * We first reformulate "index" in terms of the AST loop iterators.
1552 * Then we check if we are accessing the global array or
1553 * a shared/private copy. In particular, if we are not inside a kernel
1554 * then we must be accessing a global array.
1555 * In the former case, we simply return
1556 * the updated index. If "index" is an affine expression rather
1557 * than an array access, then we also return the updated index here.
1559 * If no reference groups have been computed for the array,
1560 * then we can only be accessing the global array.
1562 * Otherwise, we apply the tiling to the index.
1563 * This tiling is of the form
1567 * where D corresponds to the outer tile->depth dimensions of
1568 * the kernel schedule.
1569 * The index is of the form
1573 * We update the tiling to refer to the AST loop iterators
1577 * and combine it with the index to obtain a tiled index expression in terms
1578 * of the AST loop iterators
1582 * Note that while the tiling applies directly to an outer array.
1583 * the index may refer to some subfield of this outer array.
1584 * In such cases, the result will refer to the same subfield of the tile.
1585 * That is, an index expression of the form L -> F(A) will be transformed
1586 * into an index expression of the form L -> F(T).
1588 static __isl_give isl_multi_pw_aff
*transform_index(
1589 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*ref_id
,
1592 struct ppcg_transform_data
*data
= user
;
1593 struct gpu_stmt_access
*access
;
1594 struct gpu_array_ref_group
*group
;
1595 struct gpu_array_tile
*tile
;
1596 isl_pw_multi_aff
*iterator_map
;
1601 isl_multi_pw_aff
*tiling
;
1602 isl_pw_multi_aff
*pma
;
1603 isl_multi_pw_aff
*mpa
;
1604 isl_pw_multi_aff
*sched2depth
;
1608 iterator_map
= isl_pw_multi_aff_copy(data
->iterator_map
);
1609 index
= isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
1614 access
= find_access(data
->accesses
, ref_id
);
1617 if (!isl_map_has_tuple_name(access
->access
, isl_dim_out
))
1620 name
= get_outer_array_name(access
->access
);
1621 i
= find_array_index(data
->kernel
, name
);
1623 isl_die(isl_multi_pw_aff_get_ctx(index
), isl_error_internal
,
1624 "cannot find array",
1625 return isl_multi_pw_aff_free(index
));
1626 data
->local_array
= &data
->kernel
->array
[i
];
1627 data
->array
= data
->local_array
->array
;
1629 group
= find_ref_group(data
->local_array
, access
);
1635 tile
= gpu_array_ref_group_tile(group
);
1636 data
->global
= !tile
;
1640 space
= isl_space_domain(isl_multi_aff_get_space(tile
->tiling
));
1641 space
= isl_space_range(isl_space_unwrap(space
));
1642 space
= isl_space_map_from_set(space
);
1643 pma
= isl_pw_multi_aff_identity(space
);
1644 sched2depth
= isl_pw_multi_aff_copy(data
->sched2copy
);
1645 dim
= isl_pw_multi_aff_dim(sched2depth
, isl_dim_out
);
1646 sched2depth
= isl_pw_multi_aff_drop_dims(sched2depth
, isl_dim_out
,
1647 tile
->depth
, dim
- tile
->depth
);
1648 pma
= isl_pw_multi_aff_product(sched2depth
, pma
);
1649 tiling
= isl_multi_pw_aff_from_multi_aff(
1650 isl_multi_aff_copy(tile
->tiling
));
1651 tiling
= isl_multi_pw_aff_pullback_pw_multi_aff(tiling
, pma
);
1653 index
= tile_outer(index
, tiling
);
1658 /* Dereference "expr" by adding an index [0].
1659 * The original "expr" is assumed not to have any indices.
1661 * If "expr" is a member access, then the dereferencing needs
1662 * to be applied to the structure argument of this member access.
1664 static __isl_give isl_ast_expr
*dereference(__isl_take isl_ast_expr
*expr
)
1667 isl_ast_expr
*arg0
, *res
;
1668 isl_ast_expr_list
*list
;
1670 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1672 return isl_ast_expr_free(expr
);
1673 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1674 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1677 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1678 arg
= dereference(arg
);
1679 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1680 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1684 isl_ast_expr_free(arg0
);
1686 ctx
= isl_ast_expr_get_ctx(expr
);
1687 res
= isl_ast_expr_from_val(isl_val_zero(ctx
));
1688 list
= isl_ast_expr_list_from_ast_expr(res
);
1689 res
= isl_ast_expr_get_op_arg(expr
, 0);
1690 res
= isl_ast_expr_access(res
, list
);
1691 isl_ast_expr_free(expr
);
1696 /* Linearize the index expression "expr" based on the array bounds
1699 * That is, transform expression
1701 * A[i_0][i_1]...[i_n]
1705 * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
1707 * where b_0, b_1, ..., b_n are the bounds on the array.
1709 * If the base of "expr" is a member access, then the linearization needs
1710 * to be applied to the structure argument of this member access.
1712 * In the base case, if "expr" has no arguments (other than the name of
1713 * the array), then we are passing an entire array to a function.
1714 * In this case, there is nothing to linearize.
1715 * Note that at this point an expression with no arguments can
1716 * only be an entire array because the scalar case and
1717 * the case of single struct are handled by the caller.
1719 * If the number of specified index expressions in "expr"
1720 * is smaller than the dimension of the accessed array,
1721 * then the missing i_j also do not appear in the linearized expression.
1722 * Furthermore, since such an expression does not refer to a single
1723 * element while the default linearized expression would refer to
1724 * a single element, we return the expression
1726 * A + (..((i_0 * b_1 + i_1) ... ) * b_l + i_l)
1728 * instead. Note that because of the special case handling above,
1729 * we can assume here that there is at least one index expression.
1731 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
1732 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
)
1737 isl_ast_expr_list
*list
;
1739 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1740 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1741 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1744 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1745 arg
= gpu_local_array_info_linearize_index(array
, arg
);
1746 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1747 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1751 isl_ast_expr_free(arg0
);
1753 if (isl_ast_expr_get_op_n_arg(expr
) == 1)
1756 n
= isl_ast_expr_get_op_n_arg(expr
);
1757 res
= isl_ast_expr_get_op_arg(expr
, 1);
1758 for (i
= 1; i
< array
->n_index
; ++i
) {
1759 isl_ast_expr
*expr_i
;
1761 expr_i
= isl_ast_expr_get_op_arg(array
->bound_expr
, 1 + i
);
1762 res
= isl_ast_expr_mul(res
, expr_i
);
1766 expr_i
= isl_ast_expr_get_op_arg(expr
, i
+ 1);
1767 res
= isl_ast_expr_add(res
, expr_i
);
1770 if (1 + array
->n_index
> n
) {
1771 res
= isl_ast_expr_add(isl_ast_expr_get_op_arg(expr
, 0), res
);
1773 list
= isl_ast_expr_list_from_ast_expr(res
);
1774 res
= isl_ast_expr_get_op_arg(expr
, 0);
1775 res
= isl_ast_expr_access(res
, list
);
1778 isl_ast_expr_free(expr
);
1783 /* AST expression transformation callback for pet_stmt_build_ast_exprs.
1785 * If the AST expression refers to an array that is not accessed
1786 * at all, then this means the value of the expression is not used,
1787 * so we might as well print zero (NULL pointer) instead.
1789 * If the AST expression refers to a global scalar that is not
1790 * a read-only scalar, then its address was passed to the kernel and
1791 * we need to dereference it.
1793 * If the AST expression refers to an access to a global array,
1794 * then we linearize the access exploiting the bounds in data->local_array.
1796 static __isl_give isl_ast_expr
*transform_expr(__isl_take isl_ast_expr
*expr
,
1797 __isl_keep isl_id
*id
, void *user
)
1799 struct ppcg_transform_data
*data
= user
;
1803 if (!data
->array
->accessed
) {
1806 ctx
= isl_ast_expr_get_ctx(expr
);
1807 isl_ast_expr_free(expr
);
1808 return isl_ast_expr_from_val(isl_val_zero(ctx
));
1810 if (gpu_array_is_read_only_scalar(data
->array
))
1814 if (data
->array
->n_index
== 0)
1815 return dereference(expr
);
1816 if (!data
->array
->linearize
)
1819 return gpu_local_array_info_linearize_index(data
->local_array
, expr
);
1822 /* This function is called for each instance of a user statement
1823 * in the kernel "kernel", identified by "gpu_stmt".
1824 * "kernel" may be NULL if we are not inside a kernel.
1826 * We attach a struct ppcg_kernel_stmt to the "node", containing
1827 * a computed AST expression for each access, through an annotation
1829 * These AST expressions are computed from iterator_map,
1830 * which expresses the domain
1831 * elements in terms of the generated loops, and sched2copy,
1832 * which expresses the outer copy_schedule_dim dimensions of
1833 * the kernel schedule computed by PPCG in terms of the generated loops.
1835 static __isl_give isl_ast_node
*create_domain_leaf(
1836 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1837 __isl_keep isl_ast_build
*build
, struct gpu_stmt
*gpu_stmt
)
1839 struct ppcg_transform_data data
;
1840 struct ppcg_kernel_stmt
*stmt
;
1843 isl_pw_multi_aff
*sched2copy
;
1845 isl_pw_multi_aff
*iterator_map
;
1846 isl_union_map
*schedule
;
1850 ctx
= isl_ast_node_get_ctx(node
);
1852 stmt
= isl_calloc_type(ctx
, struct ppcg_kernel_stmt
);
1854 return isl_ast_node_free(node
);
1856 schedule
= isl_ast_build_get_schedule(build
);
1857 map
= isl_map_reverse(isl_map_from_union_map(schedule
));
1858 iterator_map
= isl_pw_multi_aff_from_map(map
);
1860 sched2copy
= compute_sched_to_copy(kernel
,
1861 isl_pw_multi_aff_copy(iterator_map
));
1865 stmt
->type
= ppcg_kernel_domain
;
1866 stmt
->u
.d
.stmt
= gpu_stmt
;
1868 data
.kernel
= kernel
;
1869 data
.accesses
= stmt
->u
.d
.stmt
->accesses
;
1870 data
.iterator_map
= iterator_map
;
1871 data
.sched2copy
= sched2copy
;
1872 stmt
->u
.d
.ref2expr
= pet_stmt_build_ast_exprs(stmt
->u
.d
.stmt
->stmt
,
1873 build
, &transform_index
, &data
,
1874 &transform_expr
, &data
);
1876 isl_pw_multi_aff_free(iterator_map
);
1877 isl_pw_multi_aff_free(sched2copy
);
1879 id
= isl_id_alloc(ctx
, "user", stmt
);
1880 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1881 return isl_ast_node_set_annotation(node
, id
);
1884 /* This function is called for each statement node in the AST
1885 * for copying to or from shared/private memory.
1886 * Attach a pointer to a ppcg_kernel_stmt representing the copy
1887 * statement to the node.
1888 * The statement name is "read" or "write", depending on whether we are
1889 * reading from global memory or writing to global memory.
1891 * The schedule is of the form
1895 * where D corresponds to the outer tile->depth dimensions of
1896 * the kernel schedule, A to the global array and L to the outer
1897 * generated AST schedule.
1898 * We compute the inverse and strip off the type, resulting in
1902 * We combine this mapping with on the one hand the projection
1906 * and on the other hand the group tiling
1914 * and store the corresponding expressions in stmt->index and stmt->local_index,
1915 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
1916 * stmt->index is linearized if the global memory array is linearized.
1918 static __isl_give isl_ast_node
*create_access_leaf(struct ppcg_kernel
*kernel
,
1919 struct gpu_array_ref_group
*group
, __isl_take isl_ast_node
*node
,
1920 __isl_keep isl_ast_build
*build
)
1922 struct ppcg_kernel_stmt
*stmt
;
1923 struct gpu_array_tile
*tile
;
1928 isl_pw_multi_aff
*pma
, *pma2
;
1931 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1933 return isl_ast_node_free(node
);
1935 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
1936 type
= isl_map_get_tuple_name(access
, isl_dim_in
);
1937 stmt
->u
.c
.read
= !strcmp(type
, "read");
1938 access
= isl_map_reverse(access
);
1939 pma
= isl_pw_multi_aff_from_map(access
);
1940 pma
= isl_pw_multi_aff_reset_tuple_id(pma
, isl_dim_out
);
1942 space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
1943 space
= isl_space_unwrap(space
);
1944 pma2
= isl_pw_multi_aff_range_map(space
);
1945 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
,
1946 isl_pw_multi_aff_copy(pma
));
1947 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1948 if (group
->array
->linearize
)
1949 expr
= gpu_local_array_info_linearize_index(group
->local_array
,
1951 stmt
->u
.c
.index
= expr
;
1953 tile
= gpu_array_ref_group_tile(group
);
1954 pma2
= isl_pw_multi_aff_from_multi_aff(
1955 isl_multi_aff_copy(tile
->tiling
));
1956 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
, pma
);
1957 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1958 stmt
->u
.c
.local_index
= expr
;
1960 stmt
->u
.c
.array
= group
->array
;
1961 stmt
->u
.c
.local_array
= group
->local_array
;
1962 stmt
->type
= ppcg_kernel_copy
;
1964 id
= isl_id_alloc(kernel
->ctx
, "copy", stmt
);
1965 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1966 return isl_ast_node_set_annotation(node
, id
);
1969 /* Create a synchronization ppcg_kernel_stmt and
1970 * attach it to the node "node" representing the synchronization.
1972 static __isl_give isl_ast_node
*create_sync_leaf(
1973 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1974 __isl_keep isl_ast_build
*build
)
1976 struct ppcg_kernel_stmt
*stmt
;
1979 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1981 return isl_ast_node_free(node
);
1983 stmt
->type
= ppcg_kernel_sync
;
1984 id
= isl_id_alloc(kernel
->ctx
, "sync", stmt
);
1985 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1986 return isl_ast_node_set_annotation(node
, id
);
1989 /* Build AST expressions for the device array sizes of all arrays in "prog"
1990 * that require allocation on the device using "build", as well as
1991 * for the original array sizes of all arrays that need to be declared
1993 * "node" is freed in case of error.
1995 static __isl_give isl_ast_node
*build_array_bounds(
1996 __isl_take isl_ast_node
*node
, struct gpu_prog
*prog
,
1997 __isl_keep isl_ast_build
*build
)
2001 for (i
= 0; i
< prog
->n_array
; ++i
) {
2002 struct gpu_array_info
*array
= &prog
->array
[i
];
2003 isl_multi_pw_aff
*size
;
2006 if (!gpu_array_requires_device_allocation(array
))
2009 size
= isl_multi_pw_aff_copy(array
->bound
);
2010 expr
= ppcg_build_size_expr(size
, build
);
2011 array
->bound_expr
= expr
;
2013 return isl_ast_node_free(node
);
2016 for (i
= 0; i
< prog
->n_array
; ++i
) {
2017 struct gpu_array_info
*array
= &prog
->array
[i
];
2019 isl_multi_pw_aff
*size
;
2022 if (!array
->declare_local
)
2024 extent
= isl_set_copy(array
->declared_extent
);
2025 size
= ppcg_size_from_extent(extent
);
2026 expr
= ppcg_build_size_expr(size
, build
);
2027 array
->declared_size
= expr
;
2029 return isl_ast_node_free(node
);
2035 /* Internal data structure for at_domain.
2037 * "prog" represents the entire scop.
2038 * "kernel" points to the kernel to which the current schedule node
2039 * belongs. It is set by before_mark and reset by after_mark.
2040 * It may be NULL if we are outside any kernel.
2042 struct ppcg_at_domain_data
{
2043 struct gpu_prog
*prog
;
2044 struct ppcg_kernel
*kernel
;
2047 /* This function is called for each instance of a user statement
2048 * in the kernel. This may be one of the original user statements
2049 * or a statement introduced by PPCG.
2051 * We first check if the statement id corresponds to a gpu statement,
2052 * which indicates the statement is an original user statement. Any statement
2053 * that is not an original user statement has been introduced by PPCG and
2054 * requires special handling.
2056 * If the user statement is one of the original user statements, then we call
2057 * create_domain_leaf. If it is "init_device", then we call
2058 * build_array_bounds. Otherwise, we check if it is a copy or synchronization
2059 * statement and call the appropriate functions. Statements that copy an array
2060 * to/from the device do not need any further treatment.
2061 * Neither does "clear_device".
2063 static __isl_give isl_ast_node
*at_domain(__isl_take isl_ast_node
*node
,
2064 __isl_keep isl_ast_build
*build
, void *user
)
2066 struct ppcg_at_domain_data
*data
= user
;
2067 struct gpu_stmt
*gpu_stmt
;
2068 isl_ast_expr
*expr
, *arg
;
2074 expr
= isl_ast_node_user_get_expr(node
);
2075 arg
= isl_ast_expr_get_op_arg(expr
, 0);
2076 id
= isl_ast_expr_get_id(arg
);
2077 name
= isl_id_get_name(id
);
2078 p
= isl_id_get_user(id
);
2079 isl_ast_expr_free(expr
);
2080 isl_ast_expr_free(arg
);
2082 gpu_stmt
= find_stmt(data
->prog
, id
);
2083 is_sync
= gpu_tree_id_is_sync(id
, data
->kernel
);
2087 return create_domain_leaf(data
->kernel
, node
, build
, gpu_stmt
);
2089 if (!prefixcmp(name
, "to_device_") || !prefixcmp(name
, "from_device_"))
2091 if (!strcmp(name
, "init_device"))
2092 return build_array_bounds(node
, data
->prog
, build
);
2093 if (!strcmp(name
, "clear_device"))
2096 return isl_ast_node_free(node
);
2097 if (!strcmp(name
, "read") || !strcmp(name
, "write")) {
2098 struct gpu_array_ref_group
*group
= p
;
2099 return create_access_leaf(data
->kernel
, group
, node
, build
);
2102 isl_die(data
->prog
->ctx
, isl_error_internal
,
2103 "unknown statement type",
2104 return isl_ast_node_free(node
));
2105 return create_sync_leaf(data
->kernel
, node
, build
);
2108 /* Given a set of wrapped references "ref", return the corresponding
2109 * access relations based on the tagged access relations "tagged".
2111 * The elements of "ref" are of the form
2115 * with D an iteration domains and R a reference.
2116 * The elements of "tagged" are of the form
2122 * Extend "tagged" to include the iteration domain in the range, i.e.,
2124 * [D -> R] -> [D -> A]
2126 * apply the result to "ref" and then unwrap the resulting set
2127 * to obtain relations of the form
2131 static __isl_give isl_union_map
*wrapped_reference_to_access(
2132 __isl_take isl_union_set
*ref
, __isl_take isl_union_map
*tagged
)
2134 isl_union_map
*tag2access
;
2136 tag2access
= isl_union_map_copy(tagged
);
2137 tag2access
= isl_union_map_universe(tag2access
);
2138 tag2access
= isl_union_set_unwrap(isl_union_map_domain(tag2access
));
2139 tag2access
= isl_union_map_domain_map(tag2access
);
2140 tag2access
= isl_union_map_range_product(tag2access
, tagged
);
2142 ref
= isl_union_set_coalesce(ref
);
2143 ref
= isl_union_set_apply(ref
, tag2access
);
2145 return isl_union_set_unwrap(ref
);
2148 /* Given an access relation "access" from one or more array reference groups,
2149 * remove those reads if ("read" is 1) or writes (if "read" is 0)
2150 * that are only needed to communicate data within
2151 * the same iteration of "sched".
2152 * "tagged" contains all tagged access relations to all
2153 * the array reference groups accessed by "access" from statement
2154 * instances scheduled by "sched".
2156 * If the access is a read then it is either an element of
2158 * live_in union (range flow)
2160 * where live_in and flow may be overapproximations, or
2161 * it reads an uninitialized value (that is not live-in because
2162 * there is an intermediate kill) or it reads a value that was
2163 * written within the same (compound) statement instance.
2164 * If the access is a write then it is either an element of
2166 * live_out union (domain flow)
2168 * or it writes a value that is never read (and is not live-out
2169 * because of an intermediate kill) or only
2170 * within the same (compound) statement instance.
2171 * In both cases, the access relation is also a subset of
2172 * the group access relation.
2174 * The cases where an uninitialized value is read or a value is written
2175 * that is never read or where the dataflow occurs within a statement
2176 * instance are also considered local and may also be removed.
2178 * Essentially, we compute the intersection of "access" with either
2180 * live_in union (range non-local-flow)
2184 * live_out union (domain non-local-flow)
2186 * We first construct a relation "local"
2188 * [[D -> R] -> [D' -> R']]
2190 * of pairs of domain iterations accessing the reference group
2191 * and references in the group that are coscheduled by "sched".
2193 * If this relation does not intersect the dataflow dependences,
2194 * then there is nothing we can possibly remove, unless the dataflow
2195 * dependences themselves only relate a subset of the accesses.
2196 * In particular, the accesses may not be involved in any dataflow
2197 * dependences, either because they are uninitialized reads/dead writes
2198 * or because the dataflow occurs inside a statement instance.
2200 * Since the computation below may break up the access relation
2201 * into smaller pieces, we only perform the intersection with
2202 * the non-local dependent accesses if the local pairs
2203 * intersect the dataflow dependences. Otherwise, we intersect
2204 * with the universe of the non-local dependent accesses.
2205 * This should at least remove accesses from statements that
2206 * do not participate in any dependences.
2208 * In particular, we remove the "local" dataflow dependences from
2209 * the set of all dataflow dependences, or at least those
2210 * that may contribute to a domain/range that intersects
2211 * the domain of "access".
2212 * Note that if the potential dataflow dependences are an overapproximation
2213 * of the actual dataflow dependences, then the result remains an
2214 * overapproximation of the non-local dataflow dependences.
2215 * Copying to/from global memory is only needed for the references
2216 * in the domain/range of the result or for accesses that are live out/in
2217 * for the entire scop.
2219 * We therefore map the domain/range of the "external" relation
2220 * to the corresponding access relation and take the union with
2221 * the live out/in relation.
2223 static __isl_give isl_union_map
*remove_local_accesses(
2224 struct gpu_prog
*prog
, __isl_take isl_union_map
*tagged
,
2225 __isl_take isl_union_map
*access
, __isl_take isl_union_map
*sched
,
2229 isl_union_pw_multi_aff
*tagger
;
2230 isl_union_set
*domain
, *access_domain
;
2231 isl_union_map
*local
, *external
, *universe
;
2232 isl_union_set
*tag_set
;
2234 if (isl_union_map_is_empty(access
)) {
2235 isl_union_map_free(sched
);
2236 isl_union_map_free(tagged
);
2240 tagger
= isl_union_pw_multi_aff_copy(prog
->scop
->tagger
);
2241 domain
= isl_union_map_domain(isl_union_map_copy(tagged
));
2242 tagger
= isl_union_pw_multi_aff_intersect_domain(tagger
,
2243 isl_union_set_copy(domain
));
2244 sched
= isl_union_map_preimage_domain_union_pw_multi_aff(sched
, tagger
);
2246 local
= isl_union_map_apply_range(sched
,
2247 isl_union_map_reverse(isl_union_map_copy(sched
)));
2248 local
= isl_union_map_intersect(local
,
2249 isl_union_map_copy(prog
->scop
->tagged_dep_flow
));
2251 empty
= isl_union_map_is_empty(local
);
2253 external
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
2254 universe
= isl_union_map_universe(isl_union_map_copy(access
));
2255 access_domain
= isl_union_map_domain(universe
);
2256 domain
= isl_union_set_universe(domain
);
2257 universe
= isl_union_set_unwrap(domain
);
2258 universe
= isl_union_map_intersect_domain(universe
, access_domain
);
2259 domain
= isl_union_map_wrap(universe
);
2261 external
= isl_union_map_intersect_range(external
, domain
);
2263 external
= isl_union_map_intersect_domain(external
, domain
);
2264 external
= isl_union_map_intersect_params(external
,
2265 isl_set_copy(prog
->scop
->context
));
2266 external
= isl_union_map_subtract(external
, local
);
2269 tag_set
= isl_union_map_range(external
);
2270 external
= wrapped_reference_to_access(tag_set
, tagged
);
2271 external
= isl_union_map_union(external
,
2272 isl_union_map_copy(prog
->scop
->live_in
));
2274 tag_set
= isl_union_map_domain(external
);
2275 external
= wrapped_reference_to_access(tag_set
, tagged
);
2276 external
= isl_union_map_union(external
,
2277 isl_union_map_copy(prog
->scop
->live_out
));
2281 external
= isl_union_map_free(external
);
2283 external
= isl_union_map_universe(external
);
2285 access
= isl_union_map_intersect(access
, external
);
2290 /* Given an access relation "access" from "group", remove those reads
2291 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2292 * communicate data within the same iteration of the schedule "prefix"
2293 * at the position where the copying of the group is inserted.
2294 * That is, the output dimension of "prefix"
2295 * is equal to tile->depth.
2297 * Extract the tagged access relation of "group" and
2298 * then call remove_local_accesses.
2300 static __isl_give isl_union_map
*remove_local_accesses_group(
2301 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
2302 __isl_take isl_union_map
*access
, __isl_keep isl_union_map
*prefix
,
2305 isl_union_map
*sched
, *tagged
;
2307 if (isl_union_map_is_empty(access
))
2310 tagged
= group_tagged_access_relation(group
);
2311 sched
= isl_union_map_copy(prefix
);
2313 return remove_local_accesses(kernel
->prog
, tagged
, access
, sched
, read
);
2316 /* Build an access AST expression for the effective grid size using "build".
2317 * Store the result in kernel->grid_size_expr.
2319 static isl_stat
build_grid_size(struct ppcg_kernel
*kernel
,
2320 __isl_keep isl_ast_build
*build
)
2322 isl_multi_pw_aff
*size
;
2324 size
= isl_multi_pw_aff_copy(kernel
->grid_size
);
2325 size
= isl_multi_pw_aff_set_tuple_name(size
, isl_dim_out
, "grid");
2326 kernel
->grid_size_expr
= ppcg_build_size_expr(size
, build
);
2328 if (!kernel
->grid_size_expr
)
2329 return isl_stat_error
;
2333 /* Build access AST expressions for the localized array sizes using "build".
2334 * Store the result in local->bound_expr.
2335 * Only do this for arrays for which localized bounds have been computed.
2337 static isl_stat
build_local_array_sizes(struct ppcg_kernel
*kernel
,
2338 __isl_keep isl_ast_build
*build
)
2342 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2343 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
2344 isl_multi_pw_aff
*size
;
2346 if (local
->n_group
== 0)
2348 size
= isl_multi_pw_aff_copy(local
->bound
);
2349 local
->bound_expr
= ppcg_build_size_expr(size
, build
);
2350 if (!local
->bound_expr
)
2351 return isl_stat_error
;
2357 /* Build access AST expressions for the effective grid size and
2358 * the localized array sizes using "build".
2360 static isl_stat
build_grid_and_local_array_sizes(struct ppcg_kernel
*kernel
,
2361 __isl_keep isl_ast_build
*build
)
2363 if (build_grid_size(kernel
, build
) < 0)
2364 return isl_stat_error
;
2365 if (build_local_array_sizes(kernel
, build
) < 0)
2366 return isl_stat_error
;
2370 /* This function is called before the AST generator starts traversing
2371 * the schedule subtree of a node with mark "mark".
2373 * If the mark is called "kernel", store the kernel pointer in data->kernel
2374 * for use in at_domain and build AST expressions for the grid size and
2375 * the localized array sizes.
2377 static isl_stat
before_mark(__isl_keep isl_id
*mark
,
2378 __isl_keep isl_ast_build
*build
, void *user
)
2380 struct ppcg_at_domain_data
*data
= user
;
2383 return isl_stat_error
;
2384 if (!strcmp(isl_id_get_name(mark
), "kernel")) {
2385 data
->kernel
= isl_id_get_user(mark
);
2386 if (build_grid_and_local_array_sizes(data
->kernel
, build
) < 0)
2387 return isl_stat_error
;
2392 /* This function is called after the AST generator has finished traversing
2393 * the schedule subtree of a mark node. "node" points to the corresponding
2396 * If the mark is called "kernel", then replace "node" by a user node
2397 * that "calls" the kernel, representing the launch of the kernel.
2398 * The original "node" is stored inside the kernel object so that
2399 * it can be used to print the device code.
2400 * Note that this assumes that a kernel is only launched once.
2401 * Also clear data->kernel.
2403 static __isl_give isl_ast_node
*after_mark(__isl_take isl_ast_node
*node
,
2404 __isl_keep isl_ast_build
*build
, void *user
)
2409 isl_ast_expr_list
*list
;
2410 struct ppcg_kernel
*kernel
;
2411 struct ppcg_at_domain_data
*data
= user
;
2413 ctx
= isl_ast_node_get_ctx(node
);
2414 id
= isl_ast_node_mark_get_id(node
);
2416 return isl_ast_node_free(node
);
2417 if (strcmp(isl_id_get_name(id
), "kernel") || !data
->kernel
) {
2421 kernel
= data
->kernel
;
2422 data
->kernel
= NULL
;
2423 kernel
->space
= isl_ast_build_get_schedule_space(build
);
2424 kernel
->tree
= isl_ast_node_mark_get_node(node
);
2425 isl_ast_node_free(node
);
2427 expr
= isl_ast_expr_from_id(isl_id_copy(id
));
2428 list
= isl_ast_expr_list_alloc(ctx
, 0);
2429 expr
= isl_ast_expr_call(expr
, list
);
2430 node
= isl_ast_node_alloc_user(expr
);
2431 node
= isl_ast_node_set_annotation(node
, id
);
2436 static isl_bool
update_depth(__isl_keep isl_schedule_node
*node
, void *user
)
2441 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2442 return isl_bool_true
;
2443 node_depth
= isl_schedule_node_get_schedule_depth(node
);
2444 if (node_depth
> *depth
)
2445 *depth
= node_depth
;
2447 return isl_bool_false
;
2450 /* Use isl to generate code for both the host and the device
2452 * The device code is marked by "kernel" mark nodes in the schedule tree,
2453 * containing a pointer to a ppcg_kernel object.
2454 * The returned AST only contains the AST for the host code.
2455 * The ASTs for the device code are embedded in ppcg_kernel objects
2456 * attached to the leaf nodes that call "kernel".
2458 static __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
,
2459 __isl_take isl_schedule
*schedule
)
2461 struct ppcg_at_domain_data data
;
2462 isl_ast_build
*build
;
2464 isl_id_list
*iterators
;
2467 data
.prog
= gen
->prog
;
2471 if (isl_schedule_foreach_schedule_node_top_down(schedule
, &update_depth
,
2474 build
= isl_ast_build_alloc(gen
->prog
->ctx
);
2475 iterators
= ppcg_scop_generate_names(gen
->prog
->scop
, depth
, "c");
2476 build
= isl_ast_build_set_iterators(build
, iterators
);
2477 build
= isl_ast_build_set_at_each_domain(build
, &at_domain
, &data
);
2478 build
= isl_ast_build_set_before_each_mark(build
, &before_mark
, &data
);
2479 build
= isl_ast_build_set_after_each_mark(build
, &after_mark
, &data
);
2480 if (gen
->prog
->scop
->options
->debug
->dump_final_schedule
)
2481 isl_schedule_dump(schedule
);
2482 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2483 isl_ast_build_free(build
);
2488 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
2492 return isl_union_map_read_from_str(ctx
, str
);
2495 /* Can "node" be tiled and then mapped to block and thread identifiers?
2496 * That is, is it permutable with at least one coincident dimension?
2498 static int is_permutable(__isl_keep isl_schedule_node
*node
)
2503 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
2505 if (!isl_schedule_node_band_get_permutable(node
))
2507 if (isl_schedule_node_band_n_member(node
) < 1)
2509 if (!isl_schedule_node_band_member_get_coincident(node
, 0))
2515 /* A isl_schedule_foreach_schedule_node_top_down callback
2516 * for setting *any_permutable and aborting the search
2517 * if "node" is a permutable band with coincident dimensions.
2518 * Otherwise, continue searching.
2520 static isl_bool
set_permutable(__isl_keep isl_schedule_node
*node
, void *user
)
2522 int *any_permutable
= user
;
2525 permutable
= is_permutable(node
);
2527 return isl_bool_error
;
2529 return isl_bool_true
;
2531 *any_permutable
= 1;
2533 return isl_bool_error
;
2536 /* Does the subtree rooted at "node" have any suitably permutable band nodes?
2537 * That is, does it have any nodes that are permutable and that
2538 * have a least one coincident dimension?
2540 static int subtree_has_permutable_bands(__isl_keep isl_schedule_node
*node
)
2542 int any_parallelism
= 0;
2544 if (isl_schedule_node_foreach_descendant_top_down(node
, &set_permutable
,
2545 &any_parallelism
) < 0 &&
2549 return any_parallelism
;
2552 /* Does "schedule" contain any permutable band with at least one coincident
2555 static int has_any_permutable_node(__isl_keep isl_schedule
*schedule
)
2557 isl_schedule_node
*root
;
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
)
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
)
2752 ctx
= isl_schedule_node_get_ctx(node
);
2753 space
= isl_schedule_node_band_get_space(node
);
2754 n
= isl_schedule_node_band_n_member(node
);
2755 mv
= isl_multi_val_zero(space
);
2756 for (i
= 0; i
< n
; ++i
) {
2759 v
= isl_val_int_from_si(ctx
, tile_size
[i
]);
2760 mv
= isl_multi_val_set_val(mv
, i
, v
);
2766 /* Replace the partial schedule S of the band node "node" by
2774 * if scale_tile_loops is set, with f the integers in "factor".
2775 * The list that "factor" points to is assumed to contain at least
2776 * as many elements as the number of members in the band.
2778 static __isl_give isl_schedule_node
*snap_band_to_sizes(
2779 __isl_take isl_schedule_node
*node
, int *factor
,
2780 struct ppcg_options
*options
)
2784 mv
= construct_band_tiles_sizes(node
, factor
);
2785 node
= isl_schedule_node_band_scale_down(node
, isl_multi_val_copy(mv
));
2786 if (options
->scale_tile_loops
)
2787 node
= isl_schedule_node_band_scale(node
,
2788 isl_multi_val_copy(mv
));
2789 isl_multi_val_free(mv
);
2794 /* Tile "band" with tile size specified by "sizes".
2796 * Since the tile loops will be mapped to block ids, we forcibly
2797 * turn off tile loop scaling. We may want to enable tile loop scaling
2798 * at some later point, but then we would have to support the detection
2799 * of strides during the mapping to block ids.
2800 * Similarly, since the point loops will be mapped to thread ids,
2801 * we forcibly shift the point loops so that they start at zero.
2803 static __isl_give isl_schedule_node
*tile_band(
2804 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2806 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
2810 scale_tile
= isl_options_get_tile_scale_tile_loops(ctx
);
2811 isl_options_set_tile_scale_tile_loops(ctx
, 0);
2812 shift_point
= isl_options_get_tile_shift_point_loops(ctx
);
2813 isl_options_set_tile_shift_point_loops(ctx
, 1);
2815 node
= isl_schedule_node_band_tile(node
, sizes
);
2817 isl_options_set_tile_scale_tile_loops(ctx
, scale_tile
);
2818 isl_options_set_tile_shift_point_loops(ctx
, shift_point
);
2823 /* Extract the set of parameter values and outer schedule dimensions
2824 * for which any statement instance
2825 * in the kernel inserted at "node" needs to be executed.
2826 * Intersect the set of parameter values derived from the host schedule
2827 * relation with the context of "prog".
2829 static __isl_give isl_set
*extract_context(__isl_keep isl_schedule_node
*node
,
2830 struct gpu_prog
*prog
)
2832 isl_union_map
*schedule
;
2833 isl_union_set
*schedule_domain
;
2837 schedule
= isl_schedule_node_get_prefix_schedule_relation(node
);
2838 schedule_domain
= isl_union_map_range(schedule
);
2839 empty
= isl_union_set_is_empty(schedule_domain
);
2841 isl_union_set_free(schedule_domain
);
2848 space
= isl_union_set_get_space(schedule_domain
);
2849 isl_union_set_free(schedule_domain
);
2850 space
= isl_space_set_from_params(space
);
2851 depth
= isl_schedule_node_get_schedule_depth(node
);
2852 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
2853 context
= isl_set_empty(space
);
2855 context
= isl_set_from_union_set(schedule_domain
);
2857 context
= isl_set_intersect_params(context
,
2858 isl_set_copy(prog
->context
));
2863 /* Return the set of outer array elements accessed by
2864 * by the statement instances in "domain" in "prog".
2865 * The instances in "domain" are those that appear
2866 * in the domains of the access relations in "prog".
2868 static __isl_give isl_union_set
*accessed_by_domain(
2869 __isl_take isl_union_set
*domain
, struct gpu_prog
*prog
)
2871 isl_union_map
*access
;
2872 isl_union_set
*arrays
;
2874 access
= isl_union_map_union(isl_union_map_copy(prog
->read
),
2875 isl_union_map_copy(prog
->may_write
));
2876 access
= isl_union_map_intersect_domain(access
, domain
);
2877 arrays
= isl_union_map_range(access
);
2878 arrays
= isl_union_set_apply(arrays
,
2879 isl_union_map_copy(prog
->to_outer
));
2884 /* Return the number of outer band members of the band node "node"
2885 * that are marked coincident.
2887 static int n_outer_coincidence(__isl_keep isl_schedule_node
*node
)
2891 n
= isl_schedule_node_band_n_member(node
);
2893 for (i
= 0; i
< n
; ++i
)
2894 if (!isl_schedule_node_band_member_get_coincident(node
, i
))
2900 /* If the band node "node" has more than "n" members, then split off
2901 * the first "n" of them.
2903 static __isl_give isl_schedule_node
*split_band(
2904 __isl_take isl_schedule_node
*node
, int n
)
2908 dim
= isl_schedule_node_band_n_member(node
);
2910 node
= isl_schedule_node_band_split(node
, n
);
2915 /* Scale a band node that may have been split by split_band.
2916 * "sizes" are the scaling factors for the original node.
2917 * "node" either points to the original band node, or the outer
2918 * of the two pieces after splitting.
2920 * If the number of elements in "node" is smaller than the number of
2921 * elements in "sizes", then some splitting has occurred and we split
2922 * "sizes" in the same way.
2924 static __isl_give isl_schedule_node
*scale_band(
2925 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2929 n
= isl_multi_val_dim(sizes
, isl_dim_set
);
2930 dim
= isl_schedule_node_band_n_member(node
);
2932 isl_multi_val
*sizes2
;
2934 sizes2
= isl_multi_val_copy(sizes
);
2935 sizes
= isl_multi_val_drop_dims(sizes
,
2936 isl_dim_set
, dim
, n
- dim
);
2937 sizes2
= isl_multi_val_drop_dims(sizes2
, isl_dim_set
, 0, dim
);
2938 node
= isl_schedule_node_child(node
, 0);
2939 node
= isl_schedule_node_band_scale(node
, sizes2
);
2940 node
= isl_schedule_node_parent(node
);
2943 return isl_schedule_node_band_scale(node
, sizes
);
2946 /* Return an isl_multi_aff, with as elements the parameters in "space"
2947 * that have the names specified by the elements in "names".
2948 * If (some of) these parameters do not already appear in "space",
2949 * then they are added first.
2951 static __isl_give isl_multi_aff
*parameter_vector(__isl_take isl_space
*space
,
2952 __isl_keep isl_id_list
*names
)
2955 isl_local_space
*ls
;
2959 space
= isl_space_free(space
);
2961 n
= isl_id_list_n_id(names
);
2962 for (i
= 0; i
< n
; ++i
) {
2966 id
= isl_id_list_get_id(names
, i
);
2967 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2972 pos
= isl_space_dim(space
, isl_dim_param
);
2973 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2974 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2976 ma
= isl_multi_aff_zero(isl_space_copy(space
));
2977 ls
= isl_local_space_from_space(isl_space_domain(space
));
2978 for (i
= 0; i
< n
; ++i
) {
2983 id
= isl_id_list_get_id(names
, i
);
2984 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2986 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
2987 isl_dim_param
, pos
);
2988 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2990 isl_local_space_free(ls
);
2995 /* Return constraints on the domain elements that equate a sequence of
2996 * parameters called "names", to the partial schedule
2997 * of "node" modulo the integers in "size".
2998 * The number of elements in the array "size" should be equal
2999 * to the number of elements in "names".
3000 * The number of members of the band node "node" should be smaller
3001 * than or equal to this number. If it is smaller, then the first
3002 * elements of "names" are equated to zero.
3004 static __isl_give isl_union_set
*set_schedule_modulo(
3005 __isl_keep isl_schedule_node
*node
, __isl_keep isl_id_list
*names
,
3011 isl_multi_union_pw_aff
*mupa
, *mupa2
;
3013 isl_union_set
*domain
;
3017 n
= isl_id_list_n_id(names
);
3019 return isl_schedule_node_get_universe_domain(node
);
3020 n_zero
= n
- isl_schedule_node_band_n_member(node
);
3022 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
3023 mv
= construct_band_tiles_sizes(node
, size
+ n_zero
);
3024 mupa
= isl_multi_union_pw_aff_mod_multi_val(mupa
, mv
);
3026 space
= isl_multi_union_pw_aff_get_space(mupa
);
3027 space
= isl_space_params(space
);
3028 space
= isl_space_set_from_params(space
);
3029 space
= isl_space_add_dims(space
, isl_dim_set
, n_zero
);
3030 ma
= isl_multi_aff_zero(space
);
3032 domain
= isl_schedule_node_get_universe_domain(node
);
3033 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(
3034 isl_union_set_copy(domain
), ma
);
3035 mupa
= isl_multi_union_pw_aff_range_product(mupa2
, mupa
);
3037 space
= isl_multi_union_pw_aff_get_space(mupa
);
3038 ma
= parameter_vector(space
, names
);
3040 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(domain
, ma
);
3041 mupa
= isl_multi_union_pw_aff_sub(mupa
, mupa2
);
3043 return isl_multi_union_pw_aff_zero_union_set(mupa
);
3046 /* Insert a context node at "node" introducing the block and thread
3047 * identifiers along with their bounds, which are stored in kernel->grid_size
3048 * and kernel->block_dim.
3049 * Note that the bounds on the block identifiers may implicitly impose
3050 * constraints on the parameters. A guard needs to be inserted
3051 * in the schedule tree to ensure that those bounds hold at "node".
3052 * This guard is inserted in insert_guard.
3054 static __isl_give isl_schedule_node
*insert_context(struct ppcg_kernel
*kernel
,
3055 __isl_take isl_schedule_node
*node
)
3059 context
= isl_set_universe(isl_set_get_space(kernel
->context
));
3061 context
= add_bounded_parameters_dynamic(context
,
3062 kernel
->grid_size
, kernel
->block_ids
);
3063 context
= add_bounded_parameters(context
,
3064 kernel
->block_dim
, kernel
->thread_ids
);
3066 node
= isl_schedule_node_insert_context(node
, context
);
3071 /* Insert a guard that eliminates kernel launches where the kernel
3072 * obviously does not have any work to do.
3074 * In particular, eliminate kernel launches where there are obviously
3076 * Use the same block size constraints that are used to create the context
3077 * to ensure that all constraints implicit in the constructed context
3078 * are imposed by the guard.
3080 * Additionally, add other constraints that are valid
3081 * for each executed instance ("context"), as long as this does not result
3084 static __isl_give isl_schedule_node
*insert_guard(
3085 __isl_take isl_schedule_node
*node
, __isl_keep isl_set
*context
,
3086 __isl_keep isl_multi_pw_aff
*size
, struct ppcg_scop
*scop
)
3092 guard
= isl_set_copy(context
);
3093 guard
= isl_set_compute_divs(guard
);
3094 guard
= isl_set_from_basic_set(isl_set_simple_hull(guard
));
3096 nparam
= isl_set_dim(guard
, isl_dim_param
);
3097 n
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
3098 ids
= ppcg_scop_generate_names(scop
, n
, "__ppcg_tmp");
3099 guard
= add_bounded_parameters_dynamic(guard
, size
, ids
);
3100 isl_id_list_free(ids
);
3101 guard
= isl_set_project_out(guard
, isl_dim_param
, nparam
, n
);
3103 node
= isl_schedule_node_insert_guard(node
, guard
);
3108 /* Does any array reference group mapping require the band that is mapped
3109 * to threads to be unrolled?
3111 static int kernel_requires_unroll(struct ppcg_kernel
*kernel
)
3115 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3116 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3118 for (j
= 0; j
< array
->n_group
; ++j
) {
3119 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3120 if (gpu_array_ref_group_requires_unroll(group
))
3128 /* Mark the given band node "node" for unrolling by the AST generator and
3129 * then sink it to the leaves of the schedule tree.
3130 * All dimensions of "node" are assumed to be coincident, such that this
3131 * sinking is a valid operation.
3133 static __isl_give isl_schedule_node
*unroll(__isl_take isl_schedule_node
*node
)
3135 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
3137 node
= isl_schedule_node_band_sink(node
);
3142 /* Insert a synchronization node in the schedule tree of "node"
3143 * after the core computation of "kernel" at the level of the band
3144 * that is mapped to threads, except if that level is equal to
3145 * that of the band that is mapped to blocks or if there are no writes
3146 * to global or shared memory in the core computation that require
3148 * If there are any writes to shared memory and the shared memory
3149 * copying is performed at the same level, then synchronization
3150 * is needed between the core and the copying anyway, so we might
3151 * as well add it here. If the copying is performed at a higher
3152 * level, then different iterations of intermediate schedule dimensions
3153 * may have a different mapping from between shared memory elements and
3154 * threads, such that synchronization is required after the core.
3155 * "node" is assumed to point to the kernel node.
3157 static __isl_give isl_schedule_node
*add_sync(struct ppcg_kernel
*kernel
,
3158 __isl_take isl_schedule_node
*node
)
3163 need_sync
= any_global_or_shared_sync_writes(kernel
);
3165 return isl_schedule_node_free(node
);
3169 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3171 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3172 if (kernel_depth
== isl_schedule_node_get_schedule_depth(node
))
3173 return gpu_tree_move_up_to_kernel(node
);
3175 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3177 node
= gpu_tree_move_up_to_kernel(node
);
3182 /* Return a read ("read" is 1) or write access relation for "group"
3183 * with those accesses removed that are only needed to communicate data
3184 * within the subtree of the schedule rooted at "node".
3185 * Furthermore, include the prefix schedule at "node".
3186 * That is, return a relation of the form
3190 * with D the outer schedule dimensions at "node".
3192 static __isl_give isl_union_map
*anchored_non_local_accesses(
3193 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3194 __isl_take isl_schedule_node
*node
, int read
)
3196 isl_union_map
*access
;
3197 isl_union_map
*prefix
;
3199 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
3200 access
= gpu_array_ref_group_access_relation(group
, read
, !read
);
3201 access
= remove_local_accesses_group(kernel
, group
, access
, prefix
,
3203 access
= isl_union_map_range_product(prefix
, access
);
3208 /* Given an array reference group "group", create a mapping
3210 * read[D -> A] -> [D -> A]
3212 * if "read" is set or
3214 * write[D -> A] -> [D -> A]
3216 * if "read" is not set.
3217 * D corresponds to the outer tile->depth dimensions of
3218 * the kernel schedule.
3220 static __isl_give isl_multi_aff
*create_from_access(isl_ctx
*ctx
,
3221 struct gpu_array_ref_group
*group
, int read
)
3223 struct gpu_array_tile
*tile
;
3227 tile
= gpu_array_ref_group_tile(group
);
3228 space
= isl_space_copy(group
->array
->space
);
3229 space
= isl_space_from_range(space
);
3230 space
= isl_space_add_dims(space
, isl_dim_in
, tile
->depth
);
3231 space
= isl_space_wrap(space
);
3232 space
= isl_space_map_from_set(space
);
3234 id
= isl_id_alloc(ctx
, read
? "read" : "write", group
);
3235 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
3237 return isl_multi_aff_identity(space
);
3240 /* If any writes in "group" require synchronization, then make sure
3241 * that there is a synchronization node for "kernel" after the node
3242 * following "node" in a sequence.
3244 * If "shared" is set and no synchronization is needed for
3245 * the writes to global memory, then add synchronization before
3246 * the kernel to protect shared memory from being overwritten
3247 * by the next iteration of the core computation.
3248 * No additional synchronization is needed to protect against
3249 * the next copy into shared memory because each element of
3250 * the shared memory tile is always copied by the same thread.
3252 static __isl_give isl_schedule_node
*add_group_write_sync(
3253 __isl_take isl_schedule_node
*node
, struct ppcg_kernel
*kernel
,
3254 struct gpu_array_ref_group
*group
, int shared
)
3258 need_sync
= any_sync_writes_in_group(kernel
, group
);
3260 return isl_schedule_node_free(node
);
3262 node
= isl_schedule_node_parent(node
);
3263 node
= isl_schedule_node_next_sibling(node
);
3264 node
= isl_schedule_node_child(node
, 0);
3265 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3266 } else if (shared
) {
3267 struct gpu_array_tile
*tile
;
3269 tile
= gpu_array_ref_group_tile(group
);
3270 node
= isl_schedule_node_parent(node
);
3271 node
= isl_schedule_node_parent(node
);
3272 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
,
3274 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3280 /* Add copy statements to the schedule tree of "node"
3281 * for reading from global memory to private memory (if "read" is set) or
3282 * for writing back from private memory to global memory
3283 * (if "read" is not set) for the array reference group "group" that
3284 * is mapped to private memory.
3285 * On input, "node" points to the kernel node, and it is moved
3286 * back there on output.
3288 * The copies are performed in the order of the array elements.
3289 * The copy statement instances include a reference to the outer
3290 * tile->depth dimensions of the kernel schedule for ease of
3291 * combining them with the group tiling.
3293 * That is, the extra schedule is of the form
3297 * where D corresponds to the outer tile->depth dimensions of
3298 * the kernel schedule and A to the global array.
3299 * This schedule is unrolled because registers are not addressable.
3301 * The copying is inserted in the schedule tree through an extension
3306 * where the extra domain elements type[D -> A] are those accessed
3308 * A filter is inserted on type[D -> A] to ensure that the element
3309 * is read/written by the same thread that needs the element.
3310 * This filter is obtained by applying
3314 * to the thread filter for the core statements.
3316 * The extension is inserted before the core computation in case of a read
3317 * and after the core computation in case of a write.
3318 * In the latter case, we also make sure that there is a synchronization
3319 * node after the write to global memory, unless this write is performed
3320 * at the outer level of the kernel.
3321 * In principle, this synchronization could be inserted higher
3322 * in the schedule tree depending on where the corresponding reads
3323 * from global memory are performed.
3325 static __isl_give isl_schedule_node
*add_copies_group_private(
3326 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3327 __isl_take isl_schedule_node
*node
, int read
)
3329 struct gpu_array_tile
*tile
;
3330 isl_union_map
*access
;
3331 isl_union_set
*domain
;
3333 isl_multi_aff
*from_access
;
3334 isl_multi_pw_aff
*mpa
;
3335 isl_multi_union_pw_aff
*mupa
;
3336 isl_schedule_node
*graft
;
3337 isl_union_set
*filter
;
3341 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3342 tile
= gpu_array_ref_group_tile(group
);
3343 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3345 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3346 empty
= isl_union_map_is_empty(access
);
3347 if (empty
< 0 || empty
) {
3348 isl_union_map_free(access
);
3350 return isl_schedule_node_free(node
);
3351 return gpu_tree_move_up_to_kernel(node
);
3354 group
->array
->global
= 1;
3355 group
->local_array
->global
= 1;
3357 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3358 space
= isl_space_domain(isl_multi_aff_get_space(from_access
));
3359 access
= isl_union_map_preimage_range_multi_aff(access
, from_access
);
3361 filter
= isl_union_set_copy(kernel
->thread_filter
);
3362 filter
= isl_union_set_apply(filter
, isl_union_map_copy(access
));
3363 filter
= isl_union_set_detect_equalities(filter
);
3364 filter
= isl_union_set_coalesce(filter
);
3366 domain
= isl_union_map_range(access
);
3367 access
= isl_union_set_wrapped_domain_map(domain
);
3368 access
= isl_union_map_reverse(access
);
3369 access
= isl_union_map_coalesce(access
);
3370 graft
= isl_schedule_node_from_extension(access
);
3372 space
= isl_space_map_from_set(space
);
3373 mpa
= isl_multi_pw_aff_identity(space
);
3374 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
3375 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3377 graft
= isl_schedule_node_child(graft
, 0);
3378 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3379 graft
= unroll(graft
);
3381 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3383 graft
= isl_schedule_node_parent(graft
);
3386 node
= isl_schedule_node_graft_before(node
, graft
);
3388 node
= isl_schedule_node_graft_after(node
, graft
);
3389 if (kernel_depth
< tile
->depth
)
3390 node
= add_group_write_sync(node
, kernel
, group
, 0);
3393 node
= gpu_tree_move_up_to_kernel(node
);
3398 /* Add copy statements to the schedule tree of "node"
3399 * for reading from global memory to shared memory (if "read" is set) or
3400 * for writing back from shared memory to global memory
3401 * (if "read" is not set) for the array reference group "group" that
3402 * is mapped to shared memory.
3403 * On input, "node" points to the kernel node, and it is moved
3404 * back there on output.
3406 * The copies are performed in the order of the corresponding shared
3408 * The copy statement instances include a reference to the outer
3409 * tile->depth dimensions of the kernel schedule for ease of
3410 * combining them with the group tiling.
3412 * If we are performing a read from global memory to shared memory and
3413 * if the array involved is not a scalar, then we copy
3414 * the entire tile to shared memory. This may result in some extra
3415 * elements getting copied, but it should lead to simpler code
3416 * (which means that fewer registers may be needed) and less divergence.
3418 * Otherwise, we only copy the elements that will be read or have been written
3421 * That is, the extra schedule is of the form
3425 * where D corresponds to the outer tile->depth dimensions of
3426 * the kernel schedule, A to the global array and T is the corresponding
3427 * shared memory tile.
3429 * The copying is inserted in the schedule tree through an extension
3434 * where the extra domain elements type[D -> A] are those accessed
3435 * by the group. In the case of read from a non-scalar, this set
3436 * is replaced by the entire shared memory tile.
3438 * A filter is inserted on type[D -> A] to map the copy instances
3439 * to the threads. In particular, the thread identifiers are
3440 * equated to the position inside the shared memory tile (T)
3441 * modulo the block size.
3442 * We try to align the innermost tile dimension with the innermost
3443 * thread identifier (x) as a heuristic to improve coalescing.
3444 * In particular, if the dimension of the tile is greater than
3445 * the dimension of the block, then the schedule mapping to the tile
3446 * is broken up into two pieces and the filter is applied to the inner part.
3447 * If, on the other hand, the dimension of the tile is smaller than
3448 * the dimension of the block, then the initial thread identifiers
3449 * are equated to zero and the remaining thread identifiers are
3450 * matched to the memory tile.
3452 * The extension is inserted before the core computation in case of a read
3453 * and after the core computation in case of a write.
3454 * In the case of a read, we first need to make sure there is some
3455 * synchronization before the core computation such that we can put the read
3456 * from global memory to shared memory before that synchronization.
3457 * This ensures that all threads have finished copying into shared memory
3458 * before the shared memory is used.
3459 * We also need to make sure that there is a synchronization node after
3460 * the core computation to ensure that the next load into shared memory
3461 * only happens after all data has been used. There is no need for
3462 * this synchronization if we are at the outer level since then there
3463 * won't be a next load.
3464 * In the case of a write, we need to make sure there is some synchronization
3465 * after the core computation such taht we can put the write from shared
3466 * memory to global memory after that synchronization.
3467 * Unless we are at the outer level, we also need a synchronization node
3468 * after the write to ensure the data is saved to global memory
3469 * before the next iteration write to the same shared memory.
3470 * It also makes sure the data has arrived in global memory before
3471 * it is read in a subsequent iteration.
3473 static __isl_give isl_schedule_node
*add_copies_group_shared(
3474 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3475 __isl_take isl_schedule_node
*node
, int read
)
3477 struct gpu_array_tile
*tile
;
3478 isl_union_map
*access
;
3479 isl_union_set
*domain
;
3481 isl_multi_aff
*from_access
;
3482 isl_multi_pw_aff
*mpa
;
3483 isl_multi_union_pw_aff
*mupa
;
3484 isl_schedule_node
*graft
;
3485 isl_union_set
*filter
;
3490 tile
= gpu_array_ref_group_tile(group
);
3491 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3492 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3494 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3495 empty
= isl_union_map_is_empty(access
);
3496 if (empty
< 0 || empty
) {
3497 isl_union_map_free(access
);
3499 return isl_schedule_node_free(node
);
3500 return gpu_tree_move_up_to_kernel(node
);
3503 group
->array
->global
= 1;
3504 group
->local_array
->global
= 1;
3506 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3508 ma
= isl_multi_aff_copy(tile
->tiling
);
3509 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3510 isl_multi_aff_copy(from_access
));
3511 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3512 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3514 domain
= isl_union_map_range(access
);
3516 if (read
&& !gpu_array_is_scalar(group
->array
)) {
3518 isl_union_set_free(domain
);
3519 map
= group_tile(group
);
3520 domain
= isl_union_set_from_set(isl_map_wrap(map
));
3523 domain
= isl_union_set_preimage_multi_aff(domain
, from_access
);
3524 access
= isl_union_set_wrapped_domain_map(domain
);
3525 access
= isl_union_map_reverse(access
);
3526 access
= isl_union_map_coalesce(access
);
3527 graft
= isl_schedule_node_from_extension(access
);
3529 graft
= isl_schedule_node_child(graft
, 0);
3531 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3533 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0) {
3534 graft
= isl_schedule_node_band_split(graft
,
3535 tile
->n
- kernel
->n_block
);
3536 graft
= isl_schedule_node_child(graft
, 0);
3538 if (tile
->n
< kernel
->n_block
)
3539 skip
= kernel
->n_block
- tile
->n
;
3542 filter
= set_schedule_modulo(graft
, kernel
->thread_ids
,
3544 if (!kernel
->options
->wrap
)
3545 graft
= snap_band_to_sizes(graft
, kernel
->block_dim
+ skip
,
3547 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0)
3548 graft
= isl_schedule_node_parent(graft
);
3549 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3551 while (graft
&& isl_schedule_node_has_parent(graft
))
3552 graft
= isl_schedule_node_parent(graft
);
3555 if (kernel_depth
< tile
->depth
)
3556 node
= gpu_tree_ensure_sync_after_core(node
, kernel
);
3557 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3558 node
= isl_schedule_node_graft_before(node
, graft
);
3560 node
= gpu_tree_move_right_to_sync(node
, kernel
);
3561 node
= isl_schedule_node_graft_after(node
, graft
);
3562 if (kernel_depth
< tile
->depth
)
3563 node
= add_group_write_sync(node
, kernel
, group
, 1);
3566 node
= gpu_tree_move_up_to_kernel(node
);
3571 /* Check whether the array reference group "group" is mapped to
3572 * private or shared memory and, if so,
3573 * add copy statements to the schedule tree of "node"
3574 * for reading from global memory to private or shared memory
3575 * (if "read" is set) or for writing back from private or shared memory
3576 * to global memory (if "read" is not set) for this group.
3577 * On input, "node" points to the kernel node, and it is moved
3578 * back there on output.
3580 static __isl_give isl_schedule_node
*add_copies_group(
3581 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3582 __isl_take isl_schedule_node
*node
, int read
)
3584 enum ppcg_group_access_type type
;
3586 type
= gpu_array_ref_group_type(group
);
3587 if (type
== ppcg_access_private
)
3588 return add_copies_group_private(kernel
, group
, node
, read
);
3589 if (type
== ppcg_access_shared
)
3590 return add_copies_group_shared(kernel
, group
, node
, read
);
3594 /* For each array reference group that is mapped to private or shared memory,
3595 * add copy statements to the schedule tree of "node"
3596 * for reading from global memory to private or shared memory
3597 * and for writing back.
3598 * On input, "node" points to the kernel node, and it is moved
3599 * back there on output.
3601 static __isl_give isl_schedule_node
*add_copies(struct ppcg_kernel
*kernel
,
3602 __isl_take isl_schedule_node
*node
)
3606 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3607 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3609 for (j
= 0; j
< array
->n_group
; ++j
) {
3610 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3612 node
= add_copies_group(kernel
, group
, node
, 1);
3615 node
= add_copies_group(kernel
, group
, node
, 0);
3624 /* Mark all dimensions in the current band node atomic.
3626 static __isl_give isl_schedule_node
*atomic(__isl_take isl_schedule_node
*node
)
3628 return ppcg_set_schedule_node_type(node
, isl_ast_loop_atomic
);
3631 /* Mark "node" atomic, if it is a band node.
3632 * Do the same for all ancestors.
3633 * Return a pointer to "node" (in the updated schedule tree).
3635 static __isl_give isl_schedule_node
*atomic_ancestors(
3636 __isl_take isl_schedule_node
*node
)
3642 if (!isl_schedule_node_has_parent(node
))
3645 pos
= isl_schedule_node_get_child_position(node
);
3646 node
= isl_schedule_node_parent(node
);
3647 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
)
3648 node
= atomic(node
);
3649 node
= atomic_ancestors(node
);
3650 node
= isl_schedule_node_child(node
, pos
);
3655 /* Collect all write references that require synchronization.
3656 * "node" is assumed to point to the kernel node.
3657 * Each reference is represented by a universe set in a space
3661 * with S[i,j] the statement instance space and R[] the array reference.
3663 * This function should be called before block and thread filters are added.
3665 * Synchronization is needed after a write if there is a subsequent read
3666 * within the same block that may not be performed by the same thread.
3667 * There should not be any dependences between different blocks,
3668 * so we start with the flow dependences within the same kernel invocation
3669 * and we subtract from these those dependences that are mapped
3670 * to the same iteration of the bands where synchronization is inserted.
3671 * We do not remove pairs of instances that are known to map to
3672 * the same thread across different iterations of the intermediate
3673 * bands because the read may be performed by a different thread
3674 * than the one that needs the value if shared memory is involved.
3676 * We also consider all pairs of possible writes that access the same
3677 * memory location and that may be mapped to the same block but not
3678 * to the same iteration of the intermediate bands.
3679 * In theory, it would be possible for one thread to still be in
3680 * a previous iteration of a loop in these bands.
3681 * A write to global memory in this delayed thread could then overwrite
3682 * a write from another thread that has already moved on to
3683 * the next iteration.
3685 * After computing the above writes paired off with reads or writes
3686 * that depend on them, we project onto the domain writes.
3687 * Sychronization is needed after writes to global memory
3688 * through these references.
3690 static __isl_give isl_union_set
*compute_sync_writes(
3691 struct ppcg_kernel
*kernel
, __isl_keep isl_schedule_node
*node
)
3693 isl_union_map
*local
;
3694 isl_union_map
*may_writes
, *shared_access
;
3695 isl_union_map
*kernel_prefix
, *thread_prefix
;
3696 isl_union_map
*equal
;
3697 isl_union_set
*wrap
;
3698 isl_union_set
*domain
;
3700 domain
= isl_schedule_node_get_universe_domain(node
);
3701 kernel_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3702 node
= isl_schedule_node_copy(node
);
3703 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3704 thread_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3705 isl_schedule_node_free(node
);
3707 may_writes
= isl_union_map_copy(kernel
->prog
->scop
->tagged_may_writes
);
3708 may_writes
= isl_union_map_curry(may_writes
);
3709 may_writes
= isl_union_map_intersect_domain(may_writes
, domain
);
3710 may_writes
= isl_union_map_uncurry(may_writes
);
3711 shared_access
= isl_union_map_copy(may_writes
);
3712 shared_access
= isl_union_map_apply_range(shared_access
,
3713 isl_union_map_reverse(may_writes
));
3715 local
= isl_union_map_copy(kernel
->prog
->scop
->tagged_dep_flow
);
3716 local
= isl_union_map_union(local
, shared_access
);
3717 local
= isl_union_map_zip(local
);
3719 equal
= isl_union_map_apply_range(kernel_prefix
,
3720 isl_union_map_reverse(isl_union_map_copy(kernel_prefix
)));
3721 wrap
= isl_union_map_wrap(equal
);
3722 local
= isl_union_map_intersect_domain(local
, wrap
);
3723 equal
= isl_union_map_apply_range(thread_prefix
,
3724 isl_union_map_reverse(isl_union_map_copy(thread_prefix
)));
3725 wrap
= isl_union_map_wrap(equal
);
3726 local
= isl_union_map_subtract_domain(local
, wrap
);
3728 local
= isl_union_map_zip(local
);
3729 local
= isl_union_map_universe(local
);
3731 return isl_union_map_domain(local
);
3734 /* Group the domain elements into a single space, named kernelX,
3735 * with X the kernel sequence number "kernel_id".
3737 static __isl_give isl_schedule_node
*group_statements(
3738 __isl_take isl_schedule_node
*node
, int kernel_id
)
3746 snprintf(buffer
, sizeof(buffer
), "kernel%d", kernel_id
);
3747 id
= isl_id_alloc(isl_schedule_node_get_ctx(node
), buffer
, NULL
);
3748 return isl_schedule_node_group(node
, id
);
3751 /* Create a ppcg_kernel representing the domain instances that reach "node"
3752 * and insert a mark node pointing to the ppcg_kernel before "node".
3753 * The band that "node" points to is the band that needs to be mapped
3754 * to block identifiers. The band that needs to be mapped to thread
3755 * identifiers should be marked by a "thread" mark by the caller.
3756 * This mark is removed by this function.
3757 * If "scale" is set, then the band that "node" points to is scaled
3760 * Mark all outer band nodes as atomic to ensure each kernel is only
3762 * If the domain elements that reach "node" live in more than one space,
3763 * then group the domain elements into a single space, named kernelX,
3764 * with X the kernel sequence number.
3766 * Insert a guard node governing the kernel node to ensure that
3767 * no kernels with zero blocks are launched.
3769 * Insert a context node describing the block and thread
3770 * identifiers inside the kernel mark.
3771 * The context node needs to be inserted after the effective block size
3772 * has been determined such that the bounds on the thread identifiers
3773 * would reflect the effective block size.
3774 * Insert a filter node inside the context node mapping the statement
3775 * instances to block identifiers. In particular, the block identifiers
3776 * are equated to the partial schedule of band that was marked for mapping
3777 * to blocks modulo the grid size.
3778 * Insert a filter node inside the "thread" mark mapping the statement
3779 * instances to thread identifiers. In particular, the thread identifiers
3780 * are equated to the partial schedule of band that was marked for mapping
3781 * to threads modulo the block size.
3783 * Compute array reference groups for all arrays, set the local
3784 * array bounds based on the set of domain instances that reach
3785 * the kernel node, check the total amount of shared memory used
3786 * and compute all group tilings.
3787 * The array reference groups are computed after the block filter
3788 * has been inserted because it affects the mapping to shared or
3789 * private memory. This computation also requires the thread filter
3790 * (in the ppcg_kernel object), but this thread filter should not
3791 * have been added to the schedule tree yet since the computation
3792 * requires the schedule of the band that needs to be mapped to
3793 * threads before the privatization is applied.
3795 * If any array reference group requires the band mapped to threads
3796 * to be unrolled, then we perform the required unrolling.
3798 * We save a copy of the schedule that may influence the mappings
3799 * to shared or private memory in kernel->copy_schedule.
3801 * Finally, we add synchronization and copy statements to the schedule tree,
3802 * remove the "thread" mark and create representations for the local
3803 * variables in the kernel.
3805 * We keep a copy of the isl_id that points to the kernel to ensure
3806 * that the kernel does not get destroyed if the schedule node
3807 * is freed due to some error condition.
3809 static __isl_give isl_schedule_node
*create_kernel(struct gpu_gen
*gen
,
3810 __isl_take isl_schedule_node
*node
, int scale
,
3811 __isl_keep isl_multi_val
*sizes
)
3813 struct ppcg_kernel
*kernel
;
3815 isl_schedule_node
*node_thread
;
3816 isl_union_map
*host_schedule
;
3817 isl_union_pw_multi_aff
*contraction
;
3818 isl_set
*host_domain
;
3819 isl_union_set
*domain
, *expanded
;
3820 int single_statement
;
3822 kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
3823 kernel
= ppcg_kernel_create_local_arrays(kernel
, gen
->prog
);
3825 return isl_schedule_node_free(node
);
3827 domain
= isl_schedule_node_get_domain(node
);
3828 single_statement
= isl_union_set_n_set(domain
) == 1;
3830 kernel
->ctx
= gen
->ctx
;
3831 kernel
->prog
= gen
->prog
;
3832 kernel
->options
= gen
->options
;
3833 kernel
->context
= extract_context(node
, gen
->prog
);
3834 kernel
->core
= isl_union_set_universe(isl_union_set_copy(domain
));
3835 contraction
= isl_schedule_node_get_subtree_contraction(node
);
3836 kernel
->contraction
= isl_union_pw_multi_aff_copy(contraction
);
3837 expanded
= isl_union_set_copy(domain
);
3838 expanded
= isl_union_set_preimage_union_pw_multi_aff(expanded
,
3840 kernel
->expanded_domain
= isl_union_set_copy(expanded
);
3841 kernel
->arrays
= accessed_by_domain(expanded
, gen
->prog
);
3842 kernel
->n_grid
= n_outer_coincidence(node
);
3843 node_thread
= isl_schedule_node_copy(node
);
3844 node_thread
= gpu_tree_move_down_to_thread(node_thread
, kernel
->core
);
3845 node_thread
= isl_schedule_node_child(node_thread
, 0);
3846 kernel
->n_block
= n_outer_coincidence(node_thread
);
3847 isl_schedule_node_free(node_thread
);
3848 kernel
->id
= gen
->kernel_id
++;
3849 read_grid_and_block_sizes(kernel
, gen
);
3851 kernel
->sync_writes
= compute_sync_writes(kernel
, node
);
3853 host_schedule
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3854 host_domain
= isl_set_from_union_set(isl_union_map_range(
3857 node
= atomic_ancestors(node
);
3859 id
= isl_id_alloc(gen
->ctx
, "kernel", kernel
);
3860 id
= isl_id_set_free_user(id
, &ppcg_kernel_free_wrap
);
3861 node
= isl_schedule_node_insert_mark(node
, isl_id_copy(id
));
3863 if (!single_statement
)
3864 node
= group_statements(node
, kernel
->id
);
3866 node
= isl_schedule_node_child(node
, 0);
3867 node
= split_band(node
, kernel
->n_grid
);
3868 kernel
->block_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3869 kernel
->n_grid
, "b");
3870 kernel
->block_filter
= set_schedule_modulo(node
, kernel
->block_ids
,
3872 kernel
->grid_size
= extract_grid_size(kernel
,
3873 isl_union_set_copy(domain
));
3874 if (!kernel
->options
->wrap
)
3875 node
= snap_band_to_sizes(node
, kernel
->grid_dim
,
3878 node
= scale_band(node
, isl_multi_val_copy(sizes
));
3879 node
= isl_schedule_node_parent(node
);
3880 if (!single_statement
)
3881 node
= isl_schedule_node_parent(node
);
3882 node
= insert_guard(node
, kernel
->context
, kernel
->grid_size
,
3884 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3885 node
= isl_schedule_node_child(node
, 0);
3886 node
= split_band(node
, kernel
->n_block
);
3887 kernel
->thread_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3888 kernel
->n_block
, "t");
3889 kernel
->thread_filter
= set_schedule_modulo(node
, kernel
->thread_ids
,
3891 if (extract_block_size(kernel
, domain
) < 0)
3892 node
= isl_schedule_node_free(node
);
3894 node
= gpu_tree_move_up_to_kernel(node
);
3895 node
= isl_schedule_node_child(node
, 0);
3896 node
= insert_context(kernel
, node
);
3897 node
= isl_schedule_node_child(node
, 0);
3898 node
= isl_schedule_node_insert_filter(node
,
3899 isl_union_set_copy(kernel
->block_filter
));
3901 node
= gpu_tree_move_up_to_kernel(node
);
3903 if (gpu_group_references(kernel
, node
) < 0)
3904 node
= isl_schedule_node_free(node
);
3905 localize_bounds(kernel
, host_domain
);
3906 isl_set_free(host_domain
);
3908 check_shared_memory_bound(kernel
);
3909 mark_global_arrays(kernel
);
3910 compute_group_tilings(kernel
);
3912 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3913 node
= isl_schedule_node_child(node
, 0);
3914 if (!kernel
->options
->wrap
)
3915 node
= snap_band_to_sizes(node
, kernel
->block_dim
,
3917 node
= isl_schedule_node_insert_filter(node
,
3918 isl_union_set_copy(kernel
->thread_filter
));
3919 if (kernel_requires_unroll(kernel
)) {
3920 node
= isl_schedule_node_child(node
, 0);
3921 node
= unroll(node
);
3924 node
= gpu_tree_move_up_to_thread(node
);
3925 kernel
->copy_schedule_dim
= isl_schedule_node_get_schedule_depth(node
);
3926 kernel
->copy_schedule
=
3927 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
3929 node
= gpu_tree_move_up_to_kernel(node
);
3931 node
= add_sync(kernel
, node
);
3932 node
= add_copies(kernel
, node
);
3934 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3935 node
= isl_schedule_node_delete(node
);
3937 node
= gpu_tree_move_up_to_kernel(node
);
3939 if (create_kernel_vars(kernel
) < 0)
3940 node
= isl_schedule_node_free(node
);
3942 if (!single_statement
)
3943 node
= isl_schedule_node_parent(node
);
3944 node
= isl_schedule_node_parent(node
);
3950 /* Insert a zero-dimensional permutable band at "node".
3952 static __isl_give isl_schedule_node
*insert_empty_permutable_band(
3953 __isl_take isl_schedule_node
*node
)
3956 isl_schedule
*schedule
;
3957 isl_union_set
*domain
;
3958 isl_multi_union_pw_aff
*mupa
;
3960 schedule
= isl_schedule_node_get_schedule(node
);
3961 domain
= isl_schedule_get_domain(schedule
);
3962 space
= isl_union_set_get_space(domain
);
3963 isl_union_set_free(domain
);
3964 isl_schedule_free(schedule
);
3966 space
= isl_space_set_from_params(space
);
3967 mupa
= isl_multi_union_pw_aff_zero(space
);
3968 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
3969 node
= isl_schedule_node_band_set_permutable(node
, 1);
3974 /* If "node" is the outermost permutable band that can be mapped to block and
3975 * thread identifiers in its branch (or the root of a subtree with
3976 * no such outer bands),
3977 * then mark the band as such, attaching a ppcg_kernel to the mark.
3979 * If "node" is the root of a subtree without permutable bands,
3980 * then insert a zero-dimensional permutable band such that
3981 * we can assume that "node" always points to a band node.
3982 * This includes the case where "node" already points to a band node,
3983 * but one without any coincident dimension. In this case,
3984 * the extra node ensures that this original node does not get tiled.
3986 * Tile "node" using user specified tile sizes, after splitting the band
3987 * if the number of specified tile sizes is smaller than the dimension
3988 * of the band. Mark the point band of this tiling as the band that
3989 * needs to be mapped to threads.
3990 * Create a kernel representing the domain instances that reach "node" and
3991 * insert a mark node pointing to the ppcg_kernel before the band node.
3993 static __isl_give isl_schedule_node
*mark_outer_permutable(
3994 __isl_take isl_schedule_node
*node
, void *user
)
3996 struct gpu_gen
*gen
= user
;
4002 isl_multi_val
*sizes
;
4004 outer
= is_outer_tilable(node
);
4006 return isl_schedule_node_free(node
);
4010 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
||
4011 !isl_schedule_node_band_member_get_coincident(node
, 0))
4012 node
= insert_empty_permutable_band(node
);
4014 tile_len
= isl_schedule_node_band_n_member(node
);
4015 tile_size
= read_tile_sizes(gen
, &tile_len
);
4017 return isl_schedule_node_free(node
);
4018 if (tile_len
< isl_schedule_node_band_n_member(node
))
4019 node
= isl_schedule_node_band_split(node
, tile_len
);
4020 sizes
= construct_band_tiles_sizes(node
, tile_size
);
4021 node
= tile_band(node
, isl_multi_val_copy(sizes
));
4022 node
= isl_schedule_node_child(node
, 0);
4023 id
= isl_id_alloc(gen
->ctx
, "thread", NULL
);
4024 node
= isl_schedule_node_insert_mark(node
, id
);
4025 node
= isl_schedule_node_parent(node
);
4027 scale
= gen
->options
->scale_tile_loops
;
4028 node
= create_kernel(gen
, node
, scale
, sizes
);
4029 isl_multi_val_free(sizes
);
4035 /* Given a set or sequence node, return the union the filters of either all
4036 * (if "only_initial" is not set) or the initial (if "only_initial" is set)
4037 * direct subtrees that do not contain any suitably permutable bands
4038 * (according to subtree_has_permutable_bands).
4040 static __isl_give isl_union_set
*get_non_parallel_subtree_filters(
4041 __isl_keep isl_schedule_node
*node
, int only_initial
)
4044 isl_union_set
*filter
;
4047 n
= isl_schedule_node_n_children(node
);
4051 node
= isl_schedule_node_copy(node
);
4052 node
= isl_schedule_node_child(node
, 0);
4053 filter
= isl_schedule_node_filter_get_filter(node
);
4054 node
= isl_schedule_node_parent(node
);
4055 space
= isl_union_set_get_space(filter
);
4056 isl_union_set_free(filter
);
4057 filter
= isl_union_set_empty(space
);
4059 for (i
= 0; i
< n
; ++i
) {
4062 node
= isl_schedule_node_child(node
, i
);
4063 parallelism
= subtree_has_permutable_bands(node
);
4064 if (parallelism
< 0) {
4065 filter
= isl_union_set_free(filter
);
4066 } else if (!parallelism
) {
4067 isl_union_set
*filter_i
;
4068 filter_i
= isl_schedule_node_filter_get_filter(node
);
4069 filter
= isl_union_set_union(filter
, filter_i
);
4070 } else if (only_initial
)
4072 node
= isl_schedule_node_parent(node
);
4075 isl_schedule_node_free(node
);
4080 /* Given a set or sequence node, return the union of the filters of
4081 * the direct subtrees that do not contain any suitably permutable bands
4082 * (according to subtree_has_permutable_bands).
4084 static __isl_give isl_union_set
*get_all_non_parallel_subtree_filters(
4085 __isl_keep isl_schedule_node
*node
)
4087 return get_non_parallel_subtree_filters(node
, 0);
4090 /* Given a set or sequence node, return the union of the filters of
4091 * the initial direct subtrees that do not contain any suitably permutable
4092 * bands (according to subtree_has_permutable_bands).
4094 static __isl_give isl_union_set
*get_initial_non_parallel_subtree_filters(
4095 __isl_keep isl_schedule_node
*node
)
4097 return get_non_parallel_subtree_filters(node
, 1);
4100 /* Mark all variables that are accessed by the statement instances in "domain"
4101 * and that are local to "prog" as requiring a declaration in the host code.
4102 * The statement instances in "domain" correspond to (a subset of)
4103 * the active instances at "node".
4104 * "node" is not modified by this function, except that NULL is returned
4107 static __isl_give isl_schedule_node
*declare_accessed_local_variables(
4108 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
,
4109 __isl_keep isl_union_set
*domain
)
4111 isl_union_pw_multi_aff
*contraction
;
4112 isl_union_set
*arrays
;
4115 if (!ppcg_scop_any_hidden_declarations(prog
->scop
))
4117 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4118 domain
= isl_union_set_copy(domain
);
4119 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, contraction
);
4120 arrays
= accessed_by_domain(domain
, prog
);
4122 for (i
= 0; i
< prog
->n_array
; ++i
) {
4127 if (!prog
->array
[i
].local
)
4129 space
= isl_set_get_space(prog
->array
[i
].extent
);
4130 set
= isl_union_set_extract_set(arrays
, space
);
4131 empty
= isl_set_plain_is_empty(set
);
4136 prog
->array
[i
].declare_local
= 1;
4139 isl_union_set_free(arrays
);
4142 isl_union_set_free(arrays
);
4143 return isl_schedule_node_free(node
);
4146 /* If "node" points to a set node, then separate its children
4147 * into subtrees that have suitably permutable bands and
4148 * those that do not.
4149 * Adjust the schedule tree in order to execute the second group
4150 * after the first group and return a pointer to the first group,
4151 * assuming there are any such subtrees.
4152 * If "node" points to a sequence node, then separate the initial
4153 * children that do not have suitably permutable bands and
4154 * return a pointer to the subsequence of children that do have such bands,
4155 * assuming there are any such subtrees.
4157 * In both cases, mark all local variables in "prog" that are accessed by
4158 * the group without permutable bands as requiring a declaration on the host.
4160 static __isl_give isl_schedule_node
*isolate_permutable_subtrees(
4161 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
)
4163 isl_union_set
*filter
;
4164 enum isl_schedule_node_type type
;
4168 type
= isl_schedule_node_get_type(node
);
4169 if (type
== isl_schedule_node_set
) {
4170 filter
= get_all_non_parallel_subtree_filters(node
);
4171 node
= declare_accessed_local_variables(node
, prog
, filter
);
4172 node
= isl_schedule_node_order_after(node
, filter
);
4173 } else if (type
== isl_schedule_node_sequence
) {
4174 filter
= get_initial_non_parallel_subtree_filters(node
);
4175 node
= declare_accessed_local_variables(node
, prog
, filter
);
4176 node
= isl_schedule_node_order_before(node
, filter
);
4182 /* Replace any reference to an array element in the range of "copy"
4183 * by a reference to all array elements (defined by the extent of the array).
4185 static __isl_give isl_union_map
*approximate_copy_out(
4186 __isl_take isl_union_map
*copy
, struct gpu_prog
*prog
)
4191 res
= isl_union_map_empty(isl_union_map_get_space(copy
));
4193 for (i
= 0; i
< prog
->n_array
; ++i
) {
4196 isl_union_map
*copy_i
;
4197 isl_union_set
*extent
, *domain
;
4199 space
= isl_space_copy(prog
->array
[i
].space
);
4200 extent
= isl_union_set_from_set(isl_set_universe(space
));
4201 copy_i
= isl_union_map_copy(copy
);
4202 copy_i
= isl_union_map_intersect_range(copy_i
, extent
);
4203 set
= isl_set_copy(prog
->array
[i
].extent
);
4204 extent
= isl_union_set_from_set(set
);
4205 domain
= isl_union_map_domain(copy_i
);
4206 copy_i
= isl_union_map_from_domain_and_range(domain
, extent
);
4207 res
= isl_union_map_union(res
, copy_i
);
4210 isl_union_map_free(copy
);
4215 /* Insert "kernel" marks that point to a ppcg_kernel structure
4216 * in front of all outermost tilable band that (by construction)
4217 * have at least one parallel loop.
4219 static __isl_give isl_schedule_node
*mark_kernels(struct gpu_gen
*gen
,
4220 __isl_take isl_schedule_node
*node
)
4222 return isl_schedule_node_map_descendant_bottom_up(node
,
4223 &mark_outer_permutable
, gen
);
4226 /* Construct schedule constraints from the dependences in prog->scop and
4227 * the array order dependences in prog->array_order.
4229 * If live range reordering is allowed, then we need to make sure
4230 * that live ranges on arrays are not run in parallel since doing
4231 * so would require array expansion. We therefore add the array
4232 * order dependences to the coincidence dependences. Non-zero array
4233 * order dependences will then prevent a schedule dimension from being
4234 * considered parallel.
4235 * Live ranges derived from scalars are allowed to be run in parallel
4236 * since we force the scalars to be mapped to private memory in
4237 * check_scalar_live_ranges.
4238 * If live range reordering is allowed, then the false dependences
4239 * are not added to the validity constraints as that would prevent
4240 * reordering. Instead, the external false dependences that enforce that reads
4241 * from potentially live-in data precede any later write and
4242 * that writes of potentially live-out data follow any other earlier write
4243 * are added to the validity and the coincidence constraints.
4244 * The false dependences are still added to the proximity constraints
4245 * for consistency with the case where live range reordering is not allowed.
4246 * The coincidence constraints then consist of flow dependences,
4247 * external false dependences and array order dependences.
4248 * The independences can be filtered out from the first two sets.
4249 * They have already been filtered out from the array order dependences
4250 * on a per array basis in collect_order_dependences.
4251 * There is no need for a per array handling of the other two sets
4252 * as there should be no flow or external false dependence on local
4253 * variables that can be filtered out.
4255 static __isl_give isl_schedule_constraints
*construct_schedule_constraints(
4256 struct gpu_prog
*prog
)
4258 isl_union_set
*domain
;
4259 isl_union_map
*dep_raw
, *dep
;
4260 isl_union_map
*validity
, *proximity
, *coincidence
;
4261 isl_schedule_constraints
*sc
;
4263 domain
= isl_union_set_copy(prog
->scop
->domain
);
4264 sc
= isl_schedule_constraints_on_domain(domain
);
4265 sc
= isl_schedule_constraints_set_context(sc
,
4266 isl_set_copy(prog
->scop
->context
));
4267 if (prog
->scop
->options
->live_range_reordering
) {
4268 sc
= isl_schedule_constraints_set_conditional_validity(sc
,
4269 isl_union_map_copy(prog
->scop
->tagged_dep_flow
),
4270 isl_union_map_copy(prog
->scop
->tagged_dep_order
));
4271 proximity
= isl_union_map_copy(prog
->scop
->dep_flow
);
4272 validity
= isl_union_map_copy(proximity
);
4273 validity
= isl_union_map_union(validity
,
4274 isl_union_map_copy(prog
->scop
->dep_forced
));
4275 proximity
= isl_union_map_union(proximity
,
4276 isl_union_map_copy(prog
->scop
->dep_false
));
4277 coincidence
= isl_union_map_copy(validity
);
4278 coincidence
= isl_union_map_subtract(coincidence
,
4279 isl_union_map_copy(prog
->scop
->independence
));
4280 coincidence
= isl_union_map_union(coincidence
,
4281 isl_union_map_copy(prog
->array_order
));
4283 dep_raw
= isl_union_map_copy(prog
->scop
->dep_flow
);
4284 dep
= isl_union_map_copy(prog
->scop
->dep_false
);
4285 dep
= isl_union_map_union(dep
, dep_raw
);
4286 dep
= isl_union_map_coalesce(dep
);
4287 proximity
= isl_union_map_copy(dep
);
4288 coincidence
= isl_union_map_copy(dep
);
4291 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4292 sc
= isl_schedule_constraints_set_coincidence(sc
, coincidence
);
4293 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4295 if (prog
->scop
->options
->debug
->dump_schedule_constraints
)
4296 isl_schedule_constraints_dump(sc
);
4300 /* Compute an appropriate schedule based on the accesses in
4301 * gen->read and gen->write.
4303 * We derive schedule constraints from the dependences in gen->prog->scop
4304 * and then use isl to compute a schedule that has a parallel loop
4305 * in each tilable band.
4307 static __isl_give isl_schedule
*compute_schedule(struct gpu_gen
*gen
)
4309 isl_schedule_constraints
*sc
;
4310 isl_schedule
*schedule
;
4312 sc
= construct_schedule_constraints(gen
->prog
);
4313 schedule
= isl_schedule_constraints_compute_schedule(sc
);
4318 /* If the band node "node" has exactly one member then mark it permutable.
4320 static __isl_give isl_schedule_node
*band_set_permutable(
4321 __isl_take isl_schedule_node
*node
,
4322 __isl_keep isl_schedule_constraints
*sc
)
4324 if (isl_schedule_node_band_n_member(node
) == 1)
4325 node
= isl_schedule_node_band_set_permutable(node
, 1);
4330 /* Return the coincidence constraints between pairs of instances
4331 * that are scheduled together by the ancestors of "node".
4332 * That is, select those coincidence constraints that relate
4333 * pairs of instances that have the same value for the prefix schedule.
4334 * If the schedule depth is zero, then the prefix schedule does not
4335 * contain any information, so we intersect domain and range
4336 * of the schedule constraints with the reaching domain elements instead.
4338 static __isl_give isl_union_map
*get_local_coincidence(
4339 __isl_keep isl_schedule_node
*node
,
4340 __isl_keep isl_schedule_constraints
*sc
)
4342 isl_union_map
*coincidence
;
4343 isl_multi_union_pw_aff
*prefix
;
4344 isl_union_pw_multi_aff
*contraction
;
4346 coincidence
= isl_schedule_constraints_get_coincidence(sc
);
4347 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4348 if (isl_schedule_node_get_schedule_depth(node
) == 0) {
4349 isl_union_set
*domain
;
4351 domain
= isl_schedule_node_get_domain(node
);
4352 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4354 coincidence
= isl_union_map_intersect_domain(coincidence
,
4355 isl_union_set_copy(domain
));
4356 coincidence
= isl_union_map_intersect_range(coincidence
,
4361 prefix
= isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
4362 prefix
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
4364 return isl_union_map_eq_at_multi_union_pw_aff(coincidence
, prefix
);
4367 /* For each member in the band node "node", determine whether
4368 * it is coincident with respect to the outer nodes and mark
4371 * That is, for each coincidence constraint between pairs
4372 * of instances that are scheduled together by the outer nodes,
4373 * check that domain and range are assigned the same value
4374 * by the band member. This test is performed by checking
4375 * that imposing the same value for the band member does not
4376 * remove any elements from the set of coincidence constraints.
4378 static __isl_give isl_schedule_node
*band_set_coincident(
4379 __isl_take isl_schedule_node
*node
,
4380 __isl_keep isl_schedule_constraints
*sc
)
4382 isl_union_map
*coincidence
;
4383 isl_union_pw_multi_aff
*contraction
;
4384 isl_multi_union_pw_aff
*partial
;
4387 coincidence
= get_local_coincidence(node
, sc
);
4389 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4390 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4391 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4393 n
= isl_schedule_node_band_n_member(node
);
4394 for (i
= 0; i
< n
; ++i
) {
4395 isl_union_map
*coincidence_i
;
4396 isl_union_pw_aff
*upa
;
4397 isl_multi_union_pw_aff
*partial_i
;
4400 upa
= isl_multi_union_pw_aff_get_union_pw_aff(partial
, i
);
4401 partial_i
= isl_multi_union_pw_aff_from_union_pw_aff(upa
);
4402 coincidence_i
= isl_union_map_copy(coincidence
);
4403 coincidence_i
= isl_union_map_eq_at_multi_union_pw_aff(
4404 coincidence_i
, partial_i
);
4405 subset
= isl_union_map_is_subset(coincidence
, coincidence_i
);
4406 isl_union_map_free(coincidence_i
);
4410 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
4414 node
= isl_schedule_node_free(node
);
4415 isl_multi_union_pw_aff_free(partial
);
4416 isl_union_map_free(coincidence
);
4421 /* If "node" is a band, then set its properties.
4423 * In particular, if the band has exactly one member, then mark it permutable.
4424 * Mark the band member coincident based on the coincidence constraints
4427 static __isl_give isl_schedule_node
*set_band_properties(
4428 __isl_take isl_schedule_node
*node
, void *user
)
4430 isl_schedule_constraints
*sc
= user
;
4432 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
4434 if (isl_schedule_node_band_n_member(node
) == 0)
4437 node
= band_set_permutable(node
, sc
);
4438 node
= band_set_coincident(node
, sc
);
4443 /* Return the original schedule with all bands marked permutable and
4444 * all band members marked coincident based on the coincidence constraints.
4445 * The bands are explicitly marked permutable so that they will be considered
4446 * by mark_outer_permutable.
4448 static __isl_give isl_schedule
*determine_properties_original_schedule(
4449 struct gpu_gen
*gen
)
4451 isl_schedule
*schedule
;
4452 isl_schedule_constraints
*sc
;
4454 schedule
= isl_schedule_copy(gen
->prog
->scop
->schedule
);
4455 sc
= construct_schedule_constraints(gen
->prog
);
4456 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
4457 &set_band_properties
, sc
);
4458 isl_schedule_constraints_free(sc
);
4463 /* Compute a schedule or determine the properties of the original schedule
4464 * depending on the value of the "reschedule" option.
4466 static __isl_give isl_schedule
*compute_or_set_properties(void *user
)
4468 struct gpu_gen
*gen
= user
;
4470 if (gen
->options
->reschedule
)
4471 return compute_schedule(gen
);
4473 return determine_properties_original_schedule(gen
);
4476 /* Obtain a schedule for the scop, by reading it from
4477 * a file, by computing one or by determining the properties
4478 * of the original schedule.
4480 static __isl_give isl_schedule
*get_schedule(struct gpu_gen
*gen
)
4482 return ppcg_get_schedule(gen
->ctx
, gen
->options
,
4483 &compute_or_set_properties
, gen
);
4486 /* Construct the string "<a>_<b>".
4488 static char *concat(isl_ctx
*ctx
, const char *a
, const char *b
)
4493 p
= isl_printer_to_str(ctx
);
4494 p
= isl_printer_print_str(p
, a
);
4495 p
= isl_printer_print_str(p
, "_");
4496 p
= isl_printer_print_str(p
, b
);
4497 s
= isl_printer_get_str(p
);
4498 isl_printer_free(p
);
4503 /* For each array in "prog" of which an element appears in "accessed" and
4504 * that is not a read only scalar, create a zero-dimensional universe set
4505 * of which the tuple id has name "<prefix>_<name of array>" and a user
4506 * pointer pointing to the array (gpu_array_info).
4508 * If the array is local to "prog", then make sure it will be declared
4511 * Return the list of these universe sets.
4513 static __isl_give isl_union_set_list
*create_copy_filters(struct gpu_prog
*prog
,
4514 const char *prefix
, __isl_take isl_union_set
*accessed
)
4518 isl_union_set_list
*filters
;
4521 filters
= isl_union_set_list_alloc(ctx
, 0);
4522 for (i
= 0; i
< prog
->n_array
; ++i
) {
4523 struct gpu_array_info
*array
= &prog
->array
[i
];
4525 isl_set
*accessed_i
;
4529 isl_union_set
*uset
;
4531 if (gpu_array_is_read_only_scalar(array
))
4534 space
= isl_space_copy(array
->space
);
4535 accessed_i
= isl_union_set_extract_set(accessed
, space
);
4536 empty
= isl_set_plain_is_empty(accessed_i
);
4537 isl_set_free(accessed_i
);
4539 filters
= isl_union_set_list_free(filters
);
4547 array
->declare_local
= 1;
4549 name
= concat(ctx
, prefix
, array
->name
);
4550 id
= name
? isl_id_alloc(ctx
, name
, array
) : NULL
;
4552 space
= isl_space_set_alloc(ctx
, 0, 0);
4553 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4554 uset
= isl_union_set_from_set(isl_set_universe(space
));
4556 filters
= isl_union_set_list_add(filters
, uset
);
4558 isl_union_set_free(accessed
);
4563 /* Make sure that code for the statements in "filters" that
4564 * copy arrays to or from the device is only generated when
4565 * the size of the corresponding array is positive.
4566 * That is, add a set node underneath "graft" with "filters" as children
4567 * and for each child add a guard that the selects the parameter
4568 * values for which the corresponding array has a positive size.
4569 * The array is available in the user pointer of the statement identifier.
4570 * "depth" is the schedule depth of the position where "graft"
4573 static __isl_give isl_schedule_node
*insert_positive_size_guards(
4574 __isl_take isl_schedule_node
*graft
,
4575 __isl_take isl_union_set_list
*filters
, int depth
)
4579 graft
= isl_schedule_node_child(graft
, 0);
4580 graft
= isl_schedule_node_insert_set(graft
, filters
);
4581 n
= isl_schedule_node_n_children(graft
);
4582 for (i
= 0; i
< n
; ++i
) {
4583 isl_union_set
*filter
;
4584 isl_set
*domain
, *guard
;
4586 struct gpu_array_info
*array
;
4588 graft
= isl_schedule_node_child(graft
, i
);
4589 filter
= isl_schedule_node_filter_get_filter(graft
);
4590 domain
= isl_set_from_union_set(filter
);
4591 id
= isl_set_get_tuple_id(domain
);
4592 array
= isl_id_get_user(id
);
4594 isl_set_free(domain
);
4595 guard
= gpu_array_positive_size_guard(array
);
4596 guard
= isl_set_from_params(guard
);
4597 guard
= isl_set_add_dims(guard
, isl_dim_set
, depth
);
4598 graft
= isl_schedule_node_child(graft
, 0);
4599 graft
= isl_schedule_node_insert_guard(graft
, guard
);
4600 graft
= isl_schedule_node_parent(graft
);
4601 graft
= isl_schedule_node_parent(graft
);
4603 graft
= isl_schedule_node_parent(graft
);
4608 /* Create a graft for copying arrays to or from the device,
4609 * whenever the size of the array is strictly positive.
4610 * Each statement is called "<prefix>_<name of array>" and
4611 * the identifier has a user pointer pointing to the array.
4612 * The graft will be added at the position specified by "node".
4613 * "copy" contains the array elements that need to be copied.
4614 * Only arrays of which some elements need to be copied
4615 * will have a corresponding statement in the graph.
4616 * Note though that each such statement will copy the entire array.
4618 static __isl_give isl_schedule_node
*create_copy_device(struct gpu_prog
*prog
,
4619 __isl_keep isl_schedule_node
*node
, const char *prefix
,
4620 __isl_take isl_union_set
*copy
)
4625 isl_union_set
*all
, *domain
;
4626 isl_union_set_list
*filters
;
4627 isl_union_map
*extension
;
4628 isl_schedule_node
*graft
;
4631 depth
= isl_schedule_node_get_schedule_depth(node
);
4632 filters
= create_copy_filters(prog
, prefix
, copy
);
4633 all
= isl_union_set_list_union(isl_union_set_list_copy(filters
));
4635 space
= depth
< 0 ? NULL
: isl_space_set_alloc(ctx
, 0, depth
);
4636 domain
= isl_union_set_from_set(isl_set_universe(space
));
4637 extension
= isl_union_map_from_domain_and_range(domain
, all
);
4638 graft
= isl_schedule_node_from_extension(extension
);
4641 return isl_schedule_node_free(graft
);
4642 if (isl_union_set_list_n_union_set(filters
) == 0) {
4643 isl_union_set_list_free(filters
);
4647 return insert_positive_size_guards(graft
, filters
, depth
);
4650 /* Return (the universe spaces of) the arrays that are declared
4651 * inside the scop corresponding to "prog" and for which all
4652 * potential writes inside the scop form a subset of "domain".
4654 static __isl_give isl_union_set
*extract_local_accesses(struct gpu_prog
*prog
,
4655 __isl_keep isl_union_set
*domain
)
4658 isl_union_set
*local
;
4660 local
= isl_union_set_empty(isl_union_set_get_space(domain
));
4662 for (i
= 0; i
< prog
->n_array
; ++i
) {
4664 isl_union_map
*to_outer
;
4665 isl_union_map
*may_write
;
4666 isl_union_set
*write_domain
;
4667 isl_union_set
*fields
;
4670 if (!prog
->array
[i
].local
)
4673 set
= isl_set_universe(isl_space_copy(prog
->array
[i
].space
));
4674 to_outer
= isl_union_map_copy(prog
->to_outer
);
4675 to_outer
= isl_union_map_intersect_range(to_outer
,
4676 isl_union_set_from_set(isl_set_copy(set
)));
4677 fields
= isl_union_map_domain(to_outer
);
4678 may_write
= isl_union_map_copy(prog
->may_write
);
4679 may_write
= isl_union_map_intersect_range(may_write
, fields
);
4680 write_domain
= isl_union_map_domain(may_write
);
4681 subset
= isl_union_set_is_subset(write_domain
, domain
);
4682 isl_union_set_free(write_domain
);
4686 return isl_union_set_free(local
);
4687 } else if (subset
) {
4688 local
= isl_union_set_add_set(local
, set
);
4697 /* Internal data structure for node_may_persist.
4699 * "tagger" maps tagged iteration domains to the corresponding untagged
4702 * "may_persist_flow" is the set of all tagged dataflow dependences
4703 * with those dependences removed that either precede or follow
4704 * the kernel launch in a sequence.
4705 * "inner_band_flow" is the set of all tagged dataflow dependences
4706 * that are local to a given iteration of the outer band nodes
4707 * with respect to the current node.
4708 * "local_flow" is equal to "inner_band_flow", except that the domain
4709 * and the range have been intersected with intermediate filters
4710 * on children of sets or sequences.
4712 struct ppcg_may_persist_data
{
4713 isl_union_pw_multi_aff
*tagger
;
4715 isl_union_map
*local_flow
;
4716 isl_union_map
*inner_band_flow
;
4717 isl_union_map
*may_persist_flow
;
4720 /* Update the information in "data" based on the band ancestor "node".
4722 * In particular, we restrict the dependences in data->local_flow
4723 * to those dependence where the source and the sink occur in
4724 * the same iteration of the given band node.
4725 * We also update data->inner_band_flow to the new value of
4728 static int update_may_persist_at_band(__isl_keep isl_schedule_node
*node
,
4729 struct ppcg_may_persist_data
*data
)
4731 isl_multi_union_pw_aff
*partial
;
4732 isl_union_pw_multi_aff
*contraction
;
4733 isl_union_map
*flow
;
4735 if (isl_schedule_node_band_n_member(node
) == 0)
4738 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4739 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4740 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4742 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4743 isl_union_pw_multi_aff_copy(data
->tagger
));
4745 flow
= data
->local_flow
;
4746 flow
= isl_union_map_eq_at_multi_union_pw_aff(flow
, partial
);
4747 data
->local_flow
= flow
;
4749 isl_union_map_free(data
->inner_band_flow
);
4750 data
->inner_band_flow
= isl_union_map_copy(data
->local_flow
);
4755 /* Given a set of local reaching domain elements "domain",
4756 * expand them to the corresponding leaf domain elements using "contraction"
4757 * and insert the array references tags using data->tagger.
4759 static __isl_give isl_union_set
*expand_and_tag(
4760 __isl_take isl_union_set
*domain
,
4761 __isl_take isl_union_pw_multi_aff
*contraction
,
4762 struct ppcg_may_persist_data
*data
)
4764 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4766 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4767 isl_union_pw_multi_aff_copy(data
->tagger
));
4771 /* Given a filter node that is the child of a set or sequence node,
4772 * restrict data->local_flow to refer only to those elements
4773 * in the filter of the node.
4774 * "contraction" maps the leaf domain elements of the schedule tree
4775 * to the corresponding domain elements at (the parent of) "node".
4777 static int filter_flow(__isl_keep isl_schedule_node
*node
,
4778 struct ppcg_may_persist_data
*data
,
4779 __isl_take isl_union_pw_multi_aff
*contraction
)
4781 isl_union_set
*filter
;
4782 isl_union_map
*flow
;
4784 flow
= data
->local_flow
;
4785 filter
= isl_schedule_node_filter_get_filter(node
);
4786 filter
= expand_and_tag(filter
, contraction
, data
);
4787 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(filter
));
4788 flow
= isl_union_map_intersect_range(flow
, filter
);
4789 data
->local_flow
= flow
;
4794 /* Given a filter node "node", collect the filters on all preceding siblings
4795 * (which are also filter nodes), add them to "filters" and return the result.
4797 static __isl_give isl_union_set
*add_previous_filters(
4798 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4800 isl_schedule_node
*sibling
;
4802 sibling
= isl_schedule_node_copy(node
);
4803 while (sibling
&& isl_schedule_node_has_previous_sibling(sibling
)) {
4804 isl_union_set
*filter
;
4806 sibling
= isl_schedule_node_previous_sibling(sibling
);
4807 filter
= isl_schedule_node_filter_get_filter(sibling
);
4808 filters
= isl_union_set_union(filters
, filter
);
4810 isl_schedule_node_free(sibling
);
4812 return isl_union_set_free(filters
);
4817 /* Given a filter node "node", collect the filters on all following siblings
4818 * (which are also filter nodes), add them to "filters" and return the result.
4820 static __isl_give isl_union_set
*add_next_filters(
4821 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4823 isl_schedule_node
*sibling
;
4825 sibling
= isl_schedule_node_copy(node
);
4826 while (sibling
&& isl_schedule_node_has_next_sibling(sibling
)) {
4827 isl_union_set
*filter
;
4829 sibling
= isl_schedule_node_next_sibling(sibling
);
4830 filter
= isl_schedule_node_filter_get_filter(sibling
);
4831 filters
= isl_union_set_union(filters
, filter
);
4833 isl_schedule_node_free(sibling
);
4835 return isl_union_set_free(filters
);
4840 /* Remove those flow dependences from data->may_persist_flow
4841 * that flow between elements of "domain" within the same iteration
4842 * of all outer band nodes.
4843 * "contraction" maps the leaf domain elements of the schedule tree
4844 * to the corresponding elements "domain".
4846 static void remove_external_flow(struct ppcg_may_persist_data
*data
,
4847 __isl_take isl_union_set
*domain
,
4848 __isl_keep isl_union_pw_multi_aff
*contraction
)
4850 isl_union_map
*flow
;
4852 contraction
= isl_union_pw_multi_aff_copy(contraction
);
4853 domain
= expand_and_tag(domain
, contraction
, data
);
4854 flow
= isl_union_map_copy(data
->local_flow
);
4855 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(domain
));
4856 flow
= isl_union_map_intersect_range(flow
, domain
);
4858 data
->may_persist_flow
= isl_union_map_subtract(data
->may_persist_flow
,
4862 /* Update the information in "data" based on the filter ancestor "node".
4863 * We only need to modify anything if the filter is the child
4864 * of a set or sequence node.
4866 * In the case of a sequence, we remove the dependences between
4867 * statement instances that are both executed either before or
4868 * after the subtree that will be mapped to a kernel, within
4869 * the same iteration of outer bands.
4871 * In both cases, we restrict data->local_flow to the current child.
4873 static int update_may_persist_at_filter(__isl_keep isl_schedule_node
*node
,
4874 struct ppcg_may_persist_data
*data
)
4876 enum isl_schedule_node_type type
;
4877 isl_schedule_node
*parent
;
4879 isl_union_pw_multi_aff
*contraction
;
4880 isl_union_set
*before
, *after
, *filter
;
4882 type
= isl_schedule_node_get_parent_type(node
);
4883 if (type
!= isl_schedule_node_sequence
&& type
!= isl_schedule_node_set
)
4886 parent
= isl_schedule_node_copy(node
);
4887 parent
= isl_schedule_node_parent(parent
);
4888 contraction
= isl_schedule_node_get_subtree_contraction(parent
);
4889 isl_schedule_node_free(parent
);
4891 if (type
== isl_schedule_node_set
)
4892 return filter_flow(node
, data
, contraction
);
4894 filter
= isl_schedule_node_filter_get_filter(node
);
4895 space
= isl_union_set_get_space(filter
);
4896 isl_union_set_free(filter
);
4897 before
= isl_union_set_empty(space
);
4898 after
= isl_union_set_copy(before
);
4899 before
= add_previous_filters(before
, node
);
4900 after
= add_next_filters(after
, node
);
4902 remove_external_flow(data
, before
, contraction
);
4903 remove_external_flow(data
, after
, contraction
);
4905 return filter_flow(node
, data
, contraction
);
4908 /* Update the information in "data" based on the ancestor "node".
4910 static isl_stat
update_may_persist_at(__isl_keep isl_schedule_node
*node
,
4913 struct ppcg_may_persist_data
*data
= user
;
4915 switch (isl_schedule_node_get_type(node
)) {
4916 case isl_schedule_node_error
:
4917 return isl_stat_error
;
4918 case isl_schedule_node_context
:
4919 case isl_schedule_node_domain
:
4920 case isl_schedule_node_expansion
:
4921 case isl_schedule_node_extension
:
4922 case isl_schedule_node_guard
:
4923 case isl_schedule_node_leaf
:
4924 case isl_schedule_node_mark
:
4925 case isl_schedule_node_sequence
:
4926 case isl_schedule_node_set
:
4928 case isl_schedule_node_band
:
4929 if (update_may_persist_at_band(node
, data
) < 0)
4930 return isl_stat_error
;
4932 case isl_schedule_node_filter
:
4933 if (update_may_persist_at_filter(node
, data
) < 0)
4934 return isl_stat_error
;
4941 /* Determine the set of array elements that may need to be perserved
4942 * by a kernel constructed from the subtree at "node".
4943 * This includes the set of array elements that may need to be preserved
4944 * by the entire scop (prog->may_persist) and the elements for which
4945 * there is a potential flow dependence that may cross a kernel launch.
4947 * To determine the second set, we start from all flow dependences.
4948 * From this set of dependences, we remove those that cannot possibly
4949 * require data to be preserved by a kernel launch.
4950 * In particular, we consider the following sets of dependences.
4951 * - dependences of which the write occurs inside the kernel.
4952 * If the data is needed outside the kernel, then it will
4953 * be copied out immediately after the kernel launch, so there
4954 * is no need for any special care.
4955 * - dependences of which the read occurs inside the kernel and the
4956 * corresponding write occurs inside the same iteration of the
4957 * outer band nodes. This means that the data is needed in
4958 * the first kernel launch after the write, which is already
4959 * taken care of by the standard copy-in. That is, the data
4960 * do not need to be preserved by any intermediate call to
4962 * - dependences of which the write and the read either both occur
4963 * before the kernel launch or both occur after the kernel launch,
4964 * within the same iteration of the outer band nodes with respect
4965 * to the sequence that determines the ordering of the dependence
4966 * and the kernel launch. Such flow dependences cannot cross
4967 * any kernel launch.
4969 * For the remaining (tagged) dependences, we take the domain
4970 * (i.e., the tagged writes) and apply the tagged access relation
4971 * to obtain the accessed data elements.
4972 * These are then combined with the elements that may need to be
4973 * preserved by the entire scop.
4975 static __isl_give isl_union_set
*node_may_persist(
4976 __isl_keep isl_schedule_node
*node
, struct gpu_prog
*prog
)
4978 struct ppcg_may_persist_data data
;
4979 isl_union_pw_multi_aff
*contraction
;
4980 isl_union_set
*domain
;
4981 isl_union_set
*persist
;
4982 isl_union_map
*flow
, *local_flow
;
4984 data
.tagger
= prog
->scop
->tagger
;
4986 flow
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
4987 data
.local_flow
= isl_union_map_copy(flow
);
4988 data
.inner_band_flow
= isl_union_map_copy(flow
);
4989 data
.may_persist_flow
= flow
;
4990 if (isl_schedule_node_foreach_ancestor_top_down(node
,
4991 &update_may_persist_at
, &data
) < 0)
4992 data
.may_persist_flow
=
4993 isl_union_map_free(data
.may_persist_flow
);
4994 flow
= data
.may_persist_flow
;
4995 isl_union_map_free(data
.local_flow
);
4997 domain
= isl_schedule_node_get_domain(node
);
4998 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4999 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5001 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5002 isl_union_pw_multi_aff_copy(data
.tagger
));
5003 flow
= isl_union_map_subtract_domain(flow
, isl_union_set_copy(domain
));
5004 local_flow
= data
.inner_band_flow
;
5005 local_flow
= isl_union_map_intersect_range(local_flow
, domain
);
5006 flow
= isl_union_map_subtract(flow
, local_flow
);
5008 persist
= isl_union_map_domain(flow
);
5009 persist
= isl_union_set_apply(persist
,
5010 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5011 persist
= isl_union_set_union(persist
,
5012 isl_union_set_copy(prog
->may_persist
));
5017 /* Add nodes for copying outer arrays in and out of the device
5018 * before and after the subtree "node", which contains one or more kernels.
5019 * "domain" contains the original statement instances, i.e.,
5020 * those that correspond to the domains of the access relations in "prog".
5021 * In particular, the domain has not been contracted in any way.
5022 * "prefix" contains the prefix schedule at that point, in terms
5023 * of the same original statement instances.
5025 * We first compute the sets of outer array elements that need
5026 * to be copied in and out and then graft in the nodes for
5027 * performing this copying.
5029 * In particular, for each array that is possibly written anywhere in
5030 * the subtree "node" and that may be used after "node"
5031 * or that may be visible outside the corresponding scop,
5032 * we copy out its entire extent.
5034 * Any array elements that is read without first being written inside
5035 * the subtree "node" needs to be copied in.
5036 * Furthermore, if there are any array elements that
5037 * are copied out, but that may not be written inside "node, then
5038 * they also need to be copied in to ensure that the value after execution
5039 * is the same as the value before execution, at least for those array
5040 * elements that may have their values preserved by the scop or that
5041 * may be written before "node" and read after "node".
5042 * In case the array elements are structures, we need to take into
5043 * account that all members of the structures need to be written
5044 * by "node" before we can avoid copying the data structure in.
5046 * Note that the may_write relation is intersected with the domain,
5047 * which has been intersected with the context.
5048 * This helps in those cases where the arrays are declared with a fixed size,
5049 * while the accesses are parametric and the context assigns a fixed value
5050 * to the parameters.
5052 * If an element from a local array is read without first being written,
5053 * then there is no point in copying it in since it cannot have been
5054 * written prior to the scop. Warn about the uninitialized read instead.
5056 static __isl_give isl_schedule_node
*add_to_from_device(
5057 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
5058 __isl_take isl_union_map
*prefix
, struct gpu_prog
*prog
)
5060 isl_union_set
*local
;
5061 isl_union_set
*may_persist
;
5062 isl_union_map
*may_write
, *must_write
, *copy_out
, *not_written
;
5063 isl_union_map
*read
, *copy_in
;
5064 isl_union_map
*tagged
;
5065 isl_union_map
*local_uninitialized
;
5066 isl_schedule_node
*graft
;
5068 tagged
= isl_union_map_copy(prog
->scop
->tagged_reads
);
5069 tagged
= isl_union_map_union(tagged
,
5070 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5072 may_write
= isl_union_map_copy(prog
->may_write
);
5073 may_write
= isl_union_map_intersect_domain(may_write
,
5074 isl_union_set_copy(domain
));
5075 may_write
= remove_local_accesses(prog
,
5076 isl_union_map_copy(tagged
), may_write
,
5077 isl_union_map_copy(prefix
), 0);
5078 may_write
= isl_union_map_apply_range(may_write
,
5079 isl_union_map_copy(prog
->to_outer
));
5080 may_write
= isl_union_map_apply_domain(may_write
,
5081 isl_union_map_copy(prefix
));
5082 may_write
= approximate_copy_out(may_write
, prog
);
5083 copy_out
= isl_union_map_copy(may_write
);
5084 may_write
= isl_union_map_apply_range(may_write
,
5085 isl_union_map_copy(prog
->to_inner
));
5086 must_write
= isl_union_map_copy(prog
->must_write
);
5087 must_write
= isl_union_map_apply_domain(must_write
,
5088 isl_union_map_copy(prefix
));
5089 may_persist
= node_may_persist(node
, prog
);
5090 may_write
= isl_union_map_intersect_range(may_write
, may_persist
);
5091 not_written
= isl_union_map_subtract(may_write
, must_write
);
5093 local
= extract_local_accesses(prog
, domain
);
5094 read
= isl_union_map_copy(prog
->read
);
5095 read
= isl_union_map_intersect_domain(read
, domain
);
5096 read
= remove_local_accesses(prog
, tagged
, read
,
5097 isl_union_map_copy(prefix
), 1);
5098 local
= isl_union_set_apply(local
, isl_union_map_copy(prog
->to_inner
));
5099 local_uninitialized
= isl_union_map_copy(prog
->scop
->live_in
);
5100 local_uninitialized
= isl_union_map_intersect_range(local_uninitialized
,
5102 local_uninitialized
= isl_union_map_intersect(local_uninitialized
,
5103 isl_union_map_copy(read
));
5104 if (!isl_union_map_is_empty(local_uninitialized
)) {
5106 "possibly uninitialized reads (not copied in):\n");
5107 isl_union_map_dump(local_uninitialized
);
5109 read
= isl_union_map_subtract(read
, local_uninitialized
);
5110 read
= isl_union_map_apply_domain(read
, prefix
);
5111 copy_in
= isl_union_map_union(read
, not_written
);
5112 copy_in
= isl_union_map_apply_range(copy_in
,
5113 isl_union_map_copy(prog
->to_outer
));
5115 graft
= create_copy_device(prog
, node
, "to_device",
5116 isl_union_map_range(copy_in
));
5117 node
= isl_schedule_node_graft_before(node
, graft
);
5118 graft
= create_copy_device(prog
, node
, "from_device",
5119 isl_union_map_range(copy_out
));
5120 node
= isl_schedule_node_graft_after(node
, graft
);
5125 /* Add nodes for initializing ("init_device") and clearing ("clear_device")
5126 * the device before and after "node".
5128 static __isl_give isl_schedule_node
*add_init_clear_device(
5129 __isl_take isl_schedule_node
*node
)
5133 isl_union_set
*domain
;
5134 isl_schedule_node
*graft
;
5136 ctx
= isl_schedule_node_get_ctx(node
);
5138 space
= isl_space_set_alloc(ctx
, 0, 0);
5139 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "init_device");
5140 domain
= isl_union_set_from_set(isl_set_universe(space
));
5141 graft
= isl_schedule_node_from_domain(domain
);
5143 node
= isl_schedule_node_graft_before(node
, graft
);
5145 space
= isl_space_set_alloc(ctx
, 0, 0);
5146 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "clear_device");
5147 domain
= isl_union_set_from_set(isl_set_universe(space
));
5148 graft
= isl_schedule_node_from_domain(domain
);
5150 node
= isl_schedule_node_graft_after(node
, graft
);
5155 /* Update "schedule" for mapping to a GPU device.
5157 * In particular, insert a context node, create kernels for
5158 * each outermost tilable band and introduce nodes for copying arrays
5159 * in and out of the device and for initializing and clearing the device.
5160 * If the child of the initial root points to a set node,
5161 * then children of this node that do not contain any tilable bands
5162 * are separated from the other children and are not mapped to
5165 * The GPU code is generated in a context where at least one
5166 * statement instance is executed. The corresponding guard is inserted
5167 * around the entire schedule.
5169 static __isl_give isl_schedule
*map_to_device(struct gpu_gen
*gen
,
5170 __isl_take isl_schedule
*schedule
)
5172 isl_schedule_node
*node
;
5175 isl_union_set
*domain
;
5176 isl_union_map
*prefix
;
5177 isl_union_pw_multi_aff
*contraction
;
5178 struct gpu_prog
*prog
;
5180 context
= isl_set_copy(gen
->prog
->context
);
5181 context
= isl_set_from_params(context
);
5182 schedule
= isl_schedule_insert_context(schedule
, context
);
5185 guard
= isl_union_set_params(isl_union_set_copy(prog
->scop
->domain
));
5186 prog
->context
= isl_set_intersect(prog
->context
, isl_set_copy(guard
));
5187 guard
= isl_set_from_params(guard
);
5189 node
= isl_schedule_get_root(schedule
);
5190 isl_schedule_free(schedule
);
5191 node
= isl_schedule_node_child(node
, 0);
5192 node
= isl_schedule_node_child(node
, 0);
5193 node
= isolate_permutable_subtrees(node
, gen
->prog
);
5194 domain
= isl_schedule_node_get_domain(node
);
5195 contraction
= isl_schedule_node_get_subtree_contraction(node
);
5196 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5197 isl_union_pw_multi_aff_copy(contraction
));
5198 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
5199 prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(prefix
,
5201 node
= mark_kernels(gen
, node
);
5202 node
= add_to_from_device(node
, domain
, prefix
, gen
->prog
);
5203 node
= isl_schedule_node_root(node
);
5204 node
= isl_schedule_node_child(node
, 0);
5205 node
= isl_schedule_node_child(node
, 0);
5206 node
= isl_schedule_node_insert_guard(node
, guard
);
5207 node
= isl_schedule_node_child(node
, 0);
5208 node
= add_init_clear_device(node
);
5209 schedule
= isl_schedule_node_get_schedule(node
);
5210 isl_schedule_node_free(node
);
5215 /* Internal data structure for extract_access.
5216 * "next_access" points to the end of a linked list that is extended
5217 * by extract_access.
5218 * "single_expression" is set if the access expressions belong to
5219 * an expression statement (i.e., a statement without internal control).
5220 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5222 struct ppcg_extract_access_data
{
5223 struct gpu_stmt_access
**next_access
;
5224 int single_expression
;
5225 isl_union_map
*any_to_outer
;
5228 /* Given a tagged access relation to a single array "tagged", extract it
5229 * as a map, taking into account that the input may be empty.
5230 * If the access relation is empty, then it does not contain
5231 * any space information, so we try to recover it from the index
5233 * The space of the index expression is of the form I -> A,
5234 * with I the statement instances and A the array, or [I -> F] -> A,
5235 * with F the filters corresponding to arguments.
5236 * We first drop F, if present, obtaining I -> A.
5237 * Then we construct I -> R, with R the reference tag,
5238 * combine the two into I -> [R -> A] and uncurry to obtain
5239 * the final result [I -> R] -> A.
5240 * Note that the index expression may have a lower dimension
5241 * than that of the array, but this dimension is not used
5242 * if the access relation is empty.
5244 static __isl_give isl_map
*extract_single_tagged_access(
5245 __isl_take isl_union_map
*tagged
, __isl_keep pet_expr
*expr
)
5249 isl_space
*space
, *space2
;
5250 isl_multi_pw_aff
*index
;
5252 empty
= isl_union_map_is_empty(tagged
);
5256 return isl_map_from_union_map(tagged
);
5257 isl_union_map_free(tagged
);
5259 index
= pet_expr_access_get_index(expr
);
5260 space
= isl_multi_pw_aff_get_space(index
);
5261 isl_multi_pw_aff_free(index
);
5262 if (isl_space_domain_is_wrapping(space
))
5263 space
= isl_space_domain_factor_domain(space
);
5264 space2
= isl_space_copy(space
);
5265 space2
= isl_space_from_domain(isl_space_domain(space
));
5266 id
= pet_expr_access_get_ref_id(expr
);
5267 space2
= isl_space_set_tuple_id(space2
, isl_dim_out
, id
);
5268 space
= isl_space_range_product(space2
, space
);
5269 space
= isl_space_uncurry(space
);
5271 return isl_map_empty(space
);
5273 isl_union_map_free(tagged
);
5277 /* Does the index expression "index" of "expr" represent an access
5278 * to a single element?
5279 * That is, is "index" completely specified?
5281 * If "expr" accesses elements from different spaces (i.e., fields
5282 * of a structure), then it does not access a single element.
5283 * Otherwise, if the single space of the access matches the space
5284 * of "index", then the index expression is completely specified
5285 * (no pointer to a lower-dimensional slice of the accessed array)
5286 * and a single element is being accessed.
5288 static isl_bool
complete_index(__isl_keep pet_expr
*expr
,
5289 __isl_keep isl_multi_pw_aff
*index
)
5291 isl_union_map
*read
, *write
, *all
;
5293 isl_space
*space1
, *space2
;
5296 read
= pet_expr_access_get_may_read(expr
);
5297 write
= pet_expr_access_get_may_write(expr
);
5298 all
= isl_union_map_union(read
, write
);
5300 return isl_bool_error
;
5301 if (isl_union_map_n_map(all
) != 1) {
5302 isl_union_map_free(all
);
5303 return isl_bool_false
;
5305 map
= isl_map_from_union_map(all
);
5306 space1
= isl_map_get_space(map
);
5308 space2
= isl_multi_pw_aff_get_space(index
);
5309 complete
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
5310 space2
, isl_dim_out
);
5311 isl_space_free(space1
);
5312 isl_space_free(space2
);
5317 /* Does "expr" access a single, fixed element (independently of the statement
5319 * That is, does it have a completely specified constant index expression?
5321 * Note that it is not sufficient for the index expression to be
5322 * piecewise constant. isl_multi_pw_aff_is_cst can therefore not be used.
5324 static isl_bool
accesses_fixed_element(__isl_keep pet_expr
*expr
)
5327 isl_multi_pw_aff
*index
;
5328 isl_bool fixed
= isl_bool_true
;
5330 index
= pet_expr_access_get_index(expr
);
5332 return isl_bool_error
;
5333 n
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5334 for (i
= 0; i
< n
; ++i
) {
5337 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
5338 fixed
= isl_pw_aff_n_piece(pa
) == 1;
5340 fixed
= isl_pw_aff_is_cst(pa
);
5341 isl_pw_aff_free(pa
);
5342 if (fixed
< 0 || !fixed
)
5345 if (fixed
>= 0 && fixed
)
5346 fixed
= complete_index(expr
, index
);
5347 isl_multi_pw_aff_free(index
);
5352 /* Extract a gpu_stmt_access from "expr", append it to the list
5353 * that ends in *data->next_access and update the end of the list.
5354 * If the access expression performs a write, then it is considered
5355 * exact only if it appears in a single expression statement and
5356 * if its may access relation is equal to its must access relation.
5358 * The combined set of may accesses may be a union if member accesses
5359 * are involved, but the entire set is derived from a single reference and
5360 * therefore from a single index expression. These accesses therefore
5361 * all map to the same outer array.
5363 static int extract_access(__isl_keep pet_expr
*expr
, void *user
)
5365 struct ppcg_extract_access_data
*data
= user
;
5366 isl_union_map
*tagged
;
5367 struct gpu_stmt_access
*access
;
5368 isl_ctx
*ctx
= pet_expr_get_ctx(expr
);
5369 isl_multi_pw_aff
*index
;
5371 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
5373 access
->next
= NULL
;
5374 access
->read
= pet_expr_access_is_read(expr
);
5375 access
->write
= pet_expr_access_is_write(expr
);
5376 tagged
= pet_expr_access_get_tagged_may_read(expr
);
5377 tagged
= isl_union_map_union(tagged
,
5378 pet_expr_access_get_tagged_may_write(expr
));
5379 tagged
= isl_union_map_apply_range(tagged
,
5380 isl_union_map_copy(data
->any_to_outer
));
5381 if (!access
->write
) {
5382 access
->exact_write
= 1;
5383 } else if (!data
->single_expression
) {
5384 access
->exact_write
= 0;
5386 isl_union_map
*must
, *may
;
5387 may
= isl_union_map_copy(tagged
);
5388 may
= isl_union_map_domain_factor_domain(may
);
5389 must
= pet_expr_access_get_must_write(expr
);
5390 access
->exact_write
= isl_union_map_is_equal(must
, may
);
5391 isl_union_map_free(must
);
5392 isl_union_map_free(may
);
5394 index
= pet_expr_access_get_index(expr
);
5395 access
->n_index
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5396 isl_multi_pw_aff_free(index
);
5397 access
->ref_id
= pet_expr_access_get_ref_id(expr
);
5398 access
->tagged_access
= extract_single_tagged_access(tagged
, expr
);
5399 access
->access
= isl_map_copy(access
->tagged_access
);
5400 access
->access
= isl_map_domain_factor_domain(access
->access
);
5401 access
->fixed_element
= accesses_fixed_element(expr
);
5403 *data
->next_access
= access
;
5404 data
->next_access
= &(*data
->next_access
)->next
;
5406 if (!access
->access
|| access
->fixed_element
< 0)
5412 /* Construct a linked list of gpu_stmt_access objects,
5413 * one for each access expression in the statement body.
5414 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5416 static int pet_stmt_extract_accesses(struct gpu_stmt
*stmt
,
5417 __isl_keep isl_union_map
*any_to_outer
)
5419 struct ppcg_extract_access_data data
;
5421 stmt
->accesses
= NULL
;
5422 data
.next_access
= &stmt
->accesses
;
5423 data
.single_expression
=
5424 pet_tree_get_type(stmt
->stmt
->body
) == pet_tree_expr
;
5425 data
.any_to_outer
= any_to_outer
;
5426 return pet_tree_foreach_access_expr(stmt
->stmt
->body
,
5427 &extract_access
, &data
);
5430 /* Has statement "stmt" been killed from "scop"?
5431 * That is, is the instance set of "scop" free from any
5432 * instances of "stmt"?
5434 static isl_bool
is_stmt_killed(struct ppcg_scop
*scop
, struct pet_stmt
*stmt
)
5441 return isl_bool_error
;
5442 space
= isl_set_get_space(stmt
->domain
);
5443 left
= isl_union_set_extract_set(scop
->domain
, space
);
5444 empty
= isl_set_plain_is_empty(left
);
5450 /* Return an array of gpu_stmt representing the statements in "scop".
5451 * Do not collect array accesses for statements that have been killed.
5453 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
5454 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*any_to_outer
)
5457 struct gpu_stmt
*stmts
;
5459 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->pet
->n_stmt
);
5463 for (i
= 0; i
< scop
->pet
->n_stmt
; ++i
) {
5464 struct gpu_stmt
*s
= &stmts
[i
];
5467 s
->id
= isl_set_get_tuple_id(scop
->pet
->stmts
[i
]->domain
);
5468 s
->stmt
= scop
->pet
->stmts
[i
];
5469 killed
= is_stmt_killed(scop
, scop
->pet
->stmts
[i
]);
5471 return free_stmts(stmts
, i
+ 1);
5474 if (pet_stmt_extract_accesses(s
, any_to_outer
) < 0)
5475 return free_stmts(stmts
, i
+ 1);
5481 /* Generate CUDA code for "scop" and print it to "p".
5482 * After generating an AST for the transformed scop as explained below,
5483 * we call "gen->print" to print the AST in the desired output format
5486 * If it turns out that it does not make sense to generate GPU code,
5487 * then we generate CPU code instead.
5489 * The declarations of the arrays that are visible outside of the scop
5490 * are printed outside of the code generated from the schedule,
5491 * because the generated code may involve a guard around the entire code.
5493 * We first compute a schedule that respects the dependences
5494 * of the original program and select the outermost bands
5495 * of tilable dimensions that have at least one parallel loop.
5496 * If the --load-schedule is specified, then the loaded schedule
5497 * is used instead of a computed schedule.
5499 * Each of these bands B is then tiled according to "tile" sizes, resulting
5500 * in two nested bands, with a kernel marker on top
5508 * We then split off at most 2 parallel dimensions from the T band and
5509 * at most 3 parallel dimension from the P band
5522 * A filter is introduced in front of T1 that maps the domain instances
5523 * to block identifiers. Similarly, a filter is introduced in front of P1
5524 * that maps the domain instances to thread identifiers.
5526 * For each iteration of the T2 band and for each array, we compute
5527 * the array elements accessed by that iteration, construct a rectangular
5528 * box around it and shift it to the origin. The result is used
5529 * as shared memory for the array.
5531 * Copying and synchronization statements are added to this schedule tree.
5532 * In principle, these are added in front of the P1 band, but some of
5533 * them may get hoisted up to higher levels.
5535 * The entire AST is then generated from the single resulting schedule tree.
5536 * During the generation the subtrees at kernel nodes (K) are saved
5537 * aside and replaced by kernel calls. The result is printed as host code
5538 * while the saved subtrees are printed as device code.
5540 static __isl_give isl_printer
*generate(__isl_take isl_printer
*p
,
5541 struct gpu_gen
*gen
, struct ppcg_scop
*scop
,
5542 struct ppcg_options
*options
)
5544 struct gpu_prog
*prog
;
5546 isl_schedule
*schedule
;
5550 return isl_printer_free(p
);
5552 ctx
= isl_printer_get_ctx(p
);
5553 prog
= gpu_prog_alloc(ctx
, scop
);
5555 return isl_printer_free(p
);
5558 schedule
= get_schedule(gen
);
5560 any_permutable
= has_any_permutable_node(schedule
);
5561 if (any_permutable
< 0 || !any_permutable
) {
5562 if (any_permutable
< 0)
5563 p
= isl_printer_free(p
);
5565 p
= print_cpu(p
, scop
, options
);
5566 isl_schedule_free(schedule
);
5568 schedule
= map_to_device(gen
, schedule
);
5569 gen
->tree
= generate_code(gen
, schedule
);
5570 p
= ppcg_set_macro_names(p
);
5571 p
= ppcg_print_exposed_declarations(p
, prog
->scop
);
5572 p
= gen
->print(p
, gen
->prog
, gen
->tree
, &gen
->types
,
5574 isl_ast_node_free(gen
->tree
);
5577 gpu_prog_free(prog
);
5582 /* Wrapper around generate for use as a ppcg_transform callback.
5584 static __isl_give isl_printer
*generate_wrap(__isl_take isl_printer
*p
,
5585 struct ppcg_scop
*scop
, void *user
)
5587 struct gpu_gen
*gen
= user
;
5589 return generate(p
, gen
, scop
, gen
->options
);
5592 /* Transform the code in the file called "input" by replacing
5593 * all scops by corresponding GPU code and write the results to "out".
5595 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
5596 struct ppcg_options
*options
,
5597 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
5598 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
5599 struct gpu_types
*types
, void *user
), void *user
)
5606 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
5607 gen
.options
= options
;
5610 gen
.print_user
= user
;
5612 gen
.types
.name
= NULL
;
5614 if (options
->debug
->dump_sizes
) {
5615 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
5616 gen
.used_sizes
= isl_union_map_empty(space
);
5619 r
= ppcg_transform(ctx
, input
, out
, options
, &generate_wrap
, &gen
);
5621 if (options
->debug
->dump_sizes
) {
5622 isl_union_map_dump(gen
.used_sizes
);
5623 isl_union_map_free(gen
.used_sizes
);
5626 isl_union_map_free(gen
.sizes
);
5627 for (i
= 0; i
< gen
.types
.n
; ++i
)
5628 free(gen
.types
.name
[i
]);
5629 free(gen
.types
.name
);
5634 /* Compute the set of inner array elements that may have their values
5635 * preserved by "prog". In particular, collect the array elements of
5636 * arrays that are not local to "prog" and remove those elements that
5637 * are definitely killed or definitely written by "prog".
5639 static __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
)
5642 isl_union_set
*may_persist
, *killed
;
5643 isl_union_map
*must_kill
;
5645 may_persist
= isl_union_set_empty(isl_set_get_space(prog
->context
));
5646 for (i
= 0; i
< prog
->n_array
; ++i
) {
5649 if (prog
->array
[i
].local
)
5652 extent
= isl_set_copy(prog
->array
[i
].extent
);
5653 may_persist
= isl_union_set_add_set(may_persist
, extent
);
5656 may_persist
= isl_union_set_intersect_params(may_persist
,
5657 isl_set_copy(prog
->context
));
5658 may_persist
= isl_union_set_apply(may_persist
,
5659 isl_union_map_copy(prog
->to_inner
));
5660 must_kill
= isl_union_map_copy(prog
->tagged_must_kill
);
5661 killed
= isl_union_map_range(must_kill
);
5662 must_kill
= isl_union_map_copy(prog
->must_write
);
5663 killed
= isl_union_set_union(killed
, isl_union_map_range(must_kill
));
5665 may_persist
= isl_union_set_subtract(may_persist
, killed
);
5669 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
5671 struct gpu_prog
*prog
;
5678 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
5683 prog
->context
= isl_set_copy(scop
->context
);
5684 prog
->n_stmts
= scop
->pet
->n_stmt
;
5685 prog
->any_to_outer
= pet_scop_compute_outer_to_any(scop
->pet
);
5686 prog
->any_to_outer
= isl_union_map_reverse(prog
->any_to_outer
);
5687 space
= isl_union_map_get_space(prog
->any_to_outer
);
5688 space
= isl_space_set_from_params(space
);
5689 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
5690 space
= isl_space_map_from_set(space
);
5691 id
= isl_map_identity(space
);
5692 prog
->any_to_outer
= isl_union_map_add_map(prog
->any_to_outer
, id
);
5693 prog
->stmts
= extract_stmts(ctx
, scop
,
5694 prog
->context
, prog
->any_to_outer
);
5695 prog
->read
= isl_union_map_copy(scop
->reads
);
5696 prog
->may_write
= isl_union_map_copy(scop
->may_writes
);
5697 prog
->must_write
= isl_union_map_copy(scop
->must_writes
);
5698 prog
->tagged_must_kill
= isl_union_map_copy(scop
->tagged_must_kills
);
5699 prog
->to_inner
= pet_scop_compute_outer_to_inner(scop
->pet
);
5700 prog
->to_outer
= isl_union_map_copy(prog
->to_inner
);
5701 prog
->to_outer
= isl_union_map_reverse(prog
->to_outer
);
5704 return gpu_prog_free(prog
);
5706 if (collect_array_info(prog
) < 0)
5707 return gpu_prog_free(prog
);
5708 prog
->may_persist
= compute_may_persist(prog
);
5713 void *gpu_prog_free(struct gpu_prog
*prog
)
5717 free_array_info(prog
);
5718 free_stmts(prog
->stmts
, prog
->n_stmts
);
5719 isl_union_map_free(prog
->any_to_outer
);
5720 isl_union_map_free(prog
->to_outer
);
5721 isl_union_map_free(prog
->to_inner
);
5722 isl_union_map_free(prog
->read
);
5723 isl_union_map_free(prog
->may_write
);
5724 isl_union_map_free(prog
->must_write
);
5725 isl_union_map_free(prog
->tagged_must_kill
);
5726 isl_union_map_free(prog
->array_order
);
5727 isl_union_set_free(prog
->may_persist
);
5728 isl_set_free(prog
->context
);