2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 #include <isl/polynomial.h>
18 #include <isl/union_set.h>
22 #include <isl/schedule.h>
23 #include <isl/schedule_node.h>
24 #include <isl/options.h>
25 #include <isl/ast_build.h>
29 #include "gpu_array_tile.h"
30 #include "gpu_group.h"
33 #include "ppcg_options.h"
36 struct gpu_array_info
;
38 /* Collect all references to the given array and store pointers to them
41 * If the array contains structures, then there is no need to collect
42 * the references since we will not be computing any reference groups.
44 static void collect_references(struct gpu_prog
*prog
,
45 struct gpu_array_info
*array
)
50 if (array
->has_compound_element
)
54 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
55 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
56 struct gpu_stmt_access
*access
;
58 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
60 name
= isl_map_get_tuple_name(access
->access
,
62 if (name
&& !strcmp(array
->name
, name
))
68 array
->refs
= isl_alloc_array(prog
->ctx
, struct gpu_stmt_access
*, n
);
72 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
73 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
74 struct gpu_stmt_access
*access
;
76 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
78 name
= isl_map_get_tuple_name(access
->access
,
80 if (!name
|| strcmp(array
->name
, name
))
83 array
->refs
[n
++] = access
;
88 /* Compute and return the extent of "array", taking into account the set of
91 * In particular, the extent in the outer dimension is taken
92 * from "accessed", while the extents in the remaining dimensions
93 * are taken from array->extent.
95 * The extent in the outer dimension cannot be taken from array->extent
96 * because that may be unbounded. Furthermore, even if it is bounded,
97 * it may be larger than the piece of the array that is being accessed.
99 static __isl_give isl_set
*compute_extent(struct pet_array
*array
,
100 __isl_keep isl_set
*accessed
)
107 extent
= isl_set_copy(array
->extent
);
109 n_index
= isl_set_dim(accessed
, isl_dim_set
);
113 extent
= isl_set_project_out(extent
, isl_dim_set
, 0, 1);
114 outer
= isl_set_copy(accessed
);
115 outer
= isl_set_project_out(outer
, isl_dim_set
, 1, n_index
- 1);
116 extent
= isl_set_flat_product(outer
, extent
);
117 id
= isl_set_get_tuple_id(accessed
);
118 extent
= isl_set_set_tuple_id(extent
, id
);
123 /* Is the array "array" being extracted a read-only scalar?
125 * That is, is "array" a scalar that is never possibly written to.
126 * An array containing structures is never considered to be a scalar.
128 static int is_read_only_scalar(struct gpu_array_info
*array
,
129 struct gpu_prog
*prog
)
132 isl_union_map
*write
;
135 if (array
->has_compound_element
)
137 if (array
->n_index
!= 0)
140 write
= isl_union_map_copy(prog
->may_write
);
141 space
= isl_set_universe(isl_space_copy(array
->space
));
142 write
= isl_union_map_intersect_range(write
,
143 isl_union_set_from_set(space
));
144 empty
= isl_union_map_is_empty(write
);
145 isl_union_map_free(write
);
150 /* Compute bounds on the host array "pa" based on the corresponding
151 * accessed elements in "arrays"
152 * and collect all references to the array.
153 * Store the results in "info".
155 * If the array is zero-dimensional and does not contain structures,
156 * i.e., if the array is a scalar, we check whether it is read-only.
157 * We also check whether the array is accessed at all.
159 static int extract_array_info(struct gpu_prog
*prog
,
160 struct gpu_array_info
*info
, struct pet_array
*pa
,
161 __isl_keep isl_union_set
*arrays
)
167 isl_set
*accessed
, *extent
;
169 n_index
= isl_set_dim(pa
->extent
, isl_dim_set
);
170 name
= isl_set_get_tuple_name(pa
->extent
);
171 bounds
= isl_alloc_array(prog
->ctx
, isl_pw_aff
*, n_index
);
175 info
->space
= isl_set_get_space(pa
->extent
);
176 info
->name
= strdup(name
);
177 info
->n_index
= n_index
;
178 info
->bound
= bounds
;
179 info
->linearize
= prog
->scop
->options
->linearize_device_arrays
;
181 info
->type
= strdup(pa
->element_type
);
182 info
->size
= pa
->element_size
;
183 info
->local
= pa
->declared
&& !pa
->exposed
;
184 info
->has_compound_element
= pa
->element_is_record
;
185 info
->read_only_scalar
= is_read_only_scalar(info
, prog
);
187 accessed
= isl_union_set_extract_set(arrays
,
188 isl_space_copy(info
->space
));
189 empty
= isl_set_is_empty(accessed
);
190 extent
= compute_extent(pa
, accessed
);
191 isl_set_free(accessed
);
192 info
->extent
= extent
;
195 info
->accessed
= !empty
;
196 for (i
= 0; i
< n_index
; ++i
) {
202 dom
= isl_set_copy(extent
);
203 dom
= isl_set_project_out(dom
, isl_dim_set
, i
+ 1,
205 dom
= isl_set_project_out(dom
, isl_dim_set
, 0, i
);
206 if (!isl_set_dim_has_upper_bound(dom
, isl_dim_set
, 0)) {
207 fprintf(stderr
, "unable to determine extent of '%s' "
208 "in dimension %d\n", info
->name
, i
);
209 dom
= isl_set_free(dom
);
211 bound
= isl_set_dim_max(dom
, 0);
212 dom
= isl_pw_aff_domain(isl_pw_aff_copy(bound
));
213 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
214 one
= isl_aff_zero_on_domain(ls
);
215 one
= isl_aff_add_constant_si(one
, 1);
216 bound
= isl_pw_aff_add(bound
, isl_pw_aff_alloc(dom
, one
));
217 bound
= isl_pw_aff_gist(bound
, isl_set_copy(prog
->context
));
220 if (!isl_pw_aff_is_cst(bound
))
224 collect_references(prog
, info
);
229 /* Remove independence from the order constraints "order" on array "array".
230 * Since the pairs of iterations in the filter relation of an independence
231 * are guaranteed to be completely independent by the user, there is
232 * no need to ensure that live ranges are ordered along thong pairs.
233 * We make an exception for local variables, though, as the independence
234 * guarantee does not apply to those.
236 * The order constraints are used in two places.
237 * Those on scalars are used in check_scalar_live_ranges to check if
238 * we need to force the scalar to be private. Any non-local scalar
239 * should not be forced scalar if it only appears in independent loops.
240 * Those on non-scalars are added to the coincidence constraints
241 * in compute_schedule because we do not support any array expansion.
242 * Accesses to non-local arrays should not prevent a loop from being
243 * considered coincident so we should indeed remove those constraints
244 * from the order constraints.
246 static __isl_give isl_union_map
*remove_independences(struct gpu_prog
*prog
,
247 struct gpu_array_info
*array
, __isl_take isl_union_map
*order
)
251 for (i
= 0; i
< prog
->scop
->pet
->n_independence
; ++i
) {
252 struct pet_independence
*pi
= prog
->scop
->pet
->independences
[i
];
253 if (isl_union_set_contains(pi
->local
, array
->space
))
256 order
= isl_union_map_subtract(order
,
257 isl_union_map_copy(pi
->filter
));
263 /* For each array in "prog", store the (untagged) order dependences
264 * derived from the array in array->dep_order.
265 * In particular, consider all references that access the given array
266 * and take the order dependences that have one of these references
267 * as source. (Since an order dependence relates two references to
268 * the same array, the target of these order dependences will also
269 * be one of these references.)
270 * Additionally, store the union of these array->dep_order relations
271 * for all non-scalar arrays in prog->array_order.
273 void collect_order_dependences(struct gpu_prog
*prog
)
277 isl_union_map
*accesses
;
279 space
= isl_union_map_get_space(prog
->read
);
280 prog
->array_order
= isl_union_map_empty(space
);
282 accesses
= isl_union_map_copy(prog
->scop
->tagged_reads
);
283 accesses
= isl_union_map_union(accesses
,
284 isl_union_map_copy(prog
->scop
->tagged_may_writes
));
285 accesses
= isl_union_map_universe(accesses
);
286 accesses
= isl_union_map_apply_range(accesses
,
287 isl_union_map_copy(prog
->to_outer
));
289 for (i
= 0; i
< prog
->n_array
; ++i
) {
290 struct gpu_array_info
*array
= &prog
->array
[i
];
293 isl_union_map
*order
;
295 set
= isl_set_universe(isl_space_copy(array
->space
));
296 uset
= isl_union_set_from_set(set
);
297 uset
= isl_union_map_domain(
298 isl_union_map_intersect_range(isl_union_map_copy(accesses
),
300 order
= isl_union_map_copy(prog
->scop
->tagged_dep_order
);
301 order
= isl_union_map_intersect_domain(order
, uset
);
302 order
= isl_union_map_zip(order
);
303 order
= isl_union_set_unwrap(isl_union_map_domain(order
));
304 order
= remove_independences(prog
, array
, order
);
305 array
->dep_order
= order
;
307 if (gpu_array_is_scalar(array
) && !array
->has_compound_element
)
310 prog
->array_order
= isl_union_map_union(prog
->array_order
,
311 isl_union_map_copy(array
->dep_order
));
314 isl_union_map_free(accesses
);
317 /* Construct a gpu_array_info for each array referenced by prog->scop and
318 * collect them in prog->array.
320 * The sizes are based on the extents and the set of possibly accessed
321 * elements by "prog".
322 * If there are any member accesses involved, then they are first mapped
323 * to the outer arrays of structs.
325 * If we are allowing live range reordering, then also set
326 * the dep_order field. Otherwise leave it NULL.
328 static int collect_array_info(struct gpu_prog
*prog
)
332 isl_union_set
*arrays
;
334 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
335 arrays
= isl_union_set_union(arrays
,
336 isl_union_map_range(isl_union_map_copy(prog
->may_write
)));
338 arrays
= isl_union_set_apply(arrays
,
339 isl_union_map_copy(prog
->to_outer
));
341 arrays
= isl_union_set_coalesce(arrays
);
343 prog
->n_array
= prog
->scop
->pet
->n_array
;
344 prog
->array
= isl_calloc_array(prog
->ctx
,
345 struct gpu_array_info
, prog
->n_array
);
347 for (i
= 0; i
< prog
->scop
->pet
->n_array
; ++i
)
348 if (extract_array_info(prog
, &prog
->array
[i
],
349 prog
->scop
->pet
->arrays
[i
], arrays
) < 0)
352 isl_union_set_free(arrays
);
354 if (prog
->scop
->options
->live_range_reordering
)
355 collect_order_dependences(prog
);
360 static void free_array_info(struct gpu_prog
*prog
)
364 for (i
= 0; i
< prog
->n_array
; ++i
) {
365 int n_index
= prog
->array
[i
].n_index
;
366 free(prog
->array
[i
].type
);
367 free(prog
->array
[i
].name
);
368 for (j
= 0; j
< n_index
; ++j
)
369 isl_pw_aff_free(prog
->array
[i
].bound
[j
]);
370 isl_space_free(prog
->array
[i
].space
);
371 isl_set_free(prog
->array
[i
].extent
);
372 free(prog
->array
[i
].bound
);
373 free(prog
->array
[i
].refs
);
374 isl_union_map_free(prog
->array
[i
].dep_order
);
379 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
380 * as an array or through a pointer reference, but as a single data element.
381 * At the moment, scalars are represented as zero-dimensional arrays.
382 * Note that the single data element may be an entire structure.
384 int gpu_array_is_scalar(struct gpu_array_info
*array
)
386 return array
->n_index
== 0;
389 /* Is "array" a read-only scalar?
391 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
393 return array
->read_only_scalar
;
396 /* Return the set of parameter values for which the array has a positive
397 * size in all dimensions.
398 * If the sizes are only valid for some parameter values, then those
399 * constraints are also taken into account.
401 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
)
407 space
= isl_space_params(isl_space_copy(array
->space
));
408 guard
= isl_set_universe(space
);
410 for (i
= 0; i
< array
->n_index
; ++i
) {
412 isl_set
*guard_i
, *zero
;
414 bound
= isl_pw_aff_copy(array
->bound
[i
]);
415 guard_i
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound
));
416 zero
= isl_pw_aff_zero_set(bound
);
417 guard_i
= isl_set_subtract(guard_i
, zero
);
418 guard
= isl_set_intersect(guard
, guard_i
);
424 /* Internal data structure for extract_size_of_type.
425 * "type" specifies the name of the space that we want to extract.
426 * "res" is used to store the subset of that space.
428 struct ppcg_extract_size_data
{
433 /* This function is called for each set in a union_set.
434 * If the name of the set matches data->type, we store the
437 static int extract_size_of_type(__isl_take isl_set
*size
, void *user
)
439 struct ppcg_extract_size_data
*data
= user
;
442 name
= isl_set_get_tuple_name(size
);
443 if (name
&& !strcmp(name
, data
->type
)) {
452 /* Given a union map { kernel[i] -> *[...] },
453 * return the range in the space called "type" for the kernel with
454 * sequence number "id".
456 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
457 const char *type
, int id
)
461 isl_union_set
*local_sizes
;
462 struct ppcg_extract_size_data data
= { type
, NULL
};
467 space
= isl_union_map_get_space(sizes
);
468 space
= isl_space_set_from_params(space
);
469 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
470 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
471 dom
= isl_set_universe(space
);
472 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
474 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
475 isl_union_map_copy(sizes
));
476 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
477 isl_union_set_free(local_sizes
);
481 /* Given a singleton set, extract the first (at most *len) elements
482 * of the single integer tuple into *sizes and update *len if needed.
484 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
492 dim
= isl_set_dim(set
, isl_dim_set
);
496 for (i
= 0; i
< *len
; ++i
) {
499 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
502 sizes
[i
] = isl_val_get_num_si(v
);
509 /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
510 * if the option debug->dump_sizes is set.
512 static void set_used_sizes(struct gpu_gen
*gen
, const char *type
, int id
,
519 if (!gen
->options
->debug
->dump_sizes
)
522 space
= isl_union_map_get_space(gen
->used_sizes
);
523 space
= isl_space_set_from_params(space
);
524 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
525 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
526 space
= isl_space_from_domain(space
);
527 space
= isl_space_add_dims(space
, isl_dim_out
, len
);
528 space
= isl_space_set_tuple_name(space
, isl_dim_out
, type
);
530 map
= isl_map_universe(space
);
531 map
= isl_map_fix_si(map
, isl_dim_in
, 0, id
);
532 for (i
= 0; i
< len
; ++i
)
533 map
= isl_map_fix_si(map
, isl_dim_out
, i
, sizes
[i
]);
535 gen
->used_sizes
= isl_union_map_add_map(gen
->used_sizes
, map
);
538 /* Extract user specified "tile" sizes from the "sizes" command line option,
539 * defaulting to option->tile_size in each dimension.
540 * *tile_len contains the maximum number of tile sizes needed.
541 * Update *tile_len to the number of specified tile sizes, if any, and
542 * return a pointer to the tile sizes (or NULL on error).
543 * Add the effectively used sizes to gen->used_sizes.
545 static int *read_tile_sizes(struct gpu_gen
*gen
, int *tile_len
)
551 tile_size
= isl_alloc_array(gen
->ctx
, int, *tile_len
);
554 for (n
= 0; n
< *tile_len
; ++n
)
555 tile_size
[n
] = gen
->options
->tile_size
;
557 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
558 read_sizes_from_set(size
, tile_size
, tile_len
);
559 set_used_sizes(gen
, "tile", gen
->kernel_id
, tile_size
, *tile_len
);
564 /* Extract user specified "block" sizes from the "sizes" command line option,
565 * after filling in some potentially useful defaults.
567 static void read_block_sizes(struct ppcg_kernel
*kernel
,
568 __isl_keep isl_union_map
*sizes
)
572 if (kernel
->n_block
> 3)
574 switch (kernel
->n_block
) {
576 kernel
->block_dim
[0] = 512;
579 kernel
->block_dim
[0] = 32;
580 kernel
->block_dim
[1] = 16;
583 kernel
->block_dim
[0] = 32;
584 kernel
->block_dim
[1] = 4;
585 kernel
->block_dim
[2] = 4;
589 size
= extract_sizes(sizes
, "block", kernel
->id
);
590 read_sizes_from_set(size
, kernel
->block_dim
, &kernel
->n_block
);
593 /* Extract user specified "grid" sizes from the "sizes" command line option,
594 * after filling in some potentially useful defaults.
596 static void read_grid_sizes(struct ppcg_kernel
*kernel
,
597 __isl_keep isl_union_map
*sizes
)
601 if (kernel
->n_grid
> 2)
603 switch (kernel
->n_grid
) {
605 kernel
->grid_dim
[0] = 32768;
608 kernel
->grid_dim
[0] = 256;
609 kernel
->grid_dim
[1] = 256;
613 size
= extract_sizes(sizes
, "grid", kernel
->id
);
614 read_sizes_from_set(size
, kernel
->grid_dim
, &kernel
->n_grid
);
617 /* Extract user specified grid and block sizes from the gen->sizes
618 * command line option after filling in some potentially useful defaults.
619 * Store the extracted sizes in "kernel".
620 * Add the effectively used sizes to gen->used_sizes.
622 static void read_grid_and_block_sizes(struct ppcg_kernel
*kernel
,
625 read_block_sizes(kernel
, gen
->sizes
);
626 read_grid_sizes(kernel
, gen
->sizes
);
627 set_used_sizes(gen
, "block", kernel
->id
,
628 kernel
->block_dim
, kernel
->n_block
);
629 set_used_sizes(gen
, "grid", kernel
->id
,
630 kernel
->grid_dim
, kernel
->n_grid
);
633 static void *free_stmts(struct gpu_stmt
*stmts
, int n
)
640 for (i
= 0; i
< n
; ++i
) {
641 struct gpu_stmt_access
*access
, *next
;
643 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
645 isl_id_free(access
->ref_id
);
646 isl_map_free(access
->access
);
647 isl_map_free(access
->tagged_access
);
651 isl_id_free(stmts
[i
].id
);
658 /* Construct a map from a domain of dimensionality "len"
659 * to a domain of dimensionality "len" + "tile_len" that tiles
660 * the "tile_len" coordinates starting at "first".
661 * In particular, [s_i] -> [s_i / tile_size[i], s_i % tile_size[i]].
662 * "dim" prescribes the parameters.
664 static __isl_give isl_map
*tile(__isl_take isl_space
*dim
, int len
,
665 int first
, int tile_len
, int *tile_size
)
672 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
673 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
+ tile_len
);
674 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
675 ls
= isl_local_space_from_space(dim
);
677 for (i
= 0; i
< len
- tile_len
; ++i
) {
678 int j
= i
< first
? i
: i
+ tile_len
;
679 int k
= i
< first
? i
: i
+ 2 * tile_len
;
681 c
= isl_equality_alloc(isl_local_space_copy(ls
));
682 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, j
, -1);
683 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, k
, 1);
684 bmap
= isl_basic_map_add_constraint(bmap
, c
);
687 for (i
= 0; i
< tile_len
; ++i
) {
688 c
= isl_equality_alloc(isl_local_space_copy(ls
));
689 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
,
691 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
692 first
+ i
, tile_size
[i
]);
693 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
694 first
+ i
+ tile_len
, 1);
695 bmap
= isl_basic_map_add_constraint(bmap
, c
);
697 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
698 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
699 first
+ i
+ tile_len
, 1);
700 bmap
= isl_basic_map_add_constraint(bmap
, c
);
702 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
703 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
704 first
+ i
+ tile_len
, -1);
705 c
= isl_constraint_set_constant_si(c
, tile_size
[i
] - 1);
706 bmap
= isl_basic_map_add_constraint(bmap
, c
);
709 isl_local_space_free(ls
);
711 return isl_map_from_basic_map(bmap
);
714 /* Construct a map from a domain of dimensionality "len"
715 * to a domain of dimensionality "len" + "wrap_len" that "wraps"
716 * the "wrap_len" coordinates starting at "first" according to "wrap_size".
717 * In particular, [s_i] -> [s_i, s_i % wrap_size[i]].
718 * To do so, we need extra variables corresponding to [s_i / wrap_size[i]],
719 * that are projected out at the end.
720 * "dim" prescribes the parameters.
722 static __isl_give isl_map
*wrap(__isl_take isl_space
*dim
, int len
,
723 int first
, int wrap_len
, int *wrap_size
)
730 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
731 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
+ 2 * wrap_len
);
732 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
733 ls
= isl_local_space_from_space(dim
);
735 for (i
= 0; i
< len
; ++i
) {
736 int k
= i
< first
+ wrap_len
? i
: i
+ 2 * wrap_len
;
738 c
= isl_equality_alloc(isl_local_space_copy(ls
));
739 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, -1);
740 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, k
, 1);
741 bmap
= isl_basic_map_add_constraint(bmap
, c
);
744 for (i
= 0; i
< wrap_len
; ++i
) {
745 c
= isl_equality_alloc(isl_local_space_copy(ls
));
746 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
748 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
749 first
+ wrap_len
+ i
, 1);
750 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
751 first
+ 2 * wrap_len
+ i
, wrap_size
[i
]);
752 bmap
= isl_basic_map_add_constraint(bmap
, c
);
754 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
755 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
756 first
+ wrap_len
+ i
, 1);
757 bmap
= isl_basic_map_add_constraint(bmap
, c
);
759 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
760 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
761 first
+ wrap_len
+ i
, -1);
762 c
= isl_constraint_set_constant_si(c
, wrap_size
[i
] - 1);
763 bmap
= isl_basic_map_add_constraint(bmap
, c
);
766 isl_local_space_free(ls
);
768 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
,
769 first
+ 2 * wrap_len
, wrap_len
);
771 return isl_map_from_basic_map(bmap
);
774 /* Tile the B loops over the tile sizes and then tile/wrap
775 * the T1 loops over the blocks.
777 static __isl_give isl_union_map
*tile_schedule(struct gpu_gen
*gen
,
778 __isl_take isl_union_map
*sched
)
780 struct ppcg_kernel
*kernel
= gen
->kernel
;
782 isl_map
*tiling
, *block_tiling
;
784 dim
= isl_union_map_get_space(sched
);
785 tiling
= tile(isl_space_copy(dim
), gen
->untiled_len
,
786 gen
->tile_first
, kernel
->tile_len
, kernel
->tile_size
);
788 if (gen
->options
->wrap
)
789 block_tiling
= wrap(dim
, gen
->untiled_len
+ kernel
->tile_len
,
790 gen
->tile_first
, kernel
->n_grid
, kernel
->grid_dim
);
792 block_tiling
= tile(dim
, gen
->untiled_len
+ kernel
->tile_len
,
793 gen
->tile_first
, kernel
->n_grid
, kernel
->grid_dim
);
795 gen
->tiled_len
= gen
->untiled_len
+ kernel
->tile_len
+ kernel
->n_grid
;
797 tiling
= isl_map_apply_range(tiling
, block_tiling
);
799 sched
= isl_union_map_apply_range(sched
,
800 isl_union_map_from_map(tiling
));
802 gen
->shared_len
= gen
->tile_first
+ kernel
->tile_len
+ kernel
->n_grid
;
807 /* Equate the "T1P" iterators in the tiled schedule "sched"
808 * to the block dimensions.
810 static __isl_give isl_union_map
*parametrize_tiled_schedule(
811 struct gpu_gen
*gen
, __isl_take isl_union_map
*sched
)
813 struct ppcg_kernel
*kernel
= gen
->kernel
;
817 dim
= isl_union_map_get_space(sched
);
818 par
= parametrization(dim
, gen
->tiled_len
,
819 gen
->tile_first
+ kernel
->n_grid
, kernel
->block_ids
);
820 sched
= isl_union_map_intersect_range(sched
,
821 isl_union_set_from_set(par
));
826 /* Tile/wrap the P1 loops over the threads.
828 static __isl_give isl_union_map
*thread_tile_schedule(struct gpu_gen
*gen
,
829 __isl_take isl_union_map
*sched
)
831 struct ppcg_kernel
*kernel
= gen
->kernel
;
836 dim
= isl_union_map_get_space(sched
);
838 if (gen
->options
->wrap
)
839 tiling
= wrap(isl_space_copy(dim
), gen
->tiled_len
,
840 gen
->shared_len
, kernel
->n_block
, kernel
->block_dim
);
842 tiling
= tile(isl_space_copy(dim
), gen
->tiled_len
,
843 gen
->shared_len
, kernel
->n_block
, kernel
->block_dim
);
844 gen
->thread_tiled_len
= gen
->tiled_len
+ kernel
->n_block
;
846 sched
= isl_union_map_apply_range(sched
,
847 isl_union_map_from_map(tiling
));
849 par
= parametrization(dim
, gen
->thread_tiled_len
,
850 gen
->tile_first
+ kernel
->tile_len
+
851 kernel
->n_grid
+ kernel
->n_block
, kernel
->thread_ids
);
852 sched
= isl_union_map_intersect_range(sched
,
853 isl_union_set_from_set(par
));
855 gen
->shared_len
= gen
->tile_first
+ kernel
->tile_len
+ kernel
->n_grid
;
860 /* If the user asked for it, scale the shared memory tile loops
861 * (T1T and T2) of "sched" by kernel->tile_size[i].
862 * If we are not performing "wrapping", then additionally scale the T1P
863 * loops by kernel->grid_dim[i].
865 static __isl_give isl_union_map
*scale_tile_loops(struct gpu_gen
*gen
,
866 __isl_take isl_union_map
*sched
)
868 struct ppcg_kernel
*kernel
= gen
->kernel
;
871 isl_basic_map
*scale
;
875 if (!gen
->options
->scale_tile_loops
)
878 dim
= isl_union_map_get_space(sched
);
879 dim
= isl_space_add_dims(dim
, isl_dim_in
, gen
->tiled_len
);
880 dim
= isl_space_add_dims(dim
, isl_dim_out
, gen
->tiled_len
);
881 scale
= isl_basic_map_universe(isl_space_copy(dim
));
882 ls
= isl_local_space_from_space(dim
);
884 for (i
= 0; i
< gen
->tiled_len
; ++i
) {
887 if (i
>= gen
->tile_first
&&
888 i
< gen
->tile_first
+ kernel
->n_grid
) {
889 f
= kernel
->tile_size
[i
- gen
->tile_first
];
890 if (!gen
->options
->wrap
)
891 f
*= kernel
->grid_dim
[i
- gen
->tile_first
];
892 } else if (i
>= gen
->tile_first
+ kernel
->n_grid
&&
893 i
< gen
->tile_first
+ kernel
->n_grid
+
895 f
= kernel
->tile_size
[i
-
896 (gen
->tile_first
+ kernel
->n_grid
)];
899 c
= isl_equality_alloc(isl_local_space_copy(ls
));
900 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
901 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
902 scale
= isl_basic_map_add_constraint(scale
, c
);
905 isl_local_space_free(ls
);
907 sched
= isl_union_map_apply_range(sched
,
908 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
913 /* If we are not performing "wrapping" and if the user asked for it,
914 * scale the thread tile loops (P1T) of "sched" by kernel->block_dim[i].
916 static __isl_give isl_union_map
*scale_thread_tile_loops(struct gpu_gen
*gen
,
917 __isl_take isl_union_map
*sched
)
921 isl_basic_map
*scale
;
925 if (gen
->options
->wrap
)
927 if (!gen
->options
->scale_tile_loops
)
930 dim
= isl_union_map_get_space(sched
);
931 dim
= isl_space_add_dims(dim
, isl_dim_in
, gen
->thread_tiled_len
);
932 dim
= isl_space_add_dims(dim
, isl_dim_out
, gen
->thread_tiled_len
);
933 scale
= isl_basic_map_universe(isl_space_copy(dim
));
934 ls
= isl_local_space_from_space(dim
);
936 for (i
= 0; i
< gen
->thread_tiled_len
; ++i
) {
939 if (i
>= gen
->shared_len
&&
940 i
< gen
->shared_len
+ gen
->kernel
->n_block
)
941 f
= gen
->kernel
->block_dim
[i
- gen
->shared_len
];
943 c
= isl_equality_alloc(isl_local_space_copy(ls
));
944 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
945 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
946 scale
= isl_basic_map_add_constraint(scale
, c
);
949 isl_local_space_free(ls
);
951 sched
= isl_union_map_apply_range(sched
,
952 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
957 /* If we are not performing "wrapping" and if the user asked for it,
958 * scale the "n_tile" loops starting at "first" of "sched" by gen->block_dim[i].
960 static __isl_give isl_union_map
*scale_access_tile_loops(struct gpu_gen
*gen
,
961 __isl_take isl_union_map
*sched
, int len
, int first
, int n_tile
)
965 isl_basic_map
*scale
;
969 if (gen
->options
->wrap
)
971 if (!gen
->options
->scale_tile_loops
)
974 dim
= isl_union_map_get_space(sched
);
975 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
976 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
);
977 scale
= isl_basic_map_universe(isl_space_copy(dim
));
978 ls
= isl_local_space_from_space(dim
);
980 for (i
= 0; i
< len
; ++i
) {
983 if (i
>= first
&& i
< first
+ n_tile
)
984 f
= gen
->kernel
->block_dim
[i
- first
];
986 c
= isl_equality_alloc(isl_local_space_copy(ls
));
987 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
988 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
989 scale
= isl_basic_map_add_constraint(scale
, c
);
992 isl_local_space_free(ls
);
994 sched
= isl_union_map_apply_range(sched
,
995 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
1000 /* Add parameters p[i] with identifiers "ids" to "set",
1001 * with bounds to 0 <= p[i] < size[i].
1003 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
1004 int *size
, __isl_keep isl_id_list
*ids
)
1009 len
= isl_id_list_n_id(ids
);
1010 nparam
= isl_set_dim(set
, isl_dim_param
);
1011 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
1013 for (i
= 0; i
< len
; ++i
) {
1016 id
= isl_id_list_get_id(ids
, i
);
1017 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
1018 set
= isl_set_lower_bound_si(set
, isl_dim_param
, nparam
+ i
, 0);
1019 set
= isl_set_upper_bound_si(set
, isl_dim_param
,
1020 nparam
+ i
, size
[i
] - 1);
1026 /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
1029 * { : 0 <= p[i] < size[i] }
1031 * or an overapproximation.
1033 static __isl_give isl_set
*add_bounded_parameters_dynamic(
1034 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
1035 __isl_keep isl_id_list
*ids
)
1040 isl_local_space
*ls
;
1042 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
1043 nparam
= isl_set_dim(set
, isl_dim_param
);
1044 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
1046 for (i
= 0; i
< len
; ++i
) {
1049 id
= isl_id_list_get_id(ids
, i
);
1050 set
= isl_set_set_dim_id(set
, isl_dim_param
, nparam
+ i
, id
);
1053 space
= isl_space_params(isl_set_get_space(set
));
1054 ls
= isl_local_space_from_space(space
);
1055 for (i
= 0; i
< len
; ++i
) {
1056 isl_pw_aff
*param
, *size_i
, *zero
;
1059 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
1060 isl_dim_param
, nparam
+ i
);
1062 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
1063 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
1064 bound
= isl_set_from_basic_set(isl_set_simple_hull(bound
));
1065 set
= isl_set_intersect_params(set
, bound
);
1067 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
1068 bound
= isl_pw_aff_ge_set(param
, zero
);
1069 set
= isl_set_intersect_params(set
, bound
);
1071 isl_local_space_free(ls
);
1076 /* Construct a map from an access to group->array to the corresponding
1077 * shared/private memory tile.
1078 * The map is of the form
1080 * { [D[i] -> A[a]] -> T[t] }
1082 * where D represents the initial shared_len dimensions
1083 * of the computed schedule.
1085 static __isl_give isl_map
*shift_access(struct gpu_array_ref_group
*group
)
1087 struct gpu_array_tile
*tile
;
1088 isl_multi_aff
*tiling
;
1090 tile
= group
->private_tile
;
1092 tile
= group
->shared_tile
;
1094 tiling
= isl_multi_aff_copy(tile
->tiling
);
1096 return isl_map_from_multi_aff(tiling
);
1099 /* Given a schedule that iterates over all elements in a piece of an array,
1100 * perform tiling/wrapping over the threads.
1102 * In particular, we tile the final iterators so that the final thread
1103 * dimension runs over the final array dimension.
1104 * However, if those final iterators have only a single iteration,
1105 * we try to tile earlier iterators instead.
1107 static __isl_give isl_map
*tile_access_schedule(struct gpu_gen
*gen
,
1108 __isl_take isl_map
*sched
)
1111 isl_union_map
*usched
;
1114 unsigned nvar
= isl_map_dim(sched
, isl_dim_out
);
1118 n_tile
= gen
->kernel
->n_block
;
1119 if (n_tile
> nvar
) {
1121 sched
= isl_map_insert_dims(sched
,
1122 isl_dim_out
, 0, n_tile
- nvar
);
1123 for (i
= 0; i
< n_tile
- nvar
; ++i
)
1124 sched
= isl_map_fix_si(sched
, isl_dim_out
, i
, 0);
1128 first
= nvar
- n_tile
;
1130 for (; first
> 0; first
--)
1131 if (!map_plain_is_fixed(sched
, isl_dim_out
, first
+ n_tile
- 1))
1134 dim
= isl_map_get_space(sched
);
1135 dim
= isl_space_params(dim
);
1136 if (gen
->options
->wrap
)
1137 tiling
= wrap(isl_space_copy(dim
), nvar
, first
,
1138 n_tile
, gen
->kernel
->block_dim
);
1140 tiling
= tile(isl_space_copy(dim
), nvar
, first
,
1141 n_tile
, gen
->kernel
->block_dim
);
1142 sched
= isl_map_apply_range(sched
, tiling
);
1144 par
= parametrization(dim
, nvar
+ n_tile
, first
+ n_tile
,
1145 gen
->kernel
->thread_ids
);
1146 sched
= isl_map_intersect_range(sched
, par
);
1148 usched
= isl_union_map_from_map(sched
);
1149 usched
= scale_access_tile_loops(gen
, usched
, nvar
+ n_tile
,
1151 sched
= isl_map_from_union_map(usched
);
1156 /* Return the union of all tagged access relations in the group.
1158 static __isl_give isl_union_map
*group_tagged_access_relation(
1159 struct gpu_array_ref_group
*group
)
1162 isl_union_map
*access
;
1164 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
1165 for (i
= 0; i
< group
->n_ref
; ++i
) {
1168 map_i
= isl_map_copy(group
->refs
[i
]->tagged_access
);
1169 access
= isl_union_map_union(access
,
1170 isl_union_map_from_map(map_i
));
1176 /* Return the extent of "array", recomputed from the bounds.
1177 * The recomputed extent may be simpler than the original extent.
1179 static __isl_give isl_set
*array_extent(struct gpu_array_info
*array
)
1184 isl_local_space
*ls
;
1187 id
= isl_set_get_tuple_id(array
->extent
);
1188 space
= isl_set_get_space(array
->extent
);
1189 extent
= isl_set_universe(isl_space_copy(space
));
1190 ls
= isl_local_space_from_space(space
);
1191 for (i
= 0; i
< array
->n_index
; ++i
) {
1197 extent
= isl_set_lower_bound_si(extent
, isl_dim_set
, i
, 0);
1199 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
1201 index
= isl_pw_aff_from_aff(aff
);
1202 bound
= isl_pw_aff_copy(array
->bound
[i
]);
1203 bound
= isl_pw_aff_from_range(bound
);
1204 bound
= isl_pw_aff_add_dims(bound
, isl_dim_in
, array
->n_index
);
1205 bound
= isl_pw_aff_set_tuple_id(bound
, isl_dim_in
,
1207 lt
= isl_pw_aff_lt_set(index
, bound
);
1208 extent
= isl_set_intersect(extent
, lt
);
1210 isl_local_space_free(ls
);
1216 /* Return a map from the first group->depth dimensions of the computed
1217 * schedule to the array tile in
1218 * global memory that corresponds to the shared memory copy.
1220 * In particular, return a map
1226 * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
1230 * 0 <= a <= array_size - 1 (2)
1232 * Note that if some stride has been detected (i.e., when
1233 * group->shared_tile->bound[i].shift is set), then a in (1) refers
1234 * to the shifted and scaled down version.
1236 * Constraints (1) are obtained by mapping the size constraints on the
1237 * shared/private memory tile back to the access relation.
1238 * Constraints (2) are obtained from the (recomputed) extent.
1240 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
1243 int n_index
= group
->array
->n_index
;
1249 space
= isl_multi_aff_get_space(group
->shared_tile
->tiling
);
1250 space
= isl_space_range(space
);
1251 local
= isl_set_universe(space
);
1252 for (i
= 0; i
< n_index
; ++i
) {
1255 local
= isl_set_lower_bound_si(local
, isl_dim_set
, i
, 0);
1256 bound
= isl_val_copy(group
->shared_tile
->bound
[i
].size
);
1257 bound
= isl_val_sub_ui(bound
, 1);
1258 local
= isl_set_upper_bound_val(local
, isl_dim_set
, i
, bound
);
1260 local
= isl_set_preimage_multi_aff(local
,
1261 isl_multi_aff_copy(group
->shared_tile
->tiling
));
1262 tile
= isl_set_unwrap(local
);
1263 extent
= array_extent(group
->array
);
1264 tile
= isl_map_intersect_range(tile
, extent
);
1269 /* Given a mapping "iterator_map" from the AST schedule to a domain,
1270 * return the corresponding mapping from the AST schedule to
1271 * to the outer kernel->shared_schedule_dim dimensions of
1272 * the schedule computed by PPCG for this kernel.
1274 * Note that kernel->shared_schedule_dim is at least as large as
1275 * the largest depth of any array reference group associated to the kernel.
1276 * This is needed as the returned schedule is used to extract a mapping
1277 * to the outer group->depth dimensions in transform_index.
1279 static __isl_give isl_pw_multi_aff
*compute_sched_to_shared(
1280 struct ppcg_kernel
*kernel
, __isl_take isl_pw_multi_aff
*iterator_map
)
1282 isl_union_pw_multi_aff
*upma
;
1283 isl_pw_multi_aff
*pma
;
1286 space
= isl_space_range(isl_pw_multi_aff_get_space(iterator_map
));
1287 space
= isl_space_from_domain(space
);
1288 space
= isl_space_add_dims(space
, isl_dim_out
,
1289 kernel
->shared_schedule_dim
);
1291 upma
= isl_union_pw_multi_aff_copy(kernel
->shared_schedule
);
1292 pma
= isl_union_pw_multi_aff_extract_pw_multi_aff(upma
, space
);
1293 isl_union_pw_multi_aff_free(upma
);
1295 return isl_pw_multi_aff_pullback_pw_multi_aff(pma
, iterator_map
);
1298 /* Set unroll[j] if the input dimension j is involved in
1299 * the index expression represented by ma.
1301 static int check_unroll(__isl_take isl_set
*set
, __isl_take isl_multi_aff
*ma
,
1305 int n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
1306 int n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
1309 for (i
= 0; i
< n_out
; ++i
) {
1312 aff
= isl_multi_aff_get_aff(ma
, i
);
1313 for (j
= 0; j
< n_in
; ++j
)
1314 if (isl_aff_involves_dims(aff
, isl_dim_in
, j
, 1))
1320 isl_multi_aff_free(ma
);
1324 /* Given an array pos mapping input dimensions to the corresponding
1325 * output dimension, construct the corresponding map.
1327 static __isl_give isl_map
*permutation(__isl_take isl_space
*dim
,
1332 isl_basic_map
*bmap
;
1333 isl_local_space
*ls
;
1335 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
1336 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
);
1337 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
1338 ls
= isl_local_space_from_space(dim
);
1340 for (i
= 0; i
< len
; ++i
) {
1341 c
= isl_equality_alloc(isl_local_space_copy(ls
));
1342 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
,
1344 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, pos
[i
],
1346 bmap
= isl_basic_map_add_constraint(bmap
, c
);
1348 isl_local_space_free(ls
);
1350 return isl_map_from_basic_map(bmap
);
1353 /* Find all loops involved in any of the index expressions for any of
1354 * the private accesses that require unrolling, move them innermost
1355 * and then mark them as requiring unrolling by setting gen->first_unroll.
1356 * The loops involved should all be parallel because of the checks
1357 * we performed in check_private_group_access. Moving them innermost
1358 * is therefore a valid transformation.
1360 * Loops up to gen->shared_len are generated before the mapping to
1361 * threads is applied. They should therefore be ignored.
1363 * We compute the hidden equalities of the schedule first
1364 * since we will need them in our calls to isl_pw_multi_aff_from_map
1365 * and because we want to make sure that the same equalities
1366 * are also available to the code generator.
1368 static __isl_give isl_union_map
*interchange_for_unroll(struct gpu_gen
*gen
,
1369 __isl_take isl_union_map
*sched
)
1371 struct ppcg_kernel
*kernel
= gen
->kernel
;
1373 int unroll
[gen
->thread_tiled_len
];
1374 int perm
[gen
->thread_tiled_len
];
1377 int len
= gen
->shared_len
+ kernel
->n_parallel
+ kernel
->n_block
;
1379 gen
->first_unroll
= -1;
1381 sched
= isl_union_map_detect_equalities(sched
);
1382 for (i
= 0; i
< gen
->thread_tiled_len
; ++i
)
1384 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1385 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1387 for (j
= 0; j
< array
->n_group
; ++j
) {
1388 isl_union_map
*access
;
1390 isl_pw_multi_aff
*pma
;
1391 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1393 if (!array
->groups
[j
]->private_tile
)
1395 if (!gpu_array_ref_group_requires_unroll(group
))
1398 access
= gpu_array_ref_group_access_relation(
1399 array
->groups
[j
], 1, 1);
1400 access
= isl_union_map_apply_domain(access
,
1401 isl_union_map_copy(sched
));
1403 acc
= isl_map_from_union_map(access
);
1404 pma
= isl_pw_multi_aff_from_map(acc
);
1405 isl_pw_multi_aff_foreach_piece(pma
,
1406 &check_unroll
, unroll
);
1408 isl_pw_multi_aff_free(pma
);
1412 for (i
= gen
->shared_len
; i
< len
; ++i
)
1419 for (i
= len
; i
< gen
->thread_tiled_len
; ++i
)
1424 for (i
= 0; i
< gen
->shared_len
; ++i
)
1426 for (i
= gen
->shared_len
; i
< gen
->thread_tiled_len
; ++i
)
1429 gen
->first_unroll
= j
- gen
->shared_len
;
1430 for (i
= gen
->shared_len
; i
< len
; ++i
)
1434 dim
= isl_union_map_get_space(sched
);
1435 permute
= permutation(dim
, perm
, gen
->thread_tiled_len
);
1436 sched
= isl_union_map_apply_range(sched
,
1437 isl_union_map_from_map(permute
));
1442 /* Construct a map with input the shared tile loops and the loops that
1443 * will be wrapped around the threads that relates these later loops
1444 * to the thread indices and then projects them out.
1446 static __isl_give isl_map
*compute_privatization(struct gpu_gen
*gen
)
1448 struct ppcg_kernel
*kernel
= gen
->kernel
;
1455 dim
= isl_union_map_get_space(gen
->shared_sched
);
1457 if (gen
->options
->wrap
)
1458 tiling
= wrap(isl_space_copy(dim
),
1459 gen
->shared_len
+ kernel
->n_block
,
1460 gen
->shared_len
, kernel
->n_block
, kernel
->block_dim
);
1462 tiling
= tile(isl_space_copy(dim
),
1463 gen
->shared_len
+ kernel
->n_block
,
1464 gen
->shared_len
, kernel
->n_block
, kernel
->block_dim
);
1468 par
= parametrization(dim
, gen
->shared_len
+ 2 * kernel
->n_block
,
1469 gen
->tile_first
+ kernel
->tile_len
+
1470 kernel
->n_grid
+ kernel
->n_block
, kernel
->thread_ids
);
1472 priv
= isl_map_align_params(priv
, isl_set_get_space(par
));
1473 priv
= isl_map_intersect_range(priv
, par
);
1475 dim
= isl_map_get_space(priv
);
1476 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
1477 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
1478 proj
= projection(dim
, gen
->shared_len
+ 2 * kernel
->n_block
,
1481 priv
= isl_map_apply_range(priv
, proj
);
1486 /* If max_shared_memory is not set to infinity (-1), then make
1487 * sure that the total amount of shared memory required by the
1488 * array reference groups mapped to shared memory by "kernel"
1489 * is no larger than this maximum.
1491 * We apply a greedy approach and discard (keep in global memory)
1492 * those groups that would result in a total memory size that
1493 * is larger than the maximum.
1495 * This function should be called after any function that may
1496 * affect the decision on whether to place a reference group
1497 * in private, shared or global memory.
1499 static void check_shared_memory_bound(struct ppcg_kernel
*kernel
)
1502 isl_val
*left
, *size
;
1504 if (kernel
->options
->max_shared_memory
< 0)
1507 left
= isl_val_int_from_si(kernel
->ctx
,
1508 kernel
->options
->max_shared_memory
);
1510 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1511 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1513 for (j
= 0; j
< local
->n_group
; ++j
) {
1514 struct gpu_array_ref_group
*group
;
1516 group
= local
->groups
[j
];
1517 if (group
->private_tile
)
1519 if (!group
->shared_tile
)
1522 size
= gpu_array_tile_size(group
->shared_tile
);
1523 size
= isl_val_mul_ui(size
, local
->array
->size
);
1525 if (isl_val_le(size
, left
)) {
1526 left
= isl_val_sub(left
, size
);
1531 group
->shared_tile
=
1532 gpu_array_tile_free(group
->shared_tile
);
1539 /* Compute a tiling for all the array reference groups in "kernel".
1541 static void compute_group_tilings(struct ppcg_kernel
*kernel
)
1545 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1546 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1548 for (j
= 0; j
< array
->n_group
; ++j
)
1549 gpu_array_ref_group_compute_tiling(array
->groups
[j
]);
1553 /* Take tiled_sched, project it onto the shared tile loops and
1554 * the loops that will be wrapped over the threads and
1555 * store the result in gen->shared_sched.
1556 * Also compute a projection that projects out the loops that will be
1557 * wrapped over the threads and store this projection in gen->shared_proj.
1559 static void compute_shared_sched(struct gpu_gen
*gen
)
1564 isl_union_map
*sched
;
1566 sched
= isl_union_map_copy(gen
->tiled_sched
);
1568 dim
= isl_union_map_get_space(sched
);
1569 proj
= projection(dim
, gen
->tiled_len
,
1570 gen
->shared_len
+ gen
->kernel
->n_block
);
1571 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
1573 dim
= isl_union_map_get_space(sched
);
1574 proj
= projection(dim
, gen
->shared_len
+ gen
->kernel
->n_block
,
1577 gen
->shared_sched
= sched
;
1578 gen
->shared_proj
= isl_union_map_from_map(proj
);
1581 /* Compute the size of a bounding box around the origin and "set",
1582 * where "set" is assumed to contain only non-negative elements.
1583 * In particular, compute the maximal value of "set" in each direction
1586 static __isl_give isl_multi_pw_aff
*extract_size(__isl_take isl_set
*set
,
1587 __isl_take isl_set
*context
)
1590 isl_multi_pw_aff
*mpa
;
1592 context
= isl_set_params(context
);
1593 n
= isl_set_dim(set
, isl_dim_set
);
1594 mpa
= isl_multi_pw_aff_zero(isl_set_get_space(set
));
1595 for (i
= 0; i
< n
; ++i
) {
1600 bound
= isl_set_dim_max(isl_set_copy(set
), i
);
1601 bound
= isl_pw_aff_coalesce(bound
);
1602 bound
= isl_pw_aff_gist(bound
, isl_set_copy(context
));
1604 space
= isl_pw_aff_get_domain_space(bound
);
1605 one
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1606 one
= isl_aff_add_constant_si(one
, 1);
1607 bound
= isl_pw_aff_add(bound
, isl_pw_aff_from_aff(one
));
1608 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, bound
);
1611 isl_set_free(context
);
1616 /* Compute the effective grid size as a list of the sizes in each dimension.
1618 * The grid size specified by the user or set by default
1619 * in read_grid_sizes() and applied by the block filter,
1620 * may be too large for the given code in the sense that
1621 * it may contain blocks that don't need to execute anything.
1622 * We therefore don't return this grid size, but instead the
1623 * smallest grid size that ensures that all blocks that actually
1624 * execute code are included in the grid.
1626 * We first extract a description of the grid, i.e., the possible values
1627 * of the block ids, from the domain elements in "domain" and
1628 * kernel->block_filter.
1629 * The block ids are parameters in kernel->block_filter.
1630 * We simply need to change them into set dimensions.
1632 * Then, for each block dimension, we compute the maximal value of the block id
1635 static __isl_give isl_multi_pw_aff
*extract_grid_size(
1636 struct ppcg_kernel
*kernel
, __isl_take isl_union_set
*domain
)
1641 domain
= isl_union_set_intersect(domain
,
1642 isl_union_set_copy(kernel
->block_filter
));
1643 grid
= isl_union_set_params(domain
);
1644 grid
= isl_set_from_params(grid
);
1645 grid
= isl_set_add_dims(grid
, isl_dim_set
, kernel
->n_grid
);
1646 for (i
= 0; i
< kernel
->n_grid
; ++i
) {
1650 id
= isl_id_list_get_id(kernel
->block_ids
, i
);
1651 pos
= isl_set_find_dim_by_id(grid
, isl_dim_param
, id
);
1654 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
1655 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
1658 return extract_size(grid
, isl_set_copy(kernel
->context
));
1661 /* Compute the size of a fixed bounding box around the origin and "set",
1662 * where "set" is assumed to contain only non-negative elements,
1663 * and store the results in "size".
1664 * In particular, compute the maximal value of "set" in each direction
1667 static void extract_fixed_size(__isl_take isl_set
*set
, int *size
)
1670 isl_local_space
*ls
;
1673 n
= isl_set_dim(set
, isl_dim_set
);
1674 ls
= isl_local_space_from_space(isl_set_get_space(set
));
1675 obj
= isl_aff_zero_on_domain(ls
);
1676 for (i
= 0; i
< n
; ++i
) {
1679 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 1);
1680 max
= isl_set_max_val(set
, obj
);
1681 size
[i
] = isl_val_get_num_si(max
) + 1;
1683 obj
= isl_aff_set_coefficient_si(obj
, isl_dim_in
, i
, 0);
1689 /* Compute the effective block size as a list of the sizes in each dimension
1690 * and store the sizes in kernel->block_dim.
1692 * The block size specified by the user or set by default
1693 * in read_block_sizes() and applied by the thread filter,
1694 * may be too large for the given code in the sense that
1695 * it may contain threads that don't need to execute anything.
1696 * We therefore update this block size in kernel->block_dim
1697 * to the smallest block size that ensures that all threads
1698 * that actually execute code are included in the block.
1700 * The possible values of the thread ids is obtained from
1701 * the domain elements "domain" and kernel->thread_filter.
1702 * The current implementation eliminates all parameters, ensuring
1703 * that the size is a fixed constant in each dimension.
1704 * In principle we could also compute parametric sizes.
1705 * We would have to make sure to project out all b%d and t%d parameters,
1708 static void extract_block_size(struct ppcg_kernel
*kernel
,
1709 __isl_take isl_union_set
*domain
)
1715 domain
= isl_union_set_intersect(domain
,
1716 isl_union_set_copy(kernel
->thread_filter
));
1717 block
= isl_union_set_params(domain
);
1718 block
= isl_set_from_params(block
);
1719 block
= isl_set_add_dims(block
, isl_dim_set
, kernel
->n_block
);
1720 for (i
= 0; i
< kernel
->n_block
; ++i
) {
1724 id
= isl_id_list_get_id(kernel
->thread_ids
, i
);
1725 pos
= isl_set_find_dim_by_id(block
, isl_dim_param
, id
);
1728 block
= isl_set_equate(block
, isl_dim_param
, pos
,
1731 nparam
= isl_set_dim(block
, isl_dim_param
);
1732 block
= isl_set_project_out(block
, isl_dim_param
, 0, nparam
);
1734 extract_fixed_size(block
, kernel
->block_dim
);
1737 struct ppcg_kernel
*ppcg_kernel_free(struct ppcg_kernel
*kernel
)
1744 isl_id_list_free(kernel
->block_ids
);
1745 isl_id_list_free(kernel
->thread_ids
);
1746 isl_multi_pw_aff_free(kernel
->grid_size
);
1747 isl_set_free(kernel
->context
);
1748 isl_union_set_free(kernel
->core
);
1749 isl_union_set_free(kernel
->arrays
);
1750 isl_space_free(kernel
->space
);
1751 isl_ast_node_free(kernel
->tree
);
1752 isl_union_set_free(kernel
->block_filter
);
1753 isl_union_set_free(kernel
->thread_filter
);
1754 isl_union_pw_multi_aff_free(kernel
->shared_schedule
);
1756 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1757 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1759 for (j
= 0; j
< array
->n_group
; ++j
)
1760 gpu_array_ref_group_free(array
->groups
[j
]);
1761 free(array
->groups
);
1763 isl_pw_aff_list_free(array
->bound
);
1765 free(kernel
->array
);
1767 for (i
= 0; i
< kernel
->n_var
; ++i
) {
1768 free(kernel
->var
[i
].name
);
1769 isl_vec_free(kernel
->var
[i
].size
);
1772 free(kernel
->tile_size
);
1779 /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
1781 static void ppcg_kernel_free_wrap(void *user
)
1783 struct ppcg_kernel
*kernel
= user
;
1785 ppcg_kernel_free(kernel
);
1788 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
1789 struct ppcg_kernel_var
*var
)
1792 struct gpu_array_tile
*tile
;
1796 var
->array
= group
->array
;
1798 tile
= group
->private_tile
;
1799 var
->type
= ppcg_access_private
;
1801 tile
= group
->shared_tile
;
1802 var
->type
= ppcg_access_shared
;
1805 p
= isl_printer_to_str(ctx
);
1806 p
= gpu_array_ref_group_print_name(group
, p
);
1807 var
->name
= isl_printer_get_str(p
);
1808 isl_printer_free(p
);
1810 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
1812 for (j
= 0; j
< group
->array
->n_index
; ++j
)
1813 var
->size
= isl_vec_set_element_val(var
->size
, j
,
1814 isl_val_copy(tile
->bound
[j
].size
));
1817 static void create_kernel_vars(struct ppcg_kernel
*kernel
)
1822 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1823 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1825 for (j
= 0; j
< array
->n_group
; ++j
) {
1826 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1827 if (group
->private_tile
|| group
->shared_tile
)
1833 kernel
->var
= isl_calloc_array(kernel
->ctx
, struct ppcg_kernel_var
, n
);
1834 assert(kernel
->var
);
1837 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1838 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
1840 for (j
= 0; j
< array
->n_group
; ++j
) {
1841 struct gpu_array_ref_group
*group
= array
->groups
[j
];
1842 if (!group
->private_tile
&& !group
->shared_tile
)
1844 create_kernel_var(kernel
->ctx
, group
, &kernel
->var
[n
]);
1850 /* Replace "pa" by the zero function defined over the universe domain
1851 * in the space of "pa".
1853 static __isl_give isl_pw_aff
*set_universally_zero(__isl_take isl_pw_aff
*pa
)
1858 space
= isl_space_domain(isl_pw_aff_get_space(pa
));
1859 isl_pw_aff_free(pa
);
1860 zero
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1862 return isl_pw_aff_from_aff(zero
);
1865 /* The sizes of the arrays on the host that have been computed by
1866 * extract_array_info may depend on the parameters. Use the extra
1867 * constraints on the parameters that are valid at "host_domain"
1868 * to simplify these expressions and store the results in kernel->array.
1870 * We only need these localized bounds for arrays that are accessed
1871 * by the current kernel. If we have found at least one reference group
1872 * then the array is accessed by the kernel. If the array has compound
1873 * elements then we skipped the construction of array reference groups.
1875 * The resulting sizes may be functions that are nowhere defined
1876 * in case the access function cannot possibly access anything inside
1877 * the kernel for some reason. If so, they are replaced by the zero
1878 * function. Since the access function cannot actually access anything,
1879 * there is no harm in printing the array sizes as zero.
1881 static void localize_bounds(struct ppcg_kernel
*kernel
,
1882 __isl_keep isl_set
*host_domain
)
1887 context
= isl_set_copy(host_domain
);
1888 context
= isl_set_params(context
);
1890 for (i
= 0; i
< kernel
->n_array
; ++i
) {
1891 struct gpu_local_array_info
*local
= &kernel
->array
[i
];
1892 isl_pw_aff_list
*bound
;
1895 if (local
->n_group
== 0 && !local
->array
->has_compound_element
)
1898 n_index
= local
->array
->n_index
;
1899 bound
= isl_pw_aff_list_alloc(kernel
->ctx
, n_index
);
1901 for (j
= 0; j
< n_index
; ++j
) {
1905 pwaff
= isl_pw_aff_copy(local
->array
->bound
[j
]);
1906 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
1907 empty
= isl_pw_aff_is_empty(pwaff
);
1909 pwaff
= isl_pw_aff_free(pwaff
);
1911 pwaff
= set_universally_zero(pwaff
);
1912 bound
= isl_pw_aff_list_add(bound
, pwaff
);
1915 local
->n_index
= n_index
;
1916 local
->bound
= bound
;
1918 isl_set_free(context
);
1921 /* Create the array of gpu_local_array_info structures "array"
1922 * inside "kernel". The number of elements in this array is
1923 * the same as the number of arrays in "prog".
1924 * Initialize the "array" field of each local array to point
1925 * to the corresponding array in "prog".
1927 static struct ppcg_kernel
*ppcg_kernel_create_local_arrays(
1928 struct ppcg_kernel
*kernel
, struct gpu_prog
*prog
)
1933 ctx
= isl_set_get_ctx(prog
->context
);
1934 kernel
->array
= isl_calloc_array(ctx
,
1935 struct gpu_local_array_info
, prog
->n_array
);
1937 return ppcg_kernel_free(kernel
);
1938 kernel
->n_array
= prog
->n_array
;
1940 for (i
= 0; i
< prog
->n_array
; ++i
)
1941 kernel
->array
[i
].array
= &prog
->array
[i
];
1946 /* Find the element in gen->stmt that has the given "id".
1947 * Return NULL if no such gpu_stmt can be found.
1949 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
1953 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
1954 if (id
== prog
->stmts
[i
].id
)
1958 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
1961 void ppcg_kernel_stmt_free(void *user
)
1964 struct ppcg_kernel_stmt
*stmt
= user
;
1969 switch (stmt
->type
) {
1970 case ppcg_kernel_copy
:
1971 isl_ast_expr_free(stmt
->u
.c
.index
);
1972 isl_ast_expr_free(stmt
->u
.c
.local_index
);
1974 case ppcg_kernel_domain
:
1975 isl_id_to_ast_expr_free(stmt
->u
.d
.ref2expr
);
1977 case ppcg_kernel_sync
:
1984 /* Return the gpu_stmt_access in the list "accesses" that corresponds
1987 static struct gpu_stmt_access
*find_access(struct gpu_stmt_access
*accesses
,
1988 __isl_keep isl_id
*ref_id
)
1990 struct gpu_stmt_access
*access
;
1992 for (access
= accesses
; access
; access
= access
->next
)
1993 if (access
->ref_id
== ref_id
)
1999 /* Return the index of the array called "name" in the list of arrays.
2001 static int find_array_index(struct ppcg_kernel
*kernel
, const char *name
)
2005 for (i
= 0; i
< kernel
->n_array
; ++i
)
2006 if (!strcmp(name
, kernel
->array
[i
].array
->name
))
2012 /* Internal data structure for the index and AST expression transformation
2013 * callbacks for pet_stmt_build_ast_exprs.
2015 * "accesses" is the list of gpu_stmt_access in the statement.
2016 * "iterator_map" expresses the statement iterators in terms of
2017 * the AST loop iterators.
2018 * "sched2shared" expresses the outer shared_schedule_dim dimensions of
2019 * the kernel schedule in terms of the AST loop iterators.
2021 * The following fields are set in transform_index and used in transform_expr.
2022 * "array" is the array that is being accessed.
2023 * "global" is set if the global array is accessed (rather than
2024 * shared/private memory).
2025 * "local_array" refers to information on the array specialized
2026 * to the current kernel.
2028 struct ppcg_transform_data
{
2029 struct gpu_gen
*gen
;
2030 struct gpu_stmt_access
*accesses
;
2031 isl_pw_multi_aff
*iterator_map
;
2032 isl_pw_multi_aff
*sched2shared
;
2034 struct gpu_array_info
*array
;
2036 struct gpu_local_array_info
*local_array
;
2039 /* Return the name of the outer array (of structs) accessed by "access".
2041 static const char *get_outer_array_name(__isl_keep isl_map
*access
)
2046 space
= isl_space_range(isl_map_get_space(access
));
2047 while (space
&& isl_space_is_wrapping(space
))
2048 space
= isl_space_domain(isl_space_unwrap(space
));
2049 name
= isl_space_get_tuple_name(space
, isl_dim_set
);
2050 isl_space_free(space
);
2055 /* Return a pointer to the gpu_array_ref_group in "local"
2056 * that contains the reference "access".
2057 * Return NULL if no such group can be found.
2059 static struct gpu_array_ref_group
*find_ref_group(
2060 struct gpu_local_array_info
*local
, struct gpu_stmt_access
*access
)
2064 for (i
= 0; i
< local
->n_group
; ++i
) {
2065 struct gpu_array_ref_group
*group
= local
->groups
[i
];
2067 for (j
= 0; j
< group
->n_ref
; ++j
)
2068 if (group
->refs
[j
] == access
)
2075 /* Index transformation callback for pet_stmt_build_ast_exprs.
2077 * "index" expresses the array indices in terms of statement iterators
2079 * We first reformulate "index" in terms of the AST loop iterators.
2080 * Then we check if we are accessing the global array or
2081 * a shared/private copy. In the former case, we simply return
2082 * the updated index. If "index" is an affine expression rather
2083 * than an array access, then we also return the updated index here.
2085 * If no reference groups have been computed for the array,
2086 * then we can only be accessing the global array.
2088 * Otherwise, we apply the tiling to the index.
2089 * This tiling is of the form
2093 * where D corresponds to the outer group->depth dimensions of
2094 * the kernel schedule.
2095 * The index is of the form
2099 * We update the tiling to refer to the AST loop iterators
2103 * and modify index to keep track of those iterators
2107 * Combining these two yields a tiled index expression in terms
2108 * of the AST loop iterators
2112 static __isl_give isl_multi_pw_aff
*transform_index(
2113 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*ref_id
,
2116 struct ppcg_transform_data
*data
= user
;
2117 struct gpu_stmt_access
*access
;
2118 struct gpu_array_ref_group
*group
;
2119 struct gpu_array_tile
*tile
;
2120 isl_pw_multi_aff
*iterator_map
;
2125 isl_multi_pw_aff
*tiling
;
2126 isl_pw_multi_aff
*pma
;
2127 isl_multi_pw_aff
*mpa
;
2128 isl_pw_multi_aff
*sched2depth
;
2132 iterator_map
= isl_pw_multi_aff_copy(data
->iterator_map
);
2133 index
= isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
2135 access
= find_access(data
->accesses
, ref_id
);
2138 if (!isl_map_has_tuple_name(access
->access
, isl_dim_out
))
2141 name
= get_outer_array_name(access
->access
);
2142 i
= find_array_index(data
->gen
->kernel
, name
);
2144 isl_die(isl_multi_pw_aff_get_ctx(index
), isl_error_internal
,
2145 "cannot find array",
2146 return isl_multi_pw_aff_free(index
));
2147 data
->array
= &data
->gen
->prog
->array
[i
];
2148 data
->local_array
= &data
->gen
->kernel
->array
[i
];
2150 group
= find_ref_group(data
->local_array
, access
);
2156 tile
= group
->private_tile
;
2158 tile
= group
->shared_tile
;
2159 data
->global
= !tile
;
2163 space
= isl_space_range(isl_multi_pw_aff_get_space(index
));
2164 space
= isl_space_map_from_set(space
);
2165 pma
= isl_pw_multi_aff_identity(space
);
2166 sched2depth
= isl_pw_multi_aff_copy(data
->sched2shared
);
2167 dim
= isl_pw_multi_aff_dim(sched2depth
, isl_dim_out
);
2168 sched2depth
= isl_pw_multi_aff_drop_dims(sched2depth
, isl_dim_out
,
2169 group
->depth
, dim
- group
->depth
);
2170 pma
= isl_pw_multi_aff_product(sched2depth
, pma
);
2171 tiling
= isl_multi_pw_aff_from_multi_aff(
2172 isl_multi_aff_copy(tile
->tiling
));
2173 tiling
= isl_multi_pw_aff_pullback_pw_multi_aff(tiling
, pma
);
2175 space
= isl_space_domain(isl_multi_pw_aff_get_space(index
));
2176 space
= isl_space_map_from_set(space
);
2177 mpa
= isl_multi_pw_aff_identity(space
);
2178 index
= isl_multi_pw_aff_range_product(mpa
, index
);
2179 index
= isl_multi_pw_aff_pullback_multi_pw_aff(tiling
, index
);
2184 /* Dereference "expr" by adding an index [0].
2185 * The original "expr" is assumed not to have any indices.
2187 * If "expr" is a member access, then the dereferencing needs
2188 * to be applied to the structure argument of this member access.
2190 static __isl_give isl_ast_expr
*dereference(__isl_take isl_ast_expr
*expr
)
2193 isl_ast_expr
*arg0
, *res
;
2194 isl_ast_expr_list
*list
;
2196 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
2198 return isl_ast_expr_free(expr
);
2199 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
2200 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
2203 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
2204 arg
= dereference(arg
);
2205 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
2206 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
2210 isl_ast_expr_free(arg0
);
2212 ctx
= isl_ast_expr_get_ctx(expr
);
2213 res
= isl_ast_expr_from_val(isl_val_zero(ctx
));
2214 list
= isl_ast_expr_list_from_ast_expr(res
);
2215 res
= isl_ast_expr_get_op_arg(expr
, 0);
2216 res
= isl_ast_expr_access(res
, list
);
2217 isl_ast_expr_free(expr
);
2222 /* Linearize the index expression "expr" based on the array bounds
2225 * That is, transform expression
2227 * A[i_0][i_1]...[i_n]
2231 * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
2233 * where b_0, b_1, ..., b_n are the bounds on the array.
2235 * If the base of "expr" is a member access, then the linearization needs
2236 * to be applied to the structure argument of this member access.
2238 * In the base case, if "expr" has no arguments (other than the name of
2239 * the array), then we are passing an entire array to a function.
2240 * In this case, there is nothing to linearize.
2241 * Note that at this point an expression with no arguments can
2242 * only be an entire array because the scalar case and
2243 * the case of single struct are handled by the caller.
2245 * If the number of specified index expressions in "expr"
2246 * is smaller than the dimension of the accessed array,
2247 * then the missing i_j also do not appear in the linearized expression.
2248 * Furthermore, since such an expression does not refer to a single
2249 * element while the default linearized expression would refer to
2250 * a single element, we return the expression
2252 * A + (..((i_0 * b_1 + i_1) ... ) * b_n]
2254 * instead. Note that because of the special case handling above,
2255 * we can assume here that here that there is at least one index expression.
2257 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
2258 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
)
2265 isl_ast_expr_list
*list
;
2266 isl_ast_build
*build
;
2268 arg0
= isl_ast_expr_get_op_arg(expr
, 0);
2269 if (isl_ast_expr_get_type(arg0
) == isl_ast_expr_op
&&
2270 isl_ast_expr_get_op_type(arg0
) == isl_ast_op_member
) {
2273 arg
= isl_ast_expr_get_op_arg(arg0
, 0);
2274 arg
= gpu_local_array_info_linearize_index(array
, arg
);
2275 arg0
= isl_ast_expr_set_op_arg(arg0
, 0, arg
);
2276 expr
= isl_ast_expr_set_op_arg(expr
, 0, arg0
);
2280 isl_ast_expr_free(arg0
);
2282 if (isl_ast_expr_get_op_n_arg(expr
) == 1)
2285 ctx
= isl_ast_expr_get_ctx(expr
);
2286 context
= isl_set_universe(isl_space_params_alloc(ctx
, 0));
2287 build
= isl_ast_build_from_context(context
);
2289 n
= isl_ast_expr_get_op_n_arg(expr
);
2290 res
= isl_ast_expr_get_op_arg(expr
, 1);
2291 for (i
= 1; i
< array
->n_index
; ++i
) {
2292 isl_pw_aff
*bound_i
;
2293 isl_ast_expr
*expr_i
;
2295 bound_i
= isl_pw_aff_list_get_pw_aff(array
->bound
, i
);
2296 expr_i
= isl_ast_build_expr_from_pw_aff(build
, bound_i
);
2297 res
= isl_ast_expr_mul(res
, expr_i
);
2301 expr_i
= isl_ast_expr_get_op_arg(expr
, i
+ 1);
2302 res
= isl_ast_expr_add(res
, expr_i
);
2305 isl_ast_build_free(build
);
2307 if (1 + array
->n_index
> n
) {
2308 res
= isl_ast_expr_add(isl_ast_expr_get_op_arg(expr
, 0), res
);
2310 list
= isl_ast_expr_list_from_ast_expr(res
);
2311 res
= isl_ast_expr_get_op_arg(expr
, 0);
2312 res
= isl_ast_expr_access(res
, list
);
2315 isl_ast_expr_free(expr
);
2320 /* AST expression transformation callback for pet_stmt_build_ast_exprs.
2322 * If the AST expression refers to an array that is not accessed
2323 * at all, then this means the value of the expression is not used,
2324 * so we might as well print zero (NULL pointer) instead.
2326 * If the AST expression refers to a global scalar that is not
2327 * a read-only scalar, then its address was passed to the kernel and
2328 * we need to dereference it.
2330 * If the AST expression refers to an access to a global array,
2331 * then we linearize the access exploiting the bounds in data->local_array.
2333 static __isl_give isl_ast_expr
*transform_expr(__isl_take isl_ast_expr
*expr
,
2334 __isl_keep isl_id
*id
, void *user
)
2336 struct ppcg_transform_data
*data
= user
;
2340 if (!data
->array
->accessed
) {
2343 ctx
= isl_ast_expr_get_ctx(expr
);
2344 isl_ast_expr_free(expr
);
2345 return isl_ast_expr_from_val(isl_val_zero(ctx
));
2347 if (gpu_array_is_read_only_scalar(data
->array
))
2351 if (data
->array
->n_index
== 0)
2352 return dereference(expr
);
2353 if (!data
->array
->linearize
)
2356 return gpu_local_array_info_linearize_index(data
->local_array
, expr
);
2359 /* This function is called for each instance of a user statement
2360 * in the kernel, identified by "gpu_stmt".
2362 * We attach a struct ppcg_kernel_stmt to the "node", containing
2363 * a computed AST expression for each access.
2364 * These AST expressions are computed from iterator_map,
2365 * which expresses the domain
2366 * elements in terms of the generated loops, and sched2shared,
2367 * which expresses the outer shared_schedule_dim dimensions of
2368 * the kernel schedule computed by PPCG in terms of the generated loops.
2370 static __isl_give isl_ast_node
*create_domain_leaf(struct gpu_gen
*gen
,
2371 __isl_take isl_ast_node
*node
, __isl_keep isl_ast_build
*build
,
2372 struct gpu_stmt
*gpu_stmt
)
2374 struct ppcg_transform_data data
;
2375 struct ppcg_kernel_stmt
*stmt
;
2377 isl_pw_multi_aff
*sched2shared
;
2379 isl_pw_multi_aff
*iterator_map
;
2380 isl_union_map
*schedule
;
2382 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
2384 return isl_ast_node_free(node
);
2386 schedule
= isl_ast_build_get_schedule(build
);
2387 map
= isl_map_reverse(isl_map_from_union_map(schedule
));
2388 iterator_map
= isl_pw_multi_aff_from_map(map
);
2389 sched2shared
= compute_sched_to_shared(gen
->kernel
,
2390 isl_pw_multi_aff_copy(iterator_map
));
2392 stmt
->type
= ppcg_kernel_domain
;
2393 stmt
->u
.d
.stmt
= gpu_stmt
;
2396 data
.accesses
= stmt
->u
.d
.stmt
->accesses
;
2397 data
.iterator_map
= iterator_map
;
2398 data
.sched2shared
= sched2shared
;
2399 stmt
->u
.d
.ref2expr
= pet_stmt_build_ast_exprs(stmt
->u
.d
.stmt
->stmt
,
2400 build
, &transform_index
, &data
,
2401 &transform_expr
, &data
);
2403 isl_pw_multi_aff_free(iterator_map
);
2404 isl_pw_multi_aff_free(sched2shared
);
2406 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
2407 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
2408 return isl_ast_node_set_annotation(node
, id
);
2411 /* This function is called for each statement node in the AST
2412 * for copying to or from shared/private memory.
2413 * Attach a pointer to a ppcg_kernel_stmt representing the copy
2414 * statement to the node.
2415 * The statement name is "read" or "write", depending on whether we are
2416 * reading from global memory or writing to global memory.
2418 * The schedule is of the form
2422 * where D corresponds to the outer group->depth dimensions of
2423 * the kernel schedule, A to the global array and L to the outer
2424 * generated AST schedule.
2425 * We compute the inverse and strip off the type, resulting in
2429 * We combine this mapping with on the one hand the projection
2433 * and on the other hand the group tiling
2441 * and store the corresponding expressions in stmt->index and stmt->local_index,
2442 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
2444 static __isl_give isl_ast_node
*create_access_leaf(struct gpu_gen
*gen
,
2445 struct gpu_array_ref_group
*group
, __isl_take isl_ast_node
*node
,
2446 __isl_keep isl_ast_build
*build
)
2448 struct ppcg_kernel_stmt
*stmt
;
2449 struct gpu_array_tile
*tile
;
2454 isl_pw_multi_aff
*pma
, *pma2
;
2457 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
2459 return isl_ast_node_free(node
);
2461 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
2462 type
= isl_map_get_tuple_name(access
, isl_dim_in
);
2463 stmt
->u
.c
.read
= !strcmp(type
, "read");
2464 access
= isl_map_reverse(access
);
2465 pma
= isl_pw_multi_aff_from_map(access
);
2466 pma
= isl_pw_multi_aff_reset_tuple_id(pma
, isl_dim_out
);
2468 space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
2469 space
= isl_space_unwrap(space
);
2470 pma2
= isl_pw_multi_aff_range_map(space
);
2471 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
,
2472 isl_pw_multi_aff_copy(pma
));
2473 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
2474 stmt
->u
.c
.index
= expr
;
2476 tile
= gpu_array_ref_group_tile(group
);
2477 pma2
= isl_pw_multi_aff_from_multi_aff(
2478 isl_multi_aff_copy(tile
->tiling
));
2479 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(pma2
, pma
);
2480 expr
= isl_ast_build_access_from_pw_multi_aff(build
, pma2
);
2481 stmt
->u
.c
.local_index
= expr
;
2483 stmt
->u
.c
.array
= group
->array
;
2484 stmt
->u
.c
.local_array
= group
->local_array
;
2485 stmt
->type
= ppcg_kernel_copy
;
2487 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
2488 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
2489 return isl_ast_node_set_annotation(node
, id
);
2492 /* Create a synchronization ppcg_kernel_stmt and
2493 * attach it to the node "node" representing the synchronization.
2495 static __isl_give isl_ast_node
*create_sync_leaf(
2496 struct gpu_gen
*gen
, __isl_take isl_ast_node
*node
,
2497 __isl_keep isl_ast_build
*build
)
2499 struct ppcg_kernel_stmt
*stmt
;
2502 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
2504 return isl_ast_node_free(node
);
2506 stmt
->type
= ppcg_kernel_sync
;
2507 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
2508 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
2509 return isl_ast_node_set_annotation(node
, id
);
2512 /* This function is called for each instance of a user statement
2513 * in the kernel. This may be one of the original user statements
2514 * or a statement introduced by PPCG.
2516 * We assume that the original user statements only have a name
2517 * and no user pointer. The statements introduced by PPCG
2518 * on the other hand all have a user pointer.
2520 * If the user statement is one of the original user statements
2521 * (one with no user pointer), then we call create_domain_leaf. Otherwise,
2522 * we check if it is a copy or synchronization statement and
2523 * call the appropriate functions.
2525 static __isl_give isl_ast_node
*at_domain(__isl_take isl_ast_node
*node
,
2526 __isl_keep isl_ast_build
*build
, void *user
)
2528 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
2529 isl_ast_expr
*expr
, *arg
;
2535 expr
= isl_ast_node_user_get_expr(node
);
2536 arg
= isl_ast_expr_get_op_arg(expr
, 0);
2537 id
= isl_ast_expr_get_id(arg
);
2538 name
= isl_id_get_name(id
);
2539 p
= isl_id_get_user(id
);
2540 isl_ast_expr_free(expr
);
2541 isl_ast_expr_free(arg
);
2544 struct gpu_stmt
*gpu_stmt
;
2546 gpu_stmt
= find_stmt(gen
->prog
, id
);
2549 isl_die(gen
->ctx
, isl_error_internal
,
2550 "statement not found",
2551 return isl_ast_node_free(node
));
2553 return create_domain_leaf(gen
, node
, build
, gpu_stmt
);
2556 is_sync
= gpu_tree_id_is_sync(id
, gen
->kernel
);
2559 return isl_ast_node_free(node
);
2560 if (!strcmp(name
, "read") || !strcmp(name
, "write")) {
2561 struct gpu_array_ref_group
*group
= p
;
2562 return create_access_leaf(gen
, group
, node
, build
);
2565 isl_die(gen
->ctx
, isl_error_internal
,
2566 "unknown statement type",
2567 return isl_ast_node_free(node
));
2568 return create_sync_leaf(gen
, node
, build
);
2571 /* Mark all odd schedule dimensions as "atomic" (when the even dimensions
2572 * have value 0) and all even schedule dimensions as "unroll".
2574 * That is, the options look as follows
2576 * { [0, b, 0, d, ..., 0] -> atomic[i] : exists a : i = 2 a + 1;
2577 * [a, b, c, d, ..., z] -> unroll[i] : exists a : i = 2 a }
2579 * The even positions are used to be able to schedule copying blocks
2580 * and synchronization before or after each level of the shared memory
2581 * tile loops and we want to make sure that code for these is generated
2582 * separately (within each level).
2584 static __isl_give isl_ast_build
*set_atomic_and_unroll(
2585 __isl_take isl_ast_build
*build
,
2586 __isl_take isl_space
*space
, int sched_len
)
2592 isl_local_space
*ls
;
2595 ctx
= isl_ast_build_get_ctx(build
);
2597 space
= isl_space_params(space
);
2598 space
= isl_space_add_dims(space
, isl_dim_set
, sched_len
);
2599 space
= isl_space_from_domain(space
);
2600 space
= isl_space_add_dims(space
, isl_dim_out
, 2);
2601 map
= isl_map_universe(isl_space_copy(space
));
2602 for (i
= 0; i
< sched_len
; i
+= 2)
2603 map
= isl_map_fix_si(map
, isl_dim_in
, i
, 0);
2604 ls
= isl_local_space_from_space(isl_map_get_space(map
));
2605 c
= isl_equality_alloc(ls
);
2606 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
2607 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
2608 c
= isl_constraint_set_constant_si(c
, 1);
2609 map
= isl_map_add_constraint(map
, c
);
2610 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
2611 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "atomic");
2612 opt
= isl_union_map_from_map(map
);
2614 map
= isl_map_universe(space
);
2615 ls
= isl_local_space_from_space(isl_map_get_space(map
));
2616 c
= isl_equality_alloc(ls
);
2617 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
2618 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
2619 map
= isl_map_add_constraint(map
, c
);
2620 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
2621 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "unroll");
2622 opt
= isl_union_map_add_map(opt
, map
);
2624 build
= isl_ast_build_set_options(build
, opt
);
2629 /* Return a map that maps a space of dimension gen->shared_len
2630 * to its last dimensions starting at gen->tile_first.
2631 * The range is of dimension
2633 * 2 * (gen->shared_len - gen->tile_first) + 1
2635 * The input dimensions are mapped to the odd dimensions in the output,
2636 * while the even dimensions (except 2*pos) are fixed to 0.
2637 * Output dimension 2*pos (if pos >= 0) is fixed to "val".
2638 * If pos >= 0, then only the pos first dimensions starting at gen->tile_first
2639 * are mapped to the output. The remaining input dimensions are projected
2640 * out and the corresponding output dimensions are fixed to 0.
2642 static __isl_give isl_map
*insert_even(struct gpu_gen
*gen
,
2643 __isl_take isl_space
*space
, int pos
, int val
)
2648 space
= isl_space_set_from_params(space
);
2649 space
= isl_space_add_dims(space
, isl_dim_set
, gen
->shared_len
);
2650 space
= isl_space_map_from_set(space
);
2651 proj
= isl_map_identity(space
);
2652 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, gen
->tile_first
);
2653 n
= gen
->shared_len
- gen
->tile_first
;
2654 for (i
= 0; i
<= n
; ++i
) {
2655 proj
= isl_map_insert_dims(proj
, isl_dim_out
, 2 * i
, 1);
2657 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, val
);
2659 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, 0);
2665 proj
= isl_map_eliminate(proj
, isl_dim_in
, gen
->tile_first
+ pos
,
2666 gen
->shared_len
- (gen
->tile_first
+ pos
));
2667 for (i
= pos
; i
< n
; ++i
)
2668 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
+ 1, 0);
2673 /* Given the AST context schedule "schedule" and the mapping from
2674 * domains to the shared tile loops "shared_sched", add a schedule
2675 * for a synchronization operation at position "val" of loop level "pos".
2677 * schedule is of the form
2681 * (with D the iteration domains and L the already generated loops),
2682 * while shared_sched is of the form
2686 * We combine them into
2692 * [s_0,...] -> [0,s_{tile_first},0,..., val, 0, 0, ... 0]
2694 * and use the result as a schedule for "sync".
2696 static __isl_give isl_union_map
*add_sync_schedule(struct gpu_gen
*gen
,
2697 __isl_take isl_union_map
*res
, __isl_keep isl_union_map
*schedule
,
2698 __isl_keep isl_union_map
*shared_sched
, int pos
, int val
)
2701 isl_map
*proj
, *map
;
2703 shared_sched
= isl_union_map_copy(shared_sched
);
2704 schedule
= isl_union_map_copy(schedule
);
2706 space
= isl_union_map_get_space(shared_sched
);
2707 schedule
= isl_union_map_apply_domain(shared_sched
, schedule
);
2708 map
= isl_map_from_union_map(schedule
);
2710 proj
= insert_even(gen
, space
, pos
, val
);
2711 map
= isl_map_apply_range(map
, proj
);
2712 map
= isl_map_from_range(isl_map_wrap(map
));
2713 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "sync");
2715 res
= isl_union_map_add_map(res
, map
);
2720 /* Given a set of wrapped references "ref", return the corresponding
2721 * access relations based on the tagged access relations "tagged".
2723 * The elements of "ref" are of the form
2727 * with D an iteration domains and R a reference.
2728 * The elements of "tagged" are of the form
2734 * Extend "tagged" to include the iteration domain in the range, i.e.,
2736 * [D -> R] -> [D -> A]
2738 * apply the result to "ref" and then unwrap the resulting set
2739 * to obtain relations of the form
2743 static __isl_give isl_union_map
*wrapped_reference_to_access(
2744 __isl_take isl_union_set
*ref
, __isl_take isl_union_map
*tagged
)
2746 isl_union_map
*tag2access
;
2748 tag2access
= isl_union_map_copy(tagged
);
2749 tag2access
= isl_union_map_universe(tag2access
);
2750 tag2access
= isl_union_set_unwrap(isl_union_map_domain(tag2access
));
2751 tag2access
= isl_union_map_domain_map(tag2access
);
2752 tag2access
= isl_union_map_range_product(tag2access
, tagged
);
2754 ref
= isl_union_set_coalesce(ref
);
2755 ref
= isl_union_set_apply(ref
, tag2access
);
2757 return isl_union_set_unwrap(ref
);
2760 /* Given an access relation "access" from "group", remove those reads
2761 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2762 * communicate data within the same iteration of "sched".
2764 * If the access is a read then it is either an element of
2766 * live_in union (range flow)
2768 * where live_in and flow may be overapproximations, or
2769 * it reads an uninitialized value (that is not live-in because
2770 * there is an intermediate kill) or it reads a value that was
2771 * written within the same (compound) statement instance.
2772 * If the access is a write then it is either an element of
2774 * live_out union (domain flow)
2776 * or it writes a value that is never read (and is not live-out
2777 * because of an intermediate kill) or only
2778 * within the same (compound) statement instance.
2779 * In both cases, the access relation is also a subset of
2780 * the group access relation.
2782 * The cases where an uninitialized value is read or a value is written
2783 * that is never read or where the dataflow occurs within a statement
2784 * instance are also considered local and may also be removed.
2786 * Essentially, we compute the intersection of "access" with either
2788 * live_in union (range non-local-flow)
2792 * live_out union (domain non-local-flow)
2794 * We first construct a relation "local"
2796 * [[D -> R] -> [D' -> R']]
2798 * of pairs of domain iterations accessing the reference group
2799 * and references in the group that are coscheduled by "sched".
2801 * If this relation does not intersect the dataflow dependences,
2802 * then there is nothing we can possibly remove, unless the dataflow
2803 * dependences themselves only relate a subset of the accesses.
2804 * In particular, the accesses may not be involved in any dataflow
2805 * dependences, either because they are uninitialized reads/dead writes
2806 * or because the dataflow occurs inside a statement instance.
2808 * Since the computation below may break up the access relation
2809 * into smaller pieces, we only perform the intersection with
2810 * the non-local dependent accesses if the local pairs
2811 * intersect the dataflow dependences. Otherwise, we intersect
2812 * with the universe of the non-local dependent accesses.
2813 * This should at least remove accesses from statements that
2814 * do not participate in any dependences.
2816 * In particular, we remove the "local" dataflow dependences from
2817 * the set of all dataflow dependences.
2818 * Note that if the potential dataflow dependences are an overapproximation
2819 * of the actual dataflow dependences, then the result remains an
2820 * overapproximation of the non-local dataflow dependences.
2821 * Copying to/from global memory is only needed for the references
2822 * in the domain/range of the result or for accesses that are live out/in
2823 * for the entire scop.
2825 * We therefore map the domain/range of the "external" relation
2826 * to the corresponding access relation and take the union with
2827 * the live out/in relation.
2829 static __isl_give isl_union_map
*remove_local_accesses(
2830 struct gpu_prog
*prog
, struct gpu_array_ref_group
*group
,
2831 __isl_take isl_union_map
*access
, __isl_take isl_union_map
*sched
,
2835 isl_union_pw_multi_aff
*tagger
;
2836 isl_union_set
*domain
;
2837 isl_union_map
*local
, *tagged
, *external
;
2838 isl_union_set
*tag_set
;
2840 if (isl_union_map_is_empty(access
)) {
2841 isl_union_map_free(sched
);
2845 tagged
= group_tagged_access_relation(group
);
2847 tagger
= isl_union_pw_multi_aff_copy(prog
->scop
->tagger
);
2848 domain
= isl_union_map_domain(isl_union_map_copy(tagged
));
2849 tagger
= isl_union_pw_multi_aff_intersect_domain(tagger
, domain
);
2850 sched
= isl_union_map_preimage_domain_union_pw_multi_aff(sched
, tagger
);
2852 local
= isl_union_map_apply_range(sched
,
2853 isl_union_map_reverse(isl_union_map_copy(sched
)));
2854 local
= isl_union_map_intersect(local
,
2855 isl_union_map_copy(prog
->scop
->tagged_dep_flow
));
2857 empty
= isl_union_map_is_empty(local
);
2859 external
= isl_union_map_copy(prog
->scop
->tagged_dep_flow
);
2860 external
= isl_union_map_intersect_params(external
,
2861 isl_set_copy(prog
->scop
->context
));
2862 external
= isl_union_map_subtract(external
, local
);
2865 tag_set
= isl_union_map_range(external
);
2866 external
= wrapped_reference_to_access(tag_set
, tagged
);
2867 external
= isl_union_map_union(external
,
2868 isl_union_map_copy(prog
->scop
->live_in
));
2870 tag_set
= isl_union_map_domain(external
);
2871 external
= wrapped_reference_to_access(tag_set
, tagged
);
2872 external
= isl_union_map_union(external
,
2873 isl_union_map_copy(prog
->scop
->live_out
));
2877 external
= isl_union_map_free(external
);
2879 external
= isl_union_map_universe(external
);
2881 access
= isl_union_map_intersect(access
, external
);
2886 /* Given an access relation "access" from "group", remove those reads
2887 * if ("read" is 1) or writes (if "read" is 0) that are only needed to
2888 * communicate data within the same iteration of the schedule at the
2889 * position where the copying of the group is inserted.
2890 * "node" points to this position, i.e., the depth at "node"
2891 * is equal to group->depth.
2893 * We extract a schedule that picks out the iterations of the outer
2894 * group->depth dimensions and call remove_local_accesses.
2896 static __isl_give isl_union_map
*remove_local_accesses_group(
2897 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
2898 __isl_take isl_union_map
*access
, __isl_keep isl_schedule_node
*node
,
2901 isl_union_map
*sched
;
2903 if (isl_union_map_is_empty(access
))
2906 sched
= isl_schedule_node_get_prefix_schedule_relation(node
);
2908 return remove_local_accesses(kernel
->prog
, group
, access
, sched
, read
);
2911 /* Attach "id" to the given node.
2913 static __isl_give isl_ast_node
*attach_id(__isl_take isl_ast_node
*node
,
2914 __isl_keep isl_ast_build
*build
, void *user
)
2918 node
= isl_ast_node_set_annotation(node
, id
);
2923 /* Construct an AST node for performing a kernel launch and attach
2924 * the information about the kernel to that node.
2925 * "kernel_id" has name "kernel" and contains a pointer
2926 * to the ppcg_kernel structure.
2928 * The kernel AST has been constructed in the context of the range
2929 * of "schedule". In particular, the grid size has been computed
2930 * in the context. We therefore still need to make sure that these
2931 * constraints are expressed in the code. We do this by creating a schedule
2933 * kernel[] -> [S -> []]
2935 * where S is the schedule domain, i.e., the range of "schedule".
2936 * The AST generation will then create a single call surrounded by
2937 * all the condition in "S" that have not been expressed yet.
2939 * The kernel information is attached to this node in attach_id.
2941 static __isl_give isl_ast_node
*construct_launch(
2942 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*schedule
,
2943 __isl_take isl_id
*kernel_id
)
2946 isl_union_set
*domain
;
2951 ctx
= isl_ast_build_get_ctx(build
);
2953 domain
= isl_union_map_range(schedule
);
2954 set
= isl_set_from_union_set(domain
);
2955 map
= isl_map_from_domain(set
);
2956 map
= isl_map_from_range(isl_map_wrap(map
));
2957 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "kernel");
2958 schedule
= isl_union_map_from_map(map
);
2960 build
= isl_ast_build_set_at_each_domain(build
, &attach_id
, kernel_id
);
2961 node
= isl_ast_build_node_from_schedule_map(build
, schedule
);
2962 isl_ast_build_free(build
);
2967 /* This function is called before the AST generator starts traversing
2968 * the schedule subtree of a node with mark "mark".
2970 * If the mark is called "kernel", store the mark itself in gen->kernel_mark
2971 * and the kernel pointer in gen->kernel for use in at_domain.
2973 static int before_mark(__isl_keep isl_id
*mark
,
2974 __isl_keep isl_ast_build
*build
, void *user
)
2976 struct gpu_gen
*gen
= user
;
2980 if (!strcmp(isl_id_get_name(mark
), "kernel")) {
2981 gen
->kernel_mark
= isl_id_copy(mark
);
2982 gen
->kernel
= isl_id_get_user(mark
);
2987 /* This function is called after the AST generator has finished traversing
2988 * the schedule subtree of a mark node. "node" points to the corresponding
2991 * If the mark is called "kernel", then replace "node" by a user node
2992 * that "calls" the kernel, representing the launch of the kernel.
2993 * The original "node" is stored inside the kernel object so that
2994 * it can be used to print the device code.
2995 * Note that this assumes that a kernel is only launched once.
2996 * Also clear the kernel and kernel_mark fields of gen.
2998 static __isl_give isl_ast_node
*after_mark(__isl_take isl_ast_node
*node
,
2999 __isl_keep isl_ast_build
*build
, void *user
)
3004 isl_ast_expr_list
*list
;
3005 struct ppcg_kernel
*kernel
;
3006 struct gpu_gen
*gen
= user
;
3008 ctx
= isl_ast_node_get_ctx(node
);
3009 id
= isl_ast_node_mark_get_id(node
);
3011 return isl_ast_node_free(node
);
3012 if (strcmp(isl_id_get_name(id
), "kernel") || !gen
->kernel
) {
3016 gen
->kernel_mark
= isl_id_free(gen
->kernel_mark
);
3017 kernel
= gen
->kernel
;
3019 kernel
->space
= isl_ast_build_get_schedule_space(build
);
3020 kernel
->tree
= isl_ast_node_mark_get_node(node
);
3021 isl_ast_node_free(node
);
3023 expr
= isl_ast_expr_from_id(isl_id_copy(id
));
3024 list
= isl_ast_expr_list_alloc(ctx
, 0);
3025 expr
= isl_ast_expr_call(expr
, list
);
3026 node
= isl_ast_node_alloc_user(expr
);
3027 node
= isl_ast_node_set_annotation(node
, id
);
3032 static int update_depth(__isl_keep isl_schedule_node
*node
, void *user
)
3037 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
3039 node_depth
= isl_schedule_node_get_schedule_depth(node
);
3040 if (node_depth
> *depth
)
3041 *depth
= node_depth
;
3046 /* Use isl to generate code for both the host and the device
3047 * from gen->schedule.
3048 * The device code is marked by "kernel" mark nodes in the schedule tree,
3049 * containing a pointer to a ppcg_kernel object.
3050 * The returned AST only contains the AST for the host code.
3051 * The ASTs for the device code are embedded in ppcg_kernel objects
3052 * attached to the leaf nodes that call "kernel".
3054 static __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
)
3056 isl_ast_build
*build
;
3058 isl_schedule
*schedule
;
3059 isl_id_list
*iterators
;
3063 if (isl_schedule_foreach_schedule_node(gen
->schedule
, &update_depth
,
3066 build
= isl_ast_build_from_context(isl_set_copy(gen
->prog
->context
));
3067 iterators
= ppcg_scop_generate_names(gen
->prog
->scop
, depth
, "c");
3068 build
= isl_ast_build_set_iterators(build
, iterators
);
3069 build
= isl_ast_build_set_at_each_domain(build
, &at_domain
, gen
);
3070 build
= isl_ast_build_set_before_each_mark(build
, &before_mark
, gen
);
3071 build
= isl_ast_build_set_after_each_mark(build
, &after_mark
, gen
);
3072 schedule
= isl_schedule_copy(gen
->schedule
);
3073 tree
= isl_ast_build_node_from_schedule(build
, schedule
);
3074 isl_ast_build_free(build
);
3079 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
3083 return isl_union_map_read_from_str(ctx
, str
);
3086 /* Information about the outermost tilable bands in the forest of bands.
3088 * prefix is the (padded) schedule leading up to the outermost tilable bands.
3090 * tile_first is the number of schedule dimensions in prefix.
3092 * suffix is the schedule of the outermost tilable bands and their descendants.
3095 struct gpu_gen
*gen
;
3097 isl_union_map
*prefix
;
3098 isl_union_map
*suffix
;
3101 /* Construct an isl_multi_val for use as tile sizes for tiling "node"
3102 * from the elements in "tile_size".
3104 static __isl_give isl_multi_val
*construct_band_tiles_sizes(
3105 __isl_keep isl_schedule_node
*node
, int *tile_size
)
3115 ctx
= isl_schedule_node_get_ctx(node
);
3116 space
= isl_schedule_node_band_get_space(node
);
3117 n
= isl_schedule_node_band_n_member(node
);
3118 mv
= isl_multi_val_zero(space
);
3119 for (i
= 0; i
< n
; ++i
) {
3122 v
= isl_val_int_from_si(ctx
, tile_size
[i
]);
3123 mv
= isl_multi_val_set_val(mv
, i
, v
);
3129 /* Replace the partial schedule S of the band node "node" by
3137 * if scale_tile_loops is set, with f the integers in "factor".
3138 * The list that "factor" points to is assumed to contain at least
3139 * as many elements as the number of members in the band.
3141 static __isl_give isl_schedule_node
*snap_band_to_sizes(
3142 __isl_take isl_schedule_node
*node
, int *factor
,
3143 struct ppcg_options
*options
)
3147 mv
= construct_band_tiles_sizes(node
, factor
);
3148 node
= isl_schedule_node_band_scale_down(node
, isl_multi_val_copy(mv
));
3149 if (options
->scale_tile_loops
)
3150 node
= isl_schedule_node_band_scale(node
,
3151 isl_multi_val_copy(mv
));
3152 isl_multi_val_free(mv
);
3157 /* Tile "band" with tile size specified by "sizes".
3159 * Since the tile loops will be mapped to block ids, we forcibly
3160 * turn off tile loop scaling. We may want to enable tile loop scaling
3161 * at some later point, but then we would have to support the detection
3162 * of strides during the mapping to block ids.
3163 * Similarly, since the point loops will be mapped to thread ids,
3164 * we forcibly shift the point loops so that they start at zero.
3166 static __isl_give isl_schedule_node
*tile_band(
3167 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
3169 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
3173 scale_tile
= isl_options_get_tile_scale_tile_loops(ctx
);
3174 isl_options_set_tile_scale_tile_loops(ctx
, 0);
3175 shift_point
= isl_options_get_tile_shift_point_loops(ctx
);
3176 isl_options_set_tile_shift_point_loops(ctx
, 1);
3178 node
= isl_schedule_node_band_tile(node
, sizes
);
3180 isl_options_set_tile_scale_tile_loops(ctx
, scale_tile
);
3181 isl_options_set_tile_shift_point_loops(ctx
, shift_point
);
3186 /* Extract the set of parameter values and outer schedule dimensions
3187 * for which any statement instance
3188 * in the kernel inserted at "node" needs to be executed.
3189 * Intersect the set of parameter values derived from the host schedule
3190 * relation with the context of "prog".
3192 static __isl_give isl_set
*extract_context(__isl_keep isl_schedule_node
*node
,
3193 struct gpu_prog
*prog
)
3195 isl_union_map
*schedule
;
3196 isl_union_set
*schedule_domain
;
3200 schedule
= isl_schedule_node_get_prefix_schedule_relation(node
);
3201 schedule_domain
= isl_union_map_range(schedule
);
3202 empty
= isl_union_set_is_empty(schedule_domain
);
3204 isl_union_set_free(schedule_domain
);
3211 space
= isl_union_set_get_space(schedule_domain
);
3212 isl_union_set_free(schedule_domain
);
3213 space
= isl_space_set_from_params(space
);
3214 depth
= isl_schedule_node_get_schedule_depth(node
);
3215 space
= isl_space_add_dims(space
, isl_dim_set
, depth
);
3216 context
= isl_set_empty(space
);
3218 context
= isl_set_from_union_set(schedule_domain
);
3220 context
= isl_set_intersect_params(context
,
3221 isl_set_copy(prog
->context
));
3226 /* Return the set of outer array elements accessed by
3227 * by the statement instance in "domain" in "prog".
3229 static __isl_give isl_union_set
*accessed_by_domain(
3230 __isl_take isl_union_set
*domain
, struct gpu_prog
*prog
)
3232 isl_union_map
*access
;
3233 isl_union_set
*arrays
;
3235 access
= isl_union_map_union(isl_union_map_copy(prog
->read
),
3236 isl_union_map_copy(prog
->may_write
));
3237 access
= isl_union_map_intersect_domain(access
, domain
);
3238 arrays
= isl_union_map_range(access
);
3239 arrays
= isl_union_set_apply(arrays
,
3240 isl_union_map_copy(prog
->to_outer
));
3245 /* Return the number of outer band members of the band node "node"
3246 * that are marked coincident.
3248 static int n_outer_coincidence(__isl_keep isl_schedule_node
*node
)
3252 n
= isl_schedule_node_band_n_member(node
);
3254 for (i
= 0; i
< n
; ++i
)
3255 if (!isl_schedule_node_band_member_get_coincident(node
, i
))
3261 /* If the band node "node" has more than "n" members, then split off
3262 * the first "n" of them.
3264 static __isl_give isl_schedule_node
*split_band(
3265 __isl_take isl_schedule_node
*node
, int n
)
3269 dim
= isl_schedule_node_band_n_member(node
);
3271 node
= isl_schedule_node_band_split(node
, n
);
3276 /* Scale a band node that may have been split by split_band.
3277 * "sizes" are the scaling factors for the original node.
3278 * "node" either points to the original band node, or the outer
3279 * of the two pieces after splitting.
3281 * If the number of elements in "node" is smaller than the number of
3282 * elements in "sizes", then some splitting has occurred and we split
3283 * "sizes" in the same way.
3285 static __isl_give isl_schedule_node
*scale_band(
3286 __isl_take isl_schedule_node
*node
, __isl_take isl_multi_val
*sizes
)
3290 n
= isl_multi_val_dim(sizes
, isl_dim_set
);
3291 dim
= isl_schedule_node_band_n_member(node
);
3293 isl_multi_val
*sizes2
;
3295 sizes2
= isl_multi_val_copy(sizes
);
3296 sizes
= isl_multi_val_drop_dims(sizes
,
3297 isl_dim_set
, dim
, n
- dim
);
3298 sizes2
= isl_multi_val_drop_dims(sizes2
, isl_dim_set
, 0, dim
);
3299 node
= isl_schedule_node_child(node
, 0);
3300 node
= isl_schedule_node_band_scale(node
, sizes2
);
3301 node
= isl_schedule_node_parent(node
);
3304 return isl_schedule_node_band_scale(node
, sizes
);
3307 /* Return an isl_multi_aff, with as elements the parameters in "space"
3308 * that have the names specified by the elements in "names".
3309 * If (some of) these parameters do not already appear in "space",
3310 * then they are added first.
3312 static __isl_give isl_multi_aff
*parameter_vector(__isl_take isl_space
*space
,
3313 __isl_keep isl_id_list
*names
)
3316 isl_local_space
*ls
;
3320 space
= isl_space_free(space
);
3322 n
= isl_id_list_n_id(names
);
3323 for (i
= 0; i
< n
; ++i
) {
3327 id
= isl_id_list_get_id(names
, i
);
3328 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
3333 pos
= isl_space_dim(space
, isl_dim_param
);
3334 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
3335 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
3337 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3338 ls
= isl_local_space_from_space(isl_space_domain(space
));
3339 for (i
= 0; i
< n
; ++i
) {
3344 id
= isl_id_list_get_id(names
, i
);
3345 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
3347 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3348 isl_dim_param
, pos
);
3349 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3351 isl_local_space_free(ls
);
3356 /* Return constraints on the domain elements that equate a sequence of
3357 * parameters called "names", to the partial schedule
3358 * of "node" modulo the integers in "size".
3359 * The number of elements in the array "size" should be equal
3360 * to the number of elements in "names".
3361 * The number of members of the band node "node" should be smaller
3362 * than or equal to this number. If it is smaller, then the first
3363 * elements of "names" are equated to zero.
3365 static __isl_give isl_union_set
*set_schedule_modulo(
3366 __isl_keep isl_schedule_node
*node
, __isl_keep isl_id_list
*names
,
3372 isl_multi_union_pw_aff
*mupa
, *mupa2
;
3374 isl_union_set
*domain
;
3378 n
= isl_id_list_n_id(names
);
3380 return isl_schedule_node_get_universe_domain(node
);
3381 n_zero
= n
- isl_schedule_node_band_n_member(node
);
3383 mupa
= isl_schedule_node_band_get_partial_schedule(node
);
3384 mv
= construct_band_tiles_sizes(node
, size
+ n_zero
);
3385 mupa
= isl_multi_union_pw_aff_mod_multi_val(mupa
, mv
);
3387 space
= isl_multi_union_pw_aff_get_space(mupa
);
3388 space
= isl_space_params(space
);
3389 space
= isl_space_set_from_params(space
);
3390 space
= isl_space_add_dims(space
, isl_dim_set
, n_zero
);
3391 ma
= isl_multi_aff_zero(space
);
3393 domain
= isl_schedule_node_get_universe_domain(node
);
3394 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(
3395 isl_union_set_copy(domain
), ma
);
3396 mupa
= isl_multi_union_pw_aff_range_product(mupa2
, mupa
);
3398 space
= isl_multi_union_pw_aff_get_space(mupa
);
3399 ma
= parameter_vector(space
, names
);
3401 mupa2
= isl_multi_union_pw_aff_multi_aff_on_domain(domain
, ma
);
3402 mupa
= isl_multi_union_pw_aff_sub(mupa
, mupa2
);
3404 return isl_multi_union_pw_aff_zero_union_set(mupa
);
3407 /* Insert a context node at "node" introducing the block and thread
3408 * identifiers along with their bounds, which are stored in kernel->grid_size
3409 * and kernel->block_dim.
3410 * Note that the bounds on the block identifiers may implicitly impose
3411 * constraints on the parameters. A guard needs to be inserted
3412 * in the schedule tree to ensure that those bounds hold at "node".
3413 * This guard is inserted in insert_guard.
3415 static __isl_give isl_schedule_node
*insert_context(struct ppcg_kernel
*kernel
,
3416 __isl_take isl_schedule_node
*node
)
3420 context
= isl_set_universe(isl_set_get_space(kernel
->context
));
3422 context
= add_bounded_parameters_dynamic(context
,
3423 kernel
->grid_size
, kernel
->block_ids
);
3424 context
= add_bounded_parameters(context
,
3425 kernel
->block_dim
, kernel
->thread_ids
);
3427 node
= isl_schedule_node_insert_context(node
, context
);
3432 /* Insert a guard that eliminates kernel launches where the kernel
3433 * obviously does not have any work to do.
3435 * In particular, eliminate kernel launches where there are obviously
3437 * Use the same block size constraints that are used to create the context
3438 * to ensure that all constraints implicit in the constructed context
3439 * are imposed by the guard.
3441 * Additionally, add other constraints that are valid
3442 * for each executed instance ("context"), as long as this does not result
3445 static __isl_give isl_schedule_node
*insert_guard(
3446 __isl_take isl_schedule_node
*node
, __isl_keep isl_set
*context
,
3447 __isl_keep isl_multi_pw_aff
*size
, struct ppcg_scop
*scop
)
3453 guard
= isl_set_copy(context
);
3454 guard
= isl_set_compute_divs(guard
);
3455 guard
= isl_set_from_basic_set(isl_set_simple_hull(guard
));
3457 nparam
= isl_set_dim(guard
, isl_dim_param
);
3458 n
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
3459 ids
= ppcg_scop_generate_names(scop
, n
, "__ppcg_tmp");
3460 guard
= add_bounded_parameters_dynamic(guard
, size
, ids
);
3461 isl_id_list_free(ids
);
3462 guard
= isl_set_project_out(guard
, isl_dim_param
, nparam
, n
);
3464 node
= isl_schedule_node_insert_guard(node
, guard
);
3469 /* Does any array reference group mapping require the band that is mapped
3470 * to threads to be unrolled?
3472 static int kernel_requires_unroll(struct ppcg_kernel
*kernel
)
3476 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3477 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3479 for (j
= 0; j
< array
->n_group
; ++j
) {
3480 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3481 if (gpu_array_ref_group_requires_unroll(group
))
3489 /* Mark the given band node "node" for unrolling by the AST generator and
3490 * then sink it to the leaves of the schedule tree.
3491 * All dimensions of "node" are assumed to be coincident, such that this
3492 * sinking is a valid operation.
3494 static __isl_give isl_schedule_node
*unroll(__isl_take isl_schedule_node
*node
)
3498 n
= isl_schedule_node_band_n_member(node
);
3499 for (i
= 0; i
< n
; ++i
)
3500 node
= isl_schedule_node_band_member_set_ast_loop_type(node
, i
,
3501 isl_ast_loop_unroll
);
3503 node
= isl_schedule_node_band_sink(node
);
3508 /* Is there any write in "kernel" that writes directly to global memory?
3509 * That is, is there any array reference group that involves a write and
3510 * that is not mapped to private or shared memory?
3512 static int any_global_write(struct ppcg_kernel
*kernel
)
3516 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3517 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3519 for (j
= 0; j
< array
->n_group
; ++j
) {
3520 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3524 if (group
->private_tile
|| group
->shared_tile
)
3533 /* Insert a synchronization node in the schedule tree of "node"
3534 * after the core computation of "kernel" at the level of the band
3535 * that is mapped to threads, except if that level is equal to
3536 * that of the band that is mapped to blocks.
3537 * "node" is assumed to point to the kernel node.
3539 static __isl_give isl_schedule_node
*add_sync(struct ppcg_kernel
*kernel
,
3540 __isl_take isl_schedule_node
*node
)
3544 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3546 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
3547 if (kernel_depth
== isl_schedule_node_get_schedule_depth(node
))
3548 return gpu_tree_move_up_to_kernel(node
);
3550 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3552 node
= gpu_tree_move_up_to_kernel(node
);
3557 /* Return a read ("read" is 1) or write access relation for "group"
3558 * with those accesses removed that are only needed to communicate data
3559 * within the subtree of the schedule rooted at "node".
3560 * Furthermore, include the prefix schedule at "node".
3561 * That is, return a relation of the form
3565 * with D the outer schedule dimensions at "node".
3567 static __isl_give isl_union_map
*anchored_non_local_accesses(
3568 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3569 __isl_take isl_schedule_node
*node
, int read
)
3571 isl_union_map
*access
;
3572 isl_union_map
*prefix
;
3574 access
= gpu_array_ref_group_access_relation(group
, read
, !read
);
3575 access
= remove_local_accesses_group(kernel
, group
, access
, node
, read
);
3576 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
3577 access
= isl_union_map_range_product(prefix
, access
);
3582 /* Given an array reference group "group", create a mapping
3584 * read[D -> A] -> [D -> A]
3586 * if "read" is set or
3588 * write[D -> A] -> [D -> A]
3590 * if "read" is not set.
3591 * D corresponds to the outer group->depth dimensions of
3592 * the kernel schedule.
3594 static __isl_give isl_multi_aff
*create_from_access(isl_ctx
*ctx
,
3595 struct gpu_array_ref_group
*group
, int read
)
3600 space
= isl_space_copy(group
->array
->space
);
3601 space
= isl_space_from_range(space
);
3602 space
= isl_space_add_dims(space
, isl_dim_in
, group
->depth
);
3603 space
= isl_space_wrap(space
);
3604 space
= isl_space_map_from_set(space
);
3606 id
= isl_id_alloc(ctx
, read
? "read" : "write", group
);
3607 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
3609 return isl_multi_aff_identity(space
);
3612 /* Add copy statements to the schedule tree of "node"
3613 * for reading from global memory to private memory (if "read" is set) or
3614 * for writing back from private memory to global memory
3615 * (if "read" is not set) for the array reference group "group" that
3616 * is mapped to private memory.
3617 * On input, "node" points to the kernel node, and it is moved
3618 * back there on output.
3620 * The copies are performed in the order of the array elements.
3621 * The copy statement instances include a reference to the outer
3622 * group->depth dimensions of the kernel schedule for ease of
3623 * combining them with the group tiling.
3625 * That is, the extra schedule is of the form
3629 * where D corresponds to the outer group->depth dimensions of
3630 * the kernel schedule and A to the global array.
3631 * This schedule is unrolled because registers are not addressable.
3633 * The copying is inserted in the schedule tree through an extension
3638 * where the extra domain elements type[D -> A] are those accessed
3640 * A filter is inserted on type[D -> A] to ensure that the element
3641 * is read/written by the same thread that needs the element.
3642 * This filter is obtained by applying
3646 * to the thread filter for the core statements.
3648 * The extension is inserted before the core computation in case of a read
3649 * and after the core computation in case of a write.
3650 * In the latter case, we also make sure that there is a synchronization
3651 * node after the write to global memory, unless this write is performed
3652 * at the outer level of the kernel.
3653 * In principle, this synchronization could be inserted higher
3654 * in the schedule tree depending on where the corresponding reads
3655 * from global memory are performed.
3657 static __isl_give isl_schedule_node
*add_copies_group_private(
3658 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3659 __isl_take isl_schedule_node
*node
, int read
)
3661 isl_union_map
*access
;
3662 isl_union_map
*prefix
;
3663 isl_union_set
*domain
;
3665 isl_multi_aff
*from_access
;
3666 isl_multi_pw_aff
*mpa
;
3667 isl_multi_union_pw_aff
*mupa
;
3668 isl_schedule_node
*graft
;
3669 isl_union_set
*filter
;
3673 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3674 node
= gpu_tree_move_down_to_depth(node
, group
->depth
, kernel
->core
);
3676 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3677 empty
= isl_union_map_is_empty(access
);
3678 if (empty
< 0 || empty
) {
3679 isl_union_map_free(access
);
3681 return isl_schedule_node_free(node
);
3682 return gpu_tree_move_up_to_kernel(node
);
3685 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3686 space
= isl_space_domain(isl_multi_aff_get_space(from_access
));
3687 access
= isl_union_map_preimage_range_multi_aff(access
, from_access
);
3689 filter
= isl_union_set_copy(kernel
->thread_filter
);
3690 filter
= isl_union_set_apply(filter
, isl_union_map_copy(access
));
3691 filter
= isl_union_set_detect_equalities(filter
);
3692 filter
= isl_union_set_coalesce(filter
);
3694 domain
= isl_union_map_range(access
);
3695 access
= isl_union_set_wrapped_domain_map(domain
);
3696 access
= isl_union_map_reverse(access
);
3697 access
= isl_union_map_coalesce(access
);
3698 graft
= isl_schedule_node_from_extension(access
);
3700 space
= isl_space_map_from_set(space
);
3701 mpa
= isl_multi_pw_aff_identity(space
);
3702 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
3703 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3705 graft
= isl_schedule_node_child(graft
, 0);
3706 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3707 graft
= unroll(graft
);
3709 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3711 graft
= isl_schedule_node_parent(graft
);
3714 node
= isl_schedule_node_graft_before(node
, graft
);
3716 node
= isl_schedule_node_graft_after(node
, graft
);
3717 if (kernel_depth
< group
->depth
) {
3718 node
= isl_schedule_node_parent(node
);
3719 node
= isl_schedule_node_next_sibling(node
);
3720 node
= isl_schedule_node_child(node
, 0);
3721 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3725 node
= gpu_tree_move_up_to_kernel(node
);
3730 /* Add copy statements to the schedule tree of "node"
3731 * for reading from global memory to shared memory (if "read" is set) or
3732 * for writing back from shared memory to global memory
3733 * (if "read" is not set) for the array reference group "group" that
3734 * is mapped to shared memory.
3735 * On input, "node" points to the kernel node, and it is moved
3736 * back there on output.
3738 * The copies are performed in the order of the corresponding shared
3740 * The copy statement instances include a reference to the outer
3741 * group->depth dimensions of the kernel schedule for ease of
3742 * combining them with the group tiling.
3744 * If we are performing a read from global memory to shared memory and
3745 * if the array involved is not a scalar, then we copy
3746 * the entire tile to shared memory. This may result in some extra
3747 * elements getting copied, but it should lead to simpler code
3748 * (which means that fewer registers may be needed) and less divergence.
3750 * Otherwise, we only copy the elements that will be read or have been written
3753 * That is, the extra schedule is of the form
3757 * where D corresponds to the outer group->depth dimensions of
3758 * the kernel schedule, A to the global array and T is the corresponding
3759 * shared memory tile.
3761 * The copying is inserted in the schedule tree through an extension
3766 * where the extra domain elements type[D -> A] are those accessed
3767 * by the group. In the case of read from a non-scalar, this set
3768 * is replaced by the entire shared memory tile.
3770 * A filter is inserted on type[D -> A] to map the copy instances
3771 * to the threads. In particular, the thread identifiers are
3772 * equated to the position inside the shared memory tile (T)
3773 * modulo the block size.
3774 * We try to align the innermost tile dimension with the innermost
3775 * thread identifier (x) as a heuristic to improve coalescing.
3776 * In particular, if the dimension of the tile is greater than
3777 * the dimension of the block, then the schedule mapping to the tile
3778 * is broken up into two pieces and the filter is applied to the inner part.
3779 * If, on the other hand, the dimension of the tile is smaller than
3780 * the dimension of the block, then the initial thread identifiers
3781 * are equated to zero and the remaining thread identifiers are
3782 * matched to the memory tile.
3784 * The extension is inserted before the core computation in case of a read
3785 * and after the core computation in case of a write.
3786 * In the case of a read, we first need to make sure there is some
3787 * synchronization before the core computation such that we can put the read
3788 * from global memory to shared memory before that synchronization.
3789 * This ensures that all threads have finished copying into shared memory
3790 * before the shared memory is used.
3791 * We also need to make sure that there is a synchronization node after
3792 * the core computation to ensure that the next load into shared memory
3793 * only happens after all data has been used. There is no need for
3794 * this synchronization if we are at the outer level since then there
3795 * won't be a next load.
3796 * In the case of a write, we need to make sure there is some synchronization
3797 * after the core computation such taht we can put the write from shared
3798 * memory to global memory after that synchronization.
3799 * Unless we are at the outer level, we also need a synchronization node
3800 * after the write to ensure the data is saved to global memory
3801 * before the next iteration write to the same shared memory.
3802 * It also makes sure the data has arrived in global memory before
3803 * it is read in a subsequent iteration.
3805 static __isl_give isl_schedule_node
*add_copies_group_shared(
3806 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3807 __isl_take isl_schedule_node
*node
, int read
)
3809 struct gpu_array_tile
*tile
;
3810 isl_union_map
*access
;
3811 isl_union_set
*domain
;
3812 isl_union_set
*sync
;
3814 isl_multi_aff
*from_access
;
3815 isl_multi_pw_aff
*mpa
;
3816 isl_multi_union_pw_aff
*mupa
;
3817 isl_schedule_node
*graft
;
3818 isl_union_set
*filter
;
3823 kernel_depth
= isl_schedule_node_get_schedule_depth(node
);
3824 node
= gpu_tree_move_down_to_depth(node
, group
->depth
, kernel
->core
);
3826 access
= anchored_non_local_accesses(kernel
, group
, node
, read
);
3827 empty
= isl_union_map_is_empty(access
);
3828 if (empty
< 0 || empty
) {
3829 isl_union_map_free(access
);
3831 return isl_schedule_node_free(node
);
3832 return gpu_tree_move_up_to_kernel(node
);
3835 from_access
= create_from_access(kernel
->ctx
, group
, read
);
3837 tile
= gpu_array_ref_group_tile(group
);
3838 ma
= isl_multi_aff_copy(tile
->tiling
);
3839 ma
= isl_multi_aff_pullback_multi_aff(ma
,
3840 isl_multi_aff_copy(from_access
));
3841 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
3842 mupa
= isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
3844 domain
= isl_union_map_range(access
);
3846 if (read
&& !gpu_array_is_scalar(group
->array
)) {
3848 isl_union_set_free(domain
);
3849 map
= group_tile(group
);
3850 domain
= isl_union_set_from_set(isl_map_wrap(map
));
3853 domain
= isl_union_set_preimage_multi_aff(domain
, from_access
);
3854 access
= isl_union_set_wrapped_domain_map(domain
);
3855 access
= isl_union_map_reverse(access
);
3856 access
= isl_union_map_coalesce(access
);
3857 graft
= isl_schedule_node_from_extension(access
);
3859 graft
= isl_schedule_node_child(graft
, 0);
3861 graft
= isl_schedule_node_insert_partial_schedule(graft
, mupa
);
3863 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0) {
3864 graft
= isl_schedule_node_band_split(graft
,
3865 tile
->n
- kernel
->n_block
);
3866 graft
= isl_schedule_node_child(graft
, 0);
3868 if (tile
->n
< kernel
->n_block
)
3869 skip
= kernel
->n_block
- tile
->n
;
3872 filter
= set_schedule_modulo(graft
, kernel
->thread_ids
,
3874 if (!kernel
->options
->wrap
)
3875 graft
= snap_band_to_sizes(graft
, kernel
->block_dim
+ skip
,
3877 if (tile
->n
> kernel
->n_block
&& kernel
->n_block
> 0)
3878 graft
= isl_schedule_node_parent(graft
);
3879 graft
= isl_schedule_node_insert_filter(graft
, filter
);
3881 while (graft
&& isl_schedule_node_has_parent(graft
))
3882 graft
= isl_schedule_node_parent(graft
);
3885 if (kernel_depth
< group
->depth
)
3886 node
= gpu_tree_ensure_sync_after_core(node
, kernel
);
3887 node
= gpu_tree_move_left_to_sync(node
, kernel
);
3888 node
= isl_schedule_node_graft_before(node
, graft
);
3890 node
= gpu_tree_move_right_to_sync(node
, kernel
);
3891 node
= isl_schedule_node_graft_after(node
, graft
);
3892 node
= isl_schedule_node_parent(node
);
3893 node
= isl_schedule_node_next_sibling(node
);
3894 node
= isl_schedule_node_child(node
, 0);
3895 if (kernel_depth
< group
->depth
)
3896 node
= gpu_tree_ensure_following_sync(node
, kernel
);
3899 node
= gpu_tree_move_up_to_kernel(node
);
3904 /* Check whether the array reference group "group" is mapped to
3905 * private or shared memory and, if so,
3906 * add copy statements to the schedule tree of "node"
3907 * for reading from global memory to private or shared memory
3908 * (if "read" is set) or for writing back from private or shared memory
3909 * to global memory (if "read" is not set) for this group.
3910 * On input, "node" points to the kernel node, and it is moved
3911 * back there on output.
3913 static __isl_give isl_schedule_node
*add_copies_group(
3914 struct ppcg_kernel
*kernel
, struct gpu_array_ref_group
*group
,
3915 __isl_take isl_schedule_node
*node
, int read
)
3917 if (group
->private_tile
)
3918 return add_copies_group_private(kernel
, group
, node
, read
);
3919 if (group
->shared_tile
)
3920 return add_copies_group_shared(kernel
, group
, node
, read
);
3924 /* For each array reference group that is mapped to private or shared memory,
3925 * add copy statements to the schedule tree of "node"
3926 * for reading from global memory to private or shared memory
3927 * and for writing back.
3928 * On input, "node" points to the kernel node, and it is moved
3929 * back there on output.
3931 static __isl_give isl_schedule_node
*add_copies(struct ppcg_kernel
*kernel
,
3932 __isl_take isl_schedule_node
*node
)
3936 for (i
= 0; i
< kernel
->n_array
; ++i
) {
3937 struct gpu_local_array_info
*array
= &kernel
->array
[i
];
3939 for (j
= 0; j
< array
->n_group
; ++j
) {
3940 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3942 node
= add_copies_group(kernel
, group
, node
, 1);
3945 node
= add_copies_group(kernel
, group
, node
, 0);
3954 /* Mark all dimensions in the current band node atomic.
3956 static __isl_give isl_schedule_node
*atomic(__isl_take isl_schedule_node
*node
)
3960 n
= isl_schedule_node_band_n_member(node
);
3961 for (i
= 0; i
< n
; ++i
)
3962 node
= isl_schedule_node_band_member_set_ast_loop_type(node
, i
,
3963 isl_ast_loop_atomic
);
3968 /* Mark "node" atomic, if it is a band node.
3969 * Do the same for all ancestors.
3970 * Return a pointer to "node" (in the updated schedule tree).
3972 static __isl_give isl_schedule_node
*atomic_ancestors(
3973 __isl_take isl_schedule_node
*node
)
3979 if (!isl_schedule_node_has_parent(node
))
3982 pos
= isl_schedule_node_get_child_position(node
);
3983 node
= isl_schedule_node_parent(node
);
3984 if (isl_schedule_node_get_type(node
) == isl_schedule_node_band
)
3985 node
= atomic(node
);
3986 node
= atomic_ancestors(node
);
3987 node
= isl_schedule_node_child(node
, pos
);
3992 /* Group the domain elements into a single space, named kernelX,
3993 * with X the kernel sequence number "kernel_id".
3995 static __isl_give isl_schedule_node
*group_statements(
3996 __isl_take isl_schedule_node
*node
, int kernel_id
)
4004 snprintf(buffer
, sizeof(buffer
), "kernel%d", kernel_id
);
4005 id
= isl_id_alloc(isl_schedule_node_get_ctx(node
), buffer
, NULL
);
4006 return isl_schedule_node_group(node
, id
);
4009 /* Create a ppcg_kernel representing the domain instances that reach "node"
4010 * and insert a mark node pointing to the ppcg_kernel before "node".
4011 * The band that "node" points to is the band that needs to be mapped
4012 * to block identifiers. The band that needs to be mapped to thread
4013 * identifiers should be marked by a "thread" mark by the caller.
4014 * This mark is removed by this function.
4015 * If "scale" is set, then the band that "node" points to is scaled
4018 * Mark all outer band nodes as atomic to ensure each kernel is only
4020 * If the domain elements that reach "node" live in more than one space,
4021 * then group the domain elements into a single space, named kernelX,
4022 * with X the kernel sequence number.
4024 * Insert a guard node governing the kernel node to ensure that
4025 * no kernels with zero blocks are launched.
4027 * Insert a context node describing the block and thread
4028 * identifiers inside the kernel mark.
4029 * The context node needs to be inserted after the effective block size
4030 * has been determined such that the bounds on the thread identifiers
4031 * would reflect the effective block size.
4032 * Insert a filter node inside the context node mapping the statement
4033 * instances to block identifiers. In particular, the block identifiers
4034 * are equated to the partial schedule of band that was marked for mapping
4035 * to blocks modulo the grid size.
4036 * Insert a filter node inside the "thread" mark mapping the statement
4037 * instances to thread identifiers. In particular, the thread identifiers
4038 * are equated to the partial schedule of band that was marked for mapping
4039 * to threads modulo the block size.
4041 * Compute array reference groups for all arrays, set the local
4042 * array bounds based on the set of domain instances that reach
4043 * the kernel node, check the total amount of shared memory used
4044 * and compute all group tilings.
4045 * The array reference groups are computed after the block filter
4046 * has been inserted because it affects the mapping to shared or
4047 * private memory. This computation also requires the thread filter
4048 * (in the ppcg_kernel object), but this thread filter should not
4049 * have been added to the schedule tree yet since the computation
4050 * requires the schedule of the band that needs to be mapped to
4051 * threads before the privatization is applied.
4053 * If any array reference group requires the band mapped to threads
4054 * to be unrolled, then we perform the required unrolling.
4056 * We save a copy of the schedule that may influence the mappings
4057 * to shared or private memory in kernel->shared_schedule.
4059 * Finally, we add synchronization and copy statements to the schedule tree,
4060 * remove the "thread" mark and create representations for the local
4061 * variables in the kernel.
4063 * Store a pointer to the created ppcg_kernel in gen->kernel.
4065 * We keep a copy of the isl_id that points to the kernel to ensure
4066 * that the kernel does not get destroyed if the schedule node
4067 * is freed due to some error condition.
4069 static __isl_give isl_schedule_node
*create_kernel(struct gpu_gen
*gen
,
4070 __isl_take isl_schedule_node
*node
, int scale
,
4071 __isl_keep isl_multi_val
*sizes
)
4073 struct ppcg_kernel
*kernel
;
4075 isl_schedule_node
*node_thread
;
4076 isl_union_map
*host_schedule
;
4077 isl_set
*host_domain
;
4078 isl_union_set
*domain
;
4079 int single_statement
;
4081 kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
4082 kernel
= ppcg_kernel_create_local_arrays(kernel
, gen
->prog
);
4084 return isl_schedule_node_free(node
);
4086 domain
= isl_schedule_node_get_domain(node
);
4087 single_statement
= isl_union_set_n_set(domain
) == 1;
4089 kernel
->ctx
= gen
->ctx
;
4090 kernel
->prog
= gen
->prog
;
4091 kernel
->options
= gen
->options
;
4092 kernel
->context
= extract_context(node
, gen
->prog
);
4093 kernel
->core
= isl_union_set_universe(isl_union_set_copy(domain
));
4094 kernel
->arrays
= accessed_by_domain(isl_union_set_copy(domain
),
4096 kernel
->tile_len
= isl_schedule_node_band_n_member(node
);
4097 kernel
->n_parallel
= n_outer_coincidence(node
);
4098 kernel
->n_grid
= kernel
->n_parallel
;
4099 node_thread
= isl_schedule_node_copy(node
);
4100 node_thread
= gpu_tree_move_down_to_thread(node_thread
, kernel
->core
);
4101 node_thread
= isl_schedule_node_child(node_thread
, 0);
4102 kernel
->n_block
= n_outer_coincidence(node_thread
);
4103 isl_schedule_node_free(node_thread
);
4104 kernel
->id
= gen
->kernel_id
++;
4105 read_grid_and_block_sizes(kernel
, gen
);
4107 gen
->kernel
= kernel
;
4109 host_schedule
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4110 host_domain
= isl_set_from_union_set(isl_union_map_range(
4113 node
= atomic_ancestors(node
);
4115 id
= isl_id_alloc(gen
->ctx
, "kernel", kernel
);
4116 id
= isl_id_set_free_user(id
, &ppcg_kernel_free_wrap
);
4117 node
= isl_schedule_node_insert_mark(node
, isl_id_copy(id
));
4119 if (!single_statement
)
4120 node
= group_statements(node
, kernel
->id
);
4122 node
= isl_schedule_node_child(node
, 0);
4123 node
= split_band(node
, kernel
->n_grid
);
4124 kernel
->block_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
4125 kernel
->n_grid
, "b");
4126 kernel
->block_filter
= set_schedule_modulo(node
, kernel
->block_ids
,
4128 kernel
->grid_size
= extract_grid_size(kernel
,
4129 isl_union_set_copy(domain
));
4130 if (!kernel
->options
->wrap
)
4131 node
= snap_band_to_sizes(node
, kernel
->grid_dim
,
4134 node
= scale_band(node
, isl_multi_val_copy(sizes
));
4135 node
= isl_schedule_node_parent(node
);
4136 if (!single_statement
)
4137 node
= isl_schedule_node_parent(node
);
4138 node
= insert_guard(node
, kernel
->context
, kernel
->grid_size
,
4140 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
4141 node
= isl_schedule_node_child(node
, 0);
4142 node
= split_band(node
, kernel
->n_block
);
4143 kernel
->thread_ids
= ppcg_scop_generate_names(gen
->prog
->scop
,
4144 kernel
->n_block
, "t");
4145 kernel
->thread_filter
= set_schedule_modulo(node
, kernel
->thread_ids
,
4147 extract_block_size(kernel
, domain
);
4149 node
= gpu_tree_move_up_to_kernel(node
);
4150 node
= isl_schedule_node_child(node
, 0);
4151 node
= insert_context(kernel
, node
);
4152 node
= isl_schedule_node_child(node
, 0);
4153 node
= isl_schedule_node_insert_filter(node
,
4154 isl_union_set_copy(kernel
->block_filter
));
4156 node
= gpu_tree_move_up_to_kernel(node
);
4158 if (gpu_group_references(kernel
, node
) < 0)
4159 node
= isl_schedule_node_free(node
);
4160 localize_bounds(kernel
, host_domain
);
4161 isl_set_free(host_domain
);
4163 check_shared_memory_bound(kernel
);
4164 compute_group_tilings(kernel
);
4166 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
4167 node
= isl_schedule_node_child(node
, 0);
4168 if (!kernel
->options
->wrap
)
4169 node
= snap_band_to_sizes(node
, kernel
->block_dim
,
4171 node
= isl_schedule_node_insert_filter(node
,
4172 isl_union_set_copy(kernel
->thread_filter
));
4173 if (kernel_requires_unroll(kernel
)) {
4174 node
= isl_schedule_node_child(node
, 0);
4175 node
= unroll(node
);
4178 node
= gpu_tree_move_up_to_thread(node
);
4179 kernel
->shared_schedule_dim
=
4180 isl_schedule_node_get_schedule_depth(node
);
4181 kernel
->shared_schedule
=
4182 isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node
);
4184 node
= gpu_tree_move_up_to_kernel(node
);
4186 if (any_global_write(kernel
))
4187 node
= add_sync(kernel
, node
);
4188 node
= add_copies(kernel
, node
);
4190 node
= gpu_tree_move_down_to_thread(node
, kernel
->core
);
4191 node
= isl_schedule_node_delete(node
);
4193 node
= gpu_tree_move_up_to_kernel(node
);
4195 create_kernel_vars(kernel
);
4197 if (!single_statement
)
4198 node
= isl_schedule_node_parent(node
);
4199 node
= isl_schedule_node_parent(node
);
4205 /* Insert a zero-dimensional permutable band at "node".
4207 static __isl_give isl_schedule_node
*insert_empty_permutable_band(
4208 __isl_take isl_schedule_node
*node
)
4211 isl_schedule
*schedule
;
4212 isl_union_set
*domain
;
4213 isl_multi_union_pw_aff
*mupa
;
4215 schedule
= isl_schedule_node_get_schedule(node
);
4216 domain
= isl_schedule_get_domain(schedule
);
4217 space
= isl_union_set_get_space(domain
);
4218 isl_union_set_free(domain
);
4219 isl_schedule_free(schedule
);
4221 space
= isl_space_set_from_params(space
);
4222 mupa
= isl_multi_union_pw_aff_zero(space
);
4223 node
= isl_schedule_node_insert_partial_schedule(node
, mupa
);
4224 node
= isl_schedule_node_band_set_permutable(node
, 1);
4229 /* Mark "node" as outer permutable.
4231 * If "node" originally points to a leaf, then insert a zero-dimensional
4232 * permutable band such that we can assume that "node" always
4233 * points to a band node.
4235 * Tile "node" using user specified tile sizes, after splitting the band
4236 * if the number of specified tile sizes is smaller than the dimension
4237 * of the band. Mark the point band of this tiling as the band that
4238 * needs to be mapped to threads.
4239 * Create a kernel representing the domain instances that reach "node" and
4240 * insert a mark node pointing to the ppcg_kernel before the band node.
4242 static __isl_give isl_schedule_node
*mark_outer_permutable(
4243 struct gpu_gen
*gen
, __isl_take isl_schedule_node
*node
)
4245 struct ppcg_kernel
*kernel
;
4250 isl_multi_val
*sizes
;
4252 if (isl_schedule_node_get_type(node
) == isl_schedule_node_leaf
)
4253 node
= insert_empty_permutable_band(node
);
4255 tile_len
= isl_schedule_node_band_n_member(node
);
4256 tile_size
= read_tile_sizes(gen
, &tile_len
);
4258 return isl_schedule_node_free(node
);
4259 if (tile_len
< isl_schedule_node_band_n_member(node
))
4260 node
= isl_schedule_node_band_split(node
, tile_len
);
4261 sizes
= construct_band_tiles_sizes(node
, tile_size
);
4262 node
= tile_band(node
, isl_multi_val_copy(sizes
));
4263 node
= isl_schedule_node_child(node
, 0);
4264 id
= isl_id_alloc(gen
->ctx
, "thread", NULL
);
4265 node
= isl_schedule_node_insert_mark(node
, id
);
4266 node
= isl_schedule_node_parent(node
);
4268 scale
= gen
->options
->scale_tile_loops
;
4269 node
= create_kernel(gen
, node
, scale
, sizes
);
4270 isl_multi_val_free(sizes
);
4273 kernel
= gen
->kernel
;
4274 kernel
->tile_len
= tile_len
;
4275 kernel
->tile_size
= tile_size
;
4280 static __isl_give isl_schedule_node
*select_outer_band(struct gpu_gen
*gen
,
4281 __isl_take isl_schedule_node
*node
, int pos
, struct band_info
*info
);
4283 /* Check if this band node is tilable and has any parallel loops. If so,
4284 * take it as the outermost tilable band. If not, continue looking for the
4285 * outermost tilable band in the children of the current band.
4286 * Return a pointer to the same node in a tree where all outermost tilable
4287 * bands in the current subtree have been replaced by mark nodes
4288 * containing a pointer to a ppcg_kernel object.
4290 static __isl_give isl_schedule_node
*band_select_outer_band(struct gpu_gen
*gen
,
4291 __isl_take isl_schedule_node
*node
, int pos
, struct band_info
*info
)
4293 int n
= isl_schedule_node_band_n_member(node
);
4296 n_parallel
= n_outer_coincidence(node
);
4298 if (!isl_schedule_node_band_get_permutable(node
) || n_parallel
== 0) {
4299 node
= isl_schedule_node_child(node
, 0);
4300 node
= select_outer_band(gen
, node
, pos
+ n
, info
);
4301 return isl_schedule_node_parent(node
);
4304 gen
->any_parallelism
= 1;
4306 info
->tile_first
= pos
;
4307 info
->prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4308 info
->suffix
= isl_schedule_node_get_subtree_schedule_union_map(node
);
4310 node
= mark_outer_permutable(gen
, node
);
4315 /* Extend "umap" with coordinates with fixed value "val"
4316 * to a total length of "dst_len", assuming the original dimension is "src_len".
4318 static __isl_give isl_union_map
*extend_range(
4319 __isl_take isl_union_map
*umap
, int src_len
, int dst_len
, int val
)
4325 dim
= isl_union_map_get_space(umap
);
4326 map
= isl_map_reverse(projection(dim
, dst_len
, src_len
));
4327 for (i
= src_len
; i
< dst_len
; ++i
)
4328 map
= isl_map_fix_si(map
, isl_dim_out
, i
, val
);
4330 umap
= isl_union_map_apply_range(umap
, isl_union_map_from_map(map
));
4335 /* Select the outermost bands in the elements of the sequence or set
4336 * node "node", align their prefix schedules and combine the resulting
4337 * prefix and suffix schedules into a single pair of prefix and
4338 * suffix schedules for the entire list.
4339 * Return a pointer to the same node in a tree where all outermost tilable
4340 * bands in the current subtree have been replaced by mark nodes
4341 * containing a pointer to a ppcg_kernel object.
4343 static __isl_give isl_schedule_node
*list_select_outer_band(
4344 struct gpu_gen
*gen
, __isl_take isl_schedule_node
*node
, int pos
,
4345 struct band_info
*list_info
)
4348 int n
= isl_schedule_node_n_children(node
);
4349 isl_ctx
*ctx
= isl_schedule_node_get_ctx(node
);
4350 struct band_info
*info
;
4352 isl_union_map
*prefix
;
4353 isl_union_map
*suffix
;
4356 info
= isl_calloc_array(ctx
, struct band_info
, n
);
4360 for (i
= 0; i
< n
; ++i
) {
4361 node
= isl_schedule_node_child(node
, i
);
4362 node
= select_outer_band(gen
, node
, pos
, &info
[i
]);
4363 if (info
[i
].tile_first
> max_tile_first
)
4364 max_tile_first
= info
[i
].tile_first
;
4365 node
= isl_schedule_node_parent(node
);
4368 for (i
= 0; i
< n
; ++i
) {
4369 if (info
[i
].tile_first
== max_tile_first
)
4371 info
[i
].prefix
= extend_range(info
[i
].prefix
,
4372 info
[i
].tile_first
, max_tile_first
, 0);
4373 info
[i
].tile_first
= max_tile_first
;
4376 prefix
= info
[0].prefix
;
4377 suffix
= info
[0].suffix
;
4379 for (i
= 1; i
< n
; ++i
) {
4380 prefix
= isl_union_map_union(prefix
, info
[i
].prefix
);
4381 suffix
= isl_union_map_union(suffix
, info
[i
].suffix
);
4384 list_info
->tile_first
= info
[0].tile_first
;
4385 list_info
->prefix
= prefix
;
4386 list_info
->suffix
= suffix
;
4392 /* If we reach a leaf node, then we have not found any outer tilable
4393 * band with parallel loops, so consider the leaf node as the outermost
4395 * Return a pointer to a mark node containing a pointer
4396 * to a ppcg_kernel object inserted at the original leaf node.
4398 static __isl_give isl_schedule_node
*leaf_select_outer_band(struct gpu_gen
*gen
,
4399 __isl_take isl_schedule_node
*node
, int pos
, struct band_info
*info
)
4402 info
->tile_first
= pos
;
4403 info
->prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
4404 info
->suffix
= isl_schedule_node_get_subtree_schedule_union_map(node
);
4406 node
= mark_outer_permutable(gen
, node
);
4411 /* Select the outermost tilable band in the subtree that "node" points to and
4412 * return a pointer to the same node in a tree where all outermost tilable
4413 * bands in the current subtree have been replaced by mark nodes
4414 * containing a pointer to a ppcg_kernel object.
4416 static __isl_give isl_schedule_node
*select_outer_band(struct gpu_gen
*gen
,
4417 __isl_take isl_schedule_node
*node
, int pos
, struct band_info
*info
)
4419 enum isl_schedule_node_type type
;
4421 type
= isl_schedule_node_get_type(node
);
4423 case isl_schedule_node_domain
:
4424 case isl_schedule_node_filter
:
4425 node
= isl_schedule_node_child(node
, 0);
4426 node
= select_outer_band(gen
, node
, pos
, info
);
4427 return isl_schedule_node_parent(node
);
4428 case isl_schedule_node_leaf
:
4429 return leaf_select_outer_band(gen
, node
, pos
, info
);
4430 case isl_schedule_node_band
:
4431 return band_select_outer_band(gen
, node
, pos
, info
);
4432 case isl_schedule_node_set
:
4433 case isl_schedule_node_sequence
:
4434 return list_select_outer_band(gen
, node
, pos
, info
);
4436 isl_die(isl_schedule_node_get_ctx(node
),
4437 isl_error_unsupported
, "unhandled schedule node type",
4439 case isl_schedule_node_error
:
4440 info
->prefix
= NULL
;
4441 info
->suffix
= NULL
;
4445 return isl_schedule_node_free(node
);
4448 /* Select the outermost tilable band that (by construction)
4449 * has at least one parallel loop.
4450 * The starting position of the aligned band is stored in the pair
4452 * The sizes and number of parallel loops may be different in different
4453 * parts of the band forest and are therefore stored in the gpu_stmts.
4455 * Return the complete schedule, with the tilable bands aligned
4456 * at gen->tile_first and padded with zero, if needed.
4457 * Store a schedule tree corresponding to the outer gen->tile_first
4458 * dimensions, with mark nodes containing pointers to ppcg_kernel objects,
4461 static __isl_give isl_union_map
*select_outer_tilable_band(struct gpu_gen
*gen
,
4462 __isl_keep isl_schedule
*schedule
)
4464 isl_schedule_node
*node
;
4465 struct band_info info
;
4467 node
= isl_schedule_get_root(schedule
);
4468 node
= select_outer_band(gen
, node
, 0, &info
);
4469 gen
->schedule
= isl_schedule_node_get_schedule(node
);
4470 isl_schedule_node_free(node
);
4472 gen
->tile_first
= info
.tile_first
;
4473 info
.suffix
= align_range(info
.suffix
);
4475 return isl_union_map_flat_range_product(info
.prefix
, info
.suffix
);
4478 /* Set gen->untiled_len to the number of scheduling dimensions
4479 * for the schedule of the first domain.
4480 * We assume here that this number is the same for all domains.
4482 static int set_untiled_len(__isl_take isl_map
*map
, void *user
)
4484 unsigned *untiled_len
= user
;
4486 *untiled_len
= isl_map_dim(map
, isl_dim_out
);
4492 /* Compute an appropriate schedule based on the accesses in
4493 * gen->read and gen->write.
4495 * We use the dependences in gen->prog->scop to compute
4496 * a schedule that has a parallel loop in each tilable band.
4497 * Finally, we select the outermost tilable band.
4499 * If live range reordering is allowed, then we need to make sure
4500 * that live ranges on arrays are not run in parallel since doing
4501 * so would require array expansion. We therefore add the array
4502 * order dependences to the coincidence dependences. Non-zero array
4503 * order dependences will then prevent a schedule dimension from being
4504 * considered parallel.
4505 * Live ranges derived from scalars are allowed to be run in parallel
4506 * since we force the scalars to be mapped to private memory in
4507 * check_scalar_live_ranges.
4508 * If live range reordering is allowed, then the false dependences
4509 * are not added to the validity constraints as that would prevent
4510 * reordering. Instead, the external false dependences that enforce that reads
4511 * from potentially live-in data precede any later write and
4512 * that writes of potentially live-out data follow any other earlier write
4513 * are added to the validity and the coincidence constraints.
4514 * The false dependences are still added to the proximity constraints
4515 * for consistency with the case where live range reordering is not allowed.
4516 * The coincidence constraints then consist of flow dependences,
4517 * external false dependences and array order dependences.
4518 * The independences can be filtered out from the first two sets.
4519 * They have already been filtered out from the array order dependences
4520 * on a per array basis in collect_order_dependences.
4521 * There is no need for a per array handling of the other two sets
4522 * as there should be no flow or external false dependence on local
4523 * variables that can be filtered out.
4525 static void compute_schedule(struct gpu_gen
*gen
)
4527 isl_union_set
*domain
;
4528 isl_union_map
*dep_raw
, *dep
;
4529 isl_union_map
*validity
, *proximity
, *coincidence
;
4530 isl_union_map
*sched
;
4531 isl_schedule_constraints
*sc
;
4532 isl_schedule
*schedule
;
4534 domain
= isl_union_set_copy(gen
->prog
->scop
->domain
);
4535 sc
= isl_schedule_constraints_on_domain(isl_union_set_copy(domain
));
4536 sc
= isl_schedule_constraints_set_context(sc
,
4537 isl_set_copy(gen
->prog
->scop
->context
));
4538 if (gen
->options
->live_range_reordering
) {
4539 sc
= isl_schedule_constraints_set_conditional_validity(sc
,
4540 isl_union_map_copy(gen
->prog
->scop
->tagged_dep_flow
),
4541 isl_union_map_copy(gen
->prog
->scop
->tagged_dep_order
));
4542 proximity
= isl_union_map_copy(gen
->prog
->scop
->dep_flow
);
4543 validity
= isl_union_map_copy(proximity
);
4544 validity
= isl_union_map_union(validity
,
4545 isl_union_map_copy(gen
->prog
->scop
->dep_forced
));
4546 proximity
= isl_union_map_union(proximity
,
4547 isl_union_map_copy(gen
->prog
->scop
->dep_false
));
4548 coincidence
= isl_union_map_copy(validity
);
4549 coincidence
= isl_union_map_subtract(coincidence
,
4550 isl_union_map_copy(gen
->prog
->scop
->independence
));
4551 coincidence
= isl_union_map_union(coincidence
,
4552 isl_union_map_copy(gen
->prog
->array_order
));
4554 dep_raw
= isl_union_map_copy(gen
->prog
->scop
->dep_flow
);
4555 dep
= isl_union_map_copy(gen
->prog
->scop
->dep_false
);
4556 dep
= isl_union_map_union(dep
, dep_raw
);
4557 dep
= isl_union_map_coalesce(dep
);
4558 proximity
= isl_union_map_copy(dep
);
4559 coincidence
= isl_union_map_copy(dep
);
4562 sc
= isl_schedule_constraints_set_validity(sc
, validity
);
4563 sc
= isl_schedule_constraints_set_coincidence(sc
, coincidence
);
4564 sc
= isl_schedule_constraints_set_proximity(sc
, proximity
);
4566 if (gen
->options
->debug
->dump_schedule_constraints
)
4567 isl_schedule_constraints_dump(sc
);
4568 schedule
= isl_schedule_constraints_compute_schedule(sc
);
4569 if (gen
->options
->debug
->dump_schedule
)
4570 isl_schedule_dump(schedule
);
4572 sched
= select_outer_tilable_band(gen
, schedule
);
4574 isl_union_map_foreach_map(sched
, &set_untiled_len
, &gen
->untiled_len
);
4575 sched
= isl_union_map_intersect_domain(sched
, domain
);
4578 isl_schedule_free(schedule
);
4581 /* Compute the sets of outer array elements that need to be copied in and out.
4583 * In particular, for each array that is possibly written anywhere in
4584 * gen->prog and that is visible outside the corresponding scop,
4585 * we copy out its entire extent.
4587 * Any array elements that is read without first being written needs
4588 * to be copied in. Furthermore, if there are any array elements that
4589 * are copied out, but that may not be written inside gen->prog, then
4590 * they also need to be copied in to ensure that the value after execution
4591 * is the same as the value before execution, at least for those array
4592 * elements that may have their values preserved by the scop.
4593 * In case the array elements are structures, we need to take into
4594 * account that all members of the structures need to be written
4595 * by gen->prog before we can avoid copying the data structure in.
4597 * While computing the set of array elements that are copied out but
4598 * not necessarily written, we intersect both sets with the context.
4599 * This helps in those cases where the arrays are declared with a fixed size,
4600 * while the accesses are parametric and the context assigns a fixed value
4601 * to the parameters.
4603 * If an element from a local array is read without first being written,
4604 * then there is no point in copying it in since it cannot have been
4605 * written prior to the scop. Warn about the uninitialized read instead.
4607 static void compute_copy_in_and_out(struct gpu_gen
*gen
)
4610 isl_union_set
*local
;
4611 isl_union_set
*may_write
, *must_write
;
4612 isl_union_set
*copy_in
, *copy_out
;
4613 isl_union_set
*not_written
;
4614 isl_union_map
*uninitialized
;
4615 isl_union_map
*local_uninitialized
;
4617 must_write
= isl_union_map_range(
4618 isl_union_map_copy(gen
->prog
->must_write
));
4619 must_write
= isl_union_set_intersect_params(must_write
,
4620 isl_set_copy(gen
->prog
->context
));
4621 may_write
= isl_union_map_range(
4622 isl_union_map_copy(gen
->prog
->may_write
));
4623 may_write
= isl_union_set_intersect_params(may_write
,
4624 isl_set_copy(gen
->prog
->context
));
4625 may_write
= isl_union_set_universe(may_write
);
4626 may_write
= isl_union_set_apply(may_write
,
4627 isl_union_map_copy(gen
->prog
->to_outer
));
4628 copy_out
= isl_union_set_empty(isl_union_set_get_space(may_write
));
4629 local
= isl_union_set_copy(copy_out
);
4631 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
4636 space
= isl_space_copy(gen
->prog
->array
[i
].space
);
4638 if (gen
->prog
->array
[i
].local
) {
4641 set
= isl_set_universe(space
);
4642 local
= isl_union_set_add_set(local
, set
);
4646 write_i
= isl_union_set_extract_set(may_write
, space
);
4647 empty
= isl_set_plain_is_empty(write_i
);
4648 isl_set_free(write_i
);
4652 write_i
= isl_set_copy(gen
->prog
->array
[i
].extent
);
4653 copy_out
= isl_union_set_add_set(copy_out
, write_i
);
4655 isl_union_set_free(may_write
);
4657 copy_out
= isl_union_set_intersect_params(copy_out
,
4658 isl_set_copy(gen
->prog
->context
));
4660 gen
->prog
->copy_out
= isl_union_set_copy(copy_out
);
4662 copy_out
= isl_union_set_apply(copy_out
,
4663 isl_union_map_copy(gen
->prog
->to_inner
));
4664 copy_out
= isl_union_set_intersect(copy_out
,
4665 isl_union_set_copy(gen
->prog
->may_persist
));
4666 not_written
= isl_union_set_subtract(copy_out
, must_write
);
4668 uninitialized
= isl_union_map_copy(gen
->prog
->scop
->live_in
);
4669 local_uninitialized
= isl_union_map_copy(uninitialized
);
4671 local
= isl_union_set_apply(local
,
4672 isl_union_map_copy(gen
->prog
->to_inner
));
4673 local_uninitialized
= isl_union_map_intersect_range(local_uninitialized
,
4675 if (!isl_union_map_is_empty(local_uninitialized
)) {
4677 "possibly uninitialized reads (not copied in):\n");
4678 isl_union_map_dump(local_uninitialized
);
4680 uninitialized
= isl_union_map_subtract(uninitialized
,
4681 local_uninitialized
);
4682 copy_in
= isl_union_map_range(uninitialized
);
4683 copy_in
= isl_union_set_union(copy_in
, not_written
);
4684 copy_in
= isl_union_set_apply(copy_in
,
4685 isl_union_map_copy(gen
->prog
->to_outer
));
4687 gen
->prog
->copy_in
= copy_in
;
4690 /* Internal data structure for extract_access.
4691 * "next_access" points to the end of a linked list that is extended
4692 * by extract_access.
4693 * "single_expression" is set if the access expressions belong to
4694 * an expression statement (i.e., a statement without internal control).
4695 * "any_to_outer" maps all intermediate arrays to their outer arrays.
4697 struct ppcg_extract_access_data
{
4698 struct gpu_stmt_access
**next_access
;
4699 int single_expression
;
4700 isl_union_map
*any_to_outer
;
4703 /* Given a tagged access relation to a single array "tagged", extract it
4704 * as a map, taking into account that the input may be empty.
4705 * If the access relation is empty, then it does not contain
4706 * any space information, so we try to recover it from the index
4708 * The space of the index expression is of the form I -> A,
4709 * with I the statement instances and A the array, or [I -> F] -> A,
4710 * with F the filters corresponding to arguments.
4711 * We first drop F, if present, obtaining I -> A.
4712 * Then we construct I -> R, with R the reference tag,
4713 * combine the two into I -> [R -> A] and uncurry to obtain
4714 * the final result [I -> R] -> A.
4715 * Note that the index expression may have a lower dimension
4716 * than that of the array, but this dimension is not used
4717 * if the access relation is empty.
4719 static __isl_give isl_map
*extract_single_tagged_access(
4720 __isl_take isl_union_map
*tagged
, __isl_keep pet_expr
*expr
)
4724 isl_space
*space
, *space2
;
4725 isl_multi_pw_aff
*index
;
4727 empty
= isl_union_map_is_empty(tagged
);
4731 return isl_map_from_union_map(tagged
);
4732 isl_union_map_free(tagged
);
4734 index
= pet_expr_access_get_index(expr
);
4735 space
= isl_multi_pw_aff_get_space(index
);
4736 isl_multi_pw_aff_free(index
);
4737 if (isl_space_domain_is_wrapping(space
))
4738 space
= isl_space_domain_factor_domain(space
);
4739 space2
= isl_space_copy(space
);
4740 space2
= isl_space_from_domain(isl_space_domain(space
));
4741 id
= pet_expr_access_get_ref_id(expr
);
4742 space2
= isl_space_set_tuple_id(space2
, isl_dim_out
, id
);
4743 space
= isl_space_range_product(space2
, space
);
4744 space
= isl_space_uncurry(space
);
4746 return isl_map_empty(space
);
4748 isl_union_map_free(tagged
);
4752 /* Extract a gpu_stmt_access from "expr", append it to the list
4753 * that ends in *data->next_access and update the end of the list.
4754 * If the access expression performs a write, then it is considered
4755 * exact only if it appears in a single expression statement and
4756 * if its may access relation is equal to its must access relation.
4758 * The combined set of may accesses may be union if member accesses
4759 * are involved, but the entire set is derived from a single reference and
4760 * therefore from a single index expression. These accesses therefore
4761 * all map to the same outer array.
4763 static int extract_access(__isl_keep pet_expr
*expr
, void *user
)
4765 struct ppcg_extract_access_data
*data
= user
;
4766 isl_union_map
*tagged
;
4767 struct gpu_stmt_access
*access
;
4768 isl_ctx
*ctx
= pet_expr_get_ctx(expr
);
4769 isl_multi_pw_aff
*index
;
4771 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
4773 access
->next
= NULL
;
4774 access
->read
= pet_expr_access_is_read(expr
);
4775 access
->write
= pet_expr_access_is_write(expr
);
4776 tagged
= pet_expr_access_get_tagged_may_read(expr
);
4777 tagged
= isl_union_map_union(tagged
,
4778 pet_expr_access_get_tagged_may_write(expr
));
4779 tagged
= isl_union_map_apply_range(tagged
,
4780 isl_union_map_copy(data
->any_to_outer
));
4781 if (!access
->write
) {
4782 access
->exact_write
= 1;
4783 } else if (!data
->single_expression
) {
4784 access
->exact_write
= 0;
4786 isl_union_map
*must
, *may
;
4787 may
= isl_union_map_copy(tagged
);
4788 may
= isl_union_map_domain_factor_domain(may
);
4789 must
= pet_expr_access_get_must_write(expr
);
4790 access
->exact_write
= isl_union_map_is_equal(must
, may
);
4791 isl_union_map_free(must
);
4792 isl_union_map_free(may
);
4794 index
= pet_expr_access_get_index(expr
);
4795 access
->n_index
= isl_multi_pw_aff_dim(index
, isl_dim_out
);
4796 isl_multi_pw_aff_free(index
);
4797 access
->ref_id
= pet_expr_access_get_ref_id(expr
);
4798 access
->tagged_access
= extract_single_tagged_access(tagged
, expr
);
4799 access
->access
= isl_map_copy(access
->tagged_access
);
4800 access
->access
= isl_map_domain_factor_domain(access
->access
);
4802 *data
->next_access
= access
;
4803 data
->next_access
= &(*data
->next_access
)->next
;
4805 if (!access
->access
)
4811 /* Construct a linked list of gpu_stmt_access objects,
4812 * one for each access expression in the statement body.
4813 * "any_to_outer" maps all intermediate arrays to their outer arrays.
4815 static int pet_stmt_extract_accesses(struct gpu_stmt
*stmt
,
4816 __isl_keep isl_union_map
*any_to_outer
)
4818 struct ppcg_extract_access_data data
;
4820 stmt
->accesses
= NULL
;
4821 data
.next_access
= &stmt
->accesses
;
4822 data
.single_expression
=
4823 pet_tree_get_type(stmt
->stmt
->body
) == pet_tree_expr
;
4824 data
.any_to_outer
= any_to_outer
;
4825 return pet_tree_foreach_access_expr(stmt
->stmt
->body
,
4826 &extract_access
, &data
);
4829 /* Return an array of gpu_stmt representing the statements in "scop".
4831 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
4832 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*any_to_outer
)
4835 struct gpu_stmt
*stmts
;
4837 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->pet
->n_stmt
);
4841 for (i
= 0; i
< scop
->pet
->n_stmt
; ++i
) {
4842 struct gpu_stmt
*s
= &stmts
[i
];
4844 s
->id
= isl_set_get_tuple_id(scop
->pet
->stmts
[i
]->domain
);
4845 s
->stmt
= scop
->pet
->stmts
[i
];
4846 if (pet_stmt_extract_accesses(s
, any_to_outer
) < 0)
4847 return free_stmts(stmts
, i
+ 1);
4853 /* Callback for ppcg_print_guarded that calls the callback for generate_gpu.
4855 static __isl_give isl_printer
*print_gpu(__isl_take isl_printer
*p
, void *user
)
4857 struct gpu_gen
*gen
= user
;
4859 return gen
->print(p
, gen
->prog
, gen
->tree
, &gen
->types
,
4863 /* Generate CUDA code for "scop" and print it to "p".
4864 * After generating an AST for the transformed scop as explained below,
4865 * we call "gen->print" to print the AST in the desired output format
4868 * If it turns out that it does not make sense to generate GPU code,
4869 * then we generate CPU code instead.
4871 * The GPU code is generated in a context where at least one
4872 * statement instance is executed. The corresponding guard (if any) is printed
4873 * around the entire generated GPU code, except for the declaration
4874 * of the arrays that are visible outside of the scop and that therefore
4875 * cannot be declared inside the body of any possible guard.
4877 * We first compute a schedule that respects the dependences
4878 * of the original program and select the outermost bands
4879 * of tilable dimensions that have at least one parallel loop.
4881 * Each of these bands B is then tiled according to "tile" sizes, resulting
4882 * in two nested bands, with a kernel marker on top
4890 * We then split off at most 2 parallel dimensions from the T band and
4891 * at most 3 parallel dimension from the P band
4904 * A filter is introduced in front of T1 that maps the domain instances
4905 * to block identifiers. Similarly, a filter is introduced in front of P1
4906 * that maps the domain instances to thread identifiers.
4908 * For each iteration of the T2 band and for each array, we compute
4909 * the array elements accessed by that iteration, construct a rectangular
4910 * box around it and shift it to the origin. The result is used
4911 * as shared memory for the array.
4913 * Copying and synchronization statements are added to this schedule tree.
4914 * In principle, these are added in front of the P1 band, but some of
4915 * them may get hoisted up to higher levels.
4917 * The entire AST is then generated from the single resulting schedule tree.
4918 * During the generation the subtrees at kernel nodes (K) are saved
4919 * aside and replaced by kernel calls. The result is printed as host code
4920 * while the saved subtrees are printed as device code.
4922 static __isl_give isl_printer
*generate(__isl_take isl_printer
*p
,
4923 struct gpu_gen
*gen
, struct ppcg_scop
*scop
,
4924 struct ppcg_options
*options
)
4926 struct gpu_prog
*prog
;
4928 isl_set
*context
, *guard
;
4931 return isl_printer_free(p
);
4933 ctx
= isl_printer_get_ctx(p
);
4934 prog
= gpu_prog_alloc(ctx
, scop
);
4936 return isl_printer_free(p
);
4938 context
= isl_set_copy(prog
->context
);
4939 guard
= isl_union_set_params(isl_union_set_copy(prog
->scop
->domain
));
4940 prog
->context
= isl_set_intersect(prog
->context
, isl_set_copy(guard
));
4943 gen
->any_parallelism
= 0;
4944 compute_schedule(gen
);
4946 if (!gen
->any_parallelism
) {
4947 isl_set_free(context
);
4948 isl_set_free(guard
);
4949 p
= print_cpu(p
, scop
, options
);
4951 compute_copy_in_and_out(gen
);
4952 gen
->tree
= generate_code(gen
);
4953 p
= ppcg_print_exposed_declarations(p
, prog
->scop
);
4954 p
= ppcg_print_guarded(p
, guard
, context
, &print_gpu
, gen
);
4955 isl_ast_node_free(gen
->tree
);
4958 isl_union_map_free(gen
->sched
);
4959 isl_schedule_free(gen
->schedule
);
4961 gpu_prog_free(prog
);
4966 /* Wrapper around generate for use as a ppcg_transform callback.
4968 static __isl_give isl_printer
*generate_wrap(__isl_take isl_printer
*p
,
4969 struct ppcg_scop
*scop
, void *user
)
4971 struct gpu_gen
*gen
= user
;
4973 return generate(p
, gen
, scop
, gen
->options
);
4976 /* Transform the code in the file called "input" by replacing
4977 * all scops by corresponding GPU code and write the results to "out".
4979 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
4980 struct ppcg_options
*options
,
4981 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
4982 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
4983 struct gpu_types
*types
, void *user
), void *user
)
4990 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
4991 gen
.options
= options
;
4994 gen
.print_user
= user
;
4996 gen
.types
.name
= NULL
;
4998 if (options
->debug
->dump_sizes
) {
4999 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
5000 gen
.used_sizes
= isl_union_map_empty(space
);
5003 r
= ppcg_transform(ctx
, input
, out
, options
, &generate_wrap
, &gen
);
5005 if (options
->debug
->dump_sizes
) {
5006 isl_union_map_dump(gen
.used_sizes
);
5007 isl_union_map_free(gen
.used_sizes
);
5010 isl_union_map_free(gen
.sizes
);
5011 for (i
= 0; i
< gen
.types
.n
; ++i
)
5012 free(gen
.types
.name
[i
]);
5013 free(gen
.types
.name
);
5018 /* Compute the set of inner array elements that may have their values
5019 * preserved by "prog". In particular, collect the array elements of
5020 * arrays that are not local to "prog" and remove those elements that
5021 * are definitely killed or definitely written by "prog".
5023 static __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
)
5026 isl_union_set
*may_persist
, *killed
;
5027 isl_union_map
*must_kill
;
5029 may_persist
= isl_union_set_empty(isl_set_get_space(prog
->context
));
5030 for (i
= 0; i
< prog
->n_array
; ++i
) {
5033 if (prog
->array
[i
].local
)
5036 extent
= isl_set_copy(prog
->array
[i
].extent
);
5037 may_persist
= isl_union_set_add_set(may_persist
, extent
);
5040 may_persist
= isl_union_set_intersect_params(may_persist
,
5041 isl_set_copy(prog
->context
));
5042 may_persist
= isl_union_set_apply(may_persist
,
5043 isl_union_map_copy(prog
->to_inner
));
5044 must_kill
= isl_union_map_copy(prog
->tagged_must_kill
);
5045 killed
= isl_union_map_range(must_kill
);
5046 must_kill
= isl_union_map_copy(prog
->must_write
);
5047 killed
= isl_union_set_union(killed
, isl_union_map_range(must_kill
));
5049 may_persist
= isl_union_set_subtract(may_persist
, killed
);
5053 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
5055 struct gpu_prog
*prog
;
5062 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
5067 prog
->context
= isl_set_copy(scop
->context
);
5068 prog
->n_stmts
= scop
->pet
->n_stmt
;
5069 prog
->any_to_outer
= pet_scop_compute_outer_to_any(scop
->pet
);
5070 prog
->any_to_outer
= isl_union_map_reverse(prog
->any_to_outer
);
5071 space
= isl_union_map_get_space(prog
->any_to_outer
);
5072 space
= isl_space_set_from_params(space
);
5073 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
5074 space
= isl_space_map_from_set(space
);
5075 id
= isl_map_identity(space
);
5076 prog
->any_to_outer
= isl_union_map_add_map(prog
->any_to_outer
, id
);
5077 prog
->stmts
= extract_stmts(ctx
, scop
,
5078 prog
->context
, prog
->any_to_outer
);
5079 prog
->read
= isl_union_map_copy(scop
->reads
);
5080 prog
->may_write
= isl_union_map_copy(scop
->may_writes
);
5081 prog
->must_write
= isl_union_map_copy(scop
->must_writes
);
5082 prog
->tagged_must_kill
= isl_union_map_copy(scop
->tagged_must_kills
);
5083 prog
->to_inner
= pet_scop_compute_outer_to_inner(scop
->pet
);
5084 prog
->to_outer
= isl_union_map_copy(prog
->to_inner
);
5085 prog
->to_outer
= isl_union_map_reverse(prog
->to_outer
);
5088 return gpu_prog_free(prog
);
5090 if (collect_array_info(prog
) < 0)
5091 return gpu_prog_free(prog
);
5092 prog
->may_persist
= compute_may_persist(prog
);
5097 void *gpu_prog_free(struct gpu_prog
*prog
)
5101 free_array_info(prog
);
5102 free_stmts(prog
->stmts
, prog
->n_stmts
);
5103 isl_union_map_free(prog
->any_to_outer
);
5104 isl_union_map_free(prog
->to_outer
);
5105 isl_union_map_free(prog
->to_inner
);
5106 isl_union_set_free(prog
->copy_in
);
5107 isl_union_set_free(prog
->copy_out
);
5108 isl_union_map_free(prog
->read
);
5109 isl_union_map_free(prog
->may_write
);
5110 isl_union_map_free(prog
->must_write
);
5111 isl_union_map_free(prog
->tagged_must_kill
);
5112 isl_union_map_free(prog
->array_order
);
5113 isl_union_set_free(prog
->may_persist
);
5114 isl_set_free(prog
->context
);