eliminate dead code
[ppcg.git] / cpu.c
blobc498eebb1f6d50d223797b5f83ce9a5d8003d7d0
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 "print.h"
24 #include "rewrite.h"
26 /* Representation of a statement inside a generated AST.
28 * "stmt" refers to the original statement.
29 * "n_access" is the number of accesses in the statement.
30 * "access" is the list of accesses transformed to refer to the iterators
31 * in the generated AST.
33 struct ppcg_stmt {
34 struct pet_stmt *stmt;
36 int n_access;
37 isl_ast_expr_list **access;
40 static void ppcg_stmt_free(void *user)
42 struct ppcg_stmt *stmt = user;
43 int i;
45 if (!stmt)
46 return;
48 for (i = 0; i < stmt->n_access; ++i)
49 isl_ast_expr_list_free(stmt->access[i]);
51 free(stmt->access);
52 free(stmt);
55 /* Derive the output file name from the input file name.
56 * 'input' is the entire path of the input file. The output
57 * is the file name plus the additional extension.
59 * We will basically replace everything after the last point
60 * with '.ppcg.c'. This means file.c becomes file.ppcg.c
62 FILE *get_output_file(const char *input)
64 char name[PATH_MAX];
65 const char *base;
66 const char *ext;
67 const char ppcg_marker[] = ".ppcg";
68 int len;
70 base = strrchr(input, '/');
71 if (base)
72 base++;
73 else
74 base = input;
75 ext = strrchr(base, '.');
76 len = ext ? ext - base : strlen(base);
78 memcpy(name, base, len);
79 strcpy(name + len, ppcg_marker);
80 strcpy(name + len + sizeof(ppcg_marker) - 1, ext);
82 return fopen(name, "w");
85 /* Print a memory access 'access' to the printer 'p'.
87 * "expr" refers to the original access.
88 * "access" is the list of index expressions transformed to refer
89 * to the iterators of the generated AST.
91 * In case the original access is unnamed (and presumably single-dimensional),
92 * we assume this is not a memory access, but just an expression.
94 static __isl_give isl_printer *print_access(__isl_take isl_printer *p,
95 struct pet_expr *expr, __isl_keep isl_ast_expr_list *access)
97 int i;
98 const char *name;
99 unsigned n_index;
101 n_index = isl_ast_expr_list_n_ast_expr(access);
102 name = isl_map_get_tuple_name(expr->acc.access, isl_dim_out);
104 if (name == NULL) {
105 isl_ast_expr *index;
106 index = isl_ast_expr_list_get_ast_expr(access, 0);
107 p = isl_printer_print_str(p, "(");
108 p = isl_printer_print_ast_expr(p, index);
109 p = isl_printer_print_str(p, ")");
110 isl_ast_expr_free(index);
111 return p;
114 p = isl_printer_print_str(p, name);
116 for (i = 0; i < n_index; ++i) {
117 isl_ast_expr *index;
119 index = isl_ast_expr_list_get_ast_expr(access, i);
121 p = isl_printer_print_str(p, "[");
122 p = isl_printer_print_ast_expr(p, index);
123 p = isl_printer_print_str(p, "]");
124 isl_ast_expr_free(index);
127 return p;
130 /* Find the element in scop->stmts that has the given "id".
132 static struct pet_stmt *find_stmt(struct ppcg_scop *scop, __isl_keep isl_id *id)
134 int i;
136 for (i = 0; i < scop->n_stmt; ++i) {
137 struct pet_stmt *stmt = scop->stmts[i];
138 isl_id *id_i;
140 id_i = isl_set_get_tuple_id(stmt->domain);
141 isl_id_free(id_i);
143 if (id_i == id)
144 return stmt;
147 isl_die(isl_id_get_ctx(id), isl_error_internal,
148 "statement not found", return NULL);
151 /* To print the transformed accesses we walk the list of transformed accesses
152 * simultaneously with the pet printer. This means that whenever
153 * the pet printer prints a pet access expression we have
154 * the corresponding transformed access available for printing.
156 static __isl_give isl_printer *print_access_expr(__isl_take isl_printer *p,
157 struct pet_expr *expr, void *user)
159 isl_ast_expr_list ***access = user;
161 p = print_access(p, expr, **access);
162 (*access)++;
164 return p;
167 /* Print a user statement in the generated AST.
168 * The ppcg_stmt has been attached to the node in at_each_domain.
170 static __isl_give isl_printer *print_user(__isl_take isl_printer *p,
171 __isl_keep isl_ast_node *node, void *user)
173 struct ppcg_stmt *stmt;
174 isl_ast_expr_list **access;
175 isl_id *id;
177 id = isl_ast_node_get_annotation(node);
178 stmt = isl_id_get_user(id);
179 isl_id_free(id);
181 access = stmt->access;
183 p = isl_printer_start_line(p);
184 p = print_pet_expr(p, stmt->stmt->body, &print_access_expr, &access);
185 p = isl_printer_print_str(p, ";");
186 p = isl_printer_end_line(p);
188 return p;
191 /* Call "fn" on each access expression in "expr".
193 static int foreach_access_expr(struct pet_expr *expr,
194 int (*fn)(struct pet_expr *expr, void *user), void *user)
196 int i;
198 if (!expr)
199 return -1;
201 if (expr->type == pet_expr_access)
202 return fn(expr, user);
204 for (i = 0; i < expr->n_arg; ++i)
205 if (foreach_access_expr(expr->args[i], fn, user) < 0)
206 return -1;
208 return 0;
211 static int inc_n_access(struct pet_expr *expr, void *user)
213 struct ppcg_stmt *stmt = user;
214 stmt->n_access++;
215 return 0;
218 /* Internal data for add_access.
220 * "stmt" is the statement to which an access needs to be added.
221 * "build" is the current AST build.
222 * "map" maps the AST loop iterators to the iteration domain of the statement.
224 struct ppcg_add_access_data {
225 struct ppcg_stmt *stmt;
226 isl_ast_build *build;
227 isl_map *map;
230 /* Given an access expression, add it to data->stmt after
231 * transforming it to refer to the AST loop iterators.
233 static int add_access(struct pet_expr *expr, void *user)
235 int i, n;
236 isl_ctx *ctx;
237 isl_map *access;
238 isl_pw_multi_aff *pma;
239 struct ppcg_add_access_data *data = user;
240 isl_ast_expr_list *index;
242 ctx = isl_map_get_ctx(expr->acc.access);
243 n = isl_map_dim(expr->acc.access, isl_dim_out);
244 access = isl_map_copy(expr->acc.access);
245 access = isl_map_apply_range(isl_map_copy(data->map), access);
246 pma = isl_pw_multi_aff_from_map(access);
247 pma = isl_pw_multi_aff_coalesce(pma);
249 index = isl_ast_expr_list_alloc(ctx, n);
250 for (i = 0; i < n; ++i) {
251 isl_pw_aff *pa;
252 isl_ast_expr *expr;
254 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
255 expr = isl_ast_build_expr_from_pw_aff(data->build, pa);
256 index = isl_ast_expr_list_add(index, expr);
258 isl_pw_multi_aff_free(pma);
260 data->stmt->access[data->stmt->n_access] = index;
261 data->stmt->n_access++;
262 return 0;
265 /* Transform the accesses in the statement associated to the domain
266 * called by "node" to refer to the AST loop iterators,
267 * collect them in a ppcg_stmt and annotate the node with the ppcg_stmt.
269 static __isl_give isl_ast_node *at_each_domain(__isl_take isl_ast_node *node,
270 __isl_keep isl_ast_build *build, void *user)
272 struct ppcg_scop *scop = user;
273 isl_ast_expr *expr, *arg;
274 isl_ctx *ctx;
275 isl_id *id;
276 isl_map *map;
277 struct ppcg_stmt *stmt;
278 struct ppcg_add_access_data data;
280 ctx = isl_ast_node_get_ctx(node);
281 stmt = isl_calloc_type(ctx, struct ppcg_stmt);
282 if (!stmt)
283 goto error;
285 expr = isl_ast_node_user_get_expr(node);
286 arg = isl_ast_expr_get_op_arg(expr, 0);
287 isl_ast_expr_free(expr);
288 id = isl_ast_expr_get_id(arg);
289 isl_ast_expr_free(arg);
290 stmt->stmt = find_stmt(scop, id);
291 isl_id_free(id);
292 if (!stmt->stmt)
293 goto error;
295 stmt->n_access = 0;
296 if (foreach_access_expr(stmt->stmt->body, &inc_n_access, stmt) < 0)
297 goto error;
299 stmt->access = isl_calloc_array(ctx, isl_ast_expr_list *,
300 stmt->n_access);
301 if (!stmt->access)
302 goto error;
304 map = isl_map_from_union_map(isl_ast_build_get_schedule(build));
305 map = isl_map_reverse(map);
307 stmt->n_access = 0;
308 data.stmt = stmt;
309 data.build = build;
310 data.map = map;
311 if (foreach_access_expr(stmt->stmt->body, &add_access, &data) < 0)
312 node = isl_ast_node_free(node);
314 isl_map_free(map);
316 id = isl_id_alloc(isl_ast_node_get_ctx(node), NULL, stmt);
317 id = isl_id_set_free_user(id, &ppcg_stmt_free);
318 return isl_ast_node_set_annotation(node, id);
319 error:
320 ppcg_stmt_free(stmt);
321 return isl_ast_node_free(node);
324 /* Code generate the scop 'scop' and print the corresponding C code to 'p'.
326 static __isl_give isl_printer *print_scop(isl_ctx *ctx, struct ppcg_scop *scop,
327 __isl_take isl_printer *p)
329 isl_set *context;
330 isl_union_set *domain_set;
331 isl_union_map *schedule_map;
332 isl_ast_build *build;
333 isl_ast_print_options *print_options;
334 isl_ast_node *tree;
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_ast_node_print_macros(tree, p);
351 p = isl_ast_node_print(tree, p, print_options);
353 isl_ast_print_options_free(print_options);
355 isl_ast_node_free(tree);
357 return p;
360 /* Does "scop" refer to any arrays that are declared, but not
361 * exposed to the code after the scop?
363 static int any_hidden_declarations(struct ppcg_scop *scop)
365 int i;
367 if (!scop)
368 return 0;
370 for (i = 0; i < scop->n_array; ++i)
371 if (scop->arrays[i]->declared && !scop->arrays[i]->exposed)
372 return 1;
374 return 0;
377 int generate_cpu(isl_ctx *ctx, struct ppcg_scop *ps,
378 struct ppcg_options *options, const char *input)
380 FILE *input_file;
381 FILE *output_file;
382 isl_printer *p;
383 int hidden;
385 if (!ps)
386 return -1;
388 input_file = fopen(input, "r");
389 output_file = get_output_file(input);
391 copy_before_scop(input_file, output_file);
392 fprintf(output_file, "/* ppcg generated CPU code */\n\n");
393 p = isl_printer_to_file(ctx, output_file);
394 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
395 p = ppcg_print_exposed_declarations(p, ps);
396 hidden = any_hidden_declarations(ps);
397 if (hidden) {
398 p = ppcg_start_block(p);
399 p = ppcg_print_hidden_declarations(p, ps);
401 p = print_scop(ctx, ps, p);
402 if (hidden)
403 p = ppcg_end_block(p);
404 isl_printer_free(p);
405 copy_after_scop(input_file, output_file);
407 fclose(output_file);
408 fclose(input_file);
410 return 0;