pass ppcg_scop to generate_*
[ppcg.git] / cpu.c
blob9f015307dedc4b4e23a85b13630da2a595dfe765
1 /*
2 * Copyright 2012 INRIA Paris-Rocquencourt
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Tobias Grosser, INRIA Paris-Rocquencourt,
7 * Domaine de Voluceau, Rocquenqourt, B.P. 105,
8 * 78153 Le Chesnay Cedex France
9 */
11 #include <limits.h>
12 #include <stdio.h>
14 #include <isl/aff.h>
15 #include <isl/ctx.h>
16 #include <isl/map.h>
17 #include <isl/ast_build.h>
18 #include <pet.h>
20 #include "ppcg.h"
21 #include "cpu.h"
22 #include "pet_printer.h"
23 #include "rewrite.h"
25 /* Representation of a statement inside a generated AST.
27 * "stmt" refers to the original statement.
28 * "n_access" is the number of accesses in the statement.
29 * "access" is the list of accesses transformed to refer to the iterators
30 * in the generated AST.
32 struct ppcg_stmt {
33 struct pet_stmt *stmt;
35 int n_access;
36 isl_ast_expr_list **access;
39 static void ppcg_stmt_free(void *user)
41 struct ppcg_stmt *stmt = user;
42 int i;
44 if (!stmt)
45 return;
47 for (i = 0; i < stmt->n_access; ++i)
48 isl_ast_expr_list_free(stmt->access[i]);
50 free(stmt->access);
51 free(stmt);
54 /* Derive the output file name from the input file name.
55 * 'input' is the entire path of the input file. The output
56 * is the file name plus the additional extension.
58 * We will basically replace everything after the last point
59 * with '.ppcg.c'. This means file.c becomes file.ppcg.c
61 FILE *get_output_file(const char *input)
63 char name[PATH_MAX];
64 const char *base;
65 const char *ext;
66 const char ppcg_marker[] = ".ppcg";
67 int len;
69 base = strrchr(input, '/');
70 if (base)
71 base++;
72 else
73 base = input;
74 ext = strrchr(base, '.');
75 len = ext ? ext - base : strlen(base);
77 memcpy(name, base, len);
78 strcpy(name + len, ppcg_marker);
79 strcpy(name + len + sizeof(ppcg_marker) - 1, ext);
81 return fopen(name, "w");
84 /* Print a memory access 'access' to the printer 'p'.
86 * "expr" refers to the original access.
87 * "access" is the list of index expressions transformed to refer
88 * to the iterators of the generated AST.
90 * In case the original access is unnamed (and presumably single-dimensional),
91 * we assume this is not a memory access, but just an expression.
93 static __isl_give isl_printer *print_access(__isl_take isl_printer *p,
94 struct pet_expr *expr, __isl_keep isl_ast_expr_list *access)
96 int i;
97 const char *name;
98 unsigned n_index;
100 n_index = isl_ast_expr_list_n_ast_expr(access);
101 name = isl_map_get_tuple_name(expr->acc.access, isl_dim_out);
103 if (name == NULL) {
104 isl_ast_expr *index;
105 index = isl_ast_expr_list_get_ast_expr(access, 0);
106 p = isl_printer_print_str(p, "(");
107 p = isl_printer_print_ast_expr(p, index);
108 p = isl_printer_print_str(p, ")");
109 isl_ast_expr_free(index);
110 return p;
113 p = isl_printer_print_str(p, name);
115 for (i = 0; i < n_index; ++i) {
116 isl_ast_expr *index;
118 index = isl_ast_expr_list_get_ast_expr(access, i);
120 p = isl_printer_print_str(p, "[");
121 p = isl_printer_print_ast_expr(p, index);
122 p = isl_printer_print_str(p, "]");
123 isl_ast_expr_free(index);
126 return p;
129 /* Find the element in scop->stmts that has the given "id".
131 static struct pet_stmt *find_stmt(struct ppcg_scop *scop, __isl_keep isl_id *id)
133 int i;
135 for (i = 0; i < scop->n_stmt; ++i) {
136 struct pet_stmt *stmt = scop->stmts[i];
137 isl_id *id_i;
139 id_i = isl_set_get_tuple_id(stmt->domain);
140 isl_id_free(id_i);
142 if (id_i == id)
143 return stmt;
146 isl_die(isl_id_get_ctx(id), isl_error_internal,
147 "statement not found", return NULL);
150 /* To print the transformed accesses we walk the list of transformed accesses
151 * simultaneously with the pet printer. This means that whenever
152 * the pet printer prints a pet access expression we have
153 * the corresponding transformed access available for printing.
155 static __isl_give isl_printer *print_access_expr(__isl_take isl_printer *p,
156 struct pet_expr *expr, void *user)
158 isl_ast_expr_list ***access = user;
160 p = print_access(p, expr, **access);
161 (*access)++;
163 return p;
166 /* Print a user statement in the generated AST.
167 * The ppcg_stmt has been attached to the node in at_each_domain.
169 static __isl_give isl_printer *print_user(__isl_take isl_printer *p,
170 __isl_keep isl_ast_node *node, void *user)
172 struct ppcg_stmt *stmt;
173 isl_ast_expr_list **access;
174 isl_id *id;
176 id = isl_ast_node_get_annotation(node);
177 stmt = isl_id_get_user(id);
178 isl_id_free(id);
180 access = stmt->access;
182 p = isl_printer_start_line(p);
183 p = print_pet_expr(p, stmt->stmt->body, &print_access_expr, &access);
184 p = isl_printer_print_str(p, ";");
185 p = isl_printer_end_line(p);
187 return p;
190 /* Call "fn" on each access expression in "expr".
192 static int foreach_access_expr(struct pet_expr *expr,
193 int (*fn)(struct pet_expr *expr, void *user), void *user)
195 int i;
197 if (!expr)
198 return -1;
200 if (expr->type == pet_expr_access)
201 return fn(expr, user);
203 for (i = 0; i < expr->n_arg; ++i)
204 if (foreach_access_expr(expr->args[i], fn, user) < 0)
205 return -1;
207 return 0;
210 static int inc_n_access(struct pet_expr *expr, void *user)
212 struct ppcg_stmt *stmt = user;
213 stmt->n_access++;
214 return 0;
217 /* Internal data for add_access.
219 * "stmt" is the statement to which an access needs to be added.
220 * "build" is the current AST build.
221 * "map" maps the AST loop iterators to the iteration domain of the statement.
223 struct ppcg_add_access_data {
224 struct ppcg_stmt *stmt;
225 isl_ast_build *build;
226 isl_map *map;
229 /* Given an access expression, add it to data->stmt after
230 * transforming it to refer to the AST loop iterators.
232 static int add_access(struct pet_expr *expr, void *user)
234 int i, n;
235 isl_ctx *ctx;
236 isl_map *access;
237 isl_pw_multi_aff *pma;
238 struct ppcg_add_access_data *data = user;
239 isl_ast_expr_list *index;
241 ctx = isl_map_get_ctx(expr->acc.access);
242 n = isl_map_dim(expr->acc.access, isl_dim_out);
243 access = isl_map_copy(expr->acc.access);
244 access = isl_map_apply_range(isl_map_copy(data->map), access);
245 pma = isl_pw_multi_aff_from_map(access);
246 pma = isl_pw_multi_aff_coalesce(pma);
248 index = isl_ast_expr_list_alloc(ctx, n);
249 for (i = 0; i < n; ++i) {
250 isl_pw_aff *pa;
251 isl_ast_expr *expr;
253 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
254 expr = isl_ast_build_expr_from_pw_aff(data->build, pa);
255 index = isl_ast_expr_list_add(index, expr);
257 isl_pw_multi_aff_free(pma);
259 data->stmt->access[data->stmt->n_access] = index;
260 data->stmt->n_access++;
261 return 0;
264 /* Transform the accesses in the statement associated to the domain
265 * called by "node" to refer to the AST loop iterators,
266 * collect them in a ppcg_stmt and annotate the node with the ppcg_stmt.
268 static __isl_give isl_ast_node *at_each_domain(__isl_take isl_ast_node *node,
269 __isl_keep isl_ast_build *build, void *user)
271 struct ppcg_scop *scop = user;
272 isl_ast_expr *expr, *arg;
273 isl_ctx *ctx;
274 isl_id *id;
275 isl_map *map;
276 struct ppcg_stmt *stmt;
277 struct ppcg_add_access_data data;
279 ctx = isl_ast_node_get_ctx(node);
280 stmt = isl_calloc_type(ctx, struct ppcg_stmt);
281 if (!stmt)
282 goto error;
284 expr = isl_ast_node_user_get_expr(node);
285 arg = isl_ast_expr_get_op_arg(expr, 0);
286 isl_ast_expr_free(expr);
287 id = isl_ast_expr_get_id(arg);
288 isl_ast_expr_free(arg);
289 stmt->stmt = find_stmt(scop, id);
290 isl_id_free(id);
291 if (!stmt->stmt)
292 goto error;
294 stmt->n_access = 0;
295 if (foreach_access_expr(stmt->stmt->body, &inc_n_access, stmt) < 0)
296 goto error;
298 stmt->access = isl_calloc_array(ctx, isl_ast_expr_list *,
299 stmt->n_access);
300 if (!stmt->access)
301 goto error;
303 map = isl_map_from_union_map(isl_ast_build_get_schedule(build));
304 map = isl_map_reverse(map);
306 stmt->n_access = 0;
307 data.stmt = stmt;
308 data.build = build;
309 data.map = map;
310 if (foreach_access_expr(stmt->stmt->body, &add_access, &data) < 0)
311 node = isl_ast_node_free(node);
313 isl_map_free(map);
315 id = isl_id_alloc(isl_ast_node_get_ctx(node), NULL, stmt);
316 id = isl_id_set_free_user(id, &ppcg_stmt_free);
317 return isl_ast_node_set_annotation(node, id);
318 error:
319 ppcg_stmt_free(stmt);
320 return isl_ast_node_free(node);
323 /* Code generate the scop 'scop' and print the corresponding C code to
324 * 'output'.
326 static void print_scop(isl_ctx *ctx, struct ppcg_scop *scop, FILE *output)
328 isl_set *context;
329 isl_union_set *domain_set;
330 isl_union_map *schedule_map;
331 isl_ast_build *build;
332 isl_ast_print_options *print_options;
333 isl_ast_node *tree;
334 isl_printer *p;
336 context = isl_set_copy(scop->context);
337 domain_set = isl_union_set_copy(scop->domain);
338 schedule_map = isl_union_map_copy(scop->schedule);
339 schedule_map = isl_union_map_intersect_domain(schedule_map, domain_set);
341 build = isl_ast_build_from_context(context);
342 build = isl_ast_build_set_at_each_domain(build, &at_each_domain, scop);
343 tree = isl_ast_build_ast_from_schedule(build, schedule_map);
344 isl_ast_build_free(build);
346 print_options = isl_ast_print_options_alloc(ctx);
347 print_options = isl_ast_print_options_set_print_user(print_options,
348 &print_user, NULL);
350 p = isl_printer_to_file(ctx, output);
351 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
352 p = isl_ast_node_print_macros(tree, p);
353 p = isl_ast_node_print(tree, p, print_options);
354 isl_printer_free(p);
356 isl_ast_print_options_free(print_options);
358 isl_ast_node_free(tree);
360 fprintf(output, "\n");
363 int generate_cpu(isl_ctx *ctx, struct ppcg_scop *ps,
364 struct ppcg_options *options, const char *input)
366 FILE *input_file;
367 FILE *output_file;
369 if (!ps)
370 return -1;
372 input_file = fopen(input, "r");
373 output_file = get_output_file(input);
375 copy_before_scop(input_file, output_file);
376 fprintf(output_file, "/* ppcg generated CPU code */\n\n");
377 print_scop(ctx, ps, output_file);
378 copy_after_scop(input_file, output_file);
380 fclose(output_file);
381 fclose(input_file);
383 return 0;