2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2015-2016 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 #include <isl/polynomial.h>
19 #include <isl/union_set.h>
23 #include <isl/schedule.h>
24 #include <isl/schedule_node.h>
25 #include <isl/options.h>
26 #include <isl/ast_build.h>
30 #include "gpu_array_tile.h"
31 #include "gpu_group.h"
32 #include "gpu_hybrid.h"
36 #include "ppcg_options.h"
40 struct gpu_array_info
;
42 /* Return the name of the outer array (of structs) accessed by "access".
44 static const char *get_outer_array_name(__isl_keep isl_map
*access
)
49 space
= isl_space_range(isl_map_get_space(access
));
50 while (space
&& isl_space_is_wrapping(space
))
51 space
= isl_space_domain(isl_space_unwrap(space
));
52 name
= isl_space_get_tuple_name(space
, isl_dim_set
);
53 isl_space_free(space
);
58 /* Collect all references to the given array and store pointers to them
61 static isl_stat
collect_references(struct gpu_prog
*prog
,
62 struct gpu_array_info
*array
)
68 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
69 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
70 struct gpu_stmt_access
*access
;
72 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
74 name
= get_outer_array_name(access
->access
);
75 if (name
&& !strcmp(array
->name
, name
))
80 array
->refs
= isl_alloc_array(prog
->ctx
, struct gpu_stmt_access
*, n
);
82 return isl_stat_error
;
86 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
87 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
88 struct gpu_stmt_access
*access
;
90 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
92 name
= get_outer_array_name(access
->access
);
93 if (!name
|| strcmp(array
->name
, name
))
96 array
->refs
[n
++] = access
;
103 /* Compute and return the extent of "array", taking into account the set of
106 * In particular, the extent in the outer dimension is taken
107 * from "accessed", while the extents in the remaining dimensions
108 * are taken from array->extent.
110 * The extent in the outer dimension cannot be taken from array->extent
111 * because that may be unbounded. Furthermore, even if it is bounded,
112 * it may be larger than the piece of the array that is being accessed.
114 static __isl_give isl_set
*compute_extent(struct pet_array
*array
,
115 __isl_keep isl_set
*accessed
)
122 extent
= isl_set_copy(array
->extent
);
124 n_index
= isl_set_dim(accessed
, isl_dim_set
);
128 extent
= isl_set_project_out(extent
, isl_dim_set
, 0, 1);
129 outer
= isl_set_copy(accessed
);
130 outer
= isl_set_project_out(outer
, isl_dim_set
, 1, n_index
- 1);
131 extent
= isl_set_flat_product(outer
, extent
);
132 id
= isl_set_get_tuple_id(accessed
);
133 extent
= isl_set_set_tuple_id(extent
, id
);
138 /* Is the array "array" being extracted a read-only scalar?
140 * That is, is "array" a scalar that is never possibly written to.
141 * An array containing structures is never considered to be a scalar.
143 static int is_read_only_scalar(struct gpu_array_info
*array
,
144 struct gpu_prog
*prog
)
147 isl_union_map
*write
;
150 if (array
->has_compound_element
)
152 if (array
->n_index
!= 0)
155 write
= isl_union_map_copy(prog
->may_write
);
156 space
= isl_set_universe(isl_space_copy(array
->space
));
157 write
= isl_union_map_intersect_range(write
,
158 isl_union_set_from_set(space
));
159 empty
= isl_union_map_is_empty(write
);
160 isl_union_map_free(write
);
165 /* Is "array" only accessed as individual, fixed elements?
166 * That is, does each access to "array" access a single, fixed element?
168 static isl_bool
only_fixed_element_accessed(struct gpu_array_info
*array
)
172 for (i
= 0; i
< array
->n_ref
; ++i
)
173 if (!array
->refs
[i
]->fixed_element
)
174 return isl_bool_false
;
176 return isl_bool_true
;
179 /* Compute bounds on the host array "pa" based on the corresponding
180 * accessed elements in "arrays"
181 * and collect all references to the array.
182 * Store the results in "info".
184 * If the array is zero-dimensional and does not contain structures,
185 * i.e., if the array is a scalar, we check whether it is read-only.
186 * We also check whether the array is accessed at all.
188 static isl_stat
extract_array_info(struct gpu_prog
*prog
,
189 struct gpu_array_info
*info
, struct pet_array
*pa
,
190 __isl_keep isl_union_set
*arrays
)
195 isl_multi_pw_aff
*bounds
;
196 isl_set
*accessed
, *extent
;
198 n_index
= isl_set_dim(pa
->extent
, isl_dim_set
);
199 name
= isl_set_get_tuple_name(pa
->extent
);
201 info
->space
= isl_set_get_space(pa
->extent
);
202 info
->name
= strdup(name
);
203 info
->n_index
= n_index
;
204 info
->linearize
= prog
->scop
->options
->linearize_device_arrays
;
206 info
->type
= strdup(pa
->element_type
);
207 info
->size
= pa
->element_size
;
208 info
->local
= pa
->declared
&& !pa
->exposed
;
209 info
->has_compound_element
= pa
->element_is_record
;
210 info
->read_only_scalar
= is_read_only_scalar(info
, prog
);
212 info
->declared_extent
= isl_set_copy(pa
->extent
);
213 accessed
= isl_union_set_extract_set(arrays
,
214 isl_space_copy(info
->space
));
215 empty
= isl_set_is_empty(accessed
);
216 extent
= compute_extent(pa
, accessed
);
217 isl_set_free(accessed
);
218 info
->extent
= extent
;
220 return isl_stat_error
;
221 info
->accessed
= !empty
;
222 bounds
= ppcg_size_from_extent(isl_set_copy(extent
));
223 bounds
= isl_multi_pw_aff_gist(bounds
, isl_set_copy(prog
->context
));
225 return isl_stat_error
;
226 if (!isl_multi_pw_aff_is_cst(bounds
))
228 info
->bound
= bounds
;
230 if (collect_references(prog
, info
) < 0)
231 return isl_stat_error
;
232 info
->only_fixed_element
= only_fixed_element_accessed(info
);
237 /* Remove independence from the order constraints "order" on array "array".
238 * Since the pairs of iterations in the filter relation of an independence
239 * are guaranteed to be completely independent by the user, there is
240 * no need to ensure that live ranges are ordered along those pairs.
241 * We make an exception for local variables, though, as the independence
242 * guarantee does not apply to those.
244 * The order constraints are used in two places.
245 * Those on scalars are used in check_scalar_live_ranges to check if
246 * we need to force the scalar to be private. Any non-local scalar
247 * should not be forced scalar if it only appears in independent loops.
248 * Those on non-scalars are added to the coincidence constraints
249 * in compute_schedule because we do not support any array expansion.
250 * Accesses to non-local arrays should not prevent a loop from being
251 * considered coincident so we should indeed remove those constraints
252 * from the order constraints.
254 static __isl_give isl_union_map
*remove_independences(struct gpu_prog
*prog
,
255 struct gpu_array_info
*array
, __isl_take isl_union_map
*order
)
259 for (i
= 0; i
< prog
->scop
->pet
->n_independence
; ++i
) {
260 struct pet_independence
*pi
= prog
->scop
->pet
->independences
[i
];
261 if (isl_union_set_contains(pi
->local
, array
->space
))
264 order
= isl_union_map_subtract(order
,
265 isl_union_map_copy(pi
->filter
));
271 /* For each array in "prog", store the (untagged) order dependences
272 * derived from the array in array->dep_order.
273 * In particular, consider all references that access the given array
274 * and take the order dependences that have one of these references
275 * as source. (Since an order dependence relates two references to
276 * the same array, the target of these order dependences will also
277 * be one of these references.)
278 * Additionally, store the union of these array->dep_order relations
279 * for all arrays that cannot be mapped to private memory in prog->array_order.
281 void collect_order_dependences(struct gpu_prog
*prog
)
285 isl_union_map
*accesses
;
287 space
= isl_union_map_get_space(prog
->read
);
288 prog
->array_order
= isl_union_map_empty(space
);
290 accesses
= isl_union_map_copy(prog
->scop
->tagged_reads
);
291 accesses
= isl_union_map_union(accesses
,
292 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
293 accesses
= isl_union_map_universe(accesses
);
294 accesses
= isl_union_map_apply_range(accesses
,
295 isl_union_map_copy(prog
->to_outer
));
297 for (i
= 0; i
< prog
->n_array
; ++i
) {
298 struct gpu_array_info
*array
= &prog
->array
[i
];
301 isl_union_map
*order
;
303 set
= isl_set_universe(isl_space_copy(array
->space
));
304 uset
= isl_union_set_from_set(set
);
305 uset
= isl_union_map_domain(
306 isl_union_map_intersect_range(isl_union_map_copy(accesses
),
308 order
= isl_union_map_copy(prog
->scop
->tagged_dep_order
);
309 order
= isl_union_map_intersect_domain(order
, uset
);
310 order
= isl_union_map_zip(order
);
311 order
= isl_union_set_unwrap(isl_union_map_domain(order
));
312 order
= remove_independences(prog
, array
, order
);
313 array
->dep_order
= order
;
315 if (gpu_array_can_be_private(array
))
318 prog
->array_order
= isl_union_map_union(prog
->array_order
,
319 isl_union_map_copy(array
->dep_order
));
322 isl_union_map_free(accesses
);
325 /* Construct a gpu_array_info for each array referenced by prog->scop and
326 * collect them in prog->array.
328 * The sizes are based on the extents and the set of possibly accessed
329 * elements by "prog".
330 * If there are any member accesses involved, then they are first mapped
331 * to the outer arrays of structs.
332 * Only extract gpu_array_info entries for these outer arrays.
334 * If we are allowing live range reordering, then also set
335 * the dep_order field. Otherwise leave it NULL.
337 static isl_stat
collect_array_info(struct gpu_prog
*prog
)
340 isl_stat r
= isl_stat_ok
;
341 isl_union_set
*arrays
;
344 prog
->array
= isl_calloc_array(prog
->ctx
,
345 struct gpu_array_info
, prog
->scop
->pet
->n_array
);
347 return isl_stat_error
;
349 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
350 arrays
= isl_union_set_union(arrays
,
351 isl_union_map_range(isl_union_map_copy(prog
->may_write
)));
353 arrays
= isl_union_set_apply(arrays
,
354 isl_union_map_copy(prog
->to_outer
));
356 arrays
= isl_union_set_coalesce(arrays
);
358 for (i
= 0; i
< prog
->scop
->pet
->n_array
; ++i
) {
361 field
= isl_set_is_wrapping(prog
->scop
->pet
->arrays
[i
]->extent
);
366 if (extract_array_info(prog
, &prog
->array
[prog
->n_array
++],
367 prog
->scop
->pet
->arrays
[i
], arrays
) < 0)
370 if (i
< prog
->scop
->pet
->n_array
)
373 isl_union_set_free(arrays
);
375 if (prog
->scop
->options
->live_range_reordering
)
376 collect_order_dependences(prog
);
381 static void free_array_info(struct gpu_prog
*prog
)
385 for (i
= 0; i
< prog
->n_array
; ++i
) {
386 free(prog
->array
[i
].type
);
387 free(prog
->array
[i
].name
);
388 isl_multi_pw_aff_free(prog
->array
[i
].bound
);
389 isl_ast_expr_free(prog
->array
[i
].bound_expr
);
390 isl_space_free(prog
->array
[i
].space
);
391 isl_set_free(prog
->array
[i
].declared_extent
);
392 isl_set_free(prog
->array
[i
].extent
);
393 isl_ast_expr_free(prog
->array
[i
].declared_size
);
394 free(prog
->array
[i
].refs
);
395 isl_union_map_free(prog
->array
[i
].dep_order
);
400 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
401 * as an array or through a pointer reference, but as a single data element.
402 * At the moment, scalars are represented as zero-dimensional arrays.
403 * Note that the single data element may be an entire structure.
405 int gpu_array_is_scalar(struct gpu_array_info
*array
)
407 return array
->n_index
== 0;
410 /* Can "array" be mapped to private memory?
411 * That is, is it only accessed as individual elements with
412 * constant index expressions?
414 isl_bool
gpu_array_can_be_private(struct gpu_array_info
*array
)
417 return isl_bool_error
;
418 return array
->only_fixed_element
;
421 /* Is "array" a read-only scalar?
423 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
425 return array
->read_only_scalar
;
428 /* Does "array" need to be allocated on the device?
429 * If it is a read-only scalar, then it will be passed as an argument
430 * to the kernel and therefore does not require any allocation.
431 * If this device memory is not accessed at all, then it does not
432 * need to be allocated either.
434 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
)
436 if (gpu_array_is_read_only_scalar(array
))
443 /* Return the set of parameter values for which the array has a positive
444 * size in all dimensions.
445 * If the sizes are only valid for some parameter values, then those
446 * constraints are also taken into account.
448 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
)
457 space
= isl_space_params(isl_space_copy(array
->space
));
458 guard
= isl_set_universe(space
);
460 for (i
= 0; i
< array
->n_index
; ++i
) {
462 isl_set
*guard_i
, *zero
;
464 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
465 guard_i
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound
));
466 zero
= isl_pw_aff_zero_set(bound
);
467 guard_i
= isl_set_subtract(guard_i
, zero
);
468 guard
= isl_set_intersect(guard
, guard_i
);
474 /* Internal data structure for extract_size_of_type.
475 * "type" specifies the name of the space that we want to extract.
476 * "res" is used to store the subset of that space.
478 struct ppcg_extract_size_data
{
483 /* This function is called for each set in a union_set.
484 * If the name of the set matches data->type, we store the
487 static isl_stat
extract_size_of_type(__isl_take isl_set
*size
, void *user
)
489 struct ppcg_extract_size_data
*data
= user
;
492 name
= isl_set_get_tuple_name(size
);
493 if (name
&& !strcmp(name
, data
->type
)) {
495 return isl_stat_error
;
502 /* Given a union map { kernel[i] -> *[...] },
503 * return the range in the space called "type" for the kernel with
504 * sequence number "id".
506 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
507 const char *type
, int id
)
511 isl_union_set
*local_sizes
;
512 struct ppcg_extract_size_data data
= { type
, NULL
};
517 space
= isl_union_map_get_space(sizes
);
518 space
= isl_space_set_from_params(space
);
519 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
520 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
521 dom
= isl_set_universe(space
);
522 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
524 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
525 isl_union_map_copy(sizes
));
526 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
527 isl_union_set_free(local_sizes
);
531 /* Given a singleton set, extract the first (at most *len) elements
532 * of the single integer tuple into *sizes and update *len if needed.
534 * If "set" is NULL, then the "sizes" array is not updated.
536 static isl_stat
read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
,
545 dim
= isl_set_dim(set
, isl_dim_set
);
549 for (i
= 0; i
< *len
; ++i
) {
552 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
555 sizes
[i
] = isl_val_get_num_si(v
);
563 return isl_stat_error
;
566 /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
567 * if the option debug->dump_sizes is set.
569 static void set_used_sizes(struct gpu_gen
*gen
, const char *type
, int id
,
576 if (!gen
->options
->debug
->dump_sizes
)
579 space
= isl_union_map_get_space(gen
->used_sizes
);
580 space
= isl_space_set_from_params(space
);
581 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
582 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
583 space
= isl_space_from_domain(space
);
584 space
= isl_space_add_dims(space
, isl_dim_out
, len
);
585 space
= isl_space_set_tuple_name(space
, isl_dim_out
, type
);
587 map
= isl_map_universe(space
);
588 map
= isl_map_fix_si(map
, isl_dim_in
, 0, id
);
589 for (i
= 0; i
< len
; ++i
)
590 map
= isl_map_fix_si(map
, isl_dim_out
, i
, sizes
[i
]);
592 gen
->used_sizes
= isl_union_map_add_map(gen
->used_sizes
, map
);
595 /* Extract user specified "tile" sizes from the "sizes" command line option,
596 * defaulting to option->tile_size in each dimension.
597 * *tile_len contains the maximum number of tile sizes needed.
598 * Update *tile_len to the number of specified tile sizes, if any, and
599 * return a pointer to the tile sizes (or NULL on error).
600 * Add the effectively used sizes to gen->used_sizes.
602 static int *read_tile_sizes(struct gpu_gen
*gen
, int *tile_len
)
608 tile_size
= isl_alloc_array(gen
->ctx
, int, *tile_len
);
611 for (n
= 0; n
< *tile_len
; ++n
)
612 tile_size
[n
] = gen
->options
->tile_size
;
614 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
615 if (read_sizes_from_set(size
, tile_size
, tile_len
) < 0)
617 set_used_sizes(gen
, "tile", gen
->kernel_id
, tile_size
, *tile_len
);
625 /* Extract user specified "block" sizes from the "sizes" command line option,
626 * after filling in some potentially useful defaults.
628 static isl_stat
read_block_sizes(struct ppcg_kernel
*kernel
,
629 __isl_keep isl_union_map
*sizes
)
633 if (kernel
->n_block
> 3)
635 switch (kernel
->n_block
) {
637 kernel
->block_dim
[0] = 512;
640 kernel
->block_dim
[0] = 32;
641 kernel
->block_dim
[1] = 16;
644 kernel
->block_dim
[0] = 32;
645 kernel
->block_dim
[1] = 4;
646 kernel
->block_dim
[2] = 4;
650 size
= extract_sizes(sizes
, "block", kernel
->id
);
651 return read_sizes_from_set(size
, kernel
->block_dim
, &kernel
->n_block
);
654 /* Extract user specified "grid" sizes from the "sizes" command line option,
655 * after filling in some potentially useful defaults.
657 static isl_stat
read_grid_sizes(struct ppcg_kernel
*kernel
,
658 __isl_keep isl_union_map
*sizes
)
662 if (kernel
->n_grid
> 2)
664 switch (kernel
->n_grid
) {
666 kernel
->grid_dim
[0] = 32768;
669 kernel
->grid_dim
[0] = 256;
670 kernel
->grid_dim
[1] = 256;
674 size
= extract_sizes(sizes
, "grid", kernel
->id
);
675 return read_sizes_from_set(size
, kernel
->grid_dim
, &kernel
->n_grid
);
678 /* Extract user specified grid and block sizes from the gen->sizes
679 * command line option after filling in some potentially useful defaults.
680 * Store the extracted sizes in "kernel".
681 * Add the effectively used sizes to gen->used_sizes.
683 static isl_stat
read_grid_and_block_sizes(struct ppcg_kernel
*kernel
,
686 if (read_block_sizes(kernel
, gen
->sizes
) < 0)
687 return isl_stat_error
;
688 if (read_grid_sizes(kernel
, gen
->sizes
) < 0)
689 return isl_stat_error
;
690 set_used_sizes(gen
, "block", kernel
->id
,
691 kernel
->block_dim
, kernel
->n_block
);
692 set_used_sizes(gen
, "grid", kernel
->id
,
693 kernel
->grid_dim
, kernel
->n_grid
);
697 static void *free_stmts(struct gpu_stmt
*stmts
, int n
)
704 for (i
= 0; i
< n
; ++i
) {
705 struct gpu_stmt_access
*access
, *next
;
707 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
709 isl_id_free(access
->ref_id
);
710 isl_map_free(access
->access
);
711 isl_map_free(access
->tagged_access
);
715 isl_id_free(stmts
[i
].id
);
722 /* Add parameters p[i] with identifiers "ids" to "set",
723 * with bounds to 0 <= p[i] < size[i].
725 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
726 int *size
, __isl_keep isl_id_list
*ids
)
731 len
= isl_id_list_n_id(ids
);
732 nparam
= isl_set_dim(set
, isl_dim_param
);
733 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
735 for (i
= 0; i
< len
; ++i
) {
738 id
= isl_id_list_get_id(ids
, i
);
739 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
740 set
= isl_set_lower_bound_si(set
, isl_dim_param
, nparam
+ i
, 0);
741 set
= isl_set_upper_bound_si(set
, isl_dim_param
,
742 nparam
+ i
, size
[i
] - 1);
748 /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
751 * { : 0 <= p[i] < size[i] }
753 * or an overapproximation.
755 static __isl_give isl_set
*add_bounded_parameters_dynamic(
756 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
757 __isl_keep isl_id_list
*ids
)
764 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
765 nparam
= isl_set_dim(set
, isl_dim_param
);
766 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
768 for (i
= 0; i
< len
; ++i
) {
771 id
= isl_id_list_get_id(ids
, i
);
772 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
775 space
= isl_space_params(isl_set_get_space(set
));
776 ls
= isl_local_space_from_space(space
);
777 for (i
= 0; i
< len
; ++i
) {
778 isl_pw_aff
*param
, *size_i
, *zero
;
781 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
782 isl_dim_param
, nparam
+ i
);
784 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
785 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
786 bound
= isl_set_from_basic_set(isl_set_simple_hull(bound
));
787 set
= isl_set_intersect_params(set
, bound
);
789 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
790 bound
= isl_pw_aff_ge_set(param
, zero
);
791 set
= isl_set_intersect_params(set
, bound
);
793 isl_local_space_free(ls
);
798 /* Return the union of all tagged access relations in the group.
800 static __isl_give isl_union_map
*group_tagged_access_relation(
801 struct gpu_array_ref_group
*group
)
804 isl_union_map
*access
;
806 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
807 for (i
= 0; i
< group
->n_ref
; ++i
) {
810 map_i
= isl_map_copy(group
->refs
[i
]->tagged_access
);
811 access
= isl_union_map_union(access
,
812 isl_union_map_from_map(map_i
));
818 /* Return the extent of "array", recomputed from the bounds.
819 * The recomputed extent may be simpler than the original extent.
821 static __isl_give isl_set
*array_extent(struct gpu_array_info
*array
)
829 id
= isl_set_get_tuple_id(array
->extent
);
830 space
= isl_set_get_space(array
->extent
);
831 extent
= isl_set_universe(isl_space_copy(space
));
832 ls
= isl_local_space_from_space(space
);
833 for (i
= 0; i
< array
->n_index
; ++i
) {
839 extent
= isl_set_lower_bound_si(extent
, isl_dim_set
, i
, 0);
841 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
843 index
= isl_pw_aff_from_aff(aff
);
844 bound
= isl_multi_pw_aff_get_pw_aff(array
->bound
, i
);
845 bound
= isl_pw_aff_from_range(bound
);
846 bound
= isl_pw_aff_add_dims(bound
, isl_dim_in
, array
->n_index
);
847 bound
= isl_pw_aff_set_tuple_id(bound
, isl_dim_in
,
849 lt
= isl_pw_aff_lt_set(index
, bound
);
850 extent
= isl_set_intersect(extent
, lt
);
852 isl_local_space_free(ls
);
858 /* Return a map from the first group->shared_tile->depth dimensions
859 * of the computed schedule to the array tile in
860 * global memory that corresponds to the shared memory copy.
862 * In particular, return a map
868 * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
872 * 0 <= a <= array_size - 1 (2)
874 * Note that if some stride has been detected (i.e., when
875 * group->shared_tile->bound[i].shift is set), then a in (1) refers
876 * to the shifted and scaled down version.
878 * Constraints (1) are obtained by mapping the size constraints on the
879 * shared/private memory tile back to the access relation.
880 * Constraints (2) are obtained from the (recomputed) extent.
882 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
885 int n_index
= group
->array
->n_index
;
891 space
= isl_multi_aff_get_space(group
->shared_tile
->tiling
);
892 space
= isl_space_range(space
);
893 local
= isl_set_universe(space
);
894 for (i
= 0; i
< n_index
; ++i
) {
897 local
= isl_set_lower_bound_si(local
, isl_dim_set
, i
, 0);
898 bound
= isl_val_copy(group
->shared_tile
->bound
[i
].size
);
899 bound
= isl_val_sub_ui(bound
, 1);
900 local
= isl_set_upper_bound_val(local
, isl_dim_set
, i
, bound
);
902 local
= isl_set_preimage_multi_aff(local
,
903 isl_multi_aff_copy(group
->shared_tile
->tiling
));
904 tile
= isl_set_unwrap(local
);
905 extent
= array_extent(group
->array
);
906 tile
= isl_map_intersect_range(tile
, extent
);
911 /* Given a mapping "iterator_map" from the AST schedule to a domain,
912 * return the corresponding mapping from the AST schedule to
913 * to the outer kernel->copy_schedule_dim dimensions of
914 * the schedule computed by PPCG for this kernel.
916 * Note that kernel->copy_schedule_dim is at least as large as
917 * the largest depth of any array reference group associated to the kernel.
918 * This is needed as the returned schedule is used to extract a mapping
919 * to the outer tile->depth dimensions in transform_index.
921 static __isl_give isl_pw_multi_aff
*compute_sched_to_copy(
922 struct ppcg_kernel
*kernel
, __isl_take isl_pw_multi_aff
*iterator_map
)
924 isl_union_pw_multi_aff
*upma
;
925 isl_pw_multi_aff
*pma
;
928 space
= isl_space_range(isl_pw_multi_aff_get_space(iterator_map
));
929 space
= isl_space_from_domain(space
);
930 space
= isl_space_add_dims(space
, isl_dim_out
,
931 kernel
->copy_schedule_dim
);
933 upma
= isl_union_pw_multi_aff_copy(kernel
->copy_schedule
);
934 pma
= isl_union_pw_multi_aff_extract_pw_multi_aff(upma
, space
);
935 isl_union_pw_multi_aff_free(upma
);
937 return isl_pw_multi_aff_pullback_pw_multi_aff(pma
, iterator_map
);
940 /* If max_shared_memory is not set to infinity (-1), then make
941 * sure that the total amount of shared memory required by the
942 * array reference groups mapped to shared memory by "kernel"
943 * is no larger than this maximum.
945 * We apply a greedy approach and discard (keep in global memory)
946 * those groups that would result in a total memory size that
947 * is larger than the maximum.
949 * This function should be called after any function that may
950 * affect the decision on whether to place a reference group
951 * in private, shared or global memory.
953 static void check_shared_memory_bound(struct ppcg_kernel
*kernel
)
956 isl_val
*left
, *size
;
958 if (kernel
->options
->max_shared_memory
< 0)
961 left
= isl_val_int_from_si(kernel
->ctx
,
962 kernel
->options
->max_shared_memory
);
964 for (i
= 0; i
< kernel
->n_array
; ++i
) {
965 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
967 for (j
= 0; j
< local
->n_group
; ++j
) {
968 struct gpu_array_ref_group
*group
;
969 enum ppcg_group_access_type type
;
971 group
= local
->groups
[j
];
972 type
= gpu_array_ref_group_type(group
);
973 if (type
!= ppcg_access_shared
)
976 size
= gpu_array_tile_size(group
->shared_tile
);
977 size
= isl_val_mul_ui(size
, local
->array
->size
);
979 if (isl_val_le(size
, left
)) {
980 left
= isl_val_sub(left
, size
);
986 gpu_array_tile_free(group
->shared_tile
);
993 /* Mark all arrays of "kernel" that have an array reference group
994 * that is not mapped to private or shared memory as
995 * accessing the corresponding global device memory.
997 static void mark_global_arrays(struct ppcg_kernel
*kernel
)
1001 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1002 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1006 for (j
= 0; j
< local
->n_group
; ++j
) {
1007 if (gpu_array_ref_group_tile(local
->groups
[j
]))
1011 local
->array
->global
= 1;
1017 /* Compute a tiling for all the array reference groups in "kernel".
1019 static void compute_group_tilings(struct ppcg_kernel
*kernel
)
1023 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1024 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1026 for (j
= 0; j
< array
->n_group
; ++j
)
1027 gpu_array_ref_group_compute_tiling(array
->groups
[j
]);
1031 /* Compute the effective grid size as a list of the sizes in each dimension.
1033 * The grid size specified by the user or set by default
1034 * in read_grid_sizes() and applied by the block filter,
1035 * may be too large for the given code in the sense that
1036 * it may contain blocks that don't need to execute anything.
1037 * We therefore don't return this grid size, but instead the
1038 * smallest grid size that ensures that all blocks that actually
1039 * execute code are included in the grid.
1041 * We first extract a description of the grid, i.e., the possible values
1042 * of the block ids, from the domain elements in "domain" and
1043 * kernel->block_filter.
1044 * The block ids are parameters in kernel->block_filter.
1045 * We simply need to change them into set dimensions.
1047 * Then, for each block dimension, we compute the maximal value of the block id
1050 static __isl_give isl_multi_pw_aff
*extract_grid_size(
1051 struct ppcg_kernel
*kernel
, __isl_take isl_union_set
*domain
)
1056 isl_multi_pw_aff
*size
;
1058 domain
= isl_union_set_intersect(domain
,
1059 isl_union_set_copy(kernel
->block_filter
));
1060 grid
= isl_union_set_params(domain
);
1061 grid
= isl_set_from_params(grid
);
1062 grid
= isl_set_add_dims(grid
, isl_dim_set
, kernel
->n_grid
);
1063 for (i
= 0; i
< kernel
->n_grid
; ++i
) {
1070 id
= isl_id_list_get_id(kernel
->block_ids
, i
);
1071 pos
= isl_set_find_dim_by_id(grid
, isl_dim_param
, id
);
1074 isl_die(isl_set_get_ctx(grid
), isl_error_internal
,
1075 "missing constraints on block identifier",
1076 grid
= isl_set_free(grid
));
1077 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
1078 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
1081 grid
= isl_set_coalesce(grid
);
1082 size
= ppcg_size_from_extent(grid
);
1083 context
= isl_set_params(isl_set_copy(kernel
->context
));
1084 return isl_multi_pw_aff_gist(size
, context
);
1087 /* Compute the size of a fixed bounding box around the origin and "set",
1088 * where "set" is assumed to contain only non-negative elements,
1089 * and store the results in "size".
1090 * In particular, compute the maximal value of "set" in each direction
1093 static void extract_fixed_size(__isl_take isl_set
*set
, int *size
)
1096 isl_local_space
*ls
;
1099 n
= isl_set_dim(set
, isl_dim_set
);
1100 ls
= isl_local_space_from_space(isl_set_get_space(set
));
1101 obj
= isl_aff_zero_on_domain(ls
);
1102 for (i
= 0; i
< n
; ++i
) {
1105 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 1);
1106 max
= isl_set_max_val(set
, obj
);
1107 size
[i
] = isl_val_get_num_si(max
) + 1;
1109 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 0);
1115 /* Compute the effective block size as a list of the sizes in each dimension
1116 * and store the sizes in kernel->block_dim.
1118 * The block size specified by the user or set by default
1119 * in read_block_sizes() and applied by the thread filter,
1120 * may be too large for the given code in the sense that
1121 * it may contain threads that don't need to execute anything.
1122 * We therefore update this block size in kernel->block_dim
1123 * to the smallest block size that ensures that all threads
1124 * that actually execute code are included in the block.
1126 * The set of possible values of the thread ids is obtained from
1127 * the domain elements "domain" and kernel->thread_filter.
1128 * The current implementation eliminates all parameters, ensuring
1129 * that the size is a fixed constant in each dimension.
1130 * In principle we could also compute parametric sizes.
1131 * We would have to make sure to project out all b%d and t%d parameters,
1134 static isl_stat
extract_block_size(struct ppcg_kernel
*kernel
,
1135 __isl_take isl_union_set
*domain
)
1141 domain
= isl_union_set_intersect(domain
,
1142 isl_union_set_copy(kernel
->thread_filter
));
1143 block
= isl_union_set_params(domain
);
1144 block
= isl_set_from_params(block
);
1145 block
= isl_set_add_dims(block
, isl_dim_set
, kernel
->n_block
);
1146 for (i
= 0; i
< kernel
->n_block
; ++i
) {
1151 return isl_stat_error
;
1153 id
= isl_id_list_get_id(kernel
->thread_ids
, i
);
1154 pos
= isl_set_find_dim_by_id(block
, isl_dim_param
, id
);
1157 isl_die(isl_set_get_ctx(block
), isl_error_internal
,
1158 "missing constraints on thread identifier",
1159 block
= isl_set_free(block
));
1160 block
= isl_set_equate(block
, isl_dim_param
, pos
,
1163 nparam
= isl_set_dim(block
, isl_dim_param
);
1164 block
= isl_set_project_out(block
, isl_dim_param
, 0, nparam
);
1167 return isl_stat_error
;
1169 extract_fixed_size(block
, kernel
->block_dim
);
1174 struct ppcg_kernel
*ppcg_kernel_free(struct ppcg_kernel
*kernel
)
1181 isl_id_list_free(kernel
->block_ids
);
1182 isl_id_list_free(kernel
->thread_ids
);
1183 isl_multi_pw_aff_free(kernel
->grid_size
);
1184 isl_ast_expr_free(kernel
->grid_size_expr
);
1185 isl_set_free(kernel
->context
);
1186 isl_union_set_free(kernel
->core
);
1187 isl_union_set_free(kernel
->arrays
);
1188 isl_union_pw_multi_aff_free(kernel
->contraction
);
1189 isl_union_set_free(kernel
->expanded_domain
);
1190 isl_space_free(kernel
->space
);
1191 isl_ast_node_free(kernel
->tree
);
1192 isl_union_set_free(kernel
->block_filter
);
1193 isl_union_set_free(kernel
->thread_filter
);
1194 isl_union_pw_multi_aff_free(kernel
->copy_schedule
);
1195 isl_union_set_free(kernel
->sync_writes
);
1197 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1198 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1200 for (j
= 0; j
< array
->n_group
; ++j
)
1201 gpu_array_ref_group_free(array
->groups
[j
]);
1202 free(array
->groups
);
1204 isl_multi_pw_aff_free(array
->bound
);
1205 isl_ast_expr_free(array
->bound_expr
);
1207 free(kernel
->array
);
1209 for (i
= 0; i
< kernel
->n_var
; ++i
) {
1210 free(kernel
->var
[i
].name
);
1211 isl_vec_free(kernel
->var
[i
].size
);
1220 /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
1222 static void ppcg_kernel_free_wrap(void *user
)
1224 struct ppcg_kernel
*kernel
= user
;
1226 ppcg_kernel_free(kernel
);
1229 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
1230 struct ppcg_kernel_var
*var
)
1233 struct gpu_array_tile
*tile
;
1236 var
->array
= group
->array
;
1238 var
->type
= gpu_array_ref_group_type(group
);
1239 tile
= gpu_array_ref_group_tile(group
);
1241 p
= isl_printer_to_str(ctx
);
1242 p
= gpu_array_ref_group_print_name(group
, p
);
1243 var
->name
= isl_printer_get_str(p
);
1244 isl_printer_free(p
);
1246 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
1248 for (j
= 0; j
< group
->array
->n_index
; ++j
)
1249 var
->size
= isl_vec_set_element_val(var
->size
, j
,
1250 isl_val_copy(tile
->bound
[j
].size
));
1253 static isl_stat
create_kernel_vars(struct ppcg_kernel
*kernel
)
1258 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1259 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1261 for (j
= 0; j
< array
->n_group
; ++j
) {
1262 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1263 enum ppcg_group_access_type type
;
1265 type
= gpu_array_ref_group_type(group
);
1266 if (type
!= ppcg_access_global
)
1271 kernel
->var
= isl_calloc_array(kernel
->ctx
, struct ppcg_kernel_var
, n
);
1273 return isl_stat_error
;
1277 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1278 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1280 for (j
= 0; j
< array
->n_group
; ++j
) {
1281 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1282 enum ppcg_group_access_type type
;
1284 type
= gpu_array_ref_group_type(group
);
1285 if (type
== ppcg_access_global
)
1287 create_kernel_var(kernel
->ctx
, group
, &kernel
->var
[n
]);
1295 /* Replace "pa" by the zero function defined over the universe domain
1296 * in the space of "pa".
1298 static __isl_give isl_pw_aff
*set_universally_zero(__isl_take isl_pw_aff
*pa
)
1303 space
= isl_space_domain(isl_pw_aff_get_space(pa
));
1304 isl_pw_aff_free(pa
);
1305 zero
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1307 return isl_pw_aff_from_aff(zero
);
1310 /* The sizes of the arrays on the host that have been computed by
1311 * extract_array_info may depend on the parameters. Use the extra
1312 * constraints on the parameters that are valid at "host_domain"
1313 * to simplify these expressions and store the results in kernel->array.
1315 * We only need these localized bounds for arrays that are accessed
1316 * by the current kernel. If we have found at least one reference group
1317 * then the array is accessed by the kernel.
1319 * The resulting sizes may be functions that are nowhere defined
1320 * in case the access function cannot possibly access anything inside
1321 * the kernel for some reason. If so, they are replaced by the zero
1322 * function. Since the access function cannot actually access anything,
1323 * there is no harm in printing the array sizes as zero.
1325 static void localize_bounds(struct ppcg_kernel
*kernel
,
1326 __isl_keep isl_set
*host_domain
)
1331 context
= isl_set_copy(host_domain
);
1332 context
= isl_set_params(context
);
1334 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1335 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1336 isl_multi_pw_aff
*bound
;
1339 if (local
->n_group
== 0)
1342 n_index
= local
->array
->n_index
;
1343 bound
= isl_multi_pw_aff_copy(local
->array
->bound
);
1345 for (j
= 0; j
< n_index
; ++j
) {
1349 pwaff
= isl_multi_pw_aff_get_pw_aff(bound
, j
);
1350 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
1351 empty
= isl_pw_aff_is_empty(pwaff
);
1353 pwaff
= isl_pw_aff_free(pwaff
);
1355 pwaff
= set_universally_zero(pwaff
);
1356 bound
= isl_multi_pw_aff_set_pw_aff(bound
, j
, pwaff
);
1359 local
->n_index
= n_index
;
1360 local
->bound
= bound
;
1362 isl_set_free(context
);
1365 /* Create the array of gpu_local_array_info structures "array"
1366 * inside "kernel". The number of elements in this array is
1367 * the same as the number of arrays in "prog".
1368 * Initialize the "array" field of each local array to point
1369 * to the corresponding array in "prog".
1371 static struct ppcg_kernel
*ppcg_kernel_create_local_arrays(
1372 struct ppcg_kernel
*kernel
, struct gpu_prog
*prog
)
1380 ctx
= isl_set_get_ctx(prog
->context
);
1381 kernel
->array
= isl_calloc_array(ctx
,
1382 struct gpu_local_array_info
, prog
->n_array
);
1384 return ppcg_kernel_free(kernel
);
1385 kernel
->n_array
= prog
->n_array
;
1387 for (i
= 0; i
< prog
->n_array
; ++i
)
1388 kernel
->array
[i
].array
= &prog
->array
[i
];
1393 /* Does "kernel" need to be passed an argument corresponding to array "i"?
1395 * The argument is only needed if the kernel accesses this device memory.
1397 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
)
1399 return kernel
->array
[i
].global
;
1402 /* Find the element in gen->stmt that has the given "id".
1403 * Return NULL if no such gpu_stmt can be found.
1405 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
1409 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
1410 if (id
== prog
->stmts
[i
].id
)
1414 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
1417 void ppcg_kernel_stmt_free(void *user
)
1419 struct ppcg_kernel_stmt
*stmt
= user
;
1424 switch (stmt
->type
) {
1425 case ppcg_kernel_copy
:
1426 isl_ast_expr_free(stmt
->u
.c
.index
);
1427 isl_ast_expr_free(stmt
->u
.c
.local_index
);
1429 case ppcg_kernel_domain
:
1430 isl_id_to_ast_expr_free(stmt
->u
.d
.ref2expr
);
1432 case ppcg_kernel_sync
:
1439 /* Return the gpu_stmt_access in the list "accesses" that corresponds
1442 static struct gpu_stmt_access
*find_access(struct gpu_stmt_access
*accesses
,
1443 __isl_keep isl_id
*ref_id
)
1445 struct gpu_stmt_access
*access
;
1447 for (access
= accesses
; access
; access
= access
->next
)
1448 if (access
->ref_id
== ref_id
)
1454 /* Return the index of the array called "name" in the list of arrays.
1456 static int find_array_index(struct ppcg_kernel
*kernel
, const char *name
)
1460 for (i
= 0; i
< kernel
->n_array
; ++i
)
1461 if (!strcmp(name
, kernel
->array
[i
].array
->name
))
1467 /* Internal data structure for the index and AST expression transformation
1468 * callbacks for pet_stmt_build_ast_exprs.
1470 * "kernel" is the kernel for which are computing AST expressions and
1471 * may be NULL if we are not inside a kernel.
1472 * "accesses" is the list of gpu_stmt_access in the statement.
1473 * "iterator_map" expresses the statement iterators in terms of
1474 * the AST loop iterators.
1475 * "sched2copy" expresses the outer copy_schedule_dim dimensions of
1476 * the kernel schedule in terms of the AST loop iterators and
1477 * may be NULL if we are not inside a kernel.
1479 * The following fields are set in transform_index and used in transform_expr.
1480 * "array" is the array that is being accessed.
1481 * "global" is set if the global array is accessed (rather than
1482 * shared/private memory).
1483 * "local_array" refers to information on the array specialized
1484 * to the current kernel.
1486 struct ppcg_transform_data
{
1487 struct ppcg_kernel
*kernel
;
1488 struct gpu_stmt_access
*accesses
;
1489 isl_pw_multi_aff
*iterator_map
;
1490 isl_pw_multi_aff
*sched2copy
;
1492 struct gpu_array_info
*array
;
1494 struct gpu_local_array_info
*local_array
;
1497 /* Return a pointer to the gpu_array_ref_group in "local"
1498 * that contains the reference "access".
1499 * Return NULL if no such group can be found.
1501 static struct gpu_array_ref_group
*find_ref_group(
1502 struct gpu_local_array_info
*local
, struct gpu_stmt_access
*access
)
1506 for (i
= 0; i
< local
->n_group
; ++i
) {
1507 struct gpu_array_ref_group
*group
= local
->groups
[i
];
1509 for (j
= 0; j
< group
->n_ref
; ++j
)
1510 if (group
->refs
[j
] == access
)
1517 /* Given an index expression "index" of the form
1521 * with F(A) either A or some subfield of A and L the AST loop iterators,
1522 * and a tiling "tiling" of the form
1526 * apply the tiling to the outer array in the index expression to obtain
1530 * If F(A) is some subfield of A, then separate the member access
1531 * into the base index expression and the field index expression,
1532 * apply the tiling to the base index expression and combine the result
1533 * with the field index expression.
1535 * If F(A) is A, then modify index to keep track of the iterators
1539 * and combine the result with the tiling to obtain a tiled index expression
1540 * in terms of the AST loop iterators
1544 static __isl_give isl_multi_pw_aff
*tile_outer(
1545 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_multi_pw_aff
*tiling
)
1547 isl_bool is_wrapping
;
1549 isl_multi_pw_aff
*mpa
;
1551 is_wrapping
= isl_multi_pw_aff_range_is_wrapping(index
);
1552 if (is_wrapping
< 0)
1555 isl_multi_pw_aff
*field
;
1557 field
= isl_multi_pw_aff_copy(index
);
1558 field
= isl_multi_pw_aff_range_factor_range(field
);
1559 index
= isl_multi_pw_aff_range_factor_domain(index
);
1560 index
= tile_outer(index
, tiling
);
1561 return isl_multi_pw_aff_range_product(index
, field
);
1564 space
= isl_space_domain(isl_multi_pw_aff_get_space(index
));
1565 space
= isl_space_map_from_set(space
);
1566 mpa
= isl_multi_pw_aff_identity(space
);
1567 index
= isl_multi_pw_aff_range_product(mpa
, index
);
1568 index
= isl_multi_pw_aff_pullback_multi_pw_aff(tiling
, index
);
1572 isl_multi_pw_aff_free(index
);
1573 isl_multi_pw_aff_free(tiling
);
1577 /* Index transformation callback for pet_stmt_build_ast_exprs.
1579 * "index" expresses the array indices in terms of statement iterators
1581 * We first reformulate "index" in terms of the AST loop iterators.
1582 * Then we check if we are accessing the global array or
1583 * a shared/private copy. In particular, if we are not inside a kernel
1584 * then we must be accessing a global array.
1585 * In the former case, we simply return
1586 * the updated index. If "index" is an affine expression rather
1587 * than an array access, then we also return the updated index here.
1589 * If no reference groups have been computed for the array,
1590 * then we can only be accessing the global array.
1592 * Otherwise, we apply the tiling to the index.
1593 * This tiling is of the form
1597 * where D corresponds to the outer tile->depth dimensions of
1598 * the kernel schedule.
1599 * The index is of the form
1603 * We update the tiling to refer to the AST loop iterators
1607 * and combine it with the index to obtain a tiled index expression in terms
1608 * of the AST loop iterators
1612 * Note that while the tiling applies directly to an outer array.
1613 * the index may refer to some subfield of this outer array.
1614 * In such cases, the result will refer to the same subfield of the tile.
1615 * That is, an index expression of the form L -> F(A) will be transformed
1616 * into an index expression of the form L -> F(T).
1618 static __isl_give isl_multi_pw_aff
*transform_index(
1619 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*ref_id
,
1622 struct ppcg_transform_data
*data
= user
;
1623 struct gpu_stmt_access
*access
;
1624 struct gpu_array_ref_group
*group
;
1625 struct gpu_array_tile
*tile
;
1626 isl_pw_multi_aff
*iterator_map
;
1631 isl_multi_pw_aff
*tiling
;
1632 isl_pw_multi_aff
*pma
;
1633 isl_pw_multi_aff
*sched2depth
;
1637 iterator_map
= isl_pw_multi_aff_copy(data
->iterator_map
);
1638 index
= isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
1643 access
= find_access(data
->accesses
, ref_id
);
1646 if (!isl_map_has_tuple_name(access
->access
, isl_dim_out
))
1649 name
= get_outer_array_name(access
->access
);
1651 return isl_multi_pw_aff_free(index
);
1652 i
= find_array_index(data
->kernel
, name
);
1654 isl_die(isl_multi_pw_aff_get_ctx(index
), isl_error_internal
,
1655 "cannot find array",
1656 return isl_multi_pw_aff_free(index
));
1657 data
->local_array
= &data
->kernel
->array
[i
];
1658 data
->array
= data
->local_array
->array
;
1660 group
= find_ref_group(data
->local_array
, access
);
1666 tile
= gpu_array_ref_group_tile(group
);
1667 data
->global
= !tile
;
1671 space
= isl_space_domain(isl_multi_aff_get_space(tile
->tiling
));
1672 space
= isl_space_range(isl_space_unwrap(space
));
1673 space
= isl_space_map_from_set(space
);
1674 pma
= isl_pw_multi_aff_identity(space
);
1675 sched2depth
= isl_pw_multi_aff_copy(data
->sched2copy
);
1676 dim
= isl_pw_multi_aff_dim(sched2depth
, isl_dim_out
);
1677 sched2depth
= isl_pw_multi_aff_drop_dims(sched2depth
, isl_dim_out
,
1678 tile
->depth
, dim
- tile
->depth
);
1679 pma
= isl_pw_multi_aff_product(sched2depth
, pma
);
1680 tiling
= isl_multi_pw_aff_from_multi_aff(
1681 isl_multi_aff_copy(tile
->tiling
));
1682 tiling
= isl_multi_pw_aff_pullback_pw_multi_aff(tiling
, pma
);
1684 index
= tile_outer(index
, tiling
);
1689 /* Dereference "expr" by adding an index [0].
1690 * The original "expr" is assumed not to have any indices.
1692 * If "expr" is a member access, then the dereferencing needs
1693 * to be applied to the structure argument of this member access.
1695 static __isl_give isl_ast_expr
*dereference(__isl_take isl_ast_expr
*expr
)
1698 isl_ast_expr
*arg0
, *res
;
1699 isl_ast_expr_list
*list
;
1701 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1703 return isl_ast_expr_free(expr
);
1704 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1705 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1708 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1709 arg
= dereference(arg
);
1710 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1711 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1715 isl_ast_expr_free(arg0
);
1717 ctx
= isl_ast_expr_get_ctx(expr
);
1718 res
= isl_ast_expr_from_val(isl_val_zero(ctx
));
1719 list
= isl_ast_expr_list_from_ast_expr(res
);
1720 res
= isl_ast_expr_get_op_arg(expr
, 0);
1721 res
= isl_ast_expr_access(res
, list
);
1722 isl_ast_expr_free(expr
);
1727 /* Linearize the index expression "expr" based on the array bounds
1730 * That is, transform expression
1732 * A[i_0][i_1]...[i_n]
1736 * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
1738 * where b_0, b_1, ..., b_n are the bounds on the array.
1740 * If the base of "expr" is a member access, then the linearization needs
1741 * to be applied to the structure argument of this member access.
1743 * In the base case, if "expr" has no arguments (other than the name of
1744 * the array), then we are passing an entire array to a function.
1745 * In this case, there is nothing to linearize.
1746 * Note that at this point an expression with no arguments can
1747 * only be an entire array because the scalar case and
1748 * the case of single struct are handled by the caller.
1750 * If the number of specified index expressions in "expr"
1751 * is smaller than the dimension of the accessed array,
1752 * then the missing i_j also do not appear in the linearized expression.
1753 * Furthermore, since such an expression does not refer to a single
1754 * element while the default linearized expression would refer to
1755 * a single element, we return the expression
1757 * A + (..((i_0 * b_1 + i_1) ... ) * b_l + i_l)
1759 * instead. Note that because of the special case handling above,
1760 * we can assume here that there is at least one index expression.
1762 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
1763 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
)
1768 isl_ast_expr_list
*list
;
1770 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
1771 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
1772 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
1775 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
1776 arg
= gpu_local_array_info_linearize_index(array
, arg
);
1777 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
1778 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
1782 isl_ast_expr_free(arg0
);
1784 if (isl_ast_expr_get_op_n_arg(expr
) == 1)
1787 n
= isl_ast_expr_get_op_n_arg(expr
);
1788 res
= isl_ast_expr_get_op_arg(expr
, 1);
1789 for (i
= 1; i
< array
->n_index
; ++i
) {
1790 isl_ast_expr
*expr_i
;
1792 expr_i
= isl_ast_expr_get_op_arg(array
->bound_expr
, 1 + i
);
1793 res
= isl_ast_expr_mul(res
, expr_i
);
1797 expr_i
= isl_ast_expr_get_op_arg(expr
, i
+ 1);
1798 res
= isl_ast_expr_add(res
, expr_i
);
1801 if (1 + array
->n_index
> n
) {
1802 res
= isl_ast_expr_add(isl_ast_expr_get_op_arg(expr
, 0), res
);
1804 list
= isl_ast_expr_list_from_ast_expr(res
);
1805 res
= isl_ast_expr_get_op_arg(expr
, 0);
1806 res
= isl_ast_expr_access(res
, list
);
1809 isl_ast_expr_free(expr
);
1814 /* AST expression transformation callback for pet_stmt_build_ast_exprs.
1816 * If the AST expression refers to an array that is not accessed
1817 * at all, then this means the value of the expression is not used,
1818 * so we might as well print zero (NULL pointer) instead.
1820 * If the AST expression refers to a global scalar that is not
1821 * a read-only scalar, then its address was passed to the kernel and
1822 * we need to dereference it.
1824 * If the AST expression refers to an access to a global array,
1825 * then we linearize the access exploiting the bounds in data->local_array.
1827 static __isl_give isl_ast_expr
*transform_expr(__isl_take isl_ast_expr
*expr
,
1828 __isl_keep isl_id
*id
, void *user
)
1830 struct ppcg_transform_data
*data
= user
;
1834 if (!data
->array
->accessed
) {
1837 ctx
= isl_ast_expr_get_ctx(expr
);
1838 isl_ast_expr_free(expr
);
1839 return isl_ast_expr_from_val(isl_val_zero(ctx
));
1841 if (gpu_array_is_read_only_scalar(data
->array
))
1845 if (data
->array
->n_index
== 0)
1846 return dereference(expr
);
1847 if (!data
->array
->linearize
)
1850 return gpu_local_array_info_linearize_index(data
->local_array
, expr
);
1853 /* This function is called for each instance of a user statement
1854 * in the kernel "kernel", identified by "gpu_stmt".
1855 * "kernel" may be NULL if we are not inside a kernel.
1857 * We attach a struct ppcg_kernel_stmt to the "node", containing
1858 * a computed AST expression for each access, through an annotation
1860 * These AST expressions are computed from iterator_map,
1861 * which expresses the domain
1862 * elements in terms of the generated loops, and sched2copy,
1863 * which expresses the outer copy_schedule_dim dimensions of
1864 * the kernel schedule computed by PPCG in terms of the generated loops.
1866 static __isl_give isl_ast_node
*create_domain_leaf(
1867 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
1868 __isl_keep isl_ast_build
*build
, struct gpu_stmt
*gpu_stmt
)
1870 struct ppcg_transform_data data
;
1871 struct ppcg_kernel_stmt
*stmt
;
1874 isl_pw_multi_aff
*sched2copy
;
1876 isl_pw_multi_aff
*iterator_map
;
1877 isl_union_map
*schedule
;
1881 ctx
= isl_ast_node_get_ctx(node
);
1883 stmt
= isl_calloc_type(ctx
, struct ppcg_kernel_stmt
);
1885 return isl_ast_node_free(node
);
1887 schedule
= isl_ast_build_get_schedule(build
);
1888 map
= isl_map_reverse(isl_map_from_union_map(schedule
));
1889 iterator_map
= isl_pw_multi_aff_from_map(map
);
1891 sched2copy
= compute_sched_to_copy(kernel
,
1892 isl_pw_multi_aff_copy(iterator_map
));
1896 stmt
->type
= ppcg_kernel_domain
;
1897 stmt
->u
.d
.stmt
= gpu_stmt
;
1899 data
.kernel
= kernel
;
1900 data
.accesses
= stmt
->u
.d
.stmt
->accesses
;
1901 data
.iterator_map
= iterator_map
;
1902 data
.sched2copy
= sched2copy
;
1903 stmt
->u
.d
.ref2expr
= pet_stmt_build_ast_exprs(stmt
->u
.d
.stmt
->stmt
,
1904 build
, &transform_index
, &data
,
1905 &transform_expr
, &data
);
1907 isl_pw_multi_aff_free(iterator_map
);
1908 isl_pw_multi_aff_free(sched2copy
);
1910 id
= isl_id_alloc(ctx
, "user", stmt
);
1911 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
1913 ppcg_kernel_stmt_free(stmt
);
1914 return isl_ast_node_set_annotation(node
, id
);
1917 /* This function is called for each statement node in the AST
1918 * for copying to or from shared/private memory.
1919 * Attach a pointer to a ppcg_kernel_stmt representing the copy
1920 * statement to the node.
1921 * The statement name is "read" or "write", depending on whether we are
1922 * reading from global memory or writing to global memory.
1924 * The schedule is of the form
1928 * where D corresponds to the outer tile->depth dimensions of
1929 * the kernel schedule, A to the global array and L to the outer
1930 * generated AST schedule.
1931 * We compute the inverse and strip off the type, resulting in
1935 * We combine this mapping with on the one hand the projection
1939 * and on the other hand the group tiling
1947 * and store the corresponding expressions in stmt->index and stmt->local_index,
1948 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
1949 * stmt->index is linearized if the global memory array is linearized.
1951 static __isl_give isl_ast_node
*create_access_leaf(struct ppcg_kernel
*kernel
,
1952 struct gpu_array_ref_group
*group
, __isl_take isl_ast_node
*node
,
1953 __isl_keep isl_ast_build
*build
)
1955 struct ppcg_kernel_stmt
*stmt
;
1956 struct gpu_array_tile
*tile
;
1961 isl_pw_multi_aff
*pma
, *pma2
;
1964 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
1966 return isl_ast_node_free(node
);
1968 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
1969 type
= isl_map_get_tuple_name(access
, isl_dim_in
);
1970 stmt
->u
.c
.read
= type
&& !strcmp(type
, "read");
1971 access
= isl_map_reverse(access
);
1972 pma
= isl_pw_multi_aff_from_map(access
);
1973 pma
= isl_pw_multi_aff_reset_tuple_id(pma
, isl_dim_out
);
1975 space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
1976 space
= isl_space_unwrap(space
);
1977 pma2
= isl_pw_multi_aff_range_map(space
);
1978 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
,
1979 isl_pw_multi_aff_copy(pma
));
1980 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1981 if (group
->array
->linearize
)
1982 expr
= gpu_local_array_info_linearize_index(group
->local_array
,
1984 stmt
->u
.c
.index
= expr
;
1986 tile
= gpu_array_ref_group_tile(group
);
1987 pma2
= isl_pw_multi_aff_from_multi_aff(
1988 isl_multi_aff_copy(tile
->tiling
));
1989 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
, pma
);
1990 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
1991 stmt
->u
.c
.local_index
= expr
;
1993 stmt
->u
.c
.array
= group
->array
;
1994 stmt
->u
.c
.local_array
= group
->local_array
;
1995 stmt
->type
= ppcg_kernel_copy
;
1997 id
= isl_id_alloc(kernel
->ctx
, "copy", stmt
);
1998 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
2000 ppcg_kernel_stmt_free(stmt
);
2001 return isl_ast_node_set_annotation(node
, id
);
2004 /* Create a synchronization ppcg_kernel_stmt and
2005 * attach it to the node "node" representing the synchronization.
2007 static __isl_give isl_ast_node
*create_sync_leaf(
2008 struct ppcg_kernel
*kernel
, __isl_take isl_ast_node
*node
,
2009 __isl_keep isl_ast_build
*build
)
2011 struct ppcg_kernel_stmt
*stmt
;
2014 stmt
= isl_calloc_type(kernel
->ctx
, struct ppcg_kernel_stmt
);
2016 return isl_ast_node_free(node
);
2018 stmt
->type
= ppcg_kernel_sync
;
2019 id
= isl_id_alloc(kernel
->ctx
, "sync", stmt
);
2020 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
2022 ppcg_kernel_stmt_free(stmt
);
2023 return isl_ast_node_set_annotation(node
, id
);
2026 /* Build AST expressions for the device array sizes of all arrays in "prog"
2027 * that require allocation on the device using "build", as well as
2028 * for the original array sizes of all arrays that need to be declared
2030 * "node" is freed in case of error.
2032 static __isl_give isl_ast_node
*build_array_bounds(
2033 __isl_take isl_ast_node
*node
, struct gpu_prog
*prog
,
2034 __isl_keep isl_ast_build
*build
)
2038 for (i
= 0; i
< prog
->n_array
; ++i
) {
2039 struct gpu_array_info
*array
= &prog
->array
[i
];
2040 isl_multi_pw_aff
*size
;
2043 if (!gpu_array_requires_device_allocation(array
))
2046 size
= isl_multi_pw_aff_copy(array
->bound
);
2047 expr
= ppcg_build_size_expr(size
, build
);
2048 array
->bound_expr
= expr
;
2050 return isl_ast_node_free(node
);
2053 for (i
= 0; i
< prog
->n_array
; ++i
) {
2054 struct gpu_array_info
*array
= &prog
->array
[i
];
2056 isl_multi_pw_aff
*size
;
2059 if (!array
->declare_local
)
2061 extent
= isl_set_copy(array
->declared_extent
);
2062 size
= ppcg_size_from_extent(extent
);
2063 expr
= ppcg_build_size_expr(size
, build
);
2064 array
->declared_size
= expr
;
2066 return isl_ast_node_free(node
);
2072 /* Internal data structure for at_domain.
2074 * "prog" represents the entire scop.
2075 * "kernel" points to the kernel to which the current schedule node
2076 * belongs. It is set by before_mark and reset by after_mark.
2077 * It may be NULL if we are outside any kernel.
2079 struct ppcg_at_domain_data
{
2080 struct gpu_prog
*prog
;
2081 struct ppcg_kernel
*kernel
;
2084 /* This function is called for each instance of a user statement
2085 * in the kernel. This may be one of the original user statements
2086 * or a statement introduced by PPCG.
2088 * We first check if the statement id corresponds to a gpu statement,
2089 * which indicates the statement is an original user statement. Any statement
2090 * that is not an original user statement has been introduced by PPCG and
2091 * requires special handling.
2093 * If the user statement is one of the original user statements, then we call
2094 * create_domain_leaf. If it is "init_device", then we call
2095 * build_array_bounds. Otherwise, we check if it is a copy or synchronization
2096 * statement and call the appropriate functions. Statements that copy an array
2097 * to/from the device do not need any further treatment.
2098 * Neither does "clear_device".
2100 static __isl_give isl_ast_node
*at_domain(__isl_take isl_ast_node
*node
,
2101 __isl_keep isl_ast_build
*build
, void *user
)
2103 struct ppcg_at_domain_data
*data
= user
;
2104 struct gpu_stmt
*gpu_stmt
;
2105 isl_ast_expr
*expr
, *arg
;
2111 expr
= isl_ast_node_user_get_expr(node
);
2112 arg
= isl_ast_expr_get_op_arg(expr
, 0);
2113 id
= isl_ast_expr_get_id(arg
);
2114 name
= isl_id_get_name(id
);
2115 p
= isl_id_get_user(id
);
2116 isl_ast_expr_free(expr
);
2117 isl_ast_expr_free(arg
);
2119 gpu_stmt
= find_stmt(data
->prog
, id
);
2120 is_sync
= gpu_tree_id_is_sync(id
, data
->kernel
);
2124 return create_domain_leaf(data
->kernel
, node
, build
, gpu_stmt
);
2126 if (!prefixcmp(name
, "to_device_") || !prefixcmp(name
, "from_device_"))
2128 if (!strcmp(name
, "init_device"))
2129 return build_array_bounds(node
, data
->prog
, build
);
2130 if (!strcmp(name
, "clear_device"))
2133 return isl_ast_node_free(node
);
2134 if (!strcmp(name
, "read") || !strcmp(name
, "write")) {
2135 struct gpu_array_ref_group
*group
= p
;
2136 return create_access_leaf(data
->kernel
, group
, node
, build
);
2139 isl_die(data
->prog
->ctx
, isl_error_internal
,
2140 "unknown statement type",
2141 return isl_ast_node_free(node
));
2142 return create_sync_leaf(data
->kernel
, node
, build
);
2145 /* Given a set of wrapped references "ref", return the corresponding
2146 * access relations based on the tagged access relations "tagged".
2148 * The elements of "ref" are of the form
2152 * with D an iteration domains and R a reference.
2153 * The elements of "tagged" are of the form
2159 * Extend "tagged" to include the iteration domain in the range, i.e.,
2161 * [D -> R] -> [D -> A]
2163 * apply the result to "ref" and then unwrap the resulting set
2164 * to obtain relations of the form
2168 static __isl_give isl_union_map
*wrapped_reference_to_access(
2169 __isl_take isl_union_set
*ref
, __isl_take isl_union_map
*tagged
)
2171 isl_union_map
*tag2access
;
2173 tag2access
= isl_union_map_copy(tagged
);
2174 tag2access
= isl_union_map_universe(tag2access
);
2175 tag2access
= isl_union_set_unwrap(isl_union_map_domain(tag2access
));
2176 tag2access
= isl_union_map_domain_map(tag2access
);
2177 tag2access
= isl_union_map_range_product(tag2access
, tagged
);
2179 ref
= isl_union_set_coalesce(ref
);
2180 ref
= isl_union_set_apply(ref
, tag2access
);
2182 return isl_union_set_unwrap(ref
);
2185 /* Given an access relation "access" from one or more array reference groups,
2186 * remove those reads if ("read" is 1) or writes (if "read" is 0)
2187 * that are only needed to communicate data within
2188 * the same iteration of "sched".
2189 * The domain of "sched" corresponds to the original statement instances,
2190 * i.e., those that appear in the domains of the access relations.
2191 * "tagged" contains all tagged access relations to all
2192 * the array reference groups accessed by "access" from statement
2193 * instances scheduled by "sched".
2195 * If the access is a read then it is either an element of
2197 * live_in union (range flow)
2199 * where live_in and flow may be overapproximations, or
2200 * it reads an uninitialized value (that is not live-in because
2201 * there is an intermediate kill) or it reads a value that was
2202 * written within the same (compound) statement instance.
2203 * If the access is a write then it is either an element of
2205 * live_out union (domain flow)
2207 * or it writes a value that is never read (and is not live-out
2208 * because of an intermediate kill) or only
2209 * within the same (compound) statement instance.
2210 * In both cases, the access relation is also a subset of
2211 * the group access relation.
2213 * The cases where an uninitialized value is read or a value is written
2214 * that is never read or where the dataflow occurs within a statement
2215 * instance are also considered local and may also be removed.
2217 * Essentially, we compute the intersection of "access" with either
2219 * live_in union (range non-local-flow)
2223 * live_out union (domain non-local-flow)
2225 * We first construct a relation "local"
2227 * [[D -> R] -> [D' -> R']]
2229 * of pairs of domain iterations accessing the reference group
2230 * and references in the group that are coscheduled by "sched".
2232 * If this relation does not intersect the dataflow dependences,
2233 * then there is nothing we can possibly remove, unless the dataflow
2234 * dependences themselves only relate a subset of the accesses.
2235 * In particular, the accesses may not be involved in any dataflow
2236 * dependences, either because they are uninitialized reads/dead writes
2237 * or because the dataflow occurs inside a statement instance.
2239 * Since the computation below may break up the access relation
2240 * into smaller pieces, we only perform the intersection with
2241 * the non-local dependent accesses if the local pairs
2242 * intersect the dataflow dependences. Otherwise, we intersect
2243 * with the universe of the non-local dependent accesses.
2244 * This should at least remove accesses from statements that
2245 * do not participate in any dependences.
2247 * In particular, we remove the "local" dataflow dependences from
2248 * the set of all dataflow dependences, or at least those
2249 * that may contribute to a domain/range that intersects
2250 * the domain of "access".
2251 * Note that if the potential dataflow dependences are an overapproximation
2252 * of the actual dataflow dependences, then the result remains an
2253 * overapproximation of the non-local dataflow dependences.
2254 * Copying to/from global memory is only needed for the references
2255 * in the domain/range of the result or for accesses that are live out/in
2256 * for the entire scop.
2258 * We therefore map the domain/range of the "external" relation
2259 * to the corresponding access relation and take the union with
2260 * the live out/in relation.
2262 static __isl_give isl_union_map
*remove_local_accesses(
2263 struct gpu_prog
*prog
, __isl_take isl_union_map
*tagged
,
2264 __isl_take isl_union_map
*access
, __isl_take isl_union_map
*sched
,
2268 isl_union_pw_multi_aff
*tagger
;
2269 isl_union_set
*domain
, *access_domain
;
2270 isl_union_map
*local
, *external
, *universe
;
2271 isl_union_set
*tag_set
;
2273 if (isl_union_map_is_empty(access
)) {
2274 isl_union_map_free(sched
);
2275 isl_union_map_free(tagged
);
2279 tagger
= isl_union_pw_multi_aff_copy(prog
->scop
->tagger
);
2280 domain
= isl_union_map_domain(isl_union_map_copy(tagged
));
2281 tagger
= isl_union_pw_multi_aff_intersect_domain(tagger
,
2282 isl_union_set_copy(domain
));
2283 sched
= isl_union_map_preimage_domain_union_pw_multi_aff(sched
, tagger
);
2285 local
= isl_union_map_apply_range(sched
,
2286 isl_union_map_reverse(isl_union_map_copy(sched
)));
2287 local
= isl_union_map_intersect(local
,
2288 isl_union_map_copy(prog
->scop
->tagged_dep_flow
));
2290 empty
= isl_union_map_is_empty(local
);
2292 external
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
2293 universe
= isl_union_map_universe(isl_union_map_copy(access
));
2294 access_domain
= isl_union_map_domain(universe
);
2295 domain
= isl_union_set_universe(domain
);
2296 universe
= isl_union_set_unwrap(domain
);
2297 universe
= isl_union_map_intersect_domain(universe
, access_domain
);
2298 domain
= isl_union_map_wrap(universe
);
2300 external
= isl_union_map_intersect_range(external
, domain
);
2302 external
= isl_union_map_intersect_domain(external
, domain
);
2303 external
= isl_union_map_intersect_params(external
,
2304 isl_set_copy(prog
->scop
->context
));
2305 external
= isl_union_map_subtract(external
, local
);
2308 tag_set
= isl_union_map_range(external
);
2309 external
= wrapped_reference_to_access(tag_set
, tagged
);
2310 external
= isl_union_map_union(external
,
2311 isl_union_map_copy(prog
->scop
->live_in
));
2313 tag_set
= isl_union_map_domain(external
);
2314 external
= wrapped_reference_to_access(tag_set
, tagged
);
2315 external
= isl_union_map_union(external
,
2316 isl_union_map_copy(prog
->scop
->live_out
));
2320 external
= isl_union_map_free(external
);
2322 external
= isl_union_map_universe(external
);
2324 access
= isl_union_map_intersect(access
, external
);
2329 /* Given an access relation "access" from "group", remove those reads
2330 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2331 * communicate data within the same iteration of the schedule "prefix"
2332 * at the position where the copying of the group is inserted.
2333 * That is, the output dimension of "prefix"
2334 * is equal to tile->depth.
2335 * The domain of "prefix" corresponds to the original statement instances,
2336 * i.e., those that appear in the domains of the access relations.
2338 * Extract the tagged access relation of "group" and
2339 * then call remove_local_accesses.
2341 static __isl_give isl_union_map
*remove_local_accesses_group(
2342 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
2343 __isl_take isl_union_map
*access
, __isl_keep isl_union_map
*prefix
,
2346 isl_union_map
*sched
, *tagged
;
2348 if (isl_union_map_is_empty(access
))
2351 tagged
= group_tagged_access_relation(group
);
2352 sched
= isl_union_map_copy(prefix
);
2354 return remove_local_accesses(kernel
->prog
, tagged
, access
, sched
, read
);
2357 /* Build an access AST expression for the effective grid size using "build".
2358 * Store the result in kernel->grid_size_expr.
2360 static isl_stat
build_grid_size(struct ppcg_kernel
*kernel
,
2361 __isl_keep isl_ast_build
*build
)
2363 isl_multi_pw_aff
*size
;
2365 size
= isl_multi_pw_aff_copy(kernel
->grid_size
);
2366 size
= isl_multi_pw_aff_set_tuple_name(size
, isl_dim_out
, "grid");
2367 kernel
->grid_size_expr
= ppcg_build_size_expr(size
, build
);
2369 if (!kernel
->grid_size_expr
)
2370 return isl_stat_error
;
2374 /* Build access AST expressions for the localized array sizes using "build".
2375 * Store the result in local->bound_expr.
2376 * Only do this for arrays for which localized bounds have been computed.
2378 static isl_stat
build_local_array_sizes(struct ppcg_kernel
*kernel
,
2379 __isl_keep isl_ast_build
*build
)
2383 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2384 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
2385 isl_multi_pw_aff
*size
;
2387 if (local
->n_group
== 0)
2389 size
= isl_multi_pw_aff_copy(local
->bound
);
2390 local
->bound_expr
= ppcg_build_size_expr(size
, build
);
2391 if (!local
->bound_expr
)
2392 return isl_stat_error
;
2398 /* Build access AST expressions for the effective grid size and
2399 * the localized array sizes using "build".
2401 static isl_stat
build_grid_and_local_array_sizes(struct ppcg_kernel
*kernel
,
2402 __isl_keep isl_ast_build
*build
)
2404 if (build_grid_size(kernel
, build
) < 0)
2405 return isl_stat_error
;
2406 if (build_local_array_sizes(kernel
, build
) < 0)
2407 return isl_stat_error
;
2411 /* This function is called before the AST generator starts traversing
2412 * the schedule subtree of a node with mark "mark".
2414 * If the mark is called "kernel", store the kernel pointer in data->kernel
2415 * for use in at_domain and build AST expressions for the grid size and
2416 * the localized array sizes.
2418 static isl_stat
before_mark(__isl_keep isl_id
*mark
,
2419 __isl_keep isl_ast_build
*build
, void *user
)
2421 struct ppcg_at_domain_data
*data
= user
;
2424 return isl_stat_error
;
2425 if (!strcmp(isl_id_get_name(mark
), "kernel")) {
2426 data
->kernel
= isl_id_get_user(mark
);
2427 if (build_grid_and_local_array_sizes(data
->kernel
, build
) < 0)
2428 return isl_stat_error
;
2433 /* This function is called after the AST generator has finished traversing
2434 * the schedule subtree of a mark node. "node" points to the corresponding
2437 * If the mark is called "kernel", then replace "node" by a user node
2438 * that "calls" the kernel, representing the launch of the kernel.
2439 * The original "node" is stored inside the kernel object so that
2440 * it can be used to print the device code.
2441 * Note that this assumes that a kernel is only launched once.
2442 * Also clear data->kernel.
2444 static __isl_give isl_ast_node
*after_mark(__isl_take isl_ast_node
*node
,
2445 __isl_keep isl_ast_build
*build
, void *user
)
2450 isl_ast_expr_list
*list
;
2451 struct ppcg_kernel
*kernel
;
2452 struct ppcg_at_domain_data
*data
= user
;
2454 ctx
= isl_ast_node_get_ctx(node
);
2455 id
= isl_ast_node_mark_get_id(node
);
2457 return isl_ast_node_free(node
);
2458 if (strcmp(isl_id_get_name(id
), "kernel") || !data
->kernel
) {
2462 kernel
= data
->kernel
;
2463 data
->kernel
= NULL
;
2464 kernel
->space
= isl_ast_build_get_schedule_space(build
);
2465 kernel
->tree
= isl_ast_node_mark_get_node(node
);
2466 isl_ast_node_free(node
);
2468 expr
= isl_ast_expr_from_id(isl_id_copy(id
));
2469 list
= isl_ast_expr_list_alloc(ctx
, 0);
2470 expr
= isl_ast_expr_call(expr
, list
);
2471 node
= isl_ast_node_alloc_user(expr
);
2472 node
= isl_ast_node_set_annotation(node
, id
);
2477 static isl_bool
update_depth(__isl_keep isl_schedule_node
*node
, void *user
)
2482 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2483 return isl_bool_true
;
2484 node_depth
= isl_schedule_node_get_schedule_depth(node
);
2485 if (node_depth
> *depth
)
2486 *depth
= node_depth
;
2488 return isl_bool_false
;
2491 /* Use isl to generate code for both the host and the device
2493 * The device code is marked by "kernel" mark nodes in the schedule tree,
2494 * containing a pointer to a ppcg_kernel object.
2495 * The returned AST only contains the AST for the host code.
2496 * The ASTs for the device code are embedded in ppcg_kernel objects
2497 * attached to the leaf nodes that call "kernel".
2499 static __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
,
2500 __isl_take isl_schedule
*schedule
)
2502 struct ppcg_at_domain_data data
;
2503 isl_ast_build
*build
;
2505 isl_id_list
*iterators
;
2508 data
.prog
= gen
->prog
;
2512 if (isl_schedule_foreach_schedule_node_top_down(schedule
, &update_depth
,
2514 schedule
= isl_schedule_free(schedule
);
2515 build
= isl_ast_build_alloc(gen
->prog
->ctx
);
2516 iterators
= ppcg_scop_generate_names(gen
->prog
->scop
, depth
, "c");
2517 build
= isl_ast_build_set_iterators(build
, iterators
);
2518 build
= isl_ast_build_set_at_each_domain(build
, &at_domain
, &data
);
2519 build
= isl_ast_build_set_before_each_mark(build
, &before_mark
, &data
);
2520 build
= isl_ast_build_set_after_each_mark(build
, &after_mark
, &data
);
2521 if (gen
->prog
->scop
->options
->debug
->dump_final_schedule
)
2522 isl_schedule_dump(schedule
);
2523 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
2524 isl_ast_build_free(build
);
2529 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
2533 return isl_union_map_read_from_str(ctx
, str
);
2536 /* Can "node" be tiled and then mapped to block and thread identifiers?
2537 * That is, is it permutable with at least one coincident dimension?
2539 static isl_bool
is_permutable(__isl_keep isl_schedule_node
*node
)
2542 return isl_bool_error
;
2544 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
2545 return isl_bool_false
;
2546 if (!isl_schedule_node_band_get_permutable(node
))
2547 return isl_bool_false
;
2548 if (isl_schedule_node_band_n_member(node
) < 1)
2549 return isl_bool_false
;
2550 if (!isl_schedule_node_band_member_get_coincident(node
, 0))
2551 return isl_bool_false
;
2553 return isl_bool_true
;
2556 /* Is "node" not a suitably permutable band?
2558 static isl_bool
not_permutable(__isl_keep isl_schedule_node
*node
, void *user
)
2560 return isl_bool_not(is_permutable(node
));
2563 /* Does the subtree rooted at "node" have any suitably permutable band nodes?
2564 * That is, does it have any nodes that are permutable and that
2565 * have a least one coincident dimension?
2567 static isl_bool
subtree_has_permutable_bands(__isl_keep isl_schedule_node
*node
)
2569 isl_bool all_non_permutable
;
2571 all_non_permutable
= isl_schedule_node_every_descendant(node
,
2572 ¬_permutable
, NULL
);
2573 return isl_bool_not(all_non_permutable
);
2576 /* Does "schedule" contain any permutable band with at least one coincident
2579 static isl_bool
has_any_permutable_node(__isl_keep isl_schedule
*schedule
)
2581 isl_schedule_node
*root
;
2582 isl_bool any_permutable
;
2584 root
= isl_schedule_get_root(schedule
);
2585 any_permutable
= subtree_has_permutable_bands(root
);
2586 isl_schedule_node_free(root
);
2588 return any_permutable
;
2591 /* Is "node" a candidate for mapping to block and thread identifiers?
2592 * In particular, is it permutable with at least one coincident dimension?
2593 * Alternatively, does the subtree rooted at "node" not contain
2594 * any such permutable node? Filter nodes are skipped in this case,
2595 * because a band node will be inserted in front of the returned
2596 * node and this is not possible for filter nodes that are children
2597 * of set or sequence nodes.
2599 static int is_candidate(__isl_keep isl_schedule_node
*node
)
2601 isl_bool permutable
;
2603 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
)
2605 permutable
= is_permutable(node
);
2606 if (permutable
< 0 || permutable
)
2608 if (isl_schedule_node_get_type(node
) == isl_schedule_node_filter
)
2610 permutable
= subtree_has_permutable_bands(node
);
2616 /* Is "node" the outermost node in its branch that can be tiled
2617 * and then mapped to block and thread identifiers?
2618 * If there are no such nodes in the subtree at "node" and
2619 * if "node" is not a filter node, then it is accepted too.
2621 static int is_outer_tilable(__isl_keep isl_schedule_node
*node
)
2624 isl_schedule_node
*ancestor
;
2626 tilable
= is_candidate(node
);
2633 ancestor
= isl_schedule_node_copy(node
);
2634 while (isl_schedule_node_has_parent(ancestor
)) {
2635 ancestor
= isl_schedule_node_parent(ancestor
);
2637 tilable
= is_candidate(ancestor
);
2638 if (tilable
< 0 || tilable
)
2642 isl_schedule_node_free(ancestor
);
2643 return tilable
< 0 ? -1 : !tilable
;
2646 /* Collect the references to all writes in "group".
2647 * Each reference is represented by a universe set in a space
2651 * with S[i,j] the statement instance space and R[] the array reference.
2653 static __isl_give isl_union_set
*group_tagged_writes(
2654 struct gpu_array_ref_group
*group
)
2658 isl_union_set
*writes
;
2660 space
= isl_map_get_space(group
->access
);
2661 writes
= isl_union_set_empty(space
);
2662 for (i
= 0; i
< group
->n_ref
; ++i
) {
2666 if (!group
->refs
[i
]->write
)
2669 space
= isl_map_get_space(group
->refs
[i
]->tagged_access
);
2670 space
= isl_space_domain(space
);
2671 writes_i
= isl_set_universe(space
);
2672 writes
= isl_union_set_add_set(writes
, writes_i
);
2678 /* Is there any write access in "group" that requires synchronization
2679 * on a write to global memory?
2680 * We currently take into account all writes that would require
2681 * synchronization at the thread level depth, but if the copying
2682 * for this group is performed at an outer level, then we do not
2683 * actually need to take into account dependences at intermediate levels.
2685 static int any_sync_writes_in_group(struct ppcg_kernel
*kernel
,
2686 struct gpu_array_ref_group
*group
)
2688 isl_union_set
*writes
;
2689 int empty
, disjoint
;
2691 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2697 writes
= group_tagged_writes(group
);
2698 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2699 isl_union_set_free(writes
);
2701 return disjoint
< 0 ? -1 : !disjoint
;
2704 /* Collect the references to all writes in "kernel" that write directly
2705 * to global or shared memory, i.e., that are not mapped to private memory.
2706 * Each reference is represented by a universe set in a space
2710 * with S[i,j] the statement instance space and R[] the array reference.
2712 static __isl_give isl_union_set
*collect_non_private_tagged_writes(
2713 struct ppcg_kernel
*kernel
)
2715 isl_union_set
*writes
;
2718 writes
= isl_union_set_empty(isl_union_set_get_space(kernel
->arrays
));
2720 for (i
= 0; i
< kernel
->n_array
; ++i
) {
2721 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
2723 for (j
= 0; j
< array
->n_group
; ++j
) {
2724 struct gpu_array_ref_group
*group
= array
->groups
[j
];
2725 enum ppcg_group_access_type type
;
2726 isl_union_set
*writes_ij
;
2730 type
= gpu_array_ref_group_type(group
);
2731 if (type
== ppcg_access_private
)
2733 writes_ij
= group_tagged_writes(group
);
2734 writes
= isl_union_set_union(writes
, writes_ij
);
2741 /* Are there any direct writes to global memory that require
2744 static int any_global_or_shared_sync_writes(struct ppcg_kernel
*kernel
)
2746 isl_union_set
*writes
;
2747 int empty
, disjoint
;
2749 empty
= isl_union_set_is_empty(kernel
->sync_writes
);
2755 writes
= collect_non_private_tagged_writes(kernel
);
2756 disjoint
= isl_union_set_is_disjoint(kernel
->sync_writes
, writes
);
2757 isl_union_set_free(writes
);
2759 return disjoint
< 0 ? -1 : !disjoint
;
2762 /* Construct an isl_multi_val for use as tile sizes for tiling "node"
2763 * from the elements in "tile_size".
2765 static __isl_give isl_multi_val
*construct_band_tiles_sizes(
2766 __isl_keep isl_schedule_node
*node
, int *tile_size
)
2773 space
= isl_schedule_node_band_get_space(node
);
2774 return ppcg_multi_val_from_int_list(space
, tile_size
);
2777 /* Replace the partial schedule S of the band node "node" by
2785 * if scale_tile_loops is set, with f the integers in "factor".
2786 * The list that "factor" points to is assumed to contain at least
2787 * as many elements as the number of members in the band.
2789 static __isl_give isl_schedule_node
*snap_band_to_sizes(
2790 __isl_take isl_schedule_node
*node
, int *factor
,
2791 struct ppcg_options
*options
)
2795 mv
= construct_band_tiles_sizes(node
, factor
);
2796 node
= isl_schedule_node_band_scale_down(node
, isl_multi_val_copy(mv
));
2797 if (options
->scale_tile_loops
)
2798 node
= isl_schedule_node_band_scale(node
,
2799 isl_multi_val_copy(mv
));
2800 isl_multi_val_free(mv
);
2805 /* Tile "band" with tile size specified by "sizes".
2807 * Since the tile loops will be mapped to block ids, we forcibly
2808 * turn off tile loop scaling. We may want to enable tile loop scaling
2809 * at some later point, but then we would have to support the detection
2810 * of strides during the mapping to block ids.
2811 * Similarly, since the point loops will be mapped to thread ids,
2812 * we forcibly shift the point loops so that they start at zero.
2814 static __isl_give isl_schedule_node
*tile_band(
2815 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2817 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
2821 scale_tile
= isl_options_get_tile_scale_tile_loops(ctx
);
2822 isl_options_set_tile_scale_tile_loops(ctx
, 0);
2823 shift_point
= isl_options_get_tile_shift_point_loops(ctx
);
2824 isl_options_set_tile_shift_point_loops(ctx
, 1);
2826 node
= isl_schedule_node_band_tile(node
, sizes
);
2828 isl_options_set_tile_scale_tile_loops(ctx
, scale_tile
);
2829 isl_options_set_tile_shift_point_loops(ctx
, shift_point
);
2834 /* Extract the set of parameter values and outer schedule dimensions
2835 * for which any statement instance
2836 * in the kernel inserted at "node" needs to be executed.
2837 * Intersect the set of parameter values derived from the host schedule
2838 * relation with the context of "prog".
2840 static __isl_give isl_set
*extract_context(__isl_keep isl_schedule_node
*node
,
2841 struct gpu_prog
*prog
)
2843 isl_union_map
*schedule
;
2844 isl_union_set
*schedule_domain
;
2848 schedule
= isl_schedule_node_get_prefix_schedule_relation(node
);
2849 schedule_domain
= isl_union_map_range(schedule
);
2850 empty
= isl_union_set_is_empty(schedule_domain
);
2852 isl_union_set_free(schedule_domain
);
2859 space
= isl_union_set_get_space(schedule_domain
);
2860 isl_union_set_free(schedule_domain
);
2861 space
= isl_space_set_from_params(space
);
2862 depth
= isl_schedule_node_get_schedule_depth(node
);
2863 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
2864 context
= isl_set_empty(space
);
2866 context
= isl_set_from_union_set(schedule_domain
);
2868 context
= isl_set_intersect_params(context
,
2869 isl_set_copy(prog
->context
));
2874 /* Return the set of outer array elements accessed by
2875 * by the statement instances in "domain" in "prog".
2876 * The instances in "domain" are those that appear
2877 * in the domains of the access relations in "prog".
2879 static __isl_give isl_union_set
*accessed_by_domain(
2880 __isl_take isl_union_set
*domain
, struct gpu_prog
*prog
)
2882 isl_union_map
*access
;
2883 isl_union_set
*arrays
;
2885 access
= isl_union_map_union(isl_union_map_copy(prog
->read
),
2886 isl_union_map_copy(prog
->may_write
));
2887 access
= isl_union_map_intersect_domain(access
, domain
);
2888 arrays
= isl_union_map_range(access
);
2889 arrays
= isl_union_set_apply(arrays
,
2890 isl_union_map_copy(prog
->to_outer
));
2895 /* Return the number of outer band members of the band node "node"
2896 * that are marked coincident.
2898 static int n_outer_coincidence(__isl_keep isl_schedule_node
*node
)
2902 n
= isl_schedule_node_band_n_member(node
);
2904 for (i
= 0; i
< n
; ++i
)
2905 if (!isl_schedule_node_band_member_get_coincident(node
, i
))
2911 /* If the band node "node" has more than "n" members, then split off
2912 * the first "n" of them.
2914 static __isl_give isl_schedule_node
*split_band(
2915 __isl_take isl_schedule_node
*node
, int n
)
2919 dim
= isl_schedule_node_band_n_member(node
);
2921 node
= isl_schedule_node_band_split(node
, n
);
2926 /* Scale a band node that may have been split by split_band.
2927 * "sizes" are the scaling factors for the original node.
2928 * "node" either points to the original band node, or the outer
2929 * of the two pieces after splitting.
2931 * If the number of elements in "node" is smaller than the number of
2932 * elements in "sizes", then some splitting has occurred and we split
2933 * "sizes" in the same way.
2935 static __isl_give isl_schedule_node
*scale_band(
2936 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
2940 n
= isl_multi_val_dim(sizes
, isl_dim_set
);
2941 dim
= isl_schedule_node_band_n_member(node
);
2943 isl_multi_val
*sizes2
;
2945 sizes2
= isl_multi_val_copy(sizes
);
2946 sizes
= isl_multi_val_drop_dims(sizes
,
2947 isl_dim_set
, dim
, n
- dim
);
2948 sizes2
= isl_multi_val_drop_dims(sizes2
, isl_dim_set
, 0, dim
);
2949 node
= isl_schedule_node_child(node
, 0);
2950 node
= isl_schedule_node_band_scale(node
, sizes2
);
2951 node
= isl_schedule_node_parent(node
);
2954 return isl_schedule_node_band_scale(node
, sizes
);
2957 /* Return an isl_multi_aff, with as elements the parameters in "space"
2958 * that have the names specified by the elements in "names".
2959 * If (some of) these parameters do not already appear in "space",
2960 * then they are added first.
2962 static __isl_give isl_multi_aff
*parameter_vector(__isl_take isl_space
*space
,
2963 __isl_keep isl_id_list
*names
)
2966 isl_local_space
*ls
;
2970 space
= isl_space_free(space
);
2972 n
= isl_id_list_n_id(names
);
2973 for (i
= 0; i
< n
; ++i
) {
2977 id
= isl_id_list_get_id(names
, i
);
2978 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2983 pos
= isl_space_dim(space
, isl_dim_param
);
2984 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
2985 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
2987 ma
= isl_multi_aff_zero(isl_space_copy(space
));
2988 ls
= isl_local_space_from_space(isl_space_domain(space
));
2989 for (i
= 0; i
< n
; ++i
) {
2994 id
= isl_id_list_get_id(names
, i
);
2995 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2997 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
2998 isl_dim_param
, pos
);
2999 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3001 isl_local_space_free(ls
);
3006 /* Return constraints on the domain elements that equate a sequence of
3007 * parameters called "names", to the partial schedule
3008 * of "node" modulo the integers in "size".
3009 * The number of elements in the array "size" should be equal
3010 * to the number of elements in "names".
3011 * The number of members of the band node "node" should be smaller
3012 * than or equal to this number. If it is smaller, then the first
3013 * elements of "names" are equated to zero.
3015 static __isl_give isl_union_set
*set_schedule_modulo(
3016 __isl_keep isl_schedule_node
*node
, __isl_keep isl_id_list
*names
,
3022 isl_multi_union_pw_aff
*mupa
, *mupa2
;
3024 isl_union_set
*domain
;
3028 n
= isl_id_list_n_id(names
);
3030 return isl_schedule_node_get_universe_domain(node
);
3031 n_zero
= n
- isl_schedule_node_band_n_member(node
);
3033 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
3034 mv
= construct_band_tiles_sizes(node
, size
+ n_zero
);
3035 mupa
= isl_multi_union_pw_aff_mod_multi_val(mupa
, mv
);
3037 space
= isl_multi_union_pw_aff_get_space(mupa
);
3038 space
= isl_space_params(space
);
3039 space
= isl_space_set_from_params(space
);
3040 space
= isl_space_add_dims(space
, isl_dim_set
, n_zero
);
3041 ma
= isl_multi_aff_zero(space
);
3043 domain
= isl_schedule_node_get_universe_domain(node
);
3044 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(
3045 isl_union_set_copy(domain
), ma
);
3046 mupa
= isl_multi_union_pw_aff_range_product(mupa2
, mupa
);
3048 space
= isl_multi_union_pw_aff_get_space(mupa
);
3049 ma
= parameter_vector(space
, names
);
3051 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(domain
, ma
);
3052 mupa
= isl_multi_union_pw_aff_sub(mupa
, mupa2
);
3054 return isl_multi_union_pw_aff_zero_union_set(mupa
);
3057 /* Insert a context node at "node" introducing the block and thread
3058 * identifiers along with their bounds, which are stored in kernel->grid_size
3059 * and kernel->block_dim.
3060 * Note that the bounds on the block identifiers may implicitly impose
3061 * constraints on the parameters. A guard needs to be inserted
3062 * in the schedule tree to ensure that those bounds hold at "node".
3063 * This guard is inserted in insert_guard.
3065 static __isl_give isl_schedule_node
*insert_context(struct ppcg_kernel
*kernel
,
3066 __isl_take isl_schedule_node
*node
)
3070 context
= isl_set_universe(isl_set_get_space(kernel
->context
));
3072 context
= add_bounded_parameters_dynamic(context
,
3073 kernel
->grid_size
, kernel
->block_ids
);
3074 context
= add_bounded_parameters(context
,
3075 kernel
->block_dim
, kernel
->thread_ids
);
3077 node
= isl_schedule_node_insert_context(node
, context
);
3082 /* Insert a guard that eliminates kernel launches where the kernel
3083 * obviously does not have any work to do.
3085 * In particular, eliminate kernel launches where there are obviously
3087 * Use the same block size constraints that are used to create the context
3088 * to ensure that all constraints implicit in the constructed context
3089 * are imposed by the guard.
3091 * Additionally, add other constraints that are valid
3092 * for each executed instance ("context"), as long as this does not result
3095 static __isl_give isl_schedule_node
*insert_guard(
3096 __isl_take isl_schedule_node
*node
, __isl_keep isl_set
*context
,
3097 __isl_keep isl_multi_pw_aff
*size
, struct ppcg_scop
*scop
)
3103 guard
= isl_set_copy(context
);
3104 guard
= isl_set_compute_divs(guard
);
3105 guard
= isl_set_from_basic_set(isl_set_simple_hull(guard
));
3107 nparam
= isl_set_dim(guard
, isl_dim_param
);
3108 n
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
3109 ids
= ppcg_scop_generate_names(scop
, n
, "__ppcg_tmp");
3110 guard
= add_bounded_parameters_dynamic(guard
, size
, ids
);
3111 isl_id_list_free(ids
);
3112 guard
= isl_set_project_out(guard
, isl_dim_param
, nparam
, n
);
3114 node
= isl_schedule_node_insert_guard(node
, guard
);
3119 /* Does any array reference group mapping require the band that is mapped
3120 * to threads to be unrolled?
3122 static int kernel_requires_unroll(struct ppcg_kernel
*kernel
)
3126 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3127 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3129 for (j
= 0; j
< array
->n_group
; ++j
) {
3130 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3131 if (gpu_array_ref_group_requires_unroll(group
))
3139 /* Mark the given band node "node" for unrolling by the AST generator and
3140 * then sink it to the leaves of the schedule tree.
3141 * All dimensions of "node" are assumed to be coincident, such that this
3142 * sinking is a valid operation.
3144 static __isl_give isl_schedule_node
*unroll(__isl_take isl_schedule_node
*node
)
3146 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
3148 node
= isl_schedule_node_band_sink(node
);
3153 /* Insert a synchronization node in the schedule tree of "node"
3154 * after the core computation of "kernel" at the level of the band
3155 * that is mapped to threads, except if that level is equal to
3156 * that of the band that is mapped to blocks or if there are no writes
3157 * to global or shared memory in the core computation that require
3159 * If there are any writes to shared memory and the shared memory
3160 * copying is performed at the same level, then synchronization
3161 * is needed between the core and the copying anyway, so we might
3162 * as well add it here. If the copying is performed at a higher
3163 * level, then different iterations of intermediate schedule dimensions
3164 * may have a different mapping from between shared memory elements and
3165 * threads, such that synchronization is required after the core.
3166 * "node" is assumed to point to the kernel node.
3168 * If the shared and the thread mark point to the same node, then make
3169 * sure the synchronization is inserted outside of the shared mark.
3171 static __isl_give isl_schedule_node
*add_sync(struct ppcg_kernel
*kernel
,
3172 __isl_take isl_schedule_node
*node
)
3177 need_sync
= any_global_or_shared_sync_writes(kernel
);
3179 return isl_schedule_node_free(node
);
3183 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3184 depth
= isl_schedule_node_get_schedule_depth(node
);
3185 node
= gpu_tree_move_up_to_kernel(node
);
3186 if (depth
== isl_schedule_node_get_schedule_depth(node
))
3189 node
= gpu_tree_move_down_to_depth(node
, depth
, kernel
->core
);
3190 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3192 node
= gpu_tree_move_up_to_kernel(node
);
3197 /* Return a read ("read" is 1) or write access relation for "group"
3198 * with those accesses removed that are only needed to communicate data
3199 * within the subtree of the schedule rooted at "node".
3200 * Furthermore, include the prefix schedule at "node".
3201 * That is, return a relation of the form
3205 * with D the outer schedule dimensions at "node".
3207 static __isl_give isl_union_map
*anchored_non_local_accesses(
3208 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3209 __isl_take isl_schedule_node
*node
, int read
)
3211 isl_union_map
*access
;
3212 isl_union_map
*prefix
;
3214 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
3215 prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(prefix
,
3216 isl_union_pw_multi_aff_copy(kernel
->contraction
));
3217 access
= gpu_array_ref_group_access_relation(group
, read
, !read
);
3218 access
= remove_local_accesses_group(kernel
, group
, access
, prefix
,
3220 access
= isl_union_map_range_product(prefix
, access
);
3225 /* Given an array reference group "group", create a mapping
3227 * read[D -> A] -> [D -> A]
3229 * if "read" is set or
3231 * write[D -> A] -> [D -> A]
3233 * if "read" is not set.
3234 * D corresponds to the outer tile->depth dimensions of
3235 * the kernel schedule.
3237 static __isl_give isl_multi_aff
*create_from_access(isl_ctx
*ctx
,
3238 struct gpu_array_ref_group
*group
, int read
)
3240 struct gpu_array_tile
*tile
;
3244 tile
= gpu_array_ref_group_tile(group
);
3245 space
= isl_space_copy(group
->array
->space
);
3246 space
= isl_space_from_range(space
);
3247 space
= isl_space_add_dims(space
, isl_dim_in
, tile
->depth
);
3248 space
= isl_space_wrap(space
);
3249 space
= isl_space_map_from_set(space
);
3251 id
= isl_id_alloc(ctx
, read
? "read" : "write", group
);
3252 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
3254 return isl_multi_aff_identity(space
);
3257 /* If any writes in "group" require synchronization, then make sure
3258 * that there is a synchronization node for "kernel" after the node
3259 * following "node" in a sequence.
3261 * If "shared" is set and no synchronization is needed for
3262 * the writes to global memory, then add synchronization before
3263 * the kernel to protect shared memory from being overwritten
3264 * by the next iteration of the core computation.
3265 * No additional synchronization is needed to protect against
3266 * the next copy into shared memory because each element of
3267 * the shared memory tile is always copied by the same thread.
3269 static __isl_give isl_schedule_node
*add_group_write_sync(
3270 __isl_take isl_schedule_node
*node
, struct ppcg_kernel
*kernel
,
3271 struct gpu_array_ref_group
*group
, int shared
)
3275 need_sync
= any_sync_writes_in_group(kernel
, group
);
3277 return isl_schedule_node_free(node
);
3279 node
= isl_schedule_node_parent(node
);
3280 node
= isl_schedule_node_next_sibling(node
);
3281 node
= isl_schedule_node_child(node
, 0);
3282 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3283 } else if (shared
) {
3284 struct gpu_array_tile
*tile
;
3286 tile
= gpu_array_ref_group_tile(group
);
3287 node
= isl_schedule_node_parent(node
);
3288 node
= isl_schedule_node_parent(node
);
3289 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
,
3291 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3297 /* Add copy statements to the schedule tree of "node"
3298 * for reading from global memory to private memory (if "read" is set) or
3299 * for writing back from private memory to global memory
3300 * (if "read" is not set) for the array reference group "group" that
3301 * is mapped to private memory.
3302 * On input, "node" points to the kernel node, and it is moved
3303 * back there on output.
3305 * The copies are performed in the order of the array elements.
3306 * The copy statement instances include a reference to the outer
3307 * tile->depth dimensions of the kernel schedule for ease of
3308 * combining them with the group tiling.
3310 * That is, the extra schedule is of the form
3314 * where D corresponds to the outer tile->depth dimensions of
3315 * the kernel schedule and A to the global array.
3316 * This schedule is unrolled because registers are not addressable.
3318 * The copying is inserted in the schedule tree through an extension
3323 * where the extra domain elements type[D -> A] are those accessed
3325 * A filter is inserted on type[D -> A] to ensure that the element
3326 * is read/written by the same thread that needs the element.
3327 * This filter is obtained by applying
3331 * to the thread filter for the core statements.
3333 * The extension is inserted before the core computation in case of a read
3334 * and after the core computation in case of a write.
3335 * In the latter case, we also make sure that there is a synchronization
3336 * node after the write to global memory, unless this write is performed
3337 * at the outer level of the kernel.
3338 * In principle, this synchronization could be inserted higher
3339 * in the schedule tree depending on where the corresponding reads
3340 * from global memory are performed.
3342 static __isl_give isl_schedule_node
*add_copies_group_private(
3343 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3344 __isl_take isl_schedule_node
*node
, int read
)
3346 struct gpu_array_tile
*tile
;
3347 isl_union_map
*access
;
3348 isl_union_set
*domain
;
3350 isl_multi_aff
*from_access
;
3351 isl_multi_pw_aff
*mpa
;
3352 isl_multi_union_pw_aff
*mupa
;
3353 isl_union_pw_multi_aff
*contraction
;
3354 isl_schedule_node
*graft
;
3355 isl_union_set
*filter
;
3359 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3360 tile
= gpu_array_ref_group_tile(group
);
3361 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3363 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3364 empty
= isl_union_map_is_empty(access
);
3365 if (empty
< 0 || empty
) {
3366 isl_union_map_free(access
);
3368 return isl_schedule_node_free(node
);
3369 return gpu_tree_move_up_to_kernel(node
);
3372 group
->array
->global
= 1;
3373 group
->local_array
->global
= 1;
3375 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3376 space
= isl_space_domain(isl_multi_aff_get_space(from_access
));
3377 access
= isl_union_map_preimage_range_multi_aff(access
, from_access
);
3379 filter
= isl_union_set_copy(kernel
->thread_filter
);
3380 contraction
= isl_union_pw_multi_aff_copy(kernel
->contraction
);
3381 filter
= isl_union_set_preimage_union_pw_multi_aff(filter
, contraction
);
3382 filter
= isl_union_set_apply(filter
, isl_union_map_copy(access
));
3383 filter
= isl_union_set_detect_equalities(filter
);
3384 filter
= isl_union_set_coalesce(filter
);
3386 domain
= isl_union_map_range(access
);
3387 access
= isl_union_set_wrapped_domain_map(domain
);
3388 access
= isl_union_map_reverse(access
);
3389 access
= isl_union_map_coalesce(access
);
3390 graft
= isl_schedule_node_from_extension(access
);
3392 space
= isl_space_map_from_set(space
);
3393 mpa
= isl_multi_pw_aff_identity(space
);
3394 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
3395 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3397 graft
= isl_schedule_node_child(graft
, 0);
3398 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3399 graft
= unroll(graft
);
3401 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3403 graft
= isl_schedule_node_parent(graft
);
3406 node
= isl_schedule_node_graft_before(node
, graft
);
3408 node
= isl_schedule_node_graft_after(node
, graft
);
3409 if (kernel_depth
< tile
->depth
)
3410 node
= add_group_write_sync(node
, kernel
, group
, 0);
3413 node
= gpu_tree_move_up_to_kernel(node
);
3418 /* Add copy statements to the schedule tree of "node"
3419 * for reading from global memory to shared memory (if "read" is set) or
3420 * for writing back from shared memory to global memory
3421 * (if "read" is not set) for the array reference group "group" that
3422 * is mapped to shared memory.
3423 * On input, "node" points to the kernel node, and it is moved
3424 * back there on output.
3426 * The copies are performed in the order of the corresponding shared
3428 * The copy statement instances include a reference to the outer
3429 * tile->depth dimensions of the kernel schedule for ease of
3430 * combining them with the group tiling.
3432 * If we are performing a read from global memory to shared memory and
3433 * if the array involved is not a scalar, then we copy
3434 * the entire tile to shared memory. This may result in some extra
3435 * elements getting copied, but it should lead to simpler code
3436 * (which means that fewer registers may be needed) and less divergence.
3438 * Otherwise, we only copy the elements that will be read or have been written
3441 * That is, the extra schedule is of the form
3445 * where D corresponds to the outer tile->depth dimensions of
3446 * the kernel schedule, A to the global array and T is the corresponding
3447 * shared memory tile.
3449 * The copying is inserted in the schedule tree through an extension
3454 * where the extra domain elements type[D -> A] are those accessed
3455 * by the group. In the case of read from a non-scalar, this set
3456 * is replaced by the entire shared memory tile.
3458 * If the "unroll_copy_shared" option is set, then the AST generator
3459 * is instructed to unroll the copying code.
3461 * A filter is inserted on type[D -> A] to map the copy instances
3462 * to the threads. In particular, the thread identifiers are
3463 * equated to the position inside the shared memory tile (T)
3464 * modulo the block size.
3465 * We try to align the innermost tile dimension with the innermost
3466 * thread identifier (x) as a heuristic to improve coalescing.
3467 * In particular, if the dimension of the tile is greater than
3468 * the dimension of the block, then the schedule mapping to the tile
3469 * is broken up into two pieces and the filter is applied to the inner part.
3470 * If, on the other hand, the dimension of the tile is smaller than
3471 * the dimension of the block, then the initial thread identifiers
3472 * are equated to zero and the remaining thread identifiers are
3473 * matched to the memory tile.
3475 * The extension is inserted before the core computation in case of a read
3476 * and after the core computation in case of a write.
3477 * In the case of a read, we first need to make sure there is some
3478 * synchronization before the core computation such that we can put the read
3479 * from global memory to shared memory before that synchronization.
3480 * This ensures that all threads have finished copying into shared memory
3481 * before the shared memory is used.
3482 * We also need to make sure that there is a synchronization node after
3483 * the core computation to ensure that the next load into shared memory
3484 * only happens after all data has been used. There is no need for
3485 * this synchronization if we are at the outer level since then there
3486 * won't be a next load.
3487 * In the case of a write, we need to make sure there is some synchronization
3488 * after the core computation such that we can put the write from shared
3489 * memory to global memory after that synchronization.
3490 * Unless we are at the outer level, we also need a synchronization node
3491 * after the write to ensure the data is saved to global memory
3492 * before the next iteration writes to the same shared memory.
3493 * It also makes sure the data has arrived in global memory before
3494 * it is read in a subsequent iteration.
3496 static __isl_give isl_schedule_node
*add_copies_group_shared(
3497 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3498 __isl_take isl_schedule_node
*node
, int read
)
3500 struct gpu_array_tile
*tile
;
3501 isl_union_map
*access
;
3502 isl_union_set
*domain
;
3504 isl_multi_aff
*from_access
;
3505 isl_multi_pw_aff
*mpa
;
3506 isl_multi_union_pw_aff
*mupa
;
3507 isl_schedule_node
*graft
;
3508 isl_union_set
*filter
;
3513 tile
= gpu_array_ref_group_tile(group
);
3514 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3515 node
= gpu_tree_move_down_to_depth(node
, tile
->depth
, kernel
->core
);
3517 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3518 empty
= isl_union_map_is_empty(access
);
3519 if (empty
< 0 || empty
) {
3520 isl_union_map_free(access
);
3522 return isl_schedule_node_free(node
);
3523 return gpu_tree_move_up_to_kernel(node
);
3526 group
->array
->global
= 1;
3527 group
->local_array
->global
= 1;
3529 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3531 ma
= isl_multi_aff_copy(tile
->tiling
);
3532 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3533 isl_multi_aff_copy(from_access
));
3534 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3535 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3537 domain
= isl_union_map_range(access
);
3539 if (read
&& !gpu_array_is_scalar(group
->array
)) {
3541 isl_union_set_free(domain
);
3542 map
= group_tile(group
);
3543 domain
= isl_union_set_from_set(isl_map_wrap(map
));
3546 domain
= isl_union_set_preimage_multi_aff(domain
, from_access
);
3547 access
= isl_union_set_wrapped_domain_map(domain
);
3548 access
= isl_union_map_reverse(access
);
3549 access
= isl_union_map_coalesce(access
);
3550 graft
= isl_schedule_node_from_extension(access
);
3552 graft
= isl_schedule_node_child(graft
, 0);
3554 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3555 if (kernel
->options
->unroll_copy_shared
)
3556 graft
= ppcg_set_schedule_node_type(graft
, isl_ast_loop_unroll
);
3558 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0) {
3559 graft
= isl_schedule_node_band_split(graft
,
3560 tile
->n
- kernel
->n_block
);
3561 graft
= isl_schedule_node_child(graft
, 0);
3563 if (tile
->n
< kernel
->n_block
)
3564 skip
= kernel
->n_block
- tile
->n
;
3567 filter
= set_schedule_modulo(graft
, kernel
->thread_ids
,
3569 if (!kernel
->options
->wrap
)
3570 graft
= snap_band_to_sizes(graft
, kernel
->block_dim
+ skip
,
3572 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0)
3573 graft
= isl_schedule_node_parent(graft
);
3574 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3576 while (graft
&& isl_schedule_node_has_parent(graft
))
3577 graft
= isl_schedule_node_parent(graft
);
3580 if (kernel_depth
< tile
->depth
)
3581 node
= gpu_tree_ensure_sync_after_core(node
, kernel
);
3582 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3583 node
= isl_schedule_node_graft_before(node
, graft
);
3585 node
= gpu_tree_move_right_to_sync(node
, kernel
);
3586 node
= isl_schedule_node_graft_after(node
, graft
);
3587 if (kernel_depth
< tile
->depth
)
3588 node
= add_group_write_sync(node
, kernel
, group
, 1);
3591 node
= gpu_tree_move_up_to_kernel(node
);
3596 /* Check whether the array reference group "group" is mapped to
3597 * private or shared memory and, if so,
3598 * add copy statements to the schedule tree of "node"
3599 * for reading from global memory to private or shared memory
3600 * (if "read" is set) or for writing back from private or shared memory
3601 * to global memory (if "read" is not set) for this group.
3602 * On input, "node" points to the kernel node, and it is moved
3603 * back there on output.
3605 static __isl_give isl_schedule_node
*add_copies_group(
3606 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3607 __isl_take isl_schedule_node
*node
, int read
)
3609 enum ppcg_group_access_type type
;
3611 type
= gpu_array_ref_group_type(group
);
3612 if (type
== ppcg_access_private
)
3613 return add_copies_group_private(kernel
, group
, node
, read
);
3614 if (type
== ppcg_access_shared
)
3615 return add_copies_group_shared(kernel
, group
, node
, read
);
3619 /* For each array reference group that is mapped to private or shared memory,
3620 * add copy statements to the schedule tree of "node"
3621 * for reading from global memory to private or shared memory
3622 * and for writing back.
3623 * On input, "node" points to the kernel node, and it is moved
3624 * back there on output.
3626 static __isl_give isl_schedule_node
*add_copies(struct ppcg_kernel
*kernel
,
3627 __isl_take isl_schedule_node
*node
)
3631 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3632 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3634 for (j
= 0; j
< array
->n_group
; ++j
) {
3635 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3637 node
= add_copies_group(kernel
, group
, node
, 1);
3640 node
= add_copies_group(kernel
, group
, node
, 0);
3649 /* Mark all dimensions in the current band node atomic.
3651 static __isl_give isl_schedule_node
*atomic(__isl_take isl_schedule_node
*node
)
3653 return ppcg_set_schedule_node_type(node
, isl_ast_loop_atomic
);
3656 /* Mark "node" atomic, if it is a band node.
3657 * Do the same for all ancestors.
3658 * Return a pointer to "node" (in the updated schedule tree).
3660 static __isl_give isl_schedule_node
*atomic_ancestors(
3661 __isl_take isl_schedule_node
*node
)
3667 if (!isl_schedule_node_has_parent(node
))
3670 pos
= isl_schedule_node_get_child_position(node
);
3671 node
= isl_schedule_node_parent(node
);
3672 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
)
3673 node
= atomic(node
);
3674 node
= atomic_ancestors(node
);
3675 node
= isl_schedule_node_child(node
, pos
);
3680 /* Collect all write references that require synchronization.
3681 * "node" is assumed to point to the kernel node.
3682 * Each reference is represented by a universe set in a space
3686 * with S[i,j] the statement instance space and R[] the array reference.
3688 * This function should be called before block and thread filters are added.
3690 * Synchronization is needed after a write if there is a subsequent read
3691 * within the same block that may not be performed by the same thread.
3692 * There should not be any dependences between different blocks,
3693 * so we start with the flow dependences within the same kernel invocation
3694 * and we subtract from these those dependences that are mapped
3695 * to the same iteration of the bands where synchronization is inserted.
3696 * We do not remove pairs of instances that are known to map to
3697 * the same thread across different iterations of the intermediate
3698 * bands because the read may be performed by a different thread
3699 * than the one that needs the value if shared memory is involved.
3701 * We also consider all pairs of possible writes that access the same
3702 * memory location and that may be mapped to the same block but not
3703 * to the same iteration of the intermediate bands.
3704 * In theory, it would be possible for one thread to still be in
3705 * a previous iteration of a loop in these bands.
3706 * A write to global memory in this delayed thread could then overwrite
3707 * a write from another thread that has already moved on to
3708 * the next iteration.
3710 * After computing the above writes paired off with reads or writes
3711 * that depend on them, we project onto the domain writes.
3712 * Sychronization is needed after writes to global memory
3713 * through these references.
3715 static __isl_give isl_union_set
*compute_sync_writes(
3716 struct ppcg_kernel
*kernel
, __isl_keep isl_schedule_node
*node
)
3718 isl_union_map
*local
;
3719 isl_union_map
*may_writes
, *shared_access
;
3720 isl_union_map
*kernel_prefix
, *thread_prefix
;
3721 isl_union_map
*equal
;
3722 isl_union_set
*wrap
;
3723 isl_union_set
*domain
;
3724 isl_union_pw_multi_aff
*contraction
;
3726 kernel_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3727 node
= isl_schedule_node_copy(node
);
3728 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3729 thread_prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3730 isl_schedule_node_free(node
);
3732 contraction
= kernel
->contraction
;
3733 kernel_prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(
3734 kernel_prefix
, isl_union_pw_multi_aff_copy(contraction
));
3735 thread_prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(
3736 thread_prefix
, isl_union_pw_multi_aff_copy(contraction
));
3737 domain
= isl_union_set_copy(kernel
->expanded_domain
);
3738 domain
= isl_union_set_universe(domain
);
3740 may_writes
= isl_union_map_copy(kernel
->prog
->scop
->tagged_may_writes
);
3741 may_writes
= isl_union_map_curry(may_writes
);
3742 may_writes
= isl_union_map_intersect_domain(may_writes
, domain
);
3743 may_writes
= isl_union_map_uncurry(may_writes
);
3744 shared_access
= isl_union_map_copy(may_writes
);
3745 shared_access
= isl_union_map_apply_range(shared_access
,
3746 isl_union_map_reverse(may_writes
));
3748 local
= isl_union_map_copy(kernel
->prog
->scop
->tagged_dep_flow
);
3749 local
= isl_union_map_union(local
, shared_access
);
3750 local
= isl_union_map_zip(local
);
3752 equal
= isl_union_map_apply_range(kernel_prefix
,
3753 isl_union_map_reverse(isl_union_map_copy(kernel_prefix
)));
3754 wrap
= isl_union_map_wrap(equal
);
3755 local
= isl_union_map_intersect_domain(local
, wrap
);
3756 equal
= isl_union_map_apply_range(thread_prefix
,
3757 isl_union_map_reverse(isl_union_map_copy(thread_prefix
)));
3758 wrap
= isl_union_map_wrap(equal
);
3759 local
= isl_union_map_subtract_domain(local
, wrap
);
3761 local
= isl_union_map_zip(local
);
3762 local
= isl_union_map_universe(local
);
3764 return isl_union_map_domain(local
);
3767 /* Group the domain elements into a single space, named kernelX,
3768 * with X the kernel sequence number "kernel_id".
3770 static __isl_give isl_schedule_node
*group_statements(
3771 __isl_take isl_schedule_node
*node
, int kernel_id
)
3779 snprintf(buffer
, sizeof(buffer
), "kernel%d", kernel_id
);
3780 id
= isl_id_alloc(isl_schedule_node_get_ctx(node
), buffer
, NULL
);
3781 return isl_schedule_node_group(node
, id
);
3784 /* Create a ppcg_kernel representing the domain instances that reach "node"
3785 * and insert a mark node pointing to the ppcg_kernel before "node".
3786 * The band that "node" points to is the band that needs to be mapped
3787 * to block identifiers. The band that needs to be mapped to thread
3788 * identifiers should be marked by a "thread" mark by the caller.
3789 * The linear branch between the current node and the "thread" mark
3790 * may also have a "shared" mark. If present, the mapping to shared
3791 * memory is computed at that point.
3792 * Both marks are removed by this function.
3793 * If "scale" is set, then the band that "node" points to is scaled
3796 * Mark all outer band nodes as atomic to ensure each kernel is only
3798 * If the domain elements that reach "node" live in more than one space,
3799 * then group the domain elements into a single space, named kernelX,
3800 * with X the kernel sequence number.
3802 * Insert a guard node governing the kernel node to ensure that
3803 * no kernels with zero blocks are launched.
3805 * Insert a context node describing the block and thread
3806 * identifiers inside the kernel mark.
3807 * The context node needs to be inserted after the effective block size
3808 * has been determined such that the bounds on the thread identifiers
3809 * would reflect the effective block size.
3810 * Insert a filter node inside the context node mapping the statement
3811 * instances to block identifiers. In particular, the block identifiers
3812 * are equated to the partial schedule of band that was marked for mapping
3813 * to blocks modulo the grid size.
3814 * Insert a filter node inside the "thread" mark mapping the statement
3815 * instances to thread identifiers. In particular, the thread identifiers
3816 * are equated to the partial schedule of band that was marked for mapping
3817 * to threads modulo the block size.
3819 * Compute array reference groups for all arrays, set the local
3820 * array bounds based on the set of domain instances that reach
3821 * the kernel node, check the total amount of shared memory used
3822 * and compute all group tilings.
3823 * The array reference groups are computed after the block filter
3824 * has been inserted because it affects the mapping to shared or
3825 * private memory. This computation also requires the thread filter
3826 * (in the ppcg_kernel object), but this thread filter should not
3827 * have been added to the schedule tree yet since the computation
3828 * requires the schedule of the band that needs to be mapped to
3829 * threads before the privatization is applied.
3831 * If any array reference group requires the band mapped to threads
3832 * to be unrolled, then we perform the required unrolling.
3834 * We save a copy of the schedule that may influence the mappings
3835 * to shared or private memory in kernel->copy_schedule.
3837 * Finally, we add synchronization and copy statements to the schedule tree,
3838 * remove the "thread" mark and create representations for the local
3839 * variables in the kernel.
3841 * We keep a copy of the isl_id that points to the kernel to ensure
3842 * that the kernel does not get destroyed if the schedule node
3843 * is freed due to some error condition.
3845 __isl_give isl_schedule_node
*gpu_create_kernel(struct gpu_gen
*gen
,
3846 __isl_take isl_schedule_node
*node
, int scale
,
3847 __isl_keep isl_multi_val
*sizes
)
3849 struct ppcg_kernel
*kernel
;
3851 isl_schedule_node
*node_thread
;
3852 isl_union_map
*host_schedule
;
3853 isl_union_pw_multi_aff
*contraction
;
3854 isl_set
*host_domain
;
3855 isl_union_set
*domain
, *expanded
;
3856 int single_statement
;
3858 node
= gpu_tree_insert_shared_before_thread(node
);
3862 kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
3863 kernel
= ppcg_kernel_create_local_arrays(kernel
, gen
->prog
);
3865 return isl_schedule_node_free(node
);
3867 domain
= isl_schedule_node_get_domain(node
);
3868 single_statement
= isl_union_set_n_set(domain
) == 1;
3870 kernel
->ctx
= gen
->ctx
;
3871 kernel
->prog
= gen
->prog
;
3872 kernel
->options
= gen
->options
;
3873 kernel
->context
= extract_context(node
, gen
->prog
);
3874 kernel
->core
= isl_union_set_universe(isl_union_set_copy(domain
));
3875 contraction
= isl_schedule_node_get_subtree_contraction(node
);
3876 kernel
->contraction
= isl_union_pw_multi_aff_copy(contraction
);
3877 expanded
= isl_union_set_copy(domain
);
3878 expanded
= isl_union_set_preimage_union_pw_multi_aff(expanded
,
3880 kernel
->expanded_domain
= isl_union_set_copy(expanded
);
3881 kernel
->arrays
= accessed_by_domain(expanded
, gen
->prog
);
3882 kernel
->n_grid
= n_outer_coincidence(node
);
3883 node_thread
= isl_schedule_node_copy(node
);
3884 node_thread
= gpu_tree_move_down_to_thread(node_thread
, kernel
->core
);
3885 node_thread
= isl_schedule_node_child(node_thread
, 0);
3886 kernel
->n_block
= n_outer_coincidence(node_thread
);
3887 isl_schedule_node_free(node_thread
);
3888 kernel
->id
= gen
->kernel_id
++;
3889 if (read_grid_and_block_sizes(kernel
, gen
) < 0)
3890 node
= isl_schedule_node_free(node
);
3892 kernel
->sync_writes
= compute_sync_writes(kernel
, node
);
3894 host_schedule
= isl_schedule_node_get_prefix_schedule_union_map(node
);
3895 host_domain
= isl_set_from_union_set(isl_union_map_range(
3898 node
= atomic_ancestors(node
);
3900 id
= isl_id_alloc(gen
->ctx
, "kernel", kernel
);
3901 id
= isl_id_set_free_user(id
, &ppcg_kernel_free_wrap
);
3902 node
= isl_schedule_node_insert_mark(node
, isl_id_copy(id
));
3904 if (!single_statement
)
3905 node
= group_statements(node
, kernel
->id
);
3907 node
= isl_schedule_node_child(node
, 0);
3908 node
= split_band(node
, kernel
->n_grid
);
3909 kernel
->block_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3910 kernel
->n_grid
, "b");
3911 kernel
->block_filter
= set_schedule_modulo(node
, kernel
->block_ids
,
3913 kernel
->grid_size
= extract_grid_size(kernel
,
3914 isl_union_set_copy(domain
));
3915 if (!kernel
->options
->wrap
)
3916 node
= snap_band_to_sizes(node
, kernel
->grid_dim
,
3919 node
= scale_band(node
, isl_multi_val_copy(sizes
));
3920 node
= isl_schedule_node_parent(node
);
3921 if (!single_statement
)
3922 node
= isl_schedule_node_parent(node
);
3923 node
= insert_guard(node
, kernel
->context
, kernel
->grid_size
,
3925 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3926 node
= isl_schedule_node_child(node
, 0);
3927 node
= split_band(node
, kernel
->n_block
);
3928 kernel
->thread_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
3929 kernel
->n_block
, "t");
3930 kernel
->thread_filter
= set_schedule_modulo(node
, kernel
->thread_ids
,
3932 if (extract_block_size(kernel
, domain
) < 0)
3933 node
= isl_schedule_node_free(node
);
3935 node
= gpu_tree_move_up_to_kernel(node
);
3936 node
= isl_schedule_node_child(node
, 0);
3937 node
= insert_context(kernel
, node
);
3938 node
= isl_schedule_node_child(node
, 0);
3939 node
= isl_schedule_node_insert_filter(node
,
3940 isl_union_set_copy(kernel
->block_filter
));
3942 node
= gpu_tree_move_up_to_kernel(node
);
3944 if (gpu_group_references(kernel
, node
) < 0)
3945 node
= isl_schedule_node_free(node
);
3946 localize_bounds(kernel
, host_domain
);
3947 isl_set_free(host_domain
);
3949 check_shared_memory_bound(kernel
);
3950 mark_global_arrays(kernel
);
3951 compute_group_tilings(kernel
);
3953 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3954 node
= isl_schedule_node_child(node
, 0);
3955 if (!kernel
->options
->wrap
)
3956 node
= snap_band_to_sizes(node
, kernel
->block_dim
,
3958 node
= isl_schedule_node_insert_filter(node
,
3959 isl_union_set_copy(kernel
->thread_filter
));
3960 if (kernel_requires_unroll(kernel
)) {
3961 node
= isl_schedule_node_child(node
, 0);
3962 node
= unroll(node
);
3965 node
= gpu_tree_move_up_to_thread(node
);
3966 kernel
->copy_schedule_dim
= isl_schedule_node_get_schedule_depth(node
);
3967 kernel
->copy_schedule
=
3968 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
3969 contraction
= isl_union_pw_multi_aff_copy(kernel
->contraction
);
3970 kernel
->copy_schedule
=
3971 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
3972 kernel
->copy_schedule
, contraction
);
3974 node
= gpu_tree_move_up_to_kernel(node
);
3976 node
= add_sync(kernel
, node
);
3977 node
= add_copies(kernel
, node
);
3979 node
= gpu_tree_move_down_to_shared(node
, kernel
->core
);
3980 node
= isl_schedule_node_delete(node
);
3982 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3983 node
= isl_schedule_node_delete(node
);
3985 node
= gpu_tree_move_up_to_kernel(node
);
3987 if (create_kernel_vars(kernel
) < 0)
3988 node
= isl_schedule_node_free(node
);
3990 if (!single_statement
)
3991 node
= isl_schedule_node_parent(node
);
3992 node
= isl_schedule_node_parent(node
);
3996 ppcg_kernel_free(kernel
);
4000 /* Insert a zero-dimensional permutable band at "node".
4002 static __isl_give isl_schedule_node
*insert_empty_permutable_band(
4003 __isl_take isl_schedule_node
*node
)
4006 isl_schedule
*schedule
;
4007 isl_union_set
*domain
;
4008 isl_multi_union_pw_aff
*mupa
;
4010 schedule
= isl_schedule_node_get_schedule(node
);
4011 domain
= isl_schedule_get_domain(schedule
);
4012 space
= isl_union_set_get_space(domain
);
4013 isl_union_set_free(domain
);
4014 isl_schedule_free(schedule
);
4016 space
= isl_space_set_from_params(space
);
4017 mupa
= isl_multi_union_pw_aff_zero(space
);
4018 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
4019 node
= isl_schedule_node_band_set_permutable(node
, 1);
4024 /* See if hybrid tiling can be performed on "node" and its parent.
4025 * If so, apply hybrid tiling and return the updated schedule tree.
4026 * If not, return the original schedule tree.
4027 * Return NULL on error.
4029 * First check if "node", together with its parent, meets
4030 * the basic requirements for hybrid tiling.
4031 * If so, compute the relative dependence distances of "node"
4032 * with respect to its parent and check if they are sufficiently bounded.
4033 * If so, apply hybrid tiling using user specified tile sizes.
4035 * The tile sizes are read before the dependence distance bounds are
4036 * computed, because the user may have specified fewer dimensions
4037 * than are available. In this case, the remaining schedule dimensions
4038 * are split off and the dependence distances should be computed
4039 * after these dimensions have been split off.
4041 static __isl_give isl_schedule_node
*try_hybrid_tile(struct gpu_gen
*gen
,
4042 __isl_take isl_schedule_node
*node
)
4047 isl_schedule_node
*orig
= node
;
4048 ppcg_ht_bounds
*bounds
;
4050 ok
= ppcg_ht_parent_has_input_pattern(node
);
4052 return isl_schedule_node_free(node
);
4056 tile_len
= 1 + isl_schedule_node_band_n_member(node
);
4057 tile_size
= read_tile_sizes(gen
, &tile_len
);
4059 return isl_schedule_node_free(node
);
4061 node
= isl_schedule_node_copy(node
);
4062 node
= split_band(node
, tile_len
- 1);
4063 node
= isl_schedule_node_parent(node
);
4064 bounds
= ppcg_ht_compute_bounds(gen
->prog
->scop
, node
);
4065 node
= isl_schedule_node_child(node
, 0);
4067 ok
= ppcg_ht_bounds_is_valid(bounds
);
4069 node
= gpu_hybrid_tile(gen
, node
, bounds
, tile_size
);
4071 ppcg_ht_bounds_free(bounds
);
4074 if (ok
>= 0 && !ok
) {
4075 isl_schedule_node_free(node
);
4078 isl_schedule_node_free(orig
);
4080 return isl_schedule_node_free(node
);
4084 /* If "node" is the outermost permutable band that can be mapped to block and
4085 * thread identifiers in its branch (or the root of a subtree with
4086 * no such outer bands),
4087 * then mark the band as such, attaching a ppcg_kernel to the mark.
4089 * If hybrid tiling is allowed, then first try and apply it
4090 * to "node" and its parent.
4092 * If "node" is the root of a subtree without permutable bands,
4093 * then insert a zero-dimensional permutable band such that
4094 * we can assume that "node" always points to a band node.
4095 * This includes the case where "node" already points to a band node,
4096 * but one without any coincident dimension. In this case,
4097 * the extra node ensures that this original node does not get tiled.
4099 * Tile "node" using user specified tile sizes, after splitting the band
4100 * if the number of specified tile sizes is smaller than the dimension
4101 * of the band. Mark the point band of this tiling as the band that
4102 * needs to be mapped to threads and instruct the AST generator to unroll
4103 * the band if the "unroll_gpu_tile" option is set.
4104 * Create a kernel representing the domain instances that reach "node" and
4105 * insert a mark node pointing to the ppcg_kernel before the band node.
4107 static __isl_give isl_schedule_node
*mark_outer_permutable(
4108 __isl_take isl_schedule_node
*node
, void *user
)
4110 struct gpu_gen
*gen
= user
;
4116 isl_multi_val
*sizes
;
4118 outer
= is_outer_tilable(node
);
4120 return isl_schedule_node_free(node
);
4124 if (gen
->options
->hybrid
) {
4125 isl_schedule_node
*saved
= isl_schedule_node_copy(node
);
4126 node
= try_hybrid_tile(gen
, node
);
4127 isl_schedule_node_free(saved
);
4132 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
||
4133 !isl_schedule_node_band_member_get_coincident(node
, 0))
4134 node
= insert_empty_permutable_band(node
);
4136 tile_len
= isl_schedule_node_band_n_member(node
);
4137 tile_size
= read_tile_sizes(gen
, &tile_len
);
4139 return isl_schedule_node_free(node
);
4140 if (tile_len
< isl_schedule_node_band_n_member(node
))
4141 node
= isl_schedule_node_band_split(node
, tile_len
);
4142 sizes
= construct_band_tiles_sizes(node
, tile_size
);
4143 node
= tile_band(node
, isl_multi_val_copy(sizes
));
4144 node
= isl_schedule_node_child(node
, 0);
4145 if (gen
->options
->unroll_gpu_tile
)
4146 node
= ppcg_set_schedule_node_type(node
, isl_ast_loop_unroll
);
4147 id
= isl_id_alloc(gen
->ctx
, "thread", NULL
);
4148 node
= isl_schedule_node_insert_mark(node
, id
);
4149 node
= isl_schedule_node_parent(node
);
4151 scale
= gen
->options
->scale_tile_loops
;
4152 node
= gpu_create_kernel(gen
, node
, scale
, sizes
);
4153 isl_multi_val_free(sizes
);
4159 /* Given a set or sequence node, return the union the filters of either all
4160 * (if "only_initial" is not set) or the initial (if "only_initial" is set)
4161 * direct subtrees that do not contain any suitably permutable bands
4162 * (according to subtree_has_permutable_bands).
4164 static __isl_give isl_union_set
*get_non_parallel_subtree_filters(
4165 __isl_keep isl_schedule_node
*node
, int only_initial
)
4168 isl_union_set
*filter
;
4171 n
= isl_schedule_node_n_children(node
);
4175 node
= isl_schedule_node_copy(node
);
4176 node
= isl_schedule_node_child(node
, 0);
4177 filter
= isl_schedule_node_filter_get_filter(node
);
4178 node
= isl_schedule_node_parent(node
);
4179 space
= isl_union_set_get_space(filter
);
4180 isl_union_set_free(filter
);
4181 filter
= isl_union_set_empty(space
);
4183 for (i
= 0; i
< n
; ++i
) {
4186 node
= isl_schedule_node_child(node
, i
);
4187 parallelism
= subtree_has_permutable_bands(node
);
4188 if (parallelism
< 0) {
4189 filter
= isl_union_set_free(filter
);
4190 } else if (!parallelism
) {
4191 isl_union_set
*filter_i
;
4192 filter_i
= isl_schedule_node_filter_get_filter(node
);
4193 filter
= isl_union_set_union(filter
, filter_i
);
4194 } else if (only_initial
)
4196 node
= isl_schedule_node_parent(node
);
4199 isl_schedule_node_free(node
);
4204 /* Given a set or sequence node, return the union of the filters of
4205 * the direct subtrees that do not contain any suitably permutable bands
4206 * (according to subtree_has_permutable_bands).
4208 static __isl_give isl_union_set
*get_all_non_parallel_subtree_filters(
4209 __isl_keep isl_schedule_node
*node
)
4211 return get_non_parallel_subtree_filters(node
, 0);
4214 /* Given a set or sequence node, return the union of the filters of
4215 * the initial direct subtrees that do not contain any suitably permutable
4216 * bands (according to subtree_has_permutable_bands).
4218 static __isl_give isl_union_set
*get_initial_non_parallel_subtree_filters(
4219 __isl_keep isl_schedule_node
*node
)
4221 return get_non_parallel_subtree_filters(node
, 1);
4224 /* Mark all variables that are accessed by the statement instances in "domain"
4225 * and that are local to "prog" as requiring a declaration in the host code.
4226 * The statement instances in "domain" correspond to (a subset of)
4227 * the active instances at "node".
4228 * "node" is not modified by this function, except that NULL is returned
4231 static __isl_give isl_schedule_node
*declare_accessed_local_variables(
4232 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
,
4233 __isl_keep isl_union_set
*domain
)
4235 isl_union_pw_multi_aff
*contraction
;
4236 isl_union_set
*arrays
;
4239 if (!ppcg_scop_any_hidden_declarations(prog
->scop
))
4241 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4242 domain
= isl_union_set_copy(domain
);
4243 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, contraction
);
4244 arrays
= accessed_by_domain(domain
, prog
);
4246 for (i
= 0; i
< prog
->n_array
; ++i
) {
4251 if (!prog
->array
[i
].local
)
4253 space
= isl_set_get_space(prog
->array
[i
].extent
);
4254 set
= isl_union_set_extract_set(arrays
, space
);
4255 empty
= isl_set_plain_is_empty(set
);
4260 prog
->array
[i
].declare_local
= 1;
4263 isl_union_set_free(arrays
);
4266 isl_union_set_free(arrays
);
4267 return isl_schedule_node_free(node
);
4270 /* If "node" points to a set node, then separate its children
4271 * into subtrees that have suitably permutable bands and
4272 * those that do not.
4273 * Adjust the schedule tree in order to execute the second group
4274 * after the first group and return a pointer to the first group,
4275 * assuming there are any such subtrees.
4276 * If "node" points to a sequence node, then separate the initial
4277 * children that do not have suitably permutable bands and
4278 * return a pointer to the subsequence of children that do have such bands,
4279 * assuming there are any such subtrees.
4281 * In both cases, mark all local variables in "prog" that are accessed by
4282 * the group without permutable bands as requiring a declaration on the host.
4284 static __isl_give isl_schedule_node
*isolate_permutable_subtrees(
4285 __isl_take isl_schedule_node
*node
, struct gpu_prog
*prog
)
4287 isl_union_set
*filter
;
4288 enum isl_schedule_node_type type
;
4292 type
= isl_schedule_node_get_type(node
);
4293 if (type
== isl_schedule_node_set
) {
4294 filter
= get_all_non_parallel_subtree_filters(node
);
4295 node
= declare_accessed_local_variables(node
, prog
, filter
);
4296 node
= isl_schedule_node_order_after(node
, filter
);
4297 } else if (type
== isl_schedule_node_sequence
) {
4298 filter
= get_initial_non_parallel_subtree_filters(node
);
4299 node
= declare_accessed_local_variables(node
, prog
, filter
);
4300 node
= isl_schedule_node_order_before(node
, filter
);
4306 /* Replace any reference to an array element in the range of "copy"
4307 * by a reference to all array elements (defined by the extent of the array).
4309 static __isl_give isl_union_map
*approximate_copy_out(
4310 __isl_take isl_union_map
*copy
, struct gpu_prog
*prog
)
4315 res
= isl_union_map_empty(isl_union_map_get_space(copy
));
4317 for (i
= 0; i
< prog
->n_array
; ++i
) {
4320 isl_union_map
*copy_i
;
4321 isl_union_set
*extent
, *domain
;
4323 space
= isl_space_copy(prog
->array
[i
].space
);
4324 extent
= isl_union_set_from_set(isl_set_universe(space
));
4325 copy_i
= isl_union_map_copy(copy
);
4326 copy_i
= isl_union_map_intersect_range(copy_i
, extent
);
4327 set
= isl_set_copy(prog
->array
[i
].extent
);
4328 extent
= isl_union_set_from_set(set
);
4329 domain
= isl_union_map_domain(copy_i
);
4330 copy_i
= isl_union_map_from_domain_and_range(domain
, extent
);
4331 res
= isl_union_map_union(res
, copy_i
);
4334 isl_union_map_free(copy
);
4339 /* Insert "kernel" marks that point to a ppcg_kernel structure
4340 * in front of all outermost tilable band that (by construction)
4341 * have at least one parallel loop.
4343 static __isl_give isl_schedule_node
*mark_kernels(struct gpu_gen
*gen
,
4344 __isl_take isl_schedule_node
*node
)
4346 return isl_schedule_node_map_descendant_bottom_up(node
,
4347 &mark_outer_permutable
, gen
);
4350 /* Construct schedule constraints from the dependences in prog->scop and
4351 * the array order dependences in prog->array_order.
4353 * If live range reordering is allowed, then we need to make sure
4354 * that live ranges on arrays are not run in parallel since doing
4355 * so would require array expansion. We therefore add the array
4356 * order dependences to the coincidence dependences. Non-zero array
4357 * order dependences will then prevent a schedule dimension from being
4358 * considered parallel.
4359 * Live ranges derived from scalars are allowed to be run in parallel
4360 * since we force the scalars to be mapped to private memory in
4361 * check_scalar_live_ranges.
4362 * If live range reordering is allowed, then the false dependences
4363 * are not added to the validity constraints as that would prevent
4364 * reordering. Instead, the external false dependences that enforce that reads
4365 * from potentially live-in data precede any later write and
4366 * that writes of potentially live-out data follow any other earlier write
4367 * are added to the validity and the coincidence constraints.
4368 * The false dependences are still added to the proximity constraints
4369 * for consistency with the case where live range reordering is not allowed.
4370 * The coincidence constraints then consist of flow dependences,
4371 * external false dependences and array order dependences.
4372 * The independences can be filtered out from the first two sets.
4373 * They have already been filtered out from the array order dependences
4374 * on a per array basis in collect_order_dependences.
4375 * There is no need for a per array handling of the other two sets
4376 * as there should be no flow or external false dependence on local
4377 * variables that can be filtered out.
4379 static __isl_give isl_schedule_constraints
*construct_schedule_constraints(
4380 struct gpu_prog
*prog
)
4382 isl_union_set
*domain
;
4383 isl_union_map
*dep_raw
, *dep
;
4384 isl_union_map
*validity
, *proximity
, *coincidence
;
4385 isl_schedule_constraints
*sc
;
4387 domain
= isl_union_set_copy(prog
->scop
->domain
);
4388 sc
= isl_schedule_constraints_on_domain(domain
);
4389 sc
= isl_schedule_constraints_set_context(sc
,
4390 isl_set_copy(prog
->scop
->context
));
4391 if (prog
->scop
->options
->live_range_reordering
) {
4392 sc
= isl_schedule_constraints_set_conditional_validity(sc
,
4393 isl_union_map_copy(prog
->scop
->tagged_dep_flow
),
4394 isl_union_map_copy(prog
->scop
->tagged_dep_order
));
4395 proximity
= isl_union_map_copy(prog
->scop
->dep_flow
);
4396 validity
= isl_union_map_copy(proximity
);
4397 validity
= isl_union_map_union(validity
,
4398 isl_union_map_copy(prog
->scop
->dep_forced
));
4399 proximity
= isl_union_map_union(proximity
,
4400 isl_union_map_copy(prog
->scop
->dep_false
));
4401 coincidence
= isl_union_map_copy(validity
);
4402 coincidence
= isl_union_map_subtract(coincidence
,
4403 isl_union_map_copy(prog
->scop
->independence
));
4404 coincidence
= isl_union_map_union(coincidence
,
4405 isl_union_map_copy(prog
->array_order
));
4407 dep_raw
= isl_union_map_copy(prog
->scop
->dep_flow
);
4408 dep
= isl_union_map_copy(prog
->scop
->dep_false
);
4409 dep
= isl_union_map_union(dep
, dep_raw
);
4410 dep
= isl_union_map_coalesce(dep
);
4411 proximity
= isl_union_map_copy(dep
);
4412 coincidence
= isl_union_map_copy(dep
);
4415 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4416 sc
= isl_schedule_constraints_set_coincidence(sc
, coincidence
);
4417 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4422 /* Compute an appropriate schedule based on the accesses in
4423 * gen->read and gen->write.
4425 * We derive schedule constraints from the dependences in gen->prog->scop
4426 * and then use isl to compute a schedule that has a parallel loop
4427 * in each tilable band.
4428 * During the schedule construction, some statement instances
4429 * may be grouped first based on the input schedule.
4431 static __isl_give isl_schedule
*compute_schedule(struct gpu_gen
*gen
)
4433 isl_schedule_constraints
*sc
;
4434 isl_schedule
*schedule
;
4436 sc
= construct_schedule_constraints(gen
->prog
);
4437 schedule
= gen
->prog
->scop
->schedule
;
4438 schedule
= ppcg_compute_schedule(sc
, schedule
, gen
->options
);
4443 /* If the band node "node" has exactly one member then mark it permutable.
4445 static __isl_give isl_schedule_node
*band_set_permutable(
4446 __isl_take isl_schedule_node
*node
,
4447 __isl_keep isl_schedule_constraints
*sc
)
4449 if (isl_schedule_node_band_n_member(node
) == 1)
4450 node
= isl_schedule_node_band_set_permutable(node
, 1);
4455 /* Return the coincidence constraints between pairs of instances
4456 * that are scheduled together by the ancestors of "node".
4457 * That is, select those coincidence constraints that relate
4458 * pairs of instances that have the same value for the prefix schedule.
4459 * If the schedule depth is zero, then the prefix schedule does not
4460 * contain any information, so we intersect domain and range
4461 * of the schedule constraints with the reaching domain elements instead.
4463 static __isl_give isl_union_map
*get_local_coincidence(
4464 __isl_keep isl_schedule_node
*node
,
4465 __isl_keep isl_schedule_constraints
*sc
)
4467 isl_union_map
*coincidence
;
4468 isl_multi_union_pw_aff
*prefix
;
4469 isl_union_pw_multi_aff
*contraction
;
4471 coincidence
= isl_schedule_constraints_get_coincidence(sc
);
4472 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4473 if (isl_schedule_node_get_schedule_depth(node
) == 0) {
4474 isl_union_set
*domain
;
4476 domain
= isl_schedule_node_get_domain(node
);
4477 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4479 coincidence
= isl_union_map_intersect_domain(coincidence
,
4480 isl_union_set_copy(domain
));
4481 coincidence
= isl_union_map_intersect_range(coincidence
,
4486 prefix
= isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node
);
4487 prefix
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix
,
4489 return isl_union_map_eq_at_multi_union_pw_aff(coincidence
, prefix
);
4492 /* For each member in the band node "node", determine whether
4493 * it is coincident with respect to the outer nodes and mark
4496 * That is, for each coincidence constraint between pairs
4497 * of instances that are scheduled together by the outer nodes,
4498 * check that domain and range are assigned the same value
4499 * by the band member. This test is performed by checking
4500 * that imposing the same value for the band member does not
4501 * remove any elements from the set of coincidence constraints.
4503 static __isl_give isl_schedule_node
*band_set_coincident(
4504 __isl_take isl_schedule_node
*node
,
4505 __isl_keep isl_schedule_constraints
*sc
)
4507 isl_union_map
*coincidence
;
4508 isl_union_pw_multi_aff
*contraction
;
4509 isl_multi_union_pw_aff
*partial
;
4512 coincidence
= get_local_coincidence(node
, sc
);
4514 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4515 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4516 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4518 n
= isl_schedule_node_band_n_member(node
);
4519 for (i
= 0; i
< n
; ++i
) {
4520 isl_union_map
*coincidence_i
;
4521 isl_union_pw_aff
*upa
;
4522 isl_multi_union_pw_aff
*partial_i
;
4525 upa
= isl_multi_union_pw_aff_get_union_pw_aff(partial
, i
);
4526 partial_i
= isl_multi_union_pw_aff_from_union_pw_aff(upa
);
4527 coincidence_i
= isl_union_map_copy(coincidence
);
4528 coincidence_i
= isl_union_map_eq_at_multi_union_pw_aff(
4529 coincidence_i
, partial_i
);
4530 subset
= isl_union_map_is_subset(coincidence
, coincidence_i
);
4531 isl_union_map_free(coincidence_i
);
4535 node
= isl_schedule_node_band_member_set_coincident(node
, i
,
4539 node
= isl_schedule_node_free(node
);
4540 isl_multi_union_pw_aff_free(partial
);
4541 isl_union_map_free(coincidence
);
4546 /* If "node" is a band, then set its properties.
4548 * In particular, if the band has exactly one member, then mark it permutable.
4549 * Mark the band members coincident based on the coincidence constraints
4552 static __isl_give isl_schedule_node
*set_band_properties(
4553 __isl_take isl_schedule_node
*node
, void *user
)
4555 isl_schedule_constraints
*sc
= user
;
4557 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
4559 if (isl_schedule_node_band_n_member(node
) == 0)
4562 node
= band_set_permutable(node
, sc
);
4563 node
= band_set_coincident(node
, sc
);
4568 /* Return the original schedule with all bands marked permutable and
4569 * all band members marked coincident based on the coincidence constraints.
4570 * The bands are explicitly marked permutable so that they will be considered
4571 * by mark_outer_permutable.
4573 static __isl_give isl_schedule
*determine_properties_original_schedule(
4574 struct gpu_gen
*gen
)
4576 isl_schedule
*schedule
;
4577 isl_schedule_constraints
*sc
;
4579 schedule
= isl_schedule_copy(gen
->prog
->scop
->schedule
);
4580 sc
= construct_schedule_constraints(gen
->prog
);
4581 schedule
= isl_schedule_map_schedule_node_bottom_up(schedule
,
4582 &set_band_properties
, sc
);
4583 isl_schedule_constraints_free(sc
);
4588 /* Compute a schedule or determine the properties of the original schedule
4589 * depending on the value of the "reschedule" option.
4591 static __isl_give isl_schedule
*compute_or_set_properties(void *user
)
4593 struct gpu_gen
*gen
= user
;
4595 if (gen
->options
->reschedule
)
4596 return compute_schedule(gen
);
4598 return determine_properties_original_schedule(gen
);
4601 /* Obtain a schedule for the scop, by reading it from
4602 * a file, by computing one or by determining the properties
4603 * of the original schedule.
4605 static __isl_give isl_schedule
*get_schedule(struct gpu_gen
*gen
)
4607 return ppcg_get_schedule(gen
->ctx
, gen
->options
,
4608 &compute_or_set_properties
, gen
);
4611 /* Construct the string "<a>_<b>".
4613 static char *concat(isl_ctx
*ctx
, const char *a
, const char *b
)
4618 p
= isl_printer_to_str(ctx
);
4619 p
= isl_printer_print_str(p
, a
);
4620 p
= isl_printer_print_str(p
, "_");
4621 p
= isl_printer_print_str(p
, b
);
4622 s
= isl_printer_get_str(p
);
4623 isl_printer_free(p
);
4628 /* For each array in "prog" of which an element appears in "accessed" and
4629 * that is not a read only scalar, create a zero-dimensional universe set
4630 * of which the tuple id has name "<prefix>_<name of array>" and a user
4631 * pointer pointing to the array (gpu_array_info).
4633 * If the array is local to "prog", then make sure it will be declared
4636 * Return the list of these universe sets.
4638 static __isl_give isl_union_set_list
*create_copy_filters(struct gpu_prog
*prog
,
4639 const char *prefix
, __isl_take isl_union_set
*accessed
)
4643 isl_union_set_list
*filters
;
4646 filters
= isl_union_set_list_alloc(ctx
, 0);
4647 for (i
= 0; i
< prog
->n_array
; ++i
) {
4648 struct gpu_array_info
*array
= &prog
->array
[i
];
4650 isl_set
*accessed_i
;
4654 isl_union_set
*uset
;
4656 if (gpu_array_is_read_only_scalar(array
))
4659 space
= isl_space_copy(array
->space
);
4660 accessed_i
= isl_union_set_extract_set(accessed
, space
);
4661 empty
= isl_set_plain_is_empty(accessed_i
);
4662 isl_set_free(accessed_i
);
4664 filters
= isl_union_set_list_free(filters
);
4672 array
->declare_local
= 1;
4674 name
= concat(ctx
, prefix
, array
->name
);
4675 id
= name
? isl_id_alloc(ctx
, name
, array
) : NULL
;
4677 space
= isl_space_set_alloc(ctx
, 0, 0);
4678 space
= isl_space_set_tuple_id(space
, isl_dim_set
, id
);
4679 uset
= isl_union_set_from_set(isl_set_universe(space
));
4681 filters
= isl_union_set_list_add(filters
, uset
);
4683 isl_union_set_free(accessed
);
4688 /* Make sure that code for the statements in "filters" that
4689 * copy arrays to or from the device is only generated when
4690 * the size of the corresponding array is positive.
4691 * That is, add a set node underneath "graft" with "filters" as children
4692 * and for each child add a guard that the selects the parameter
4693 * values for which the corresponding array has a positive size.
4694 * The array is available in the user pointer of the statement identifier.
4695 * "depth" is the schedule depth of the position where "graft"
4698 static __isl_give isl_schedule_node
*insert_positive_size_guards(
4699 __isl_take isl_schedule_node
*graft
,
4700 __isl_take isl_union_set_list
*filters
, int depth
)
4704 graft
= isl_schedule_node_child(graft
, 0);
4705 graft
= isl_schedule_node_insert_set(graft
, filters
);
4706 n
= isl_schedule_node_n_children(graft
);
4707 for (i
= 0; i
< n
; ++i
) {
4708 isl_union_set
*filter
;
4709 isl_set
*domain
, *guard
;
4711 struct gpu_array_info
*array
;
4713 graft
= isl_schedule_node_child(graft
, i
);
4714 filter
= isl_schedule_node_filter_get_filter(graft
);
4715 domain
= isl_set_from_union_set(filter
);
4716 id
= isl_set_get_tuple_id(domain
);
4717 array
= isl_id_get_user(id
);
4719 isl_set_free(domain
);
4720 guard
= gpu_array_positive_size_guard(array
);
4721 guard
= isl_set_from_params(guard
);
4722 guard
= isl_set_add_dims(guard
, isl_dim_set
, depth
);
4723 graft
= isl_schedule_node_child(graft
, 0);
4724 graft
= isl_schedule_node_insert_guard(graft
, guard
);
4725 graft
= isl_schedule_node_parent(graft
);
4726 graft
= isl_schedule_node_parent(graft
);
4728 graft
= isl_schedule_node_parent(graft
);
4733 /* Create a graft for copying arrays to or from the device,
4734 * whenever the size of the array is strictly positive.
4735 * Each statement is called "<prefix>_<name of array>" and
4736 * the identifier has a user pointer pointing to the array.
4737 * The graft will be added at the position specified by "node".
4738 * "copy" contains the array elements that need to be copied.
4739 * Only arrays of which some elements need to be copied
4740 * will have a corresponding statement in the graph.
4741 * Note though that each such statement will copy the entire array.
4743 static __isl_give isl_schedule_node
*create_copy_device(struct gpu_prog
*prog
,
4744 __isl_keep isl_schedule_node
*node
, const char *prefix
,
4745 __isl_take isl_union_set
*copy
)
4750 isl_union_set
*all
, *domain
;
4751 isl_union_set_list
*filters
;
4752 isl_union_map
*extension
;
4753 isl_schedule_node
*graft
;
4756 depth
= isl_schedule_node_get_schedule_depth(node
);
4757 filters
= create_copy_filters(prog
, prefix
, copy
);
4758 all
= isl_union_set_list_union(isl_union_set_list_copy(filters
));
4760 space
= depth
< 0 ? NULL
: isl_space_set_alloc(ctx
, 0, depth
);
4761 domain
= isl_union_set_from_set(isl_set_universe(space
));
4762 extension
= isl_union_map_from_domain_and_range(domain
, all
);
4763 graft
= isl_schedule_node_from_extension(extension
);
4766 return isl_schedule_node_free(graft
);
4767 if (isl_union_set_list_n_union_set(filters
) == 0) {
4768 isl_union_set_list_free(filters
);
4772 return insert_positive_size_guards(graft
, filters
, depth
);
4775 /* Return (the universe spaces of) the arrays that are declared
4776 * inside the scop corresponding to "prog" and for which all
4777 * potential writes inside the scop form a subset of "domain".
4779 static __isl_give isl_union_set
*extract_local_accesses(struct gpu_prog
*prog
,
4780 __isl_keep isl_union_set
*domain
)
4783 isl_union_set
*local
;
4785 local
= isl_union_set_empty(isl_union_set_get_space(domain
));
4787 for (i
= 0; i
< prog
->n_array
; ++i
) {
4789 isl_union_map
*to_outer
;
4790 isl_union_map
*may_write
;
4791 isl_union_set
*write_domain
;
4792 isl_union_set
*fields
;
4795 if (!prog
->array
[i
].local
)
4798 set
= isl_set_universe(isl_space_copy(prog
->array
[i
].space
));
4799 to_outer
= isl_union_map_copy(prog
->to_outer
);
4800 to_outer
= isl_union_map_intersect_range(to_outer
,
4801 isl_union_set_from_set(isl_set_copy(set
)));
4802 fields
= isl_union_map_domain(to_outer
);
4803 may_write
= isl_union_map_copy(prog
->may_write
);
4804 may_write
= isl_union_map_intersect_range(may_write
, fields
);
4805 write_domain
= isl_union_map_domain(may_write
);
4806 subset
= isl_union_set_is_subset(write_domain
, domain
);
4807 isl_union_set_free(write_domain
);
4811 return isl_union_set_free(local
);
4812 } else if (subset
) {
4813 local
= isl_union_set_add_set(local
, set
);
4822 /* Internal data structure for node_may_persist.
4824 * "tagger" maps tagged iteration domains to the corresponding untagged
4827 * "may_persist_flow" is the set of all tagged dataflow dependences
4828 * with those dependences removed that either precede or follow
4829 * the kernel launch in a sequence.
4830 * "inner_band_flow" is the set of all tagged dataflow dependences
4831 * that are local to a given iteration of the outer band nodes
4832 * with respect to the current node.
4833 * "local_flow" is equal to "inner_band_flow", except that the domain
4834 * and the range have been intersected with intermediate filters
4835 * on children of sets or sequences.
4837 struct ppcg_may_persist_data
{
4838 isl_union_pw_multi_aff
*tagger
;
4840 isl_union_map
*local_flow
;
4841 isl_union_map
*inner_band_flow
;
4842 isl_union_map
*may_persist_flow
;
4845 /* Update the information in "data" based on the band ancestor "node".
4847 * In particular, we restrict the dependences in data->local_flow
4848 * to those dependence where the source and the sink occur in
4849 * the same iteration of the given band node.
4850 * We also update data->inner_band_flow to the new value of
4853 static int update_may_persist_at_band(__isl_keep isl_schedule_node
*node
,
4854 struct ppcg_may_persist_data
*data
)
4856 isl_multi_union_pw_aff
*partial
;
4857 isl_union_pw_multi_aff
*contraction
;
4858 isl_union_map
*flow
;
4860 if (isl_schedule_node_band_n_member(node
) == 0)
4863 partial
= isl_schedule_node_band_get_partial_schedule(node
);
4864 contraction
= isl_schedule_node_get_subtree_contraction(node
);
4865 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4867 partial
= isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial
,
4868 isl_union_pw_multi_aff_copy(data
->tagger
));
4870 flow
= data
->local_flow
;
4871 flow
= isl_union_map_eq_at_multi_union_pw_aff(flow
, partial
);
4872 data
->local_flow
= flow
;
4874 isl_union_map_free(data
->inner_band_flow
);
4875 data
->inner_band_flow
= isl_union_map_copy(data
->local_flow
);
4880 /* Given a set of local reaching domain elements "domain",
4881 * expand them to the corresponding leaf domain elements using "contraction"
4882 * and insert the array references tags using data->tagger.
4884 static __isl_give isl_union_set
*expand_and_tag(
4885 __isl_take isl_union_set
*domain
,
4886 __isl_take isl_union_pw_multi_aff
*contraction
,
4887 struct ppcg_may_persist_data
*data
)
4889 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4891 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
4892 isl_union_pw_multi_aff_copy(data
->tagger
));
4896 /* Given a filter node that is the child of a set or sequence node,
4897 * restrict data->local_flow to refer only to those elements
4898 * in the filter of the node.
4899 * "contraction" maps the leaf domain elements of the schedule tree
4900 * to the corresponding domain elements at (the parent of) "node".
4902 static int filter_flow(__isl_keep isl_schedule_node
*node
,
4903 struct ppcg_may_persist_data
*data
,
4904 __isl_take isl_union_pw_multi_aff
*contraction
)
4906 isl_union_set
*filter
;
4907 isl_union_map
*flow
;
4909 flow
= data
->local_flow
;
4910 filter
= isl_schedule_node_filter_get_filter(node
);
4911 filter
= expand_and_tag(filter
, contraction
, data
);
4912 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(filter
));
4913 flow
= isl_union_map_intersect_range(flow
, filter
);
4914 data
->local_flow
= flow
;
4919 /* Given a filter node "node", collect the filters on all preceding siblings
4920 * (which are also filter nodes), add them to "filters" and return the result.
4922 static __isl_give isl_union_set
*add_previous_filters(
4923 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4925 isl_schedule_node
*sibling
;
4927 sibling
= isl_schedule_node_copy(node
);
4928 while (sibling
&& isl_schedule_node_has_previous_sibling(sibling
)) {
4929 isl_union_set
*filter
;
4931 sibling
= isl_schedule_node_previous_sibling(sibling
);
4932 filter
= isl_schedule_node_filter_get_filter(sibling
);
4933 filters
= isl_union_set_union(filters
, filter
);
4935 isl_schedule_node_free(sibling
);
4937 return isl_union_set_free(filters
);
4942 /* Given a filter node "node", collect the filters on all following siblings
4943 * (which are also filter nodes), add them to "filters" and return the result.
4945 static __isl_give isl_union_set
*add_next_filters(
4946 __isl_take isl_union_set
*filters
, __isl_keep isl_schedule_node
*node
)
4948 isl_schedule_node
*sibling
;
4950 sibling
= isl_schedule_node_copy(node
);
4951 while (sibling
&& isl_schedule_node_has_next_sibling(sibling
)) {
4952 isl_union_set
*filter
;
4954 sibling
= isl_schedule_node_next_sibling(sibling
);
4955 filter
= isl_schedule_node_filter_get_filter(sibling
);
4956 filters
= isl_union_set_union(filters
, filter
);
4958 isl_schedule_node_free(sibling
);
4960 return isl_union_set_free(filters
);
4965 /* Remove those flow dependences from data->may_persist_flow
4966 * that flow between elements of "domain" within the same iteration
4967 * of all outer band nodes.
4968 * "contraction" maps the leaf domain elements of the schedule tree
4969 * to the corresponding elements "domain".
4971 static void remove_external_flow(struct ppcg_may_persist_data
*data
,
4972 __isl_take isl_union_set
*domain
,
4973 __isl_keep isl_union_pw_multi_aff
*contraction
)
4975 isl_union_map
*flow
;
4977 contraction
= isl_union_pw_multi_aff_copy(contraction
);
4978 domain
= expand_and_tag(domain
, contraction
, data
);
4979 flow
= isl_union_map_copy(data
->local_flow
);
4980 flow
= isl_union_map_intersect_domain(flow
, isl_union_set_copy(domain
));
4981 flow
= isl_union_map_intersect_range(flow
, domain
);
4983 data
->may_persist_flow
= isl_union_map_subtract(data
->may_persist_flow
,
4987 /* Update the information in "data" based on the filter ancestor "node".
4988 * We only need to modify anything if the filter is the child
4989 * of a set or sequence node.
4991 * In the case of a sequence, we remove the dependences between
4992 * statement instances that are both executed either before or
4993 * after the subtree that will be mapped to a kernel, within
4994 * the same iteration of outer bands.
4996 * In both cases, we restrict data->local_flow to the current child.
4998 static int update_may_persist_at_filter(__isl_keep isl_schedule_node
*node
,
4999 struct ppcg_may_persist_data
*data
)
5001 enum isl_schedule_node_type type
;
5002 isl_schedule_node
*parent
;
5004 isl_union_pw_multi_aff
*contraction
;
5005 isl_union_set
*before
, *after
, *filter
;
5007 type
= isl_schedule_node_get_parent_type(node
);
5008 if (type
!= isl_schedule_node_sequence
&& type
!= isl_schedule_node_set
)
5011 parent
= isl_schedule_node_copy(node
);
5012 parent
= isl_schedule_node_parent(parent
);
5013 contraction
= isl_schedule_node_get_subtree_contraction(parent
);
5014 isl_schedule_node_free(parent
);
5016 if (type
== isl_schedule_node_set
)
5017 return filter_flow(node
, data
, contraction
);
5019 filter
= isl_schedule_node_filter_get_filter(node
);
5020 space
= isl_union_set_get_space(filter
);
5021 isl_union_set_free(filter
);
5022 before
= isl_union_set_empty(space
);
5023 after
= isl_union_set_copy(before
);
5024 before
= add_previous_filters(before
, node
);
5025 after
= add_next_filters(after
, node
);
5027 remove_external_flow(data
, before
, contraction
);
5028 remove_external_flow(data
, after
, contraction
);
5030 return filter_flow(node
, data
, contraction
);
5033 /* Update the information in "data" based on the ancestor "node".
5035 static isl_stat
update_may_persist_at(__isl_keep isl_schedule_node
*node
,
5038 struct ppcg_may_persist_data
*data
= user
;
5040 switch (isl_schedule_node_get_type(node
)) {
5041 case isl_schedule_node_error
:
5042 return isl_stat_error
;
5043 case isl_schedule_node_context
:
5044 case isl_schedule_node_domain
:
5045 case isl_schedule_node_expansion
:
5046 case isl_schedule_node_extension
:
5047 case isl_schedule_node_guard
:
5048 case isl_schedule_node_leaf
:
5049 case isl_schedule_node_mark
:
5050 case isl_schedule_node_sequence
:
5051 case isl_schedule_node_set
:
5053 case isl_schedule_node_band
:
5054 if (update_may_persist_at_band(node
, data
) < 0)
5055 return isl_stat_error
;
5057 case isl_schedule_node_filter
:
5058 if (update_may_persist_at_filter(node
, data
) < 0)
5059 return isl_stat_error
;
5066 /* Determine the set of array elements that may need to be perserved
5067 * by a kernel constructed from the subtree at "node".
5068 * This includes the set of array elements that may need to be preserved
5069 * by the entire scop (prog->may_persist) and the elements for which
5070 * there is a potential flow dependence that may cross a kernel launch.
5072 * To determine the second set, we start from all flow dependences.
5073 * From this set of dependences, we remove those that cannot possibly
5074 * require data to be preserved by a kernel launch.
5075 * In particular, we consider the following sets of dependences.
5076 * - dependences of which the write occurs inside the kernel.
5077 * If the data is needed outside the kernel, then it will
5078 * be copied out immediately after the kernel launch, so there
5079 * is no need for any special care.
5080 * - dependences of which the read occurs inside the kernel and the
5081 * corresponding write occurs inside the same iteration of the
5082 * outer band nodes. This means that the data is needed in
5083 * the first kernel launch after the write, which is already
5084 * taken care of by the standard copy-in. That is, the data
5085 * do not need to be preserved by any intermediate call to
5087 * - dependences of which the write and the read either both occur
5088 * before the kernel launch or both occur after the kernel launch,
5089 * within the same iteration of the outer band nodes with respect
5090 * to the sequence that determines the ordering of the dependence
5091 * and the kernel launch. Such flow dependences cannot cross
5092 * any kernel launch.
5094 * For the remaining (tagged) dependences, we take the domain
5095 * (i.e., the tagged writes) and apply the tagged access relation
5096 * to obtain the accessed data elements.
5097 * These are then combined with the elements that may need to be
5098 * preserved by the entire scop.
5100 static __isl_give isl_union_set
*node_may_persist(
5101 __isl_keep isl_schedule_node
*node
, struct gpu_prog
*prog
)
5103 struct ppcg_may_persist_data data
;
5104 isl_union_pw_multi_aff
*contraction
;
5105 isl_union_set
*domain
;
5106 isl_union_set
*persist
;
5107 isl_union_map
*flow
, *local_flow
;
5109 data
.tagger
= prog
->scop
->tagger
;
5111 flow
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
5112 data
.local_flow
= isl_union_map_copy(flow
);
5113 data
.inner_band_flow
= isl_union_map_copy(flow
);
5114 data
.may_persist_flow
= flow
;
5115 if (isl_schedule_node_foreach_ancestor_top_down(node
,
5116 &update_may_persist_at
, &data
) < 0)
5117 data
.may_persist_flow
=
5118 isl_union_map_free(data
.may_persist_flow
);
5119 flow
= data
.may_persist_flow
;
5120 isl_union_map_free(data
.local_flow
);
5122 domain
= isl_schedule_node_get_domain(node
);
5123 contraction
= isl_schedule_node_get_subtree_contraction(node
);
5124 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5126 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5127 isl_union_pw_multi_aff_copy(data
.tagger
));
5128 flow
= isl_union_map_subtract_domain(flow
, isl_union_set_copy(domain
));
5129 local_flow
= data
.inner_band_flow
;
5130 local_flow
= isl_union_map_intersect_range(local_flow
, domain
);
5131 flow
= isl_union_map_subtract(flow
, local_flow
);
5133 persist
= isl_union_map_domain(flow
);
5134 persist
= isl_union_set_apply(persist
,
5135 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5136 persist
= isl_union_set_union(persist
,
5137 isl_union_set_copy(prog
->may_persist
));
5142 /* Add nodes for copying outer arrays in and out of the device
5143 * before and after the subtree "node", which contains one or more kernels.
5144 * "domain" contains the original statement instances, i.e.,
5145 * those that correspond to the domains of the access relations in "prog".
5146 * In particular, the domain has not been contracted in any way.
5147 * "prefix" contains the prefix schedule at that point, in terms
5148 * of the same original statement instances.
5150 * We first compute the sets of outer array elements that need
5151 * to be copied in and out and then graft in the nodes for
5152 * performing this copying.
5154 * In particular, for each array that is possibly written anywhere in
5155 * the subtree "node" and that may be used after "node"
5156 * or that may be visible outside the corresponding scop,
5157 * we copy out its entire extent.
5159 * Any array elements that is read without first being written inside
5160 * the subtree "node" needs to be copied in.
5161 * Furthermore, if there are any array elements that
5162 * are copied out, but that may not be written inside "node, then
5163 * they also need to be copied in to ensure that the value after execution
5164 * is the same as the value before execution, at least for those array
5165 * elements that may have their values preserved by the scop or that
5166 * may be written before "node" and read after "node".
5167 * In case the array elements are structures, we need to take into
5168 * account that all members of the structures need to be written
5169 * by "node" before we can avoid copying the data structure in.
5171 * Note that the may_write relation is intersected with the domain,
5172 * which has been intersected with the context.
5173 * This helps in those cases where the arrays are declared with a fixed size,
5174 * while the accesses are parametric and the context assigns a fixed value
5175 * to the parameters.
5177 * If an element from a local array is read without first being written,
5178 * then there is no point in copying it in since it cannot have been
5179 * written prior to the scop. Warn about the uninitialized read instead.
5181 static __isl_give isl_schedule_node
*add_to_from_device(
5182 __isl_take isl_schedule_node
*node
, __isl_take isl_union_set
*domain
,
5183 __isl_take isl_union_map
*prefix
, struct gpu_prog
*prog
)
5185 isl_union_set
*local
;
5186 isl_union_set
*may_persist
;
5187 isl_union_map
*may_write
, *must_write
, *copy_out
, *not_written
;
5188 isl_union_map
*read
, *copy_in
;
5189 isl_union_map
*tagged
;
5190 isl_union_map
*local_uninitialized
;
5191 isl_schedule_node
*graft
;
5193 tagged
= isl_union_map_copy(prog
->scop
->tagged_reads
);
5194 tagged
= isl_union_map_union(tagged
,
5195 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
5197 may_write
= isl_union_map_copy(prog
->may_write
);
5198 may_write
= isl_union_map_intersect_domain(may_write
,
5199 isl_union_set_copy(domain
));
5200 may_write
= remove_local_accesses(prog
,
5201 isl_union_map_copy(tagged
), may_write
,
5202 isl_union_map_copy(prefix
), 0);
5203 may_write
= isl_union_map_apply_range(may_write
,
5204 isl_union_map_copy(prog
->to_outer
));
5205 may_write
= isl_union_map_apply_domain(may_write
,
5206 isl_union_map_copy(prefix
));
5207 may_write
= approximate_copy_out(may_write
, prog
);
5208 copy_out
= isl_union_map_copy(may_write
);
5209 may_write
= isl_union_map_apply_range(may_write
,
5210 isl_union_map_copy(prog
->to_inner
));
5211 must_write
= isl_union_map_copy(prog
->must_write
);
5212 must_write
= isl_union_map_apply_domain(must_write
,
5213 isl_union_map_copy(prefix
));
5214 may_persist
= node_may_persist(node
, prog
);
5215 may_write
= isl_union_map_intersect_range(may_write
, may_persist
);
5216 not_written
= isl_union_map_subtract(may_write
, must_write
);
5218 local
= extract_local_accesses(prog
, domain
);
5219 read
= isl_union_map_copy(prog
->read
);
5220 read
= isl_union_map_intersect_domain(read
, domain
);
5221 read
= remove_local_accesses(prog
, tagged
, read
,
5222 isl_union_map_copy(prefix
), 1);
5223 local
= isl_union_set_apply(local
, isl_union_map_copy(prog
->to_inner
));
5224 local_uninitialized
= isl_union_map_copy(prog
->scop
->live_in
);
5225 local_uninitialized
= isl_union_map_intersect_range(local_uninitialized
,
5227 local_uninitialized
= isl_union_map_intersect(local_uninitialized
,
5228 isl_union_map_copy(read
));
5229 if (!isl_union_map_is_empty(local_uninitialized
)) {
5231 "possibly uninitialized reads (not copied in):\n");
5232 isl_union_map_dump(local_uninitialized
);
5234 read
= isl_union_map_subtract(read
, local_uninitialized
);
5235 read
= isl_union_map_apply_domain(read
, prefix
);
5236 copy_in
= isl_union_map_union(read
, not_written
);
5237 copy_in
= isl_union_map_apply_range(copy_in
,
5238 isl_union_map_copy(prog
->to_outer
));
5240 graft
= create_copy_device(prog
, node
, "to_device",
5241 isl_union_map_range(copy_in
));
5242 node
= isl_schedule_node_graft_before(node
, graft
);
5243 graft
= create_copy_device(prog
, node
, "from_device",
5244 isl_union_map_range(copy_out
));
5245 node
= isl_schedule_node_graft_after(node
, graft
);
5250 /* Add nodes for initializing ("init_device") and clearing ("clear_device")
5251 * the device before and after "node".
5253 static __isl_give isl_schedule_node
*add_init_clear_device(
5254 __isl_take isl_schedule_node
*node
)
5258 isl_union_set
*domain
;
5259 isl_schedule_node
*graft
;
5261 ctx
= isl_schedule_node_get_ctx(node
);
5263 space
= isl_space_set_alloc(ctx
, 0, 0);
5264 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "init_device");
5265 domain
= isl_union_set_from_set(isl_set_universe(space
));
5266 graft
= isl_schedule_node_from_domain(domain
);
5268 node
= isl_schedule_node_graft_before(node
, graft
);
5270 space
= isl_space_set_alloc(ctx
, 0, 0);
5271 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "clear_device");
5272 domain
= isl_union_set_from_set(isl_set_universe(space
));
5273 graft
= isl_schedule_node_from_domain(domain
);
5275 node
= isl_schedule_node_graft_after(node
, graft
);
5280 /* Update "schedule" for mapping to a GPU device.
5282 * In particular, insert a context node, create kernels for
5283 * each outermost tilable band and introduce nodes for copying arrays
5284 * in and out of the device and for initializing and clearing the device.
5285 * If the child of the initial root points to a set node,
5286 * then children of this node that do not contain any tilable bands
5287 * are separated from the other children and are not mapped to
5290 * The GPU code is generated in a context where at least one
5291 * statement instance is executed. The corresponding guard is inserted
5292 * around the entire schedule.
5294 static __isl_give isl_schedule
*map_to_device(struct gpu_gen
*gen
,
5295 __isl_take isl_schedule
*schedule
)
5297 isl_schedule_node
*node
;
5300 isl_union_set
*domain
;
5301 isl_union_map
*prefix
;
5302 isl_union_pw_multi_aff
*contraction
;
5303 struct gpu_prog
*prog
;
5305 context
= isl_set_copy(gen
->prog
->context
);
5306 context
= isl_set_from_params(context
);
5307 schedule
= isl_schedule_insert_context(schedule
, context
);
5310 guard
= isl_union_set_params(isl_union_set_copy(prog
->scop
->domain
));
5311 prog
->context
= isl_set_intersect(prog
->context
, isl_set_copy(guard
));
5312 guard
= isl_set_from_params(guard
);
5314 node
= isl_schedule_get_root(schedule
);
5315 isl_schedule_free(schedule
);
5316 node
= isl_schedule_node_child(node
, 0);
5317 node
= isl_schedule_node_child(node
, 0);
5318 node
= isolate_permutable_subtrees(node
, gen
->prog
);
5319 domain
= isl_schedule_node_get_domain(node
);
5320 contraction
= isl_schedule_node_get_subtree_contraction(node
);
5321 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
,
5322 isl_union_pw_multi_aff_copy(contraction
));
5323 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
5324 prefix
= isl_union_map_preimage_domain_union_pw_multi_aff(prefix
,
5326 node
= mark_kernels(gen
, node
);
5327 node
= add_to_from_device(node
, domain
, prefix
, gen
->prog
);
5328 node
= isl_schedule_node_root(node
);
5329 node
= isl_schedule_node_child(node
, 0);
5330 node
= isl_schedule_node_child(node
, 0);
5331 node
= isl_schedule_node_insert_guard(node
, guard
);
5332 node
= isl_schedule_node_child(node
, 0);
5333 node
= add_init_clear_device(node
);
5334 schedule
= isl_schedule_node_get_schedule(node
);
5335 isl_schedule_node_free(node
);
5340 /* Internal data structure for extract_access.
5341 * "next_access" points to the end of a linked list that is extended
5342 * by extract_access.
5343 * "single_expression" is set if the access expressions belong to
5344 * an expression statement (i.e., a statement without internal control).
5345 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5347 struct ppcg_extract_access_data
{
5348 struct gpu_stmt_access
**next_access
;
5349 int single_expression
;
5350 isl_union_map
*any_to_outer
;
5353 /* Given a tagged access relation to a single array "tagged", extract it
5354 * as a map, taking into account that the input may be empty.
5355 * If the access relation is empty, then it does not contain
5356 * any space information, so we try to recover it from the index
5358 * The space of the index expression is of the form I -> A,
5359 * with I the statement instances and A the array, or [I -> F] -> A,
5360 * with F the filters corresponding to arguments.
5361 * We first drop F, if present, obtaining I -> A.
5362 * Then we construct I -> R, with R the reference tag,
5363 * combine the two into I -> [R -> A] and uncurry to obtain
5364 * the final result [I -> R] -> A.
5365 * Note that the index expression may have a lower dimension
5366 * than that of the array, but this dimension is not used
5367 * if the access relation is empty.
5369 static __isl_give isl_map
*extract_single_tagged_access(
5370 __isl_take isl_union_map
*tagged
, __isl_keep pet_expr
*expr
)
5374 isl_space
*space
, *space2
;
5375 isl_multi_pw_aff
*index
;
5377 empty
= isl_union_map_is_empty(tagged
);
5381 return isl_map_from_union_map(tagged
);
5382 isl_union_map_free(tagged
);
5384 index
= pet_expr_access_get_index(expr
);
5385 space
= isl_multi_pw_aff_get_space(index
);
5386 isl_multi_pw_aff_free(index
);
5387 if (isl_space_domain_is_wrapping(space
))
5388 space
= isl_space_domain_factor_domain(space
);
5389 space2
= isl_space_copy(space
);
5390 space2
= isl_space_from_domain(isl_space_domain(space
));
5391 id
= pet_expr_access_get_ref_id(expr
);
5392 space2
= isl_space_set_tuple_id(space2
, isl_dim_out
, id
);
5393 space
= isl_space_range_product(space2
, space
);
5394 space
= isl_space_uncurry(space
);
5396 return isl_map_empty(space
);
5398 isl_union_map_free(tagged
);
5402 /* Does the index expression "index" of "expr" represent an access
5403 * to a single element?
5404 * That is, is "index" completely specified?
5406 * If "expr" accesses elements from different spaces (i.e., fields
5407 * of a structure), then it does not access a single element.
5408 * Otherwise, if the single space of the access matches the space
5409 * of "index", then the index expression is completely specified
5410 * (no pointer to a lower-dimensional slice of the accessed array)
5411 * and a single element is being accessed.
5413 static isl_bool
complete_index(__isl_keep pet_expr
*expr
,
5414 __isl_keep isl_multi_pw_aff
*index
)
5416 isl_union_map
*read
, *write
, *all
;
5418 isl_space
*space1
, *space2
;
5421 read
= pet_expr_access_get_may_read(expr
);
5422 write
= pet_expr_access_get_may_write(expr
);
5423 all
= isl_union_map_union(read
, write
);
5425 return isl_bool_error
;
5426 if (isl_union_map_n_map(all
) != 1) {
5427 isl_union_map_free(all
);
5428 return isl_bool_false
;
5430 map
= isl_map_from_union_map(all
);
5431 space1
= isl_map_get_space(map
);
5433 space2
= isl_multi_pw_aff_get_space(index
);
5434 complete
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
5435 space2
, isl_dim_out
);
5436 isl_space_free(space1
);
5437 isl_space_free(space2
);
5442 /* Does "expr" access a single, fixed element (independently of the statement
5444 * That is, does it have a completely specified constant index expression?
5446 * Note that it is not sufficient for the index expression to be
5447 * piecewise constant. isl_multi_pw_aff_is_cst can therefore not be used.
5449 static isl_bool
accesses_fixed_element(__isl_keep pet_expr
*expr
)
5452 isl_multi_pw_aff
*index
;
5453 isl_bool fixed
= isl_bool_true
;
5455 index
= pet_expr_access_get_index(expr
);
5457 return isl_bool_error
;
5458 n
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5459 for (i
= 0; i
< n
; ++i
) {
5462 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
5463 fixed
= isl_pw_aff_n_piece(pa
) == 1;
5465 fixed
= isl_pw_aff_is_cst(pa
);
5466 isl_pw_aff_free(pa
);
5467 if (fixed
< 0 || !fixed
)
5470 if (fixed
>= 0 && fixed
)
5471 fixed
= complete_index(expr
, index
);
5472 isl_multi_pw_aff_free(index
);
5477 /* Extract a gpu_stmt_access from "expr", append it to the list
5478 * that ends in *data->next_access and update the end of the list.
5479 * If the access expression performs a write, then it is considered
5480 * exact only if it appears in a single expression statement and
5481 * if its may access relation is equal to its must access relation.
5483 * The combined set of may accesses may be a union if member accesses
5484 * are involved, but the entire set is derived from a single reference and
5485 * therefore from a single index expression. These accesses therefore
5486 * all map to the same outer array.
5488 static int extract_access(__isl_keep pet_expr
*expr
, void *user
)
5490 struct ppcg_extract_access_data
*data
= user
;
5491 isl_union_map
*tagged
;
5492 struct gpu_stmt_access
*access
;
5493 isl_ctx
*ctx
= pet_expr_get_ctx(expr
);
5494 isl_multi_pw_aff
*index
;
5496 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
5499 access
->next
= NULL
;
5500 access
->read
= pet_expr_access_is_read(expr
);
5501 access
->write
= pet_expr_access_is_write(expr
);
5502 tagged
= pet_expr_access_get_tagged_may_read(expr
);
5503 tagged
= isl_union_map_union(tagged
,
5504 pet_expr_access_get_tagged_may_write(expr
));
5505 tagged
= isl_union_map_apply_range(tagged
,
5506 isl_union_map_copy(data
->any_to_outer
));
5507 if (!access
->write
) {
5508 access
->exact_write
= 1;
5509 } else if (!data
->single_expression
) {
5510 access
->exact_write
= 0;
5512 isl_union_map
*must
, *may
;
5513 may
= isl_union_map_copy(tagged
);
5514 may
= isl_union_map_domain_factor_domain(may
);
5515 must
= pet_expr_access_get_must_write(expr
);
5516 access
->exact_write
= isl_union_map_is_equal(must
, may
);
5517 isl_union_map_free(must
);
5518 isl_union_map_free(may
);
5520 index
= pet_expr_access_get_index(expr
);
5521 access
->n_index
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
5522 isl_multi_pw_aff_free(index
);
5523 access
->ref_id
= pet_expr_access_get_ref_id(expr
);
5524 access
->tagged_access
= extract_single_tagged_access(tagged
, expr
);
5525 access
->access
= isl_map_copy(access
->tagged_access
);
5526 access
->access
= isl_map_domain_factor_domain(access
->access
);
5527 access
->fixed_element
= accesses_fixed_element(expr
);
5529 *data
->next_access
= access
;
5530 data
->next_access
= &(*data
->next_access
)->next
;
5532 if (!access
->access
|| access
->fixed_element
< 0)
5538 /* Construct a linked list of gpu_stmt_access objects,
5539 * one for each access expression in the statement body.
5540 * "any_to_outer" maps all intermediate arrays to their outer arrays.
5542 static int pet_stmt_extract_accesses(struct gpu_stmt
*stmt
,
5543 __isl_keep isl_union_map
*any_to_outer
)
5545 struct ppcg_extract_access_data data
;
5547 stmt
->accesses
= NULL
;
5548 data
.next_access
= &stmt
->accesses
;
5549 data
.single_expression
=
5550 pet_tree_get_type(stmt
->stmt
->body
) == pet_tree_expr
;
5551 data
.any_to_outer
= any_to_outer
;
5552 return pet_tree_foreach_access_expr(stmt
->stmt
->body
,
5553 &extract_access
, &data
);
5556 /* Has statement "stmt" been killed from "scop"?
5557 * That is, is the instance set of "scop" free from any
5558 * instances of "stmt"?
5560 static isl_bool
is_stmt_killed(struct ppcg_scop
*scop
, struct pet_stmt
*stmt
)
5567 return isl_bool_error
;
5568 space
= isl_set_get_space(stmt
->domain
);
5569 left
= isl_union_set_extract_set(scop
->domain
, space
);
5570 empty
= isl_set_plain_is_empty(left
);
5576 /* Return an array of gpu_stmt representing the statements in "scop".
5577 * Do not collect array accesses for statements that have been killed.
5579 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
5580 __isl_keep isl_union_map
*any_to_outer
)
5583 struct gpu_stmt
*stmts
;
5585 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->pet
->n_stmt
);
5589 for (i
= 0; i
< scop
->pet
->n_stmt
; ++i
) {
5590 struct gpu_stmt
*s
= &stmts
[i
];
5593 s
->id
= isl_set_get_tuple_id(scop
->pet
->stmts
[i
]->domain
);
5594 s
->stmt
= scop
->pet
->stmts
[i
];
5595 killed
= is_stmt_killed(scop
, scop
->pet
->stmts
[i
]);
5597 return free_stmts(stmts
, i
+ 1);
5600 if (pet_stmt_extract_accesses(s
, any_to_outer
) < 0)
5601 return free_stmts(stmts
, i
+ 1);
5607 /* Generate CUDA code for "scop" and print it to "p".
5608 * After generating an AST for the transformed scop as explained below,
5609 * we call "gen->print" to print the AST in the desired output format
5612 * If it turns out that it does not make sense to generate GPU code,
5613 * then we generate CPU code instead.
5615 * The declarations of the arrays that are visible outside of the scop
5616 * are printed outside of the code generated from the schedule,
5617 * because the generated code may involve a guard around the entire code.
5619 * We first compute a schedule that respects the dependences
5620 * of the original program and select the outermost bands
5621 * of tilable dimensions that have at least one parallel loop.
5622 * If the --load-schedule is specified, then the loaded schedule
5623 * is used instead of a computed schedule.
5625 * Each of these bands B is then tiled according to "tile" sizes, resulting
5626 * in two nested bands, with a kernel marker on top
5634 * We then split off at most 2 parallel dimensions from the T band and
5635 * at most 3 parallel dimension from the P band
5648 * A filter is introduced in front of T1 that maps the domain instances
5649 * to block identifiers. Similarly, a filter is introduced in front of P1
5650 * that maps the domain instances to thread identifiers.
5652 * For each iteration of the T2 band and for each array, we compute
5653 * the array elements accessed by that iteration, construct a rectangular
5654 * box around it and shift it to the origin. The result is used
5655 * as shared memory for the array.
5657 * Copying and synchronization statements are added to this schedule tree.
5658 * In principle, these are added in front of the P1 band, but some of
5659 * them may get hoisted up to higher levels.
5661 * The entire AST is then generated from the single resulting schedule tree.
5662 * During the generation the subtrees at kernel nodes (K) are saved
5663 * aside and replaced by kernel calls. The result is printed as host code
5664 * while the saved subtrees are printed as device code.
5666 static __isl_give isl_printer
*generate(__isl_take isl_printer
*p
,
5667 struct gpu_gen
*gen
, struct ppcg_scop
*scop
,
5668 struct ppcg_options
*options
)
5670 struct gpu_prog
*prog
;
5672 isl_schedule
*schedule
;
5673 isl_bool any_permutable
;
5676 return isl_printer_free(p
);
5678 ctx
= isl_printer_get_ctx(p
);
5679 prog
= gpu_prog_alloc(ctx
, scop
);
5681 return isl_printer_free(p
);
5684 schedule
= get_schedule(gen
);
5686 any_permutable
= has_any_permutable_node(schedule
);
5687 if (any_permutable
< 0 || !any_permutable
) {
5688 if (any_permutable
< 0)
5689 p
= isl_printer_free(p
);
5691 p
= print_cpu(p
, scop
, options
);
5692 isl_schedule_free(schedule
);
5694 schedule
= map_to_device(gen
, schedule
);
5695 gen
->tree
= generate_code(gen
, schedule
);
5696 p
= ppcg_set_macro_names(p
);
5697 p
= ppcg_print_exposed_declarations(p
, prog
->scop
);
5698 p
= gen
->print(p
, gen
->prog
, gen
->tree
, &gen
->types
,
5700 isl_ast_node_free(gen
->tree
);
5703 gpu_prog_free(prog
);
5708 /* Wrapper around generate for use as a ppcg_transform callback.
5710 static __isl_give isl_printer
*generate_wrap(__isl_take isl_printer
*p
,
5711 struct ppcg_scop
*scop
, void *user
)
5713 struct gpu_gen
*gen
= user
;
5715 return generate(p
, gen
, scop
, gen
->options
);
5718 /* Transform the code in the file called "input" by replacing
5719 * all scops by corresponding GPU code and write the results to "out".
5721 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
5722 struct ppcg_options
*options
,
5723 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
5724 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
5725 struct gpu_types
*types
, void *user
), void *user
)
5732 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
5733 gen
.options
= options
;
5736 gen
.print_user
= user
;
5738 gen
.types
.name
= NULL
;
5740 if (options
->debug
->dump_sizes
) {
5741 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
5742 gen
.used_sizes
= isl_union_map_empty(space
);
5745 r
= ppcg_transform(ctx
, input
, out
, options
, &generate_wrap
, &gen
);
5747 if (options
->debug
->dump_sizes
) {
5748 isl_union_map_dump(gen
.used_sizes
);
5749 isl_union_map_free(gen
.used_sizes
);
5752 isl_union_map_free(gen
.sizes
);
5753 for (i
= 0; i
< gen
.types
.n
; ++i
)
5754 free(gen
.types
.name
[i
]);
5755 free(gen
.types
.name
);
5760 /* Compute the set of inner array elements that may have their values
5761 * preserved by "prog". In particular, collect the array elements of
5762 * arrays that are not local to "prog" and remove those elements that
5763 * are definitely killed or definitely written by "prog".
5765 static __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
)
5768 isl_union_set
*may_persist
, *killed
;
5769 isl_union_map
*must_kill
;
5771 may_persist
= isl_union_set_empty(isl_set_get_space(prog
->context
));
5772 for (i
= 0; i
< prog
->n_array
; ++i
) {
5775 if (prog
->array
[i
].local
)
5778 extent
= isl_set_copy(prog
->array
[i
].extent
);
5779 may_persist
= isl_union_set_add_set(may_persist
, extent
);
5782 may_persist
= isl_union_set_intersect_params(may_persist
,
5783 isl_set_copy(prog
->context
));
5784 may_persist
= isl_union_set_apply(may_persist
,
5785 isl_union_map_copy(prog
->to_inner
));
5786 must_kill
= isl_union_map_copy(prog
->tagged_must_kill
);
5787 killed
= isl_union_map_range(must_kill
);
5788 must_kill
= isl_union_map_copy(prog
->must_write
);
5789 killed
= isl_union_set_union(killed
, isl_union_map_range(must_kill
));
5791 may_persist
= isl_union_set_subtract(may_persist
, killed
);
5795 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
5797 struct gpu_prog
*prog
;
5804 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
5809 prog
->context
= isl_set_copy(scop
->context
);
5810 prog
->n_stmts
= scop
->pet
->n_stmt
;
5811 prog
->any_to_outer
= pet_scop_compute_outer_to_any(scop
->pet
);
5812 prog
->any_to_outer
= isl_union_map_reverse(prog
->any_to_outer
);
5813 space
= isl_union_map_get_space(prog
->any_to_outer
);
5814 space
= isl_space_set_from_params(space
);
5815 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
5816 space
= isl_space_map_from_set(space
);
5817 id
= isl_map_identity(space
);
5818 prog
->any_to_outer
= isl_union_map_add_map(prog
->any_to_outer
, id
);
5819 prog
->stmts
= extract_stmts(ctx
, scop
, prog
->any_to_outer
);
5820 prog
->read
= isl_union_map_copy(scop
->reads
);
5821 prog
->may_write
= isl_union_map_copy(scop
->may_writes
);
5822 prog
->must_write
= isl_union_map_copy(scop
->must_writes
);
5823 prog
->tagged_must_kill
= isl_union_map_copy(scop
->tagged_must_kills
);
5824 prog
->to_inner
= pet_scop_compute_outer_to_inner(scop
->pet
);
5825 prog
->to_outer
= isl_union_map_copy(prog
->to_inner
);
5826 prog
->to_outer
= isl_union_map_reverse(prog
->to_outer
);
5829 return gpu_prog_free(prog
);
5831 if (collect_array_info(prog
) < 0)
5832 return gpu_prog_free(prog
);
5833 prog
->may_persist
= compute_may_persist(prog
);
5838 void *gpu_prog_free(struct gpu_prog
*prog
)
5842 free_array_info(prog
);
5843 free_stmts(prog
->stmts
, prog
->n_stmts
);
5844 isl_union_map_free(prog
->any_to_outer
);
5845 isl_union_map_free(prog
->to_outer
);
5846 isl_union_map_free(prog
->to_inner
);
5847 isl_union_map_free(prog
->read
);
5848 isl_union_map_free(prog
->may_write
);
5849 isl_union_map_free(prog
->must_write
);
5850 isl_union_map_free(prog
->tagged_must_kill
);
5851 isl_union_map_free(prog
->array_order
);
5852 isl_union_set_free(prog
->may_persist
);
5853 isl_set_free(prog
->context
);