5 #include <isl/id_to_ast_expr.h>
8 #include "ppcg_options.h"
10 /* Represents an outer array possibly accessed by a gpu_prog.
12 struct gpu_array_info
{
13 /* The array data space. */
19 /* Name of the array. */
21 /* Extent of the array that needs to be copied. */
23 /* Number of indices. */
25 /* For each index, a bound on "extent" in that direction. */
28 /* All references to this array; point to elements of a linked list. */
30 struct gpu_stmt_access
**refs
;
32 /* Is this array accessed at all by the program? */
35 /* Is this a scalar that is read-only within the entire program? */
38 /* Are the elements of the array structures? */
39 int has_compound_element
;
41 /* Is the array local to the scop? */
44 /* Should the array be linearized? */
47 /* Order dependences on this array.
48 * Only used if live_range_reordering option is set.
49 * It is set to NULL otherwise.
51 isl_union_map
*dep_order
;
54 /* Represents an outer array accessed by a ppcg_kernel, localized
55 * to the context of this kernel.
57 * "array" points to the corresponding array in the gpu_prog.
58 * The "n_group" "groups" are the reference groups associated to the array.
59 * If the outer array represented by the gpu_local_array_info
60 * contains structures, then the references are not
61 * collected and the reference groups are not computed.
62 * If "force_private" is set, then the array (in practice a scalar)
63 * must be mapped to a register.
64 * For each index i with 0 <= i < n_index,
65 * bound[i] is equal to array->bound[i] specialized to the current kernel.
67 struct gpu_local_array_info
{
68 struct gpu_array_info
*array
;
71 struct gpu_array_ref_group
**groups
;
76 isl_pw_aff_list
*bound
;
79 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
80 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
);
82 /* A sequence of "n" names of types.
89 /* "read" and "write" contain the original access relations, possibly
90 * involving member accesses.
92 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
93 * only refer to the outer arrays of any possible member accesses.
98 struct ppcg_scop
*scop
;
100 /* Set of parameter values */
103 /* All potential read accesses in the entire program */
106 /* All potential write accesses in the entire program */
107 isl_union_map
*may_write
;
108 /* All definite write accesses in the entire program */
109 isl_union_map
*must_write
;
110 /* All tagged definite kills in the entire program */
111 isl_union_map
*tagged_must_kill
;
113 /* The set of inner array elements that may be preserved. */
114 isl_union_set
*may_persist
;
116 /* Set of outer array elements that need to be copied in. */
117 isl_union_set
*copy_in
;
118 /* Set of outer array elements that need to be copied out. */
119 isl_union_set
*copy_out
;
121 /* A mapping from all innermost arrays to their outer arrays. */
122 isl_union_map
*to_outer
;
123 /* A mapping from the outer arrays to all corresponding inner arrays. */
124 isl_union_map
*to_inner
;
125 /* A mapping from all intermediate arrays to their outer arrays,
126 * including an identity mapping from the anoymous 1D space to itself.
128 isl_union_map
*any_to_outer
;
130 /* Order dependences on non-scalars. */
131 isl_union_map
*array_order
;
133 /* Array of statements */
135 struct gpu_stmt
*stmts
;
138 struct gpu_array_info
*array
;
143 struct ppcg_options
*options
;
145 /* Callback for printing of AST in appropriate format. */
146 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
147 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
148 struct gpu_types
*types
, void *user
);
151 struct gpu_prog
*prog
;
152 /* The generated AST. */
155 /* The sequence of types for which a definition has been printed. */
156 struct gpu_types types
;
158 /* User specified tile, grid and block sizes for each kernel */
159 isl_union_map
*sizes
;
161 /* Effectively used tile, grid and block sizes for each kernel */
162 isl_union_map
*used_sizes
;
164 /* Pointer to current ppcg_kernel. */
166 /* Identifier of the next kernel. */
168 /* Pointer to the current kernel. */
169 struct ppcg_kernel
*kernel
;
170 /* Does the computed schedule exhibit any parallelism? */
173 /* First tile dimension. */
175 /* Number of tile dimensions. */
177 /* Number of initial parallel loops among tile dimensions. */
180 /* Number of dimensions determining shared memory. */
183 /* Number of rows in the untiled schedule. */
185 /* Number of rows in the tiled schedule. */
187 /* Number of rows in schedule after tiling/wrapping over threads. */
188 int thread_tiled_len
;
190 /* A schedule tree corresponding to the host code. */
191 isl_schedule
*host_schedule
;
192 /* Global untiled schedule. */
193 isl_union_map
*sched
;
194 /* Local (per kernel launch) tiled schedule. */
195 isl_union_map
*tiled_sched
;
196 /* Local schedule per shared memory tile loop iteration. */
197 isl_union_map
*local_sched
;
199 /* Local tiled schedule projected onto the shared tile loops and
200 * the loops that will be wrapped over the threads,
201 * with all shared tile loops parametrized.
203 isl_union_map
*shared_sched
;
204 /* Projects out the loops that will be wrapped over the threads
207 isl_union_map
*shared_proj
;
209 /* A map that takes the range of shared_sched as input,
210 * wraps the appropriate loops over the threads and then projects
213 isl_map
*privatization
;
215 /* The array reference group corresponding to copy_sched. */
216 struct gpu_array_ref_group
*copy_group
;
218 /* First loop to unroll (or -1 if none) in the current part of the
225 /* Note: in the input file, the sizes of the grid and the blocks
226 * are specified in the order x, y, z, but internally, the sizes
227 * are stored in reverse order, so that the last element always
228 * refers to the x dimension.
235 enum ppcg_kernel_access_type
{
241 enum ppcg_kernel_stmt_type
{
247 /* Representation of special statements, in particular copy statements
248 * and __syncthreads statements, inside a kernel.
250 * type represents the kind of statement
253 * for ppcg_kernel_copy statements we have
255 * read is set if the statement should copy data from global memory
256 * to shared memory or registers.
258 * index expresses an access to the array element that needs to be copied
259 * local_index expresses the corresponding element in the tile
261 * array refers to the original array being copied
262 * local_array is a pointer to the appropriate element in the "array"
263 * array of the ppcg_kernel to which this copy access belongs
266 * for ppcg_kernel_domain statements we have
268 * stmt is the corresponding input statement
270 * n_access is the number of accesses in stmt
271 * access is an array of local information about the accesses
273 struct ppcg_kernel_stmt
{
274 enum ppcg_kernel_stmt_type type
;
280 isl_ast_expr
*local_index
;
281 struct gpu_array_info
*array
;
282 struct gpu_local_array_info
*local_array
;
285 struct gpu_stmt
*stmt
;
286 isl_id_to_ast_expr
*ref2expr
;
291 /* Representation of a local variable in a kernel.
293 struct ppcg_kernel_var
{
294 struct gpu_array_info
*array
;
295 enum ppcg_kernel_access_type type
;
300 /* Representation of a kernel.
302 * id is the sequence number of the kernel.
304 * block_ids contains the list of block identifiers for this kernel.
305 * thread_ids contains the list of thread identifiers for this kernel.
307 * the first n_block elements of block_dim represent the effective size
310 * grid_size reflects the effective grid size.
312 * context contains the values of the parameters and outer schedule dimensions
313 * for which any statement instance in this kernel needs to be executed.
315 * arrays is the set of possibly accessed outer array elements.
317 * space is the schedule space of the AST context. That is, it represents
318 * the loops of the generated host code containing the kernel launch.
320 * n_array is the total number of arrays in the input program and also
321 * the number of element in the array array.
322 * array contains information about each array that is local
323 * to the current kernel. If an array is not used in a kernel,
324 * then the corresponding entry does not contain any information.
326 * any_force_private is set if any array in the kernel is marked force_private
330 struct ppcg_options
*options
;
334 isl_id_list
*block_ids
;
335 isl_id_list
*thread_ids
;
340 isl_multi_pw_aff
*grid_size
;
343 isl_union_set
*arrays
;
348 struct gpu_local_array_info
*array
;
351 struct ppcg_kernel_var
*var
;
353 int any_force_private
;
358 int gpu_array_is_scalar(struct gpu_array_info
*array
);
359 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
);
360 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
);
362 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
);
363 void *gpu_prog_free(struct gpu_prog
*prog
);
365 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
366 struct ppcg_options
*options
,
367 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
368 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
369 struct gpu_types
*types
, void *user
), void *user
);