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 isl_int_init(tile
->bound
[i
].size
);
270 tile
->bound
[i
].lb
= NULL
;
271 isl_int_init(tile
->bound
[i
].stride
);
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_int_clear(tile
->bound
[j
].size
);
288 isl_int_clear(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 bounds on the host arrays based on the accessed elements
321 * and collect all references to the array.
323 * If the array is zero-dimensional, i.e., a scalar, we check
324 * whether it is read-only.
326 static int extract_array_info(__isl_take isl_set
*array
, void *user
)
329 struct gpu_prog
*prog
= (struct gpu_prog
*)user
;
333 struct pet_array
*pa
;
335 n_index
= isl_set_dim(array
, isl_dim_set
);
336 name
= isl_set_get_tuple_name(array
);
337 bounds
= isl_alloc_array(isl_set_get_ctx(array
),
338 isl_pw_aff
*, n_index
);
340 prog
->array
[prog
->n_array
].dim
= isl_set_get_space(array
);
341 prog
->array
[prog
->n_array
].name
= strdup(name
);
342 prog
->array
[prog
->n_array
].n_index
= n_index
;
343 prog
->array
[prog
->n_array
].bound
= bounds
;
345 pa
= find_array(prog
->scop
, array
);
348 prog
->array
[prog
->n_array
].type
= strdup(pa
->element_type
);
349 prog
->array
[prog
->n_array
].size
= pa
->element_size
;
350 prog
->array
[prog
->n_array
].local
= pa
->declared
&& !pa
->exposed
;
354 isl_union_map
*write
;
357 write
= isl_union_map_copy(prog
->write
);
358 space
= isl_set_universe(isl_set_get_space(array
));
359 write
= isl_union_map_intersect_range(write
,
360 isl_union_set_from_set(space
));
361 empty
= isl_union_map_is_empty(write
);
362 isl_union_map_free(write
);
364 prog
->array
[prog
->n_array
].read_only
= empty
;
367 for (i
= 0; i
< n_index
; ++i
) {
372 isl_set
*size
= i
== 0 ? array
: pa
->extent
;
374 bound
= isl_set_dim_max(isl_set_copy(size
), i
);
376 dom
= isl_pw_aff_domain(isl_pw_aff_copy(bound
));
377 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
378 one
= isl_aff_zero_on_domain(ls
);
379 one
= isl_aff_add_constant_si(one
, 1);
380 bound
= isl_pw_aff_add(bound
, isl_pw_aff_alloc(dom
, one
));
381 bound
= isl_pw_aff_gist(bound
, isl_set_copy(prog
->context
));
386 collect_references(prog
, &prog
->array
[prog
->n_array
]);
394 void collect_array_info(struct gpu_prog
*prog
)
396 isl_union_set
*arrays
;
398 arrays
= isl_union_map_range(isl_union_map_copy(prog
->read
));
399 arrays
= isl_union_set_union(arrays
,
400 isl_union_map_range(isl_union_map_copy(prog
->write
)));
401 arrays
= isl_union_set_coalesce(arrays
);
403 prog
->n_array
= isl_union_set_n_set(arrays
);
404 prog
->array
= isl_alloc_array(prog
->ctx
,
405 struct gpu_array_info
, prog
->n_array
);
408 isl_union_set_foreach_set(arrays
, &extract_array_info
, prog
);
409 isl_union_set_free(arrays
);
412 static void free_array_info(struct gpu_prog
*prog
)
416 for (i
= 0; i
< prog
->n_array
; ++i
) {
417 int n_index
= prog
->array
[i
].n_index
;
418 free(prog
->array
[i
].type
);
419 free(prog
->array
[i
].name
);
420 for (j
= 0; j
< n_index
; ++j
)
421 isl_pw_aff_free(prog
->array
[i
].bound
[j
]);
422 isl_space_free(prog
->array
[i
].dim
);
423 free(prog
->array
[i
].bound
);
424 free(prog
->array
[i
].refs
);
429 /* Check if a gpu array is a scalar. A scalar is a value that is not stored
430 * as an array or through a pointer reference, but as single data element. At
431 * the moment, scalars are represented as zero dimensional arrays.
433 int gpu_array_is_scalar(struct gpu_array_info
*array
)
435 return (array
->n_index
== 0);
438 /* Is "array" a read-only scalar?
440 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
)
442 return gpu_array_is_scalar(array
) && array
->read_only
;
445 /* Internal data structure for extract_size_of_type.
446 * "type" specifies the name of the space that we want to extract.
447 * "res" is used to store the subset of that space.
449 struct ppcg_extract_size_data
{
454 /* This function is called for each set in a union_set.
455 * If the name of the set matches data->type, we store the
458 static int extract_size_of_type(__isl_take isl_set
*size
, void *user
)
460 struct ppcg_extract_size_data
*data
= user
;
463 name
= isl_set_get_tuple_name(size
);
464 if (name
&& !strcmp(name
, data
->type
)) {
473 /* Given a union map { kernel[i] -> *[...] },
474 * return the range in the space called "type" for the kernel with
475 * sequence number "id".
477 static __isl_give isl_set
*extract_sizes(__isl_keep isl_union_map
*sizes
,
478 const char *type
, int id
)
482 isl_union_set
*local_sizes
;
483 struct ppcg_extract_size_data data
= { type
, NULL
};
488 space
= isl_union_map_get_space(sizes
);
489 space
= isl_space_set_from_params(space
);
490 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
491 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "kernel");
492 dom
= isl_set_universe(space
);
493 dom
= isl_set_fix_si(dom
, isl_dim_set
, 0, id
);
495 local_sizes
= isl_union_set_apply(isl_union_set_from_set(dom
),
496 isl_union_map_copy(sizes
));
497 isl_union_set_foreach_set(local_sizes
, &extract_size_of_type
, &data
);
498 isl_union_set_free(local_sizes
);
502 /* Given a singleton set, extract the first (at most *len) elements
503 * of the single integer tuple into *sizes and update *len if needed.
505 static void read_sizes_from_set(__isl_take isl_set
*set
, int *sizes
, int *len
)
514 dim
= isl_set_dim(set
, isl_dim_set
);
520 for (i
= 0; i
< *len
; ++i
) {
523 ok
= isl_set_plain_is_fixed(set
, isl_dim_set
, i
, &v
);
526 sizes
[i
] = isl_int_get_si(v
);
534 /* Extract user specified "tile" sizes from the "sizes" command line option,
535 * defaulting to option->tile_size in each dimension.
537 static void read_tile_sizes(struct gpu_gen
*gen
)
542 gen
->tile_size
= isl_alloc_array(gen
->ctx
, int, gen
->tile_len
);
543 assert(gen
->tile_size
);
544 for (n
= 0; n
< gen
->tile_len
; ++n
)
545 gen
->tile_size
[n
] = gen
->options
->tile_size
;
547 size
= extract_sizes(gen
->sizes
, "tile", gen
->kernel_id
);
548 read_sizes_from_set(size
, gen
->tile_size
, &gen
->tile_len
);
550 if (gen
->n_parallel
> gen
->tile_len
)
551 gen
->n_parallel
= gen
->tile_len
;
554 /* Extract user specified "block" sizes from the "sizes" command line option,
555 * after filling in some potentially useful defaults.
557 static void read_block_sizes(struct gpu_gen
*gen
)
563 gen
->n_block
= (n
<= 3) ? n
: 3;
564 switch (gen
->n_block
) {
566 gen
->block_dim
[0] = 512;
569 gen
->block_dim
[0] = 32;
570 gen
->block_dim
[1] = 16;
573 gen
->block_dim
[0] = 32;
574 gen
->block_dim
[1] = 4;
575 gen
->block_dim
[2] = 4;
579 size
= extract_sizes(gen
->sizes
, "block", gen
->kernel_id
);
580 read_sizes_from_set(size
, gen
->block_dim
, &gen
->n_block
);
583 /* Extract user specified "grid" sizes from the "sizes" command line option,
584 * after filling in some potentially useful defaults.
586 static void read_grid_sizes(struct gpu_gen
*gen
)
588 int n
= gen
->n_parallel
;
591 gen
->n_grid
= (n
<= 2) ? n
: 2;
592 switch (gen
->n_grid
) {
594 gen
->grid_dim
[0] = 32768;
597 gen
->grid_dim
[0] = 256;
598 gen
->grid_dim
[1] = 256;
602 size
= extract_sizes(gen
->sizes
, "grid", gen
->kernel_id
);
603 read_sizes_from_set(size
, gen
->grid_dim
, &gen
->n_grid
);
606 /* Extract user specified sizes from the "sizes" command line option
607 * after filling in some potentially useful defaults.
609 static void read_sizes(struct gpu_gen
*gen
)
611 read_tile_sizes(gen
);
612 read_block_sizes(gen
);
613 read_grid_sizes(gen
);
616 static void free_stmts(struct gpu_stmt
*stmts
, int n
)
620 for (i
= 0; i
< n
; ++i
) {
621 struct gpu_stmt_access
*access
, *next
;
623 for (access
= stmts
[i
].accesses
; access
; access
= next
) {
625 isl_map_free(access
->access
);
629 isl_id_free(stmts
[i
].id
);
634 void clear_gpu_gen(struct gpu_gen
*gen
)
636 isl_union_map_free(gen
->sizes
);
637 isl_union_map_free(gen
->sched
);
640 /* Construct a map from a domain of dimensionality "len"
641 * to a domain of dimensionality "len" + "tile_len" that tiles
642 * the "tile_len" coordinates starting at "first".
643 * In particular, [s_i] -> [s_i / tile_size[i], s_i % tile_size[i]].
644 * "dim" prescribes the parameters.
646 static __isl_give isl_map
*tile(__isl_take isl_space
*dim
, int len
,
647 int first
, int tile_len
, int *tile_size
)
657 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
658 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
+ tile_len
);
659 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
660 ls
= isl_local_space_from_space(dim
);
662 for (i
= 0; i
< len
- tile_len
; ++i
) {
663 int j
= i
< first
? i
: i
+ tile_len
;
664 int k
= i
< first
? i
: i
+ 2 * tile_len
;
666 c
= isl_equality_alloc(isl_local_space_copy(ls
));
667 isl_int_set_si(v
, -1);
668 c
= isl_constraint_set_coefficient(c
, isl_dim_in
, j
, v
);
669 isl_int_set_si(v
, 1);
670 c
= isl_constraint_set_coefficient(c
, isl_dim_out
, k
, v
);
671 bmap
= isl_basic_map_add_constraint(bmap
, c
);
674 for (i
= 0; i
< tile_len
; ++i
) {
675 c
= isl_equality_alloc(isl_local_space_copy(ls
));
676 isl_int_set_si(v
, -1);
677 c
= isl_constraint_set_coefficient(c
, isl_dim_in
, first
+ i
, v
);
678 isl_int_set_si(v
, tile_size
[i
]);
679 c
= isl_constraint_set_coefficient(c
, isl_dim_out
,
681 isl_int_set_si(v
, 1);
682 c
= isl_constraint_set_coefficient(c
, isl_dim_out
,
683 first
+ i
+ tile_len
, v
);
684 bmap
= isl_basic_map_add_constraint(bmap
, c
);
686 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
687 isl_int_set_si(v
, 1);
688 c
= isl_constraint_set_coefficient(c
, isl_dim_out
,
689 first
+ i
+ tile_len
, v
);
690 bmap
= isl_basic_map_add_constraint(bmap
, c
);
692 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
693 isl_int_set_si(v
, -1);
694 c
= isl_constraint_set_coefficient(c
, isl_dim_out
,
695 first
+ i
+ tile_len
, v
);
696 isl_int_set_si(v
, tile_size
[i
] - 1);
697 c
= isl_constraint_set_constant(c
, v
);
698 bmap
= isl_basic_map_add_constraint(bmap
, c
);
701 isl_local_space_free(ls
);
704 return isl_map_from_basic_map(bmap
);
707 /* Construct a map from a domain of dimensionality "len"
708 * to a domain of dimensionality "len" + "wrap_len" that "wraps"
709 * the "wrap_len" coordinates starting at "first" according to "wrap_size".
710 * In particular, [s_i] -> [s_i, s_i % wrap_size[i]].
711 * To do so, we need extra variables corresponding to [s_i / wrap_size[i]],
712 * that are projected out at the end.
713 * "dim" prescribes the parameters.
715 static __isl_give isl_map
*wrap(__isl_take isl_space
*dim
, int len
,
716 int first
, int wrap_len
, int *wrap_size
)
723 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
724 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
+ 2 * wrap_len
);
725 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
726 ls
= isl_local_space_from_space(dim
);
728 for (i
= 0; i
< len
; ++i
) {
729 int k
= i
< first
+ wrap_len
? i
: i
+ 2 * wrap_len
;
731 c
= isl_equality_alloc(isl_local_space_copy(ls
));
732 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, -1);
733 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, k
, 1);
734 bmap
= isl_basic_map_add_constraint(bmap
, c
);
737 for (i
= 0; i
< wrap_len
; ++i
) {
738 c
= isl_equality_alloc(isl_local_space_copy(ls
));
739 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
741 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
742 first
+ wrap_len
+ i
, 1);
743 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
744 first
+ 2 * wrap_len
+ i
, wrap_size
[i
]);
745 bmap
= isl_basic_map_add_constraint(bmap
, c
);
747 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
748 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
749 first
+ wrap_len
+ i
, 1);
750 bmap
= isl_basic_map_add_constraint(bmap
, c
);
752 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
753 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
,
754 first
+ wrap_len
+ i
, -1);
755 c
= isl_constraint_set_constant_si(c
, wrap_size
[i
] - 1);
756 bmap
= isl_basic_map_add_constraint(bmap
, c
);
759 isl_local_space_free(ls
);
761 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
,
762 first
+ 2 * wrap_len
, wrap_len
);
764 return isl_map_from_basic_map(bmap
);
767 /* Add "n" parameters named prefix%d.
769 static __isl_give isl_set
*add_params( __isl_take isl_set
*set
,
770 int n
, const char *prefix
)
776 nparam
= isl_set_dim(set
, isl_dim_param
);
777 set
= isl_set_add_dims(set
, isl_dim_param
, n
);
779 for (i
= 0; i
< n
; ++i
) {
780 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
781 set
= isl_set_set_dim_name(set
, isl_dim_param
,
788 /* Equate the "n" dimensions of "set" starting at "first" to
789 * freshly created parameters named prefix%d.
791 static __isl_give isl_set
*parametrize(__isl_take isl_set
*set
,
792 int first
, int n
, const char *prefix
)
802 nparam
= isl_set_dim(set
, isl_dim_param
);
804 set
= add_params(set
, n
, prefix
);
806 dim
= isl_set_get_space(set
);
807 bset
= isl_basic_set_universe(isl_space_copy(dim
));
808 ls
= isl_local_space_from_space(dim
);
812 for (i
= 0; i
< n
; ++i
) {
813 c
= isl_equality_alloc(isl_local_space_copy(ls
));
814 isl_int_set_si(v
, -1);
815 c
= isl_constraint_set_coefficient(c
, isl_dim_param
,
817 isl_int_set_si(v
, 1);
818 c
= isl_constraint_set_coefficient(c
, isl_dim_set
, first
+ i
,
820 bset
= isl_basic_set_add_constraint(bset
, c
);
824 isl_local_space_free(ls
);
826 return isl_set_intersect(set
, isl_set_from_basic_set(bset
));
829 /* Given a parameter space "space", create a set of dimension "len"
830 * of which the "n" dimensions starting at "first" are equated to
831 * freshly created parameters named prefix%d.
833 static __isl_give isl_set
*parametrization(__isl_take isl_space
*space
,
834 int len
, int first
, int n
, const char *prefix
)
838 space
= isl_space_set_from_params(space
);
839 space
= isl_space_add_dims(space
, isl_dim_set
, len
);
840 set
= isl_set_universe(space
);
842 return parametrize(set
, first
, n
, prefix
);
845 /* Tile the B loops over the tile sizes and then tile/wrap
846 * the T1 loops over the blocks.
848 static __isl_give isl_union_map
*tile_schedule(struct gpu_gen
*gen
,
849 __isl_take isl_union_map
*sched
)
852 isl_map
*tiling
, *block_tiling
;
854 dim
= isl_union_map_get_space(sched
);
855 tiling
= tile(isl_space_copy(dim
), gen
->untiled_len
,
856 gen
->tile_first
, gen
->tile_len
, gen
->tile_size
);
858 if (gen
->options
->wrap
)
859 block_tiling
= wrap(dim
, gen
->untiled_len
+ gen
->tile_len
,
860 gen
->tile_first
, gen
->n_grid
, gen
->grid_dim
);
862 block_tiling
= tile(dim
, gen
->untiled_len
+ gen
->tile_len
,
863 gen
->tile_first
, gen
->n_grid
, gen
->grid_dim
);
865 gen
->tiled_len
= gen
->untiled_len
+ gen
->tile_len
+ gen
->n_grid
;
867 tiling
= isl_map_apply_range(tiling
, block_tiling
);
869 sched
= isl_union_map_apply_range(sched
,
870 isl_union_map_from_map(tiling
));
872 gen
->shared_len
= gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
;
877 /* Equate the "T1P" iterators in the tiled schedule "sched"
878 * to the block dimensions.
880 static __isl_give isl_union_map
*parametrize_tiled_schedule(
881 struct gpu_gen
*gen
, __isl_take isl_union_map
*sched
)
886 dim
= isl_union_map_get_space(sched
);
887 par
= parametrization(dim
, gen
->tiled_len
,
888 gen
->tile_first
+ gen
->n_grid
, gen
->n_grid
, "b");
889 sched
= isl_union_map_intersect_range(sched
,
890 isl_union_set_from_set(par
));
895 /* Tile/wrap the P1 loops over the threads.
897 static __isl_give isl_union_map
*thread_tile_schedule(struct gpu_gen
*gen
,
898 __isl_take isl_union_map
*sched
)
904 dim
= isl_union_map_get_space(sched
);
906 if (gen
->options
->wrap
)
907 tiling
= wrap(isl_space_copy(dim
), gen
->tiled_len
,
908 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
910 tiling
= tile(isl_space_copy(dim
), gen
->tiled_len
,
911 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
912 gen
->thread_tiled_len
= gen
->tiled_len
+ gen
->n_block
;
914 sched
= isl_union_map_apply_range(sched
,
915 isl_union_map_from_map(tiling
));
917 par
= parametrization(dim
, gen
->thread_tiled_len
,
918 gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
+ gen
->n_block
,
920 sched
= isl_union_map_intersect_range(sched
,
921 isl_union_set_from_set(par
));
923 gen
->shared_len
= gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
;
928 /* If the user asked for it, scale the shared memory tile loops
929 * (T1T and T2) of "sched" by gen->tile_size[i].
930 * If we are not performing "wrapping", then additionally scale the T1P
931 * loops by gen->grid_dim[i].
933 static __isl_give isl_union_map
*scale_tile_loops(struct gpu_gen
*gen
,
934 __isl_take isl_union_map
*sched
)
938 isl_basic_map
*scale
;
942 if (!gen
->options
->scale_tile_loops
)
945 dim
= isl_union_map_get_space(sched
);
946 dim
= isl_space_add_dims(dim
, isl_dim_in
, gen
->tiled_len
);
947 dim
= isl_space_add_dims(dim
, isl_dim_out
, gen
->tiled_len
);
948 scale
= isl_basic_map_universe(isl_space_copy(dim
));
949 ls
= isl_local_space_from_space(dim
);
951 for (i
= 0; i
< gen
->tiled_len
; ++i
) {
954 if (i
>= gen
->tile_first
&& i
< gen
->tile_first
+ gen
->n_grid
) {
955 f
= gen
->tile_size
[i
- gen
->tile_first
];
956 if (!gen
->options
->wrap
)
957 f
*= gen
->grid_dim
[i
- gen
->tile_first
];
958 } else if (i
>= gen
->tile_first
+ gen
->n_grid
&&
959 i
< gen
->tile_first
+ gen
->n_grid
+ gen
->tile_len
) {
960 f
= gen
->tile_size
[i
- (gen
->tile_first
+ gen
->n_grid
)];
963 c
= isl_equality_alloc(isl_local_space_copy(ls
));
964 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
965 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
966 scale
= isl_basic_map_add_constraint(scale
, c
);
969 isl_local_space_free(ls
);
971 sched
= isl_union_map_apply_range(sched
,
972 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
977 /* If we are not performing "wrapping" and if the user asked for it,
978 * scale the thread tile loops (P1T) of "sched" by gen->block_dim[i].
980 static __isl_give isl_union_map
*scale_thread_tile_loops(struct gpu_gen
*gen
,
981 __isl_take isl_union_map
*sched
)
985 isl_basic_map
*scale
;
989 if (gen
->options
->wrap
)
991 if (!gen
->options
->scale_tile_loops
)
994 dim
= isl_union_map_get_space(sched
);
995 dim
= isl_space_add_dims(dim
, isl_dim_in
, gen
->thread_tiled_len
);
996 dim
= isl_space_add_dims(dim
, isl_dim_out
, gen
->thread_tiled_len
);
997 scale
= isl_basic_map_universe(isl_space_copy(dim
));
998 ls
= isl_local_space_from_space(dim
);
1000 for (i
= 0; i
< gen
->thread_tiled_len
; ++i
) {
1003 if (i
>= gen
->shared_len
&&
1004 i
< gen
->shared_len
+ gen
->n_block
)
1005 f
= gen
->block_dim
[i
- gen
->shared_len
];
1007 c
= isl_equality_alloc(isl_local_space_copy(ls
));
1008 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
1009 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
1010 scale
= isl_basic_map_add_constraint(scale
, c
);
1013 isl_local_space_free(ls
);
1015 sched
= isl_union_map_apply_range(sched
,
1016 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
1021 /* If we are not performing "wrapping" and if the user asked for it,
1022 * scale the "n_tile" loops starting at "first" of "sched" by gen->block_dim[i].
1024 static __isl_give isl_union_map
*scale_access_tile_loops(struct gpu_gen
*gen
,
1025 __isl_take isl_union_map
*sched
, int len
, int first
, int n_tile
)
1029 isl_basic_map
*scale
;
1031 isl_local_space
*ls
;
1033 if (gen
->options
->wrap
)
1035 if (!gen
->options
->scale_tile_loops
)
1038 dim
= isl_union_map_get_space(sched
);
1039 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
1040 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
);
1041 scale
= isl_basic_map_universe(isl_space_copy(dim
));
1042 ls
= isl_local_space_from_space(dim
);
1044 for (i
= 0; i
< len
; ++i
) {
1047 if (i
>= first
&& i
< first
+ n_tile
)
1048 f
= gen
->block_dim
[i
- first
];
1050 c
= isl_equality_alloc(isl_local_space_copy(ls
));
1051 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, f
);
1052 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
1053 scale
= isl_basic_map_add_constraint(scale
, c
);
1056 isl_local_space_free(ls
);
1058 sched
= isl_union_map_apply_range(sched
,
1059 isl_union_map_from_map(isl_map_from_basic_map(scale
)));
1064 /* Add "len" parameters p[i] called prefix%d,
1065 * with bounds to 0 <= p[i] < size[i].
1067 __isl_give isl_set
*add_bounded_parameters(__isl_take isl_set
*set
,
1068 int len
, int *size
, const char *prefix
)
1074 isl_basic_set
*bset
;
1076 isl_local_space
*ls
;
1079 nparam
= isl_set_dim(set
, isl_dim_param
);
1080 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
1082 for (i
= 0; i
< len
; ++i
) {
1083 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
1084 set
= isl_set_set_dim_name(set
, isl_dim_param
,
1088 dim
= isl_set_get_space(set
);
1089 bset
= isl_basic_set_universe(isl_space_copy(dim
));
1090 ls
= isl_local_space_from_space(dim
);
1094 for (i
= 0; i
< len
; ++i
) {
1095 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
1096 isl_int_set_si(v
, 1);
1097 c
= isl_constraint_set_coefficient(c
, isl_dim_param
, nparam
+ i
,
1099 bset
= isl_basic_set_add_constraint(bset
, c
);
1101 c
= isl_inequality_alloc(isl_local_space_copy(ls
));
1102 isl_int_set_si(v
, -1);
1103 c
= isl_constraint_set_coefficient(c
, isl_dim_param
, nparam
+ i
,
1105 isl_int_set_si(v
, size
[i
] - 1);
1106 c
= isl_constraint_set_constant(c
, v
);
1107 bset
= isl_basic_set_add_constraint(bset
, c
);
1111 isl_local_space_free(ls
);
1113 return isl_set_intersect(set
, isl_set_from_basic_set(bset
));
1116 /* Add "len" parameters p[i] called prefix%d,
1117 * with bounds to 0 <= p[i] < size[i].
1119 static __isl_give isl_set
*add_bounded_parameters_dynamic(
1120 __isl_take isl_set
*set
, __isl_keep isl_multi_pw_aff
*size
,
1126 isl_local_space
*ls
;
1129 len
= isl_multi_pw_aff_dim(size
, isl_dim_out
);
1130 nparam
= isl_set_dim(set
, isl_dim_param
);
1131 set
= isl_set_add_dims(set
, isl_dim_param
, len
);
1133 for (i
= 0; i
< len
; ++i
) {
1134 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
1135 set
= isl_set_set_dim_name(set
, isl_dim_param
,
1139 space
= isl_space_params(isl_set_get_space(set
));
1140 ls
= isl_local_space_from_space(space
);
1141 for (i
= 0; i
< len
; ++i
) {
1142 isl_pw_aff
*param
, *size_i
, *zero
;
1145 param
= isl_pw_aff_var_on_domain(isl_local_space_copy(ls
),
1146 isl_dim_param
, nparam
+ i
);
1148 size_i
= isl_multi_pw_aff_get_pw_aff(size
, i
);
1149 bound
= isl_pw_aff_lt_set(isl_pw_aff_copy(param
), size_i
);
1150 set
= isl_set_intersect_params(set
, bound
);
1152 zero
= isl_pw_aff_zero_on_domain(isl_local_space_copy(ls
));
1153 bound
= isl_pw_aff_ge_set(param
, zero
);
1154 set
= isl_set_intersect_params(set
, bound
);
1156 isl_local_space_free(ls
);
1161 /* Given a mapping "sched" of the form
1163 * [D -> A] -> [D -> T(A)]
1165 * apply the mapping encoded in tile->bound[i].shift_map
1166 * to the range of "sched".
1167 * The mappings in tile->bound[i].shift_map are of the form
1169 * [D -> a] -> [D -> s(D,a)]
1171 * We first compose them with a mapping
1175 * (If tile->bound[i].shift_map is not set, then it is assumed to be
1176 * an identity mapping and then we use this second mapping instead.)
1179 * [D -> a] -> s(D,a)
1181 * We precompose them with a projection on the i th dimension to obtain
1183 * [D -> T] -> s(D,T)
1185 * and collect these into
1187 * [D -> T] -> S(D,T)
1189 * Introducing D in the range yields
1191 * [D -> T] -> [D -> S(D,T)]
1193 * and application to "sched" yields
1195 * [D -> A] -> [D -> S(D,T(A))]
1197 static __isl_give isl_map
*pre_shift(__isl_take isl_map
*sched
,
1198 struct gpu_array_tile
*tile
)
1201 isl_ctx
*ctx
= isl_map_get_ctx(sched
);
1202 isl_space
*space
, *space2
;
1204 isl_map
*map
, *id
, *pre_shift
;
1206 space
= isl_space_range(isl_map_get_space(sched
));
1207 space2
= isl_space_from_domain(isl_space_copy(space
));
1208 pre_shift
= isl_map_universe(space2
);
1209 space
= isl_space_domain(isl_space_unwrap(space
));
1210 id
= isl_map_identity(isl_space_map_from_set(isl_space_copy(space
)));
1211 space
= isl_space_from_domain(space
);
1212 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
1213 def
= isl_basic_map_range_map(isl_basic_map_universe(space
));
1215 for (i
= 0; i
< tile
->n
; ++i
) {
1216 isl_basic_map
*bmap
, *drop
;
1219 space
= isl_space_alloc(ctx
, 0, tile
->n
, tile
->n
);
1220 proj
= isl_map_identity(space
);
1221 proj
= isl_map_project_out(proj
, isl_dim_out
,
1222 i
+ 1, tile
->n
- (i
+ 1));
1223 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, i
);
1224 proj
= isl_map_product(isl_map_copy(id
), proj
);
1226 if (!tile
->bound
[i
].shift_map
)
1227 bmap
= isl_basic_map_copy(def
);
1229 bmap
= isl_basic_map_copy(tile
->bound
[i
].shift_map
);
1230 bmap
= isl_basic_map_apply_range(bmap
,
1231 isl_basic_map_copy(def
));
1234 map
= isl_map_from_basic_map(bmap
);
1235 map
= isl_map_apply_range(proj
, map
);
1236 pre_shift
= isl_map_flat_range_product(pre_shift
, map
);
1240 isl_basic_map_free(def
);
1242 space
= isl_space_domain(isl_map_get_space(pre_shift
));
1243 map
= isl_map_domain_map(isl_map_universe(isl_space_unwrap(space
)));
1244 pre_shift
= isl_map_range_product(map
, pre_shift
);
1246 sched
= isl_map_apply_range(sched
, pre_shift
);
1251 /* Given an access relation to a tile of an array, construct a map that
1252 * maps each element in the space of the access relation
1253 * to a copy of the tile shifted to the origin
1254 * (based on the lower bounds in group->private_tile or group->shared_tile).
1255 * If any of the indices is strided, then
1256 * {private,shared}_tile->bound[i].shift_map is applied to the index first.
1257 * The domain space of the resulting map is that of access "access",
1258 * while the range space is anonymous.
1259 * The resulting map only encodes the mapping to the shift tile and
1260 * not the constraints of "access".
1262 * Let the space of the access relation be
1266 * We first construct an identity relation on a wrapped copy of this space,
1267 * except that it strips off the name of array
1269 * [D -> A] -> [D -> T(A)] (1)
1271 * The bounds in tile->bound[i].lb are of the form
1275 * We collect them into
1279 * and then transform them into
1281 * [D -> T] -> T - B(D) (2)
1283 * Combining those two mappings (1) and (2) yields
1285 * [D -> A] -> T(A) - B(D)
1287 * If there are any strides, then (1) is first transformed into (1')
1289 * [D -> A] -> [D -> T'(A)] (1')
1291 * by a call to pre_shift.
1293 static __isl_give isl_map
*shift_access(__isl_take isl_map
*access
,
1294 struct gpu_array_ref_group
*group
)
1302 struct gpu_array_tile
*tile
;
1303 int n_index
= group
->array
->n_index
;
1305 tile
= group
->private_tile
;
1307 tile
= group
->shared_tile
;
1309 space
= isl_space_domain(isl_map_get_space(access
));
1310 space
= isl_space_map_from_set(space
);
1311 id1
= isl_map_identity(space
);
1312 space
= isl_space_range(isl_map_get_space(access
));
1313 space
= isl_space_map_from_set(space
);
1314 space
= isl_space_set_tuple_name(space
, isl_dim_out
, NULL
);
1315 id2
= isl_map_identity(space
);
1316 sched
= isl_map_product(id1
, id2
);
1318 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(sched
)));
1319 space
= isl_space_from_domain(isl_space_domain(space
));
1320 shift
= isl_map_universe(space
);
1321 for (i
= 0; i
< n_index
; ++i
) {
1322 map
= isl_map_from_aff(isl_aff_copy(tile
->bound
[i
].lb
));
1323 shift
= isl_map_flat_range_product(shift
, map
);
1326 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(sched
)));
1327 map
= isl_map_universe(space
);
1328 id1
= isl_map_range_map(isl_map_copy(map
));
1329 map
= isl_map_domain_map(map
);
1330 shift
= isl_map_neg(shift
);
1331 shift
= isl_map_apply_range(map
, shift
);
1332 shift
= isl_map_sum(id1
, shift
);
1334 for (i
= 0; i
< n_index
; ++i
)
1335 if (tile
->bound
[i
].shift_map
)
1339 sched
= pre_shift(sched
, tile
);
1341 sched
= isl_map_apply_range(sched
, shift
);
1343 isl_map_free(access
);
1348 /* Given a schedule that iterates over all elements in a piece of an array,
1349 * perform tiling/wrapping over the threads.
1351 * In particular, we tile the final iterators so that the final thread
1352 * dimension runs over the final array dimension.
1353 * However, if those final iterators have only a single iteration,
1354 * we try to tile earlier iterators instead.
1356 static __isl_give isl_map
*tile_access_schedule(struct gpu_gen
*gen
,
1357 __isl_take isl_map
*sched
)
1360 isl_union_map
*usched
;
1363 unsigned nvar
= isl_map_dim(sched
, isl_dim_out
);
1367 n_tile
= gen
->n_block
;
1368 if (n_tile
> nvar
) {
1370 sched
= isl_map_insert_dims(sched
,
1371 isl_dim_out
, 0, n_tile
- nvar
);
1372 for (i
= 0; i
< n_tile
- nvar
; ++i
)
1373 sched
= isl_map_fix_si(sched
, isl_dim_out
, i
, 0);
1377 first
= nvar
- n_tile
;
1379 for (; first
> 0; first
--)
1380 if (!isl_map_plain_is_fixed(sched
, isl_dim_out
,
1381 first
+ n_tile
- 1, NULL
))
1384 dim
= isl_map_get_space(sched
);
1385 dim
= isl_space_params(dim
);
1386 if (gen
->options
->wrap
)
1387 tiling
= wrap(isl_space_copy(dim
), nvar
, first
,
1388 n_tile
, gen
->block_dim
);
1390 tiling
= tile(isl_space_copy(dim
), nvar
, first
,
1391 n_tile
, gen
->block_dim
);
1392 sched
= isl_map_apply_range(sched
, tiling
);
1394 par
= parametrization(dim
, nvar
+ n_tile
, first
+ n_tile
, n_tile
, "t");
1395 sched
= isl_map_intersect_range(sched
, par
);
1397 usched
= isl_union_map_from_map(sched
);
1398 usched
= scale_access_tile_loops(gen
, usched
, nvar
+ n_tile
,
1400 sched
= isl_map_from_union_map(usched
);
1405 /* Given an index expression "pa" into a tile of an array, adjust the expression
1406 * to a shift of the tile to the origin
1407 * (based on the lower bounds in "bound".
1408 * If the index is strided, then we first add
1409 * bound->shift and divide by bound->stride.
1410 * In the end, we compute the gist with respect to "domain".
1412 * All of the input expression "pa", the set "domain" and
1413 * the output are expressed in terms of the AST schedule domain.
1414 * The expressions in "bound" are expressed
1415 * in terms of the first shared_len dimensions of the schedule computed by PPCG.
1416 * The mapping "sched2shared" maps the former domain to the latter domain.
1418 static __isl_give isl_pw_aff
*shift_index(__isl_take isl_pw_aff
*pa
,
1419 struct gpu_array_info
*array
,
1420 struct gpu_array_bound
*bound
, __isl_take isl_set
*domain
,
1421 __isl_take isl_map
*sched2shared
)
1425 isl_pw_multi_aff
*pma
;
1428 map
= isl_map_from_aff(isl_aff_copy(bound
->shift
));
1429 map
= isl_map_apply_range(isl_map_copy(sched2shared
), map
);
1430 pma
= isl_pw_multi_aff_from_map(map
);
1431 tmp
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
1432 isl_pw_multi_aff_free(pma
);
1433 pa
= isl_pw_aff_add(pa
, tmp
);
1434 pa
= isl_pw_aff_scale_down(pa
, bound
->stride
);
1438 map
= isl_map_from_aff(isl_aff_copy(bound
->lb
));
1439 map
= isl_map_apply_range(sched2shared
, map
);
1440 pma
= isl_pw_multi_aff_from_map(map
);
1441 tmp
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
1442 isl_pw_multi_aff_free(pma
);
1443 pa
= isl_pw_aff_sub(pa
, tmp
);
1444 pa
= isl_pw_aff_coalesce(pa
);
1445 pa
= isl_pw_aff_gist(pa
, domain
);
1450 /* Return the union of all read (read = 1) and/or write (write = 1)
1451 * access relations in the group.
1453 static __isl_give isl_union_map
*group_access_relation(
1454 struct gpu_array_ref_group
*group
, int read
, int write
)
1457 isl_union_map
*access
;
1459 access
= isl_union_map_empty(isl_map_get_space(group
->access
));
1460 for (i
= 0; i
< group
->n_ref
; ++i
) {
1463 if (!((read
&& group
->refs
[i
]->read
) ||
1464 (write
&& group
->refs
[i
]->write
)))
1466 map_i
= isl_map_copy(group
->refs
[i
]->access
);
1467 access
= isl_union_map_union(access
,
1468 isl_union_map_from_map(map_i
));
1474 /* Return a map from the first shared_len dimensions of the computed
1475 * schedule to the values of the given index "i"
1476 * of the elements in the array tile in global memory that corresponds
1477 * to the shared memory copy.
1478 * In particular, if a is the index, then the range of the map
1482 * is constrained as follows
1484 * tile_offset(D) <= a <= tile_offset(D) + tile_size - 1 (1)
1488 * 0 <= a <= array_size - 1 (2)
1491 * Note that if some stride has been detected (i.e., when
1492 * group->shared_tile->bound[i].shift is set), then offset and size (i.e.,
1493 * constraints (1)) apply to the shifted and scaled down copy of the tile.
1494 * These constraints therefore have to be mapped back to the original
1495 * array space using the inverse of the shift_map.
1497 static __isl_give isl_map
*group_tile_dim(struct gpu_array_ref_group
*group
,
1502 isl_map
*map
, *tile
, *gt
;
1505 map
= isl_map_from_aff(isl_aff_copy(group
->shared_tile
->bound
[i
].lb
));
1506 space
= isl_space_range(isl_map_get_space(map
));
1507 map
= isl_map_apply_range(map
, isl_map_lex_le(isl_space_copy(space
)));
1510 aff
= isl_aff_copy(group
->shared_tile
->bound
[i
].lb
);
1511 aff
= isl_aff_add_constant(aff
, group
->shared_tile
->bound
[i
].size
);
1512 map
= isl_map_from_aff(aff
);
1513 gt
= isl_map_lex_gt(space
);
1514 map
= isl_map_apply_range(map
, isl_map_copy(gt
));
1515 tile
= isl_map_intersect(tile
, map
);
1517 if (group
->shared_tile
->bound
[i
].shift
) {
1518 isl_basic_map
*shift
;
1519 shift
= isl_basic_map_copy(group
->shared_tile
->bound
[i
].shift_map
);
1520 shift
= isl_basic_map_reverse(shift
);
1521 tile
= isl_set_unwrap(isl_set_apply(isl_map_wrap(tile
),
1522 isl_map_from_basic_map(shift
)));
1525 tile
= isl_map_lower_bound_si(tile
, isl_dim_out
, 0, 0);
1527 bound
= isl_set_from_pw_aff(isl_pw_aff_copy(group
->array
->bound
[i
]));
1528 bound
= isl_set_apply(bound
, gt
);
1529 tile
= isl_map_intersect_range(tile
, bound
);
1534 /* Return a map from the first shared_len dimensions of the computed
1535 * schedule to the array tile in
1536 * global memory that corresponds to the shared memory copy.
1538 static __isl_give isl_map
*group_tile(struct gpu_array_ref_group
*group
)
1541 int n_index
= group
->array
->n_index
;
1544 tile
= group_tile_dim(group
, 0);
1545 for (i
= 1; i
< n_index
; ++i
) {
1548 tile_i
= group_tile_dim(group
, i
);
1549 tile
= isl_map_flat_range_product(tile
, tile_i
);
1552 tile
= isl_map_set_tuple_name(tile
, isl_dim_out
, group
->array
->name
);
1557 /* Given a mapping "sched" from the AST schedule to a domain,
1558 * return the corresponding mapping from the AST schedule to
1559 * to the first shared_len dimensions of the schedule computed by PPCG.
1561 static __isl_give isl_map
*compute_sched_to_shared(struct gpu_gen
*gen
,
1562 __isl_take isl_map
*sched
)
1564 isl_union_map
*umap
;
1568 space
= isl_space_range(isl_map_get_space(sched
));
1569 space
= isl_space_from_domain(space
);
1570 space
= isl_space_add_dims(space
, isl_dim_out
, gen
->shared_len
);
1572 umap
= isl_union_map_copy(gen
->shared_sched
);
1573 umap
= isl_union_map_apply_range(umap
,
1574 isl_union_map_copy(gen
->shared_proj
));
1575 map
= isl_union_map_extract_map(umap
, space
);
1576 isl_union_map_free(umap
);
1578 sched
= isl_map_apply_range(sched
, map
);
1579 sched
= isl_map_detect_equalities(sched
);
1584 /* Set unroll[j] if the input dimension j is involved in
1585 * the index expression represented by ma.
1587 static int check_unroll(__isl_take isl_set
*set
, __isl_take isl_multi_aff
*ma
,
1591 int n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
1592 int n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
1595 for (i
= 0; i
< n_out
; ++i
) {
1598 aff
= isl_multi_aff_get_aff(ma
, i
);
1599 for (j
= 0; j
< n_in
; ++j
)
1600 if (isl_aff_involves_dims(aff
, isl_dim_in
, j
, 1))
1606 isl_multi_aff_free(ma
);
1610 /* Given an array pos mapping input dimensions to the corresponding
1611 * output dimension, construct the corresponding map.
1613 static __isl_give isl_map
*permutation(__isl_take isl_space
*dim
,
1618 isl_basic_map
*bmap
;
1619 isl_local_space
*ls
;
1621 dim
= isl_space_add_dims(dim
, isl_dim_in
, len
);
1622 dim
= isl_space_add_dims(dim
, isl_dim_out
, len
);
1623 bmap
= isl_basic_map_universe(isl_space_copy(dim
));
1624 ls
= isl_local_space_from_space(dim
);
1626 for (i
= 0; i
< len
; ++i
) {
1627 c
= isl_equality_alloc(isl_local_space_copy(ls
));
1628 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
,
1630 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, pos
[i
],
1632 bmap
= isl_basic_map_add_constraint(bmap
, c
);
1634 isl_local_space_free(ls
);
1636 return isl_map_from_basic_map(bmap
);
1639 /* Find all loops involved in any of the index expressions for any of
1640 * the private accesses, move them innermost and then mark them as
1641 * requiring unrolling by setting gen->first_unroll.
1642 * The loops involved should all be parallel because of the checks
1643 * we performed in check_private_group_access. Moving them innermost
1644 * is therefore a valid transformation.
1646 * Loops up to gen->shared_len are generated before the mapping to
1647 * threads is applied. They should therefore be ignored.
1649 * We compute the hidden equalities of the schedule first
1650 * since we will need them in our calls to isl_pw_multi_aff_from_map
1651 * and because we want to make sure that the same equalities
1652 * are also available to the code generator.
1654 static __isl_give isl_union_map
*interchange_for_unroll(struct gpu_gen
*gen
,
1655 __isl_take isl_union_map
*sched
)
1658 int unroll
[gen
->thread_tiled_len
];
1659 int perm
[gen
->thread_tiled_len
];
1662 int len
= gen
->shared_len
+ gen
->n_parallel
+ gen
->n_block
;
1664 gen
->first_unroll
= -1;
1666 sched
= isl_union_map_detect_equalities(sched
);
1667 for (i
= 0; i
< gen
->thread_tiled_len
; ++i
)
1669 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
1670 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
1672 for (j
= 0; j
< array
->n_group
; ++j
) {
1673 isl_union_map
*access
;
1675 isl_pw_multi_aff
*pma
;
1677 if (!array
->groups
[j
]->private_tile
)
1680 access
= group_access_relation(array
->groups
[j
], 1, 1);
1681 access
= isl_union_map_apply_domain(access
,
1682 isl_union_map_copy(sched
));
1684 acc
= isl_map_from_union_map(access
);
1685 pma
= isl_pw_multi_aff_from_map(acc
);
1686 isl_pw_multi_aff_foreach_piece(pma
,
1687 &check_unroll
, unroll
);
1689 isl_pw_multi_aff_free(pma
);
1693 for (i
= gen
->shared_len
; i
< len
; ++i
)
1700 for (i
= len
; i
< gen
->thread_tiled_len
; ++i
)
1705 for (i
= 0; i
< gen
->shared_len
; ++i
)
1707 for (i
= gen
->shared_len
; i
< gen
->thread_tiled_len
; ++i
)
1710 gen
->first_unroll
= j
- gen
->shared_len
;
1711 for (i
= gen
->shared_len
; i
< len
; ++i
)
1715 dim
= isl_union_map_get_space(sched
);
1716 permute
= permutation(dim
, perm
, gen
->thread_tiled_len
);
1717 sched
= isl_union_map_apply_range(sched
,
1718 isl_union_map_from_map(permute
));
1723 /* Given a constraint
1725 * a(p,i) + j = g f(e)
1727 * or -a(p,i) - j = g f(e) if sign < 0,
1728 * store a(p,i) in bound->shift and g (stride) in bound->stride.
1729 * a(p,i) is assumed to be an expression in only the parameters
1730 * and the input dimensions.
1732 static void extract_stride(__isl_keep isl_constraint
*c
,
1733 struct gpu_array_bound
*bound
, isl_int stride
, int sign
)
1742 isl_int_set(bound
->stride
, stride
);
1744 space
= isl_constraint_get_space(c
);
1745 space
= isl_space_domain(space
);
1747 nparam
= isl_space_dim(space
, isl_dim_param
);
1748 nvar
= isl_space_dim(space
, isl_dim_set
);
1752 isl_constraint_get_constant(c
, &v
);
1755 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1756 aff
= isl_aff_set_constant(aff
, v
);
1758 for (i
= 0; i
< nparam
; ++i
) {
1759 isl_constraint_get_coefficient(c
, isl_dim_param
, i
, &v
);
1760 if (isl_int_is_zero(v
))
1764 aff
= isl_aff_add_coefficient(aff
, isl_dim_param
, i
, v
);
1767 for (i
= 0; i
< nvar
; ++i
) {
1768 isl_constraint_get_coefficient(c
, isl_dim_in
, i
, &v
);
1769 if (isl_int_is_zero(v
))
1773 aff
= isl_aff_add_coefficient(aff
, isl_dim_in
, i
, v
);
1781 /* Given an equality constraint of a map with a single output dimension j,
1782 * check if the constraint is of the form
1784 * a(p,i) + j = g f(e)
1786 * with a(p,i) an expression in the parameters and input dimensions
1787 * and f(e) an expression in the existentially quantified variables.
1788 * If so, and if g is larger than any such g from a previously considered
1789 * constraint, then call extract_stride to record the stride information
1792 static int check_stride_constraint(__isl_take isl_constraint
*c
, void *user
)
1797 struct gpu_array_bound
*bound
= user
;
1800 isl_int_init(stride
);
1802 n_div
= isl_constraint_dim(c
, isl_dim_div
);
1803 isl_constraint_get_coefficient(c
, isl_dim_out
, 0, &v
);
1805 if (n_div
&& (isl_int_is_one(v
) || isl_int_is_negone(v
))) {
1806 int s
= isl_int_sgn(v
);
1807 isl_int_set_si(stride
, 0);
1808 for (i
= 0; i
< n_div
; ++i
) {
1809 isl_constraint_get_coefficient(c
, isl_dim_div
, i
, &v
);
1810 isl_int_gcd(stride
, stride
, v
);
1812 if (!isl_int_is_zero(stride
) &&
1813 isl_int_gt(stride
, bound
->stride
))
1814 extract_stride(c
, bound
, stride
, s
);
1817 isl_int_clear(stride
);
1820 isl_constraint_free(c
);
1824 /* Given contraints on an array index i, check if we can find
1825 * a shift a(p) and a stride g such that
1827 * a(p) + i = 0 mod g
1829 * If so, record the information in bound and apply the mapping
1830 * i -> (i + a(p))/g to the array index in bounds and return
1831 * the new constraints.
1832 * If not, simply return the original constraints.
1834 * If bounds is a subset of the space
1838 * then the bound recorded in bound->shift is of the form
1842 * with s(D) equal to a(p) above.
1843 * The mapping recorded in bound->shift_map is of the form
1845 * [D -> i] -> [D -> (i + S(D))/g]
1847 * This mapping is computed as follows.
1848 * We first introduce "i" in the domain through precomposition
1849 * with [D -> i] -> D obtaining
1853 * Adding [D -> i] -> i produces
1855 * [D -> i] -> i + s(D)
1857 * and the domain product with [D -> i] -> D yields
1859 * [D -> i] -> [D -> i + s(D)]
1861 * Composition with [D -> i] -> [D -> i/g] gives the desired result.
1863 static __isl_give isl_basic_map
*check_stride(struct gpu_array_bound
*bound
,
1864 __isl_take isl_basic_map
*bounds
)
1867 isl_basic_map
*hull
;
1868 isl_basic_map
*shift
, *id
, *bmap
, *scale
;
1869 isl_basic_set
*bset
;
1872 isl_int_set_si(bound
->stride
, -1);
1874 hull
= isl_basic_map_affine_hull(isl_basic_map_copy(bounds
));
1876 isl_basic_map_foreach_constraint(hull
, &check_stride_constraint
, bound
);
1878 isl_basic_map_free(hull
);
1880 if (isl_int_is_neg(bound
->stride
))
1883 shift
= isl_basic_map_from_aff(isl_aff_copy(bound
->shift
));
1884 space
= isl_basic_map_get_space(bounds
);
1885 bmap
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
1886 shift
= isl_basic_map_apply_range(bmap
, shift
);
1887 space
= isl_basic_map_get_space(bounds
);
1888 id
= isl_basic_map_range_map(isl_basic_map_universe(space
));
1889 shift
= isl_basic_map_sum(id
, shift
);
1890 space
= isl_basic_map_get_space(bounds
);
1891 id
= isl_basic_map_domain_map(isl_basic_map_universe(space
));
1892 shift
= isl_basic_map_range_product(id
, shift
);
1894 space
= isl_space_domain(isl_basic_map_get_space(bounds
));
1895 id
= isl_basic_map_identity(isl_space_map_from_set(space
));
1896 space
= isl_space_range(isl_basic_map_get_space(bounds
));
1897 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
1898 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
1899 aff
= isl_aff_scale_down(aff
, bound
->stride
);
1900 scale
= isl_basic_map_from_aff(aff
);
1901 scale
= isl_basic_map_product(id
, scale
);
1903 bound
->shift_map
= isl_basic_map_apply_range(shift
, scale
);
1904 bmap
= isl_basic_map_copy(bound
->shift_map
);
1905 bset
= isl_basic_set_apply(isl_basic_map_wrap(bounds
), bmap
);
1906 bounds
= isl_basic_set_unwrap(bset
);
1911 /* Data used in compute_array_dim_size and compute_size_in_direction.
1913 * pos is the position of the variable representing the array index,
1914 * i.e., the variable for which want to compute the size. This variable
1915 * is also the last variable in the set.
1917 struct gpu_size_info
{
1918 isl_basic_set
*bset
;
1919 struct gpu_array_bound
*bound
;
1923 /* Given a constraint from the basic set describing the bounds on
1924 * an array index, check if it is a lower bound, say m i >= b(x), and,
1925 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
1926 * upper bound. If so, and if this bound is smaller than any bound
1927 * derived from earlier constraints, set the size to this bound on
1928 * the expression and the lower bound to ceil(b(x)/m).
1930 static int compute_size_in_direction(__isl_take isl_constraint
*c
, void *user
)
1932 struct gpu_size_info
*size
= user
;
1937 nparam
= isl_basic_set_dim(size
->bset
, isl_dim_param
);
1938 n_div
= isl_constraint_dim(c
, isl_dim_div
);
1940 if (isl_constraint_involves_dims(c
, isl_dim_div
, 0, n_div
)) {
1941 isl_constraint_free(c
);
1947 isl_constraint_get_coefficient(c
, isl_dim_set
, size
->pos
, &v
);
1949 if (isl_int_is_pos(v
)) {
1952 enum isl_lp_result res
;
1954 aff
= isl_constraint_get_bound(c
, isl_dim_set
, size
->pos
);
1955 aff
= isl_aff_ceil(aff
);
1957 lb
= isl_aff_copy(aff
);
1959 aff
= isl_aff_neg(aff
);
1960 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, size
->pos
, 1);
1962 res
= isl_basic_set_max(size
->bset
, aff
, &v
);
1965 if (res
== isl_lp_ok
) {
1966 isl_int_add_ui(v
, v
, 1);
1967 if (isl_int_is_neg(size
->bound
->size
) ||
1968 isl_int_lt(v
, size
->bound
->size
)) {
1969 isl_int_set(size
->bound
->size
, v
);
1970 lb
= isl_aff_drop_dims(lb
, isl_dim_in
,
1972 isl_aff_free(size
->bound
->lb
);
1973 size
->bound
->lb
= isl_aff_copy(lb
);
1980 isl_constraint_free(c
);
1985 /* Given a basic map "bounds" that maps parameters and input dimensions
1986 * to a single output dimension, look for an expression in the parameters
1987 * and input dimensions such that the range of the output dimension shifted
1988 * by this expression is a constant.
1990 * In particular, we currently only consider lower bounds on the output
1991 * dimension as candidate expressions.
1993 static int compute_array_dim_size(struct gpu_array_bound
*bound
,
1994 __isl_take isl_basic_map
*bounds
)
1996 struct gpu_size_info size
;
1998 bounds
= isl_basic_map_detect_equalities(bounds
);
1999 bounds
= check_stride(bound
, bounds
);
2001 isl_int_set_si(bound
->size
, -1);
2005 size
.pos
= isl_basic_map_dim(bounds
, isl_dim_in
);
2006 size
.bset
= isl_basic_map_wrap(bounds
);
2007 size
.bset
= isl_basic_set_flatten(size
.bset
);
2008 size
.bset
= isl_set_simple_hull(isl_basic_set_compute_divs(size
.bset
));
2009 isl_basic_set_foreach_constraint(size
.bset
, &compute_size_in_direction
,
2011 isl_basic_set_free(size
.bset
);
2013 return isl_int_is_nonneg(bound
->size
) ? 0 : -1;
2016 /* Check if we can find a memory tile for the given array
2017 * based on the given accesses, and if so, put the results in "tile".
2019 * We project the accesses on each index in turn and look for a parametric
2020 * offset such that the size is constant.
2022 static int can_tile(__isl_keep isl_map
*access
, struct gpu_array_tile
*tile
)
2026 for (i
= 0; i
< tile
->n
; ++i
) {
2028 isl_basic_map
*hull
;
2030 access_i
= isl_map_copy(access
);
2031 access_i
= isl_map_project_out(access_i
, isl_dim_out
, 0, i
);
2032 access_i
= isl_map_project_out(access_i
, isl_dim_out
,
2033 1, tile
->n
- (i
+ 1));
2034 access_i
= isl_map_compute_divs(access_i
);
2035 hull
= isl_map_simple_hull(access_i
);
2036 if (compute_array_dim_size(&tile
->bound
[i
], hull
) < 0)
2043 /* Construct a map with input the shared tile loops and the loops that
2044 * will be wrapped around the threads that relates these later loops
2045 * to the thread indices and then projects them out.
2047 static __isl_give isl_map
*compute_privatization(struct gpu_gen
*gen
)
2055 dim
= isl_union_map_get_space(gen
->shared_sched
);
2057 if (gen
->options
->wrap
)
2058 tiling
= wrap(isl_space_copy(dim
), gen
->shared_len
+ gen
->n_block
,
2059 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
2061 tiling
= tile(isl_space_copy(dim
), gen
->shared_len
+ gen
->n_block
,
2062 gen
->shared_len
, gen
->n_block
, gen
->block_dim
);
2066 par
= parametrization(dim
, gen
->shared_len
+ 2 * gen
->n_block
,
2067 gen
->tile_first
+ gen
->tile_len
+ gen
->n_grid
+ gen
->n_block
,
2070 priv
= isl_map_align_params(priv
, isl_set_get_space(par
));
2071 priv
= isl_map_intersect_range(priv
, par
);
2073 dim
= isl_map_get_space(priv
);
2074 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
2075 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
2076 proj
= projection(dim
, gen
->shared_len
+ 2 * gen
->n_block
,
2079 priv
= isl_map_apply_range(priv
, proj
);
2084 /* Construct a map from domain_dim to domain_dim that increments
2085 * the dimension at position "pos" and leaves all other dimensions
2088 static __isl_give isl_map
*next(__isl_take isl_space
*domain_dim
, int pos
)
2091 int len
= isl_space_dim(domain_dim
, isl_dim_set
);
2093 isl_basic_map
*next
;
2094 isl_local_space
*ls
;
2096 dim
= isl_space_map_from_set(domain_dim
);
2097 next
= isl_basic_map_universe(isl_space_copy(dim
));
2098 ls
= isl_local_space_from_space(dim
);
2100 for (i
= 0; i
< len
; ++i
) {
2103 c
= isl_equality_alloc(isl_local_space_copy(ls
));
2104 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, i
, 1);
2105 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, i
, -1);
2107 c
= isl_constraint_set_constant_si(c
, 1);
2108 next
= isl_basic_map_add_constraint(next
, c
);
2111 isl_local_space_free(ls
);
2113 return isl_map_from_basic_map(next
);
2116 /* Check if the given access is coalesced.
2117 * That is, check whether incrementing the dimension that will get
2118 * wrapped over the last thread index results in incrementing
2119 * the last array index.
2121 * This function is only called for access relations without reuse.
2123 static int access_is_coalesced(struct gpu_gen
*gen
,
2124 __isl_keep isl_union_map
*access
)
2127 isl_map
*access_map
;
2128 isl_map
*next_thread_x
;
2129 isl_map
*next_element
;
2133 access
= isl_union_map_copy(access
);
2134 access
= isl_union_map_apply_domain(access
,
2135 isl_union_map_copy(gen
->tiled_sched
));
2136 access_map
= isl_map_from_union_map(access
);
2138 dim
= isl_map_get_space(access_map
);
2139 dim
= isl_space_domain(dim
);
2140 next_thread_x
= next(dim
, gen
->shared_len
+ gen
->n_block
- 1);
2142 dim
= isl_map_get_space(access_map
);
2143 dim
= isl_space_range(dim
);
2144 next_element
= next(dim
, isl_space_dim(dim
, isl_dim_set
) - 1);
2146 map
= isl_map_apply_domain(next_thread_x
, isl_map_copy(access_map
));
2147 map
= isl_map_apply_range(map
, access_map
);
2149 coalesced
= isl_map_is_subset(map
, next_element
);
2151 isl_map_free(next_element
);
2157 /* Given an access relation in terms of the first gen->shared_len + gen->n_block
2158 * dimensions of the computed schedule, check if it is bijective for
2159 * fixed values of the first gen->shared_len dimensions.
2160 * We perform this check by equating these dimensions to parameters.
2162 static int access_is_bijective(struct gpu_gen
*gen
, __isl_keep isl_map
*access
)
2168 access
= isl_map_copy(access
);
2169 space
= isl_space_params(isl_map_get_space(access
));
2170 par
= parametrization(space
, gen
->shared_len
+ gen
->n_block
,
2171 0, gen
->shared_len
, "s");
2172 access
= isl_map_intersect_domain(access
, par
);
2173 res
= isl_map_is_bijective(access
);
2174 isl_map_free(access
);
2179 /* Look for the last shared tile loop that affects the offset of "tile"
2180 * and return the result.
2181 * If there is no such loop, then return the index of the loop
2182 * before the first shared tile loop, in particular gen->tile_first - 1.
2184 static int compute_tile_last_shared(struct gpu_gen
*gen
,
2185 struct gpu_array_tile
*tile
)
2189 for (j
= gen
->shared_len
- 1; j
>= gen
->tile_first
; --j
) {
2190 for (i
= 0; i
< tile
->n
; ++i
) {
2194 lb
= tile
->bound
[i
].lb
;
2195 if (isl_aff_involves_dims(lb
, isl_dim_in
, j
, 1))
2198 shift
= tile
->bound
[i
].shift
;
2201 if (isl_aff_involves_dims(shift
, isl_dim_in
, j
, 1))
2211 /* Look for the last shared tile loop that affects the offset of the
2212 * shared or private tile and store the result in group->last_shared.
2213 * If there is no such loop, then group->last_shared is set to a value
2214 * before the first shared tile loop, in particular gen->tile_first - 1.
2215 * If there is no tile defined on the array reference group,
2216 * then set group->last_shared to gen->shared_len - 1.
2218 static void set_last_shared(struct gpu_gen
*gen
,
2219 struct gpu_array_ref_group
*group
)
2221 struct gpu_array_tile
*tile
;
2223 group
->last_shared
= gen
->shared_len
- 1;
2225 tile
= group
->private_tile
;
2227 tile
= group
->shared_tile
;
2231 group
->last_shared
= compute_tile_last_shared(gen
, tile
);
2234 /* Compute a privatized copy of all access relations from reference groups that
2235 * are mapped to private memory and store the result in gen->privatization.
2237 static void compute_private_access(struct gpu_gen
*gen
)
2240 isl_union_map
*private;
2242 if (!gen
->options
->use_private_memory
)
2245 private = isl_union_map_empty(isl_union_map_get_space(gen
->shared_sched
));
2247 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2248 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2250 if (gpu_array_is_read_only_scalar(array
))
2253 for (j
= 0; j
< array
->n_group
; ++j
) {
2254 if (!array
->groups
[j
]->private_tile
)
2257 private = isl_union_map_union(private,
2258 group_access_relation(array
->groups
[j
], 1, 1));
2262 if (isl_union_map_is_empty(private))
2263 isl_union_map_free(private);
2265 isl_union_map
*priv
;
2267 private = isl_union_map_apply_domain(private,
2268 isl_union_map_copy(gen
->shared_sched
));
2269 priv
= isl_union_map_from_map(isl_map_copy(gen
->privatization
));
2270 private = isl_union_map_apply_domain(private, priv
);
2271 gen
->private_access
= private;
2275 /* Compute the size of the tile specified by "tile"
2276 * in number of elements and put the result in *size.
2278 static void tile_size(struct gpu_array_tile
*tile
, isl_int
*size
)
2282 isl_int_set_si(*size
, 1);
2284 for (i
= 0; i
< tile
->n
; ++i
)
2285 isl_int_mul(*size
, *size
, tile
->bound
[i
].size
);
2288 /* If max_shared_memory is not set to infinity (-1), then make
2289 * sure that the total amount of shared memory required by the
2290 * array reference groups mapped to shared memory is no larger
2291 * than this maximum.
2293 * We apply a greedy approach and discard (keep in global memory)
2294 * those groups that would result in a total memory size that
2295 * is larger than the maximum.
2297 static void check_shared_memory_bound(struct gpu_gen
*gen
)
2302 if (gen
->options
->max_shared_memory
< 0)
2307 isl_int_set_si(left
, gen
->options
->max_shared_memory
);
2309 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2310 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2312 for (j
= 0; j
< array
->n_group
; ++j
) {
2313 struct gpu_array_ref_group
*group
;
2315 group
= array
->groups
[j
];
2316 if (!group
->shared_tile
)
2319 tile_size(group
->shared_tile
, &size
);
2320 isl_int_mul_ui(size
, size
, array
->size
);
2322 if (isl_int_le(size
, left
)) {
2323 isl_int_sub(left
, left
, size
);
2327 group
->shared_tile
= free_tile(group
->shared_tile
);
2331 isl_int_clear(size
);
2332 isl_int_clear(left
);
2335 /* Fill up the groups array with singleton groups, i.e., one group
2336 * per reference, initializing the array, access, write, n_ref and refs fields.
2337 * In particular the access field is initialized to the scheduled
2338 * access relation of the array reference.
2340 * Return the number of elements initialized, i.e., the number of
2341 * active references in the current kernel.
2343 static int populate_array_references(struct gpu_array_info
*array
,
2344 __isl_keep isl_union_map
*sched
, struct gpu_array_ref_group
**groups
)
2348 isl_ctx
*ctx
= isl_union_map_get_ctx(sched
);
2351 for (i
= 0; i
< array
->n_ref
; ++i
) {
2352 isl_union_map
*umap
;
2354 struct gpu_array_ref_group
*group
;
2355 struct gpu_stmt_access
*access
= array
->refs
[i
];
2357 map
= isl_map_copy(access
->access
);
2358 umap
= isl_union_map_from_map(map
);
2359 umap
= isl_union_map_apply_domain(umap
,
2360 isl_union_map_copy(sched
));
2362 if (isl_union_map_is_empty(umap
)) {
2363 isl_union_map_free(umap
);
2367 map
= isl_map_from_union_map(umap
);
2368 map
= isl_map_detect_equalities(map
);
2370 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
2372 group
->array
= array
;
2373 group
->access
= map
;
2374 group
->write
= access
->write
;
2375 group
->refs
= &array
->refs
[i
];
2378 groups
[n
++] = group
;
2384 /* If group->n_ref == 1, then group->refs was set by
2385 * populate_array_references to point directly into
2386 * group->array->refs and should not be freed.
2387 * If group->n_ref > 1, then group->refs was set by join_groups
2388 * to point to a newly allocated array.
2390 static void free_array_ref_group(struct gpu_array_ref_group
*group
)
2394 free_tile(group
->shared_tile
);
2395 free_tile(group
->private_tile
);
2396 isl_map_free(group
->access
);
2397 if (group
->n_ref
> 1)
2402 /* Given a map where the input dimensions represent the tile loops,
2403 * eliminate the innermost of those that have a fixed value
2404 * until we reach one that does not (obviously) have a fixed value.
2406 static __isl_give isl_map
*eliminate_fixed_inner_loops(
2407 __isl_take isl_map
*access
)
2411 n
= isl_map_dim(access
, isl_dim_in
);
2413 for (i
= n
- 1; i
>= 0; --i
) {
2414 if (!isl_map_plain_is_fixed(access
, isl_dim_in
, i
, NULL
))
2416 access
= isl_map_eliminate(access
, isl_dim_in
, i
, 1);
2421 /* Check if the access relations of group1 and group2 overlap within
2422 * the innermost loop. In particular, ignore any inner dimension
2423 * with a fixed value.
2424 * The copying to and from shared memory will be performed within
2425 * the innermost actual loop so we are only allowed to consider
2426 * the dimensions up to that innermost loop while checking whether
2427 * two access relations overlap.
2429 static int accesses_overlap(struct gpu_array_ref_group
*group1
,
2430 struct gpu_array_ref_group
*group2
)
2433 isl_map
*access1
, *access2
;
2435 access1
= isl_map_copy(group1
->access
);
2436 access1
= eliminate_fixed_inner_loops(access1
);
2437 access2
= isl_map_copy(group2
->access
);
2438 access2
= eliminate_fixed_inner_loops(access2
);
2439 access1
= isl_map_intersect(access1
, access2
);
2440 empty
= isl_map_is_empty(access1
);
2441 isl_map_free(access1
);
2446 /* Combine the given two groups into a single group, containing
2447 * the references of both groups.
2449 static struct gpu_array_ref_group
*join_groups(
2450 struct gpu_array_ref_group
*group1
,
2451 struct gpu_array_ref_group
*group2
)
2455 struct gpu_array_ref_group
*group
;
2457 ctx
= isl_map_get_ctx(group1
->access
);
2458 group
= isl_calloc_type(ctx
, struct gpu_array_ref_group
);
2460 group
->array
= group1
->array
;
2461 group
->access
= isl_map_union(isl_map_copy(group1
->access
),
2462 isl_map_copy(group2
->access
));
2463 group
->write
= group1
->write
|| group2
->write
;
2464 group
->n_ref
= group1
->n_ref
+ group2
->n_ref
;
2465 group
->refs
= isl_alloc_array(ctx
, struct gpu_stmt_access
*,
2467 assert(group
->refs
);
2468 for (i
= 0; i
< group1
->n_ref
; ++i
)
2469 group
->refs
[i
] = group1
->refs
[i
];
2470 for (i
= 0; i
< group2
->n_ref
; ++i
)
2471 group
->refs
[group1
->n_ref
+ i
] = group2
->refs
[i
];
2476 /* Combine the given two groups into a single group and free
2477 * the original two groups.
2479 static struct gpu_array_ref_group
*join_groups_and_free(
2480 struct gpu_array_ref_group
*group1
,
2481 struct gpu_array_ref_group
*group2
)
2483 struct gpu_array_ref_group
*group
;
2485 group
= join_groups(group1
, group2
);
2486 free_array_ref_group(group1
);
2487 free_array_ref_group(group2
);
2491 /* Compute the private and/or shared memory tiles for the array
2492 * reference group "group" of array "array".
2494 * If the array is a read-only scalar or if the user requested
2495 * not to use shared or private memory, then we do not need to do anything.
2497 * We only try to compute a shared memory tile if there is any reuse
2498 * or if the access is not coalesced.
2500 * For computing a private memory tile, we also require that there is
2501 * some reuse. Moreover, we require that the access is private
2502 * to the thread. That is, we check that any given array element
2503 * is only accessed by a single thread.
2504 * We compute an access relation that maps the shared tile loop iterators
2505 * and the shared point loop iterators that will be wrapped over the
2506 * threads to the array elements.
2507 * We actually check that those iterators that will be wrapped
2508 * partition the array space. This check is stricter than necessary
2509 * since several iterations may be mapped onto the same thread
2510 * and then they could be allowed to access the same memory elements,
2511 * but our check does not allow this situation.
2513 * We also check that the index expression only depends on parallel
2514 * loops. That way, we can move those loops innermost and unroll them.
2515 * Again, we use a test that is stricter than necessary.
2516 * We actually check whether the index expression only depends
2517 * on the iterators that are wrapped over the threads.
2518 * These are necessarily parallel, but there may be more parallel loops.
2520 * Combining the injectivity of the first test with the single-valuedness
2521 * of the second test, we simply test for bijectivity.
2523 * If it turns out we can use registers, we compute the private memory
2524 * tile size using can_tile, after introducing a dependence
2525 * on the thread indices.
2527 static void compute_group_bounds_core(struct gpu_gen
*gen
,
2528 struct gpu_array_ref_group
*group
)
2530 isl_ctx
*ctx
= isl_space_get_ctx(group
->array
->dim
);
2531 isl_union_map
*access
;
2532 int n_index
= group
->array
->n_index
;
2535 int use_shared
= gen
->options
->use_shared_memory
;
2536 int use_private
= gen
->options
->use_private_memory
;
2538 if (!use_shared
&& !use_private
)
2540 if (gpu_array_is_read_only_scalar(group
->array
))
2543 access
= group_access_relation(group
, 1, 1);
2544 no_reuse
= isl_union_map_is_injective(access
);
2546 if (use_shared
&& (!no_reuse
|| !access_is_coalesced(gen
, access
))) {
2547 group
->shared_tile
= create_tile(ctx
, group
->array
->n_index
);
2548 if (!can_tile(group
->access
, group
->shared_tile
))
2549 group
->shared_tile
= free_tile(group
->shared_tile
);
2552 if (!use_private
|| no_reuse
) {
2553 isl_union_map_free(access
);
2557 access
= isl_union_map_apply_domain(access
,
2558 isl_union_map_copy(gen
->shared_sched
));
2560 acc
= isl_map_from_union_map(access
);
2562 if (!access_is_bijective(gen
, acc
)) {
2567 group
->private_tile
= create_tile(gen
->ctx
, n_index
);
2568 acc
= isl_map_apply_domain(acc
, isl_map_copy(gen
->privatization
));
2569 if (!can_tile(acc
, group
->private_tile
))
2570 group
->private_tile
= free_tile(group
->private_tile
);
2575 /* Compute the private and/or shared memory tiles for the array
2576 * reference group "group" of array "array" and set last_shared.
2578 static void compute_group_bounds(struct gpu_gen
*gen
,
2579 struct gpu_array_ref_group
*group
)
2581 compute_group_bounds_core(gen
, group
);
2582 set_last_shared(gen
, group
);
2585 /* If two groups have overlapping access relations (as determined by
2586 * the "overlap" function) and if one of them involves a write,
2587 * then merge the two groups into one.
2588 * If "compute_bounds" is set, then call compute_group_bounds
2589 * on the merged groups.
2591 * Return the updated number of groups.
2593 static int group_writes(struct gpu_gen
*gen
,
2594 int n
, struct gpu_array_ref_group
**groups
,
2595 int (*overlap
)(struct gpu_array_ref_group
*group1
,
2596 struct gpu_array_ref_group
*group2
), int compute_bounds
)
2600 for (i
= 0; i
< n
; ++i
) {
2601 for (j
= n
- 1; j
> i
; --j
) {
2602 if (!groups
[i
]->write
&& !groups
[j
]->write
)
2605 if (!overlap(groups
[i
], groups
[j
]))
2608 groups
[i
] = join_groups_and_free(groups
[i
], groups
[j
]);
2610 compute_group_bounds(gen
, groups
[i
]);
2612 groups
[j
] = groups
[n
- 1];
2620 /* If two groups have overlapping access relations (within the innermost
2621 * loop) and if one of them involves a write, then merge the two groups
2624 * Return the updated number of groups.
2626 static int group_overlapping_writes(struct gpu_gen
*gen
,
2627 int n
, struct gpu_array_ref_group
**groups
)
2629 return group_writes(gen
, n
, groups
, &accesses_overlap
, 0);
2632 /* Check if the access relations of group1 and group2 overlap within
2633 * the outermost min(group1->last_shared, group2->last_shared) loops.
2635 static int last_shared_accesses_overlap(struct gpu_array_ref_group
*group1
,
2636 struct gpu_array_ref_group
*group2
)
2641 isl_map
*map_i
, *map_j
, *map
;
2643 last_shared
= group1
->last_shared
;
2644 if (group2
->last_shared
< last_shared
)
2645 last_shared
= group2
->last_shared
;
2646 map_i
= isl_map_copy(group1
->access
);
2647 dim
= isl_map_dim(map_i
, isl_dim_in
);
2648 map_i
= isl_map_eliminate(map_i
, isl_dim_in
,
2649 last_shared
+ 1, dim
- (last_shared
+ 1));
2650 map_j
= isl_map_copy(group2
->access
);
2651 map_j
= isl_map_eliminate(map_j
, isl_dim_in
,
2652 last_shared
+ 1, dim
- (last_shared
+ 1));
2653 map
= isl_map_intersect(map_i
, map_j
);
2654 empty
= isl_map_is_empty(map
);
2660 /* If two groups have overlapping access relations (within the outer
2661 * last_shared loops) and if one of them involves a write,
2662 * then merge the two groups into one.
2664 * Return the updated number of groups.
2666 static int group_last_shared_overlapping_writes(struct gpu_gen
*gen
, int n
,
2667 struct gpu_array_ref_group
**groups
)
2669 return group_writes(gen
, n
, groups
, &last_shared_accesses_overlap
, 1);
2672 /* Is the size of the tile specified by "tile" smaller than the sum of
2673 * the sizes of the tiles specified by "tile1" and "tile2"?
2675 static int smaller_tile(struct gpu_array_tile
*tile
,
2676 struct gpu_array_tile
*tile1
, struct gpu_array_tile
*tile2
)
2679 isl_int size
, size1
, size2
;
2682 isl_int_init(size1
);
2683 isl_int_init(size2
);
2685 tile_size(tile
, &size
);
2686 tile_size(tile1
, &size1
);
2687 tile_size(tile2
, &size2
);
2689 isl_int_sub(size
, size
, size1
);
2690 isl_int_sub(size
, size
, size2
);
2691 smaller
= isl_int_is_neg(size
);
2693 isl_int_clear(size2
);
2694 isl_int_clear(size1
);
2695 isl_int_clear(size
);
2700 /* Given an initial grouping of array references and shared memory tiles
2701 * for each group that allows for a shared memory tile, merge two groups
2702 * if both have a shared memory tile, the merged group also has
2703 * a shared memory tile and the size of the tile for the merge group
2704 * is smaller than the sum of the tile sizes of the individual groups.
2706 * If merging two groups decreases the "last_shared" dimension of
2707 * one or both of the two groups, then we need to check for overlapping
2710 * Return the number of groups after merging.
2712 static int group_common_shared_memory_tile(struct gpu_gen
*gen
,
2713 struct gpu_array_info
*array
, int n
,
2714 struct gpu_array_ref_group
**groups
)
2717 int recompute_overlap
= 0;
2718 isl_ctx
*ctx
= isl_space_get_ctx(array
->dim
);
2720 for (i
= 0; i
< n
; ++i
) {
2721 if (!groups
[i
]->shared_tile
)
2723 for (j
= n
- 1; j
> i
; --j
) {
2726 struct gpu_array_ref_group
*group
;
2728 if (!groups
[j
]->shared_tile
)
2731 map
= isl_map_intersect(isl_map_copy(groups
[i
]->access
),
2732 isl_map_copy(groups
[j
]->access
));
2733 empty
= isl_map_is_empty(map
);
2739 group
= join_groups(groups
[i
], groups
[j
]);
2740 compute_group_bounds(gen
, group
);
2741 if (!group
->shared_tile
||
2742 !smaller_tile(group
->shared_tile
,
2743 groups
[i
]->shared_tile
,
2744 groups
[j
]->shared_tile
)) {
2745 free_array_ref_group(group
);
2749 if (group
->last_shared
< groups
[i
]->last_shared
||
2750 group
->last_shared
< groups
[j
]->last_shared
)
2751 recompute_overlap
= 1;
2752 free_array_ref_group(groups
[i
]);
2753 free_array_ref_group(groups
[j
]);
2756 groups
[j
] = groups
[n
- 1];
2761 if (recompute_overlap
)
2762 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
2766 /* Set array->n_group and array->groups to n and groups.
2768 * Additionally, set the "nr" field of each group
2769 * and the "group" field of each reference in each group.
2771 static void set_array_groups(struct gpu_array_info
*array
,
2772 int n
, struct gpu_array_ref_group
**groups
)
2777 array
->groups
= groups
;
2779 for (i
= 0; i
< n
; ++i
) {
2782 for (j
= 0; j
< groups
[i
]->n_ref
; ++j
)
2783 groups
[i
]->refs
[j
]->group
= i
;
2787 /* Group array references that should be considered together when
2788 * deciding whether to access them from private, shared or global memory.
2790 * In particular, if two array references overlap and if one of them
2791 * is a write, then the two references are grouped together.
2792 * We first perform an initial grouping based only on the access relation.
2793 * After computing shared and private memory tiles, we check for
2794 * overlapping writes again, but this time taking into account
2795 * the "last_shared" property.
2797 * Furthermore, if two groups admit a shared memory tile and if the
2798 * combination of the two also admits a shared memory tile, we merge
2801 static void group_array_references(struct gpu_gen
*gen
,
2802 struct gpu_array_info
*array
, __isl_keep isl_union_map
*sched
)
2806 isl_ctx
*ctx
= isl_union_map_get_ctx(sched
);
2807 struct gpu_array_ref_group
**groups
;
2809 groups
= isl_calloc_array(ctx
, struct gpu_array_ref_group
*,
2813 n
= populate_array_references(array
, sched
, groups
);
2815 n
= group_overlapping_writes(gen
, n
, groups
);
2817 for (i
= 0; i
< n
; ++i
)
2818 compute_group_bounds(gen
, groups
[i
]);
2820 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
2822 n
= group_common_shared_memory_tile(gen
, array
, n
, groups
);
2824 set_array_groups(array
, n
, groups
);
2827 /* Take tiled_sched, project it onto the shared tile loops and
2828 * the loops that will be wrapped over the threads and
2829 * store the result in gen->shared_sched.
2830 * Also compute a projection that projects out the loops that will be
2831 * wrapped over the threads and store this projection in gen->shared_proj.
2833 static void compute_shared_sched(struct gpu_gen
*gen
)
2838 isl_union_map
*sched
;
2840 sched
= isl_union_map_copy(gen
->tiled_sched
);
2842 dim
= isl_union_map_get_space(sched
);
2843 proj
= projection(dim
, gen
->tiled_len
, gen
->shared_len
+ gen
->n_block
);
2844 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
2846 dim
= isl_union_map_get_space(sched
);
2847 proj
= projection(dim
, gen
->shared_len
+ gen
->n_block
, gen
->shared_len
);
2849 gen
->shared_sched
= sched
;
2850 gen
->shared_proj
= isl_union_map_from_map(proj
);
2853 /* Group references of all arrays in the program.
2855 static void group_references(struct gpu_gen
*gen
)
2858 isl_union_map
*sched
;
2860 sched
= isl_union_map_apply_range(isl_union_map_copy(gen
->shared_sched
),
2861 isl_union_map_copy(gen
->shared_proj
));
2863 for (i
= 0; i
< gen
->prog
->n_array
; ++i
)
2864 group_array_references(gen
, &gen
->prog
->array
[i
], sched
);
2866 isl_union_map_free(sched
);
2869 /* Free all array information that is local to the current kernel.
2871 static void free_local_array_info(struct gpu_gen
*gen
)
2875 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2876 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2878 for (j
= 0; j
< array
->n_group
; ++j
)
2879 free_array_ref_group(array
->groups
[j
]);
2880 free(array
->groups
);
2884 /* Compute the effective grid size as a list of the sizes in each dimension.
2886 * The grid size specified by the user or set by default
2887 * in read_grid_sizes() and applied in tile_schedule(),
2888 * may be too large for the given code in the sense that
2889 * it may contain blocks that don't need to execute anything.
2890 * We therefore don't return this grid size, but instead the
2891 * smallest grid size that ensures that all blocks that actually
2892 * execute code are included in the grid.
2894 * We first extract a description of the grid, i.e., the possible values
2895 * of the block ids, from gen->tiled_sched.
2896 * The block ids are parameters in gen->tiled_sched.
2897 * We simply need to change them into set dimensions.
2899 * Then, for each block dimension, we compute the maximal value of the block id
2902 static __isl_give isl_multi_pw_aff
*extract_grid_size(struct gpu_gen
*gen
,
2903 struct ppcg_kernel
*kernel
)
2907 isl_multi_pw_aff
*mpa
;
2909 grid
= isl_union_map_params(isl_union_map_copy(gen
->tiled_sched
));
2910 grid
= isl_set_from_params(grid
);
2911 grid
= isl_set_add_dims(grid
, isl_dim_set
, gen
->n_grid
);
2912 for (i
= 0; i
< gen
->n_grid
; ++i
) {
2916 snprintf(name
, sizeof(name
), "b%d", i
);
2917 pos
= isl_set_find_dim_by_name(grid
, isl_dim_param
, name
);
2919 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
2920 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
2923 mpa
= isl_multi_pw_aff_zero(isl_set_get_space(grid
));
2924 for (i
= 0; i
< gen
->n_grid
; ++i
) {
2929 bound
= isl_set_dim_max(isl_set_copy(grid
), i
);
2930 bound
= isl_pw_aff_coalesce(bound
);
2931 bound
= isl_pw_aff_gist(bound
, isl_set_copy(kernel
->context
));
2933 space
= isl_pw_aff_get_domain_space(bound
);
2934 one
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2935 one
= isl_aff_add_constant_si(one
, 1);
2936 bound
= isl_pw_aff_add(bound
, isl_pw_aff_from_aff(one
));
2937 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, bound
);
2944 void ppcg_kernel_free(void *user
)
2946 struct ppcg_kernel
*kernel
= user
;
2952 isl_multi_pw_aff_free(kernel
->grid_size
);
2953 isl_set_free(kernel
->context
);
2954 isl_union_set_free(kernel
->arrays
);
2955 isl_space_free(kernel
->space
);
2956 isl_ast_node_free(kernel
->tree
);
2958 for (i
= 0; i
< kernel
->n_array
; ++i
)
2959 isl_pw_aff_list_free(kernel
->array
[i
].bound
);
2960 free(kernel
->array
);
2962 for (i
= 0; i
< kernel
->n_var
; ++i
) {
2963 free(kernel
->var
[i
].name
);
2964 isl_vec_free(kernel
->var
[i
].size
);
2971 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
2972 struct ppcg_kernel_var
*var
)
2975 struct gpu_array_tile
*tile
;
2979 var
->array
= group
->array
;
2981 tile
= group
->private_tile
;
2982 var
->type
= ppcg_access_private
;
2984 tile
= group
->shared_tile
;
2985 var
->type
= ppcg_access_shared
;
2988 p
= isl_printer_to_str(ctx
);
2989 p
= print_array_name(p
, group
);
2990 var
->name
= isl_printer_get_str(p
);
2991 isl_printer_free(p
);
2993 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
2995 for (j
= 0; j
< group
->array
->n_index
; ++j
)
2996 var
->size
= isl_vec_set_element(var
->size
, j
,
2997 tile
->bound
[j
].size
);
3000 static void create_kernel_vars(struct gpu_gen
*gen
, struct ppcg_kernel
*kernel
)
3005 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3006 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3008 for (j
= 0; j
< array
->n_group
; ++j
) {
3009 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3010 if (group
->private_tile
|| group
->shared_tile
)
3016 kernel
->var
= isl_calloc_array(gen
->ctx
, struct ppcg_kernel_var
, n
);
3017 assert(kernel
->var
);
3020 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3021 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3023 for (j
= 0; j
< array
->n_group
; ++j
) {
3024 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3025 if (!group
->private_tile
&& !group
->shared_tile
)
3027 create_kernel_var(gen
->ctx
, group
, &kernel
->var
[n
]);
3033 /* The sizes of the arrays on the host that have been computed by
3034 * extract_array_info may depend on the parameters. Use the extra
3035 * constraints on the parameters that are valid at "host_domain"
3036 * to simplify these expressions and store the results in kernel->array.
3038 static void localize_bounds(struct gpu_gen
*gen
, struct ppcg_kernel
*kernel
,
3039 __isl_keep isl_set
*host_domain
)
3044 kernel
->array
= isl_calloc_array(gen
->ctx
,
3045 struct gpu_local_array_info
, gen
->prog
->n_array
);
3046 assert(kernel
->array
);
3047 kernel
->n_array
= gen
->prog
->n_array
;
3049 context
= isl_set_copy(host_domain
);
3050 context
= isl_set_params(context
);
3052 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3053 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3054 isl_pw_aff_list
*local
;
3056 if (array
->n_group
== 0)
3059 local
= isl_pw_aff_list_alloc(gen
->ctx
, array
->n_index
);
3061 for (j
= 0; j
< array
->n_index
; ++j
) {
3064 pwaff
= isl_pw_aff_copy(array
->bound
[j
]);
3065 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
3066 local
= isl_pw_aff_list_add(local
, pwaff
);
3069 kernel
->array
[i
].bound
= local
;
3071 isl_set_free(context
);
3074 /* Find the element in gen->stmt that has the given "id".
3075 * Return NULL if no such gpu_stmt can be found.
3077 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
3081 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
3082 if (id
== prog
->stmts
[i
].id
)
3086 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
3089 /* Set gen->tile_len and gen->n_parallel to those of the statement
3090 * affected by the first map (part of the schedule)
3091 * on which this function is called.
3092 * Because of the way the schedule is constructed, the other statements
3093 * in the list, if any, should have the same values for these properties.
3095 static int extract_tile_len(__isl_take isl_map
*map
, void *user
)
3097 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3099 struct gpu_stmt
*stmt
;
3101 id
= isl_map_get_tuple_id(map
, isl_dim_in
);
3102 stmt
= find_stmt(gen
->prog
, id
);
3108 isl_die(gen
->ctx
, isl_error_unknown
,
3109 "statement not found", return -1);
3111 gen
->tile_len
= stmt
->tile_len
;
3112 gen
->n_parallel
= stmt
->n_parallel
;
3117 void ppcg_kernel_stmt_free(void *user
)
3120 struct ppcg_kernel_stmt
*stmt
= user
;
3125 switch (stmt
->type
) {
3126 case ppcg_kernel_copy
:
3127 isl_ast_expr_free(stmt
->u
.c
.index
);
3128 isl_ast_expr_free(stmt
->u
.c
.local_index
);
3130 case ppcg_kernel_domain
:
3131 for (i
= 0; i
< stmt
->u
.d
.n_access
; ++i
) {
3132 isl_ast_expr_list_free(stmt
->u
.d
.access
[i
].index
);
3133 free(stmt
->u
.d
.access
[i
].local_name
);
3135 free(stmt
->u
.d
.access
);
3137 case ppcg_kernel_sync
:
3144 /* Set the options of "context" to
3146 * { space -> [x] : x >= first }
3148 static __isl_give isl_ast_build
*set_unroll(
3149 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
,
3156 ctx
= isl_ast_build_get_ctx(build
);
3158 space
= isl_space_from_domain(space
);
3159 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3160 space
= isl_space_set_tuple_name(space
, isl_dim_out
, "unroll");
3161 unroll
= isl_map_universe(space
);
3162 unroll
= isl_map_lower_bound_si(unroll
, isl_dim_out
, 0, first
);
3163 opt
= isl_union_map_from_map(unroll
);
3165 build
= isl_ast_build_set_options(build
, opt
);
3170 /* Return a list of isl_ids of the form "prefix%d".
3172 static __isl_give isl_id_list
*generate_names(isl_ctx
*ctx
,
3173 int n
, const char *prefix
)
3179 names
= isl_id_list_alloc(ctx
, n
);
3180 for (i
= 0; i
< n
; ++i
) {
3183 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
3184 id
= isl_id_alloc(ctx
, name
, NULL
);
3185 names
= isl_id_list_add(names
, id
);
3191 /* Extend the schedule "schedule" with the part of "extension"
3192 * starting at "first" up to "len".
3194 static __isl_give isl_union_map
*extend_schedule(
3195 __isl_take isl_union_map
*schedule
,
3196 __isl_take isl_union_map
*extension
, int first
, int len
)
3200 isl_union_map
*umap
;
3203 space
= isl_union_map_get_space(schedule
);
3204 space
= isl_space_set_from_params(space
);
3205 space
= isl_space_add_dims(space
, isl_dim_set
, len
);
3206 proj
= isl_set_identity(isl_set_universe(space
));
3207 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, first
);
3208 extension
= isl_union_map_apply_range(extension
,
3209 isl_union_map_from_map(proj
));
3211 schedule
= isl_union_map_range_product(schedule
, extension
);
3216 /* This function is called for each access to an array in each instance
3217 * in the kernel of some statement in the original code.
3218 * Replace that access by an access to global, shared or private memory
3219 * and store the results in *kernel_access.
3221 * Since the array in shared or private memory is just
3222 * a shifted copy of part of the original array, we simply need
3223 * to subtract the lower bound, which was computed in can_tile.
3224 * If any of the indices is strided, then we first add
3225 * shared_tile->bound[i].shift and divide by shared_tile->bound[i].stride.
3227 * If the given array is accessed directly from global memory,
3228 * we don't need to perform any shifting and simply simplify
3229 * the expression in the context of the domain instead.
3231 * If the array space (range of access) has no name, then we are
3232 * accessing an iterator in the original program.
3234 * The input stmt_access->access relation maps the iteration domain
3235 * of the current statement to an array element.
3236 * The first step is to reformulate
3237 * this access relation in terms of the loop iterators of the generated
3238 * code through precomposition with gen->stmt_it.
3240 * The expressions in "tile" are formulated in terms of the first
3241 * gen->shared_len dimensions of the computed schedule using the mapping
3242 * sched2shared which maps the loop iterators to these dimensions.
3244 static void compute_index_expression(struct gpu_gen
*gen
,
3245 struct ppcg_kernel_access
*kernel_access
,
3246 struct gpu_stmt_access
*stmt_access
, __isl_keep isl_map
*stmt_it
,
3247 __isl_keep isl_map
*sched2shared
, __isl_keep isl_ast_build
*build
)
3250 isl_pw_multi_aff
*pma
;
3253 struct gpu_array_tile
*tile
= NULL
;
3255 if (isl_map_has_tuple_name(stmt_access
->access
, isl_dim_out
)) {
3258 struct gpu_array_ref_group
*group
;
3261 name
= isl_map_get_tuple_name(stmt_access
->access
, isl_dim_out
);
3263 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3264 if (strcmp(name
, gen
->prog
->array
[i
].name
))
3266 kernel_access
->array
= &gen
->prog
->array
[i
];
3267 kernel_access
->local_array
= &gen
->kernel
->array
[i
];
3269 assert(kernel_access
->array
);
3270 group
= kernel_access
->array
->groups
[stmt_access
->group
];
3271 p
= isl_printer_to_str(gen
->ctx
);
3272 p
= print_array_name(p
, group
);
3273 kernel_access
->local_name
= isl_printer_get_str(p
);
3274 isl_printer_free(p
);
3275 tile
= group
->private_tile
;
3276 kernel_access
->type
= ppcg_access_private
;
3278 tile
= group
->shared_tile
;
3279 kernel_access
->type
= ppcg_access_shared
;
3283 kernel_access
->type
= ppcg_access_global
;
3285 n_index
= isl_map_dim(stmt_access
->access
, isl_dim_out
);
3286 kernel_access
->index
= isl_ast_expr_list_alloc(gen
->ctx
, n_index
);
3291 access
= isl_map_copy(stmt_access
->access
);
3292 access
= isl_map_apply_range(isl_map_copy(stmt_it
), access
);
3293 pma
= isl_pw_multi_aff_from_map(access
);
3294 pma
= isl_pw_multi_aff_coalesce(pma
);
3296 for (i
= 0; i
< n_index
; ++i
) {
3301 index
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
3303 if (!kernel_access
->array
) {
3305 domain
= isl_map_domain(isl_map_copy(stmt_it
));
3306 index
= isl_pw_aff_coalesce(index
);
3307 index
= isl_pw_aff_gist(index
, domain
);
3309 domain
= isl_map_domain(isl_map_copy(stmt_it
));
3310 index
= shift_index(index
, kernel_access
->array
,
3311 &tile
->bound
[i
], domain
,
3312 isl_map_copy(sched2shared
));
3315 expr
= isl_ast_build_expr_from_pw_aff(build
, index
);
3317 kernel_access
->index
= isl_ast_expr_list_add(
3318 kernel_access
->index
, expr
);
3321 isl_pw_multi_aff_free(pma
);
3324 /* This function is called for each instance of a user statement
3327 * We attach a struct ppcg_kernel_stmt to the "node", containing
3328 * local information about the accesses.
3329 * This information is computed from stmt_it, which expresses the domain
3330 * elements in terms of the generated loops, and sched2shared,
3331 * which expresses the first shared_len dimensions of the schedule
3332 * computed by PPCG in terms of the generated loops.
3334 static __isl_give isl_ast_node
*at_each_domain(__isl_take isl_ast_node
*node
,
3335 __isl_keep isl_ast_build
*build
, void *user
)
3337 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3338 struct ppcg_kernel_stmt
*stmt
;
3340 isl_map
*stmt_it
, *sched2shared
;
3341 isl_ast_expr
*expr
, *arg
;
3342 isl_union_map
*schedule
;
3344 struct gpu_stmt_access
*access
;
3346 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3348 return isl_ast_node_free(node
);
3350 expr
= isl_ast_node_user_get_expr(node
);
3351 arg
= isl_ast_expr_get_op_arg(expr
, 0);
3352 id
= isl_ast_expr_get_id(arg
);
3354 schedule
= isl_ast_build_get_schedule(build
);
3355 stmt_it
= isl_map_reverse(isl_map_from_union_map(schedule
));
3356 sched2shared
= compute_sched_to_shared(gen
, isl_map_copy(stmt_it
));
3358 stmt
->type
= ppcg_kernel_domain
;
3359 stmt
->u
.d
.stmt
= find_stmt(gen
->prog
, id
);
3360 if (!stmt
->u
.d
.stmt
)
3364 for (access
= stmt
->u
.d
.stmt
->accesses
; access
; access
= access
->next
)
3367 stmt
->u
.d
.access
= isl_calloc_array(gen
->ctx
,
3368 struct ppcg_kernel_access
, n
);
3369 if (!stmt
->u
.d
.access
)
3372 stmt
->u
.d
.n_access
= n
;
3374 access
= stmt
->u
.d
.stmt
->accesses
;
3375 for (i
= 0; i
< n
; ++i
, access
= access
->next
) {
3376 compute_index_expression(gen
, &stmt
->u
.d
.access
[i
], access
,
3377 stmt_it
, sched2shared
, build
);
3381 isl_map_free(stmt_it
);
3382 isl_map_free(sched2shared
);
3383 isl_ast_expr_free(arg
);
3384 isl_ast_expr_free(expr
);
3386 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3387 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3388 return isl_ast_node_set_annotation(node
, id
);
3391 isl_map_free(stmt_it
);
3392 ppcg_kernel_stmt_free(stmt
);
3393 isl_map_free(sched2shared
);
3394 return isl_ast_node_free(node
);
3397 /* This function is called when code has been generated for the shared
3398 * tile loops. The "schedule" refers only to the original statements.
3400 * We extend the schedule with that part of gen->local_sched that hasn't
3401 * been taken into account yet. This introduces parameters referring
3402 * to thread ids in the schedule, so we add them (with the appropriate
3403 * bounds to the context as well).
3404 * Finally, we set the appropriate unrolling options
3405 * if gen->first_unroll is set.
3407 static __isl_give isl_ast_node
*create_domain_leaf(
3408 __isl_take isl_union_map
*schedule
, __isl_take isl_ast_build
*build
,
3411 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3413 isl_union_map
*sched
;
3416 isl_id_list
*iterators
;
3419 schedule
= extend_schedule(schedule
,
3420 isl_union_map_copy(gen
->local_sched
),
3421 gen
->shared_len
, gen
->thread_tiled_len
);
3423 space
= isl_ast_build_get_schedule_space(build
);
3424 set
= isl_set_universe(space
);
3425 set
= add_bounded_parameters(set
, gen
->n_block
, gen
->block_dim
, "t");
3426 build
= isl_ast_build_restrict(build
, set
);
3428 n
= gen
->thread_tiled_len
- gen
->shared_len
;
3430 if (gen
->first_unroll
>= 0) {
3431 space
= isl_space_set_alloc(gen
->ctx
, 0, n
);
3432 build
= set_unroll(build
, space
, gen
->first_unroll
);
3434 iterators
= generate_names(gen
->ctx
, n
, "c");
3435 build
= isl_ast_build_set_iterators(build
, iterators
);
3436 build
= isl_ast_build_set_at_each_domain(build
, &at_each_domain
, gen
);
3437 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3438 isl_ast_build_free(build
);
3443 /* This function is called for each statement node in the AST of the code
3444 * for copying to or from shared/private memory.
3445 * Attach a pointer to a ppcg_kernel_stmt representing the copy
3446 * statement to the node.
3447 * The statement name is {read,write}_{shared,private}_<array>.
3449 * The schedule is of the form
3453 * where A refers to a piece of an array and T to the corresponding
3454 * shifted tile. We split this schedule into mappings L -> A and L -> T
3455 * and store the corresponding expressions in stmt->index and stmt->local_index,
3456 * where stmt points to the ppcg_kernel_stmt that is attached to the node.
3458 static __isl_give isl_ast_node
*attach_copy_stmt(__isl_take isl_ast_node
*node
,
3459 __isl_keep isl_ast_build
*build
, void *user
)
3461 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3462 struct ppcg_kernel_stmt
*stmt
;
3466 isl_map
*access
, *local_access
, *map
;
3467 isl_pw_multi_aff
*pma
;
3471 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3473 return isl_ast_node_free(node
);
3475 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
3476 name
= isl_map_get_tuple_name(access
, isl_dim_in
);
3477 stmt
->u
.c
.read
= !strncmp(name
, "read", 4);
3478 access
= isl_map_reverse(access
);
3479 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(access
)));
3480 local_access
= isl_map_copy(access
);
3482 map
= isl_map_domain_map(isl_map_universe(isl_space_copy(space
)));
3483 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
3484 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
3485 access
= isl_map_apply_range(access
, map
);
3486 pma
= isl_pw_multi_aff_from_map(access
);
3487 expr
= isl_ast_build_call_from_pw_multi_aff(build
, pma
);
3488 stmt
->u
.c
.index
= expr
;
3490 map
= isl_map_range_map(isl_map_universe(space
));
3491 id
= isl_map_get_tuple_id(local_access
, isl_dim_out
);
3492 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
3493 local_access
= isl_map_apply_range(local_access
, map
);
3494 pma
= isl_pw_multi_aff_from_map(local_access
);
3495 expr
= isl_ast_build_call_from_pw_multi_aff(build
, pma
);
3496 stmt
->u
.c
.local_index
= expr
;
3498 stmt
->u
.c
.array
= gen
->copy_group
->array
;
3499 array_index
= stmt
->u
.c
.array
- gen
->prog
->array
;
3500 stmt
->u
.c
.local_array
= &gen
->kernel
->array
[array_index
];
3501 stmt
->type
= ppcg_kernel_copy
;
3503 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3504 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3505 return isl_ast_node_set_annotation(node
, id
);
3508 /* Given a schedule of the form
3512 * (with S the first shared_len dimensions of the computed schedule,
3513 * A the array and L the schedule correponding to the generated loops),
3514 * indicating where the copying the array elements that need to be copied,
3515 * construct code for performing the copying.
3517 * "group" is the array reference group that is being copied
3518 * "type" is either "read" or "write"
3519 * private is set if copying needs to be performed to/from registers
3521 * We first construct a mapping to a shifted tile of the array,
3523 * [S -> A] -> T(S,A) (1)
3525 * If private is set, then we also use this mapping as a schedule
3526 * (which is already thread-specific and will be completely unrolled).
3527 * Otherwise, we wrap/tile the range over the threads.
3530 * [S -> A] -> T'(S,A)
3532 * Combined with the given schedule, we have
3534 * [S -> A] -> [L -> T'(S,A)] (2)
3536 * From the shifted tile mapping, we construct a mapping
3538 * [S -> A] -> [A -> T(S,A)]
3540 * and apply it to the schedule (2), obtaining
3542 * [A -> T(S(L),A)] -> [L -> T'(S(L),A)]
3544 * Note that we can project out S because it is uniquely defined by L.
3546 static __isl_give isl_ast_node
*copy_access(struct gpu_gen
*gen
,
3547 __isl_take isl_map
*sched
,
3548 const char *type
, struct gpu_array_ref_group
*group
,
3549 __isl_take isl_ast_build
*build
, int private)
3551 const char *array_name
;
3552 const char *mem
= private ? "private" : "shared";
3556 isl_map
*schedule
, *shift
, *map
;
3558 isl_id_list
*iterators
;
3561 shift
= isl_set_unwrap(isl_map_domain(isl_map_copy(sched
)));
3562 array_name
= isl_map_get_tuple_name(shift
, isl_dim_out
);
3563 shift
= shift_access(shift
, group
);
3565 schedule
= isl_map_copy(shift
);
3567 schedule
= tile_access_schedule(gen
, schedule
);
3569 n
= isl_map_dim(schedule
, isl_dim_out
);
3570 set
= isl_set_universe(isl_ast_build_get_schedule_space(build
));
3571 set
= add_bounded_parameters(set
, gen
->n_block
, gen
->block_dim
, "t");
3573 schedule
= isl_map_range_product(sched
, schedule
);
3576 name
= isl_alloc_array(gen
->ctx
, char,
3577 strlen(type
) + sizeof("_private_") + strlen(array_name
) + 20);
3578 if (group
->array
->n_group
> 1)
3579 sprintf(name
, "%s_%s_%s_%d", type
, mem
, array_name
, group
->nr
);
3581 sprintf(name
, "%s_%s_%s", type
, mem
, array_name
);
3582 shift
= isl_map_set_tuple_name(shift
,
3583 isl_dim_out
, name
+ strlen(type
) + 1);
3585 space
= isl_space_domain(isl_map_get_space(shift
));
3586 map
= isl_map_range_map(isl_map_universe(isl_space_unwrap(space
)));
3587 map
= isl_map_range_product(map
, shift
);
3589 schedule
= isl_map_apply_domain(schedule
, map
);
3591 schedule
= isl_map_set_tuple_name(schedule
, isl_dim_in
, name
);
3594 build
= isl_ast_build_restrict(build
, set
);
3596 gen
->copy_group
= group
;
3599 space
= isl_space_range(isl_map_get_space(schedule
));
3600 space
= isl_space_range(isl_space_unwrap(space
));
3601 build
= set_unroll(build
, space
, 0);
3603 iterators
= generate_names(gen
->ctx
, n
, "c");
3604 build
= isl_ast_build_set_iterators(build
, iterators
);
3605 build
= isl_ast_build_set_at_each_domain(build
, &attach_copy_stmt
, gen
);
3606 tree
= isl_ast_build_ast_from_schedule(build
,
3607 isl_union_map_from_map(schedule
));
3608 isl_ast_build_free(build
);
3613 /* Return code for reading into or writing from shared memory
3614 * the given array reference group.
3616 * If we are performing a read from global memory to shared memory and
3617 * if the array involved is not a scalar, then we copy
3618 * the entire tile to shared memory. This may result in some extra
3619 * elements getting copied, but it should lead to simpler code
3620 * (which means that fewer registers may be needed) and less divergence.
3622 * Otherwise, we only copy the elements that will be read or have been written
3626 * The input "sched" is of the form.
3630 * with S the first shared_len dimensions of the computed schedule,
3631 * A the array and L the schedule correponding to the generated loops.
3633 * We first drop "type",
3637 * If the above conditions are satisfied, we project out A,
3642 * and then introduce the group tile [S -> T], resulting in
3646 static __isl_give isl_ast_node
*copy_group_shared_accesses(
3647 struct gpu_gen
*gen
, struct gpu_array_ref_group
*group
,
3648 __isl_take isl_map
*sched
, __isl_take isl_ast_build
*build
)
3652 isl_union_map
*access
;
3654 type
= isl_map_get_tuple_name(sched
, isl_dim_in
);
3655 read
= !strcmp(type
, "read");
3657 sched
= isl_map_reset_tuple_id(sched
, isl_dim_in
);
3659 if (read
&& group
->array
->n_index
> 0) {
3663 space
= isl_space_domain(isl_map_get_space(sched
));
3664 space
= isl_space_unwrap(space
);
3665 map
= isl_map_domain_map(isl_map_universe(space
));
3666 sched
= isl_map_apply_domain(sched
, map
);
3668 map
= group_tile(group
);
3669 map
= isl_map_reverse(isl_map_domain_map(map
));
3670 sched
= isl_map_apply_domain(sched
, map
);
3673 return copy_access(gen
, sched
, type
, group
, build
, 0);
3676 /* Return code for reading into or writing from private memory
3677 * the given array reference group.
3679 * Let S be the first shared_len dimensions of the computed schedule,
3680 * D the iteration domains, A the array and L the schedule correponding
3681 * to the generated loops.
3682 * "sched" is of the form
3686 * where type is either "read" or "write".
3687 * We apply the privatization D -> S(t), with t the thread ids,
3688 * to the access relation D -> A to obtain the privatized access relation
3692 * We drop the type from "sched" and intersect with the privatized access
3693 * relation to obtain
3697 static __isl_give isl_ast_node
*copy_group_private_accesses(
3698 struct gpu_gen
*gen
, struct gpu_array_ref_group
*group
,
3699 __isl_take isl_map
*sched
, __isl_take isl_ast_build
*build
)
3703 isl_union_map
*priv
;
3704 isl_union_map
*access
;
3705 isl_map
*access_map
;
3707 type
= isl_map_get_tuple_name(sched
, isl_dim_in
);
3708 read
= !strcmp(type
, "read");
3710 priv
= isl_union_map_from_map(isl_map_copy(gen
->privatization
));
3711 priv
= isl_union_map_apply_range(isl_union_map_copy(gen
->shared_sched
),
3714 access
= group_access_relation(group
, read
, !read
);
3715 access
= isl_union_map_apply_domain(access
, priv
);
3716 access_map
= isl_map_from_union_map(access
);
3718 sched
= isl_map_reset_tuple_id(sched
, isl_dim_in
);
3719 sched
= isl_map_intersect_domain(sched
, isl_map_wrap(access_map
));
3721 return copy_access(gen
, sched
, type
, group
, build
, 1);
3724 /* Return code for reading into or writing from shared or private memory.
3726 * "schedule" is of the form
3730 * with S be the first shared_len dimensions of the computed schedule,
3731 * A the array and L the schedule correponding to the generated loops.
3732 * The array reference group is attached to "type".
3734 static __isl_give isl_ast_node
*create_access_leaf(
3735 struct gpu_gen
*gen
, __isl_take isl_map
*schedule
,
3736 __isl_take isl_ast_build
*build
)
3738 struct gpu_array_ref_group
*group
;
3741 id
= isl_map_get_tuple_id(schedule
, isl_dim_in
);
3742 group
= isl_id_get_user(id
);
3745 if (group
->private_tile
)
3746 return copy_group_private_accesses(gen
, group
, schedule
,
3749 return copy_group_shared_accesses(gen
, group
, schedule
,
3753 /* Create a domain node representing a synchronization.
3755 static __isl_give isl_ast_node
*create_sync_leaf(
3756 struct gpu_gen
*gen
, __isl_take isl_map
*schedule
,
3757 __isl_take isl_ast_build
*build
)
3759 struct ppcg_kernel_stmt
*stmt
;
3765 isl_map_free(schedule
);
3767 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3771 stmt
->type
= ppcg_kernel_sync
;
3773 space
= isl_ast_build_get_schedule_space(build
);
3774 space
= isl_space_from_domain(space
);
3775 space
= isl_space_set_tuple_name(space
, isl_dim_out
, "sync");
3776 expr
= isl_ast_build_call_from_pw_multi_aff(build
,
3777 isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
)));
3778 node
= isl_ast_node_alloc_user(expr
);
3779 isl_ast_build_free(build
);
3781 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3782 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3783 return isl_ast_node_set_annotation(node
, id
);
3786 /* This function is called during the code generation at the point
3787 * where the schedule domain element is completely determined by
3788 * the generated code. The input schedule contains the original
3789 * statements as well as synchronization and copy "statements".
3790 * The latter are scheduled at different points than any of the original
3791 * statements, so they will only arrive here in isolation.
3793 * If the current schedule only refers to a single statement,
3794 * we check if it is a copy or synchronization statement and
3795 * call the appropriate functions.
3796 * Otherwise, we assume we are dealing with the original statements
3797 * and we call create_domain_leaf.
3799 static __isl_give isl_ast_node
*create_kernel_leaf(
3800 __isl_take isl_ast_build
*build
, void *user
)
3802 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3804 isl_union_map
*schedule
;
3807 schedule
= isl_ast_build_get_schedule(build
);
3809 if (isl_union_map_n_map(schedule
) != 1)
3810 return create_domain_leaf(schedule
, build
, user
);
3812 map
= isl_map_from_union_map(schedule
);
3813 name
= isl_map_get_tuple_name(map
, isl_dim_in
);
3814 if (!strcmp(name
, "read") || !strcmp(name
, "write"))
3815 return create_access_leaf(gen
, map
, build
);
3816 if (!strcmp(name
, "sync"))
3817 return create_sync_leaf(gen
, map
, build
);
3819 return create_domain_leaf(isl_union_map_from_map(map
), build
, user
);
3822 /* Mark all odd schedule dimensions as "atomic" (when the even dimensions
3823 * have value 0) and all even schedule dimensions as "unroll".
3825 * That is, the options look as follows
3827 * { [0, b, 0, d, ..., 0] -> atomic[i] : exists a : i = 2 a + 1;
3828 * [a, b, c, d, ..., z] -> unroll[i] : exists a : i = 2 a }
3830 * The even positions are used to be able to schedule copying blocks
3831 * and synchronization before or after each level of the shared memory
3832 * tile loops and we want to make sure that code for these is generated
3833 * separately (within each level).
3835 static __isl_give isl_ast_build
*set_atomic_and_unroll(
3836 __isl_take isl_ast_build
*build
,
3837 __isl_take isl_space
*space
, int sched_len
)
3843 isl_local_space
*ls
;
3846 ctx
= isl_ast_build_get_ctx(build
);
3848 space
= isl_space_params(space
);
3849 space
= isl_space_add_dims(space
, isl_dim_set
, sched_len
);
3850 space
= isl_space_from_domain(space
);
3851 space
= isl_space_add_dims(space
, isl_dim_out
, 2);
3852 map
= isl_map_universe(isl_space_copy(space
));
3853 for (i
= 0; i
< sched_len
; i
+= 2)
3854 map
= isl_map_fix_si(map
, isl_dim_in
, i
, 0);
3855 ls
= isl_local_space_from_space(isl_map_get_space(map
));
3856 c
= isl_equality_alloc(ls
);
3857 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
3858 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
3859 c
= isl_constraint_set_constant_si(c
, 1);
3860 map
= isl_map_add_constraint(map
, c
);
3861 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
3862 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "atomic");
3863 opt
= isl_union_map_from_map(map
);
3865 map
= isl_map_universe(space
);
3866 ls
= isl_local_space_from_space(isl_map_get_space(map
));
3867 c
= isl_equality_alloc(ls
);
3868 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
3869 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
3870 map
= isl_map_add_constraint(map
, c
);
3871 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
3872 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "unroll");
3873 opt
= isl_union_map_add_map(opt
, map
);
3875 build
= isl_ast_build_set_options(build
, opt
);
3880 /* Return a map that maps a space of dimension gen->shared_len
3881 * to its last dimensions starting at gen->tile_first.
3882 * The range is of dimension
3884 * 2 * (gen->shared_len - gen->tile_first) + 1
3886 * The input dimensions are mapped to the odd dimensions in the output,
3887 * while the even dimensions (except 2*pos) are fixed to 0.
3888 * Output dimension 2*pos (if pos >= 0) is fixed to "val".
3889 * If pos >= 0, then only the pos first dimensions starting at gen->tile_first
3890 * are mapped to the output. The remaining input dimensions are projected
3891 * out and the corresponding output dimensions are fixed to 0.
3893 static __isl_give isl_map
*insert_even(struct gpu_gen
*gen
,
3894 __isl_take isl_space
*space
, int pos
, int val
)
3899 space
= isl_space_set_from_params(space
);
3900 space
= isl_space_add_dims(space
, isl_dim_set
, gen
->shared_len
);
3901 space
= isl_space_map_from_set(space
);
3902 proj
= isl_map_identity(space
);
3903 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, gen
->tile_first
);
3904 n
= gen
->shared_len
- gen
->tile_first
;
3905 for (i
= 0; i
<= n
; ++i
) {
3906 proj
= isl_map_insert_dims(proj
, isl_dim_out
, 2 * i
, 1);
3908 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, val
);
3910 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, 0);
3916 proj
= isl_map_eliminate(proj
, isl_dim_in
, gen
->tile_first
+ pos
,
3917 gen
->shared_len
- (gen
->tile_first
+ pos
));
3918 for (i
= pos
; i
< n
; ++i
)
3919 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
+ 1, 0);
3924 /* Given the AST context schedule "schedule" and the mapping from
3925 * domains to the shared tile loops "shared_sched", add a schedule
3926 * for a synchronization operation at position "val" of loop level "pos".
3928 * schedule is of the form
3932 * (with D the iteration domains and L the already generated loops),
3933 * while shared_sched is of the form
3937 * We combine them into
3943 * [s_0,...] -> [0,s_{tile_first},0,..., val, 0, 0, ... 0]
3945 * and use the result as a schedule for "sync".
3947 static __isl_give isl_union_map
*add_sync_schedule(struct gpu_gen
*gen
,
3948 __isl_take isl_union_map
*res
, __isl_keep isl_union_map
*schedule
,
3949 __isl_keep isl_union_map
*shared_sched
, int pos
, int val
)
3952 isl_map
*proj
, *map
;
3954 shared_sched
= isl_union_map_copy(shared_sched
);
3955 schedule
= isl_union_map_copy(schedule
);
3957 space
= isl_union_map_get_space(shared_sched
);
3958 schedule
= isl_union_map_apply_domain(shared_sched
, schedule
);
3959 map
= isl_map_from_union_map(schedule
);
3961 proj
= insert_even(gen
, space
, pos
, val
);
3962 map
= isl_map_apply_range(map
, proj
);
3963 map
= isl_map_from_range(isl_map_wrap(map
));
3964 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "sync");
3966 res
= isl_union_map_add_map(res
, map
);
3971 /* Given the AST context schedule "schedule" and the mapping from
3972 * domains to the shared tile loops "shared_sched", add a schedule
3973 * for copying an array reference group to/from shared/private memory.
3974 * "read" is set if data should be copied from global memory
3975 * to shared/private memory.
3976 * "k" represents the current group
3977 * "s" is the total number of groups
3979 * We schedule an operation before or after the innermost loop
3980 * of "shared_sched" that affects the tile of the array reference group.
3982 * schedule is of the form
3986 * (with D the iteration domains and L the already generated loops),
3987 * while shared_sched is of the form
3991 * We first compute the access relation for the reference group
3995 * and combine it with shared_sched into
3999 * If this results in an empty relation, no copying needs to be performed
4001 * Otherwise, we invert the relation and combine it with "schedule" into
4005 * The actual additional piece of the schedule is obtained from combining
4011 * [s_0,...] -> [0,s_{tile_first},0,..., val, 0, 0, ... 0]
4013 * The position of "val" corresponds to the innermost loop that affects
4014 * the tile and the value indicates where the copying is scheduled
4015 * with respect to the actual kernel code (at value 0).
4016 * Reads are schedule before the code, writes to global memory from
4017 * private memory are scheduled at values 1 to s, writes to global
4018 * memory from shared memory are scheduled at values s + 2 to 2 * s + 1.
4020 * If we are scheduling a read from global memory to shared memory,
4021 * we insert a synchronization before the kernel code (at the innermost
4023 * If we are scheduling a write to global memory, then we add
4024 * a synchronization after all writes (at value 2 *s + 2).
4025 * However, there is no need for a synchronization after the outermost loop.
4026 * A write to global memory from private memory at the innermost level
4027 * does not require a synchronization, because it is covered by
4028 * the synchronization after the kernel inserted by body_schedule.
4030 static __isl_give isl_union_map
*add_group_schedule(struct gpu_gen
*gen
,
4031 __isl_take isl_union_map
*res
, __isl_keep isl_union_map
*schedule
,
4032 __isl_keep isl_union_map
*shared_sched
,
4033 struct gpu_array_ref_group
*group
, int read
, int k
, int s
)
4038 isl_union_map
*access
;
4039 isl_map
*map
, *proj
, *access_map
;
4042 access
= group_access_relation(group
, read
, !read
);
4043 access
= isl_union_map_range_product(isl_union_map_copy(shared_sched
),
4046 if (isl_union_map_is_empty(access
)) {
4047 isl_union_map_free(access
);
4051 access
= isl_union_map_reverse(access
);
4052 access
= isl_union_map_apply_range(access
,
4053 isl_union_map_copy(schedule
));
4054 access_map
= isl_map_from_union_map(access
);
4056 space
= isl_space_copy(group
->array
->dim
);
4057 space
= isl_space_from_range(space
);
4058 space
= isl_space_add_dims(space
, isl_dim_in
, gen
->shared_len
);
4059 map
= isl_map_domain_map(isl_map_universe(space
));
4061 space
= isl_union_map_get_space(schedule
);
4062 pos
= group
->last_shared
+ 1 - gen
->tile_first
;
4066 else if (group
->private_tile
)
4069 val
= 1 + s
+ 1 + k
;
4070 proj
= insert_even(gen
, space
, pos
, val
);
4071 map
= isl_map_apply_range(map
, proj
);
4073 access_map
= isl_map_range_product(access_map
, map
);
4075 id
= isl_id_alloc(gen
->ctx
, read
? "read" : "write", group
);
4076 access_map
= isl_map_set_tuple_id(access_map
, isl_dim_in
, id
);
4078 res
= isl_union_map_add_map(res
, access_map
);
4080 n
= gen
->shared_len
- gen
->tile_first
;
4082 if (!group
->private_tile
)
4083 res
= add_sync_schedule(gen
, res
, schedule
,
4084 shared_sched
, n
, -1);
4088 if (pos
== n
&& group
->private_tile
)
4090 res
= add_sync_schedule(gen
, res
, schedule
, shared_sched
,
4097 /* Return a schedule for the shared tile loops based on the current
4098 * AST context schedule.
4100 * We create a "shared_sched" that maps the domains to the first
4101 * shared_len dimensions of the computed schedule, project out the
4102 * first tile_first dimensions (as these are already covered by
4103 * the host code) and insert "statement-level" dimensions at even
4104 * positions so that we can schedule copy blocks and synchronization
4105 * before/after each level.
4107 * In particular, copy blocks are inserted inside the innermost
4108 * level that affect the tile. For the copying to global memory,
4109 * those from private memory are scheduled before those from shared
4110 * memory such that synchronization can be inserted between the two
4111 * at the innermost level.
4112 * Synchronization is inserted at the innermost level before the
4113 * actual kernel code if there is any copying from global memory
4114 * to shared memory. It is inserted unconditionally at the innermost
4115 * level after the actual kernel code and the copying to global memory
4116 * from private memory (if any). Finally, it is inserted after
4117 * any copying to global memory, except at the outermost level
4118 * and at the innermost level if there is no copying from shared
4119 * memory. The copying from private memory is covered by the unconditional
4120 * synchronization at the innermost level.
4122 static __isl_give isl_union_map
*body_schedule(struct gpu_gen
*gen
,
4123 __isl_take isl_union_map
*schedule
)
4127 isl_union_map
*shared_sched
;
4128 isl_union_map
*sched
;
4129 isl_map
*proj
, *map
;
4132 shared_sched
= isl_union_map_copy(gen
->tiled_sched
);
4133 proj
= projection(isl_union_map_get_space(shared_sched
),
4134 gen
->tiled_len
, gen
->shared_len
);
4135 shared_sched
= isl_union_map_apply_range(shared_sched
,
4136 isl_union_map_from_map(proj
));
4137 space
= isl_union_map_get_space(shared_sched
);
4138 proj
= insert_even(gen
, space
, -1, 0);
4139 sched
= isl_union_map_apply_range(isl_union_map_copy(shared_sched
),
4140 isl_union_map_from_map(proj
));
4142 res
= isl_union_map_range_product(isl_union_map_copy(schedule
), sched
);
4145 for (i
= 0; i
< gen
->prog
->n_array
; ++i
)
4146 s
+= gen
->prog
->array
[i
].n_group
;
4149 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
4150 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
4152 for (j
= 0; j
< array
->n_group
; ++j
) {
4153 struct gpu_array_ref_group
*group
;
4155 group
= array
->groups
[j
];
4156 if (!group
->private_tile
&& !group
->shared_tile
)
4158 res
= add_group_schedule(gen
, res
, schedule
,
4159 shared_sched
, group
, 0, k
, s
);
4160 res
= add_group_schedule(gen
, res
, schedule
,
4161 shared_sched
, group
, 1, k
, s
);
4166 res
= add_sync_schedule(gen
, res
, schedule
, shared_sched
,
4167 gen
->shared_len
- gen
->tile_first
, 1 + s
);
4169 isl_union_map_free(shared_sched
);
4170 isl_union_map_free(schedule
);
4175 /* Generate code for "kernel" in the given "context".
4177 * We first generate code for the shared tile loops (T1T, T1P and T2)
4178 * in a context that includes the block ids.
4179 * Within each iteration of these loops an additional code generation
4180 * is performed (within create_kernel_leaf) for the rest of the schedule
4181 * in a context that includes the thread ids.
4183 static __isl_give isl_ast_node
*generate_kernel(struct gpu_gen
*gen
,
4184 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*host_domain
,
4185 __isl_keep isl_multi_pw_aff
*grid_size
)
4189 isl_id_list
*iterators
;
4190 isl_union_map
*schedule
;
4194 schedule
= isl_ast_build_get_schedule(build
);
4196 build
= isl_ast_build_copy(build
);
4197 build
= isl_ast_build_restrict(build
, isl_set_copy(host_domain
));
4198 space
= isl_ast_build_get_schedule_space(build
);
4199 set
= isl_set_universe(isl_space_copy(space
));
4200 set
= add_bounded_parameters_dynamic(set
, grid_size
, "b");
4201 build
= isl_ast_build_restrict(build
, set
);
4203 schedule
= body_schedule(gen
, schedule
);
4205 sched_len
= 2 * (gen
->shared_len
- gen
->tile_first
) + 1;
4207 build
= set_atomic_and_unroll(build
, space
, sched_len
);
4208 iterators
= generate_names(gen
->ctx
, sched_len
, "g");
4209 build
= isl_ast_build_set_iterators(build
, iterators
);
4210 build
= isl_ast_build_set_create_leaf(build
, &create_kernel_leaf
, gen
);
4211 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
4212 isl_ast_build_free(build
);
4217 /* Attach "id" to the given node.
4219 static __isl_give isl_ast_node
*attach_id(__isl_take isl_ast_node
*node
,
4220 __isl_keep isl_ast_build
*build
, void *user
)
4224 node
= isl_ast_node_set_annotation(node
, id
);
4229 /* Construct an AST node for performing a kernel launch and attach
4230 * the information about the kernel to that node.
4232 * The kernel AST has been constructed in the context of the range
4233 * of "schedule". In particular, the grid size has been computed
4234 * in the context. We therefore still need to make sure that these
4235 * constraints are expressed in the code. We do this by creating a schedule
4237 * kernel[] -> [S -> []]
4239 * where S is the schedule domain, i.e., the range of "schedule".
4240 * The AST generation will then create a single call surrounded by
4241 * all the condition in "S" that have not been expressed yet.
4243 * The kernel information is attached to this node in attach_id.
4245 static __isl_give isl_ast_node
*construct_launch(
4246 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*schedule
,
4247 __isl_take
struct ppcg_kernel
*kernel
)
4251 isl_union_set
*domain
;
4256 ctx
= isl_ast_build_get_ctx(build
);
4258 id
= isl_id_alloc(ctx
, NULL
, kernel
);
4259 id
= isl_id_set_free_user(id
, &ppcg_kernel_free
);
4261 domain
= isl_union_map_range(schedule
);
4262 set
= isl_set_from_union_set(domain
);
4263 map
= isl_map_from_domain(set
);
4264 map
= isl_map_from_range(isl_map_wrap(map
));
4265 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "kernel");
4266 schedule
= isl_union_map_from_map(map
);
4268 build
= isl_ast_build_set_at_each_domain(build
, &attach_id
, id
);
4269 node
= isl_ast_build_ast_from_schedule(build
, schedule
);
4270 isl_ast_build_free(build
);
4275 /* This function is called for each leaf in the AST of the host code.
4276 * We first specialize the schedule to the site of the leaf, compute
4277 * the size of shared memory and then construct the body of host code
4278 * and the associated kernel.
4280 * The necessary information for printing the kernel launch is
4281 * stored in a struct ppcg_kernel and attached to the leaf node
4282 * created to represent the launch.
4284 static __isl_give isl_ast_node
*create_host_leaf(
4285 __isl_take isl_ast_build
*build
, void *user
)
4287 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
4290 struct ppcg_kernel
*kernel
;
4291 isl_set
*host_domain
;
4292 isl_union_map
*schedule
;
4293 isl_union_map
*local_sched
;
4294 isl_union_map
*access
;
4295 isl_union_set
*domain
;
4298 schedule
= isl_ast_build_get_schedule(build
);
4300 isl_union_map_foreach_map(schedule
, &extract_tile_len
, gen
);
4303 domain
= isl_union_map_domain(isl_union_map_copy(schedule
));
4305 local_sched
= isl_union_map_copy(gen
->sched
);
4306 local_sched
= isl_union_map_intersect_domain(local_sched
, domain
);
4307 access
= isl_union_map_union(isl_union_map_copy(gen
->prog
->read
),
4308 isl_union_map_copy(gen
->prog
->write
));
4309 access
= isl_union_map_apply_domain(access
,
4310 isl_union_map_copy(local_sched
));
4312 gen
->tiled_sched
= tile_schedule(gen
, local_sched
);
4313 gen
->tiled_sched
= parametrize_tiled_schedule(gen
, gen
->tiled_sched
);
4314 gen
->tiled_sched
= scale_tile_loops(gen
, gen
->tiled_sched
);
4316 kernel
= gen
->kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
4320 kernel
->id
= gen
->kernel_id
++;
4321 kernel
->n_block
= gen
->n_block
;
4322 for (i
= 0; i
< gen
->n_block
; ++i
)
4323 kernel
->block_dim
[i
] = gen
->block_dim
[i
];
4324 kernel
->n_grid
= gen
->n_grid
;
4325 for (i
= 0; i
< gen
->n_grid
; ++i
)
4326 kernel
->grid_dim
[i
] = gen
->grid_dim
[i
];
4327 kernel
->context
= isl_union_map_params(isl_union_map_copy(schedule
));
4328 kernel
->grid_size
= extract_grid_size(gen
, kernel
);
4329 kernel
->arrays
= isl_union_map_range(access
);
4330 kernel
->space
= isl_ast_build_get_schedule_space(build
);
4332 gen
->local_sched
= isl_union_map_copy(gen
->tiled_sched
);
4334 gen
->local_sched
= thread_tile_schedule(gen
, gen
->local_sched
);
4335 gen
->local_sched
= scale_thread_tile_loops(gen
, gen
->local_sched
);
4337 gen
->private_access
= NULL
;
4338 compute_shared_sched(gen
);
4339 gen
->privatization
= compute_privatization(gen
);
4340 group_references(gen
);
4341 compute_private_access(gen
);
4342 check_shared_memory_bound(gen
);
4343 host_domain
= isl_set_from_union_set(isl_union_map_range(
4344 isl_union_map_copy(schedule
)));
4345 localize_bounds(gen
, kernel
, host_domain
);
4347 gen
->local_sched
= interchange_for_unroll(gen
, gen
->local_sched
);
4349 kernel
->tree
= generate_kernel(gen
, build
, host_domain
,
4351 create_kernel_vars(gen
, kernel
);
4353 free_local_array_info(gen
);
4354 isl_map_free(gen
->privatization
);
4355 isl_union_map_free(gen
->private_access
);
4356 isl_union_map_free(gen
->local_sched
);
4357 isl_union_map_free(gen
->tiled_sched
);
4358 isl_union_map_free(gen
->shared_sched
);
4359 isl_union_map_free(gen
->shared_proj
);
4360 isl_set_free(host_domain
);
4361 free(gen
->tile_size
);
4363 node
= construct_launch(build
, schedule
, kernel
);
4367 isl_union_map_free(schedule
);
4371 /* Use isl to generate code for the outer gen->tile_first loops
4372 * of the global schedule in gen->sched, resulting in the host code.
4373 * Within each iteration of this partial schedule, i.e., for each kernel
4374 * launch, create_host_leaf takes care of generating the kernel code.
4376 static __isl_give isl_ast_node
*generate_host_code(struct gpu_gen
*gen
)
4378 isl_ast_build
*build
;
4380 isl_union_map
*sched
;
4382 isl_id_list
*iterators
;
4384 sched
= isl_union_map_copy(gen
->sched
);
4385 proj
= projection(isl_union_map_get_space(sched
),
4386 gen
->untiled_len
, gen
->tile_first
);
4387 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
4389 isl_options_set_ast_build_group_coscheduled(gen
->ctx
, 1);
4390 build
= isl_ast_build_from_context(isl_set_copy(gen
->prog
->context
));
4391 iterators
= generate_names(gen
->ctx
, gen
->tile_first
, "h");
4392 build
= isl_ast_build_set_iterators(build
, iterators
);
4393 build
= isl_ast_build_set_create_leaf(build
, &create_host_leaf
, gen
);
4394 tree
= isl_ast_build_ast_from_schedule(build
, sched
);
4395 isl_ast_build_free(build
);
4400 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
4404 return isl_union_map_read_from_str(ctx
, str
);
4407 /* Information about the outermost tilable bands in the forest of bands.
4409 * tile_len and n_parallel are only sets on band_info structures
4410 * that correspond to outermost bands. For other bands (in particular,
4411 * ancestors of the outermost bands), n_parallal is set to 0.
4413 * prefix is the (padded) schedule leading up to the outermost tilable bands.
4415 * tile_first is the number of schedule dimensions in prefix.
4417 * suffix is the schedule of the outermost tilable bands and their descendants.
4420 struct gpu_gen
*gen
;
4424 isl_union_map
*prefix
;
4425 isl_union_map
*suffix
;
4428 /* Set tile_len and n_parallel of the statement to that of
4429 * their outermost band, recorded in the band_info.
4431 static int set_stmt_tile_len(__isl_take isl_map
*map
, void *user
)
4433 struct band_info
*info
= user
;
4434 struct gpu_stmt
*stmt
;
4437 id
= isl_map_get_tuple_id(map
, isl_dim_in
);
4438 stmt
= find_stmt(info
->gen
->prog
, id
);
4441 stmt
->tile_len
= info
->tile_len
;
4442 stmt
->n_parallel
= info
->n_parallel
;
4449 static void list_select_outer_band(struct gpu_gen
*gen
,
4450 __isl_take isl_band_list
*list
, int pos
, struct band_info
*list_info
);
4452 /* Check if this band has any parallel loops. If so, take it as
4453 * the outermost tilable band. If not, continue looking for the
4454 * outermost tilable band in the children of the current band.
4456 static void band_select_outer_band(struct gpu_gen
*gen
,
4457 __isl_take isl_band
*band
, int pos
, struct band_info
*info
)
4459 int n
= isl_band_n_member(band
);
4462 for (n_parallel
= 0; n_parallel
< n
; ++n_parallel
)
4463 if (!isl_band_member_is_zero_distance(band
, n_parallel
))
4466 info
->n_parallel
= n_parallel
;
4469 info
->tile_first
= pos
;
4471 info
->prefix
= isl_band_get_prefix_schedule(band
);
4472 info
->suffix
= isl_union_map_flat_range_product(
4473 isl_band_get_partial_schedule(band
),
4474 isl_band_get_suffix_schedule(band
));
4475 isl_union_map_foreach_map(info
->prefix
,
4476 &set_stmt_tile_len
, info
);
4477 } else if (isl_band_has_children(band
)) {
4478 isl_band_list
*children
;
4479 children
= isl_band_get_children(band
);
4480 list_select_outer_band(gen
, children
, pos
+ n
, info
);
4483 info
->tile_first
= pos
+ n
;
4485 info
->prefix
= isl_union_map_flat_range_product(
4486 isl_band_get_prefix_schedule(band
),
4487 isl_band_get_partial_schedule(band
));
4488 info
->suffix
= isl_band_get_suffix_schedule(band
);
4489 isl_union_map_foreach_map(info
->prefix
,
4490 &set_stmt_tile_len
, info
);
4493 isl_band_free(band
);
4496 /* Comparison function that returns a non-zero value for band_infos
4497 * with different tile_len fields or different n_parallel fields.
4499 static int cmp_band(const void *p1
, const void *p2
)
4501 const struct band_info
*info1
= p1
;
4502 const struct band_info
*info2
= p2
;
4504 if (info1
->tile_len
!= info2
->tile_len
)
4505 return info1
->tile_len
- info2
->tile_len
;
4507 return info1
->n_parallel
- info2
->n_parallel
;
4510 /* Extend "umap" with coordinates with fixed value "val"
4511 * to a total length of "dst_len", assuming the original dimension is "src_len".
4513 static __isl_give isl_union_map
*extend_range(
4514 __isl_take isl_union_map
*umap
, int src_len
, int dst_len
, int val
)
4520 dim
= isl_union_map_get_space(umap
);
4521 map
= isl_map_reverse(projection(dim
, dst_len
, src_len
));
4522 for (i
= src_len
; i
< dst_len
; ++i
)
4523 map
= isl_map_fix_si(map
, isl_dim_out
, i
, val
);
4525 umap
= isl_union_map_apply_range(umap
, isl_union_map_from_map(map
));
4530 /* Group bands with the same values for tile_len and n_parallel.
4531 * The prefix schedule is then extended with a fixed coordinate that
4532 * is different for each such group.
4533 * Note that the actual values for this coordinate are not important.
4534 * The bands have already been effectively separated at a higher level
4535 * or they are independent and may be executed in parallel.
4536 * The list of band_info has been sorted before this functions is called.
4538 static void separate_bands(struct band_info
*info
, int n
)
4543 for (i
= 0; i
< n
; ++i
) {
4544 int l
= info
[i
].tile_first
;
4547 (info
[i
].tile_len
!= info
[i
- 1].tile_len
||
4548 info
[i
].n_parallel
!= info
[i
- 1].n_parallel
))
4551 info
[i
].prefix
= extend_range(info
[i
].prefix
,
4553 info
[i
].tile_first
= l
+ 1;
4557 /* Select the outermost bands in the elements of the list, align
4558 * their prefix schedules, separate bands with different values
4559 * for tile_len and/or n_parallel and then combine the resulting
4560 * prefix and suffix schedules into a single pair of prefix and
4561 * suffix schedules for the entire list.
4563 static void list_select_outer_band(struct gpu_gen
*gen
,
4564 __isl_take isl_band_list
*list
, int pos
, struct band_info
*list_info
)
4568 int n
= isl_band_list_n_band(list
);
4569 isl_ctx
*ctx
= isl_band_list_get_ctx(list
);
4570 struct band_info
*info
;
4572 isl_union_map
*prefix
;
4573 isl_union_map
*suffix
;
4576 info
= isl_calloc_array(ctx
, struct band_info
, n
);
4580 for (i
= 0; i
< n
; ++i
) {
4581 band
= isl_band_list_get_band(list
, i
);
4582 band_select_outer_band(gen
, band
, pos
, &info
[i
]);
4583 if (info
[i
].tile_first
> max_tile_first
)
4584 max_tile_first
= info
[i
].tile_first
;
4587 for (i
= 0; i
< n
; ++i
) {
4588 if (info
[i
].tile_first
== max_tile_first
)
4590 info
[i
].prefix
= extend_range(info
[i
].prefix
,
4591 info
[i
].tile_first
, max_tile_first
, 0);
4592 info
[i
].tile_first
= max_tile_first
;
4595 qsort(info
, n
, sizeof(struct band_info
), &cmp_band
);
4597 for (i
= 0; i
< n
- 1; ++i
)
4598 if (info
[i
].tile_len
!= info
[i
+ 1].tile_len
||
4599 info
[i
].n_parallel
!= info
[i
+ 1].n_parallel
)
4603 separate_bands(info
, n
);
4605 prefix
= info
[0].prefix
;
4606 suffix
= info
[0].suffix
;
4608 for (i
= 1; i
< n
; ++i
) {
4609 prefix
= isl_union_map_union(prefix
, info
[i
].prefix
);
4610 suffix
= isl_union_map_union(suffix
, info
[i
].suffix
);
4613 list_info
->tile_first
= info
[0].tile_first
;
4614 list_info
->tile_len
= -1;
4615 list_info
->prefix
= prefix
;
4616 list_info
->suffix
= suffix
;
4618 isl_band_list_free(list
);
4622 /* Select the outermost tilable band that (by construction)
4623 * has at least one parallel loop.
4624 * The starting position of the aligned band is stored in the pair
4626 * The sizes and number of parallel loops may be different in different
4627 * parts of the band forest and are therefore stored in the gpu_stmts.
4629 * Return the complete schedule, with the tilable bands aligned
4630 * at gen->tile_first and padded with zero, if needed.
4632 static __isl_give isl_union_map
*select_outer_tilable_band(struct gpu_gen
*gen
,
4633 __isl_keep isl_schedule
*schedule
)
4635 isl_band_list
*list
;
4636 struct band_info info
;
4638 gen
->n_parallel
= 0;
4641 list
= isl_schedule_get_band_forest(schedule
);
4643 list_select_outer_band(gen
, list
, 0, &info
);
4645 gen
->tile_first
= info
.tile_first
;
4646 info
.suffix
= align_range(info
.suffix
);
4648 return isl_union_map_flat_range_product(info
.prefix
, info
.suffix
);
4651 /* Set gen->untiled_len to the number of scheduling dimensions
4652 * for the schedule of the first domain.
4653 * We assume here that this number is the same for all domains.
4655 static int set_untiled_len(__isl_take isl_map
*map
, void *user
)
4657 unsigned *untiled_len
= user
;
4659 *untiled_len
= isl_map_dim(map
, isl_dim_out
);
4665 /* Compute an appropriate schedule based on the accesses in
4666 * gen->read and gen->write.
4668 * We use the dependences in gen->prog->scop to compute
4669 * a schedule that has a parallel loop in each tilable band.
4670 * Finally, we select the outermost tilable band.
4672 static void compute_schedule(struct gpu_gen
*gen
)
4674 isl_union_set
*domain
;
4675 isl_union_map
*dep_raw
, *dep
;
4676 isl_union_map
*uninitialized
;
4677 isl_union_map
*sched
;
4678 isl_schedule
*schedule
;
4680 dep_raw
= isl_union_map_copy(gen
->prog
->scop
->dep_flow
);
4681 uninitialized
= isl_union_map_copy(gen
->prog
->scop
->live_in
);
4683 gen
->prog
->copy_in
= isl_union_map_range(uninitialized
);
4685 dep
= isl_union_map_copy(gen
->prog
->scop
->dep_false
);
4686 dep
= isl_union_map_union(dep
, dep_raw
);
4687 dep
= isl_union_map_coalesce(dep
);
4689 domain
= isl_union_set_copy(gen
->prog
->scop
->domain
);
4690 domain
= isl_union_set_intersect_params(domain
,
4691 isl_set_copy(gen
->prog
->scop
->context
));
4692 schedule
= isl_union_set_compute_schedule(isl_union_set_copy(domain
),
4693 isl_union_map_copy(dep
), dep
);
4694 if (gen
->options
->debug
->dump_schedule
)
4695 isl_schedule_dump(schedule
);
4697 sched
= select_outer_tilable_band(gen
, schedule
);
4699 isl_union_map_foreach_map(sched
, &set_untiled_len
, &gen
->untiled_len
);
4700 sched
= isl_union_map_intersect_domain(sched
, domain
);
4703 isl_schedule_free(schedule
);
4706 static struct gpu_stmt_access
**expr_extract_access(struct pet_expr
*expr
,
4707 struct gpu_stmt_access
**next_access
)
4709 struct gpu_stmt_access
*access
;
4710 isl_ctx
*ctx
= isl_map_get_ctx(expr
->acc
.access
);
4712 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
4714 access
->next
= NULL
;
4715 access
->read
= expr
->acc
.read
;
4716 access
->write
= expr
->acc
.write
;
4717 access
->access
= isl_map_copy(expr
->acc
.access
);
4719 *next_access
= access
;
4720 next_access
= &(*next_access
)->next
;
4724 static struct gpu_stmt_access
**expr_extract_accesses(struct pet_expr
*expr
,
4725 struct gpu_stmt_access
**next_access
)
4729 for (i
= 0; i
< expr
->n_arg
; ++i
)
4730 next_access
= expr_extract_accesses(expr
->args
[i
],
4733 if (expr
->type
== pet_expr_access
)
4734 next_access
= expr_extract_access(expr
, next_access
);
4739 static void pet_stmt_extract_accesses(struct gpu_stmt
*stmt
)
4741 struct gpu_stmt_access
**next_access
= &stmt
->accesses
;
4743 stmt
->accesses
= NULL
;
4744 expr_extract_accesses(stmt
->body
, next_access
);
4747 /* Return an array of gpu_stmt representing the statements in "scop".
4749 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
4750 __isl_keep isl_set
*context
)
4753 struct gpu_stmt
*stmts
;
4755 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->n_stmt
);
4758 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
4759 struct gpu_stmt
*s
= &stmts
[i
];
4761 s
->id
= isl_set_get_tuple_id(scop
->stmts
[i
]->domain
);
4762 s
->body
= scop
->stmts
[i
]->body
;
4763 pet_stmt_extract_accesses(s
);
4769 /* Replace the scop in the "input" file by equivalent code
4770 * that uses the GPU. "scop" is assumed to correspond to this scop.
4772 * We first compute a schedule that respects the dependences
4773 * of the original program and select the outermost band
4774 * of tilable dimensions that has at least one parallel loop.
4775 * We then have three blocks of dimensions
4779 * The tilable band "B" is first tiled according to "tile" sizes, resulting
4784 * For each iteration of the T loop and for each array, we compute
4785 * the array elements accessed by that iteration, construct a rectangular
4786 * box around it and shift it to the origin. The result is used
4787 * as shared memory for the array.
4789 * We then split off at most 2 parallel loops from the T loops and
4790 * at most 3 parallel loops from the P loops
4794 * The T1/P1 loops are then tiled or "wrapped" over the blocks/threads,
4795 * according to "grid"/"block" sizes.
4797 * H T1T T1P T2 P1T P1P P2 G
4799 * Finally, the T1P and P1P iterators are equated to the block and
4800 * thread dimensions respectively and so are effectively removed.
4801 * The H loops are run on the host. The T1T, T2, P1T, P2 and G loops
4802 * are run on the GPU.
4804 * Code is generated in three stages. We first generate code for the
4805 * host (the H loops), with iterators h%d. Then, for each leaf node
4806 * of the resulting AST, we generate code for the shared loops (up to
4807 * and including T2), with iterators g%d and after equating the H loops
4808 * to h%d parameters and the T1P loops to the block dimensions.
4809 * Finally, we generate code for the remaining loops in a similar fashion.
4811 __isl_give isl_ast_node
*generate_gpu(isl_ctx
*ctx
, struct gpu_prog
*prog
,
4812 struct ppcg_options
*options
)
4814 isl_union_map
*sched
;
4823 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
4824 gen
.options
= options
;
4826 compute_schedule(&gen
);
4829 tree
= generate_host_code(&gen
);
4831 clear_gpu_gen(&gen
);
4836 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
4838 struct gpu_prog
*prog
;
4843 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
4848 prog
->context
= isl_set_copy(scop
->context
);
4849 prog
->n_stmts
= scop
->n_stmt
;
4850 prog
->stmts
= extract_stmts(ctx
, scop
, prog
->context
);
4851 prog
->read
= isl_union_map_copy(scop
->reads
);
4852 prog
->write
= isl_union_map_copy(scop
->writes
);
4854 collect_array_info(prog
);
4859 void gpu_prog_free(struct gpu_prog
*prog
)
4863 free_array_info(prog
);
4864 free_stmts(prog
->stmts
, prog
->n_stmts
);
4865 isl_union_set_free(prog
->copy_in
);
4866 isl_union_map_free(prog
->read
);
4867 isl_union_map_free(prog
->write
);
4868 isl_set_free(prog
->context
);