README: add a note about additional variables in the generated code
[ppcg.git] / gpu.h
blob0e37faf66c16db662015dfeefa159364d5f554f6
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.
11 * If this outer array contains structures, then the references are not
12 * collected and the reference groups are not computed.
14 struct gpu_array_info {
15 /* The array data space. */
16 isl_space *space;
17 /* Element type. */
18 char *type;
19 /* Element size. */
20 int size;
21 /* Name of the array. */
22 char *name;
23 /* Extent of the array that needs to be copied. */
24 isl_set *extent;
25 /* Number of indices. */
26 unsigned n_index;
27 /* For each index, a bound on "extent" in that direction. */
28 isl_pw_aff **bound;
30 /* All references to this array; point to elements of a linked list. */
31 int n_ref;
32 struct gpu_stmt_access **refs;
34 /* The reference groups associated to this array. */
35 int n_group;
36 struct gpu_array_ref_group **groups;
38 /* Is this a scalar that is read-only within the entire program? */
39 int read_only_scalar;
41 /* Are the elements of the array structures? */
42 int has_compound_element;
44 /* Is the array local to the scop? */
45 int local;
47 /* Should the array be linearized? */
48 int linearize;
50 /* Order dependences on this array.
51 * Only used if live_range_reordering option is set.
52 * It is set to NULL otherwise.
54 isl_union_map *dep_order;
55 /* Should the array (scalar) be forcibly mapped to a register? */
56 int force_private;
59 /* For each index i, array->bound[i] specialized to the current kernel. */
60 struct gpu_local_array_info {
61 isl_pw_aff_list *bound;
64 __isl_give isl_ast_expr *gpu_local_array_info_linearize_index(
65 struct gpu_local_array_info *array, __isl_take isl_ast_expr *expr);
67 /* A sequence of "n" names of types.
69 struct gpu_types {
70 int n;
71 char **name;
74 /* "read" and "write" contain the original access relations, possibly
75 * involving member accesses.
77 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
78 * only refer to the outer arrays of any possible member accesses.
80 struct gpu_prog {
81 isl_ctx *ctx;
83 struct ppcg_scop *scop;
85 /* Set of parameter values */
86 isl_set *context;
88 /* All potential read accesses in the entire program */
89 isl_union_map *read;
91 /* All potential write accesses in the entire program */
92 isl_union_map *may_write;
93 /* All definite write accesses in the entire program */
94 isl_union_map *must_write;
96 /* Set of outer array elements that need to be copied in. */
97 isl_union_set *copy_in;
98 /* Set of outer array elements that need to be copied out. */
99 isl_union_set *copy_out;
101 /* A mapping from all innermost arrays to their outer arrays. */
102 isl_union_map *to_outer;
103 /* A mapping from the outer arrays to all corresponding inner arrays. */
104 isl_union_map *to_inner;
106 /* Order dependences on non-scalars. */
107 isl_union_map *array_order;
109 /* Array of statements */
110 int n_stmts;
111 struct gpu_stmt *stmts;
113 int n_array;
114 struct gpu_array_info *array;
117 enum ppcg_kernel_access_type {
118 ppcg_access_global,
119 ppcg_access_shared,
120 ppcg_access_private
123 enum ppcg_kernel_stmt_type {
124 ppcg_kernel_copy,
125 ppcg_kernel_domain,
126 ppcg_kernel_sync
129 /* Representation of special statements, in particular copy statements
130 * and __syncthreads statements, inside a kernel.
132 * type represents the kind of statement
135 * for ppcg_kernel_copy statements we have
137 * read is set if the statement should copy data from global memory
138 * to shared memory or registers.
140 * index expresses an access to the array element that needs to be copied
141 * local_index expresses the corresponding element in the tile
143 * array refers to the original array being copied
144 * local_array is a pointer to the appropriate element in the "array"
145 * array of the ppcg_kernel to which this copy access belongs
148 * for ppcg_kernel_domain statements we have
150 * stmt is the corresponding input statement
152 * n_access is the number of accesses in stmt
153 * access is an array of local information about the accesses
155 struct ppcg_kernel_stmt {
156 enum ppcg_kernel_stmt_type type;
158 union {
159 struct {
160 int read;
161 isl_ast_expr *index;
162 isl_ast_expr *local_index;
163 struct gpu_array_info *array;
164 struct gpu_local_array_info *local_array;
165 } c;
166 struct {
167 struct gpu_stmt *stmt;
168 isl_id_to_ast_expr *ref2expr;
169 } d;
170 } u;
173 /* Representation of a local variable in a kernel.
175 struct ppcg_kernel_var {
176 struct gpu_array_info *array;
177 enum ppcg_kernel_access_type type;
178 char *name;
179 isl_vec *size;
182 /* Representation of a kernel.
184 * id is the sequence number of the kernel.
186 * the first n_block elements of block_dim represent the effective size
187 * of the block.
189 * grid_size reflects the effective grid size.
191 * context is a parametric set containing the values of the parameters
192 * for which this kernel may be run.
194 * arrays is the set of possibly accessed outer array elements.
196 * space is the schedule space of the AST context. That is, it represents
197 * the loops of the generated host code containing the kernel launch.
199 * n_array is the total number of arrays in the input program and also
200 * the number of element in the array array.
201 * array contains information about each array that is local
202 * to the current kernel. If an array is not used in a kernel,
203 * then the corresponding entry does not contain any information.
205 struct ppcg_kernel {
206 int id;
208 int n_block;
209 int block_dim[3];
211 isl_multi_pw_aff *grid_size;
212 isl_set *context;
214 isl_union_set *arrays;
216 isl_space *space;
218 int n_array;
219 struct gpu_local_array_info *array;
221 int n_var;
222 struct ppcg_kernel_var *var;
224 isl_ast_node *tree;
227 int gpu_array_is_scalar(struct gpu_array_info *array);
228 int gpu_array_is_read_only_scalar(struct gpu_array_info *array);
229 __isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array);
231 struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop);
232 void *gpu_prog_free(struct gpu_prog *prog);
234 int generate_gpu(isl_ctx *ctx, const char *input, FILE *out,
235 struct ppcg_options *options,
236 __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
237 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
238 struct gpu_types *types, void *user), void *user);
240 #endif