update isl for change in isl_map_gist
[ppcg.git] / gpu.h
blobb482834aa2423fb918a840a6c1653554f115763c
1 #ifndef _GPU_H
2 #define _GPU_H
4 #include <isl/ast.h>
5 #include <isl/id.h>
6 #include <isl/id_to_ast_expr.h>
8 #include <pet.h>
10 #include "ppcg.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 */
21 int read;
22 /* Access writes elements */
23 int write;
24 /* All writes are definite writes. */
25 int exact_write;
26 /* The number of index expressions specified in the access. */
27 int n_index;
29 /* May access relation */
30 isl_map *access;
31 /* May access relation with as domain a mapping from iteration domain
32 * to a reference identifier.
34 isl_map *tagged_access;
35 /* The reference id of the corresponding pet_expr. */
36 isl_id *ref_id;
38 struct gpu_stmt_access *next;
41 struct gpu_stmt {
42 isl_id *id;
43 struct pet_stmt *stmt;
45 /* Linked list of accesses. */
46 struct gpu_stmt_access *accesses;
49 /* Represents an outer array possibly accessed by a gpu_prog.
51 struct gpu_array_info {
52 /* The array data space. */
53 isl_space *space;
54 /* Element type. */
55 char *type;
56 /* Element size. */
57 int size;
58 /* Name of the array. */
59 char *name;
60 /* Extent of the array that needs to be copied. */
61 isl_set *extent;
62 /* Number of indices. */
63 unsigned n_index;
64 /* For each index, a bound on "extent" in that direction. */
65 isl_pw_aff **bound;
67 /* All references to this array; point to elements of a linked list. */
68 int n_ref;
69 struct gpu_stmt_access **refs;
71 /* Is this array accessed at all by the program? */
72 int accessed;
74 /* Is this a scalar that is read-only within the entire program? */
75 int read_only_scalar;
77 /* Are the elements of the array structures? */
78 int has_compound_element;
80 /* Is the array local to the scop? */
81 int local;
82 /* Is the array local and should it be declared on the host? */
83 int declare_local;
85 /* Is the corresponding global device memory accessed in any way? */
86 int global;
88 /* Should the array be linearized? */
89 int linearize;
91 /* Order dependences on this array.
92 * Only used if live_range_reordering option is set.
93 * It is set to NULL otherwise.
95 isl_union_map *dep_order;
98 /* Represents an outer array accessed by a ppcg_kernel, localized
99 * to the context of this kernel.
101 * "array" points to the corresponding array in the gpu_prog.
102 * The "n_group" "groups" are the reference groups associated to the array.
103 * If "force_private" is set, then the array (in practice a scalar)
104 * must be mapped to a register.
105 * "global" is set if the global device memory corresponding
106 * to this array is accessed by the kernel.
107 * For each index i with 0 <= i < n_index,
108 * bound[i] is equal to array->bound[i] specialized to the current kernel.
110 struct gpu_local_array_info {
111 struct gpu_array_info *array;
113 int n_group;
114 struct gpu_array_ref_group **groups;
116 int force_private;
117 int global;
119 unsigned n_index;
120 isl_pw_aff_list *bound;
123 __isl_give isl_ast_expr *gpu_local_array_info_linearize_index(
124 struct gpu_local_array_info *array, __isl_take isl_ast_expr *expr);
126 /* A sequence of "n" names of types.
128 struct gpu_types {
129 int n;
130 char **name;
133 /* "read" and "write" contain the original access relations, possibly
134 * involving member accesses.
136 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
137 * only refer to the outer arrays of any possible member accesses.
139 struct gpu_prog {
140 isl_ctx *ctx;
142 struct ppcg_scop *scop;
144 /* Set of parameter values */
145 isl_set *context;
147 /* All potential read accesses in the entire program */
148 isl_union_map *read;
150 /* All potential write accesses in the entire program */
151 isl_union_map *may_write;
152 /* All definite write accesses in the entire program */
153 isl_union_map *must_write;
154 /* All tagged definite kills in the entire program */
155 isl_union_map *tagged_must_kill;
157 /* The set of inner array elements that may be preserved. */
158 isl_union_set *may_persist;
160 /* A mapping from all innermost arrays to their outer arrays. */
161 isl_union_map *to_outer;
162 /* A mapping from the outer arrays to all corresponding inner arrays. */
163 isl_union_map *to_inner;
164 /* A mapping from all intermediate arrays to their outer arrays,
165 * including an identity mapping from the anoymous 1D space to itself.
167 isl_union_map *any_to_outer;
169 /* Order dependences on non-scalars. */
170 isl_union_map *array_order;
172 /* Array of statements */
173 int n_stmts;
174 struct gpu_stmt *stmts;
176 int n_array;
177 struct gpu_array_info *array;
180 struct gpu_gen {
181 isl_ctx *ctx;
182 struct ppcg_options *options;
184 /* Callback for printing of AST in appropriate format. */
185 __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
186 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
187 struct gpu_types *types, void *user);
188 void *print_user;
190 struct gpu_prog *prog;
191 /* The generated AST. */
192 isl_ast_node *tree;
194 /* The sequence of types for which a definition has been printed. */
195 struct gpu_types types;
197 /* User specified tile, grid and block sizes for each kernel */
198 isl_union_map *sizes;
200 /* Effectively used tile, grid and block sizes for each kernel */
201 isl_union_map *used_sizes;
203 /* Identifier of the next kernel. */
204 int kernel_id;
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 * the first n_grid elements of grid_dim represent the specified size
282 * of the grid.
283 * the first n_block elements of block_dim represent the specified or
284 * effective size of the block.
285 * Note that in the input file, the sizes of the grid and the blocks
286 * are specified in the order x, y, z, but internally, the sizes
287 * are stored in reverse order, so that the last element always
288 * refers to the x dimension.
290 * grid_size reflects the effective grid size.
292 * context contains the values of the parameters and outer schedule dimensions
293 * for which any statement instance in this kernel needs to be executed.
295 * n_sync is the number of synchronization operations that have
296 * been introduced in the schedule tree corresponding to this kernel (so far).
298 * core contains the spaces of the statement domains that form
299 * the core computation of the kernel. It is used to navigate
300 * the tree during the construction of the device part of the schedule
301 * tree in create_kernel.
303 * arrays is the set of possibly accessed outer array elements.
305 * space is the schedule space of the AST context. That is, it represents
306 * the loops of the generated host code containing the kernel launch.
308 * n_array is the total number of arrays in the input program and also
309 * the number of element in the array array.
310 * array contains information about each array that is local
311 * to the current kernel. If an array is not used in a kernel,
312 * then the corresponding entry does not contain any information.
314 * any_force_private is set if any array in the kernel is marked force_private
316 * block_filter contains constraints on the domain elements in the kernel
317 * that encode the mapping to block identifiers, where the block identifiers
318 * are represented by "n_grid" parameters with as names the elements
319 * of "block_ids".
321 * thread_filter contains constraints on the domain elements in the kernel
322 * that encode the mapping to thread identifiers, where the thread identifiers
323 * are represented by "n_block" parameters with as names the elements
324 * of "thread_ids".
326 * shared_schedule corresponds to the schedule dimensions of
327 * the (tiled) schedule for this kernel that have been taken into account
328 * for computing private/shared memory tiles.
329 * shared_schedule_dim is the dimension of this schedule.
331 * sync_writes contains write references that require synchronization.
332 * Each reference is represented by a universe set in a space [S[i,j] -> R[]]
333 * with S[i,j] the statement instance space and R[] the array reference.
335 struct ppcg_kernel {
336 isl_ctx *ctx;
337 struct ppcg_options *options;
339 struct gpu_prog *prog;
341 int id;
343 isl_id_list *block_ids;
344 isl_id_list *thread_ids;
346 int n_grid;
347 int n_block;
348 int grid_dim[2];
349 int block_dim[3];
351 isl_multi_pw_aff *grid_size;
352 isl_set *context;
354 int n_sync;
355 isl_union_set *core;
356 isl_union_set *arrays;
358 isl_space *space;
360 int n_array;
361 struct gpu_local_array_info *array;
363 int n_var;
364 struct ppcg_kernel_var *var;
366 int any_force_private;
368 isl_union_set *block_filter;
369 isl_union_set *thread_filter;
370 isl_union_pw_multi_aff *shared_schedule;
371 int shared_schedule_dim;
373 isl_union_set *sync_writes;
375 isl_ast_node *tree;
378 int gpu_array_is_scalar(struct gpu_array_info *array);
379 int gpu_array_is_read_only_scalar(struct gpu_array_info *array);
380 int gpu_array_requires_device_allocation(struct gpu_array_info *array);
381 __isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array);
383 struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop);
384 void *gpu_prog_free(struct gpu_prog *prog);
386 int ppcg_kernel_requires_array_argument(struct ppcg_kernel *kernel, int i);
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