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
;
2536 if (!gen
->options
->use_shared_memory
&&
2537 !gen
->options
->use_private_memory
)
2539 if (gpu_array_is_read_only_scalar(group
->array
))
2542 access
= group_access_relation(group
, 1, 1);
2543 no_reuse
= isl_union_map_is_injective(access
);
2545 if (!no_reuse
|| !access_is_coalesced(gen
, access
)) {
2546 group
->shared_tile
= create_tile(ctx
, group
->array
->n_index
);
2547 if (!can_tile(group
->access
, group
->shared_tile
))
2548 group
->shared_tile
= free_tile(group
->shared_tile
);
2552 isl_union_map_free(access
);
2556 access
= isl_union_map_apply_domain(access
,
2557 isl_union_map_copy(gen
->shared_sched
));
2559 acc
= isl_map_from_union_map(access
);
2561 if (!access_is_bijective(gen
, acc
)) {
2566 group
->private_tile
= create_tile(gen
->ctx
, n_index
);
2567 acc
= isl_map_apply_domain(acc
, isl_map_copy(gen
->privatization
));
2568 if (!can_tile(acc
, group
->private_tile
))
2569 group
->private_tile
= free_tile(group
->private_tile
);
2574 /* Compute the private and/or shared memory tiles for the array
2575 * reference group "group" of array "array" and set last_shared.
2577 static void compute_group_bounds(struct gpu_gen
*gen
,
2578 struct gpu_array_ref_group
*group
)
2580 compute_group_bounds_core(gen
, group
);
2581 set_last_shared(gen
, group
);
2584 /* If two groups have overlapping access relations (as determined by
2585 * the "overlap" function) and if one of them involves a write,
2586 * then merge the two groups into one.
2587 * If "compute_bounds" is set, then call compute_group_bounds
2588 * on the merged groups.
2590 * Return the updated number of groups.
2592 static int group_writes(struct gpu_gen
*gen
,
2593 int n
, struct gpu_array_ref_group
**groups
,
2594 int (*overlap
)(struct gpu_array_ref_group
*group1
,
2595 struct gpu_array_ref_group
*group2
), int compute_bounds
)
2599 for (i
= 0; i
< n
; ++i
) {
2600 for (j
= n
- 1; j
> i
; --j
) {
2601 if (!groups
[i
]->write
&& !groups
[j
]->write
)
2604 if (!overlap(groups
[i
], groups
[j
]))
2607 groups
[i
] = join_groups_and_free(groups
[i
], groups
[j
]);
2609 compute_group_bounds(gen
, groups
[i
]);
2611 groups
[j
] = groups
[n
- 1];
2619 /* If two groups have overlapping access relations (within the innermost
2620 * loop) and if one of them involves a write, then merge the two groups
2623 * Return the updated number of groups.
2625 static int group_overlapping_writes(struct gpu_gen
*gen
,
2626 int n
, struct gpu_array_ref_group
**groups
)
2628 return group_writes(gen
, n
, groups
, &accesses_overlap
, 0);
2631 /* Check if the access relations of group1 and group2 overlap within
2632 * the outermost min(group1->last_shared, group2->last_shared) loops.
2634 static int last_shared_accesses_overlap(struct gpu_array_ref_group
*group1
,
2635 struct gpu_array_ref_group
*group2
)
2640 isl_map
*map_i
, *map_j
, *map
;
2642 last_shared
= group1
->last_shared
;
2643 if (group2
->last_shared
< last_shared
)
2644 last_shared
= group2
->last_shared
;
2645 map_i
= isl_map_copy(group1
->access
);
2646 dim
= isl_map_dim(map_i
, isl_dim_in
);
2647 map_i
= isl_map_eliminate(map_i
, isl_dim_in
,
2648 last_shared
+ 1, dim
- (last_shared
+ 1));
2649 map_j
= isl_map_copy(group2
->access
);
2650 map_j
= isl_map_eliminate(map_j
, isl_dim_in
,
2651 last_shared
+ 1, dim
- (last_shared
+ 1));
2652 map
= isl_map_intersect(map_i
, map_j
);
2653 empty
= isl_map_is_empty(map
);
2659 /* If two groups have overlapping access relations (within the outer
2660 * last_shared loops) and if one of them involves a write,
2661 * then merge the two groups into one.
2663 * Return the updated number of groups.
2665 static int group_last_shared_overlapping_writes(struct gpu_gen
*gen
, int n
,
2666 struct gpu_array_ref_group
**groups
)
2668 return group_writes(gen
, n
, groups
, &last_shared_accesses_overlap
, 1);
2671 /* Is the size of the tile specified by "tile" smaller than the sum of
2672 * the sizes of the tiles specified by "tile1" and "tile2"?
2674 static int smaller_tile(struct gpu_array_tile
*tile
,
2675 struct gpu_array_tile
*tile1
, struct gpu_array_tile
*tile2
)
2678 isl_int size
, size1
, size2
;
2681 isl_int_init(size1
);
2682 isl_int_init(size2
);
2684 tile_size(tile
, &size
);
2685 tile_size(tile1
, &size1
);
2686 tile_size(tile2
, &size2
);
2688 isl_int_sub(size
, size
, size1
);
2689 isl_int_sub(size
, size
, size2
);
2690 smaller
= isl_int_is_neg(size
);
2692 isl_int_clear(size2
);
2693 isl_int_clear(size1
);
2694 isl_int_clear(size
);
2699 /* Given an initial grouping of array references and shared memory tiles
2700 * for each group that allows for a shared memory tile, merge two groups
2701 * if both have a shared memory tile, the merged group also has
2702 * a shared memory tile and the size of the tile for the merge group
2703 * is smaller than the sum of the tile sizes of the individual groups.
2705 * If merging two groups decreases the "last_shared" dimension of
2706 * one or both of the two groups, then we need to check for overlapping
2709 * Return the number of groups after merging.
2711 static int group_common_shared_memory_tile(struct gpu_gen
*gen
,
2712 struct gpu_array_info
*array
, int n
,
2713 struct gpu_array_ref_group
**groups
)
2716 int recompute_overlap
= 0;
2717 isl_ctx
*ctx
= isl_space_get_ctx(array
->dim
);
2719 for (i
= 0; i
< n
; ++i
) {
2720 if (!groups
[i
]->shared_tile
)
2722 for (j
= n
- 1; j
> i
; --j
) {
2725 struct gpu_array_ref_group
*group
;
2727 if (!groups
[j
]->shared_tile
)
2730 map
= isl_map_intersect(isl_map_copy(groups
[i
]->access
),
2731 isl_map_copy(groups
[j
]->access
));
2732 empty
= isl_map_is_empty(map
);
2738 group
= join_groups(groups
[i
], groups
[j
]);
2739 compute_group_bounds(gen
, group
);
2740 if (!group
->shared_tile
||
2741 !smaller_tile(group
->shared_tile
,
2742 groups
[i
]->shared_tile
,
2743 groups
[j
]->shared_tile
)) {
2744 free_array_ref_group(group
);
2748 if (group
->last_shared
< groups
[i
]->last_shared
||
2749 group
->last_shared
< groups
[j
]->last_shared
)
2750 recompute_overlap
= 1;
2751 free_array_ref_group(groups
[i
]);
2752 free_array_ref_group(groups
[j
]);
2755 groups
[j
] = groups
[n
- 1];
2760 if (recompute_overlap
)
2761 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
2765 /* Set array->n_group and array->groups to n and groups.
2767 * Additionally, set the "nr" field of each group
2768 * and the "group" field of each reference in each group.
2770 static void set_array_groups(struct gpu_array_info
*array
,
2771 int n
, struct gpu_array_ref_group
**groups
)
2776 array
->groups
= groups
;
2778 for (i
= 0; i
< n
; ++i
) {
2781 for (j
= 0; j
< groups
[i
]->n_ref
; ++j
)
2782 groups
[i
]->refs
[j
]->group
= i
;
2786 /* Group array references that should be considered together when
2787 * deciding whether to access them from private, shared or global memory.
2789 * In particular, if two array references overlap and if one of them
2790 * is a write, then the two references are grouped together.
2791 * We first perform an initial grouping based only on the access relation.
2792 * After computing shared and private memory tiles, we check for
2793 * overlapping writes again, but this time taking into account
2794 * the "last_shared" property.
2796 * Furthermore, if two groups admit a shared memory tile and if the
2797 * combination of the two also admits a shared memory tile, we merge
2800 static void group_array_references(struct gpu_gen
*gen
,
2801 struct gpu_array_info
*array
, __isl_keep isl_union_map
*sched
)
2805 isl_ctx
*ctx
= isl_union_map_get_ctx(sched
);
2806 struct gpu_array_ref_group
**groups
;
2808 groups
= isl_calloc_array(ctx
, struct gpu_array_ref_group
*,
2812 n
= populate_array_references(array
, sched
, groups
);
2814 n
= group_overlapping_writes(gen
, n
, groups
);
2816 for (i
= 0; i
< n
; ++i
)
2817 compute_group_bounds(gen
, groups
[i
]);
2819 n
= group_last_shared_overlapping_writes(gen
, n
, groups
);
2821 n
= group_common_shared_memory_tile(gen
, array
, n
, groups
);
2823 set_array_groups(array
, n
, groups
);
2826 /* Take tiled_sched, project it onto the shared tile loops and
2827 * the loops that will be wrapped over the threads and
2828 * store the result in gen->shared_sched.
2829 * Also compute a projection that projects out the loops that will be
2830 * wrapped over the threads and store this projection in gen->shared_proj.
2832 static void compute_shared_sched(struct gpu_gen
*gen
)
2837 isl_union_map
*sched
;
2839 sched
= isl_union_map_copy(gen
->tiled_sched
);
2841 dim
= isl_union_map_get_space(sched
);
2842 proj
= projection(dim
, gen
->tiled_len
, gen
->shared_len
+ gen
->n_block
);
2843 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
2845 dim
= isl_union_map_get_space(sched
);
2846 proj
= projection(dim
, gen
->shared_len
+ gen
->n_block
, gen
->shared_len
);
2848 gen
->shared_sched
= sched
;
2849 gen
->shared_proj
= isl_union_map_from_map(proj
);
2852 /* Group references of all arrays in the program.
2854 static void group_references(struct gpu_gen
*gen
)
2857 isl_union_map
*sched
;
2859 sched
= isl_union_map_apply_range(isl_union_map_copy(gen
->shared_sched
),
2860 isl_union_map_copy(gen
->shared_proj
));
2862 for (i
= 0; i
< gen
->prog
->n_array
; ++i
)
2863 group_array_references(gen
, &gen
->prog
->array
[i
], sched
);
2865 isl_union_map_free(sched
);
2868 /* Free all array information that is local to the current kernel.
2870 static void free_local_array_info(struct gpu_gen
*gen
)
2874 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
2875 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
2877 for (j
= 0; j
< array
->n_group
; ++j
)
2878 free_array_ref_group(array
->groups
[j
]);
2879 free(array
->groups
);
2883 /* Compute the effective grid size as a list of the sizes in each dimension.
2885 * The grid size specified by the user or set by default
2886 * in read_grid_sizes() and applied in tile_schedule(),
2887 * may be too large for the given code in the sense that
2888 * it may contain blocks that don't need to execute anything.
2889 * We therefore don't return this grid size, but instead the
2890 * smallest grid size that ensures that all blocks that actually
2891 * execute code are included in the grid.
2893 * We first extract a description of the grid, i.e., the possible values
2894 * of the block ids, from gen->tiled_sched.
2895 * The block ids are parameters in gen->tiled_sched.
2896 * We simply need to change them into set dimensions.
2898 * Then, for each block dimension, we compute the maximal value of the block id
2901 static __isl_give isl_multi_pw_aff
*extract_grid_size(struct gpu_gen
*gen
,
2902 struct ppcg_kernel
*kernel
)
2906 isl_multi_pw_aff
*mpa
;
2908 grid
= isl_union_map_params(isl_union_map_copy(gen
->tiled_sched
));
2909 grid
= isl_set_from_params(grid
);
2910 grid
= isl_set_add_dims(grid
, isl_dim_set
, gen
->n_grid
);
2911 for (i
= 0; i
< gen
->n_grid
; ++i
) {
2915 snprintf(name
, sizeof(name
), "b%d", i
);
2916 pos
= isl_set_find_dim_by_name(grid
, isl_dim_param
, name
);
2918 grid
= isl_set_equate(grid
, isl_dim_param
, pos
, isl_dim_set
, i
);
2919 grid
= isl_set_project_out(grid
, isl_dim_param
, pos
, 1);
2922 mpa
= isl_multi_pw_aff_zero(isl_set_get_space(grid
));
2923 for (i
= 0; i
< gen
->n_grid
; ++i
) {
2928 bound
= isl_set_dim_max(isl_set_copy(grid
), i
);
2929 bound
= isl_pw_aff_coalesce(bound
);
2930 bound
= isl_pw_aff_gist(bound
, isl_set_copy(kernel
->context
));
2932 space
= isl_pw_aff_get_domain_space(bound
);
2933 one
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2934 one
= isl_aff_add_constant_si(one
, 1);
2935 bound
= isl_pw_aff_add(bound
, isl_pw_aff_from_aff(one
));
2936 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, bound
);
2943 void ppcg_kernel_free(void *user
)
2945 struct ppcg_kernel
*kernel
= user
;
2951 isl_multi_pw_aff_free(kernel
->grid_size
);
2952 isl_set_free(kernel
->context
);
2953 isl_union_set_free(kernel
->arrays
);
2954 isl_space_free(kernel
->space
);
2955 isl_ast_node_free(kernel
->tree
);
2957 for (i
= 0; i
< kernel
->n_array
; ++i
)
2958 isl_pw_aff_list_free(kernel
->array
[i
].bound
);
2959 free(kernel
->array
);
2961 for (i
= 0; i
< kernel
->n_var
; ++i
) {
2962 free(kernel
->var
[i
].name
);
2963 isl_vec_free(kernel
->var
[i
].size
);
2970 static void create_kernel_var(isl_ctx
*ctx
, struct gpu_array_ref_group
*group
,
2971 struct ppcg_kernel_var
*var
)
2974 struct gpu_array_tile
*tile
;
2978 var
->array
= group
->array
;
2980 tile
= group
->private_tile
;
2981 var
->type
= ppcg_access_private
;
2983 tile
= group
->shared_tile
;
2984 var
->type
= ppcg_access_shared
;
2987 p
= isl_printer_to_str(ctx
);
2988 p
= print_array_name(p
, group
);
2989 var
->name
= isl_printer_get_str(p
);
2990 isl_printer_free(p
);
2992 var
->size
= isl_vec_alloc(ctx
, group
->array
->n_index
);
2994 for (j
= 0; j
< group
->array
->n_index
; ++j
)
2995 var
->size
= isl_vec_set_element(var
->size
, j
,
2996 tile
->bound
[j
].size
);
2999 static void create_kernel_vars(struct gpu_gen
*gen
, struct ppcg_kernel
*kernel
)
3004 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3005 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3007 for (j
= 0; j
< array
->n_group
; ++j
) {
3008 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3009 if (group
->private_tile
|| group
->shared_tile
)
3015 kernel
->var
= isl_calloc_array(gen
->ctx
, struct ppcg_kernel_var
, n
);
3016 assert(kernel
->var
);
3019 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3020 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3022 for (j
= 0; j
< array
->n_group
; ++j
) {
3023 struct gpu_array_ref_group
*group
= array
->groups
[j
];
3024 if (!group
->private_tile
&& !group
->shared_tile
)
3026 create_kernel_var(gen
->ctx
, group
, &kernel
->var
[n
]);
3032 /* The sizes of the arrays on the host that have been computed by
3033 * extract_array_info may depend on the parameters. Use the extra
3034 * constraints on the parameters that are valid at "host_domain"
3035 * to simplify these expressions and store the results in kernel->array.
3037 static void localize_bounds(struct gpu_gen
*gen
, struct ppcg_kernel
*kernel
,
3038 __isl_keep isl_set
*host_domain
)
3043 kernel
->array
= isl_calloc_array(gen
->ctx
,
3044 struct gpu_local_array_info
, gen
->prog
->n_array
);
3045 assert(kernel
->array
);
3046 kernel
->n_array
= gen
->prog
->n_array
;
3048 context
= isl_set_copy(host_domain
);
3049 context
= isl_set_params(context
);
3051 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3052 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
3053 isl_pw_aff_list
*local
;
3055 if (array
->n_group
== 0)
3058 local
= isl_pw_aff_list_alloc(gen
->ctx
, array
->n_index
);
3060 for (j
= 0; j
< array
->n_index
; ++j
) {
3063 pwaff
= isl_pw_aff_copy(array
->bound
[j
]);
3064 pwaff
= isl_pw_aff_gist(pwaff
, isl_set_copy(context
));
3065 local
= isl_pw_aff_list_add(local
, pwaff
);
3068 kernel
->array
[i
].bound
= local
;
3070 isl_set_free(context
);
3073 /* Find the element in gen->stmt that has the given "id".
3074 * Return NULL if no such gpu_stmt can be found.
3076 static struct gpu_stmt
*find_stmt(struct gpu_prog
*prog
, __isl_keep isl_id
*id
)
3080 for (i
= 0; i
< prog
->n_stmts
; ++i
) {
3081 if (id
== prog
->stmts
[i
].id
)
3085 return i
< prog
->n_stmts
? &prog
->stmts
[i
] : NULL
;
3088 /* Set gen->tile_len and gen->n_parallel to those of the statement
3089 * affected by the first map (part of the schedule)
3090 * on which this function is called.
3091 * Because of the way the schedule is constructed, the other statements
3092 * in the list, if any, should have the same values for these properties.
3094 static int extract_tile_len(__isl_take isl_map
*map
, void *user
)
3096 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3098 struct gpu_stmt
*stmt
;
3100 id
= isl_map_get_tuple_id(map
, isl_dim_in
);
3101 stmt
= find_stmt(gen
->prog
, id
);
3107 isl_die(gen
->ctx
, isl_error_unknown
,
3108 "statement not found", return -1);
3110 gen
->tile_len
= stmt
->tile_len
;
3111 gen
->n_parallel
= stmt
->n_parallel
;
3116 void ppcg_kernel_stmt_free(void *user
)
3119 struct ppcg_kernel_stmt
*stmt
= user
;
3124 switch (stmt
->type
) {
3125 case ppcg_kernel_copy
:
3126 isl_ast_expr_free(stmt
->u
.c
.index
);
3127 isl_ast_expr_free(stmt
->u
.c
.local_index
);
3129 case ppcg_kernel_domain
:
3130 for (i
= 0; i
< stmt
->u
.d
.n_access
; ++i
) {
3131 isl_ast_expr_list_free(stmt
->u
.d
.access
[i
].index
);
3132 free(stmt
->u
.d
.access
[i
].local_name
);
3134 free(stmt
->u
.d
.access
);
3136 case ppcg_kernel_sync
:
3143 /* Set the options of "context" to
3145 * { space -> [x] : x >= first }
3147 static __isl_give isl_ast_build
*set_unroll(
3148 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
,
3155 ctx
= isl_ast_build_get_ctx(build
);
3157 space
= isl_space_from_domain(space
);
3158 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
3159 space
= isl_space_set_tuple_name(space
, isl_dim_out
, "unroll");
3160 unroll
= isl_map_universe(space
);
3161 unroll
= isl_map_lower_bound_si(unroll
, isl_dim_out
, 0, first
);
3162 opt
= isl_union_map_from_map(unroll
);
3164 build
= isl_ast_build_set_options(build
, opt
);
3169 /* Return a list of isl_ids of the form "prefix%d".
3171 static __isl_give isl_id_list
*generate_names(isl_ctx
*ctx
,
3172 int n
, const char *prefix
)
3178 names
= isl_id_list_alloc(ctx
, n
);
3179 for (i
= 0; i
< n
; ++i
) {
3182 snprintf(name
, sizeof(name
), "%s%d", prefix
, i
);
3183 id
= isl_id_alloc(ctx
, name
, NULL
);
3184 names
= isl_id_list_add(names
, id
);
3190 /* Extend the schedule "schedule" with the part of "extension"
3191 * starting at "first" up to "len".
3193 static __isl_give isl_union_map
*extend_schedule(
3194 __isl_take isl_union_map
*schedule
,
3195 __isl_take isl_union_map
*extension
, int first
, int len
)
3199 isl_union_map
*umap
;
3202 space
= isl_union_map_get_space(schedule
);
3203 space
= isl_space_set_from_params(space
);
3204 space
= isl_space_add_dims(space
, isl_dim_set
, len
);
3205 proj
= isl_set_identity(isl_set_universe(space
));
3206 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, first
);
3207 extension
= isl_union_map_apply_range(extension
,
3208 isl_union_map_from_map(proj
));
3210 schedule
= isl_union_map_range_product(schedule
, extension
);
3215 /* This function is called for each access to an array in each instance
3216 * in the kernel of some statement in the original code.
3217 * Replace that access by an access to global, shared or private memory
3218 * and store the results in *kernel_access.
3220 * Since the array in shared or private memory is just
3221 * a shifted copy of part of the original array, we simply need
3222 * to subtract the lower bound, which was computed in can_tile.
3223 * If any of the indices is strided, then we first add
3224 * shared_tile->bound[i].shift and divide by shared_tile->bound[i].stride.
3226 * If the given array is accessed directly from global memory,
3227 * we don't need to perform any shifting and simply simplify
3228 * the expression in the context of the domain instead.
3230 * If the array space (range of access) has no name, then we are
3231 * accessing an iterator in the original program.
3233 * The input stmt_access->access relation maps the iteration domain
3234 * of the current statement to an array element.
3235 * The first step is to reformulate
3236 * this access relation in terms of the loop iterators of the generated
3237 * code through precomposition with gen->stmt_it.
3239 * The expressions in "tile" are formulated in terms of the first
3240 * gen->shared_len dimensions of the computed schedule using the mapping
3241 * sched2shared which maps the loop iterators to these dimensions.
3243 static void compute_index_expression(struct gpu_gen
*gen
,
3244 struct ppcg_kernel_access
*kernel_access
,
3245 struct gpu_stmt_access
*stmt_access
, __isl_keep isl_map
*stmt_it
,
3246 __isl_keep isl_map
*sched2shared
, __isl_keep isl_ast_build
*build
)
3249 isl_pw_multi_aff
*pma
;
3252 struct gpu_array_tile
*tile
= NULL
;
3254 if (isl_map_has_tuple_name(stmt_access
->access
, isl_dim_out
)) {
3257 struct gpu_array_ref_group
*group
;
3260 name
= isl_map_get_tuple_name(stmt_access
->access
, isl_dim_out
);
3262 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
3263 if (strcmp(name
, gen
->prog
->array
[i
].name
))
3265 kernel_access
->array
= &gen
->prog
->array
[i
];
3266 kernel_access
->local_array
= &gen
->kernel
->array
[i
];
3268 assert(kernel_access
->array
);
3269 group
= kernel_access
->array
->groups
[stmt_access
->group
];
3270 p
= isl_printer_to_str(gen
->ctx
);
3271 p
= print_array_name(p
, group
);
3272 kernel_access
->local_name
= isl_printer_get_str(p
);
3273 isl_printer_free(p
);
3274 tile
= group
->private_tile
;
3275 kernel_access
->type
= ppcg_access_private
;
3277 tile
= group
->shared_tile
;
3278 kernel_access
->type
= ppcg_access_shared
;
3282 kernel_access
->type
= ppcg_access_global
;
3284 n_index
= isl_map_dim(stmt_access
->access
, isl_dim_out
);
3285 kernel_access
->index
= isl_ast_expr_list_alloc(gen
->ctx
, n_index
);
3290 access
= isl_map_copy(stmt_access
->access
);
3291 access
= isl_map_apply_range(isl_map_copy(stmt_it
), access
);
3292 pma
= isl_pw_multi_aff_from_map(access
);
3293 pma
= isl_pw_multi_aff_coalesce(pma
);
3295 for (i
= 0; i
< n_index
; ++i
) {
3300 index
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
3302 if (!kernel_access
->array
) {
3304 domain
= isl_map_domain(isl_map_copy(stmt_it
));
3305 index
= isl_pw_aff_coalesce(index
);
3306 index
= isl_pw_aff_gist(index
, domain
);
3308 domain
= isl_map_domain(isl_map_copy(stmt_it
));
3309 index
= shift_index(index
, kernel_access
->array
,
3310 &tile
->bound
[i
], domain
,
3311 isl_map_copy(sched2shared
));
3314 expr
= isl_ast_build_expr_from_pw_aff(build
, index
);
3316 kernel_access
->index
= isl_ast_expr_list_add(
3317 kernel_access
->index
, expr
);
3320 isl_pw_multi_aff_free(pma
);
3323 /* This function is called for each instance of a user statement
3326 * We attach a struct ppcg_kernel_stmt to the "node", containing
3327 * local information about the accesses.
3328 * This information is computed from stmt_it, which expresses the domain
3329 * elements in terms of the generated loops, and sched2shared,
3330 * which expresses the first shared_len dimensions of the schedule
3331 * computed by PPCG in terms of the generated loops.
3333 static __isl_give isl_ast_node
*at_each_domain(__isl_take isl_ast_node
*node
,
3334 __isl_keep isl_ast_build
*build
, void *user
)
3336 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3337 struct ppcg_kernel_stmt
*stmt
;
3339 isl_map
*stmt_it
, *sched2shared
;
3340 isl_ast_expr
*expr
, *arg
;
3341 isl_union_map
*schedule
;
3343 struct gpu_stmt_access
*access
;
3345 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3347 return isl_ast_node_free(node
);
3349 expr
= isl_ast_node_user_get_expr(node
);
3350 arg
= isl_ast_expr_get_op_arg(expr
, 0);
3351 id
= isl_ast_expr_get_id(arg
);
3353 schedule
= isl_ast_build_get_schedule(build
);
3354 stmt_it
= isl_map_reverse(isl_map_from_union_map(schedule
));
3355 sched2shared
= compute_sched_to_shared(gen
, isl_map_copy(stmt_it
));
3357 stmt
->type
= ppcg_kernel_domain
;
3358 stmt
->u
.d
.stmt
= find_stmt(gen
->prog
, id
);
3359 if (!stmt
->u
.d
.stmt
)
3363 for (access
= stmt
->u
.d
.stmt
->accesses
; access
; access
= access
->next
)
3366 stmt
->u
.d
.access
= isl_calloc_array(gen
->ctx
,
3367 struct ppcg_kernel_access
, n
);
3368 if (!stmt
->u
.d
.access
)
3371 stmt
->u
.d
.n_access
= n
;
3373 access
= stmt
->u
.d
.stmt
->accesses
;
3374 for (i
= 0; i
< n
; ++i
, access
= access
->next
) {
3375 compute_index_expression(gen
, &stmt
->u
.d
.access
[i
], access
,
3376 stmt_it
, sched2shared
, build
);
3380 isl_map_free(stmt_it
);
3381 isl_map_free(sched2shared
);
3382 isl_ast_expr_free(arg
);
3383 isl_ast_expr_free(expr
);
3385 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3386 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3387 return isl_ast_node_set_annotation(node
, id
);
3390 isl_map_free(stmt_it
);
3391 ppcg_kernel_stmt_free(stmt
);
3392 isl_map_free(sched2shared
);
3393 return isl_ast_node_free(node
);
3396 /* This function is called when code has been generated for the shared
3397 * tile loops. The "schedule" refers only to the original statements.
3399 * We extend the schedule with that part of gen->local_sched that hasn't
3400 * been taken into account yet. This introduces parameters referring
3401 * to thread ids in the schedule, so we add them (with the appropriate
3402 * bounds to the context as well).
3403 * Finally, we set the appropriate unrolling options
3404 * if gen->first_unroll is set.
3406 static __isl_give isl_ast_node
*create_domain_leaf(
3407 __isl_take isl_union_map
*schedule
, __isl_take isl_ast_build
*build
,
3410 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3412 isl_union_map
*sched
;
3415 isl_id_list
*iterators
;
3418 schedule
= extend_schedule(schedule
,
3419 isl_union_map_copy(gen
->local_sched
),
3420 gen
->shared_len
, gen
->thread_tiled_len
);
3422 space
= isl_ast_build_get_schedule_space(build
);
3423 set
= isl_set_universe(space
);
3424 set
= add_bounded_parameters(set
, gen
->n_block
, gen
->block_dim
, "t");
3425 build
= isl_ast_build_restrict(build
, set
);
3427 n
= gen
->thread_tiled_len
- gen
->shared_len
;
3429 if (gen
->first_unroll
>= 0) {
3430 space
= isl_space_set_alloc(gen
->ctx
, 0, n
);
3431 build
= set_unroll(build
, space
, gen
->first_unroll
);
3433 iterators
= generate_names(gen
->ctx
, n
, "c");
3434 build
= isl_ast_build_set_iterators(build
, iterators
);
3435 build
= isl_ast_build_set_at_each_domain(build
, &at_each_domain
, gen
);
3436 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
3437 isl_ast_build_free(build
);
3442 /* This function is called for each leaf in the AST of the code
3443 * for copying to or from shared/private memory.
3444 * The statement name is {read,write}_{shared,private}_<array>.
3446 * The schedule is of the form
3450 * where A refers to a piece of an array and T to the corresponding
3451 * shifted tile. We split this schedule into mappings L -> A and L -> T
3452 * and store the corresponding expressions in stmt->index and stmt->local_index,
3453 * where stmt represents the copy statement.
3455 static __isl_give isl_ast_node
*create_copy_leaf(
3456 __isl_take isl_ast_build
*build
, void *user
)
3458 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3459 struct ppcg_kernel_stmt
*stmt
;
3464 isl_map
*access
, *local_access
, *map
;
3465 isl_pw_multi_aff
*pma
;
3469 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3471 return isl_ast_build_free(build
);
3473 access
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
3474 name
= isl_map_get_tuple_name(access
, isl_dim_in
);
3475 stmt
->u
.c
.read
= !strncmp(name
, "read", 4);
3476 access
= isl_map_reverse(access
);
3477 space
= isl_space_unwrap(isl_space_range(isl_map_get_space(access
)));
3478 local_access
= isl_map_copy(access
);
3480 map
= isl_map_domain_map(isl_map_universe(isl_space_copy(space
)));
3481 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
3482 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
3483 access
= isl_map_apply_range(access
, map
);
3484 pma
= isl_pw_multi_aff_from_map(access
);
3485 expr
= isl_ast_build_call_from_pw_multi_aff(build
, pma
);
3486 stmt
->u
.c
.index
= expr
;
3488 map
= isl_map_range_map(isl_map_universe(space
));
3489 id
= isl_map_get_tuple_id(local_access
, isl_dim_out
);
3490 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
3491 local_access
= isl_map_apply_range(local_access
, map
);
3492 pma
= isl_pw_multi_aff_from_map(local_access
);
3493 expr
= isl_ast_build_call_from_pw_multi_aff(build
, pma
);
3494 stmt
->u
.c
.local_index
= expr
;
3496 stmt
->u
.c
.array
= gen
->copy_group
->array
;
3497 array_index
= stmt
->u
.c
.array
- gen
->prog
->array
;
3498 stmt
->u
.c
.local_array
= &gen
->kernel
->array
[array_index
];
3499 stmt
->type
= ppcg_kernel_copy
;
3501 space
= isl_ast_build_get_schedule_space(build
);
3502 space
= isl_space_from_domain(space
);
3503 space
= isl_space_set_tuple_name(space
, isl_dim_out
, name
);
3504 expr
= isl_ast_build_call_from_pw_multi_aff(build
,
3505 isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
)));
3506 node
= isl_ast_node_alloc_user(expr
);
3507 isl_ast_build_free(build
);
3509 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3510 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3511 return isl_ast_node_set_annotation(node
, id
);
3514 /* Given a schedule of the form
3518 * (with S the first shared_len dimensions of the computed schedule,
3519 * A the array and L the schedule correponding to the generated loops),
3520 * indicating where the copying the array elements that need to be copied,
3521 * construct code for performing the copying.
3523 * "group" is the array reference group that is being copied
3524 * "type" is either "read" or "write"
3525 * private is set if copying needs to be performed to/from registers
3527 * We first construct a mapping to a shifted tile of the array,
3529 * [S -> A] -> T(S,A) (1)
3531 * If private is set, then we also use this mapping as a schedule
3532 * (which is already thread-specific and will be completely unrolled).
3533 * Otherwise, we wrap/tile the range over the threads.
3536 * [S -> A] -> T'(S,A)
3538 * Combined with the given schedule, we have
3540 * [S -> A] -> [L -> T'(S,A)] (2)
3542 * From the shifted tile mapping, we construct a mapping
3544 * [S -> A] -> [A -> T(S,A)]
3546 * and apply it to the schedule (2), obtaining
3548 * [A -> T(S(L),A)] -> [L -> T'(S(L),A)]
3550 * Note that we can project out S because it is uniquely defined by L.
3552 static __isl_give isl_ast_node
*copy_access(struct gpu_gen
*gen
,
3553 __isl_take isl_map
*sched
,
3554 const char *type
, struct gpu_array_ref_group
*group
,
3555 __isl_take isl_ast_build
*build
, int private)
3557 const char *array_name
;
3558 const char *mem
= private ? "private" : "shared";
3562 isl_map
*schedule
, *shift
, *map
;
3564 isl_id_list
*iterators
;
3567 shift
= isl_set_unwrap(isl_map_domain(isl_map_copy(sched
)));
3568 array_name
= isl_map_get_tuple_name(shift
, isl_dim_out
);
3569 shift
= shift_access(shift
, group
);
3571 schedule
= isl_map_copy(shift
);
3573 schedule
= tile_access_schedule(gen
, schedule
);
3575 n
= isl_map_dim(schedule
, isl_dim_out
);
3576 set
= isl_set_universe(isl_ast_build_get_schedule_space(build
));
3577 set
= add_bounded_parameters(set
, gen
->n_block
, gen
->block_dim
, "t");
3579 schedule
= isl_map_range_product(sched
, schedule
);
3582 name
= isl_alloc_array(gen
->ctx
, char,
3583 strlen(type
) + sizeof("_private_") + strlen(array_name
) + 20);
3584 if (group
->array
->n_group
> 1)
3585 sprintf(name
, "%s_%s_%s_%d", type
, mem
, array_name
, group
->nr
);
3587 sprintf(name
, "%s_%s_%s", type
, mem
, array_name
);
3588 shift
= isl_map_set_tuple_name(shift
,
3589 isl_dim_out
, name
+ strlen(type
) + 1);
3591 space
= isl_space_domain(isl_map_get_space(shift
));
3592 map
= isl_map_range_map(isl_map_universe(isl_space_unwrap(space
)));
3593 map
= isl_map_range_product(map
, shift
);
3595 schedule
= isl_map_apply_domain(schedule
, map
);
3597 schedule
= isl_map_set_tuple_name(schedule
, isl_dim_in
, name
);
3600 build
= isl_ast_build_restrict(build
, set
);
3602 gen
->copy_group
= group
;
3605 space
= isl_space_range(isl_map_get_space(schedule
));
3606 space
= isl_space_range(isl_space_unwrap(space
));
3607 build
= set_unroll(build
, space
, 0);
3609 iterators
= generate_names(gen
->ctx
, n
, "c");
3610 build
= isl_ast_build_set_iterators(build
, iterators
);
3611 build
= isl_ast_build_set_create_leaf(build
, &create_copy_leaf
, gen
);
3612 tree
= isl_ast_build_ast_from_schedule(build
,
3613 isl_union_map_from_map(schedule
));
3614 isl_ast_build_free(build
);
3619 /* Return code for reading into or writing from shared memory
3620 * the given array reference group.
3622 * If we are performing a read from global memory to shared memory and
3623 * if the array involved is not a scalar, then we copy
3624 * the entire tile to shared memory. This may result in some extra
3625 * elements getting copied, but it should lead to simpler code
3626 * (which means that fewer registers may be needed) and less divergence.
3628 * Otherwise, we only copy the elements that will be read or have been written
3632 * The input "sched" is of the form.
3636 * with S the first shared_len dimensions of the computed schedule,
3637 * A the array and L the schedule correponding to the generated loops.
3639 * We first drop "type",
3643 * If the above conditions are satisfied, we project out A,
3648 * and then introduce the group tile [S -> T], resulting in
3652 static __isl_give isl_ast_node
*copy_group_shared_accesses(
3653 struct gpu_gen
*gen
, struct gpu_array_ref_group
*group
,
3654 __isl_take isl_map
*sched
, __isl_take isl_ast_build
*build
)
3658 isl_union_map
*access
;
3660 type
= isl_map_get_tuple_name(sched
, isl_dim_in
);
3661 read
= !strcmp(type
, "read");
3663 sched
= isl_map_reset_tuple_id(sched
, isl_dim_in
);
3665 if (read
&& group
->array
->n_index
> 0) {
3669 space
= isl_space_domain(isl_map_get_space(sched
));
3670 space
= isl_space_unwrap(space
);
3671 map
= isl_map_domain_map(isl_map_universe(space
));
3672 sched
= isl_map_apply_domain(sched
, map
);
3674 map
= group_tile(group
);
3675 map
= isl_map_reverse(isl_map_domain_map(map
));
3676 sched
= isl_map_apply_domain(sched
, map
);
3679 return copy_access(gen
, sched
, type
, group
, build
, 0);
3682 /* Return code for reading into or writing from private memory
3683 * the given array reference group.
3685 * Let S be the first shared_len dimensions of the computed schedule,
3686 * D the iteration domains, A the array and L the schedule correponding
3687 * to the generated loops.
3688 * "sched" is of the form
3692 * where type is either "read" or "write".
3693 * We apply the privatization D -> S(t), with t the thread ids,
3694 * to the access relation D -> A to obtain the privatized access relation
3698 * We drop the type from "sched" and intersect with the privatized access
3699 * relation to obtain
3703 static __isl_give isl_ast_node
*copy_group_private_accesses(
3704 struct gpu_gen
*gen
, struct gpu_array_ref_group
*group
,
3705 __isl_take isl_map
*sched
, __isl_take isl_ast_build
*build
)
3709 isl_union_map
*priv
;
3710 isl_union_map
*access
;
3711 isl_map
*access_map
;
3713 type
= isl_map_get_tuple_name(sched
, isl_dim_in
);
3714 read
= !strcmp(type
, "read");
3716 priv
= isl_union_map_from_map(isl_map_copy(gen
->privatization
));
3717 priv
= isl_union_map_apply_range(isl_union_map_copy(gen
->shared_sched
),
3720 access
= group_access_relation(group
, read
, !read
);
3721 access
= isl_union_map_apply_domain(access
, priv
);
3722 access_map
= isl_map_from_union_map(access
);
3724 sched
= isl_map_reset_tuple_id(sched
, isl_dim_in
);
3725 sched
= isl_map_intersect_domain(sched
, isl_map_wrap(access_map
));
3727 return copy_access(gen
, sched
, type
, group
, build
, 1);
3730 /* Return code for reading into or writing from shared or private memory.
3732 * "schedule" is of the form
3736 * with S be the first shared_len dimensions of the computed schedule,
3737 * A the array and L the schedule correponding to the generated loops.
3738 * The array reference group is attached to "type".
3740 static __isl_give isl_ast_node
*create_access_leaf(
3741 struct gpu_gen
*gen
, __isl_take isl_map
*schedule
,
3742 __isl_take isl_ast_build
*build
)
3744 struct gpu_array_ref_group
*group
;
3747 id
= isl_map_get_tuple_id(schedule
, isl_dim_in
);
3748 group
= isl_id_get_user(id
);
3751 if (group
->private_tile
)
3752 return copy_group_private_accesses(gen
, group
, schedule
,
3755 return copy_group_shared_accesses(gen
, group
, schedule
,
3759 /* Create a domain node representing a synchronization.
3761 static __isl_give isl_ast_node
*create_sync_leaf(
3762 struct gpu_gen
*gen
, __isl_take isl_map
*schedule
,
3763 __isl_take isl_ast_build
*build
)
3765 struct ppcg_kernel_stmt
*stmt
;
3771 isl_map_free(schedule
);
3773 stmt
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel_stmt
);
3777 stmt
->type
= ppcg_kernel_sync
;
3779 space
= isl_ast_build_get_schedule_space(build
);
3780 space
= isl_space_from_domain(space
);
3781 space
= isl_space_set_tuple_name(space
, isl_dim_out
, "sync");
3782 expr
= isl_ast_build_call_from_pw_multi_aff(build
,
3783 isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
)));
3784 node
= isl_ast_node_alloc_user(expr
);
3785 isl_ast_build_free(build
);
3787 id
= isl_id_alloc(gen
->ctx
, NULL
, stmt
);
3788 id
= isl_id_set_free_user(id
, &ppcg_kernel_stmt_free
);
3789 return isl_ast_node_set_annotation(node
, id
);
3792 /* This function is called during the code generation at the point
3793 * where the schedule domain element is completely determined by
3794 * the generated code. The input schedule contains the original
3795 * statements as well as synchronization and copy "statements".
3796 * The latter are scheduled at different points than any of the original
3797 * statements, so they will only arrive here in isolation.
3799 * If the current schedule only refers to a single statement,
3800 * we check if it is a copy or synchronization statement and
3801 * call the appropriate functions.
3802 * Otherwise, we assume we are dealing with the original statements
3803 * and we call create_domain_leaf.
3805 static __isl_give isl_ast_node
*create_kernel_leaf(
3806 __isl_take isl_ast_build
*build
, void *user
)
3808 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
3810 isl_union_map
*schedule
;
3813 schedule
= isl_ast_build_get_schedule(build
);
3815 if (isl_union_map_n_map(schedule
) != 1)
3816 return create_domain_leaf(schedule
, build
, user
);
3818 map
= isl_map_from_union_map(schedule
);
3819 name
= isl_map_get_tuple_name(map
, isl_dim_in
);
3820 if (!strcmp(name
, "read") || !strcmp(name
, "write"))
3821 return create_access_leaf(gen
, map
, build
);
3822 if (!strcmp(name
, "sync"))
3823 return create_sync_leaf(gen
, map
, build
);
3825 return create_domain_leaf(isl_union_map_from_map(map
), build
, user
);
3828 /* Mark all odd schedule dimensions as "atomic" (when the even dimensions
3829 * have value 0) and all even schedule dimensions as "unroll".
3831 * That is, the options look as follows
3833 * { [0, b, 0, d, ..., 0] -> atomic[i] : exists a : i = 2 a + 1;
3834 * [a, b, c, d, ..., z] -> unroll[i] : exists a : i = 2 a }
3836 * The even positions are used to be able to schedule copying blocks
3837 * and synchronization before or after each level of the shared memory
3838 * tile loops and we want to make sure that code for these is generated
3839 * separately (within each level).
3841 static __isl_give isl_ast_build
*set_atomic_and_unroll(
3842 __isl_take isl_ast_build
*build
,
3843 __isl_take isl_space
*space
, int sched_len
)
3849 isl_local_space
*ls
;
3852 ctx
= isl_ast_build_get_ctx(build
);
3854 space
= isl_space_params(space
);
3855 space
= isl_space_add_dims(space
, isl_dim_set
, sched_len
);
3856 space
= isl_space_from_domain(space
);
3857 space
= isl_space_add_dims(space
, isl_dim_out
, 2);
3858 map
= isl_map_universe(isl_space_copy(space
));
3859 for (i
= 0; i
< sched_len
; i
+= 2)
3860 map
= isl_map_fix_si(map
, isl_dim_in
, i
, 0);
3861 ls
= isl_local_space_from_space(isl_map_get_space(map
));
3862 c
= isl_equality_alloc(ls
);
3863 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
3864 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
3865 c
= isl_constraint_set_constant_si(c
, 1);
3866 map
= isl_map_add_constraint(map
, c
);
3867 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
3868 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "atomic");
3869 opt
= isl_union_map_from_map(map
);
3871 map
= isl_map_universe(space
);
3872 ls
= isl_local_space_from_space(isl_map_get_space(map
));
3873 c
= isl_equality_alloc(ls
);
3874 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, 1);
3875 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 1, 2);
3876 map
= isl_map_add_constraint(map
, c
);
3877 map
= isl_map_project_out(map
, isl_dim_out
, 1, 1);
3878 map
= isl_map_set_tuple_name(map
, isl_dim_out
, "unroll");
3879 opt
= isl_union_map_add_map(opt
, map
);
3881 build
= isl_ast_build_set_options(build
, opt
);
3886 /* Return a map that maps a space of dimension gen->shared_len
3887 * to its last dimensions starting at gen->tile_first.
3888 * The range is of dimension
3890 * 2 * (gen->shared_len - gen->tile_first) + 1
3892 * The input dimensions are mapped to the odd dimensions in the output,
3893 * while the even dimensions (except 2*pos) are fixed to 0.
3894 * Output dimension 2*pos (if pos >= 0) is fixed to "val".
3895 * If pos >= 0, then only the pos first dimensions starting at gen->tile_first
3896 * are mapped to the output. The remaining input dimensions are projected
3897 * out and the corresponding output dimensions are fixed to 0.
3899 static __isl_give isl_map
*insert_even(struct gpu_gen
*gen
,
3900 __isl_take isl_space
*space
, int pos
, int val
)
3905 space
= isl_space_set_from_params(space
);
3906 space
= isl_space_add_dims(space
, isl_dim_set
, gen
->shared_len
);
3907 space
= isl_space_map_from_set(space
);
3908 proj
= isl_map_identity(space
);
3909 proj
= isl_map_project_out(proj
, isl_dim_out
, 0, gen
->tile_first
);
3910 n
= gen
->shared_len
- gen
->tile_first
;
3911 for (i
= 0; i
<= n
; ++i
) {
3912 proj
= isl_map_insert_dims(proj
, isl_dim_out
, 2 * i
, 1);
3914 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, val
);
3916 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
, 0);
3922 proj
= isl_map_eliminate(proj
, isl_dim_in
, gen
->tile_first
+ pos
,
3923 gen
->shared_len
- (gen
->tile_first
+ pos
));
3924 for (i
= pos
; i
< n
; ++i
)
3925 proj
= isl_map_fix_si(proj
, isl_dim_out
, 2 * i
+ 1, 0);
3930 /* Given the AST context schedule "schedule" and the mapping from
3931 * domains to the shared tile loops "shared_sched", add a schedule
3932 * for a synchronization operation at position "val" of loop level "pos".
3934 * schedule is of the form
3938 * (with D the iteration domains and L the already generated loops),
3939 * while shared_sched is of the form
3943 * We combine them into
3949 * [s_0,...] -> [0,s_{tile_first},0,..., val, 0, 0, ... 0]
3951 * and use the result as a schedule for "sync".
3953 static __isl_give isl_union_map
*add_sync_schedule(struct gpu_gen
*gen
,
3954 __isl_take isl_union_map
*res
, __isl_keep isl_union_map
*schedule
,
3955 __isl_keep isl_union_map
*shared_sched
, int pos
, int val
)
3958 isl_map
*proj
, *map
;
3960 shared_sched
= isl_union_map_copy(shared_sched
);
3961 schedule
= isl_union_map_copy(schedule
);
3963 space
= isl_union_map_get_space(shared_sched
);
3964 schedule
= isl_union_map_apply_domain(shared_sched
, schedule
);
3965 map
= isl_map_from_union_map(schedule
);
3967 proj
= insert_even(gen
, space
, pos
, val
);
3968 map
= isl_map_apply_range(map
, proj
);
3969 map
= isl_map_from_range(isl_map_wrap(map
));
3970 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "sync");
3972 res
= isl_union_map_add_map(res
, map
);
3977 /* Given the AST context schedule "schedule" and the mapping from
3978 * domains to the shared tile loops "shared_sched", add a schedule
3979 * for copying an array reference group to/from shared/private memory.
3980 * "read" is set if data should be copied from global memory
3981 * to shared/private memory.
3982 * "k" represents the current group
3983 * "s" is the total number of groups
3985 * We schedule an operation before or after the innermost loop
3986 * of "shared_sched" that affects the tile of the array reference group.
3988 * schedule is of the form
3992 * (with D the iteration domains and L the already generated loops),
3993 * while shared_sched is of the form
3997 * We first compute the access relation for the reference group
4001 * and combine it with shared_sched into
4005 * If this results in an empty relation, no copying needs to be performed
4007 * Otherwise, we invert the relation and combine it with "schedule" into
4011 * The actual additional piece of the schedule is obtained from combining
4017 * [s_0,...] -> [0,s_{tile_first},0,..., val, 0, 0, ... 0]
4019 * The position of "val" corresponds to the innermost loop that affects
4020 * the tile and the value indicates where the copying is scheduled
4021 * with respect to the actual kernel code (at value 0).
4022 * Reads are schedule before the code, writes to global memory from
4023 * private memory are scheduled at values 1 to s, writes to global
4024 * memory from shared memory are scheduled at values s + 2 to 2 * s + 1.
4026 * If we are scheduling a read from global memory to shared memory,
4027 * we insert a synchronization before the kernel code (at the innermost
4029 * If we are scheduling a write to global memory, then we add
4030 * a synchronization after all writes (at value 2 *s + 2).
4031 * However, there is no need for a synchronization after the outermost loop.
4032 * A write to global memory from private memory at the innermost level
4033 * does not require a synchronization, because it is covered by
4034 * the synchronization after the kernel inserted by body_schedule.
4036 static __isl_give isl_union_map
*add_group_schedule(struct gpu_gen
*gen
,
4037 __isl_take isl_union_map
*res
, __isl_keep isl_union_map
*schedule
,
4038 __isl_keep isl_union_map
*shared_sched
,
4039 struct gpu_array_ref_group
*group
, int read
, int k
, int s
)
4044 isl_union_map
*access
;
4045 isl_map
*map
, *proj
, *access_map
;
4048 access
= group_access_relation(group
, read
, !read
);
4049 access
= isl_union_map_range_product(isl_union_map_copy(shared_sched
),
4052 if (isl_union_map_is_empty(access
)) {
4053 isl_union_map_free(access
);
4057 access
= isl_union_map_reverse(access
);
4058 access
= isl_union_map_apply_range(access
,
4059 isl_union_map_copy(schedule
));
4060 access_map
= isl_map_from_union_map(access
);
4062 space
= isl_space_copy(group
->array
->dim
);
4063 space
= isl_space_from_range(space
);
4064 space
= isl_space_add_dims(space
, isl_dim_in
, gen
->shared_len
);
4065 map
= isl_map_domain_map(isl_map_universe(space
));
4067 space
= isl_union_map_get_space(schedule
);
4068 pos
= group
->last_shared
+ 1 - gen
->tile_first
;
4072 else if (group
->private_tile
)
4075 val
= 1 + s
+ 1 + k
;
4076 proj
= insert_even(gen
, space
, pos
, val
);
4077 map
= isl_map_apply_range(map
, proj
);
4079 access_map
= isl_map_range_product(access_map
, map
);
4081 id
= isl_id_alloc(gen
->ctx
, read
? "read" : "write", group
);
4082 access_map
= isl_map_set_tuple_id(access_map
, isl_dim_in
, id
);
4084 res
= isl_union_map_add_map(res
, access_map
);
4086 n
= gen
->shared_len
- gen
->tile_first
;
4088 if (!group
->private_tile
)
4089 res
= add_sync_schedule(gen
, res
, schedule
,
4090 shared_sched
, n
, -1);
4094 if (pos
== n
&& group
->private_tile
)
4096 res
= add_sync_schedule(gen
, res
, schedule
, shared_sched
,
4103 /* Return a schedule for the shared tile loops based on the current
4104 * AST context schedule.
4106 * We create a "shared_sched" that maps the domains to the first
4107 * shared_len dimensions of the computed schedule, project out the
4108 * first tile_first dimensions (as these are already covered by
4109 * the host code) and insert "statement-level" dimensions at even
4110 * positions so that we can schedule copy blocks and synchronization
4111 * before/after each level.
4113 * In particular, copy blocks are inserted inside the innermost
4114 * level that affect the tile. For the copying to global memory,
4115 * those from private memory are scheduled before those from shared
4116 * memory such that synchronization can be inserted between the two
4117 * at the innermost level.
4118 * Synchronization is inserted at the innermost level before the
4119 * actual kernel code if there is any copying from global memory
4120 * to shared memory. It is inserted unconditionally at the innermost
4121 * level after the actual kernel code and the copying to global memory
4122 * from private memory (if any). Finally, it is inserted after
4123 * any copying to global memory, except at the outermost level
4124 * and at the innermost level if there is no copying from shared
4125 * memory. The copying from private memory is covered by the unconditional
4126 * synchronization at the innermost level.
4128 static __isl_give isl_union_map
*body_schedule(struct gpu_gen
*gen
,
4129 __isl_take isl_union_map
*schedule
)
4133 isl_union_map
*shared_sched
;
4134 isl_union_map
*sched
;
4135 isl_map
*proj
, *map
;
4138 shared_sched
= isl_union_map_copy(gen
->tiled_sched
);
4139 proj
= projection(isl_union_map_get_space(shared_sched
),
4140 gen
->tiled_len
, gen
->shared_len
);
4141 shared_sched
= isl_union_map_apply_range(shared_sched
,
4142 isl_union_map_from_map(proj
));
4143 space
= isl_union_map_get_space(shared_sched
);
4144 proj
= insert_even(gen
, space
, -1, 0);
4145 sched
= isl_union_map_apply_range(isl_union_map_copy(shared_sched
),
4146 isl_union_map_from_map(proj
));
4148 res
= isl_union_map_range_product(isl_union_map_copy(schedule
), sched
);
4151 for (i
= 0; i
< gen
->prog
->n_array
; ++i
)
4152 s
+= gen
->prog
->array
[i
].n_group
;
4155 for (i
= 0; i
< gen
->prog
->n_array
; ++i
) {
4156 struct gpu_array_info
*array
= &gen
->prog
->array
[i
];
4158 for (j
= 0; j
< array
->n_group
; ++j
) {
4159 struct gpu_array_ref_group
*group
;
4161 group
= array
->groups
[j
];
4162 if (!group
->private_tile
&& !group
->shared_tile
)
4164 res
= add_group_schedule(gen
, res
, schedule
,
4165 shared_sched
, group
, 0, k
, s
);
4166 res
= add_group_schedule(gen
, res
, schedule
,
4167 shared_sched
, group
, 1, k
, s
);
4172 res
= add_sync_schedule(gen
, res
, schedule
, shared_sched
,
4173 gen
->shared_len
- gen
->tile_first
, 1 + s
);
4175 isl_union_map_free(shared_sched
);
4176 isl_union_map_free(schedule
);
4181 /* Generate code for "kernel" in the given "context".
4183 * We first generate code for the shared tile loops (T1T, T1P and T2)
4184 * in a context that includes the block ids.
4185 * Within each iteration of these loops an additional code generation
4186 * is performed (within create_kernel_leaf) for the rest of the schedule
4187 * in a context that includes the thread ids.
4189 static __isl_give isl_ast_node
*generate_kernel(struct gpu_gen
*gen
,
4190 __isl_keep isl_ast_build
*build
, __isl_keep isl_set
*host_domain
,
4191 __isl_keep isl_multi_pw_aff
*grid_size
)
4195 isl_id_list
*iterators
;
4196 isl_union_map
*schedule
;
4200 schedule
= isl_ast_build_get_schedule(build
);
4202 build
= isl_ast_build_copy(build
);
4203 build
= isl_ast_build_restrict(build
, isl_set_copy(host_domain
));
4204 space
= isl_ast_build_get_schedule_space(build
);
4205 set
= isl_set_universe(isl_space_copy(space
));
4206 set
= add_bounded_parameters_dynamic(set
, grid_size
, "b");
4207 build
= isl_ast_build_restrict(build
, set
);
4209 schedule
= body_schedule(gen
, schedule
);
4211 sched_len
= 2 * (gen
->shared_len
- gen
->tile_first
) + 1;
4213 build
= set_atomic_and_unroll(build
, space
, sched_len
);
4214 iterators
= generate_names(gen
->ctx
, sched_len
, "g");
4215 build
= isl_ast_build_set_iterators(build
, iterators
);
4216 build
= isl_ast_build_set_create_leaf(build
, &create_kernel_leaf
, gen
);
4217 tree
= isl_ast_build_ast_from_schedule(build
, schedule
);
4218 isl_ast_build_free(build
);
4223 /* Attach "id" to the given node.
4225 static __isl_give isl_ast_node
*attach_id(__isl_take isl_ast_node
*node
,
4226 __isl_keep isl_ast_build
*build
, void *user
)
4230 node
= isl_ast_node_set_annotation(node
, id
);
4235 /* Construct an AST node for performing a kernel launch and attach
4236 * the information about the kernel to that node.
4238 * The kernel AST has been constructed in the context of the range
4239 * of "schedule". In particular, the grid size has been computed
4240 * in the context. We therefore still need to make sure that these
4241 * constraints are expressed in the code. We do this by creating a schedule
4243 * kernel[] -> [S -> []]
4245 * where S is the schedule domain, i.e., the range of "schedule".
4246 * The AST generation will then create a single call surrounded by
4247 * all the condition in "S" that have not been expressed yet.
4249 * The kernel information is attached to this node in attach_id.
4251 static __isl_give isl_ast_node
*construct_launch(
4252 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*schedule
,
4253 __isl_take
struct ppcg_kernel
*kernel
)
4257 isl_union_set
*domain
;
4262 ctx
= isl_ast_build_get_ctx(build
);
4264 id
= isl_id_alloc(ctx
, NULL
, kernel
);
4265 id
= isl_id_set_free_user(id
, &ppcg_kernel_free
);
4267 domain
= isl_union_map_range(schedule
);
4268 set
= isl_set_from_union_set(domain
);
4269 map
= isl_map_from_domain(set
);
4270 map
= isl_map_from_range(isl_map_wrap(map
));
4271 map
= isl_map_set_tuple_name(map
, isl_dim_in
, "kernel");
4272 schedule
= isl_union_map_from_map(map
);
4274 build
= isl_ast_build_set_at_each_domain(build
, &attach_id
, id
);
4275 node
= isl_ast_build_ast_from_schedule(build
, schedule
);
4276 isl_ast_build_free(build
);
4281 /* This function is called for each leaf in the AST of the host code.
4282 * We first specialize the schedule to the site of the leaf, compute
4283 * the size of shared memory and then construct the body of host code
4284 * and the associated kernel.
4286 * The necessary information for printing the kernel launch is
4287 * stored in a struct ppcg_kernel and attached to the leaf node
4288 * created to represent the launch.
4290 static __isl_give isl_ast_node
*create_host_leaf(
4291 __isl_take isl_ast_build
*build
, void *user
)
4293 struct gpu_gen
*gen
= (struct gpu_gen
*) user
;
4296 struct ppcg_kernel
*kernel
;
4297 isl_set
*host_domain
;
4298 isl_union_map
*schedule
;
4299 isl_union_map
*local_sched
;
4300 isl_union_map
*access
;
4301 isl_union_set
*domain
;
4304 schedule
= isl_ast_build_get_schedule(build
);
4306 isl_union_map_foreach_map(schedule
, &extract_tile_len
, gen
);
4309 domain
= isl_union_map_domain(isl_union_map_copy(schedule
));
4311 local_sched
= isl_union_map_copy(gen
->sched
);
4312 local_sched
= isl_union_map_intersect_domain(local_sched
, domain
);
4313 access
= isl_union_map_union(isl_union_map_copy(gen
->prog
->read
),
4314 isl_union_map_copy(gen
->prog
->write
));
4315 access
= isl_union_map_apply_domain(access
,
4316 isl_union_map_copy(local_sched
));
4318 gen
->tiled_sched
= tile_schedule(gen
, local_sched
);
4319 gen
->tiled_sched
= parametrize_tiled_schedule(gen
, gen
->tiled_sched
);
4320 gen
->tiled_sched
= scale_tile_loops(gen
, gen
->tiled_sched
);
4322 kernel
= gen
->kernel
= isl_calloc_type(gen
->ctx
, struct ppcg_kernel
);
4326 kernel
->id
= gen
->kernel_id
++;
4327 kernel
->n_block
= gen
->n_block
;
4328 for (i
= 0; i
< gen
->n_block
; ++i
)
4329 kernel
->block_dim
[i
] = gen
->block_dim
[i
];
4330 kernel
->n_grid
= gen
->n_grid
;
4331 for (i
= 0; i
< gen
->n_grid
; ++i
)
4332 kernel
->grid_dim
[i
] = gen
->grid_dim
[i
];
4333 kernel
->context
= isl_union_map_params(isl_union_map_copy(schedule
));
4334 kernel
->grid_size
= extract_grid_size(gen
, kernel
);
4335 kernel
->arrays
= isl_union_map_range(access
);
4336 kernel
->space
= isl_ast_build_get_schedule_space(build
);
4338 gen
->local_sched
= isl_union_map_copy(gen
->tiled_sched
);
4340 gen
->local_sched
= thread_tile_schedule(gen
, gen
->local_sched
);
4341 gen
->local_sched
= scale_thread_tile_loops(gen
, gen
->local_sched
);
4343 gen
->private_access
= NULL
;
4344 compute_shared_sched(gen
);
4345 gen
->privatization
= compute_privatization(gen
);
4346 group_references(gen
);
4347 compute_private_access(gen
);
4348 check_shared_memory_bound(gen
);
4349 host_domain
= isl_set_from_union_set(isl_union_map_range(
4350 isl_union_map_copy(schedule
)));
4351 localize_bounds(gen
, kernel
, host_domain
);
4353 gen
->local_sched
= interchange_for_unroll(gen
, gen
->local_sched
);
4355 kernel
->tree
= generate_kernel(gen
, build
, host_domain
,
4357 create_kernel_vars(gen
, kernel
);
4359 free_local_array_info(gen
);
4360 isl_map_free(gen
->privatization
);
4361 isl_union_map_free(gen
->private_access
);
4362 isl_union_map_free(gen
->local_sched
);
4363 isl_union_map_free(gen
->tiled_sched
);
4364 isl_union_map_free(gen
->shared_sched
);
4365 isl_union_map_free(gen
->shared_proj
);
4366 isl_set_free(host_domain
);
4367 free(gen
->tile_size
);
4369 node
= construct_launch(build
, schedule
, kernel
);
4373 isl_union_map_free(schedule
);
4377 /* Use isl to generate code for the outer gen->tile_first loops
4378 * of the global schedule in gen->sched, resulting in the host code.
4379 * Within each iteration of this partial schedule, i.e., for each kernel
4380 * launch, create_host_leaf takes care of generating the kernel code.
4382 static __isl_give isl_ast_node
*generate_host_code(struct gpu_gen
*gen
)
4384 isl_ast_build
*build
;
4386 isl_union_map
*sched
;
4388 isl_id_list
*iterators
;
4390 sched
= isl_union_map_copy(gen
->sched
);
4391 proj
= projection(isl_union_map_get_space(sched
),
4392 gen
->untiled_len
, gen
->tile_first
);
4393 sched
= isl_union_map_apply_range(sched
, isl_union_map_from_map(proj
));
4395 isl_options_set_ast_build_group_coscheduled(gen
->ctx
, 1);
4396 build
= isl_ast_build_from_context(isl_set_copy(gen
->prog
->context
));
4397 iterators
= generate_names(gen
->ctx
, gen
->tile_first
, "h");
4398 build
= isl_ast_build_set_iterators(build
, iterators
);
4399 build
= isl_ast_build_set_create_leaf(build
, &create_host_leaf
, gen
);
4400 tree
= isl_ast_build_ast_from_schedule(build
, sched
);
4401 isl_ast_build_free(build
);
4406 __isl_give isl_set
*add_context_from_str(__isl_take isl_set
*set
,
4415 ctx
= isl_set_get_ctx(set
);
4416 context
= isl_set_read_from_str(ctx
, str
);
4417 context
= isl_set_align_params(context
, isl_set_get_space(set
));
4418 set
= isl_set_intersect(set
, context
);
4423 __isl_give isl_union_map
*extract_sizes_from_str(isl_ctx
*ctx
, const char *str
)
4427 return isl_union_map_read_from_str(ctx
, str
);
4430 /* Information about the outermost tilable bands in the forest of bands.
4432 * tile_len and n_parallel are only sets on band_info structures
4433 * that correspond to outermost bands. For other bands (in particular,
4434 * ancestors of the outermost bands), n_parallal is set to 0.
4436 * prefix is the (padded) schedule leading up to the outermost tilable bands.
4438 * tile_first is the number of schedule dimensions in prefix.
4440 * suffix is the schedule of the outermost tilable bands and their descendants.
4443 struct gpu_gen
*gen
;
4447 isl_union_map
*prefix
;
4448 isl_union_map
*suffix
;
4451 /* Set tile_len and n_parallel of the statement to that of
4452 * their outermost band, recorded in the band_info.
4454 static int set_stmt_tile_len(__isl_take isl_map
*map
, void *user
)
4456 struct band_info
*info
= user
;
4457 struct gpu_stmt
*stmt
;
4460 id
= isl_map_get_tuple_id(map
, isl_dim_in
);
4461 stmt
= find_stmt(info
->gen
->prog
, id
);
4464 stmt
->tile_len
= info
->tile_len
;
4465 stmt
->n_parallel
= info
->n_parallel
;
4472 static void list_select_outer_band(struct gpu_gen
*gen
,
4473 __isl_take isl_band_list
*list
, int pos
, struct band_info
*list_info
);
4475 /* Check if this band has any parallel loops. If so, take it as
4476 * the outermost tilable band. If not, continue looking for the
4477 * outermost tilable band in the children of the current band.
4479 static void band_select_outer_band(struct gpu_gen
*gen
,
4480 __isl_take isl_band
*band
, int pos
, struct band_info
*info
)
4482 int n
= isl_band_n_member(band
);
4485 for (n_parallel
= 0; n_parallel
< n
; ++n_parallel
)
4486 if (!isl_band_member_is_zero_distance(band
, n_parallel
))
4489 info
->n_parallel
= n_parallel
;
4492 info
->tile_first
= pos
;
4494 info
->prefix
= isl_band_get_prefix_schedule(band
);
4495 info
->suffix
= isl_union_map_flat_range_product(
4496 isl_band_get_partial_schedule(band
),
4497 isl_band_get_suffix_schedule(band
));
4498 isl_union_map_foreach_map(info
->prefix
,
4499 &set_stmt_tile_len
, info
);
4500 } else if (isl_band_has_children(band
)) {
4501 isl_band_list
*children
;
4502 children
= isl_band_get_children(band
);
4503 list_select_outer_band(gen
, children
, pos
+ n
, info
);
4506 info
->tile_first
= pos
+ n
;
4508 info
->prefix
= isl_union_map_flat_range_product(
4509 isl_band_get_prefix_schedule(band
),
4510 isl_band_get_partial_schedule(band
));
4511 info
->suffix
= isl_band_get_suffix_schedule(band
);
4512 isl_union_map_foreach_map(info
->prefix
,
4513 &set_stmt_tile_len
, info
);
4516 isl_band_free(band
);
4519 /* Comparison function that returns a non-zero value for band_infos
4520 * with different tile_len fields or different n_parallel fields.
4522 static int cmp_band(const void *p1
, const void *p2
)
4524 const struct band_info
*info1
= p1
;
4525 const struct band_info
*info2
= p2
;
4527 if (info1
->tile_len
!= info2
->tile_len
)
4528 return info1
->tile_len
- info2
->tile_len
;
4530 return info1
->n_parallel
- info2
->n_parallel
;
4533 /* Extend "umap" with coordinates with fixed value "val"
4534 * to a total length of "dst_len", assuming the original dimension is "src_len".
4536 static __isl_give isl_union_map
*extend_range(
4537 __isl_take isl_union_map
*umap
, int src_len
, int dst_len
, int val
)
4543 dim
= isl_union_map_get_space(umap
);
4544 map
= isl_map_reverse(projection(dim
, dst_len
, src_len
));
4545 for (i
= src_len
; i
< dst_len
; ++i
)
4546 map
= isl_map_fix_si(map
, isl_dim_out
, i
, val
);
4548 umap
= isl_union_map_apply_range(umap
, isl_union_map_from_map(map
));
4553 /* Group bands with the same values for tile_len and n_parallel.
4554 * The prefix schedule is then extended with a fixed coordinate that
4555 * is different for each such group.
4556 * Note that the actual values for this coordinate are not important.
4557 * The bands have already been effectively separated at a higher level
4558 * or they are independent and may be executed in parallel.
4559 * The list of band_info has been sorted before this functions is called.
4561 static void separate_bands(struct band_info
*info
, int n
)
4566 for (i
= 0; i
< n
; ++i
) {
4567 int l
= info
[i
].tile_first
;
4570 (info
[i
].tile_len
!= info
[i
- 1].tile_len
||
4571 info
[i
].n_parallel
!= info
[i
- 1].n_parallel
))
4574 info
[i
].prefix
= extend_range(info
[i
].prefix
,
4576 info
[i
].tile_first
= l
+ 1;
4580 /* Select the outermost bands in the elements of the list, align
4581 * their prefix schedules, separate bands with different values
4582 * for tile_len and/or n_parallel and then combine the resulting
4583 * prefix and suffix schedules into a single pair of prefix and
4584 * suffix schedules for the entire list.
4586 static void list_select_outer_band(struct gpu_gen
*gen
,
4587 __isl_take isl_band_list
*list
, int pos
, struct band_info
*list_info
)
4591 int n
= isl_band_list_n_band(list
);
4592 isl_ctx
*ctx
= isl_band_list_get_ctx(list
);
4593 struct band_info
*info
;
4595 isl_union_map
*prefix
;
4596 isl_union_map
*suffix
;
4599 info
= isl_calloc_array(ctx
, struct band_info
, n
);
4603 for (i
= 0; i
< n
; ++i
) {
4604 band
= isl_band_list_get_band(list
, i
);
4605 band_select_outer_band(gen
, band
, pos
, &info
[i
]);
4606 if (info
[i
].tile_first
> max_tile_first
)
4607 max_tile_first
= info
[i
].tile_first
;
4610 for (i
= 0; i
< n
; ++i
) {
4611 if (info
[i
].tile_first
== max_tile_first
)
4613 info
[i
].prefix
= extend_range(info
[i
].prefix
,
4614 info
[i
].tile_first
, max_tile_first
, 0);
4615 info
[i
].tile_first
= max_tile_first
;
4618 qsort(info
, n
, sizeof(struct band_info
), &cmp_band
);
4620 for (i
= 0; i
< n
- 1; ++i
)
4621 if (info
[i
].tile_len
!= info
[i
+ 1].tile_len
||
4622 info
[i
].n_parallel
!= info
[i
+ 1].n_parallel
)
4626 separate_bands(info
, n
);
4628 prefix
= info
[0].prefix
;
4629 suffix
= info
[0].suffix
;
4631 for (i
= 1; i
< n
; ++i
) {
4632 prefix
= isl_union_map_union(prefix
, info
[i
].prefix
);
4633 suffix
= isl_union_map_union(suffix
, info
[i
].suffix
);
4636 list_info
->tile_first
= info
[0].tile_first
;
4637 list_info
->tile_len
= -1;
4638 list_info
->prefix
= prefix
;
4639 list_info
->suffix
= suffix
;
4641 isl_band_list_free(list
);
4645 /* Select the outermost tilable band that (by construction)
4646 * has at least one parallel loop.
4647 * The starting position of the aligned band is stored in the pair
4649 * The sizes and number of parallel loops may be different in different
4650 * parts of the band forest and are therefore stored in the gpu_stmts.
4652 * Return the complete schedule, with the tilable bands aligned
4653 * at gen->tile_first and padded with zero, if needed.
4655 static __isl_give isl_union_map
*select_outer_tilable_band(struct gpu_gen
*gen
,
4656 __isl_keep isl_schedule
*schedule
)
4658 isl_band_list
*list
;
4659 struct band_info info
;
4661 gen
->n_parallel
= 0;
4664 list
= isl_schedule_get_band_forest(schedule
);
4666 list_select_outer_band(gen
, list
, 0, &info
);
4668 gen
->tile_first
= info
.tile_first
;
4669 info
.suffix
= align_range(info
.suffix
);
4671 return isl_union_map_flat_range_product(info
.prefix
, info
.suffix
);
4674 /* Set gen->untiled_len to the number of scheduling dimensions
4675 * for the schedule of the first domain.
4676 * We assume here that this number is the same for all domains.
4678 static int set_untiled_len(__isl_take isl_map
*map
, void *user
)
4680 unsigned *untiled_len
= user
;
4682 *untiled_len
= isl_map_dim(map
, isl_dim_out
);
4688 /* Compute an appropriate schedule based on the accesses in
4689 * gen->read and gen->write.
4691 * We use the dependences in gen->prog->scop to compute
4692 * a schedule that has a parallel loop in each tilable band.
4693 * Finally, we select the outermost tilable band.
4695 static void compute_schedule(struct gpu_gen
*gen
)
4697 isl_union_set
*domain
;
4698 isl_union_map
*dep_raw
, *dep
;
4699 isl_union_map
*uninitialized
;
4700 isl_union_map
*sched
;
4701 isl_schedule
*schedule
;
4703 dep_raw
= isl_union_map_copy(gen
->prog
->scop
->dep_flow
);
4704 uninitialized
= isl_union_map_copy(gen
->prog
->scop
->live_in
);
4706 gen
->prog
->copy_in
= isl_union_map_range(uninitialized
);
4708 dep
= isl_union_map_copy(gen
->prog
->scop
->dep_false
);
4709 dep
= isl_union_map_union(dep
, dep_raw
);
4710 dep
= isl_union_map_coalesce(dep
);
4712 domain
= isl_union_set_copy(gen
->prog
->scop
->domain
);
4713 domain
= isl_union_set_intersect_params(domain
,
4714 isl_set_copy(gen
->prog
->scop
->context
));
4715 schedule
= isl_union_set_compute_schedule(isl_union_set_copy(domain
),
4716 isl_union_map_copy(dep
), dep
);
4718 sched
= select_outer_tilable_band(gen
, schedule
);
4720 isl_union_map_foreach_map(sched
, &set_untiled_len
, &gen
->untiled_len
);
4721 sched
= isl_union_map_intersect_domain(sched
, domain
);
4724 isl_schedule_free(schedule
);
4727 static struct gpu_stmt_access
**expr_extract_access(struct pet_expr
*expr
,
4728 struct gpu_stmt_access
**next_access
)
4730 struct gpu_stmt_access
*access
;
4731 isl_ctx
*ctx
= isl_map_get_ctx(expr
->acc
.access
);
4733 access
= isl_alloc_type(ctx
, struct gpu_stmt_access
);
4735 access
->next
= NULL
;
4736 access
->read
= expr
->acc
.read
;
4737 access
->write
= expr
->acc
.write
;
4738 access
->access
= isl_map_copy(expr
->acc
.access
);
4740 *next_access
= access
;
4741 next_access
= &(*next_access
)->next
;
4745 static struct gpu_stmt_access
**expr_extract_accesses(struct pet_expr
*expr
,
4746 struct gpu_stmt_access
**next_access
)
4750 for (i
= 0; i
< expr
->n_arg
; ++i
)
4751 next_access
= expr_extract_accesses(expr
->args
[i
],
4754 if (expr
->type
== pet_expr_access
)
4755 next_access
= expr_extract_access(expr
, next_access
);
4760 static void pet_stmt_extract_accesses(struct gpu_stmt
*stmt
)
4762 struct gpu_stmt_access
**next_access
= &stmt
->accesses
;
4764 stmt
->accesses
= NULL
;
4765 expr_extract_accesses(stmt
->body
, next_access
);
4768 /* Return an array of gpu_stmt representing the statements in "scop".
4770 static struct gpu_stmt
*extract_stmts(isl_ctx
*ctx
, struct ppcg_scop
*scop
,
4771 __isl_keep isl_set
*context
)
4774 struct gpu_stmt
*stmts
;
4776 stmts
= isl_calloc_array(ctx
, struct gpu_stmt
, scop
->n_stmt
);
4779 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
4780 struct gpu_stmt
*s
= &stmts
[i
];
4782 s
->id
= isl_set_get_tuple_id(scop
->stmts
[i
]->domain
);
4783 s
->body
= scop
->stmts
[i
]->body
;
4784 pet_stmt_extract_accesses(s
);
4790 /* Replace the scop in the "input" file by equivalent code
4791 * that uses the GPU. "scop" is assumed to correspond to this scop.
4793 * We first compute a schedule that respects the dependences
4794 * of the original program and select the outermost band
4795 * of tilable dimensions that has at least one parallel loop.
4796 * We then have three blocks of dimensions
4800 * The tilable band "B" is first tiled according to "tile" sizes, resulting
4805 * For each iteration of the T loop and for each array, we compute
4806 * the array elements accessed by that iteration, construct a rectangular
4807 * box around it and shift it to the origin. The result is used
4808 * as shared memory for the array.
4810 * We then split off at most 2 parallel loops from the T loops and
4811 * at most 3 parallel loops from the P loops
4815 * The T1/P1 loops are then tiled or "wrapped" over the blocks/threads,
4816 * according to "grid"/"block" sizes.
4818 * H T1T T1P T2 P1T P1P P2 G
4820 * Finally, the T1P and P1P iterators are equated to the block and
4821 * thread dimensions respectively and so are effectively removed.
4822 * The H loops are run on the host. The T1T, T2, P1T, P2 and G loops
4823 * are run on the GPU.
4825 * Code is generated in three stages. We first generate code for the
4826 * host (the H loops), with iterators h%d. Then, for each leaf node
4827 * of the resulting AST, we generate code for the shared loops (up to
4828 * and including T2), with iterators g%d and after equating the H loops
4829 * to h%d parameters and the T1P loops to the block dimensions.
4830 * Finally, we generate code for the remaining loops in a similar fashion.
4832 __isl_give isl_ast_node
*generate_gpu(isl_ctx
*ctx
, struct gpu_prog
*prog
,
4833 struct ppcg_options
*options
)
4835 isl_union_map
*sched
;
4844 gen
.sizes
= extract_sizes_from_str(ctx
, options
->sizes
);
4845 gen
.options
= options
;
4847 compute_schedule(&gen
);
4850 tree
= generate_host_code(&gen
);
4852 clear_gpu_gen(&gen
);
4857 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
)
4859 struct gpu_prog
*prog
;
4864 prog
= isl_calloc_type(ctx
, struct gpu_prog
);
4869 prog
->context
= isl_set_copy(scop
->context
);
4870 prog
->n_stmts
= scop
->n_stmt
;
4871 prog
->stmts
= extract_stmts(ctx
, scop
, prog
->context
);
4872 prog
->read
= isl_union_map_copy(scop
->reads
);
4873 prog
->write
= isl_union_map_copy(scop
->writes
);
4875 collect_array_info(prog
);
4880 void gpu_prog_free(struct gpu_prog
*prog
)
4884 free_array_info(prog
);
4885 free_stmts(prog
->stmts
, prog
->n_stmts
);
4886 isl_union_set_free(prog
->copy_in
);
4887 isl_union_map_free(prog
->read
);
4888 isl_union_map_free(prog
->write
);
4889 isl_set_free(prog
->context
);