cuda backend: fix declarations of block and thread identifier variables
[ppcg.git] / gpu.h
blob8f54faf35029fd060d6248190eb88e1142b62d62
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 with 0 <= i < n_index,
60 * bound[i] is equal to array->bound[i] specialized to the current kernel.
62 struct gpu_local_array_info {
63 unsigned n_index;
64 isl_pw_aff_list *bound;
67 __isl_give isl_ast_expr *gpu_local_array_info_linearize_index(
68 struct gpu_local_array_info *array, __isl_take isl_ast_expr *expr);
70 /* A sequence of "n" names of types.
72 struct gpu_types {
73 int n;
74 char **name;
77 /* "read" and "write" contain the original access relations, possibly
78 * involving member accesses.
80 * The elements of "array", as well as the ranges of "copy_in" and "copy_out"
81 * only refer to the outer arrays of any possible member accesses.
83 struct gpu_prog {
84 isl_ctx *ctx;
86 struct ppcg_scop *scop;
88 /* Set of parameter values */
89 isl_set *context;
91 /* All potential read accesses in the entire program */
92 isl_union_map *read;
94 /* All potential write accesses in the entire program */
95 isl_union_map *may_write;
96 /* All definite write accesses in the entire program */
97 isl_union_map *must_write;
98 /* All tagged definite kills in the entire program */
99 isl_union_map *tagged_must_kill;
101 /* The set of inner array elements that may be preserved. */
102 isl_union_set *may_persist;
104 /* Set of outer array elements that need to be copied in. */
105 isl_union_set *copy_in;
106 /* Set of outer array elements that need to be copied out. */
107 isl_union_set *copy_out;
109 /* A mapping from all innermost arrays to their outer arrays. */
110 isl_union_map *to_outer;
111 /* A mapping from the outer arrays to all corresponding inner arrays. */
112 isl_union_map *to_inner;
113 /* A mapping from all intermediate arrays to their outer arrays,
114 * including an identity mapping from the anoymous 1D space to itself.
116 isl_union_map *any_to_outer;
118 /* Order dependences on non-scalars. */
119 isl_union_map *array_order;
121 /* Array of statements */
122 int n_stmts;
123 struct gpu_stmt *stmts;
125 int n_array;
126 struct gpu_array_info *array;
129 enum ppcg_kernel_access_type {
130 ppcg_access_global,
131 ppcg_access_shared,
132 ppcg_access_private
135 enum ppcg_kernel_stmt_type {
136 ppcg_kernel_copy,
137 ppcg_kernel_domain,
138 ppcg_kernel_sync
141 /* Representation of special statements, in particular copy statements
142 * and __syncthreads statements, inside a kernel.
144 * type represents the kind of statement
147 * for ppcg_kernel_copy statements we have
149 * read is set if the statement should copy data from global memory
150 * to shared memory or registers.
152 * index expresses an access to the array element that needs to be copied
153 * local_index expresses the corresponding element in the tile
155 * array refers to the original array being copied
156 * local_array is a pointer to the appropriate element in the "array"
157 * array of the ppcg_kernel to which this copy access belongs
160 * for ppcg_kernel_domain statements we have
162 * stmt is the corresponding input statement
164 * n_access is the number of accesses in stmt
165 * access is an array of local information about the accesses
167 struct ppcg_kernel_stmt {
168 enum ppcg_kernel_stmt_type type;
170 union {
171 struct {
172 int read;
173 isl_ast_expr *index;
174 isl_ast_expr *local_index;
175 struct gpu_array_info *array;
176 struct gpu_local_array_info *local_array;
177 } c;
178 struct {
179 struct gpu_stmt *stmt;
180 isl_id_to_ast_expr *ref2expr;
181 } d;
182 } u;
185 /* Representation of a local variable in a kernel.
187 struct ppcg_kernel_var {
188 struct gpu_array_info *array;
189 enum ppcg_kernel_access_type type;
190 char *name;
191 isl_vec *size;
194 /* Representation of a kernel.
196 * id is the sequence number of the kernel.
198 * block_ids contains the list of block identifiers for this kernel.
199 * thread_ids contains the list of thread identifiers for this kernel.
201 * the first n_block elements of block_dim represent the effective size
202 * of the block.
204 * grid_size reflects the effective grid size.
206 * context is a parametric set containing the values of the parameters
207 * for which this kernel may be run.
209 * arrays is the set of possibly accessed outer array elements.
211 * space is the schedule space of the AST context. That is, it represents
212 * the loops of the generated host code containing the kernel launch.
214 * n_array is the total number of arrays in the input program and also
215 * the number of element in the array array.
216 * array contains information about each array that is local
217 * to the current kernel. If an array is not used in a kernel,
218 * then the corresponding entry does not contain any information.
220 struct ppcg_kernel {
221 int id;
223 isl_id_list *block_ids;
224 isl_id_list *thread_ids;
226 int n_block;
227 int block_dim[3];
229 isl_multi_pw_aff *grid_size;
230 isl_set *context;
232 isl_union_set *arrays;
234 isl_space *space;
236 int n_array;
237 struct gpu_local_array_info *array;
239 int n_var;
240 struct ppcg_kernel_var *var;
242 isl_ast_node *tree;
245 int gpu_array_is_scalar(struct gpu_array_info *array);
246 int gpu_array_is_read_only_scalar(struct gpu_array_info *array);
247 __isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array);
249 struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop);
250 void *gpu_prog_free(struct gpu_prog *prog);
252 int generate_gpu(isl_ctx *ctx, const char *input, FILE *out,
253 struct ppcg_options *options,
254 __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
255 struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
256 struct gpu_types *types, void *user), void *user);
258 #endif