6 #include <isl/id_to_ast_expr.h>
11 #include "ppcg_options.h"
13 /* An access to an outer array element or an iterator.
14 * Accesses to iterators have an access relation that maps to an unnamed space.
15 * An access may be both read and write.
16 * If the access relation is empty, then the output dimension may
17 * not be equal to the dimension of the corresponding array.
19 struct gpu_stmt_access
{
20 /* Access reads elements */
22 /* Access writes elements */
24 /* All writes are definite writes. */
26 /* Is a single, fixed element being accessed? */
27 isl_bool fixed_element
;
28 /* The number of index expressions specified in the access. */
31 /* May access relation */
33 /* May access relation with as domain a mapping from iteration domain
34 * to a reference identifier.
36 isl_map
*tagged_access
;
37 /* The reference id of the corresponding pet_expr. */
40 struct gpu_stmt_access
*next
;
43 /* A representation of a user statement.
44 * "stmt" points to the corresponding pet statement.
45 * "id" is the identifier of the instance set of the statement.
46 * "accesses" is a linked list of accesses performed by the statement.
47 * If the statement has been killed, i.e., if it will not be scheduled,
48 * then this linked list may be empty even if the actual statement does
53 struct pet_stmt
*stmt
;
55 struct gpu_stmt_access
*accesses
;
58 /* Represents an outer array possibly accessed by a gpu_prog.
60 struct gpu_array_info
{
61 /* The array data space. */
67 /* Name of the array. */
69 /* Declared extent of original array. */
70 isl_set
*declared_extent
;
71 /* AST expression for declared size of original array. */
72 isl_ast_expr
*declared_size
;
73 /* Extent of the array that needs to be copied. */
75 /* Number of indices. */
77 /* For each index, a bound on "extent" in that direction. */
78 isl_multi_pw_aff
*bound
;
79 /* The corresponding access AST expression, if the array needs
80 * to be allocated on the device.
82 isl_ast_expr
*bound_expr
;
84 /* All references to this array; point to elements of a linked list. */
86 struct gpu_stmt_access
**refs
;
88 /* Is this array accessed at all by the program? */
91 /* Is this a scalar that is read-only within the entire program? */
94 /* Are the elements of the array structures? */
95 int has_compound_element
;
97 /* Are the elements only accessed through constant index expressions? */
98 int only_fixed_element
;
100 /* Is the array local to the scop? */
102 /* Is the array local and should it be declared on the host? */
105 /* Is the corresponding global device memory accessed in any way? */
108 /* Should the array be linearized? */
111 /* Order dependences on this array.
112 * Only used if live_range_reordering option is set.
113 * It is set to NULL otherwise.
115 isl_union_map
*dep_order
;
118 /* Represents an outer array accessed by a ppcg_kernel, localized
119 * to the context of this kernel.
121 * "array" points to the corresponding array in the gpu_prog.
122 * The "n_group" "groups" are the reference groups associated to the array.
123 * If "force_private" is set, then the array (in practice a scalar)
124 * must be mapped to a register.
125 * "global" is set if the global device memory corresponding
126 * to this array is accessed by the kernel.
127 * "bound" is equal to array->bound specialized to the current kernel.
128 * "bound_expr" is the corresponding access AST expression.
130 struct gpu_local_array_info
{
131 struct gpu_array_info
*array
;
134 struct gpu_array_ref_group
**groups
;
140 isl_multi_pw_aff
*bound
;
141 isl_ast_expr
*bound_expr
;
144 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
145 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
);
147 /* A sequence of "n" names of types.
154 /* "read" and "write" contain the original access relations, possibly
155 * involving member accesses.
157 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
158 * only refer to the outer arrays of any possible member accesses.
163 struct ppcg_scop
*scop
;
165 /* Set of parameter values */
168 /* All potential read accesses in the entire program */
171 /* All potential write accesses in the entire program */
172 isl_union_map
*may_write
;
173 /* All definite write accesses in the entire program */
174 isl_union_map
*must_write
;
175 /* All tagged definite kills in the entire program */
176 isl_union_map
*tagged_must_kill
;
178 /* The set of inner array elements that may be preserved. */
179 isl_union_set
*may_persist
;
181 /* A mapping from all innermost arrays to their outer arrays. */
182 isl_union_map
*to_outer
;
183 /* A mapping from the outer arrays to all corresponding inner arrays. */
184 isl_union_map
*to_inner
;
185 /* A mapping from all intermediate arrays to their outer arrays,
186 * including an identity mapping from the anonymous 1D space to itself.
188 isl_union_map
*any_to_outer
;
190 /* Order dependences on non-scalars. */
191 isl_union_map
*array_order
;
193 /* Array of statements */
195 struct gpu_stmt
*stmts
;
198 struct gpu_array_info
*array
;
203 struct ppcg_options
*options
;
205 /* Callback for printing of AST in appropriate format. */
206 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
207 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
208 struct gpu_types
*types
, void *user
);
211 struct gpu_prog
*prog
;
212 /* The generated AST. */
215 /* The sequence of types for which a definition has been printed. */
216 struct gpu_types types
;
218 /* User specified tile, grid and block sizes for each kernel */
219 isl_union_map
*sizes
;
221 /* Effectively used tile, grid and block sizes for each kernel */
222 isl_union_map
*used_sizes
;
224 /* Identifier of the next kernel. */
228 enum ppcg_group_access_type
{
234 enum ppcg_kernel_stmt_type
{
240 /* Representation of special statements, in particular copy statements
241 * and __syncthreads statements, inside a kernel.
243 * type represents the kind of statement
246 * for ppcg_kernel_copy statements we have
248 * read is set if the statement should copy data from global memory
249 * to shared memory or registers.
251 * index expresses an access to the array element that needs to be copied
252 * local_index expresses the corresponding element in the tile
254 * array refers to the original array being copied
255 * local_array is a pointer to the appropriate element in the "array"
256 * array of the ppcg_kernel to which this copy access belongs
259 * for ppcg_kernel_domain statements we have
261 * stmt is the corresponding input statement
263 * n_access is the number of accesses in stmt
264 * access is an array of local information about the accesses
266 struct ppcg_kernel_stmt
{
267 enum ppcg_kernel_stmt_type type
;
273 isl_ast_expr
*local_index
;
274 struct gpu_array_info
*array
;
275 struct gpu_local_array_info
*local_array
;
278 struct gpu_stmt
*stmt
;
279 isl_id_to_ast_expr
*ref2expr
;
284 /* Representation of a local variable in a kernel.
286 struct ppcg_kernel_var
{
287 struct gpu_array_info
*array
;
288 enum ppcg_group_access_type type
;
293 /* Representation of a kernel.
295 * prog describes the original code from which the kernel is extracted.
297 * id is the sequence number of the kernel.
299 * block_ids contains the list of block identifiers for this kernel.
300 * thread_ids contains the list of thread identifiers for this kernel.
302 * the first n_grid elements of grid_dim represent the specified size
304 * the first n_block elements of block_dim represent the specified or
305 * effective size of the block.
306 * Note that in the input file, the sizes of the grid and the blocks
307 * are specified in the order x, y, z, but internally, the sizes
308 * are stored in reverse order, so that the last element always
309 * refers to the x dimension.
311 * grid_size reflects the effective grid size.
312 * grid_size_expr contains a corresponding access AST expression, built within
313 * the context where the launch appears.
315 * context contains the values of the parameters and outer schedule dimensions
316 * for which any statement instance in this kernel needs to be executed.
318 * n_sync is the number of synchronization operations that have
319 * been introduced in the schedule tree corresponding to this kernel (so far).
321 * core contains the spaces of the statement domains that form
322 * the core computation of the kernel. It is used to navigate
323 * the tree during the construction of the device part of the schedule
324 * tree in create_kernel.
326 * arrays is the set of possibly accessed outer array elements.
328 * space is the schedule space of the AST context. That is, it represents
329 * the loops of the generated host code containing the kernel launch.
331 * n_array is the total number of arrays in the input program and also
332 * the number of element in the array array.
333 * array contains information about each array that is local
334 * to the current kernel. If an array is not used in a kernel,
335 * then the corresponding entry does not contain any information.
337 * any_force_private is set if any array in the kernel is marked force_private
339 * block_filter contains constraints on the domain elements in the kernel
340 * that encode the mapping to block identifiers, where the block identifiers
341 * are represented by "n_grid" parameters with as names the elements
344 * thread_filter contains constraints on the domain elements in the kernel
345 * that encode the mapping to thread identifiers, where the thread identifiers
346 * are represented by "n_block" parameters with as names the elements
349 * copy_schedule corresponds to the schedule dimensions of
350 * the (tiled) schedule for this kernel that have been taken into account
351 * for computing private/shared memory tiles.
352 * copy_schedule_dim is the dimension of this schedule.
354 * sync_writes contains write references that require synchronization.
355 * Each reference is represented by a universe set in a space [S[i,j] -> R[]]
356 * with S[i,j] the statement instance space and R[] the array reference.
360 struct ppcg_options
*options
;
362 struct gpu_prog
*prog
;
366 isl_id_list
*block_ids
;
367 isl_id_list
*thread_ids
;
374 isl_multi_pw_aff
*grid_size
;
375 isl_ast_expr
*grid_size_expr
;
380 isl_union_set
*arrays
;
385 struct gpu_local_array_info
*array
;
388 struct ppcg_kernel_var
*var
;
390 int any_force_private
;
392 isl_union_set
*block_filter
;
393 isl_union_set
*thread_filter
;
394 isl_union_pw_multi_aff
*copy_schedule
;
395 int copy_schedule_dim
;
397 isl_union_set
*sync_writes
;
402 int gpu_array_is_scalar(struct gpu_array_info
*array
);
403 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
);
404 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
);
405 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
);
406 isl_bool
gpu_array_can_be_private(struct gpu_array_info
*array
);
408 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
);
409 void *gpu_prog_free(struct gpu_prog
*prog
);
411 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
);
413 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
414 struct ppcg_options
*options
,
415 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
416 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
417 struct gpu_types
*types
, void *user
), void *user
);