2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012 Ecole Normale Superieure
5 * Use of this software is governed by the GNU LGPLv2.1 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
16 #include <isl/polynomial.h>
17 #include <isl/union_set.h>
22 #include <isl/schedule.h>
23 #include <isl/options.h>
24 #include <isl/ast_build.h>
28 #include "ppcg_options.h"
30 /* The fields stride, shift and shift_map only contain valid information
32 * If so, they express that current index is such that if you add shift,
33 * then the result is always a multiple of stride.
34 * shift_map contains the mapping
36 * i -> (i + shift)/stride
38 * Let D represent the initial shared_len dimensions of the computed schedule.
39 * The spaces of "lb" and "shift" are of the form
43 * "shift_map" is of the form
45 * [D -> i] -> [D -> (i + shift(D))/stride]
47 struct gpu_array_bound
{
53 isl_basic_map
*shift_map
;
56 /* A tile of an array.
58 * n is the dimension of the array.
59 * bound is an array of size "n" representing the lower bound
60 * and size for each index.
62 struct gpu_array_tile
{
64 struct gpu_array_bound
*bound
;
67 struct gpu_array_info
;
69 /* A group of array references in a kernel that should be handled together.
70 * If private_tile is not NULL, then it is mapped to registers.
71 * Otherwise, if shared_tile is not NULL, it is mapped to shared memory.
72 * Otherwise, it is accessed from global memory.
74 struct gpu_array_ref_group
{
75 /* The references in this group access this array. */
76 struct gpu_array_info
*array
;
77 /* Position of this group in the list of reference groups of array. */
80 /* The following fields are use during the construction of the groups.
81 * access is the combined access relation relative to the shared
82 * memory tiling. In particular, the domain of the map corresponds
83 * to the first shared_len dimensions of the computed schedule.
84 * write is set if any access in the group is a write.
89 /* The shared memory tile, NULL if none. */
90 struct gpu_array_tile
*shared_tile
;
92 /* The private memory tile, NULL if none. */
93 struct gpu_array_tile
*private_tile
;
95 /* References in this group; point to elements of a linked list. */
97 struct gpu_stmt_access
**refs
;
99 /* Last shared memory tile dimension that affects tile of this group. */
105 struct ppcg_options
*options
;
107 struct gpu_prog
*prog
;
109 /* tile, grid and block sizes for each kernel */
110 isl_union_map
*sizes
;
112 /* Identifier of current kernel. */
114 /* Pointer to the current kernel. */
115 struct ppcg_kernel
*kernel
;
117 /* First tile dimension. */
119 /* Number of tile dimensions. */
121 /* Number of initial parallel loops among tile dimensions. */
124 /* Number of dimensions determining shared memory. */
127 /* Number of rows in the untiled schedule. */
129 /* Number of rows in the tiled schedule. */
131 /* Number of rows in schedule after tiling/wrapping over threads. */
132 int thread_tiled_len
;
134 /* Global untiled schedule. */
135 isl_union_map
*sched
;
136 /* Local (per kernel launch) tiled schedule. */
137 isl_union_map
*tiled_sched
;
138 /* Local schedule per shared memory tile loop iteration. */
139 isl_union_map
*local_sched
;
141 /* Local tiled schedule projected onto the shared tile loops and
142 * the loops that will be wrapped over the threads,
143 * with all shared tile loops parametrized.
145 isl_union_map
*shared_sched
;
146 /* Projects out the loops that will be wrapped over the threads
149 isl_union_map
*shared_proj
;
151 /* A map that takes the range of shared_sched as input,
152 * wraps the appropriate loops over the threads and then projects
155 isl_map
*privatization
;
157 /* A map from the shared memory tile loops and the thread indices
158 * (as parameters) to the set of accessed memory elements that
159 * will be accessed through private copies.
161 isl_union_map
*private_access
;
163 /* The schedule for the current private/shared access
164 * (within print_private_access or print_shared_access).
167 /* The array reference group corresponding to copy_sched. */
168 struct gpu_array_ref_group
*copy_group
;
170 /* First loop to unroll (or -1 if none) in the current part of the
177 /* Note: in the input file, the sizes of the grid and the blocks
178 * are specified in the order x, y, z, but internally, the sizes
179 * are stored in reverse order, so that the last element always
180 * refers to the x dimension.
187 /* Print the name of the local copy of a given group of array references.
189 static __isl_give isl_printer
*print_array_name(__isl_take isl_printer
*p
,
190 struct gpu_array_ref_group
*group
)
194 if (group
->private_tile
)
195 p
= isl_printer_print_str(p
, "private_");
196 else if (group
->shared_tile
)
197 p
= isl_printer_print_str(p
, "shared_");
200 p
= isl_printer_print_str(p
, group
->array
->name
);
201 if (!global
&& group
->array
->n_group
> 1) {
202 p
= isl_printer_print_str(p
, "_");
203 p
= isl_printer_print_int(p
, group
->nr
);
209 /* Collect all references to the given array and store pointers to them
212 static void collect_references(struct gpu_prog
*prog
,
213 struct gpu_array_info
*array
)
219 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
220 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
221 struct gpu_stmt_access
*access
;
223 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
225 name
= isl_map_get_tuple_name(access
->access
,
227 if (name
&& !strcmp(array
->name
, name
))
233 array
->refs
= isl_alloc_array(prog
->ctx
, struct gpu_stmt_access
*, n
);
237 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
238 struct gpu_stmt
*stmt
= &prog
->stmts
[i
];
239 struct gpu_stmt_access
*access
;
241 for (access
= stmt
->accesses
; access
; access
= access
->next
) {
243 name
= isl_map_get_tuple_name(access
->access
,
245 if (!name
|| strcmp(array
->name
, name
))
248 array
->refs
[n
++] = access
;
253 /* Create a gpu_array_tile for an array of dimension "n_index".
255 static struct gpu_array_tile
*create_tile(isl_ctx
*ctx
, int n_index
)
258 struct gpu_array_tile
*tile
;
260 tile
= isl_calloc_type(ctx
, struct gpu_array_tile
);
265 tile
->bound
= isl_alloc_array(ctx
, struct gpu_array_bound
, n_index
);
268 for (i
= 0; i
< n_index
; ++i
) {
269 tile
->bound
[i
].size
= NULL
;
270 tile
->bound
[i
].lb
= NULL
;
271 tile
->bound
[i
].stride
= NULL
;
272 tile
->bound
[i
].shift
= NULL
;
273 tile
->bound
[i
].shift_map
= NULL
;
279 static void *free_tile(struct gpu_array_tile
*tile
)
286 for (j
= 0; j
< tile
->n
; ++j
) {
287 isl_val_free(tile
->bound
[j
].size
);
288 isl_val_free(tile
->bound
[j
].stride
);
289 isl_aff_free(tile
->bound
[j
].lb
);
290 isl_aff_free(tile
->bound
[j
].shift
);
291 isl_basic_map_free(tile
->bound
[j
].shift_map
);
299 static struct pet_array
*find_array(struct ppcg_scop
*scop
,
300 __isl_keep isl_set
*accessed
)
305 id
= isl_set_get_tuple_id(accessed
);
307 for (i
= 0; i
< scop
->n_array
; ++i
) {
310 id_i
= isl_set_get_tuple_id(scop
->arrays
[i
]->extent
);
317 return i
< scop
->n_array
? scop
->arrays
[i
] : NULL
;
320 /* Compute and return the extent of "array", taking into account the set of
323 * In particular, the extent in the outer dimension is taken
324 * from "accessed", while then extent in the remaing dimensions
325 * are taken from array->extent.
327 * The extent in the outer dimension cannot be taken from array->extent
328 * because that may be unbounded. Furthermore, even if it is bounded,
329 * it may be larger than the piece of the array that is being accessed.
331 static __isl_give isl_set
*compute_extent(struct pet_array
*array
,
332 __isl_keep isl_set
*accessed
)
339 extent
= isl_set_copy(array
->extent
);
341 n_index
= isl_set_dim(accessed
, isl_dim_set
);
345 extent
= isl_set_project_out(extent
, isl_dim_set
, 0, 1);
346 outer
= isl_set_copy(accessed
);
347 outer
= isl_set_project_out(outer
, isl_dim_set
, 1, n_index
- 1);
348 extent
= isl_set_flat_product(outer
, extent
);
349 id
= isl_set_get_tuple_id(accessed
);
350 extent
= isl_set_set_tuple_id(extent
, id
);
355 /* Compute bounds on the host arrays based on the accessed elements
356 * and collect all references to the array.
358 * If the array is zero-dimensional, i.e., a scalar, we check
359 * whether it is read-only.
361 static int extract_array_info(__isl_take isl_set
*array
, void *user
)
364 struct gpu_prog
*prog
= (struct gpu_prog
*)user
;
368 struct pet_array
*pa
;
371 n_index
= isl_set_dim(array
, isl_dim_set
);
372 name
= isl_set_get_tuple_name(array
);
373 bounds
= isl_alloc_array(isl_set_get_ctx(array
),
374 isl_pw_aff
*, n_index
);
376 prog
->array
[prog
->n_array
].dim
= isl_set_get_space(array
);
377 prog
->array
[prog
->n_array
].name
= strdup(name
);
378 prog
->array
[prog
->n_array
].n_index
= n_index
;
379 prog
->array
[prog
->n_array
].bound
= bounds
;
381 pa
= find_array(prog
->scop
, array
);
384 prog
->array
[prog
->n_array
].type
= strdup(pa
->element_type
);
385 prog
->array
[prog
->n_array
].size
= pa
->element_size
;
386 prog
->array
[prog
->n_array
].local
= pa
->declared
&& !pa
->exposed
;
390 isl_union_map
*write
;
393 write
= isl_union_map_copy(prog
->write
);
394 space
= isl_set_universe(isl_set_get_space(array
));
395 write
= isl_union_map_intersect_range(write
,
396 isl_union_set_from_set(space
));
397 empty
= isl_union_map_is_empty(write
);
398 isl_union_map_free(write
);
400 prog
->array
[prog
->n_array
].read_only
= empty
;
403 extent
= compute_extent(pa
, array
);
404 for (i
= 0; i
< n_index
; ++i
) {
410 bound
= isl_set_dim_max(isl_set_copy(extent
), i
);
412 dom
= isl_pw_aff_domain(isl_pw_aff_copy(bound
));
413 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
414 one
= isl_aff_zero_on_domain(ls
);
415 one
= isl_aff_add_constant_si(one
, 1);
416 bound
= isl_pw_aff_add(bound
, isl_pw_aff_alloc(dom
, one
));
417 bound
= isl_pw_aff_gist(bound
, isl_set_copy(prog
->context
));
421 prog
->array
[prog
->n_array
].extent
= extent
;
423 collect_references(prog
, &prog
->array
[prog
->n_array
]);
431 void collect_array_info(struct gpu_prog
*prog
)
433 isl_union_set
*arrays
;
435 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
436 arrays
= isl_union_set_union(arrays
,
437 isl_union_map_range(isl_union_map_copy(prog
->write
)));
438 arrays
= isl_union_set_coalesce(arrays
);
440 prog
->n_array
= isl_union_set_n_set(arrays
);
441 prog
->array
= isl_alloc_array(prog
->ctx
,
442 struct gpu_array_info
, prog
->n_array
);
445 isl_union_set_foreach_set(arrays
, &extract_array_info
, prog
);
446 isl_union_set_free(arrays
);
449 static void free_array_info(struct gpu_prog
*prog
)
453 for (i
= 0; i
< prog
->n_array
; ++i
) {
454 int n_index
= prog
->array
[i
].n_index
;
455 free(prog
->array
[i
].type
);
456 free(prog
->array
[i
].name
);
457 for (j
= 0; j
< n_index
; ++j
)
458 isl_pw_aff_free(prog
->array
[i
].bound
[j
]);
459 isl_space_free(prog
->array
[i
].dim
);
460 isl_set_free(prog
->array
[i
].extent
);
461 free(prog
->array
[i
].bound
);
462 free(prog
->array
[i
].refs
);
467 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
468 * as an array or through a pointer reference, but as single data element. At
469 * the moment, scalars are represented as zero dimensional arrays.
471 int gpu_array_is_scalar(struct gpu_array_info
*array
)
473 return (array
->n_index
== 0);
476 /* Is "array" a read-only scalar?
478 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
480 return gpu_array_is_scalar(array
) && array
->read_only
;
483 /* Internal data structure for extract_size_of_type.
484 * "type" specifies the name of the space that we want to extract.
485 * "res" is used to store the subset of that space.
487 struct ppcg_extract_size_data
{
492 /* This function is called for each set in a union_set.
493 * If the name of the set matches data->type, we store the
496 static int extract_size_of_type(__isl_take isl_set
*size
, void *user
)
498 struct ppcg_extract_size_data
*data
= user
;
501 name
= isl_set_get_tuple_name(size
);
502 if (name
&& !strcmp(name
, data
->type
)) {
511 /* Given a union map { kernel[i] -> *[...] },
512 * return the range in the space called "type" for the kernel with
513 * sequence number "id".
515 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
516 const char *type
, int id
)
520 isl_union_set
*local_sizes
;
521 struct ppcg_extract_size_data data
= { type
, NULL
};
526 space
= isl_union_map_get_space(sizes
);
527 space
= isl_space_set_from_params(space
);
528 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
529 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
530 dom
= isl_set_universe(space
);
531 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
533 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
534 isl_union_map_copy(sizes
));
535 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
536 isl_union_set_free(local_sizes
);
540 /* Given a singleton set, extract the first (at most *len) elements
541 * of the single integer tuple into *sizes and update *len if needed.
543 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
551 dim
= isl_set_dim(set
, isl_dim_set
);
555 for (i
= 0; i
< *len
; ++i
) {
558 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
561 sizes
[i
] = isl_val_get_num_si(v
);
568 /* Extract user specified "tile" sizes from the "sizes" command line option,
569 * defaulting to option->tile_size in each dimension.
571 static void read_tile_sizes(struct gpu_gen
*gen
)
576 gen
->tile_size
= isl_alloc_array(gen
->ctx
, int, gen
->tile_len
);
577 assert(gen
->tile_size
);
578 for (n
= 0; n
< gen
->tile_len
; ++n
)
579 gen
->tile_size
[n
] = gen
->options
->tile_size
;
581 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
582 read_sizes_from_set(size
, gen
->tile_size
, &gen
->tile_len
);
584 if (gen
->n_parallel
> gen
->tile_len
)
585 gen
->n_parallel
= gen
->tile_len
;
588 /* Extract user specified "block" sizes from the "sizes" command line option,
589 * after filling in some potentially useful defaults.
591 static void read_block_sizes(struct gpu_gen
*gen
)
597 gen
->n_block
= (n
<= 3) ? n
: 3;
598 switch (gen
->n_block
) {
600 gen
->block_dim
[0] = 512;
603 gen
->block_dim
[0] = 32;
604 gen
->block_dim
[1] = 16;
607 gen
->block_dim
[0] = 32;
608 gen
->block_dim
[1] = 4;
609 gen
->block_dim
[2] = 4;
613 size
= extract_sizes(gen
->sizes
, "block", gen
->kernel_id
);
614 read_sizes_from_set(size
, gen
->block_dim
, &gen
->n_block
);
617 /* Extract user specified "grid" sizes from the "sizes" command line option,
618 * after filling in some potentially useful defaults.
620 static void read_grid_sizes(struct gpu_gen
*gen
)
622 int n
= gen
->n_parallel
;
625 gen
->n_grid
= (n
<= 2) ? n
: 2;
626 switch (gen
->n_grid
) {
628 gen
->grid_dim
[0] = 32768;
631 gen
->grid_dim
[0] = 256;
632 gen
->grid_dim
[1] = 256;
636 size
= extract_sizes(gen
->sizes
, "grid", gen
->kernel_id
);
637 read_sizes_from_set(size
, gen
->grid_dim
, &gen
->n_grid
);
640 /* Extract user specified sizes from the "sizes" command line option
641 * after filling in some potentially useful defaults.
643 static void read_sizes(struct gpu_gen
*gen
)
645 read_tile_sizes(gen
);
646 read_block_sizes(gen
);
647 read_grid_sizes(gen
);
650 static void free_stmts(struct gpu_stmt
*stmts
, int n
)
654 for (i
= 0; i
< n
; ++i
) {
655 struct gpu_stmt_access
*access
, *next
;
657 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
659 isl_map_free(access
->access
);
663 isl_id_free(stmts
[i
].id
);
668 void clear_gpu_gen(struct gpu_gen
*gen
)
670 isl_union_map_free(gen
->sizes
);
671 isl_union_map_free(gen
->sched
);
674 /* Construct a map from a domain of dimensionality "len"
675 * to a domain of dimensionality "len" + "tile_len" that tiles
676 * the "tile_len" coordinates starting at "first".
677 * In particular, [s_i] -> [s_i / tile_size[i], s_i % tile_size[i]].
678 * "dim" prescribes the parameters.
680 static __isl_give isl_map
*tile(__isl_take isl_space
*dim
, int len
,
681 int first
, int tile_len
, int *tile_size
)
688 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
689 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
+ tile_len
);
690 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
691 ls
= isl_local_space_from_space(dim
);
693 for (i
= 0; i
< len
- tile_len
; ++i
) {
694 int j
= i
< first
? i
: i
+ tile_len
;
695 int k
= i
< first
? i
: i
+ 2 * tile_len
;
697 c
= isl_equality_alloc(isl_local_space_copy(ls
));
698 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, j
, -1);
699 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, k
, 1);
700 bmap
= isl_basic_map_add_constraint(bmap
, c
);
703 for (i
= 0; i
< tile_len
; ++i
) {
704 c
= isl_equality_alloc(isl_local_space_copy(ls
));
705 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
,
707 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
708 first
+ i
, tile_size
[i
]);
709 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
710 first
+ i
+ tile_len
, 1);
711 bmap
= isl_basic_map_add_constraint(bmap
, c
);
713 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
714 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
715 first
+ i
+ tile_len
, 1);
716 bmap
= isl_basic_map_add_constraint(bmap
, c
);
718 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
719 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
720 first
+ i
+ tile_len
, -1);
721 c
= isl_constraint_set_constant_si(c
, tile_size
[i
] - 1);
722 bmap
= isl_basic_map_add_constraint(bmap
, c
);
725 isl_local_space_free(ls
);
727 return isl_map_from_basic_map(bmap
);
730 /* Construct a map from a domain of dimensionality "len"
731 * to a domain of dimensionality "len" + "wrap_len" that "wraps"
732 * the "wrap_len" coordinates starting at "first" according to "wrap_size".
733 * In particular, [s_i] -> [s_i, s_i % wrap_size[i]].
734 * To do so, we need extra variables corresponding to [s_i / wrap_size[i]],
735 * that are projected out at the end.
736 * "dim" prescribes the parameters.
738 static __isl_give isl_map
*wrap(__isl_take isl_space
*dim
, int len
,
739 int first
, int wrap_len
, int *wrap_size
)
746 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
747 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
+ 2 * wrap_len
);
748 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
749 ls
= isl_local_space_from_space(dim
);
751 for (i
= 0; i
< len
; ++i
) {
752 int k
= i
< first
+ wrap_len
? i
: i
+ 2 * wrap_len
;
754 c
= isl_equality_alloc(isl_local_space_copy(ls
));
755 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, -1);
756 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, k
, 1);
757 bmap
= isl_basic_map_add_constraint(bmap
, c
);
760 for (i
= 0; i
< wrap_len
; ++i
) {
761 c
= isl_equality_alloc(isl_local_space_copy(ls
));
762 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
764 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
765 first
+ wrap_len
+ i
, 1);
766 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
767 first
+ 2 * wrap_len
+ i
, wrap_size
[i
]);
768 bmap
= isl_basic_map_add_constraint(bmap
, c
);
770 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
771 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
772 first
+ wrap_len
+ i
, 1);
773 bmap
= isl_basic_map_add_constraint(bmap
, c
);
775 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
776 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
777 first
+ wrap_len
+ i
, -1);
778 c
= isl_constraint_set_constant_si(c
, wrap_size
[i
] - 1);
779 bmap
= isl_basic_map_add_constraint(bmap
, c
);
782 isl_local_space_free(ls
);
784 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
,
785 first
+ 2 * wrap_len
, wrap_len
);
787 return isl_map_from_basic_map(bmap
);
790 /* Add "n" parameters named prefix%d.
792 static __isl_give isl_set
*add_params( __isl_take isl_set
*set
,
793 int n
, const char *prefix
)
799 nparam
= isl_set_dim(set
, isl_dim_param
);
800 set
= isl_set_add_dims(set
, isl_dim_param
, n
);
802 for (i
= 0; i
< n
; ++i
) {
803 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
804 set
= isl_set_set_dim_name(set
, isl_dim_param
,
811 /* Equate the "n" dimensions of "set" starting at "first" to
812 * freshly created parameters named prefix%d.
814 static __isl_give isl_set
*parametrize(__isl_take isl_set
*set
,
815 int first
, int n
, const char *prefix
)
820 nparam
= isl_set_dim(set
, isl_dim_param
);
822 set
= add_params(set
, n
, prefix
);
824 for (i
= 0; i
< n
; ++i
)
825 set
= isl_set_equate(set
, isl_dim_param
, nparam
+ i
,
826 isl_dim_set
, first
+ i
);
831 /* Given a parameter space "space", create a set of dimension "len"
832 * of which the "n" dimensions starting at "first" are equated to
833 * freshly created parameters named prefix%d.
835 static __isl_give isl_set
*parametrization(__isl_take isl_space
*space
,
836 int len
, int first
, int n
, const char *prefix
)
840 space
= isl_space_set_from_params(space
);
841 space
= isl_space_add_dims(space
, isl_dim_set
, len
);
842 set
= isl_set_universe(space
);
844 return parametrize(set
, first
, n
, prefix
);
847 /* Tile the B loops over the tile sizes and then tile/wrap
848 * the T1 loops over the blocks.
850 static __isl_give isl_union_map
*tile_schedule(struct gpu_gen
*gen
,
851 __isl_take isl_union_map
*sched
)
854 isl_map
*tiling
, *block_tiling
;
856 dim
= isl_union_map_get_space(sched
);
857 tiling
= tile(isl_space_copy(dim
), gen
->untiled_len
,
858 gen
->tile_first
, gen
->tile_len
, gen
->tile_size
);
860 if (gen
->options
->wrap
)
861 block_tiling
= wrap(dim
, gen
->untiled_len
+ gen
->tile_len
,
862 gen
->tile_first
, gen
->n_grid
, gen
->grid_dim
);
864 block_tiling
= tile(dim
, gen
->untiled_len
+ gen
->tile_len
,
865 gen
->tile_first
, gen
->n_grid
, gen
->grid_dim
);
867 gen
->tiled_len
= gen
->untiled_len
+ gen
->tile_len
+ gen
->n_grid
;
869 tiling
= isl_map_apply_range(tiling
, block_tiling
);
871 sched
= isl_union_map_apply_range(sched
,
872 isl_union_map_from_map(tiling
));
874 gen
->shared_len
= gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
;
879 /* Equate the "T1P" iterators in the tiled schedule "sched"
880 * to the block dimensions.
882 static __isl_give isl_union_map
*parametrize_tiled_schedule(
883 struct gpu_gen
*gen
, __isl_take isl_union_map
*sched
)
888 dim
= isl_union_map_get_space(sched
);
889 par
= parametrization(dim
, gen
->tiled_len
,
890 gen
->tile_first
+ gen
->n_grid
, gen
->n_grid
, "b");
891 sched
= isl_union_map_intersect_range(sched
,
892 isl_union_set_from_set(par
));
897 /* Tile/wrap the P1 loops over the threads.
899 static __isl_give isl_union_map
*thread_tile_schedule(struct gpu_gen
*gen
,
900 __isl_take isl_union_map
*sched
)
906 dim
= isl_union_map_get_space(sched
);
908 if (gen
->options
->wrap
)
909 tiling
= wrap(isl_space_copy(dim
), gen
->tiled_len
,
910 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
912 tiling
= tile(isl_space_copy(dim
), gen
->tiled_len
,
913 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
914 gen
->thread_tiled_len
= gen
->tiled_len
+ gen
->n_block
;
916 sched
= isl_union_map_apply_range(sched
,
917 isl_union_map_from_map(tiling
));
919 par
= parametrization(dim
, gen
->thread_tiled_len
,
920 gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
+ gen
->n_block
,
922 sched
= isl_union_map_intersect_range(sched
,
923 isl_union_set_from_set(par
));
925 gen
->shared_len
= gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
;
930 /* If the user asked for it, scale the shared memory tile loops
931 * (T1T and T2) of "sched" by gen->tile_size[i].
932 * If we are not performing "wrapping", then additionally scale the T1P
933 * loops by gen->grid_dim[i].
935 static __isl_give isl_union_map
*scale_tile_loops(struct gpu_gen
*gen
,
936 __isl_take isl_union_map
*sched
)
940 isl_basic_map
*scale
;
944 if (!gen
->options
->scale_tile_loops
)
947 dim
= isl_union_map_get_space(sched
);
948 dim
= isl_space_add_dims(dim
, isl_dim_in
, gen
->tiled_len
);
949 dim
= isl_space_add_dims(dim
, isl_dim_out
, gen
->tiled_len
);
950 scale
= isl_basic_map_universe(isl_space_copy(dim
));
951 ls
= isl_local_space_from_space(dim
);
953 for (i
= 0; i
< gen
->tiled_len
; ++i
) {
956 if (i
>= gen
->tile_first
&& i
< gen
->tile_first
+ gen
->n_grid
) {
957 f
= gen
->tile_size
[i
- gen
->tile_first
];
958 if (!gen
->options
->wrap
)
959 f
*= gen
->grid_dim
[i
- gen
->tile_first
];
960 } else if (i
>= gen
->tile_first
+ gen
->n_grid
&&
961 i
< gen
->tile_first
+ gen
->n_grid
+ gen
->tile_len
) {
962 f
= gen
->tile_size
[i
- (gen
->tile_first
+ gen
->n_grid
)];
965 c
= isl_equality_alloc(isl_local_space_copy(ls
));
966 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
967 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
968 scale
= isl_basic_map_add_constraint(scale
, c
);
971 isl_local_space_free(ls
);
973 sched
= isl_union_map_apply_range(sched
,
974 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
979 /* If we are not performing "wrapping" and if the user asked for it,
980 * scale the thread tile loops (P1T) of "sched" by gen->block_dim[i].
982 static __isl_give isl_union_map
*scale_thread_tile_loops(struct gpu_gen
*gen
,
983 __isl_take isl_union_map
*sched
)
987 isl_basic_map
*scale
;
991 if (gen
->options
->wrap
)
993 if (!gen
->options
->scale_tile_loops
)
996 dim
= isl_union_map_get_space(sched
);
997 dim
= isl_space_add_dims(dim
, isl_dim_in
, gen
->thread_tiled_len
);
998 dim
= isl_space_add_dims(dim
, isl_dim_out
, gen
->thread_tiled_len
);
999 scale
= isl_basic_map_universe(isl_space_copy(dim
));
1000 ls
= isl_local_space_from_space(dim
);
1002 for (i
= 0; i
< gen
->thread_tiled_len
; ++i
) {
1005 if (i
>= gen
->shared_len
&&
1006 i
< gen
->shared_len
+ gen
->n_block
)
1007 f
= gen
->block_dim
[i
- gen
->shared_len
];
1009 c
= isl_equality_alloc(isl_local_space_copy(ls
));
1010 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
1011 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
1012 scale
= isl_basic_map_add_constraint(scale
, c
);
1015 isl_local_space_free(ls
);
1017 sched
= isl_union_map_apply_range(sched
,
1018 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
1023 /* If we are not performing "wrapping" and if the user asked for it,
1024 * scale the "n_tile" loops starting at "first" of "sched" by gen->block_dim[i].
1026 static __isl_give isl_union_map
*scale_access_tile_loops(struct gpu_gen
*gen
,
1027 __isl_take isl_union_map
*sched
, int len
, int first
, int n_tile
)
1031 isl_basic_map
*scale
;
1033 isl_local_space
*ls
;
1035 if (gen
->options
->wrap
)
1037 if (!gen
->options
->scale_tile_loops
)
1040 dim
= isl_union_map_get_space(sched
);
1041 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
1042 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
);
1043 scale
= isl_basic_map_universe(isl_space_copy(dim
));
1044 ls
= isl_local_space_from_space(dim
);
1046 for (i
= 0; i
< len
; ++i
) {
1049 if (i
>= first
&& i
< first
+ n_tile
)
1050 f
= gen
->block_dim
[i
- first
];
1052 c
= isl_equality_alloc(isl_local_space_copy(ls
));
1053 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
1054 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
1055 scale
= isl_basic_map_add_constraint(scale
, c
);
1058 isl_local_space_free(ls
);
1060 sched
= isl_union_map_apply_range(sched
,
1061 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
1066 /* Add "len" parameters p[i] called prefix%d,
1067 * with bounds to 0 <= p[i] < size[i].
1069 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
1070 int len
, int *size
, const char *prefix
)
1075 isl_basic_set
*bset
;
1077 isl_local_space
*ls
;
1080 nparam
= isl_set_dim(set
, isl_dim_param
);
1081 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
1083 for (i
= 0; i
< len
; ++i
) {
1084 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
1085 set
= isl_set_set_dim_name(set
, isl_dim_param
,
1089 dim
= isl_set_get_space(set
);
1090 bset
= isl_basic_set_universe(isl_space_copy(dim
));
1091 ls
= isl_local_space_from_space(dim
);
1093 for (i
= 0; i
< len
; ++i
) {
1094 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
1095 c
= isl_constraint_set_coefficient_si(c
, isl_dim_param
,
1097 bset
= isl_basic_set_add_constraint(bset
, c
);
1099 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
1100 c
= isl_constraint_set_coefficient_si(c
, isl_dim_param
,
1102 c
= isl_constraint_set_constant_si(c
, size
[i
] - 1);
1103 bset
= isl_basic_set_add_constraint(bset
, c
);
1106 isl_local_space_free(ls
);
1108 return isl_set_intersect(set
, isl_set_from_basic_set(bset
));
1111 /* Add "len" parameters p[i] called prefix%d,
1112 * with bounds to 0 <= p[i] < size[i].
1114 static __isl_give isl_set
*add_bounded_parameters_dynamic(
1115 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
1121 isl_local_space
*ls
;
1124 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
1125 nparam
= isl_set_dim(set
, isl_dim_param
);
1126 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
1128 for (i
= 0; i
< len
; ++i
) {
1129 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
1130 set
= isl_set_set_dim_name(set
, isl_dim_param
,
1134 space
= isl_space_params(isl_set_get_space(set
));
1135 ls
= isl_local_space_from_space(space
);
1136 for (i
= 0; i
< len
; ++i
) {
1137 isl_pw_aff
*param
, *size_i
, *zero
;
1140 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
1141 isl_dim_param
, nparam
+ i
);
1143 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
1144 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
1145 set
= isl_set_intersect_params(set
, bound
);
1147 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
1148 bound
= isl_pw_aff_ge_set(param
, zero
);
1149 set
= isl_set_intersect_params(set
, bound
);
1151 isl_local_space_free(ls
);
1156 /* Given a mapping "sched" of the form
1158 * [D -> A] -> [D -> T(A)]
1160 * apply the mapping encoded in tile->bound[i].shift_map
1161 * to the range of "sched".
1162 * The mappings in tile->bound[i].shift_map are of the form
1164 * [D -> a] -> [D -> s(D,a)]
1166 * We first compose them with a mapping
1170 * (If tile->bound[i].shift_map is not set, then it is assumed to be
1171 * an identity mapping and then we use this second mapping instead.)
1174 * [D -> a] -> s(D,a)
1176 * We precompose them with a projection on the i th dimension to obtain
1178 * [D -> T] -> s(D,T)
1180 * and collect these into
1182 * [D -> T] -> S(D,T)
1184 * Introducing D in the range yields
1186 * [D -> T] -> [D -> S(D,T)]
1188 * and application to "sched" yields
1190 * [D -> A] -> [D -> S(D,T(A))]
1192 static __isl_give isl_map
*pre_shift(__isl_take isl_map
*sched
,
1193 struct gpu_array_tile
*tile
)
1196 isl_ctx
*ctx
= isl_map_get_ctx(sched
);
1197 isl_space
*space
, *space2
;
1199 isl_map
*map
, *id
, *pre_shift
;
1201 space
= isl_space_range(isl_map_get_space(sched
));
1202 space2
= isl_space_from_domain(isl_space_copy(space
));
1203 pre_shift
= isl_map_universe(space2
);
1204 space
= isl_space_domain(isl_space_unwrap(space
));
1205 id
= isl_map_identity(isl_space_map_from_set(isl_space_copy(space
)));
1206 space
= isl_space_from_domain(space
);
1207 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
1208 def
= isl_basic_map_range_map(isl_basic_map_universe(space
));
1210 for (i
= 0; i
< tile
->n
; ++i
) {
1211 isl_basic_map
*bmap
, *drop
;
1214 space
= isl_space_alloc(ctx
, 0, tile
->n
, tile
->n
);
1215 proj
= isl_map_identity(space
);
1216 proj
= isl_map_project_out(proj
, isl_dim_out
,
1217 i
+ 1, tile
->n
- (i
+ 1));
1218 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, i
);
1219 proj
= isl_map_product(isl_map_copy(id
), proj
);
1221 if (!tile
->bound
[i
].shift_map
)
1222 bmap
= isl_basic_map_copy(def
);
1224 bmap
= isl_basic_map_copy(tile
->bound
[i
].shift_map
);
1225 bmap
= isl_basic_map_apply_range(bmap
,
1226 isl_basic_map_copy(def
));
1229 map
= isl_map_from_basic_map(bmap
);
1230 map
= isl_map_apply_range(proj
, map
);
1231 pre_shift
= isl_map_flat_range_product(pre_shift
, map
);
1235 isl_basic_map_free(def
);
1237 space
= isl_space_domain(isl_map_get_space(pre_shift
));
1238 map
= isl_map_domain_map(isl_map_universe(isl_space_unwrap(space
)));
1239 pre_shift
= isl_map_range_product(map
, pre_shift
);
1241 sched
= isl_map_apply_range(sched
, pre_shift
);
1246 /* Given an access relation to a tile of an array, construct a map that
1247 * maps each element in the space of the access relation
1248 * to a copy of the tile shifted to the origin
1249 * (based on the lower bounds in group->private_tile or group->shared_tile).
1250 * If any of the indices is strided, then
1251 * {private,shared}_tile->bound[i].shift_map is applied to the index first.
1252 * The domain space of the resulting map is that of access "access",
1253 * while the range space is anonymous.
1254 * The resulting map only encodes the mapping to the shift tile and
1255 * not the constraints of "access".
1257 * Let the space of the access relation be
1261 * We first construct an identity relation on a wrapped copy of this space,
1262 * except that it strips off the name of array
1264 * [D -> A] -> [D -> T(A)] (1)
1266 * The bounds in tile->bound[i].lb are of the form
1270 * We collect them into
1274 * and then transform them into
1276 * [D -> T] -> T - B(D) (2)
1278 * Combining those two mappings (1) and (2) yields
1280 * [D -> A] -> T(A) - B(D)
1282 * If there are any strides, then (1) is first transformed into (1')
1284 * [D -> A] -> [D -> T'(A)] (1')
1286 * by a call to pre_shift.
1288 static __isl_give isl_map
*shift_access(__isl_take isl_map
*access
,
1289 struct gpu_array_ref_group
*group
)
1297 struct gpu_array_tile
*tile
;
1298 int n_index
= group
->array
->n_index
;
1300 tile
= group
->private_tile
;
1302 tile
= group
->shared_tile
;
1304 space
= isl_space_domain(isl_map_get_space(access
));
1305 space
= isl_space_map_from_set(space
);
1306 id1
= isl_map_identity(space
);
1307 space
= isl_space_range(isl_map_get_space(access
));
1308 space
= isl_space_map_from_set(space
);
1309 space
= isl_space_set_tuple_name(space
, isl_dim_out
, NULL
);
1310 id2
= isl_map_identity(space
);
1311 sched
= isl_map_product(id1
, id2
);
1313 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(sched
)));
1314 space
= isl_space_from_domain(isl_space_domain(space
));
1315 shift
= isl_map_universe(space
);
1316 for (i
= 0; i
< n_index
; ++i
) {
1317 map
= isl_map_from_aff(isl_aff_copy(tile
->bound
[i
].lb
));
1318 shift
= isl_map_flat_range_product(shift
, map
);
1321 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(sched
)));
1322 map
= isl_map_universe(space
);
1323 id1
= isl_map_range_map(isl_map_copy(map
));
1324 map
= isl_map_domain_map(map
);
1325 shift
= isl_map_neg(shift
);
1326 shift
= isl_map_apply_range(map
, shift
);
1327 shift
= isl_map_sum(id1
, shift
);
1329 for (i
= 0; i
< n_index
; ++i
)
1330 if (tile
->bound
[i
].shift_map
)
1334 sched
= pre_shift(sched
, tile
);
1336 sched
= isl_map_apply_range(sched
, shift
);
1338 isl_map_free(access
);
1343 /* Given a schedule that iterates over all elements in a piece of an array,
1344 * perform tiling/wrapping over the threads.
1346 * In particular, we tile the final iterators so that the final thread
1347 * dimension runs over the final array dimension.
1348 * However, if those final iterators have only a single iteration,
1349 * we try to tile earlier iterators instead.
1351 static __isl_give isl_map
*tile_access_schedule(struct gpu_gen
*gen
,
1352 __isl_take isl_map
*sched
)
1355 isl_union_map
*usched
;
1358 unsigned nvar
= isl_map_dim(sched
, isl_dim_out
);
1362 n_tile
= gen
->n_block
;
1363 if (n_tile
> nvar
) {
1365 sched
= isl_map_insert_dims(sched
,
1366 isl_dim_out
, 0, n_tile
- nvar
);
1367 for (i
= 0; i
< n_tile
- nvar
; ++i
)
1368 sched
= isl_map_fix_si(sched
, isl_dim_out
, i
, 0);
1372 first
= nvar
- n_tile
;
1374 for (; first
> 0; first
--)
1375 if (!isl_map_plain_is_fixed(sched
, isl_dim_out
,
1376 first
+ n_tile
- 1, NULL
))
1379 dim
= isl_map_get_space(sched
);
1380 dim
= isl_space_params(dim
);
1381 if (gen
->options
->wrap
)
1382 tiling
= wrap(isl_space_copy(dim
), nvar
, first
,
1383 n_tile
, gen
->block_dim
);
1385 tiling
= tile(isl_space_copy(dim
), nvar
, first
,
1386 n_tile
, gen
->block_dim
);
1387 sched
= isl_map_apply_range(sched
, tiling
);
1389 par
= parametrization(dim
, nvar
+ n_tile
, first
+ n_tile
, n_tile
, "t");
1390 sched
= isl_map_intersect_range(sched
, par
);
1392 usched
= isl_union_map_from_map(sched
);
1393 usched
= scale_access_tile_loops(gen
, usched
, nvar
+ n_tile
,
1395 sched
= isl_map_from_union_map(usched
);
1400 /* Given an index expression "pa" into a tile of an array, adjust the expression
1401 * to a shift of the tile to the origin
1402 * (based on the lower bounds in "bound".
1403 * If the index is strided, then we first add
1404 * bound->shift and divide by bound->stride.
1405 * In the end, we compute the gist with respect to "domain".
1407 * All of the input expression "pa", the set "domain" and
1408 * the output are expressed in terms of the AST schedule domain.
1409 * The expressions in "bound" are expressed
1410 * in terms of the first shared_len dimensions of the schedule computed by PPCG.
1411 * The mapping "sched2shared" maps the former domain to the latter domain.
1413 static __isl_give isl_pw_aff
*shift_index(__isl_take isl_pw_aff
*pa
,
1414 struct gpu_array_info
*array
,
1415 struct gpu_array_bound
*bound
, __isl_take isl_set
*domain
,
1416 __isl_take isl_map
*sched2shared
)
1420 isl_pw_multi_aff
*pma
;
1423 map
= isl_map_from_aff(isl_aff_copy(bound
->shift
));
1424 map
= isl_map_apply_range(isl_map_copy(sched2shared
), map
);
1425 pma
= isl_pw_multi_aff_from_map(map
);
1426 tmp
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
1427 isl_pw_multi_aff_free(pma
);
1428 pa
= isl_pw_aff_add(pa
, tmp
);
1429 pa
= isl_pw_aff_scale_down_val(pa
, isl_val_copy(bound
->stride
));
1433 map
= isl_map_from_aff(isl_aff_copy(bound
->lb
));
1434 map
= isl_map_apply_range(sched2shared
, map
);
1435 pma
= isl_pw_multi_aff_from_map(map
);
1436 tmp
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
1437 isl_pw_multi_aff_free(pma
);
1438 pa
= isl_pw_aff_sub(pa
, tmp
);
1439 pa
= isl_pw_aff_coalesce(pa
);
1440 pa
= isl_pw_aff_gist(pa
, domain
);
1445 /* Return the union of all read (read = 1) and/or write (write = 1)
1446 * access relations in the group.
1448 static __isl_give isl_union_map
*group_access_relation(
1449 struct gpu_array_ref_group
*group
, int read
, int write
)
1452 isl_union_map
*access
;
1454 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
1455 for (i
= 0; i
< group
->n_ref
; ++i
) {
1458 if (!((read
&& group
->refs
[i
]->read
) ||
1459 (write
&& group
->refs
[i
]->write
)))
1461 map_i
= isl_map_copy(group
->refs
[i
]->access
);
1462 access
= isl_union_map_union(access
,
1463 isl_union_map_from_map(map_i
));
1469 /* Return a map from the first shared_len dimensions of the computed
1470 * schedule to the values of the given index "i"
1471 * of the elements in the array tile in global memory that corresponds
1472 * to the shared memory copy.
1473 * In particular, if a is the index, then the range of the map
1477 * is constrained as follows
1479 * tile_offset(D) <= a <= tile_offset(D) + tile_size - 1 (1)
1483 * 0 <= a <= array_size - 1 (2)
1486 * Note that if some stride has been detected (i.e., when
1487 * group->shared_tile->bound[i].shift is set), then offset and size (i.e.,
1488 * constraints (1)) apply to the shifted and scaled down copy of the tile.
1489 * These constraints therefore have to be mapped back to the original
1490 * array space using the inverse of the shift_map.
1492 static __isl_give isl_map
*group_tile_dim(struct gpu_array_ref_group
*group
,
1497 isl_map
*map
, *tile
, *gt
;
1500 map
= isl_map_from_aff(isl_aff_copy(group
->shared_tile
->bound
[i
].lb
));
1501 space
= isl_space_range(isl_map_get_space(map
));
1502 map
= isl_map_apply_range(map
, isl_map_lex_le(isl_space_copy(space
)));
1505 aff
= isl_aff_copy(group
->shared_tile
->bound
[i
].lb
);
1506 aff
= isl_aff_add_constant_val(aff
,
1507 isl_val_copy(group
->shared_tile
->bound
[i
].size
));
1508 map
= isl_map_from_aff(aff
);
1509 gt
= isl_map_lex_gt(space
);
1510 map
= isl_map_apply_range(map
, isl_map_copy(gt
));
1511 tile
= isl_map_intersect(tile
, map
);
1513 if (group
->shared_tile
->bound
[i
].shift
) {
1514 isl_basic_map
*shift
;
1515 shift
= isl_basic_map_copy(group
->shared_tile
->bound
[i
].shift_map
);
1516 shift
= isl_basic_map_reverse(shift
);
1517 tile
= isl_set_unwrap(isl_set_apply(isl_map_wrap(tile
),
1518 isl_map_from_basic_map(shift
)));
1521 tile
= isl_map_lower_bound_si(tile
, isl_dim_out
, 0, 0);
1523 bound
= isl_set_from_pw_aff(isl_pw_aff_copy(group
->array
->bound
[i
]));
1524 bound
= isl_set_apply(bound
, gt
);
1525 tile
= isl_map_intersect_range(tile
, bound
);
1530 /* Return a map from the first shared_len dimensions of the computed
1531 * schedule to the array tile in
1532 * global memory that corresponds to the shared memory copy.
1534 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
1537 int n_index
= group
->array
->n_index
;
1540 tile
= group_tile_dim(group
, 0);
1541 for (i
= 1; i
< n_index
; ++i
) {
1544 tile_i
= group_tile_dim(group
, i
);
1545 tile
= isl_map_flat_range_product(tile
, tile_i
);
1548 tile
= isl_map_set_tuple_name(tile
, isl_dim_out
, group
->array
->name
);
1553 /* Given a mapping "sched" from the AST schedule to a domain,
1554 * return the corresponding mapping from the AST schedule to
1555 * to the first shared_len dimensions of the schedule computed by PPCG.
1557 static __isl_give isl_map
*compute_sched_to_shared(struct gpu_gen
*gen
,
1558 __isl_take isl_map
*sched
)
1560 isl_union_map
*umap
;
1564 space
= isl_space_range(isl_map_get_space(sched
));
1565 space
= isl_space_from_domain(space
);
1566 space
= isl_space_add_dims(space
, isl_dim_out
, gen
->shared_len
);
1568 umap
= isl_union_map_copy(gen
->shared_sched
);
1569 umap
= isl_union_map_apply_range(umap
,
1570 isl_union_map_copy(gen
->shared_proj
));
1571 map
= isl_union_map_extract_map(umap
, space
);
1572 isl_union_map_free(umap
);
1574 sched
= isl_map_apply_range(sched
, map
);
1575 sched
= isl_map_detect_equalities(sched
);
1580 /* Set unroll[j] if the input dimension j is involved in
1581 * the index expression represented by ma.
1583 static int check_unroll(__isl_take isl_set
*set
, __isl_take isl_multi_aff
*ma
,
1587 int n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
1588 int n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
1591 for (i
= 0; i
< n_out
; ++i
) {
1594 aff
= isl_multi_aff_get_aff(ma
, i
);
1595 for (j
= 0; j
< n_in
; ++j
)
1596 if (isl_aff_involves_dims(aff
, isl_dim_in
, j
, 1))
1602 isl_multi_aff_free(ma
);
1606 /* Given an array pos mapping input dimensions to the corresponding
1607 * output dimension, construct the corresponding map.
1609 static __isl_give isl_map
*permutation(__isl_take isl_space
*dim
,
1614 isl_basic_map
*bmap
;
1615 isl_local_space
*ls
;
1617 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
1618 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
);
1619 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
1620 ls
= isl_local_space_from_space(dim
);
1622 for (i
= 0; i
< len
; ++i
) {
1623 c
= isl_equality_alloc(isl_local_space_copy(ls
));
1624 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
,
1626 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, pos
[i
],
1628 bmap
= isl_basic_map_add_constraint(bmap
, c
);
1630 isl_local_space_free(ls
);
1632 return isl_map_from_basic_map(bmap
);
1635 /* Find all loops involved in any of the index expressions for any of
1636 * the private accesses, move them innermost and then mark them as
1637 * requiring unrolling by setting gen->first_unroll.
1638 * The loops involved should all be parallel because of the checks
1639 * we performed in check_private_group_access. Moving them innermost
1640 * is therefore a valid transformation.
1642 * Loops up to gen->shared_len are generated before the mapping to
1643 * threads is applied. They should therefore be ignored.
1645 * We compute the hidden equalities of the schedule first
1646 * since we will need them in our calls to isl_pw_multi_aff_from_map
1647 * and because we want to make sure that the same equalities
1648 * are also available to the code generator.
1650 static __isl_give isl_union_map
*interchange_for_unroll(struct gpu_gen
*gen
,
1651 __isl_take isl_union_map
*sched
)
1654 int unroll
[gen
->thread_tiled_len
];
1655 int perm
[gen
->thread_tiled_len
];
1658 int len
= gen
->shared_len
+ gen
->n_parallel
+ gen
->n_block
;
1660 gen
->first_unroll
= -1;
1662 sched
= isl_union_map_detect_equalities(sched
);
1663 for (i
= 0; i
< gen
->thread_tiled_len
; ++i
)
1665 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
1666 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
1668 for (j
= 0; j
< array
->n_group
; ++j
) {
1669 isl_union_map
*access
;
1671 isl_pw_multi_aff
*pma
;
1673 if (!array
->groups
[j
]->private_tile
)
1676 access
= group_access_relation(array
->groups
[j
], 1, 1);
1677 access
= isl_union_map_apply_domain(access
,
1678 isl_union_map_copy(sched
));
1680 acc
= isl_map_from_union_map(access
);
1681 pma
= isl_pw_multi_aff_from_map(acc
);
1682 isl_pw_multi_aff_foreach_piece(pma
,
1683 &check_unroll
, unroll
);
1685 isl_pw_multi_aff_free(pma
);
1689 for (i
= gen
->shared_len
; i
< len
; ++i
)
1696 for (i
= len
; i
< gen
->thread_tiled_len
; ++i
)
1701 for (i
= 0; i
< gen
->shared_len
; ++i
)
1703 for (i
= gen
->shared_len
; i
< gen
->thread_tiled_len
; ++i
)
1706 gen
->first_unroll
= j
- gen
->shared_len
;
1707 for (i
= gen
->shared_len
; i
< len
; ++i
)
1711 dim
= isl_union_map_get_space(sched
);
1712 permute
= permutation(dim
, perm
, gen
->thread_tiled_len
);
1713 sched
= isl_union_map_apply_range(sched
,
1714 isl_union_map_from_map(permute
));
1719 /* Given a constraint
1721 * a(p,i) + j = g f(e)
1723 * or -a(p,i) - j = g f(e) if sign < 0,
1724 * store a(p,i) in bound->shift and g (stride) in bound->stride.
1725 * a(p,i) is assumed to be an expression in only the parameters
1726 * and the input dimensions.
1728 static void extract_stride(__isl_keep isl_constraint
*c
,
1729 struct gpu_array_bound
*bound
, __isl_keep isl_val
*stride
, int sign
)
1738 isl_val_free(bound
->stride
);
1739 bound
->stride
= isl_val_copy(stride
);
1741 space
= isl_constraint_get_space(c
);
1742 space
= isl_space_domain(space
);
1744 nparam
= isl_space_dim(space
, isl_dim_param
);
1745 nvar
= isl_space_dim(space
, isl_dim_set
);
1747 v
= isl_constraint_get_constant_val(c
);
1750 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1751 aff
= isl_aff_set_constant_val(aff
, v
);
1753 for (i
= 0; i
< nparam
; ++i
) {
1754 if (!isl_constraint_involves_dims(c
, isl_dim_param
, i
, 1))
1756 v
= isl_constraint_get_coefficient_val(c
, isl_dim_param
, i
);
1759 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_param
, i
, v
);
1762 for (i
= 0; i
< nvar
; ++i
) {
1763 if (!isl_constraint_involves_dims(c
, isl_dim_in
, i
, 1))
1765 v
= isl_constraint_get_coefficient_val(c
, isl_dim_in
, i
);
1768 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, i
, v
);
1774 /* Given an equality constraint of a map with a single output dimension j,
1775 * check if the constraint is of the form
1777 * a(p,i) + j = g f(e)
1779 * with a(p,i) an expression in the parameters and input dimensions
1780 * and f(e) an expression in the existentially quantified variables.
1781 * If so, and if g is larger than any such g from a previously considered
1782 * constraint, then call extract_stride to record the stride information
1785 static int check_stride_constraint(__isl_take isl_constraint
*c
, void *user
)
1791 struct gpu_array_bound
*bound
= user
;
1793 ctx
= isl_constraint_get_ctx(c
);
1794 n_div
= isl_constraint_dim(c
, isl_dim_div
);
1795 v
= isl_constraint_get_coefficient_val(c
, isl_dim_out
, 0);
1797 if (n_div
&& (isl_val_is_one(v
) || isl_val_is_negone(v
))) {
1798 int s
= isl_val_sgn(v
);
1799 isl_val
*stride
= isl_val_zero(ctx
);
1802 for (i
= 0; i
< n_div
; ++i
) {
1803 v
= isl_constraint_get_coefficient_val(c
,
1805 stride
= isl_val_gcd(stride
, v
);
1807 if (!isl_val_is_zero(stride
) &&
1808 isl_val_gt(stride
, bound
->stride
))
1809 extract_stride(c
, bound
, stride
, s
);
1811 isl_val_free(stride
);
1815 isl_constraint_free(c
);
1819 /* Given contraints on an array index i, check if we can find
1820 * a shift a(p) and a stride g such that
1822 * a(p) + i = 0 mod g
1824 * If so, record the information in bound and apply the mapping
1825 * i -> (i + a(p))/g to the array index in bounds and return
1826 * the new constraints.
1827 * If not, simply return the original constraints.
1829 * If bounds is a subset of the space
1833 * then the bound recorded in bound->shift is of the form
1837 * with s(D) equal to a(p) above.
1838 * The mapping recorded in bound->shift_map is of the form
1840 * [D -> i] -> [D -> (i + S(D))/g]
1842 * This mapping is computed as follows.
1843 * We first introduce "i" in the domain through precomposition
1844 * with [D -> i] -> D obtaining
1848 * Adding [D -> i] -> i produces
1850 * [D -> i] -> i + s(D)
1852 * and the domain product with [D -> i] -> D yields
1854 * [D -> i] -> [D -> i + s(D)]
1856 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
1858 static __isl_give isl_basic_map
*check_stride(struct gpu_array_bound
*bound
,
1859 __isl_take isl_basic_map
*bounds
)
1862 isl_basic_map
*hull
;
1863 isl_basic_map
*shift
, *id
, *bmap
, *scale
;
1864 isl_basic_set
*bset
;
1867 bound
->stride
= NULL
;
1869 hull
= isl_basic_map_affine_hull(isl_basic_map_copy(bounds
));
1871 isl_basic_map_foreach_constraint(hull
, &check_stride_constraint
, bound
);
1873 isl_basic_map_free(hull
);
1878 shift
= isl_basic_map_from_aff(isl_aff_copy(bound
->shift
));
1879 space
= isl_basic_map_get_space(bounds
);
1880 bmap
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
1881 shift
= isl_basic_map_apply_range(bmap
, shift
);
1882 space
= isl_basic_map_get_space(bounds
);
1883 id
= isl_basic_map_range_map(isl_basic_map_universe(space
));
1884 shift
= isl_basic_map_sum(id
, shift
);
1885 space
= isl_basic_map_get_space(bounds
);
1886 id
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
1887 shift
= isl_basic_map_range_product(id
, shift
);
1889 space
= isl_space_domain(isl_basic_map_get_space(bounds
));
1890 id
= isl_basic_map_identity(isl_space_map_from_set(space
));
1891 space
= isl_space_range(isl_basic_map_get_space(bounds
));
1892 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1893 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
1894 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(bound
->stride
));
1895 scale
= isl_basic_map_from_aff(aff
);
1896 scale
= isl_basic_map_product(id
, scale
);
1898 bound
->shift_map
= isl_basic_map_apply_range(shift
, scale
);
1899 bmap
= isl_basic_map_copy(bound
->shift_map
);
1900 bset
= isl_basic_set_apply(isl_basic_map_wrap(bounds
), bmap
);
1901 bounds
= isl_basic_set_unwrap(bset
);
1906 /* Data used in compute_array_dim_size and compute_size_in_direction.
1908 * pos is the position of the variable representing the array index,
1909 * i.e., the variable for which want to compute the size. This variable
1910 * is also the last variable in the set.
1912 struct gpu_size_info
{
1913 isl_basic_set
*bset
;
1914 struct gpu_array_bound
*bound
;
1918 /* Given a constraint from the basic set describing the bounds on
1919 * an array index, check if it is a lower bound, say m i >= b(x), and,
1920 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
1921 * upper bound. If so, and if this bound is smaller than any bound
1922 * derived from earlier constraints, set the size to this bound on
1923 * the expression and the lower bound to ceil(b(x)/m).
1925 static int compute_size_in_direction(__isl_take isl_constraint
*c
, void *user
)
1927 struct gpu_size_info
*size
= user
;
1934 nparam
= isl_basic_set_dim(size
->bset
, isl_dim_param
);
1935 n_div
= isl_constraint_dim(c
, isl_dim_div
);
1937 if (isl_constraint_involves_dims(c
, isl_dim_div
, 0, n_div
) ||
1938 !isl_constraint_is_lower_bound(c
, isl_dim_set
, size
->pos
)) {
1939 isl_constraint_free(c
);
1943 aff
= isl_constraint_get_bound(c
, isl_dim_set
, size
->pos
);
1944 aff
= isl_aff_ceil(aff
);
1946 lb
= isl_aff_copy(aff
);
1948 aff
= isl_aff_neg(aff
);
1949 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, size
->pos
, 1);
1951 v
= isl_basic_set_max_val(size
->bset
, aff
);
1954 if (isl_val_is_int(v
)) {
1955 v
= isl_val_add_ui(v
, 1);
1956 if (!size
->bound
->size
|| isl_val_lt(v
, size
->bound
->size
)) {
1957 isl_val_free(size
->bound
->size
);
1958 size
->bound
->size
= isl_val_copy(v
);
1959 lb
= isl_aff_drop_dims(lb
, isl_dim_in
, size
->pos
, 1);
1960 isl_aff_free(size
->bound
->lb
);
1961 size
->bound
->lb
= isl_aff_copy(lb
);
1967 isl_constraint_free(c
);
1972 /* Given a basic map "bounds" that maps parameters and input dimensions
1973 * to a single output dimension, look for an expression in the parameters
1974 * and input dimensions such that the range of the output dimension shifted
1975 * by this expression is a constant.
1977 * In particular, we currently only consider lower bounds on the output
1978 * dimension as candidate expressions.
1980 static int compute_array_dim_size(struct gpu_array_bound
*bound
,
1981 __isl_take isl_basic_map
*bounds
)
1983 struct gpu_size_info size
;
1985 bounds
= isl_basic_map_detect_equalities(bounds
);
1986 bounds
= check_stride(bound
, bounds
);
1992 size
.pos
= isl_basic_map_dim(bounds
, isl_dim_in
);
1993 size
.bset
= isl_basic_map_wrap(bounds
);
1994 size
.bset
= isl_basic_set_flatten(size
.bset
);
1995 size
.bset
= isl_set_simple_hull(isl_basic_set_compute_divs(size
.bset
));
1996 isl_basic_set_foreach_constraint(size
.bset
, &compute_size_in_direction
,
1998 isl_basic_set_free(size
.bset
);
2000 return bound
->size
? 0 : -1;
2003 /* Check if we can find a memory tile for the given array
2004 * based on the given accesses, and if so, put the results in "tile".
2006 * We project the accesses on each index in turn and look for a parametric
2007 * offset such that the size is constant.
2009 static int can_tile(__isl_keep isl_map
*access
, struct gpu_array_tile
*tile
)
2013 for (i
= 0; i
< tile
->n
; ++i
) {
2015 isl_basic_map
*hull
;
2017 access_i
= isl_map_copy(access
);
2018 access_i
= isl_map_project_out(access_i
, isl_dim_out
, 0, i
);
2019 access_i
= isl_map_project_out(access_i
, isl_dim_out
,
2020 1, tile
->n
- (i
+ 1));
2021 access_i
= isl_map_compute_divs(access_i
);
2022 hull
= isl_map_simple_hull(access_i
);
2023 if (compute_array_dim_size(&tile
->bound
[i
], hull
) < 0)
2030 /* Construct a map with input the shared tile loops and the loops that
2031 * will be wrapped around the threads that relates these later loops
2032 * to the thread indices and then projects them out.
2034 static __isl_give isl_map
*compute_privatization(struct gpu_gen
*gen
)
2042 dim
= isl_union_map_get_space(gen
->shared_sched
);
2044 if (gen
->options
->wrap
)
2045 tiling
= wrap(isl_space_copy(dim
), gen
->shared_len
+ gen
->n_block
,
2046 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
2048 tiling
= tile(isl_space_copy(dim
), gen
->shared_len
+ gen
->n_block
,
2049 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
2053 par
= parametrization(dim
, gen
->shared_len
+ 2 * gen
->n_block
,
2054 gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
+ gen
->n_block
,
2057 priv
= isl_map_align_params(priv
, isl_set_get_space(par
));
2058 priv
= isl_map_intersect_range(priv
, par
);
2060 dim
= isl_map_get_space(priv
);
2061 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
2062 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
2063 proj
= projection(dim
, gen
->shared_len
+ 2 * gen
->n_block
,
2066 priv
= isl_map_apply_range(priv
, proj
);
2071 /* Construct a map from domain_dim to domain_dim that increments
2072 * the dimension at position "pos" and leaves all other dimensions
2075 static __isl_give isl_map
*next(__isl_take isl_space
*domain_dim
, int pos
)
2078 int len
= isl_space_dim(domain_dim
, isl_dim_set
);
2080 isl_basic_map
*next
;
2081 isl_local_space
*ls
;
2083 dim
= isl_space_map_from_set(domain_dim
);
2084 next
= isl_basic_map_universe(isl_space_copy(dim
));
2085 ls
= isl_local_space_from_space(dim
);
2087 for (i
= 0; i
< len
; ++i
) {
2090 c
= isl_equality_alloc(isl_local_space_copy(ls
));
2091 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, 1);
2092 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
2094 c
= isl_constraint_set_constant_si(c
, 1);
2095 next
= isl_basic_map_add_constraint(next
, c
);
2098 isl_local_space_free(ls
);
2100 return isl_map_from_basic_map(next
);
2103 /* Check if the given access is coalesced.
2104 * That is, check whether incrementing the dimension that will get
2105 * wrapped over the last thread index results in incrementing
2106 * the last array index.
2108 * This function is only called for access relations without reuse.
2110 static int access_is_coalesced(struct gpu_gen
*gen
,
2111 __isl_keep isl_union_map
*access
)
2114 isl_map
*access_map
;
2115 isl_map
*next_thread_x
;
2116 isl_map
*next_element
;
2120 access
= isl_union_map_copy(access
);
2121 access
= isl_union_map_apply_domain(access
,
2122 isl_union_map_copy(gen
->tiled_sched
));
2123 access_map
= isl_map_from_union_map(access
);
2125 dim
= isl_map_get_space(access_map
);
2126 dim
= isl_space_domain(dim
);
2127 next_thread_x
= next(dim
, gen
->shared_len
+ gen
->n_block
- 1);
2129 dim
= isl_map_get_space(access_map
);
2130 dim
= isl_space_range(dim
);
2131 next_element
= next(dim
, isl_space_dim(dim
, isl_dim_set
) - 1);
2133 map
= isl_map_apply_domain(next_thread_x
, isl_map_copy(access_map
));
2134 map
= isl_map_apply_range(map
, access_map
);
2136 coalesced
= isl_map_is_subset(map
, next_element
);
2138 isl_map_free(next_element
);
2144 /* Given an access relation in terms of the first gen->shared_len + gen->n_block
2145 * dimensions of the computed schedule, check if it is bijective for
2146 * fixed values of the first gen->shared_len dimensions.
2147 * We perform this check by equating these dimensions to parameters.
2149 static int access_is_bijective(struct gpu_gen
*gen
, __isl_keep isl_map
*access
)
2155 access
= isl_map_copy(access
);
2156 space
= isl_space_params(isl_map_get_space(access
));
2157 par
= parametrization(space
, gen
->shared_len
+ gen
->n_block
,
2158 0, gen
->shared_len
, "s");
2159 access
= isl_map_intersect_domain(access
, par
);
2160 res
= isl_map_is_bijective(access
);
2161 isl_map_free(access
);
2166 /* Look for the last shared tile loop that affects the offset of "tile"
2167 * and return the result.
2168 * If there is no such loop, then return the index of the loop
2169 * before the first shared tile loop, in particular gen->tile_first - 1.
2171 static int compute_tile_last_shared(struct gpu_gen
*gen
,
2172 struct gpu_array_tile
*tile
)
2176 for (j
= gen
->shared_len
- 1; j
>= gen
->tile_first
; --j
) {
2177 for (i
= 0; i
< tile
->n
; ++i
) {
2181 lb
= tile
->bound
[i
].lb
;
2182 if (isl_aff_involves_dims(lb
, isl_dim_in
, j
, 1))
2185 shift
= tile
->bound
[i
].shift
;
2188 if (isl_aff_involves_dims(shift
, isl_dim_in
, j
, 1))
2198 /* Look for the last shared tile loop that affects the offset of the
2199 * shared or private tile and store the result in group->last_shared.
2200 * If there is no such loop, then group->last_shared is set to a value
2201 * before the first shared tile loop, in particular gen->tile_first - 1.
2202 * If there is no tile defined on the array reference group,
2203 * then set group->last_shared to gen->shared_len - 1.
2205 static void set_last_shared(struct gpu_gen
*gen
,
2206 struct gpu_array_ref_group
*group
)
2208 struct gpu_array_tile
*tile
;
2210 group
->last_shared
= gen
->shared_len
- 1;
2212 tile
= group
->private_tile
;
2214 tile
= group
->shared_tile
;
2218 group
->last_shared
= compute_tile_last_shared(gen
, tile
);
2221 /* Compute a privatized copy of all access relations from reference groups that
2222 * are mapped to private memory and store the result in gen->privatization.
2224 static void compute_private_access(struct gpu_gen
*gen
)
2227 isl_union_map
*private;
2229 if (!gen
->options
->use_private_memory
)
2232 private = isl_union_map_empty(isl_union_map_get_space(gen
->shared_sched
));
2234 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2235 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2237 if (gpu_array_is_read_only_scalar(array
))
2240 for (j
= 0; j
< array
->n_group
; ++j
) {
2241 if (!array
->groups
[j
]->private_tile
)
2244 private = isl_union_map_union(private,
2245 group_access_relation(array
->groups
[j
], 1, 1));
2249 if (isl_union_map_is_empty(private))
2250 isl_union_map_free(private);
2252 isl_union_map
*priv
;
2254 private = isl_union_map_apply_domain(private,
2255 isl_union_map_copy(gen
->shared_sched
));
2256 priv
= isl_union_map_from_map(isl_map_copy(gen
->privatization
));
2257 private = isl_union_map_apply_domain(private, priv
);
2258 gen
->private_access
= private;
2262 /* Compute the size of the tile specified by "tile"
2263 * in number of elements and return the result.
2265 static __isl_give isl_val
*tile_size(isl_ctx
*ctx
, struct gpu_array_tile
*tile
)
2270 size
= isl_val_one(ctx
);
2272 for (i
= 0; i
< tile
->n
; ++i
)
2273 size
= isl_val_mul(size
, isl_val_copy(tile
->bound
[i
].size
));
2278 /* If max_shared_memory is not set to infinity (-1), then make
2279 * sure that the total amount of shared memory required by the
2280 * array reference groups mapped to shared memory is no larger
2281 * than this maximum.
2283 * We apply a greedy approach and discard (keep in global memory)
2284 * those groups that would result in a total memory size that
2285 * is larger than the maximum.
2287 static void check_shared_memory_bound(struct gpu_gen
*gen
)
2290 isl_val
*left
, *size
;
2292 if (gen
->options
->max_shared_memory
< 0)
2295 left
= isl_val_int_from_si(gen
->ctx
, gen
->options
->max_shared_memory
);
2297 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2298 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2300 for (j
= 0; j
< array
->n_group
; ++j
) {
2301 struct gpu_array_ref_group
*group
;
2303 group
= array
->groups
[j
];
2304 if (!group
->shared_tile
)
2307 size
= tile_size(gen
->ctx
, group
->shared_tile
);
2308 size
= isl_val_mul_ui(size
, array
->size
);
2310 if (isl_val_le(size
, left
)) {
2311 left
= isl_val_sub(left
, size
);
2316 group
->shared_tile
= free_tile(group
->shared_tile
);
2323 /* Fill up the groups array with singleton groups, i.e., one group
2324 * per reference, initializing the array, access, write, n_ref and refs fields.
2325 * In particular the access field is initialized to the scheduled
2326 * access relation of the array reference.
2328 * Return the number of elements initialized, i.e., the number of
2329 * active references in the current kernel.
2331 static int populate_array_references(struct gpu_array_info
*array
,
2332 __isl_keep isl_union_map
*sched
, struct gpu_array_ref_group
**groups
)
2336 isl_ctx
*ctx
= isl_union_map_get_ctx(sched
);
2339 for (i
= 0; i
< array
->n_ref
; ++i
) {
2340 isl_union_map
*umap
;
2342 struct gpu_array_ref_group
*group
;
2343 struct gpu_stmt_access
*access
= array
->refs
[i
];
2345 map
= isl_map_copy(access
->access
);
2346 umap
= isl_union_map_from_map(map
);
2347 umap
= isl_union_map_apply_domain(umap
,
2348 isl_union_map_copy(sched
));
2350 if (isl_union_map_is_empty(umap
)) {
2351 isl_union_map_free(umap
);
2355 map
= isl_map_from_union_map(umap
);
2356 map
= isl_map_detect_equalities(map
);
2358 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
2360 group
->array
= array
;
2361 group
->access
= map
;
2362 group
->write
= access
->write
;
2363 group
->refs
= &array
->refs
[i
];
2366 groups
[n
++] = group
;
2372 /* If group->n_ref == 1, then group->refs was set by
2373 * populate_array_references to point directly into
2374 * group->array->refs and should not be freed.
2375 * If group->n_ref > 1, then group->refs was set by join_groups
2376 * to point to a newly allocated array.
2378 static void free_array_ref_group(struct gpu_array_ref_group
*group
)
2382 free_tile(group
->shared_tile
);
2383 free_tile(group
->private_tile
);
2384 isl_map_free(group
->access
);
2385 if (group
->n_ref
> 1)
2390 /* Given a map where the input dimensions represent the tile loops,
2391 * eliminate the innermost of those that have a fixed value
2392 * until we reach one that does not (obviously) have a fixed value.
2394 static __isl_give isl_map
*eliminate_fixed_inner_loops(
2395 __isl_take isl_map
*access
)
2399 n
= isl_map_dim(access
, isl_dim_in
);
2401 for (i
= n
- 1; i
>= 0; --i
) {
2402 if (!isl_map_plain_is_fixed(access
, isl_dim_in
, i
, NULL
))
2404 access
= isl_map_eliminate(access
, isl_dim_in
, i
, 1);
2409 /* Check if the access relations of group1 and group2 overlap within
2410 * the innermost loop. In particular, ignore any inner dimension
2411 * with a fixed value.
2412 * The copying to and from shared memory will be performed within
2413 * the innermost actual loop so we are only allowed to consider
2414 * the dimensions up to that innermost loop while checking whether
2415 * two access relations overlap.
2417 static int accesses_overlap(struct gpu_array_ref_group
*group1
,
2418 struct gpu_array_ref_group
*group2
)
2421 isl_map
*access1
, *access2
;
2423 access1
= isl_map_copy(group1
->access
);
2424 access1
= eliminate_fixed_inner_loops(access1
);
2425 access2
= isl_map_copy(group2
->access
);
2426 access2
= eliminate_fixed_inner_loops(access2
);
2427 access1
= isl_map_intersect(access1
, access2
);
2428 empty
= isl_map_is_empty(access1
);
2429 isl_map_free(access1
);
2434 /* Combine the given two groups into a single group, containing
2435 * the references of both groups.
2437 static struct gpu_array_ref_group
*join_groups(
2438 struct gpu_array_ref_group
*group1
,
2439 struct gpu_array_ref_group
*group2
)
2443 struct gpu_array_ref_group
*group
;
2445 ctx
= isl_map_get_ctx(group1
->access
);
2446 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
2448 group
->array
= group1
->array
;
2449 group
->access
= isl_map_union(isl_map_copy(group1
->access
),
2450 isl_map_copy(group2
->access
));
2451 group
->write
= group1
->write
|| group2
->write
;
2452 group
->n_ref
= group1
->n_ref
+ group2
->n_ref
;
2453 group
->refs
= isl_alloc_array(ctx
, struct gpu_stmt_access
*,
2455 assert(group
->refs
);
2456 for (i
= 0; i
< group1
->n_ref
; ++i
)
2457 group
->refs
[i
] = group1
->refs
[i
];
2458 for (i
= 0; i
< group2
->n_ref
; ++i
)
2459 group
->refs
[group1
->n_ref
+ i
] = group2
->refs
[i
];
2464 /* Combine the given two groups into a single group and free
2465 * the original two groups.
2467 static struct gpu_array_ref_group
*join_groups_and_free(
2468 struct gpu_array_ref_group
*group1
,
2469 struct gpu_array_ref_group
*group2
)
2471 struct gpu_array_ref_group
*group
;
2473 group
= join_groups(group1
, group2
);
2474 free_array_ref_group(group1
);
2475 free_array_ref_group(group2
);
2479 /* Compute the private and/or shared memory tiles for the array
2480 * reference group "group" of array "array".
2482 * If the array is a read-only scalar or if the user requested
2483 * not to use shared or private memory, then we do not need to do anything.
2485 * We only try to compute a shared memory tile if there is any reuse
2486 * or if the access is not coalesced.
2488 * For computing a private memory tile, we also require that there is
2489 * some reuse. Moreover, we require that the access is private
2490 * to the thread. That is, we check that any given array element
2491 * is only accessed by a single thread.
2492 * We compute an access relation that maps the shared tile loop iterators
2493 * and the shared point loop iterators that will be wrapped over the
2494 * threads to the array elements.
2495 * We actually check that those iterators that will be wrapped
2496 * partition the array space. This check is stricter than necessary
2497 * since several iterations may be mapped onto the same thread
2498 * and then they could be allowed to access the same memory elements,
2499 * but our check does not allow this situation.
2501 * We also check that the index expression only depends on parallel
2502 * loops. That way, we can move those loops innermost and unroll them.
2503 * Again, we use a test that is stricter than necessary.
2504 * We actually check whether the index expression only depends
2505 * on the iterators that are wrapped over the threads.
2506 * These are necessarily parallel, but there may be more parallel loops.
2508 * Combining the injectivity of the first test with the single-valuedness
2509 * of the second test, we simply test for bijectivity.
2511 * If it turns out we can use registers, we compute the private memory
2512 * tile size using can_tile, after introducing a dependence
2513 * on the thread indices.
2515 static void compute_group_bounds_core(struct gpu_gen
*gen
,
2516 struct gpu_array_ref_group
*group
)
2518 isl_ctx
*ctx
= isl_space_get_ctx(group
->array
->dim
);
2519 isl_union_map
*access
;
2520 int n_index
= group
->array
->n_index
;
2523 int use_shared
= gen
->options
->use_shared_memory
;
2524 int use_private
= gen
->options
->use_private_memory
;
2526 if (!use_shared
&& !use_private
)
2528 if (gpu_array_is_read_only_scalar(group
->array
))
2531 access
= group_access_relation(group
, 1, 1);
2532 no_reuse
= isl_union_map_is_injective(access
);
2534 if (use_shared
&& (!no_reuse
|| !access_is_coalesced(gen
, access
))) {
2535 group
->shared_tile
= create_tile(ctx
, group
->array
->n_index
);
2536 if (!can_tile(group
->access
, group
->shared_tile
))
2537 group
->shared_tile
= free_tile(group
->shared_tile
);
2540 if (!use_private
|| no_reuse
) {
2541 isl_union_map_free(access
);
2545 access
= isl_union_map_apply_domain(access
,
2546 isl_union_map_copy(gen
->shared_sched
));
2548 acc
= isl_map_from_union_map(access
);
2550 if (!access_is_bijective(gen
, acc
)) {
2555 group
->private_tile
= create_tile(gen
->ctx
, n_index
);
2556 acc
= isl_map_apply_domain(acc
, isl_map_copy(gen
->privatization
));
2557 if (!can_tile(acc
, group
->private_tile
))
2558 group
->private_tile
= free_tile(group
->private_tile
);
2563 /* Compute the private and/or shared memory tiles for the array
2564 * reference group "group" of array "array" and set last_shared.
2566 static void compute_group_bounds(struct gpu_gen
*gen
,
2567 struct gpu_array_ref_group
*group
)
2569 compute_group_bounds_core(gen
, group
);
2570 set_last_shared(gen
, group
);
2573 /* If two groups have overlapping access relations (as determined by
2574 * the "overlap" function) and if one of them involves a write,
2575 * then merge the two groups into one.
2576 * If "compute_bounds" is set, then call compute_group_bounds
2577 * on the merged groups.
2579 * Return the updated number of groups.
2581 static int group_writes(struct gpu_gen
*gen
,
2582 int n
, struct gpu_array_ref_group
**groups
,
2583 int (*overlap
)(struct gpu_array_ref_group
*group1
,
2584 struct gpu_array_ref_group
*group2
), int compute_bounds
)
2588 for (i
= 0; i
< n
; ++i
) {
2589 for (j
= n
- 1; j
> i
; --j
) {
2590 if (!groups
[i
]->write
&& !groups
[j
]->write
)
2593 if (!overlap(groups
[i
], groups
[j
]))
2596 groups
[i
] = join_groups_and_free(groups
[i
], groups
[j
]);
2598 compute_group_bounds(gen
, groups
[i
]);
2600 groups
[j
] = groups
[n
- 1];
2608 /* If two groups have overlapping access relations (within the innermost
2609 * loop) and if one of them involves a write, then merge the two groups
2612 * Return the updated number of groups.
2614 static int group_overlapping_writes(struct gpu_gen
*gen
,
2615 int n
, struct gpu_array_ref_group
**groups
)
2617 return group_writes(gen
, n
, groups
, &accesses_overlap
, 0);
2620 /* Check if the access relations of group1 and group2 overlap within
2621 * the outermost min(group1->last_shared, group2->last_shared) loops.
2623 static int last_shared_accesses_overlap(struct gpu_array_ref_group
*group1
,
2624 struct gpu_array_ref_group
*group2
)
2629 isl_map
*map_i
, *map_j
, *map
;
2631 last_shared
= group1
->last_shared
;
2632 if (group2
->last_shared
< last_shared
)
2633 last_shared
= group2
->last_shared
;
2634 map_i
= isl_map_copy(group1
->access
);
2635 dim
= isl_map_dim(map_i
, isl_dim_in
);
2636 map_i
= isl_map_eliminate(map_i
, isl_dim_in
,
2637 last_shared
+ 1, dim
- (last_shared
+ 1));
2638 map_j
= isl_map_copy(group2
->access
);
2639 map_j
= isl_map_eliminate(map_j
, isl_dim_in
,
2640 last_shared
+ 1, dim
- (last_shared
+ 1));
2641 map
= isl_map_intersect(map_i
, map_j
);
2642 empty
= isl_map_is_empty(map
);
2648 /* If two groups have overlapping access relations (within the outer
2649 * last_shared loops) and if one of them involves a write,
2650 * then merge the two groups into one.
2652 * Return the updated number of groups.
2654 static int group_last_shared_overlapping_writes(struct gpu_gen
*gen
, int n
,
2655 struct gpu_array_ref_group
**groups
)
2657 return group_writes(gen
, n
, groups
, &last_shared_accesses_overlap
, 1);
2660 /* Is the size of the tile specified by "tile" smaller than the sum of
2661 * the sizes of the tiles specified by "tile1" and "tile2"?
2663 static int smaller_tile(isl_ctx
*ctx
, struct gpu_array_tile
*tile
,
2664 struct gpu_array_tile
*tile1
, struct gpu_array_tile
*tile2
)
2667 isl_val
*size
, *size1
, *size2
;
2669 size
= tile_size(ctx
, tile
);
2670 size1
= tile_size(ctx
, tile1
);
2671 size2
= tile_size(ctx
, tile2
);
2673 size
= isl_val_sub(size
, size1
);
2674 size
= isl_val_sub(size
, size2
);
2675 smaller
= isl_val_is_neg(size
);
2682 /* Given an initial grouping of array references and shared memory tiles
2683 * for each group that allows for a shared memory tile, merge two groups
2684 * if both have a shared memory tile, the merged group also has
2685 * a shared memory tile and the size of the tile for the merge group
2686 * is smaller than the sum of the tile sizes of the individual groups.
2688 * If merging two groups decreases the "last_shared" dimension of
2689 * one or both of the two groups, then we need to check for overlapping
2692 * Return the number of groups after merging.
2694 static int group_common_shared_memory_tile(struct gpu_gen
*gen
,
2695 struct gpu_array_info
*array
, int n
,
2696 struct gpu_array_ref_group
**groups
)
2699 int recompute_overlap
= 0;
2700 isl_ctx
*ctx
= isl_space_get_ctx(array
->dim
);
2702 for (i
= 0; i
< n
; ++i
) {
2703 if (!groups
[i
]->shared_tile
)
2705 for (j
= n
- 1; j
> i
; --j
) {
2708 struct gpu_array_ref_group
*group
;
2710 if (!groups
[j
]->shared_tile
)
2713 map
= isl_map_intersect(isl_map_copy(groups
[i
]->access
),
2714 isl_map_copy(groups
[j
]->access
));
2715 empty
= isl_map_is_empty(map
);
2721 group
= join_groups(groups
[i
], groups
[j
]);
2722 compute_group_bounds(gen
, group
);
2723 if (!group
->shared_tile
||
2724 !smaller_tile(ctx
, group
->shared_tile
,
2725 groups
[i
]->shared_tile
,
2726 groups
[j
]->shared_tile
)) {
2727 free_array_ref_group(group
);
2731 if (group
->last_shared
< groups
[i
]->last_shared
||
2732 group
->last_shared
< groups
[j
]->last_shared
)
2733 recompute_overlap
= 1;
2734 free_array_ref_group(groups
[i
]);
2735 free_array_ref_group(groups
[j
]);
2738 groups
[j
] = groups
[n
- 1];
2743 if (recompute_overlap
)
2744 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
2748 /* Set array->n_group and array->groups to n and groups.
2750 * Additionally, set the "nr" field of each group
2751 * and the "group" field of each reference in each group.
2753 static void set_array_groups(struct gpu_array_info
*array
,
2754 int n
, struct gpu_array_ref_group
**groups
)
2759 array
->groups
= groups
;
2761 for (i
= 0; i
< n
; ++i
) {
2764 for (j
= 0; j
< groups
[i
]->n_ref
; ++j
)
2765 groups
[i
]->refs
[j
]->group
= i
;
2769 /* Group array references that should be considered together when
2770 * deciding whether to access them from private, shared or global memory.
2772 * In particular, if two array references overlap and if one of them
2773 * is a write, then the two references are grouped together.
2774 * We first perform an initial grouping based only on the access relation.
2775 * After computing shared and private memory tiles, we check for
2776 * overlapping writes again, but this time taking into account
2777 * the "last_shared" property.
2779 * Furthermore, if two groups admit a shared memory tile and if the
2780 * combination of the two also admits a shared memory tile, we merge
2783 static void group_array_references(struct gpu_gen
*gen
,
2784 struct gpu_array_info
*array
, __isl_keep isl_union_map
*sched
)
2788 isl_ctx
*ctx
= isl_union_map_get_ctx(sched
);
2789 struct gpu_array_ref_group
**groups
;
2791 groups
= isl_calloc_array(ctx
, struct gpu_array_ref_group
*,
2795 n
= populate_array_references(array
, sched
, groups
);
2797 n
= group_overlapping_writes(gen
, n
, groups
);
2799 for (i
= 0; i
< n
; ++i
)
2800 compute_group_bounds(gen
, groups
[i
]);
2802 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
2804 n
= group_common_shared_memory_tile(gen
, array
, n
, groups
);
2806 set_array_groups(array
, n
, groups
);
2809 /* Take tiled_sched, project it onto the shared tile loops and
2810 * the loops that will be wrapped over the threads and
2811 * store the result in gen->shared_sched.
2812 * Also compute a projection that projects out the loops that will be
2813 * wrapped over the threads and store this projection in gen->shared_proj.
2815 static void compute_shared_sched(struct gpu_gen
*gen
)
2820 isl_union_map
*sched
;
2822 sched
= isl_union_map_copy(gen
->tiled_sched
);
2824 dim
= isl_union_map_get_space(sched
);
2825 proj
= projection(dim
, gen
->tiled_len
, gen
->shared_len
+ gen
->n_block
);
2826 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
2828 dim
= isl_union_map_get_space(sched
);
2829 proj
= projection(dim
, gen
->shared_len
+ gen
->n_block
, gen
->shared_len
);
2831 gen
->shared_sched
= sched
;
2832 gen
->shared_proj
= isl_union_map_from_map(proj
);
2835 /* Group references of all arrays in the program.
2837 static void group_references(struct gpu_gen
*gen
)
2840 isl_union_map
*sched
;
2842 sched
= isl_union_map_apply_range(isl_union_map_copy(gen
->shared_sched
),
2843 isl_union_map_copy(gen
->shared_proj
));
2845 for (i
= 0; i
< gen
->prog
->n_array
; ++i
)
2846 group_array_references(gen
, &gen
->prog
->array
[i
], sched
);
2848 isl_union_map_free(sched
);
2851 /* Free all array information that is local to the current kernel.
2853 static void free_local_array_info(struct gpu_gen
*gen
)
2857 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2858 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2860 for (j
= 0; j
< array
->n_group
; ++j
)
2861 free_array_ref_group(array
->groups
[j
]);
2862 free(array
->groups
);
2866 /* Compute the effective grid size as a list of the sizes in each dimension.
2868 * The grid size specified by the user or set by default
2869 * in read_grid_sizes() and applied in tile_schedule(),
2870 * may be too large for the given code in the sense that
2871 * it may contain blocks that don't need to execute anything.
2872 * We therefore don't return this grid size, but instead the
2873 * smallest grid size that ensures that all blocks that actually
2874 * execute code are included in the grid.
2876 * We first extract a description of the grid, i.e., the possible values
2877 * of the block ids, from gen->tiled_sched.
2878 * The block ids are parameters in gen->tiled_sched.
2879 * We simply need to change them into set dimensions.
2881 * Then, for each block dimension, we compute the maximal value of the block id
2884 static __isl_give isl_multi_pw_aff
*extract_grid_size(struct gpu_gen
*gen
,
2885 struct ppcg_kernel
*kernel
)
2889 isl_multi_pw_aff
*mpa
;
2891 grid
= isl_union_map_params(isl_union_map_copy(gen
->tiled_sched
));
2892 grid
= isl_set_from_params(grid
);
2893 grid
= isl_set_add_dims(grid
, isl_dim_set
, gen
->n_grid
);
2894 for (i
= 0; i
< gen
->n_grid
; ++i
) {
2898 snprintf(name
, sizeof(name
), "b%d", i
);
2899 pos
= isl_set_find_dim_by_name(grid
, isl_dim_param
, name
);
2901 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
2902 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
2905 mpa
= isl_multi_pw_aff_zero(isl_set_get_space(grid
));
2906 for (i
= 0; i
< gen
->n_grid
; ++i
) {
2911 bound
= isl_set_dim_max(isl_set_copy(grid
), i
);
2912 bound
= isl_pw_aff_coalesce(bound
);
2913 bound
= isl_pw_aff_gist(bound
, isl_set_copy(kernel
->context
));
2915 space
= isl_pw_aff_get_domain_space(bound
);
2916 one
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2917 one
= isl_aff_add_constant_si(one
, 1);
2918 bound
= isl_pw_aff_add(bound
, isl_pw_aff_from_aff(one
));
2919 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, bound
);
2926 void ppcg_kernel_free(void *user
)
2928 struct ppcg_kernel
*kernel
= user
;
2934 isl_multi_pw_aff_free(kernel
->grid_size
);
2935 isl_set_free(kernel
->context
);
2936 isl_union_set_free(kernel
->arrays
);
2937 isl_space_free(kernel
->space
);
2938 isl_ast_node_free(kernel
->tree
);
2940 for (i
= 0; i
< kernel
->n_array
; ++i
)
2941 isl_pw_aff_list_free(kernel
->array
[i
].bound
);
2942 free(kernel
->array
);
2944 for (i
= 0; i
< kernel
->n_var
; ++i
) {
2945 free(kernel
->var
[i
].name
);
2946 isl_vec_free(kernel
->var
[i
].size
);
2953 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
2954 struct ppcg_kernel_var
*var
)
2957 struct gpu_array_tile
*tile
;
2961 var
->array
= group
->array
;
2963 tile
= group
->private_tile
;
2964 var
->type
= ppcg_access_private
;
2966 tile
= group
->shared_tile
;
2967 var
->type
= ppcg_access_shared
;
2970 p
= isl_printer_to_str(ctx
);
2971 p
= print_array_name(p
, group
);
2972 var
->name
= isl_printer_get_str(p
);
2973 isl_printer_free(p
);
2975 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
2977 for (j
= 0; j
< group
->array
->n_index
; ++j
)
2978 var
->size
= isl_vec_set_element_val(var
->size
, j
,
2979 isl_val_copy(tile
->bound
[j
].size
));
2982 static void create_kernel_vars(struct gpu_gen
*gen
, struct ppcg_kernel
*kernel
)
2987 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2988 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2990 for (j
= 0; j
< array
->n_group
; ++j
) {
2991 struct gpu_array_ref_group
*group
= array
->groups
[j
];
2992 if (group
->private_tile
|| group
->shared_tile
)
2998 kernel
->var
= isl_calloc_array(gen
->ctx
, struct ppcg_kernel_var
, n
);
2999 assert(kernel
->var
);
3002 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3003 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3005 for (j
= 0; j
< array
->n_group
; ++j
) {
3006 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3007 if (!group
->private_tile
&& !group
->shared_tile
)
3009 create_kernel_var(gen
->ctx
, group
, &kernel
->var
[n
]);
3015 /* The sizes of the arrays on the host that have been computed by
3016 * extract_array_info may depend on the parameters. Use the extra
3017 * constraints on the parameters that are valid at "host_domain"
3018 * to simplify these expressions and store the results in kernel->array.
3020 static void localize_bounds(struct gpu_gen
*gen
, struct ppcg_kernel
*kernel
,
3021 __isl_keep isl_set
*host_domain
)
3026 kernel
->array
= isl_calloc_array(gen
->ctx
,
3027 struct gpu_local_array_info
, gen
->prog
->n_array
);
3028 assert(kernel
->array
);
3029 kernel
->n_array
= gen
->prog
->n_array
;
3031 context
= isl_set_copy(host_domain
);
3032 context
= isl_set_params(context
);
3034 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3035 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3036 isl_pw_aff_list
*local
;
3038 if (array
->n_group
== 0)
3041 local
= isl_pw_aff_list_alloc(gen
->ctx
, array
->n_index
);
3043 for (j
= 0; j
< array
->n_index
; ++j
) {
3046 pwaff
= isl_pw_aff_copy(array
->bound
[j
]);
3047 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
3048 local
= isl_pw_aff_list_add(local
, pwaff
);
3051 kernel
->array
[i
].bound
= local
;
3053 isl_set_free(context
);
3056 /* Find the element in gen->stmt that has the given "id".
3057 * Return NULL if no such gpu_stmt can be found.
3059 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
3063 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
3064 if (id
== prog
->stmts
[i
].id
)
3068 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
3071 /* Set gen->tile_len and gen->n_parallel to those of the statement
3072 * affected by the first map (part of the schedule)
3073 * on which this function is called.
3074 * Because of the way the schedule is constructed, the other statements
3075 * in the list, if any, should have the same values for these properties.
3077 static int extract_tile_len(__isl_take isl_map
*map
, void *user
)
3079 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3081 struct gpu_stmt
*stmt
;
3083 id
= isl_map_get_tuple_id(map
, isl_dim_in
);
3084 stmt
= find_stmt(gen
->prog
, id
);
3090 isl_die(gen
->ctx
, isl_error_unknown
,
3091 "statement not found", return -1);
3093 gen
->tile_len
= stmt
->tile_len
;
3094 gen
->n_parallel
= stmt
->n_parallel
;
3099 void ppcg_kernel_stmt_free(void *user
)
3102 struct ppcg_kernel_stmt
*stmt
= user
;
3107 switch (stmt
->type
) {
3108 case ppcg_kernel_copy
:
3109 isl_ast_expr_free(stmt
->u
.c
.index
);
3110 isl_ast_expr_free(stmt
->u
.c
.local_index
);
3112 case ppcg_kernel_domain
:
3113 for (i
= 0; i
< stmt
->u
.d
.n_access
; ++i
) {
3114 isl_ast_expr_list_free(stmt
->u
.d
.access
[i
].index
);
3115 free(stmt
->u
.d
.access
[i
].local_name
);
3117 free(stmt
->u
.d
.access
);
3119 case ppcg_kernel_sync
:
3126 /* Set the options of "context" to
3128 * { space -> [x] : x >= first }
3130 static __isl_give isl_ast_build
*set_unroll(
3131 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
,
3138 ctx
= isl_ast_build_get_ctx(build
);
3140 space
= isl_space_from_domain(space
);
3141 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3142 space
= isl_space_set_tuple_name(space
, isl_dim_out
, "unroll");
3143 unroll
= isl_map_universe(space
);
3144 unroll
= isl_map_lower_bound_si(unroll
, isl_dim_out
, 0, first
);
3145 opt
= isl_union_map_from_map(unroll
);
3147 build
= isl_ast_build_set_options(build
, opt
);
3152 /* Return a list of isl_ids of the form "prefix%d".
3154 static __isl_give isl_id_list
*generate_names(isl_ctx
*ctx
,
3155 int n
, const char *prefix
)
3161 names
= isl_id_list_alloc(ctx
, n
);
3162 for (i
= 0; i
< n
; ++i
) {
3165 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
3166 id
= isl_id_alloc(ctx
, name
, NULL
);
3167 names
= isl_id_list_add(names
, id
);
3173 /* Extend the schedule "schedule" with the part of "extension"
3174 * starting at "first" up to "len".
3176 static __isl_give isl_union_map
*extend_schedule(
3177 __isl_take isl_union_map
*schedule
,
3178 __isl_take isl_union_map
*extension
, int first
, int len
)
3182 isl_union_map
*umap
;
3185 space
= isl_union_map_get_space(schedule
);
3186 space
= isl_space_set_from_params(space
);
3187 space
= isl_space_add_dims(space
, isl_dim_set
, len
);
3188 proj
= isl_set_identity(isl_set_universe(space
));
3189 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, first
);
3190 extension
= isl_union_map_apply_range(extension
,
3191 isl_union_map_from_map(proj
));
3193 schedule
= isl_union_map_range_product(schedule
, extension
);
3198 /* This function is called for each access to an array in each instance
3199 * in the kernel of some statement in the original code.
3200 * Replace that access by an access to global, shared or private memory
3201 * and store the results in *kernel_access.
3203 * Since the array in shared or private memory is just
3204 * a shifted copy of part of the original array, we simply need
3205 * to subtract the lower bound, which was computed in can_tile.
3206 * If any of the indices is strided, then we first add
3207 * shared_tile->bound[i].shift and divide by shared_tile->bound[i].stride.
3209 * If the given array is accessed directly from global memory,
3210 * we don't need to perform any shifting and simply simplify
3211 * the expression in the context of the domain instead.
3213 * If the array space (range of access) has no name, then we are
3214 * accessing an iterator in the original program.
3216 * The input stmt_access->access relation maps the iteration domain
3217 * of the current statement to an array element.
3218 * The first step is to reformulate
3219 * this access relation in terms of the loop iterators of the generated
3220 * code through precomposition with gen->stmt_it.
3222 * The expressions in "tile" are formulated in terms of the first
3223 * gen->shared_len dimensions of the computed schedule using the mapping
3224 * sched2shared which maps the loop iterators to these dimensions.
3226 static void compute_index_expression(struct gpu_gen
*gen
,
3227 struct ppcg_kernel_access
*kernel_access
,
3228 struct gpu_stmt_access
*stmt_access
, __isl_keep isl_map
*stmt_it
,
3229 __isl_keep isl_map
*sched2shared
, __isl_keep isl_ast_build
*build
)
3232 isl_pw_multi_aff
*pma
;
3235 struct gpu_array_tile
*tile
= NULL
;
3237 if (isl_map_has_tuple_name(stmt_access
->access
, isl_dim_out
)) {
3240 struct gpu_array_ref_group
*group
;
3243 name
= isl_map_get_tuple_name(stmt_access
->access
, isl_dim_out
);
3245 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3246 if (strcmp(name
, gen
->prog
->array
[i
].name
))
3248 kernel_access
->array
= &gen
->prog
->array
[i
];
3249 kernel_access
->local_array
= &gen
->kernel
->array
[i
];
3251 assert(kernel_access
->array
);
3252 group
= kernel_access
->array
->groups
[stmt_access
->group
];
3253 p
= isl_printer_to_str(gen
->ctx
);
3254 p
= print_array_name(p
, group
);
3255 kernel_access
->local_name
= isl_printer_get_str(p
);
3256 isl_printer_free(p
);
3257 tile
= group
->private_tile
;
3258 kernel_access
->type
= ppcg_access_private
;
3260 tile
= group
->shared_tile
;
3261 kernel_access
->type
= ppcg_access_shared
;
3265 kernel_access
->type
= ppcg_access_global
;
3267 n_index
= isl_map_dim(stmt_access
->access
, isl_dim_out
);
3268 kernel_access
->index
= isl_ast_expr_list_alloc(gen
->ctx
, n_index
);
3273 access
= isl_map_copy(stmt_access
->access
);
3274 access
= isl_map_apply_range(isl_map_copy(stmt_it
), access
);
3275 pma
= isl_pw_multi_aff_from_map(access
);
3276 pma
= isl_pw_multi_aff_coalesce(pma
);
3278 for (i
= 0; i
< n_index
; ++i
) {
3283 index
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
3285 if (!kernel_access
->array
) {
3287 domain
= isl_map_domain(isl_map_copy(stmt_it
));
3288 index
= isl_pw_aff_coalesce(index
);
3289 index
= isl_pw_aff_gist(index
, domain
);
3291 domain
= isl_map_domain(isl_map_copy(stmt_it
));
3292 index
= shift_index(index
, kernel_access
->array
,
3293 &tile
->bound
[i
], domain
,
3294 isl_map_copy(sched2shared
));
3297 expr
= isl_ast_build_expr_from_pw_aff(build
, index
);
3299 kernel_access
->index
= isl_ast_expr_list_add(
3300 kernel_access
->index
, expr
);
3303 isl_pw_multi_aff_free(pma
);
3306 /* This function is called for each instance of a user statement
3309 * We attach a struct ppcg_kernel_stmt to the "node", containing
3310 * local information about the accesses.
3311 * This information is computed from stmt_it, which expresses the domain
3312 * elements in terms of the generated loops, and sched2shared,
3313 * which expresses the first shared_len dimensions of the schedule
3314 * computed by PPCG in terms of the generated loops.
3316 static __isl_give isl_ast_node
*at_each_domain(__isl_take isl_ast_node
*node
,
3317 __isl_keep isl_ast_build
*build
, void *user
)
3319 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3320 struct ppcg_kernel_stmt
*stmt
;
3322 isl_map
*stmt_it
, *sched2shared
;
3323 isl_ast_expr
*expr
, *arg
;
3324 isl_union_map
*schedule
;
3326 struct gpu_stmt_access
*access
;
3328 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3330 return isl_ast_node_free(node
);
3332 expr
= isl_ast_node_user_get_expr(node
);
3333 arg
= isl_ast_expr_get_op_arg(expr
, 0);
3334 id
= isl_ast_expr_get_id(arg
);
3336 schedule
= isl_ast_build_get_schedule(build
);
3337 stmt_it
= isl_map_reverse(isl_map_from_union_map(schedule
));
3338 sched2shared
= compute_sched_to_shared(gen
, isl_map_copy(stmt_it
));
3340 stmt
->type
= ppcg_kernel_domain
;
3341 stmt
->u
.d
.stmt
= find_stmt(gen
->prog
, id
);
3342 if (!stmt
->u
.d
.stmt
)
3346 for (access
= stmt
->u
.d
.stmt
->accesses
; access
; access
= access
->next
)
3349 stmt
->u
.d
.access
= isl_calloc_array(gen
->ctx
,
3350 struct ppcg_kernel_access
, n
);
3351 if (!stmt
->u
.d
.access
)
3354 stmt
->u
.d
.n_access
= n
;
3356 access
= stmt
->u
.d
.stmt
->accesses
;
3357 for (i
= 0; i
< n
; ++i
, access
= access
->next
) {
3358 compute_index_expression(gen
, &stmt
->u
.d
.access
[i
], access
,
3359 stmt_it
, sched2shared
, build
);
3363 isl_map_free(stmt_it
);
3364 isl_map_free(sched2shared
);
3365 isl_ast_expr_free(arg
);
3366 isl_ast_expr_free(expr
);
3368 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3369 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3370 return isl_ast_node_set_annotation(node
, id
);
3373 isl_map_free(stmt_it
);
3374 ppcg_kernel_stmt_free(stmt
);
3375 isl_map_free(sched2shared
);
3376 return isl_ast_node_free(node
);
3379 /* This function is called when code has been generated for the shared
3380 * tile loops. The "schedule" refers only to the original statements.
3382 * We extend the schedule with that part of gen->local_sched that hasn't
3383 * been taken into account yet. This introduces parameters referring
3384 * to thread ids in the schedule, so we add them (with the appropriate
3385 * bounds to the context as well).
3386 * Finally, we set the appropriate unrolling options
3387 * if gen->first_unroll is set.
3389 static __isl_give isl_ast_node
*create_domain_leaf(
3390 __isl_take isl_union_map
*schedule
, __isl_take isl_ast_build
*build
,
3393 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3395 isl_union_map
*sched
;
3398 isl_id_list
*iterators
;
3401 schedule
= extend_schedule(schedule
,
3402 isl_union_map_copy(gen
->local_sched
),
3403 gen
->shared_len
, gen
->thread_tiled_len
);
3405 space
= isl_ast_build_get_schedule_space(build
);
3406 set
= isl_set_universe(space
);
3407 set
= add_bounded_parameters(set
, gen
->n_block
, gen
->block_dim
, "t");
3408 build
= isl_ast_build_restrict(build
, set
);
3410 n
= gen
->thread_tiled_len
- gen
->shared_len
;
3412 if (gen
->first_unroll
>= 0) {
3413 space
= isl_space_set_alloc(gen
->ctx
, 0, n
);
3414 build
= set_unroll(build
, space
, gen
->first_unroll
);
3416 iterators
= generate_names(gen
->ctx
, n
, "c");
3417 build
= isl_ast_build_set_iterators(build
, iterators
);
3418 build
= isl_ast_build_set_at_each_domain(build
, &at_each_domain
, gen
);
3419 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3420 isl_ast_build_free(build
);
3425 /* This function is called for each statement node in the AST of the code
3426 * for copying to or from shared/private memory.
3427 * Attach a pointer to a ppcg_kernel_stmt representing the copy
3428 * statement to the node.
3429 * The statement name is {read,write}_{shared,private}_<array>.
3431 * The schedule is of the form
3435 * where A refers to a piece of an array and T to the corresponding
3436 * shifted tile. We split this schedule into mappings L -> A and L -> T
3437 * and store the corresponding expressions in stmt->index and stmt->local_index,
3438 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
3440 static __isl_give isl_ast_node
*attach_copy_stmt(__isl_take isl_ast_node
*node
,
3441 __isl_keep isl_ast_build
*build
, void *user
)
3443 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3444 struct ppcg_kernel_stmt
*stmt
;
3448 isl_map
*access
, *local_access
, *map
;
3449 isl_pw_multi_aff
*pma
;
3453 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3455 return isl_ast_node_free(node
);
3457 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
3458 name
= isl_map_get_tuple_name(access
, isl_dim_in
);
3459 stmt
->u
.c
.read
= !strncmp(name
, "read", 4);
3460 access
= isl_map_reverse(access
);
3461 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(access
)));
3462 local_access
= isl_map_copy(access
);
3464 map
= isl_map_domain_map(isl_map_universe(isl_space_copy(space
)));
3465 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
3466 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
3467 access
= isl_map_apply_range(access
, map
);
3468 pma
= isl_pw_multi_aff_from_map(access
);
3469 expr
= isl_ast_build_call_from_pw_multi_aff(build
, pma
);
3470 stmt
->u
.c
.index
= expr
;
3472 map
= isl_map_range_map(isl_map_universe(space
));
3473 id
= isl_map_get_tuple_id(local_access
, isl_dim_out
);
3474 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
3475 local_access
= isl_map_apply_range(local_access
, map
);
3476 pma
= isl_pw_multi_aff_from_map(local_access
);
3477 expr
= isl_ast_build_call_from_pw_multi_aff(build
, pma
);
3478 stmt
->u
.c
.local_index
= expr
;
3480 stmt
->u
.c
.array
= gen
->copy_group
->array
;
3481 array_index
= stmt
->u
.c
.array
- gen
->prog
->array
;
3482 stmt
->u
.c
.local_array
= &gen
->kernel
->array
[array_index
];
3483 stmt
->type
= ppcg_kernel_copy
;
3485 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3486 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3487 return isl_ast_node_set_annotation(node
, id
);
3490 /* Given a schedule of the form
3494 * (with S the first shared_len dimensions of the computed schedule,
3495 * A the array and L the schedule correponding to the generated loops),
3496 * indicating where the copying the array elements that need to be copied,
3497 * construct code for performing the copying.
3499 * "group" is the array reference group that is being copied
3500 * "type" is either "read" or "write"
3501 * private is set if copying needs to be performed to/from registers
3503 * We first construct a mapping to a shifted tile of the array,
3505 * [S -> A] -> T(S,A) (1)
3507 * If private is set, then we also use this mapping as a schedule
3508 * (which is already thread-specific and will be completely unrolled).
3509 * Otherwise, we wrap/tile the range over the threads.
3512 * [S -> A] -> T'(S,A)
3514 * Combined with the given schedule, we have
3516 * [S -> A] -> [L -> T'(S,A)] (2)
3518 * From the shifted tile mapping, we construct a mapping
3520 * [S -> A] -> [A -> T(S,A)]
3522 * and apply it to the schedule (2), obtaining
3524 * [A -> T(S(L),A)] -> [L -> T'(S(L),A)]
3526 * Note that we can project out S because it is uniquely defined by L.
3528 static __isl_give isl_ast_node
*copy_access(struct gpu_gen
*gen
,
3529 __isl_take isl_map
*sched
,
3530 const char *type
, struct gpu_array_ref_group
*group
,
3531 __isl_take isl_ast_build
*build
, int private)
3533 const char *array_name
;
3534 const char *mem
= private ? "private" : "shared";
3538 isl_map
*schedule
, *shift
, *map
;
3540 isl_id_list
*iterators
;
3543 shift
= isl_set_unwrap(isl_map_domain(isl_map_copy(sched
)));
3544 array_name
= isl_map_get_tuple_name(shift
, isl_dim_out
);
3545 shift
= shift_access(shift
, group
);
3547 schedule
= isl_map_copy(shift
);
3549 schedule
= tile_access_schedule(gen
, schedule
);
3551 n
= isl_map_dim(schedule
, isl_dim_out
);
3552 set
= isl_set_universe(isl_ast_build_get_schedule_space(build
));
3553 set
= add_bounded_parameters(set
, gen
->n_block
, gen
->block_dim
, "t");
3555 schedule
= isl_map_range_product(sched
, schedule
);
3558 name
= isl_alloc_array(gen
->ctx
, char,
3559 strlen(type
) + sizeof("_private_") + strlen(array_name
) + 20);
3560 if (group
->array
->n_group
> 1)
3561 sprintf(name
, "%s_%s_%s_%d", type
, mem
, array_name
, group
->nr
);
3563 sprintf(name
, "%s_%s_%s", type
, mem
, array_name
);
3564 shift
= isl_map_set_tuple_name(shift
,
3565 isl_dim_out
, name
+ strlen(type
) + 1);
3567 space
= isl_space_domain(isl_map_get_space(shift
));
3568 map
= isl_map_range_map(isl_map_universe(isl_space_unwrap(space
)));
3569 map
= isl_map_range_product(map
, shift
);
3571 schedule
= isl_map_apply_domain(schedule
, map
);
3573 schedule
= isl_map_set_tuple_name(schedule
, isl_dim_in
, name
);
3576 build
= isl_ast_build_restrict(build
, set
);
3578 gen
->copy_group
= group
;
3581 space
= isl_space_range(isl_map_get_space(schedule
));
3582 space
= isl_space_range(isl_space_unwrap(space
));
3583 build
= set_unroll(build
, space
, 0);
3585 iterators
= generate_names(gen
->ctx
, n
, "c");
3586 build
= isl_ast_build_set_iterators(build
, iterators
);
3587 build
= isl_ast_build_set_at_each_domain(build
, &attach_copy_stmt
, gen
);
3588 tree
= isl_ast_build_ast_from_schedule(build
,
3589 isl_union_map_from_map(schedule
));
3590 isl_ast_build_free(build
);
3595 /* Return code for reading into or writing from shared memory
3596 * the given array reference group.
3598 * If we are performing a read from global memory to shared memory and
3599 * if the array involved is not a scalar, then we copy
3600 * the entire tile to shared memory. This may result in some extra
3601 * elements getting copied, but it should lead to simpler code
3602 * (which means that fewer registers may be needed) and less divergence.
3604 * Otherwise, we only copy the elements that will be read or have been written
3608 * The input "sched" is of the form.
3612 * with S the first shared_len dimensions of the computed schedule,
3613 * A the array and L the schedule correponding to the generated loops.
3615 * We first drop "type",
3619 * If the above conditions are satisfied, we project out A,
3624 * and then introduce the group tile [S -> T], resulting in
3628 static __isl_give isl_ast_node
*copy_group_shared_accesses(
3629 struct gpu_gen
*gen
, struct gpu_array_ref_group
*group
,
3630 __isl_take isl_map
*sched
, __isl_take isl_ast_build
*build
)
3634 isl_union_map
*access
;
3636 type
= isl_map_get_tuple_name(sched
, isl_dim_in
);
3637 read
= !strcmp(type
, "read");
3639 sched
= isl_map_reset_tuple_id(sched
, isl_dim_in
);
3641 if (read
&& group
->array
->n_index
> 0) {
3645 space
= isl_space_domain(isl_map_get_space(sched
));
3646 space
= isl_space_unwrap(space
);
3647 map
= isl_map_domain_map(isl_map_universe(space
));
3648 sched
= isl_map_apply_domain(sched
, map
);
3650 map
= group_tile(group
);
3651 map
= isl_map_reverse(isl_map_domain_map(map
));
3652 sched
= isl_map_apply_domain(sched
, map
);
3655 return copy_access(gen
, sched
, type
, group
, build
, 0);
3658 /* Return code for reading into or writing from private memory
3659 * the given array reference group.
3661 * Let S be the first shared_len dimensions of the computed schedule,
3662 * D the iteration domains, A the array and L the schedule correponding
3663 * to the generated loops.
3664 * "sched" is of the form
3668 * where type is either "read" or "write".
3669 * We apply the privatization D -> S(t), with t the thread ids,
3670 * to the access relation D -> A to obtain the privatized access relation
3674 * We drop the type from "sched" and intersect with the privatized access
3675 * relation to obtain
3679 static __isl_give isl_ast_node
*copy_group_private_accesses(
3680 struct gpu_gen
*gen
, struct gpu_array_ref_group
*group
,
3681 __isl_take isl_map
*sched
, __isl_take isl_ast_build
*build
)
3685 isl_union_map
*priv
;
3686 isl_union_map
*access
;
3687 isl_map
*access_map
;
3689 type
= isl_map_get_tuple_name(sched
, isl_dim_in
);
3690 read
= !strcmp(type
, "read");
3692 priv
= isl_union_map_from_map(isl_map_copy(gen
->privatization
));
3693 priv
= isl_union_map_apply_range(isl_union_map_copy(gen
->shared_sched
),
3696 access
= group_access_relation(group
, read
, !read
);
3697 access
= isl_union_map_apply_domain(access
, priv
);
3698 access_map
= isl_map_from_union_map(access
);
3700 sched
= isl_map_reset_tuple_id(sched
, isl_dim_in
);
3701 sched
= isl_map_intersect_domain(sched
, isl_map_wrap(access_map
));
3703 return copy_access(gen
, sched
, type
, group
, build
, 1);
3706 /* Return code for reading into or writing from shared or private memory.
3708 * "schedule" is of the form
3712 * with S be the first shared_len dimensions of the computed schedule,
3713 * A the array and L the schedule correponding to the generated loops.
3714 * The array reference group is attached to "type".
3716 static __isl_give isl_ast_node
*create_access_leaf(
3717 struct gpu_gen
*gen
, __isl_take isl_map
*schedule
,
3718 __isl_take isl_ast_build
*build
)
3720 struct gpu_array_ref_group
*group
;
3723 id
= isl_map_get_tuple_id(schedule
, isl_dim_in
);
3724 group
= isl_id_get_user(id
);
3727 if (group
->private_tile
)
3728 return copy_group_private_accesses(gen
, group
, schedule
,
3731 return copy_group_shared_accesses(gen
, group
, schedule
,
3735 /* Create a domain node representing a synchronization.
3737 static __isl_give isl_ast_node
*create_sync_leaf(
3738 struct gpu_gen
*gen
, __isl_take isl_map
*schedule
,
3739 __isl_take isl_ast_build
*build
)
3741 struct ppcg_kernel_stmt
*stmt
;
3747 isl_map_free(schedule
);
3749 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3753 stmt
->type
= ppcg_kernel_sync
;
3755 space
= isl_ast_build_get_schedule_space(build
);
3756 space
= isl_space_from_domain(space
);
3757 space
= isl_space_set_tuple_name(space
, isl_dim_out
, "sync");
3758 expr
= isl_ast_build_call_from_pw_multi_aff(build
,
3759 isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
)));
3760 node
= isl_ast_node_alloc_user(expr
);
3761 isl_ast_build_free(build
);
3763 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3764 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3765 return isl_ast_node_set_annotation(node
, id
);
3768 /* This function is called during the code generation at the point
3769 * where the schedule domain element is completely determined by
3770 * the generated code. The input schedule contains the original
3771 * statements as well as synchronization and copy "statements".
3772 * The latter are scheduled at different points than any of the original
3773 * statements, so they will only arrive here in isolation.
3775 * If the current schedule only refers to a single statement,
3776 * we check if it is a copy or synchronization statement and
3777 * call the appropriate functions.
3778 * Otherwise, we assume we are dealing with the original statements
3779 * and we call create_domain_leaf.
3781 static __isl_give isl_ast_node
*create_kernel_leaf(
3782 __isl_take isl_ast_build
*build
, void *user
)
3784 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3786 isl_union_map
*schedule
;
3789 schedule
= isl_ast_build_get_schedule(build
);
3791 if (isl_union_map_n_map(schedule
) != 1)
3792 return create_domain_leaf(schedule
, build
, user
);
3794 map
= isl_map_from_union_map(schedule
);
3795 name
= isl_map_get_tuple_name(map
, isl_dim_in
);
3796 if (!strcmp(name
, "read") || !strcmp(name
, "write"))
3797 return create_access_leaf(gen
, map
, build
);
3798 if (!strcmp(name
, "sync"))
3799 return create_sync_leaf(gen
, map
, build
);
3801 return create_domain_leaf(isl_union_map_from_map(map
), build
, user
);
3804 /* Mark all odd schedule dimensions as "atomic" (when the even dimensions
3805 * have value 0) and all even schedule dimensions as "unroll".
3807 * That is, the options look as follows
3809 * { [0, b, 0, d, ..., 0] -> atomic[i] : exists a : i = 2 a + 1;
3810 * [a, b, c, d, ..., z] -> unroll[i] : exists a : i = 2 a }
3812 * The even positions are used to be able to schedule copying blocks
3813 * and synchronization before or after each level of the shared memory
3814 * tile loops and we want to make sure that code for these is generated
3815 * separately (within each level).
3817 static __isl_give isl_ast_build
*set_atomic_and_unroll(
3818 __isl_take isl_ast_build
*build
,
3819 __isl_take isl_space
*space
, int sched_len
)
3825 isl_local_space
*ls
;
3828 ctx
= isl_ast_build_get_ctx(build
);
3830 space
= isl_space_params(space
);
3831 space
= isl_space_add_dims(space
, isl_dim_set
, sched_len
);
3832 space
= isl_space_from_domain(space
);
3833 space
= isl_space_add_dims(space
, isl_dim_out
, 2);
3834 map
= isl_map_universe(isl_space_copy(space
));
3835 for (i
= 0; i
< sched_len
; i
+= 2)
3836 map
= isl_map_fix_si(map
, isl_dim_in
, i
, 0);
3837 ls
= isl_local_space_from_space(isl_map_get_space(map
));
3838 c
= isl_equality_alloc(ls
);
3839 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
3840 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
3841 c
= isl_constraint_set_constant_si(c
, 1);
3842 map
= isl_map_add_constraint(map
, c
);
3843 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
3844 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "atomic");
3845 opt
= isl_union_map_from_map(map
);
3847 map
= isl_map_universe(space
);
3848 ls
= isl_local_space_from_space(isl_map_get_space(map
));
3849 c
= isl_equality_alloc(ls
);
3850 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
3851 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
3852 map
= isl_map_add_constraint(map
, c
);
3853 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
3854 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "unroll");
3855 opt
= isl_union_map_add_map(opt
, map
);
3857 build
= isl_ast_build_set_options(build
, opt
);
3862 /* Return a map that maps a space of dimension gen->shared_len
3863 * to its last dimensions starting at gen->tile_first.
3864 * The range is of dimension
3866 * 2 * (gen->shared_len - gen->tile_first) + 1
3868 * The input dimensions are mapped to the odd dimensions in the output,
3869 * while the even dimensions (except 2*pos) are fixed to 0.
3870 * Output dimension 2*pos (if pos >= 0) is fixed to "val".
3871 * If pos >= 0, then only the pos first dimensions starting at gen->tile_first
3872 * are mapped to the output. The remaining input dimensions are projected
3873 * out and the corresponding output dimensions are fixed to 0.
3875 static __isl_give isl_map
*insert_even(struct gpu_gen
*gen
,
3876 __isl_take isl_space
*space
, int pos
, int val
)
3881 space
= isl_space_set_from_params(space
);
3882 space
= isl_space_add_dims(space
, isl_dim_set
, gen
->shared_len
);
3883 space
= isl_space_map_from_set(space
);
3884 proj
= isl_map_identity(space
);
3885 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, gen
->tile_first
);
3886 n
= gen
->shared_len
- gen
->tile_first
;
3887 for (i
= 0; i
<= n
; ++i
) {
3888 proj
= isl_map_insert_dims(proj
, isl_dim_out
, 2 * i
, 1);
3890 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, val
);
3892 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, 0);
3898 proj
= isl_map_eliminate(proj
, isl_dim_in
, gen
->tile_first
+ pos
,
3899 gen
->shared_len
- (gen
->tile_first
+ pos
));
3900 for (i
= pos
; i
< n
; ++i
)
3901 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
+ 1, 0);
3906 /* Given the AST context schedule "schedule" and the mapping from
3907 * domains to the shared tile loops "shared_sched", add a schedule
3908 * for a synchronization operation at position "val" of loop level "pos".
3910 * schedule is of the form
3914 * (with D the iteration domains and L the already generated loops),
3915 * while shared_sched is of the form
3919 * We combine them into
3925 * [s_0,...] -> [0,s_{tile_first},0,..., val, 0, 0, ... 0]
3927 * and use the result as a schedule for "sync".
3929 static __isl_give isl_union_map
*add_sync_schedule(struct gpu_gen
*gen
,
3930 __isl_take isl_union_map
*res
, __isl_keep isl_union_map
*schedule
,
3931 __isl_keep isl_union_map
*shared_sched
, int pos
, int val
)
3934 isl_map
*proj
, *map
;
3936 shared_sched
= isl_union_map_copy(shared_sched
);
3937 schedule
= isl_union_map_copy(schedule
);
3939 space
= isl_union_map_get_space(shared_sched
);
3940 schedule
= isl_union_map_apply_domain(shared_sched
, schedule
);
3941 map
= isl_map_from_union_map(schedule
);
3943 proj
= insert_even(gen
, space
, pos
, val
);
3944 map
= isl_map_apply_range(map
, proj
);
3945 map
= isl_map_from_range(isl_map_wrap(map
));
3946 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "sync");
3948 res
= isl_union_map_add_map(res
, map
);
3953 /* Given the AST context schedule "schedule" and the mapping from
3954 * domains to the shared tile loops "shared_sched", add a schedule
3955 * for copying an array reference group to/from shared/private memory.
3956 * "read" is set if data should be copied from global memory
3957 * to shared/private memory.
3958 * "k" represents the current group
3959 * "s" is the total number of groups
3961 * We schedule an operation before or after the innermost loop
3962 * of "shared_sched" that affects the tile of the array reference group.
3964 * schedule is of the form
3968 * (with D the iteration domains and L the already generated loops),
3969 * while shared_sched is of the form
3973 * We first compute the access relation for the reference group
3977 * and combine it with shared_sched into
3981 * If this results in an empty relation, no copying needs to be performed
3983 * Otherwise, we invert the relation and combine it with "schedule" into
3987 * The actual additional piece of the schedule is obtained from combining
3993 * [s_0,...] -> [0,s_{tile_first},0,..., val, 0, 0, ... 0]
3995 * The position of "val" corresponds to the innermost loop that affects
3996 * the tile and the value indicates where the copying is scheduled
3997 * with respect to the actual kernel code (at value 0).
3998 * Reads are schedule before the code, writes to global memory from
3999 * private memory are scheduled at values 1 to s, writes to global
4000 * memory from shared memory are scheduled at values s + 2 to 2 * s + 1.
4002 * If we are scheduling a read from global memory to shared memory,
4003 * we insert a synchronization before the kernel code (at the innermost
4005 * If we are scheduling a write to global memory, then we add
4006 * a synchronization after all writes (at value 2 *s + 2).
4007 * However, there is no need for a synchronization after the outermost loop.
4008 * A write to global memory from private memory at the innermost level
4009 * does not require a synchronization, because it is covered by
4010 * the synchronization after the kernel inserted by body_schedule.
4012 static __isl_give isl_union_map
*add_group_schedule(struct gpu_gen
*gen
,
4013 __isl_take isl_union_map
*res
, __isl_keep isl_union_map
*schedule
,
4014 __isl_keep isl_union_map
*shared_sched
,
4015 struct gpu_array_ref_group
*group
, int read
, int k
, int s
)
4020 isl_union_map
*access
;
4021 isl_map
*map
, *proj
, *access_map
;
4024 access
= group_access_relation(group
, read
, !read
);
4025 access
= isl_union_map_range_product(isl_union_map_copy(shared_sched
),
4028 if (isl_union_map_is_empty(access
)) {
4029 isl_union_map_free(access
);
4033 access
= isl_union_map_reverse(access
);
4034 access
= isl_union_map_apply_range(access
,
4035 isl_union_map_copy(schedule
));
4036 access_map
= isl_map_from_union_map(access
);
4038 space
= isl_space_copy(group
->array
->dim
);
4039 space
= isl_space_from_range(space
);
4040 space
= isl_space_add_dims(space
, isl_dim_in
, gen
->shared_len
);
4041 map
= isl_map_domain_map(isl_map_universe(space
));
4043 space
= isl_union_map_get_space(schedule
);
4044 pos
= group
->last_shared
+ 1 - gen
->tile_first
;
4048 else if (group
->private_tile
)
4051 val
= 1 + s
+ 1 + k
;
4052 proj
= insert_even(gen
, space
, pos
, val
);
4053 map
= isl_map_apply_range(map
, proj
);
4055 access_map
= isl_map_range_product(access_map
, map
);
4057 id
= isl_id_alloc(gen
->ctx
, read
? "read" : "write", group
);
4058 access_map
= isl_map_set_tuple_id(access_map
, isl_dim_in
, id
);
4060 res
= isl_union_map_add_map(res
, access_map
);
4062 n
= gen
->shared_len
- gen
->tile_first
;
4064 if (!group
->private_tile
)
4065 res
= add_sync_schedule(gen
, res
, schedule
,
4066 shared_sched
, n
, -1);
4070 if (pos
== n
&& group
->private_tile
)
4072 res
= add_sync_schedule(gen
, res
, schedule
, shared_sched
,
4079 /* Return a schedule for the shared tile loops based on the current
4080 * AST context schedule.
4082 * We create a "shared_sched" that maps the domains to the first
4083 * shared_len dimensions of the computed schedule, project out the
4084 * first tile_first dimensions (as these are already covered by
4085 * the host code) and insert "statement-level" dimensions at even
4086 * positions so that we can schedule copy blocks and synchronization
4087 * before/after each level.
4089 * In particular, copy blocks are inserted inside the innermost
4090 * level that affect the tile. For the copying to global memory,
4091 * those from private memory are scheduled before those from shared
4092 * memory such that synchronization can be inserted between the two
4093 * at the innermost level.
4094 * Synchronization is inserted at the innermost level before the
4095 * actual kernel code if there is any copying from global memory
4096 * to shared memory. It is inserted unconditionally at the innermost
4097 * level after the actual kernel code and the copying to global memory
4098 * from private memory (if any). Finally, it is inserted after
4099 * any copying to global memory, except at the outermost level
4100 * and at the innermost level if there is no copying from shared
4101 * memory. The copying from private memory is covered by the unconditional
4102 * synchronization at the innermost level.
4104 static __isl_give isl_union_map
*body_schedule(struct gpu_gen
*gen
,
4105 __isl_take isl_union_map
*schedule
)
4109 isl_union_map
*shared_sched
;
4110 isl_union_map
*sched
;
4111 isl_map
*proj
, *map
;
4114 shared_sched
= isl_union_map_copy(gen
->tiled_sched
);
4115 proj
= projection(isl_union_map_get_space(shared_sched
),
4116 gen
->tiled_len
, gen
->shared_len
);
4117 shared_sched
= isl_union_map_apply_range(shared_sched
,
4118 isl_union_map_from_map(proj
));
4119 space
= isl_union_map_get_space(shared_sched
);
4120 proj
= insert_even(gen
, space
, -1, 0);
4121 sched
= isl_union_map_apply_range(isl_union_map_copy(shared_sched
),
4122 isl_union_map_from_map(proj
));
4124 res
= isl_union_map_range_product(isl_union_map_copy(schedule
), sched
);
4127 for (i
= 0; i
< gen
->prog
->n_array
; ++i
)
4128 s
+= gen
->prog
->array
[i
].n_group
;
4131 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
4132 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
4134 for (j
= 0; j
< array
->n_group
; ++j
) {
4135 struct gpu_array_ref_group
*group
;
4137 group
= array
->groups
[j
];
4138 if (!group
->private_tile
&& !group
->shared_tile
)
4140 res
= add_group_schedule(gen
, res
, schedule
,
4141 shared_sched
, group
, 0, k
, s
);
4142 res
= add_group_schedule(gen
, res
, schedule
,
4143 shared_sched
, group
, 1, k
, s
);
4148 res
= add_sync_schedule(gen
, res
, schedule
, shared_sched
,
4149 gen
->shared_len
- gen
->tile_first
, 1 + s
);
4151 isl_union_map_free(shared_sched
);
4152 isl_union_map_free(schedule
);
4157 /* Generate code for "kernel" in the given "context".
4159 * We first generate code for the shared tile loops (T1T, T1P and T2)
4160 * in a context that includes the block ids.
4161 * Within each iteration of these loops an additional code generation
4162 * is performed (within create_kernel_leaf) for the rest of the schedule
4163 * in a context that includes the thread ids.
4165 static __isl_give isl_ast_node
*generate_kernel(struct gpu_gen
*gen
,
4166 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*host_domain
,
4167 __isl_keep isl_multi_pw_aff
*grid_size
)
4171 isl_id_list
*iterators
;
4172 isl_union_map
*schedule
;
4176 schedule
= isl_ast_build_get_schedule(build
);
4178 build
= isl_ast_build_copy(build
);
4179 build
= isl_ast_build_restrict(build
, isl_set_copy(host_domain
));
4180 space
= isl_ast_build_get_schedule_space(build
);
4181 set
= isl_set_universe(isl_space_copy(space
));
4182 set
= add_bounded_parameters_dynamic(set
, grid_size
, "b");
4183 build
= isl_ast_build_restrict(build
, set
);
4185 schedule
= body_schedule(gen
, schedule
);
4187 sched_len
= 2 * (gen
->shared_len
- gen
->tile_first
) + 1;
4189 build
= set_atomic_and_unroll(build
, space
, sched_len
);
4190 iterators
= generate_names(gen
->ctx
, sched_len
, "g");
4191 build
= isl_ast_build_set_iterators(build
, iterators
);
4192 build
= isl_ast_build_set_create_leaf(build
, &create_kernel_leaf
, gen
);
4193 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
4194 isl_ast_build_free(build
);
4199 /* Attach "id" to the given node.
4201 static __isl_give isl_ast_node
*attach_id(__isl_take isl_ast_node
*node
,
4202 __isl_keep isl_ast_build
*build
, void *user
)
4206 node
= isl_ast_node_set_annotation(node
, id
);
4211 /* Construct an AST node for performing a kernel launch and attach
4212 * the information about the kernel to that node.
4214 * The kernel AST has been constructed in the context of the range
4215 * of "schedule". In particular, the grid size has been computed
4216 * in the context. We therefore still need to make sure that these
4217 * constraints are expressed in the code. We do this by creating a schedule
4219 * kernel[] -> [S -> []]
4221 * where S is the schedule domain, i.e., the range of "schedule".
4222 * The AST generation will then create a single call surrounded by
4223 * all the condition in "S" that have not been expressed yet.
4225 * The kernel information is attached to this node in attach_id.
4227 static __isl_give isl_ast_node
*construct_launch(
4228 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*schedule
,
4229 __isl_take
struct ppcg_kernel
*kernel
)
4233 isl_union_set
*domain
;
4238 ctx
= isl_ast_build_get_ctx(build
);
4240 id
= isl_id_alloc(ctx
, NULL
, kernel
);
4241 id
= isl_id_set_free_user(id
, &ppcg_kernel_free
);
4243 domain
= isl_union_map_range(schedule
);
4244 set
= isl_set_from_union_set(domain
);
4245 map
= isl_map_from_domain(set
);
4246 map
= isl_map_from_range(isl_map_wrap(map
));
4247 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "kernel");
4248 schedule
= isl_union_map_from_map(map
);
4250 build
= isl_ast_build_set_at_each_domain(build
, &attach_id
, id
);
4251 node
= isl_ast_build_ast_from_schedule(build
, schedule
);
4252 isl_ast_build_free(build
);
4257 /* This function is called for each leaf in the AST of the host code.
4258 * We first specialize the schedule to the site of the leaf, compute
4259 * the size of shared memory and then construct the body of host code
4260 * and the associated kernel.
4262 * The necessary information for printing the kernel launch is
4263 * stored in a struct ppcg_kernel and attached to the leaf node
4264 * created to represent the launch.
4266 static __isl_give isl_ast_node
*create_host_leaf(
4267 __isl_take isl_ast_build
*build
, void *user
)
4269 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
4272 struct ppcg_kernel
*kernel
;
4273 isl_set
*host_domain
;
4274 isl_union_map
*schedule
;
4275 isl_union_map
*local_sched
;
4276 isl_union_map
*access
;
4277 isl_union_set
*domain
;
4280 schedule
= isl_ast_build_get_schedule(build
);
4282 isl_union_map_foreach_map(schedule
, &extract_tile_len
, gen
);
4285 domain
= isl_union_map_domain(isl_union_map_copy(schedule
));
4287 local_sched
= isl_union_map_copy(gen
->sched
);
4288 local_sched
= isl_union_map_intersect_domain(local_sched
, domain
);
4289 access
= isl_union_map_union(isl_union_map_copy(gen
->prog
->read
),
4290 isl_union_map_copy(gen
->prog
->write
));
4291 access
= isl_union_map_apply_domain(access
,
4292 isl_union_map_copy(local_sched
));
4294 gen
->tiled_sched
= tile_schedule(gen
, local_sched
);
4295 gen
->tiled_sched
= parametrize_tiled_schedule(gen
, gen
->tiled_sched
);
4296 gen
->tiled_sched
= scale_tile_loops(gen
, gen
->tiled_sched
);
4298 kernel
= gen
->kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
4302 kernel
->id
= gen
->kernel_id
++;
4303 kernel
->n_block
= gen
->n_block
;
4304 for (i
= 0; i
< gen
->n_block
; ++i
)
4305 kernel
->block_dim
[i
] = gen
->block_dim
[i
];
4306 kernel
->context
= isl_union_map_params(isl_union_map_copy(schedule
));
4307 kernel
->grid_size
= extract_grid_size(gen
, kernel
);
4308 kernel
->arrays
= isl_union_map_range(access
);
4309 kernel
->space
= isl_ast_build_get_schedule_space(build
);
4311 gen
->local_sched
= isl_union_map_copy(gen
->tiled_sched
);
4313 gen
->local_sched
= thread_tile_schedule(gen
, gen
->local_sched
);
4314 gen
->local_sched
= scale_thread_tile_loops(gen
, gen
->local_sched
);
4316 gen
->private_access
= NULL
;
4317 compute_shared_sched(gen
);
4318 gen
->privatization
= compute_privatization(gen
);
4319 group_references(gen
);
4320 compute_private_access(gen
);
4321 check_shared_memory_bound(gen
);
4322 host_domain
= isl_set_from_union_set(isl_union_map_range(
4323 isl_union_map_copy(schedule
)));
4324 localize_bounds(gen
, kernel
, host_domain
);
4326 gen
->local_sched
= interchange_for_unroll(gen
, gen
->local_sched
);
4328 kernel
->tree
= generate_kernel(gen
, build
, host_domain
,
4330 create_kernel_vars(gen
, kernel
);
4332 free_local_array_info(gen
);
4333 isl_map_free(gen
->privatization
);
4334 isl_union_map_free(gen
->private_access
);
4335 isl_union_map_free(gen
->local_sched
);
4336 isl_union_map_free(gen
->tiled_sched
);
4337 isl_union_map_free(gen
->shared_sched
);
4338 isl_union_map_free(gen
->shared_proj
);
4339 isl_set_free(host_domain
);
4340 free(gen
->tile_size
);
4342 node
= construct_launch(build
, schedule
, kernel
);
4346 isl_union_map_free(schedule
);
4350 /* Use isl to generate code for the outer gen->tile_first loops
4351 * of the global schedule in gen->sched, resulting in the host code.
4352 * Within each iteration of this partial schedule, i.e., for each kernel
4353 * launch, create_host_leaf takes care of generating the kernel code.
4355 static __isl_give isl_ast_node
*generate_host_code(struct gpu_gen
*gen
)
4357 isl_ast_build
*build
;
4359 isl_union_map
*sched
;
4361 isl_id_list
*iterators
;
4363 sched
= isl_union_map_copy(gen
->sched
);
4364 proj
= projection(isl_union_map_get_space(sched
),
4365 gen
->untiled_len
, gen
->tile_first
);
4366 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
4368 isl_options_set_ast_build_group_coscheduled(gen
->ctx
, 1);
4369 build
= isl_ast_build_from_context(isl_set_copy(gen
->prog
->context
));
4370 iterators
= generate_names(gen
->ctx
, gen
->tile_first
, "h");
4371 build
= isl_ast_build_set_iterators(build
, iterators
);
4372 build
= isl_ast_build_set_create_leaf(build
, &create_host_leaf
, gen
);
4373 tree
= isl_ast_build_ast_from_schedule(build
, sched
);
4374 isl_ast_build_free(build
);
4379 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
4383 return isl_union_map_read_from_str(ctx
, str
);
4386 /* Information about the outermost tilable bands in the forest of bands.
4388 * tile_len and n_parallel are only sets on band_info structures
4389 * that correspond to outermost bands. For other bands (in particular,
4390 * ancestors of the outermost bands), n_parallal is set to 0.
4392 * prefix is the (padded) schedule leading up to the outermost tilable bands.
4394 * tile_first is the number of schedule dimensions in prefix.
4396 * suffix is the schedule of the outermost tilable bands and their descendants.
4399 struct gpu_gen
*gen
;
4403 isl_union_map
*prefix
;
4404 isl_union_map
*suffix
;
4407 /* Set tile_len and n_parallel of the statement to that of
4408 * their outermost band, recorded in the band_info.
4410 static int set_stmt_tile_len(__isl_take isl_map
*map
, void *user
)
4412 struct band_info
*info
= user
;
4413 struct gpu_stmt
*stmt
;
4416 id
= isl_map_get_tuple_id(map
, isl_dim_in
);
4417 stmt
= find_stmt(info
->gen
->prog
, id
);
4420 stmt
->tile_len
= info
->tile_len
;
4421 stmt
->n_parallel
= info
->n_parallel
;
4428 static void list_select_outer_band(struct gpu_gen
*gen
,
4429 __isl_take isl_band_list
*list
, int pos
, struct band_info
*list_info
);
4431 /* Check if this band has any parallel loops. If so, take it as
4432 * the outermost tilable band. If not, continue looking for the
4433 * outermost tilable band in the children of the current band.
4435 static void band_select_outer_band(struct gpu_gen
*gen
,
4436 __isl_take isl_band
*band
, int pos
, struct band_info
*info
)
4438 int n
= isl_band_n_member(band
);
4441 for (n_parallel
= 0; n_parallel
< n
; ++n_parallel
)
4442 if (!isl_band_member_is_zero_distance(band
, n_parallel
))
4445 info
->n_parallel
= n_parallel
;
4448 info
->tile_first
= pos
;
4450 info
->prefix
= isl_band_get_prefix_schedule(band
);
4451 info
->suffix
= isl_union_map_flat_range_product(
4452 isl_band_get_partial_schedule(band
),
4453 isl_band_get_suffix_schedule(band
));
4454 isl_union_map_foreach_map(info
->prefix
,
4455 &set_stmt_tile_len
, info
);
4456 } else if (isl_band_has_children(band
)) {
4457 isl_band_list
*children
;
4458 children
= isl_band_get_children(band
);
4459 list_select_outer_band(gen
, children
, pos
+ n
, info
);
4462 info
->tile_first
= pos
+ n
;
4464 info
->prefix
= isl_union_map_flat_range_product(
4465 isl_band_get_prefix_schedule(band
),
4466 isl_band_get_partial_schedule(band
));
4467 info
->suffix
= isl_band_get_suffix_schedule(band
);
4468 isl_union_map_foreach_map(info
->prefix
,
4469 &set_stmt_tile_len
, info
);
4472 isl_band_free(band
);
4475 /* Comparison function that returns a non-zero value for band_infos
4476 * with different tile_len fields or different n_parallel fields.
4478 static int cmp_band(const void *p1
, const void *p2
)
4480 const struct band_info
*info1
= p1
;
4481 const struct band_info
*info2
= p2
;
4483 if (info1
->tile_len
!= info2
->tile_len
)
4484 return info1
->tile_len
- info2
->tile_len
;
4486 return info1
->n_parallel
- info2
->n_parallel
;
4489 /* Extend "umap" with coordinates with fixed value "val"
4490 * to a total length of "dst_len", assuming the original dimension is "src_len".
4492 static __isl_give isl_union_map
*extend_range(
4493 __isl_take isl_union_map
*umap
, int src_len
, int dst_len
, int val
)
4499 dim
= isl_union_map_get_space(umap
);
4500 map
= isl_map_reverse(projection(dim
, dst_len
, src_len
));
4501 for (i
= src_len
; i
< dst_len
; ++i
)
4502 map
= isl_map_fix_si(map
, isl_dim_out
, i
, val
);
4504 umap
= isl_union_map_apply_range(umap
, isl_union_map_from_map(map
));
4509 /* Group bands with the same values for tile_len and n_parallel.
4510 * The prefix schedule is then extended with a fixed coordinate that
4511 * is different for each such group.
4512 * Note that the actual values for this coordinate are not important.
4513 * The bands have already been effectively separated at a higher level
4514 * or they are independent and may be executed in parallel.
4515 * The list of band_info has been sorted before this functions is called.
4517 static void separate_bands(struct band_info
*info
, int n
)
4522 for (i
= 0; i
< n
; ++i
) {
4523 int l
= info
[i
].tile_first
;
4526 (info
[i
].tile_len
!= info
[i
- 1].tile_len
||
4527 info
[i
].n_parallel
!= info
[i
- 1].n_parallel
))
4530 info
[i
].prefix
= extend_range(info
[i
].prefix
,
4532 info
[i
].tile_first
= l
+ 1;
4536 /* Select the outermost bands in the elements of the list, align
4537 * their prefix schedules, separate bands with different values
4538 * for tile_len and/or n_parallel and then combine the resulting
4539 * prefix and suffix schedules into a single pair of prefix and
4540 * suffix schedules for the entire list.
4542 static void list_select_outer_band(struct gpu_gen
*gen
,
4543 __isl_take isl_band_list
*list
, int pos
, struct band_info
*list_info
)
4547 int n
= isl_band_list_n_band(list
);
4548 isl_ctx
*ctx
= isl_band_list_get_ctx(list
);
4549 struct band_info
*info
;
4551 isl_union_map
*prefix
;
4552 isl_union_map
*suffix
;
4555 info
= isl_calloc_array(ctx
, struct band_info
, n
);
4559 for (i
= 0; i
< n
; ++i
) {
4560 band
= isl_band_list_get_band(list
, i
);
4561 band_select_outer_band(gen
, band
, pos
, &info
[i
]);
4562 if (info
[i
].tile_first
> max_tile_first
)
4563 max_tile_first
= info
[i
].tile_first
;
4566 for (i
= 0; i
< n
; ++i
) {
4567 if (info
[i
].tile_first
== max_tile_first
)
4569 info
[i
].prefix
= extend_range(info
[i
].prefix
,
4570 info
[i
].tile_first
, max_tile_first
, 0);
4571 info
[i
].tile_first
= max_tile_first
;
4574 qsort(info
, n
, sizeof(struct band_info
), &cmp_band
);
4576 for (i
= 0; i
< n
- 1; ++i
)
4577 if (info
[i
].tile_len
!= info
[i
+ 1].tile_len
||
4578 info
[i
].n_parallel
!= info
[i
+ 1].n_parallel
)
4582 separate_bands(info
, n
);
4584 prefix
= info
[0].prefix
;
4585 suffix
= info
[0].suffix
;
4587 for (i
= 1; i
< n
; ++i
) {
4588 prefix
= isl_union_map_union(prefix
, info
[i
].prefix
);
4589 suffix
= isl_union_map_union(suffix
, info
[i
].suffix
);
4592 list_info
->tile_first
= info
[0].tile_first
;
4593 list_info
->tile_len
= -1;
4594 list_info
->prefix
= prefix
;
4595 list_info
->suffix
= suffix
;
4597 isl_band_list_free(list
);
4601 /* Select the outermost tilable band that (by construction)
4602 * has at least one parallel loop.
4603 * The starting position of the aligned band is stored in the pair
4605 * The sizes and number of parallel loops may be different in different
4606 * parts of the band forest and are therefore stored in the gpu_stmts.
4608 * Return the complete schedule, with the tilable bands aligned
4609 * at gen->tile_first and padded with zero, if needed.
4611 static __isl_give isl_union_map
*select_outer_tilable_band(struct gpu_gen
*gen
,
4612 __isl_keep isl_schedule
*schedule
)
4614 isl_band_list
*list
;
4615 struct band_info info
;
4617 gen
->n_parallel
= 0;
4620 list
= isl_schedule_get_band_forest(schedule
);
4622 list_select_outer_band(gen
, list
, 0, &info
);
4624 gen
->tile_first
= info
.tile_first
;
4625 info
.suffix
= align_range(info
.suffix
);
4627 return isl_union_map_flat_range_product(info
.prefix
, info
.suffix
);
4630 /* Set gen->untiled_len to the number of scheduling dimensions
4631 * for the schedule of the first domain.
4632 * We assume here that this number is the same for all domains.
4634 static int set_untiled_len(__isl_take isl_map
*map
, void *user
)
4636 unsigned *untiled_len
= user
;
4638 *untiled_len
= isl_map_dim(map
, isl_dim_out
);
4644 /* Compute an appropriate schedule based on the accesses in
4645 * gen->read and gen->write.
4647 * We use the dependences in gen->prog->scop to compute
4648 * a schedule that has a parallel loop in each tilable band.
4649 * Finally, we select the outermost tilable band.
4651 static void compute_schedule(struct gpu_gen
*gen
)
4653 isl_union_set
*domain
;
4654 isl_union_map
*dep_raw
, *dep
;
4655 isl_union_map
*sched
;
4656 isl_schedule
*schedule
;
4658 dep_raw
= isl_union_map_copy(gen
->prog
->scop
->dep_flow
);
4660 dep
= isl_union_map_copy(gen
->prog
->scop
->dep_false
);
4661 dep
= isl_union_map_union(dep
, dep_raw
);
4662 dep
= isl_union_map_coalesce(dep
);
4664 domain
= isl_union_set_copy(gen
->prog
->scop
->domain
);
4665 domain
= isl_union_set_intersect_params(domain
,
4666 isl_set_copy(gen
->prog
->scop
->context
));
4667 schedule
= isl_union_set_compute_schedule(isl_union_set_copy(domain
),
4668 isl_union_map_copy(dep
), dep
);
4669 if (gen
->options
->debug
->dump_schedule
)
4670 isl_schedule_dump(schedule
);
4672 sched
= select_outer_tilable_band(gen
, schedule
);
4674 isl_union_map_foreach_map(sched
, &set_untiled_len
, &gen
->untiled_len
);
4675 sched
= isl_union_map_intersect_domain(sched
, domain
);
4678 isl_schedule_free(schedule
);
4681 /* Compute the sets of array elements that need to be copied in and out.
4683 * In particular, for each array that is written anywhere in gen->prog and
4684 * that is visible outside the corresponding scop, we copy out its entire
4687 * Any array elements that is read without first being written needs
4688 * to be copied in. Furthermore, if there are any array elements that
4689 * are copied out, but that are not written inside gen->prog, then
4690 * they also need to be copied in to ensure that the value after execution
4691 * is the same as the value before execution.
4692 * While computing the set of array elements that
4693 * are copied out but not written, we intersect both sets with the context.
4694 * This helps in those cases where the arrays are declared with a fixed size,
4695 * while the accesses are parametric and the context assigns a fixed value
4696 * to the parameters.
4698 static void compute_copy_in_and_out(struct gpu_gen
*gen
)
4701 isl_union_set
*write
;
4702 isl_union_set
*copy_in
, *copy_out
;
4703 isl_union_set
*not_written
;
4704 isl_union_map
*uninitialized
;
4706 write
= isl_union_map_range(isl_union_map_copy(gen
->prog
->write
));
4707 write
= isl_union_set_intersect_params(write
,
4708 isl_set_copy(gen
->prog
->context
));
4709 copy_out
= isl_union_set_empty(isl_union_set_get_space(write
));
4711 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
4716 if (gen
->prog
->array
[i
].local
)
4719 space
= isl_space_copy(gen
->prog
->array
[i
].dim
);
4720 write_i
= isl_union_set_extract_set(write
, space
);
4721 empty
= isl_set_fast_is_empty(write_i
);
4722 isl_set_free(write_i
);
4726 write_i
= isl_set_copy(gen
->prog
->array
[i
].extent
);
4727 copy_out
= isl_union_set_add_set(copy_out
, write_i
);
4730 copy_out
= isl_union_set_intersect_params(copy_out
,
4731 isl_set_copy(gen
->prog
->context
));
4733 gen
->prog
->copy_out
= isl_union_set_copy(copy_out
);
4735 uninitialized
= isl_union_map_copy(gen
->prog
->scop
->live_in
);
4736 copy_in
= isl_union_map_range(uninitialized
);
4738 not_written
= isl_union_set_subtract(copy_out
, write
);
4739 copy_in
= isl_union_set_union(copy_in
, not_written
);
4740 gen
->prog
->copy_in
= copy_in
;
4743 static struct gpu_stmt_access
**expr_extract_access(struct pet_expr
*expr
,
4744 struct gpu_stmt_access
**next_access
)
4746 struct gpu_stmt_access
*access
;
4747 isl_ctx
*ctx
= isl_map_get_ctx(expr
->acc
.access
);
4749 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
4751 access
->next
= NULL
;
4752 access
->read
= expr
->acc
.read
;
4753 access
->write
= expr
->acc
.write
;
4754 access
->access
= isl_map_copy(expr
->acc
.access
);
4756 *next_access
= access
;
4757 next_access
= &(*next_access
)->next
;
4761 static struct gpu_stmt_access
**expr_extract_accesses(struct pet_expr
*expr
,
4762 struct gpu_stmt_access
**next_access
)
4766 for (i
= 0; i
< expr
->n_arg
; ++i
)
4767 next_access
= expr_extract_accesses(expr
->args
[i
],
4770 if (expr
->type
== pet_expr_access
)
4771 next_access
= expr_extract_access(expr
, next_access
);
4776 static void pet_stmt_extract_accesses(struct gpu_stmt
*stmt
)
4778 struct gpu_stmt_access
**next_access
= &stmt
->accesses
;
4780 stmt
->accesses
= NULL
;
4781 expr_extract_accesses(stmt
->body
, next_access
);
4784 /* Return an array of gpu_stmt representing the statements in "scop".
4786 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
4787 __isl_keep isl_set
*context
)
4790 struct gpu_stmt
*stmts
;
4792 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->n_stmt
);
4795 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
4796 struct gpu_stmt
*s
= &stmts
[i
];
4798 s
->id
= isl_set_get_tuple_id(scop
->stmts
[i
]->domain
);
4799 s
->body
= scop
->stmts
[i
]->body
;
4800 pet_stmt_extract_accesses(s
);
4806 /* Replace the scop in the "input" file by equivalent code
4807 * that uses the GPU. "scop" is assumed to correspond to this scop.
4809 * We first compute a schedule that respects the dependences
4810 * of the original program and select the outermost band
4811 * of tilable dimensions that has at least one parallel loop.
4812 * We then have three blocks of dimensions
4816 * The tilable band "B" is first tiled according to "tile" sizes, resulting
4821 * For each iteration of the T loop and for each array, we compute
4822 * the array elements accessed by that iteration, construct a rectangular
4823 * box around it and shift it to the origin. The result is used
4824 * as shared memory for the array.
4826 * We then split off at most 2 parallel loops from the T loops and
4827 * at most 3 parallel loops from the P loops
4831 * The T1/P1 loops are then tiled or "wrapped" over the blocks/threads,
4832 * according to "grid"/"block" sizes.
4834 * H T1T T1P T2 P1T P1P P2 G
4836 * Finally, the T1P and P1P iterators are equated to the block and
4837 * thread dimensions respectively and so are effectively removed.
4838 * The H loops are run on the host. The T1T, T2, P1T, P2 and G loops
4839 * are run on the GPU.
4841 * Code is generated in three stages. We first generate code for the
4842 * host (the H loops), with iterators h%d. Then, for each leaf node
4843 * of the resulting AST, we generate code for the shared loops (up to
4844 * and including T2), with iterators g%d and after equating the H loops
4845 * to h%d parameters and the T1P loops to the block dimensions.
4846 * Finally, we generate code for the remaining loops in a similar fashion.
4848 __isl_give isl_ast_node
*generate_gpu(isl_ctx
*ctx
, struct gpu_prog
*prog
,
4849 struct ppcg_options
*options
)
4851 isl_union_map
*sched
;
4860 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
4861 gen
.options
= options
;
4863 compute_schedule(&gen
);
4864 compute_copy_in_and_out(&gen
);
4867 tree
= generate_host_code(&gen
);
4869 clear_gpu_gen(&gen
);
4874 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
4876 struct gpu_prog
*prog
;
4881 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
4886 prog
->context
= isl_set_copy(scop
->context
);
4887 prog
->n_stmts
= scop
->n_stmt
;
4888 prog
->stmts
= extract_stmts(ctx
, scop
, prog
->context
);
4889 prog
->read
= isl_union_map_copy(scop
->reads
);
4890 prog
->write
= isl_union_map_copy(scop
->writes
);
4892 collect_array_info(prog
);
4897 void gpu_prog_free(struct gpu_prog
*prog
)
4901 free_array_info(prog
);
4902 free_stmts(prog
->stmts
, prog
->n_stmts
);
4903 isl_union_set_free(prog
->copy_in
);
4904 isl_union_set_free(prog
->copy_out
);
4905 isl_union_map_free(prog
->read
);
4906 isl_union_map_free(prog
->write
);
4907 isl_set_free(prog
->context
);