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
;
120 /* Represents an outer array accessed by a ppcg_kernel, localized
121 * to the context of this kernel.
123 * "array" points to the corresponding array in the gpu_prog.
124 * The "n_group" "groups" are the reference groups associated to the array.
125 * If "force_private" is set, then the array (in practice a scalar)
126 * must be mapped to a register.
127 * "global" is set if the global device memory corresponding
128 * to this array is accessed by the kernel.
129 * "bound" is equal to array->bound specialized to the current kernel.
130 * "bound_expr" is the corresponding access AST expression.
132 struct gpu_local_array_info
{
133 struct gpu_array_info
*array
;
136 struct gpu_array_ref_group
**groups
;
142 isl_multi_pw_aff
*bound
;
143 isl_ast_expr
*bound_expr
;
146 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
147 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
);
149 /* A sequence of "n" names of types.
156 /* "read" and "write" contain the original access relations, possibly
157 * involving member accesses.
159 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
160 * only refer to the outer arrays of any possible member accesses.
165 struct ppcg_scop
*scop
;
167 /* Set of parameter values */
170 /* All potential read accesses in the entire program */
173 /* All potential write accesses in the entire program */
174 isl_union_map
*may_write
;
175 /* All definite write accesses in the entire program */
176 isl_union_map
*must_write
;
177 /* All tagged definite kills in the entire program */
178 isl_union_map
*tagged_must_kill
;
180 /* The set of inner array elements that may be preserved. */
181 isl_union_set
*may_persist
;
183 /* A mapping from all innermost arrays to their outer arrays. */
184 isl_union_map
*to_outer
;
185 /* A mapping from the outer arrays to all corresponding inner arrays. */
186 isl_union_map
*to_inner
;
187 /* A mapping from all intermediate arrays to their outer arrays,
188 * including an identity mapping from the anonymous 1D space to itself.
190 isl_union_map
*any_to_outer
;
192 /* Order dependences on non-scalars. */
193 isl_union_map
*array_order
;
195 /* Array of statements */
197 struct gpu_stmt
*stmts
;
200 struct gpu_array_info
*array
;
205 struct ppcg_options
*options
;
207 /* Callback for printing of AST in appropriate format. */
208 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
209 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
210 struct gpu_types
*types
, void *user
);
213 isl_id_to_ast_expr
*(*build_ast_expr
)(void *stmt
,
214 isl_ast_build
*build
,
215 isl_multi_pw_aff
*(*fn_index
)(
216 __isl_take isl_multi_pw_aff
*mpa
, isl_id
*id
,
219 isl_ast_expr
*(*fn_expr
)(isl_ast_expr
*expr
,
220 isl_id
*id
, void *user
),
223 struct gpu_prog
*prog
;
224 /* The generated AST. */
227 /* The sequence of types for which a definition has been printed. */
228 struct gpu_types types
;
230 /* User specified tile, grid and block sizes for each kernel */
231 isl_union_map
*sizes
;
233 /* Effectively used tile, grid and block sizes for each kernel */
234 isl_union_map
*used_sizes
;
236 /* Identifier of the next kernel. */
240 enum ppcg_group_access_type
{
246 enum ppcg_kernel_stmt_type
{
252 /* Representation of special statements, in particular copy statements
253 * and __syncthreads statements, inside a kernel.
255 * type represents the kind of statement
258 * for ppcg_kernel_copy statements we have
260 * read is set if the statement should copy data from global memory
261 * to shared memory or registers.
263 * index expresses an access to the array element that needs to be copied
264 * local_index expresses the corresponding element in the tile
266 * array refers to the original array being copied
267 * local_array is a pointer to the appropriate element in the "array"
268 * array of the ppcg_kernel to which this copy access belongs
271 * for ppcg_kernel_domain statements we have
273 * stmt is the corresponding input statement
275 * n_access is the number of accesses in stmt
276 * access is an array of local information about the accesses
278 struct ppcg_kernel_stmt
{
279 enum ppcg_kernel_stmt_type type
;
285 isl_ast_expr
*local_index
;
286 struct gpu_array_info
*array
;
287 struct gpu_local_array_info
*local_array
;
290 struct gpu_stmt
*stmt
;
291 isl_id_to_ast_expr
*ref2expr
;
296 /* Representation of a local variable in a kernel.
298 struct ppcg_kernel_var
{
299 struct gpu_array_info
*array
;
300 enum ppcg_group_access_type type
;
305 /* Representation of a kernel.
307 * prog describes the original code from which the kernel is extracted.
309 * id is the sequence number of the kernel.
311 * block_ids contains the list of block identifiers for this kernel.
312 * thread_ids contains the list of thread identifiers for this kernel.
314 * the first n_grid elements of grid_dim represent the specified size
316 * the first n_block elements of block_dim represent the specified or
317 * effective size of the block.
318 * Note that in the input file, the sizes of the grid and the blocks
319 * are specified in the order x, y, z, but internally, the sizes
320 * are stored in reverse order, so that the last element always
321 * refers to the x dimension.
323 * grid_size reflects the effective grid size.
324 * grid_size_expr contains a corresponding access AST expression, built within
325 * the context where the launch appears.
327 * context contains the values of the parameters and outer schedule dimensions
328 * for which any statement instance in this kernel needs to be executed.
330 * n_sync is the number of synchronization operations that have
331 * been introduced in the schedule tree corresponding to this kernel (so far).
333 * core contains the spaces of the statement domains that form
334 * the core computation of the kernel. It is used to navigate
335 * the tree during the construction of the device part of the schedule
336 * tree in gpu_create_kernel.
338 * expanded_domain contains the original statement instances,
339 * i.e., those that appear in the domains of access relations,
340 * that are involved in the kernel.
341 * contraction maps those original statement instances to
342 * the statement instances that are active at the point
343 * in the schedule tree where the kernel is created.
345 * arrays is the set of possibly accessed outer array elements.
347 * space is the schedule space of the AST context. That is, it represents
348 * the loops of the generated host code containing the kernel launch.
350 * n_array is the total number of arrays in the input program and also
351 * the number of element in the array array.
352 * array contains information about each array that is local
353 * to the current kernel. If an array is not used in a kernel,
354 * then the corresponding entry does not contain any information.
356 * any_force_private is set if any array in the kernel is marked force_private
358 * block_filter contains constraints on the domain elements in the kernel
359 * that encode the mapping to block identifiers, where the block identifiers
360 * are represented by "n_grid" parameters with as names the elements
363 * thread_filter contains constraints on the domain elements in the kernel
364 * that encode the mapping to thread identifiers, where the thread identifiers
365 * are represented by "n_block" parameters with as names the elements
368 * copy_schedule corresponds to the schedule dimensions of
369 * the (tiled) schedule for this kernel that have been taken into account
370 * for computing private/shared memory tiles.
371 * The domain corresponds to the original statement instances, i.e.,
372 * those that appear in the leaves of the schedule tree.
373 * copy_schedule_dim is the dimension of this schedule.
375 * sync_writes contains write references that require synchronization.
376 * Each reference is represented by a universe set in a space [S[i,j] -> R[]]
377 * with S[i,j] the statement instance space and R[] the array reference.
381 struct ppcg_options
*options
;
383 struct gpu_prog
*prog
;
387 isl_id_list
*block_ids
;
388 isl_id_list
*thread_ids
;
395 isl_multi_pw_aff
*grid_size
;
396 isl_ast_expr
*grid_size_expr
;
401 isl_union_set
*arrays
;
403 isl_union_pw_multi_aff
*contraction
;
404 isl_union_set
*expanded_domain
;
409 struct gpu_local_array_info
*array
;
412 struct ppcg_kernel_var
*var
;
414 int any_force_private
;
416 isl_union_set
*block_filter
;
417 isl_union_set
*thread_filter
;
418 isl_union_pw_multi_aff
*copy_schedule
;
419 int copy_schedule_dim
;
421 isl_union_set
*sync_writes
;
426 int gpu_array_is_scalar(struct gpu_array_info
*array
);
427 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
);
428 int gpu_array_requires_device_allocation(struct gpu_array_info
*array
);
429 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
);
430 isl_bool
gpu_array_can_be_private(struct gpu_array_info
*array
);
432 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
);
433 void *gpu_prog_free(struct gpu_prog
*prog
);
435 int ppcg_kernel_requires_array_argument(struct ppcg_kernel
*kernel
, int i
);
437 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
438 struct ppcg_options
*options
,
439 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
440 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
441 struct gpu_types
*types
, void *user
), void *user
);
443 __isl_give isl_schedule_node
*gpu_create_kernel(struct gpu_gen
*gen
,
444 __isl_take isl_schedule_node
*node
, int scale
,
445 __isl_keep isl_multi_val
*sizes
);
447 __isl_give isl_schedule
*get_schedule(struct gpu_gen
*gen
);
448 int has_any_permutable_node(__isl_keep isl_schedule
*schedule
);
449 __isl_give isl_schedule
*map_to_device(struct gpu_gen
*gen
,
450 __isl_take isl_schedule
*schedule
,
452 __isl_give isl_ast_node
*generate_code(struct gpu_gen
*gen
,
453 __isl_take isl_schedule
*schedule
);
455 __isl_give isl_union_set
*compute_may_persist(struct gpu_prog
*prog
);
456 void collect_references(struct gpu_prog
*prog
, struct gpu_array_info
*array
);
457 void collect_order_dependences(struct gpu_prog
*prog
);
458 isl_bool
only_fixed_element_accessed(struct gpu_array_info
*array
);