gpu.c: compute effective block size
[ppcg.git] / gpu.h
blob05fde73c44ce4ef7a1f0b145bf3083dec6c98e84
1 #ifndef _GPU_H
2 #define _GPU_H
4 #include <isl/ast.h>
6 #include "ppcg.h"
7 #include "ppcg_options.h"
9 struct gpu_array_info {
10 isl_space *dim;
11 /* Element type. */
12 char *type;
13 /* Element size. */
14 int size;
15 /* Name of the array. */
16 char *name;
17 /* Extent of the array that needs to be copied. */
18 isl_set *extent;
19 /* Number of indices. */
20 unsigned n_index;
21 /* For each index, a bound on "extent" in that direction. */
22 isl_pw_aff **bound;
24 /* All references to this array; point to elements of a linked list. */
25 int n_ref;
26 struct gpu_stmt_access **refs;
28 /* The reference groups associated to this array. */
29 int n_group;
30 struct gpu_array_ref_group **groups;
32 /* For scalars, is this scalar read-only within the entire program? */
33 int read_only;
35 /* Is the array local to the scop? */
36 int local;
39 /* For each index i, array->bound[i] specialized to the current kernel. */
40 struct gpu_local_array_info {
41 isl_pw_aff_list *bound;
44 struct gpu_prog {
45 isl_ctx *ctx;
47 struct ppcg_scop *scop;
49 /* Set of parameter values */
50 isl_set *context;
52 /* All read accesses in the entire program */
53 isl_union_map *read;
55 /* All write accesses in the entire program */
56 isl_union_map *write;
58 /* Set of array elements that need to be copied in. */
59 isl_union_set *copy_in;
60 /* Set of array elements that need to be copied out. */
61 isl_union_set *copy_out;
63 /* Array of statements */
64 int n_stmts;
65 struct gpu_stmt *stmts;
67 int n_array;
68 struct gpu_array_info *array;
71 enum ppcg_kernel_access_type {
72 ppcg_access_global,
73 ppcg_access_shared,
74 ppcg_access_private
77 enum ppcg_kernel_stmt_type {
78 ppcg_kernel_copy,
79 ppcg_kernel_domain,
80 ppcg_kernel_sync
83 /* Instance specific information about an access inside a kernel statement.
85 * type indicates whether it is a global, shared or private access
86 * array is the original array information and may be NULL in case
87 * of an affine expression
88 * local_array is a pointer to the appropriate element in the "array"
89 * array of the ppcg_kernel to which this access belongs. It is
90 * NULL whenever array is NULL.
91 * local_name is the name of the array or its local copy
92 * index is the sequence of local index expressions
94 struct ppcg_kernel_access {
95 enum ppcg_kernel_access_type type;
96 struct gpu_array_info *array;
97 struct gpu_local_array_info *local_array;
98 char *local_name;
99 isl_ast_expr_list *index;
102 /* Representation of special statements, in particular copy statements
103 * and __syncthreads statements, inside a kernel.
105 * type represents the kind of statement
108 * for ppcg_kernel_copy statements we have
110 * read is set if the statement should copy data from global memory
111 * to shared memory or registers.
113 * index expresses the array element that needs to be copied as a "call"
114 * to the array
115 * local_index expresses the corresponding element in the tile
117 * array refers to the original array being copied
118 * local_array is a pointer to the appropriate element in the "array"
119 * array of the ppcg_kernel to which this copy access belongs
122 * for ppcg_kernel_domain statements we have
124 * stmt is the corresponding input statement
126 * n_access is the number of accesses in stmt
127 * access is an array of local information about the accesses
129 struct ppcg_kernel_stmt {
130 enum ppcg_kernel_stmt_type type;
132 union {
133 struct {
134 int read;
135 isl_ast_expr *index;
136 isl_ast_expr *local_index;
137 struct gpu_array_info *array;
138 struct gpu_local_array_info *local_array;
139 } c;
140 struct {
141 struct gpu_stmt *stmt;
143 int n_access;
144 struct ppcg_kernel_access *access;
145 } d;
146 } u;
149 /* Representation of a local variable in a kernel.
151 struct ppcg_kernel_var {
152 struct gpu_array_info *array;
153 enum ppcg_kernel_access_type type;
154 char *name;
155 isl_vec *size;
158 /* Representation of a kernel.
160 * id is the sequence number of the kernel.
162 * the first n_block elements of block_dim represent the effective size
163 * of the block.
165 * grid_size reflects the effective grid size.
167 * context is a parametric set containing the values of the parameters
168 * for which this kernel may be run.
170 * arrays is the set of accessed array elements.
172 * space is the schedule space of the AST context. That is, it represents
173 * the loops of the generated host code containing the kernel launch.
175 * n_array is the total number of arrays in the input program and also
176 * the number of element in the array array.
177 * array contains information about each array that is local
178 * to the current kernel. If an array is not ussed in a kernel,
179 * then the corresponding entry does not contain any information.
181 struct ppcg_kernel {
182 int id;
184 int n_block;
185 int block_dim[3];
187 isl_multi_pw_aff *grid_size;
188 isl_set *context;
190 isl_union_set *arrays;
192 isl_space *space;
194 int n_array;
195 struct gpu_local_array_info *array;
197 int n_var;
198 struct ppcg_kernel_var *var;
200 isl_ast_node *tree;
203 int gpu_array_is_scalar(struct gpu_array_info *array);
204 int gpu_array_is_read_only_scalar(struct gpu_array_info *array);
206 struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop);
207 void gpu_prog_free(struct gpu_prog *prog);
209 __isl_give isl_ast_node *generate_gpu(isl_ctx *ctx, struct gpu_prog *prog,
210 struct ppcg_options *options);
212 #endif