gpu_gen: drop kernel_mark
[ppcg.git] / gpu.h
blobf6bf065d6a27be92ecae5209563b46ff4eaf3151
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;
44 /* Should the array be linearized? */
45 int linearize;
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;
70 int n_group;
71 struct gpu_array_ref_group **groups;
73 int force_private;
75 unsigned n_index;
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.
84 struct gpu_types {
85 int n;
86 char **name;
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.
95 struct gpu_prog {
96 isl_ctx *ctx;
98 struct ppcg_scop *scop;
100 /* Set of parameter values */
101 isl_set *context;
103 /* All potential read accesses in the entire program */
104 isl_union_map *read;
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 */
134 int n_stmts;
135 struct gpu_stmt *stmts;
137 int n_array;
138 struct gpu_array_info *array;
141 struct gpu_gen {
142 isl_ctx *ctx;
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);
149 void *print_user;
151 struct gpu_prog *prog;
152 /* The generated AST. */
153 isl_ast_node *tree;
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 /* Identifier of the next kernel. */
165 int kernel_id;
166 /* Pointer to the current kernel. */
167 struct ppcg_kernel *kernel;
168 /* Does the computed schedule exhibit any parallelism? */
169 int any_parallelism;
171 /* First tile dimension. */
172 int tile_first;
174 /* Number of dimensions determining shared memory. */
175 int shared_len;
177 /* Number of rows in the untiled schedule. */
178 int untiled_len;
179 /* Number of rows in the tiled schedule. */
180 int tiled_len;
181 /* Number of rows in schedule after tiling/wrapping over threads. */
182 int thread_tiled_len;
184 /* A schedule tree corresponding to both the host and device code. */
185 isl_schedule *schedule;
186 /* Global untiled schedule. */
187 isl_union_map *sched;
188 /* Local (per kernel launch) tiled schedule. */
189 isl_union_map *tiled_sched;
191 /* Local tiled schedule projected onto the shared tile loops and
192 * the loops that will be wrapped over the threads,
193 * with all shared tile loops parametrized.
195 isl_union_map *shared_sched;
196 /* Projects out the loops that will be wrapped over the threads
197 * from shared_sched.
199 isl_union_map *shared_proj;
201 /* First loop to unroll (or -1 if none) in the current part of the
202 * schedule.
204 int first_unroll;
207 enum ppcg_kernel_access_type {
208 ppcg_access_global,
209 ppcg_access_shared,
210 ppcg_access_private
213 enum ppcg_kernel_stmt_type {
214 ppcg_kernel_copy,
215 ppcg_kernel_domain,
216 ppcg_kernel_sync
219 /* Representation of special statements, in particular copy statements
220 * and __syncthreads statements, inside a kernel.
222 * type represents the kind of statement
225 * for ppcg_kernel_copy statements we have
227 * read is set if the statement should copy data from global memory
228 * to shared memory or registers.
230 * index expresses an access to the array element that needs to be copied
231 * local_index expresses the corresponding element in the tile
233 * array refers to the original array being copied
234 * local_array is a pointer to the appropriate element in the "array"
235 * array of the ppcg_kernel to which this copy access belongs
238 * for ppcg_kernel_domain statements we have
240 * stmt is the corresponding input statement
242 * n_access is the number of accesses in stmt
243 * access is an array of local information about the accesses
245 struct ppcg_kernel_stmt {
246 enum ppcg_kernel_stmt_type type;
248 union {
249 struct {
250 int read;
251 isl_ast_expr *index;
252 isl_ast_expr *local_index;
253 struct gpu_array_info *array;
254 struct gpu_local_array_info *local_array;
255 } c;
256 struct {
257 struct gpu_stmt *stmt;
258 isl_id_to_ast_expr *ref2expr;
259 } d;
260 } u;
263 /* Representation of a local variable in a kernel.
265 struct ppcg_kernel_var {
266 struct gpu_array_info *array;
267 enum ppcg_kernel_access_type type;
268 char *name;
269 isl_vec *size;
272 /* Representation of a kernel.
274 * prog describes the original code from which the kernel is extracted.
276 * id is the sequence number of the kernel.
278 * block_ids contains the list of block identifiers for this kernel.
279 * thread_ids contains the list of thread identifiers for this kernel.
281 * tile_len is the number of tile dimensions and
282 * n_parallel is the number of initial parallel loops among those
283 * tile dimensions.
284 * tile_size is an array of length tile_len containing the tile sizes.
286 * the first n_grid elements of grid_dim represent the specified size
287 * of the grid.
288 * the first n_block elements of block_dim represent the specified or
289 * effective size of the block.
290 * Note that in the input file, the sizes of the grid and the blocks
291 * are specified in the order x, y, z, but internally, the sizes
292 * are stored in reverse order, so that the last element always
293 * refers to the x dimension.
295 * grid_size reflects the effective grid size.
297 * context contains the values of the parameters and outer schedule dimensions
298 * for which any statement instance in this kernel needs to be executed.
300 * n_sync is the number of synchronization operations that have
301 * been introduced in the schedule tree corresponding to this kernel (so far).
303 * core contains the spaces of the statement domains that form
304 * the core computation of the kernel. It is used to navigate
305 * the tree during the construction of the device part of the schedule
306 * tree in create_kernel.
308 * arrays is the set of possibly accessed outer array elements.
310 * space is the schedule space of the AST context. That is, it represents
311 * the loops of the generated host code containing the kernel launch.
313 * n_array is the total number of arrays in the input program and also
314 * the number of element in the array array.
315 * array contains information about each array that is local
316 * to the current kernel. If an array is not used in a kernel,
317 * then the corresponding entry does not contain any information.
319 * any_force_private is set if any array in the kernel is marked force_private
321 * block_filter contains constraints on the domain elements in the kernel
322 * that encode the mapping to block identifiers, where the block identifiers
323 * are represented by "n_grid" parameters with as names the elements
324 * of "block_ids".
326 * thread_filter contains constraints on the domain elements in the kernel
327 * that encode the mapping to thread identifiers, where the thread identifiers
328 * are represented by "n_block" parameters with as names the elements
329 * of "thread_ids".
331 * shared_schedule corresponds to the schedule dimensions of
332 * the (tiled) schedule for this kernel that have been taken into account
333 * for computing private/shared memory tiles.
334 * shared_schedule_dim is the dimension of this schedule.
336 struct ppcg_kernel {
337 isl_ctx *ctx;
338 struct ppcg_options *options;
340 struct gpu_prog *prog;
342 int id;
344 isl_id_list *block_ids;
345 isl_id_list *thread_ids;
347 int tile_len;
348 int *tile_size;
349 int n_parallel;
351 int n_grid;
352 int n_block;
353 int grid_dim[2];
354 int block_dim[3];
356 isl_multi_pw_aff *grid_size;
357 isl_set *context;
359 int n_sync;
360 isl_union_set *core;
361 isl_union_set *arrays;
363 isl_space *space;
365 int n_array;
366 struct gpu_local_array_info *array;
368 int n_var;
369 struct ppcg_kernel_var *var;
371 int any_force_private;
373 isl_union_set *block_filter;
374 isl_union_set *thread_filter;
375 isl_union_pw_multi_aff *shared_schedule;
376 int shared_schedule_dim;
378 isl_ast_node *tree;
381 int gpu_array_is_scalar(struct gpu_array_info *array);
382 int gpu_array_is_read_only_scalar(struct gpu_array_info *array);
383 __isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array);
385 struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop);
386 void *gpu_prog_free(struct gpu_prog *prog);
388 int generate_gpu(isl_ctx *ctx, const char *input, FILE *out,
389 struct ppcg_options *options,
390 __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
391 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
392 struct gpu_types *types, void *user), void *user);
394 #endif