gpu_group.c: access_is_coalesced: handle zero-dimensional data
[ppcg.git] / gpu.h
blob204cf6b4472f3ec2b14fb5c21b974514fdf70968
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 /* Is the corresponding global device memory accessed in any way? */
47 int global;
49 /* Should the array be linearized? */
50 int linearize;
52 /* Order dependences on this array.
53 * Only used if live_range_reordering option is set.
54 * It is set to NULL otherwise.
56 isl_union_map *dep_order;
59 /* Represents an outer array accessed by a ppcg_kernel, localized
60 * to the context of this kernel.
62 * "array" points to the corresponding array in the gpu_prog.
63 * The "n_group" "groups" are the reference groups associated to the array.
64 * If "force_private" is set, then the array (in practice a scalar)
65 * must be mapped to a register.
66 * "global" is set if the global device memory corresponding
67 * to this array is accessed by the kernel.
68 * For each index i with 0 <= i < n_index,
69 * bound[i] is equal to array->bound[i] specialized to the current kernel.
71 struct gpu_local_array_info {
72 struct gpu_array_info *array;
74 int n_group;
75 struct gpu_array_ref_group **groups;
77 int force_private;
78 int global;
80 unsigned n_index;
81 isl_pw_aff_list *bound;
84 __isl_give isl_ast_expr *gpu_local_array_info_linearize_index(
85 struct gpu_local_array_info *array, __isl_take isl_ast_expr *expr);
87 /* A sequence of "n" names of types.
89 struct gpu_types {
90 int n;
91 char **name;
94 /* "read" and "write" contain the original access relations, possibly
95 * involving member accesses.
97 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
98 * only refer to the outer arrays of any possible member accesses.
100 struct gpu_prog {
101 isl_ctx *ctx;
103 struct ppcg_scop *scop;
105 /* Set of parameter values */
106 isl_set *context;
108 /* All potential read accesses in the entire program */
109 isl_union_map *read;
111 /* All potential write accesses in the entire program */
112 isl_union_map *may_write;
113 /* All definite write accesses in the entire program */
114 isl_union_map *must_write;
115 /* All tagged definite kills in the entire program */
116 isl_union_map *tagged_must_kill;
118 /* The set of inner array elements that may be preserved. */
119 isl_union_set *may_persist;
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;
168 enum ppcg_kernel_access_type {
169 ppcg_access_global,
170 ppcg_access_shared,
171 ppcg_access_private
174 enum ppcg_kernel_stmt_type {
175 ppcg_kernel_copy,
176 ppcg_kernel_domain,
177 ppcg_kernel_sync
180 /* Representation of special statements, in particular copy statements
181 * and __syncthreads statements, inside a kernel.
183 * type represents the kind of statement
186 * for ppcg_kernel_copy statements we have
188 * read is set if the statement should copy data from global memory
189 * to shared memory or registers.
191 * index expresses an access to the array element that needs to be copied
192 * local_index expresses the corresponding element in the tile
194 * array refers to the original array being copied
195 * local_array is a pointer to the appropriate element in the "array"
196 * array of the ppcg_kernel to which this copy access belongs
199 * for ppcg_kernel_domain statements we have
201 * stmt is the corresponding input statement
203 * n_access is the number of accesses in stmt
204 * access is an array of local information about the accesses
206 struct ppcg_kernel_stmt {
207 enum ppcg_kernel_stmt_type type;
209 union {
210 struct {
211 int read;
212 isl_ast_expr *index;
213 isl_ast_expr *local_index;
214 struct gpu_array_info *array;
215 struct gpu_local_array_info *local_array;
216 } c;
217 struct {
218 struct gpu_stmt *stmt;
219 isl_id_to_ast_expr *ref2expr;
220 } d;
221 } u;
224 /* Representation of a local variable in a kernel.
226 struct ppcg_kernel_var {
227 struct gpu_array_info *array;
228 enum ppcg_kernel_access_type type;
229 char *name;
230 isl_vec *size;
233 /* Representation of a kernel.
235 * prog describes the original code from which the kernel is extracted.
237 * id is the sequence number of the kernel.
239 * block_ids contains the list of block identifiers for this kernel.
240 * thread_ids contains the list of thread identifiers for this kernel.
242 * the first n_grid elements of grid_dim represent the specified size
243 * of the grid.
244 * the first n_block elements of block_dim represent the specified or
245 * effective size of the block.
246 * Note that in the input file, the sizes of the grid and the blocks
247 * are specified in the order x, y, z, but internally, the sizes
248 * are stored in reverse order, so that the last element always
249 * refers to the x dimension.
251 * grid_size reflects the effective grid size.
253 * context contains the values of the parameters and outer schedule dimensions
254 * for which any statement instance in this kernel needs to be executed.
256 * n_sync is the number of synchronization operations that have
257 * been introduced in the schedule tree corresponding to this kernel (so far).
259 * core contains the spaces of the statement domains that form
260 * the core computation of the kernel. It is used to navigate
261 * the tree during the construction of the device part of the schedule
262 * tree in create_kernel.
264 * arrays is the set of possibly accessed outer array elements.
266 * space is the schedule space of the AST context. That is, it represents
267 * the loops of the generated host code containing the kernel launch.
269 * n_array is the total number of arrays in the input program and also
270 * the number of element in the array array.
271 * array contains information about each array that is local
272 * to the current kernel. If an array is not used in a kernel,
273 * then the corresponding entry does not contain any information.
275 * any_force_private is set if any array in the kernel is marked force_private
277 * block_filter contains constraints on the domain elements in the kernel
278 * that encode the mapping to block identifiers, where the block identifiers
279 * are represented by "n_grid" parameters with as names the elements
280 * of "block_ids".
282 * thread_filter contains constraints on the domain elements in the kernel
283 * that encode the mapping to thread identifiers, where the thread identifiers
284 * are represented by "n_block" parameters with as names the elements
285 * of "thread_ids".
287 * shared_schedule corresponds to the schedule dimensions of
288 * the (tiled) schedule for this kernel that have been taken into account
289 * for computing private/shared memory tiles.
290 * shared_schedule_dim is the dimension of this schedule.
292 * sync_writes contains write references that require synchronization.
293 * Each reference is represented by a universe set in a space [S[i,j] -> R[]]
294 * with S[i,j] the statement instance space and R[] the array reference.
296 struct ppcg_kernel {
297 isl_ctx *ctx;
298 struct ppcg_options *options;
300 struct gpu_prog *prog;
302 int id;
304 isl_id_list *block_ids;
305 isl_id_list *thread_ids;
307 int n_grid;
308 int n_block;
309 int grid_dim[2];
310 int block_dim[3];
312 isl_multi_pw_aff *grid_size;
313 isl_set *context;
315 int n_sync;
316 isl_union_set *core;
317 isl_union_set *arrays;
319 isl_space *space;
321 int n_array;
322 struct gpu_local_array_info *array;
324 int n_var;
325 struct ppcg_kernel_var *var;
327 int any_force_private;
329 isl_union_set *block_filter;
330 isl_union_set *thread_filter;
331 isl_union_pw_multi_aff *shared_schedule;
332 int shared_schedule_dim;
334 isl_union_set *sync_writes;
336 isl_ast_node *tree;
339 int gpu_array_is_scalar(struct gpu_array_info *array);
340 int gpu_array_is_read_only_scalar(struct gpu_array_info *array);
341 int gpu_array_requires_device_allocation(struct gpu_array_info *array);
342 __isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array);
344 struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop);
345 void *gpu_prog_free(struct gpu_prog *prog);
347 int ppcg_kernel_requires_array_argument(struct ppcg_kernel *kernel, int i);
349 int generate_gpu(isl_ctx *ctx, const char *input, FILE *out,
350 struct ppcg_options *options,
351 __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
352 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
353 struct gpu_types *types, void *user), void *user);
355 #endif