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? */
43 /* Is the array local and should it be declared on the host? */
46 /* Should the array be linearized? */
49 /* Order dependences on this array.
50 * Only used if live_range_reordering option is set.
51 * It is set to NULL otherwise.
53 isl_union_map
*dep_order
;
56 /* Represents an outer array accessed by a ppcg_kernel, localized
57 * to the context of this kernel.
59 * "array" points to the corresponding array in the gpu_prog.
60 * The "n_group" "groups" are the reference groups associated to the array.
61 * If "force_private" is set, then the array (in practice a scalar)
62 * must be mapped to a register.
63 * For each index i with 0 <= i < n_index,
64 * bound[i] is equal to array->bound[i] specialized to the current kernel.
66 struct gpu_local_array_info
{
67 struct gpu_array_info
*array
;
70 struct gpu_array_ref_group
**groups
;
75 isl_pw_aff_list
*bound
;
78 __isl_give isl_ast_expr
*gpu_local_array_info_linearize_index(
79 struct gpu_local_array_info
*array
, __isl_take isl_ast_expr
*expr
);
81 /* A sequence of "n" names of types.
88 /* "read" and "write" contain the original access relations, possibly
89 * involving member accesses.
91 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
92 * only refer to the outer arrays of any possible member accesses.
97 struct ppcg_scop
*scop
;
99 /* Set of parameter values */
102 /* All potential read accesses in the entire program */
105 /* All potential write accesses in the entire program */
106 isl_union_map
*may_write
;
107 /* All definite write accesses in the entire program */
108 isl_union_map
*must_write
;
109 /* All tagged definite kills in the entire program */
110 isl_union_map
*tagged_must_kill
;
112 /* The set of inner array elements that may be preserved. */
113 isl_union_set
*may_persist
;
115 /* A mapping from all innermost arrays to their outer arrays. */
116 isl_union_map
*to_outer
;
117 /* A mapping from the outer arrays to all corresponding inner arrays. */
118 isl_union_map
*to_inner
;
119 /* A mapping from all intermediate arrays to their outer arrays,
120 * including an identity mapping from the anoymous 1D space to itself.
122 isl_union_map
*any_to_outer
;
124 /* Order dependences on non-scalars. */
125 isl_union_map
*array_order
;
127 /* Array of statements */
129 struct gpu_stmt
*stmts
;
132 struct gpu_array_info
*array
;
137 struct ppcg_options
*options
;
139 /* Callback for printing of AST in appropriate format. */
140 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
141 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
142 struct gpu_types
*types
, void *user
);
145 struct gpu_prog
*prog
;
146 /* The generated AST. */
149 /* The sequence of types for which a definition has been printed. */
150 struct gpu_types types
;
152 /* User specified tile, grid and block sizes for each kernel */
153 isl_union_map
*sizes
;
155 /* Effectively used tile, grid and block sizes for each kernel */
156 isl_union_map
*used_sizes
;
158 /* Identifier of the next kernel. */
162 enum ppcg_kernel_access_type
{
168 enum ppcg_kernel_stmt_type
{
174 /* Representation of special statements, in particular copy statements
175 * and __syncthreads statements, inside a kernel.
177 * type represents the kind of statement
180 * for ppcg_kernel_copy statements we have
182 * read is set if the statement should copy data from global memory
183 * to shared memory or registers.
185 * index expresses an access to the array element that needs to be copied
186 * local_index expresses the corresponding element in the tile
188 * array refers to the original array being copied
189 * local_array is a pointer to the appropriate element in the "array"
190 * array of the ppcg_kernel to which this copy access belongs
193 * for ppcg_kernel_domain statements we have
195 * stmt is the corresponding input statement
197 * n_access is the number of accesses in stmt
198 * access is an array of local information about the accesses
200 struct ppcg_kernel_stmt
{
201 enum ppcg_kernel_stmt_type type
;
207 isl_ast_expr
*local_index
;
208 struct gpu_array_info
*array
;
209 struct gpu_local_array_info
*local_array
;
212 struct gpu_stmt
*stmt
;
213 isl_id_to_ast_expr
*ref2expr
;
218 /* Representation of a local variable in a kernel.
220 struct ppcg_kernel_var
{
221 struct gpu_array_info
*array
;
222 enum ppcg_kernel_access_type type
;
227 /* Representation of a kernel.
229 * prog describes the original code from which the kernel is extracted.
231 * id is the sequence number of the kernel.
233 * block_ids contains the list of block identifiers for this kernel.
234 * thread_ids contains the list of thread identifiers for this kernel.
236 * the first n_grid elements of grid_dim represent the specified size
238 * the first n_block elements of block_dim represent the specified or
239 * effective size of the block.
240 * Note that in the input file, the sizes of the grid and the blocks
241 * are specified in the order x, y, z, but internally, the sizes
242 * are stored in reverse order, so that the last element always
243 * refers to the x dimension.
245 * grid_size reflects the effective grid size.
247 * context contains the values of the parameters and outer schedule dimensions
248 * for which any statement instance in this kernel needs to be executed.
250 * n_sync is the number of synchronization operations that have
251 * been introduced in the schedule tree corresponding to this kernel (so far).
253 * core contains the spaces of the statement domains that form
254 * the core computation of the kernel. It is used to navigate
255 * the tree during the construction of the device part of the schedule
256 * tree in create_kernel.
258 * arrays is the set of possibly accessed outer array elements.
260 * space is the schedule space of the AST context. That is, it represents
261 * the loops of the generated host code containing the kernel launch.
263 * n_array is the total number of arrays in the input program and also
264 * the number of element in the array array.
265 * array contains information about each array that is local
266 * to the current kernel. If an array is not used in a kernel,
267 * then the corresponding entry does not contain any information.
269 * any_force_private is set if any array in the kernel is marked force_private
271 * block_filter contains constraints on the domain elements in the kernel
272 * that encode the mapping to block identifiers, where the block identifiers
273 * are represented by "n_grid" parameters with as names the elements
276 * thread_filter contains constraints on the domain elements in the kernel
277 * that encode the mapping to thread identifiers, where the thread identifiers
278 * are represented by "n_block" parameters with as names the elements
281 * shared_schedule corresponds to the schedule dimensions of
282 * the (tiled) schedule for this kernel that have been taken into account
283 * for computing private/shared memory tiles.
284 * shared_schedule_dim is the dimension of this schedule.
286 * sync_writes contains write references that require synchronization.
287 * Each reference is represented by a universe set in a space [S[i,j] -> R[]]
288 * with S[i,j] the statement instance space and R[] the array reference.
292 struct ppcg_options
*options
;
294 struct gpu_prog
*prog
;
298 isl_id_list
*block_ids
;
299 isl_id_list
*thread_ids
;
306 isl_multi_pw_aff
*grid_size
;
311 isl_union_set
*arrays
;
316 struct gpu_local_array_info
*array
;
319 struct ppcg_kernel_var
*var
;
321 int any_force_private
;
323 isl_union_set
*block_filter
;
324 isl_union_set
*thread_filter
;
325 isl_union_pw_multi_aff
*shared_schedule
;
326 int shared_schedule_dim
;
328 isl_union_set
*sync_writes
;
333 int gpu_array_is_scalar(struct gpu_array_info
*array
);
334 int gpu_array_is_read_only_scalar(struct gpu_array_info
*array
);
335 __isl_give isl_set
*gpu_array_positive_size_guard(struct gpu_array_info
*array
);
337 struct gpu_prog
*gpu_prog_alloc(isl_ctx
*ctx
, struct ppcg_scop
*scop
);
338 void *gpu_prog_free(struct gpu_prog
*prog
);
340 int generate_gpu(isl_ctx
*ctx
, const char *input
, FILE *out
,
341 struct ppcg_options
*options
,
342 __isl_give isl_printer
*(*print
)(__isl_take isl_printer
*p
,
343 struct gpu_prog
*prog
, __isl_keep isl_ast_node
*tree
,
344 struct gpu_types
*types
, void *user
), void *user
);