extract out shared gpu_array_requires_device_allocation
[ppcg.git] / gpu.h
blobc1783359af1145af3d75d874be507df2ab8a8db8
1 #ifndef _GPU_H
2 #define _GPU_H
4 #include <isl/ast.h>
5 #include <isl/id_to_ast_expr.h>
7 #include "ppcg.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. */
14 isl_space *space;
15 /* Element type. */
16 char *type;
17 /* Element size. */
18 int size;
19 /* Name of the array. */
20 char *name;
21 /* Extent of the array that needs to be copied. */
22 isl_set *extent;
23 /* Number of indices. */
24 unsigned n_index;
25 /* For each index, a bound on "extent" in that direction. */
26 isl_pw_aff **bound;
28 /* All references to this array; point to elements of a linked list. */
29 int n_ref;
30 struct gpu_stmt_access **refs;
32 /* Is this array accessed at all by the program? */
33 int accessed;
35 /* Is this a scalar that is read-only within the entire program? */
36 int read_only_scalar;
38 /* Are the elements of the array structures? */
39 int has_compound_element;
41 /* Is the array local to the scop? */
42 int local;
43 /* Is the array local and should it be declared on the host? */
44 int declare_local;
46 /* Should the array be linearized? */
47 int linearize;
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 * "global" is set if the global device memory corresponding
64 * to this array is accessed by the kernel.
65 * For each index i with 0 <= i < n_index,
66 * bound[i] is equal to array->bound[i] specialized to the current kernel.
68 struct gpu_local_array_info {
69 struct gpu_array_info *array;
71 int n_group;
72 struct gpu_array_ref_group **groups;
74 int force_private;
75 int global;
77 unsigned n_index;
78 isl_pw_aff_list *bound;
81 __isl_give isl_ast_expr *gpu_local_array_info_linearize_index(
82 struct gpu_local_array_info *array, __isl_take isl_ast_expr *expr);
84 /* A sequence of "n" names of types.
86 struct gpu_types {
87 int n;
88 char **name;
91 /* "read" and "write" contain the original access relations, possibly
92 * involving member accesses.
94 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
95 * only refer to the outer arrays of any possible member accesses.
97 struct gpu_prog {
98 isl_ctx *ctx;
100 struct ppcg_scop *scop;
102 /* Set of parameter values */
103 isl_set *context;
105 /* All potential read accesses in the entire program */
106 isl_union_map *read;
108 /* All potential write accesses in the entire program */
109 isl_union_map *may_write;
110 /* All definite write accesses in the entire program */
111 isl_union_map *must_write;
112 /* All tagged definite kills in the entire program */
113 isl_union_map *tagged_must_kill;
115 /* The set of inner array elements that may be preserved. */
116 isl_union_set *may_persist;
118 /* A mapping from all innermost arrays to their outer arrays. */
119 isl_union_map *to_outer;
120 /* A mapping from the outer arrays to all corresponding inner arrays. */
121 isl_union_map *to_inner;
122 /* A mapping from all intermediate arrays to their outer arrays,
123 * including an identity mapping from the anoymous 1D space to itself.
125 isl_union_map *any_to_outer;
127 /* Order dependences on non-scalars. */
128 isl_union_map *array_order;
130 /* Array of statements */
131 int n_stmts;
132 struct gpu_stmt *stmts;
134 int n_array;
135 struct gpu_array_info *array;
138 struct gpu_gen {
139 isl_ctx *ctx;
140 struct ppcg_options *options;
142 /* Callback for printing of AST in appropriate format. */
143 __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
144 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
145 struct gpu_types *types, void *user);
146 void *print_user;
148 struct gpu_prog *prog;
149 /* The generated AST. */
150 isl_ast_node *tree;
152 /* The sequence of types for which a definition has been printed. */
153 struct gpu_types types;
155 /* User specified tile, grid and block sizes for each kernel */
156 isl_union_map *sizes;
158 /* Effectively used tile, grid and block sizes for each kernel */
159 isl_union_map *used_sizes;
161 /* Identifier of the next kernel. */
162 int kernel_id;
165 enum ppcg_kernel_access_type {
166 ppcg_access_global,
167 ppcg_access_shared,
168 ppcg_access_private
171 enum ppcg_kernel_stmt_type {
172 ppcg_kernel_copy,
173 ppcg_kernel_domain,
174 ppcg_kernel_sync
177 /* Representation of special statements, in particular copy statements
178 * and __syncthreads statements, inside a kernel.
180 * type represents the kind of statement
183 * for ppcg_kernel_copy statements we have
185 * read is set if the statement should copy data from global memory
186 * to shared memory or registers.
188 * index expresses an access to the array element that needs to be copied
189 * local_index expresses the corresponding element in the tile
191 * array refers to the original array being copied
192 * local_array is a pointer to the appropriate element in the "array"
193 * array of the ppcg_kernel to which this copy access belongs
196 * for ppcg_kernel_domain statements we have
198 * stmt is the corresponding input statement
200 * n_access is the number of accesses in stmt
201 * access is an array of local information about the accesses
203 struct ppcg_kernel_stmt {
204 enum ppcg_kernel_stmt_type type;
206 union {
207 struct {
208 int read;
209 isl_ast_expr *index;
210 isl_ast_expr *local_index;
211 struct gpu_array_info *array;
212 struct gpu_local_array_info *local_array;
213 } c;
214 struct {
215 struct gpu_stmt *stmt;
216 isl_id_to_ast_expr *ref2expr;
217 } d;
218 } u;
221 /* Representation of a local variable in a kernel.
223 struct ppcg_kernel_var {
224 struct gpu_array_info *array;
225 enum ppcg_kernel_access_type type;
226 char *name;
227 isl_vec *size;
230 /* Representation of a kernel.
232 * prog describes the original code from which the kernel is extracted.
234 * id is the sequence number of the kernel.
236 * block_ids contains the list of block identifiers for this kernel.
237 * thread_ids contains the list of thread identifiers for this kernel.
239 * the first n_grid elements of grid_dim represent the specified size
240 * of the grid.
241 * the first n_block elements of block_dim represent the specified or
242 * effective size of the block.
243 * Note that in the input file, the sizes of the grid and the blocks
244 * are specified in the order x, y, z, but internally, the sizes
245 * are stored in reverse order, so that the last element always
246 * refers to the x dimension.
248 * grid_size reflects the effective grid size.
250 * context contains the values of the parameters and outer schedule dimensions
251 * for which any statement instance in this kernel needs to be executed.
253 * n_sync is the number of synchronization operations that have
254 * been introduced in the schedule tree corresponding to this kernel (so far).
256 * core contains the spaces of the statement domains that form
257 * the core computation of the kernel. It is used to navigate
258 * the tree during the construction of the device part of the schedule
259 * tree in create_kernel.
261 * arrays is the set of possibly accessed outer array elements.
263 * space is the schedule space of the AST context. That is, it represents
264 * the loops of the generated host code containing the kernel launch.
266 * n_array is the total number of arrays in the input program and also
267 * the number of element in the array array.
268 * array contains information about each array that is local
269 * to the current kernel. If an array is not used in a kernel,
270 * then the corresponding entry does not contain any information.
272 * any_force_private is set if any array in the kernel is marked force_private
274 * block_filter contains constraints on the domain elements in the kernel
275 * that encode the mapping to block identifiers, where the block identifiers
276 * are represented by "n_grid" parameters with as names the elements
277 * of "block_ids".
279 * thread_filter contains constraints on the domain elements in the kernel
280 * that encode the mapping to thread identifiers, where the thread identifiers
281 * are represented by "n_block" parameters with as names the elements
282 * of "thread_ids".
284 * shared_schedule corresponds to the schedule dimensions of
285 * the (tiled) schedule for this kernel that have been taken into account
286 * for computing private/shared memory tiles.
287 * shared_schedule_dim is the dimension of this schedule.
289 * sync_writes contains write references that require synchronization.
290 * Each reference is represented by a universe set in a space [S[i,j] -> R[]]
291 * with S[i,j] the statement instance space and R[] the array reference.
293 struct ppcg_kernel {
294 isl_ctx *ctx;
295 struct ppcg_options *options;
297 struct gpu_prog *prog;
299 int id;
301 isl_id_list *block_ids;
302 isl_id_list *thread_ids;
304 int n_grid;
305 int n_block;
306 int grid_dim[2];
307 int block_dim[3];
309 isl_multi_pw_aff *grid_size;
310 isl_set *context;
312 int n_sync;
313 isl_union_set *core;
314 isl_union_set *arrays;
316 isl_space *space;
318 int n_array;
319 struct gpu_local_array_info *array;
321 int n_var;
322 struct ppcg_kernel_var *var;
324 int any_force_private;
326 isl_union_set *block_filter;
327 isl_union_set *thread_filter;
328 isl_union_pw_multi_aff *shared_schedule;
329 int shared_schedule_dim;
331 isl_union_set *sync_writes;
333 isl_ast_node *tree;
336 int gpu_array_is_scalar(struct gpu_array_info *array);
337 int gpu_array_is_read_only_scalar(struct gpu_array_info *array);
338 int gpu_array_requires_device_allocation(struct gpu_array_info *array);
339 __isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array);
341 struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop);
342 void *gpu_prog_free(struct gpu_prog *prog);
344 int ppcg_kernel_requires_array_argument(struct ppcg_kernel *kernel, int i);
346 int generate_gpu(isl_ctx *ctx, const char *input, FILE *out,
347 struct ppcg_options *options,
348 __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
349 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
350 struct gpu_types *types, void *user), void *user);
352 #endif