scop.c: filter_implied: drop unused variable
[pet.git] / pet_codegen.c
blob4ac4a9758d39387598bc655bd0bafed2d1939f93
1 /*
2 * Copyright 2012-2013 Ecole Normale Superieure. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY ECOLE NORMALE SUPERIEURE ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECOLE NORMALE SUPERIEURE OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Ecole Normale Superieure.
34 #include <assert.h>
35 #include <isl/aff.h>
36 #include <isl/ast_build.h>
37 #include <isl/options.h>
38 #include <isl/set.h>
39 #include <isl/map.h>
40 #include <isl/union_set.h>
41 #include <isl/schedule_node.h>
43 struct options {
44 struct isl_options *isl;
45 unsigned tree;
46 unsigned atomic;
47 unsigned separate;
48 unsigned read_options;
51 ISL_ARGS_START(struct options, options_args)
52 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
53 ISL_ARG_BOOL(struct options, tree, 0, "tree", 0,
54 "input schedule is specified as schedule tree")
55 ISL_ARG_BOOL(struct options, atomic, 0, "atomic", 0,
56 "globally set the atomic option")
57 ISL_ARG_BOOL(struct options, separate, 0, "separate", 0,
58 "globally set the separate option")
59 ISL_ARG_BOOL(struct options, read_options, 0, "read-options", 0,
60 "read options from standard input")
61 ISL_ARGS_END
63 ISL_ARG_DEF(options, struct options, options_args)
65 /* Return a universal, 1-dimensional set with the given name.
67 static __isl_give isl_union_set *universe(isl_ctx *ctx, const char *name)
69 isl_space *space;
71 space = isl_space_set_alloc(ctx, 0, 1);
72 space = isl_space_set_tuple_name(space, isl_dim_set, name);
73 return isl_union_set_from_set(isl_set_universe(space));
76 /* Set the "name" option for the entire schedule domain.
78 static __isl_give isl_union_map *set_universe(__isl_take isl_union_map *opt,
79 __isl_keep isl_union_map *schedule, const char *name)
81 isl_ctx *ctx;
82 isl_union_set *domain, *target;
83 isl_union_map *option;
85 ctx = isl_union_map_get_ctx(opt);
87 domain = isl_union_map_range(isl_union_map_copy(schedule));
88 domain = isl_union_set_universe(domain);
89 target = universe(ctx, name);
90 option = isl_union_map_from_domain_and_range(domain, target);
91 opt = isl_union_map_union(opt, option);
93 return opt;
96 /* Set the build options based on the command line options.
98 * If no command line options are specified, we use default build options.
99 * If --read-options is specified, we read the build options from standard
100 * input.
101 * If --separate or --atomic is specified, we turn on the corresponding
102 * build option over the entire schedule domain.
104 static __isl_give isl_ast_build *set_options(__isl_take isl_ast_build *build,
105 struct options *options, __isl_keep isl_union_map *schedule)
107 isl_ctx *ctx;
108 isl_union_map *opt;
110 if (!options->separate && !options->atomic && !options->read_options)
111 return build;
113 ctx = isl_union_map_get_ctx(schedule);
115 if (options->read_options)
116 opt = isl_union_map_read_from_file(ctx, stdin);
117 else
118 opt = isl_union_map_empty(isl_union_map_get_space(schedule));
120 if (options->separate)
121 opt = set_universe(opt, schedule, "separate");
122 if (options->atomic)
123 opt = set_universe(opt, schedule, "atomic");
125 build = isl_ast_build_set_options(build, opt);
127 return build;
130 /* Print a function declaration for the domain "set".
132 * In particular, print a declaration of the form
134 * void S(int, ..., int);
136 * where S is the name of the domain and the number of arguments
137 * is equal to the dimension of "set".
139 static isl_stat print_declaration(__isl_take isl_set *set, void *user)
141 isl_printer **p = user;
142 int i, n;
144 n = isl_set_dim(set, isl_dim_set);
146 *p = isl_printer_start_line(*p);
147 *p = isl_printer_print_str(*p, "void ");
148 *p = isl_printer_print_str(*p, isl_set_get_tuple_name(set));
149 *p = isl_printer_print_str(*p, "(");
150 for (i = 0; i < n; ++i) {
151 if (i)
152 *p = isl_printer_print_str(*p, ", ");
153 *p = isl_printer_print_str(*p, "int");
155 *p = isl_printer_print_str(*p, ");");
156 *p = isl_printer_end_line(*p);
158 isl_set_free(set);
160 return isl_stat_ok;
163 /* Print a function declaration for each domain in "uset".
165 static __isl_give isl_printer *print_declarations(__isl_take isl_printer *p,
166 __isl_keep isl_union_set *uset)
168 if (isl_union_set_foreach_set(uset, &print_declaration, &p) >= 0)
169 return p;
170 isl_printer_free(p);
171 return NULL;
174 /* Check that the domain of "map" is named.
176 static isl_stat check_name(__isl_take isl_map *map, void *user)
178 int named;
179 isl_ctx *ctx;
181 ctx = isl_map_get_ctx(map);
182 named = isl_map_has_tuple_name(map, isl_dim_in);
183 isl_map_free(map);
185 if (named < 0)
186 return isl_stat_error;
187 if (!named)
188 isl_die(ctx, isl_error_invalid,
189 "all domains should be named", return isl_stat_error);
190 return isl_stat_ok;
193 /* Given an AST "tree", print out the following code
195 * void foo(<parameters>/)
197 * void S1(int,...,int);
198 * #pragma scop
199 * AST
200 * #pragma endscop
203 * where the declarations are derived from the spaces in "domain".
205 static void print_tree(__isl_take isl_union_set *domain,
206 __isl_take isl_ast_node *tree)
208 int i, n;
209 isl_ctx *ctx;
210 isl_space *space;
211 isl_printer *p;
212 isl_ast_print_options *print_options;
214 if (!domain || !tree)
215 goto error;
217 ctx = isl_union_set_get_ctx(domain);
219 p = isl_printer_to_file(ctx, stdout);
220 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
221 p = isl_printer_start_line(p);
222 p = isl_printer_print_str(p, "void foo(");
224 space = isl_union_set_get_space(domain);
225 n = isl_space_dim(space, isl_dim_param);
226 for (i = 0; i < n; ++i) {
227 const char *name;
229 if (i)
230 p = isl_printer_print_str(p, ", ");
231 name = isl_space_get_dim_name(space, isl_dim_param, i);
232 p = isl_printer_print_str(p, "int ");
233 p = isl_printer_print_str(p, name);
235 isl_space_free(space);
237 p = isl_printer_print_str(p, ")");
238 p = isl_printer_end_line(p);
239 p = isl_printer_start_line(p);
240 p = isl_printer_print_str(p, "{");
241 p = isl_printer_end_line(p);
242 p = isl_printer_start_line(p);
243 p = isl_printer_indent(p, 2);
244 p = print_declarations(p, domain);
245 p = isl_printer_indent(p, -2);
246 p = isl_printer_print_str(p, "#pragma scop");
247 p = isl_printer_end_line(p);
249 p = isl_printer_indent(p, 2);
250 print_options = isl_ast_print_options_alloc(ctx);
251 p = isl_ast_node_print(tree, p, print_options);
252 p = isl_printer_indent(p, -2);
253 p = isl_printer_start_line(p);
254 p = isl_printer_print_str(p, "#pragma endscop");
255 p = isl_printer_end_line(p);
256 p = isl_printer_start_line(p);
257 p = isl_printer_print_str(p, "}");
258 p = isl_printer_end_line(p);
259 isl_printer_free(p);
261 error:
262 isl_union_set_free(domain);
263 isl_ast_node_free(tree);
266 /* If "node" is a band node, then replace the AST build options
267 * by "options".
269 static __isl_give isl_schedule_node *node_set_options(
270 __isl_take isl_schedule_node *node, void *user)
272 enum isl_ast_loop_type *type = user;
273 int i, n;
275 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
276 return node;
278 n = isl_schedule_node_band_n_member(node);
279 for (i = 0; i < n; ++i)
280 node = isl_schedule_node_band_member_set_ast_loop_type(node,
281 i, *type);
282 return node;
285 /* Replace the AST build options on all band nodes if requested
286 * by the user.
288 static __isl_give isl_schedule *schedule_set_options(
289 __isl_take isl_schedule *schedule, struct options *options)
291 enum isl_ast_loop_type type;
293 if (!options->separate && !options->atomic)
294 return schedule;
296 type = options->separate ? isl_ast_loop_separate : isl_ast_loop_atomic;
297 schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
298 &node_set_options, &type);
300 return schedule;
303 /* Read a schedule tree, generate an AST and print the result
304 * in a form that is readable by pet.
306 static int print_schedule_tree(isl_ctx *ctx, struct options *options)
308 isl_union_set *domain;
309 isl_schedule *schedule;
310 isl_ast_build *build;
311 isl_ast_node *tree;
313 schedule = isl_schedule_read_from_file(ctx, stdin);
314 domain = isl_schedule_get_domain(schedule);
316 build = isl_ast_build_alloc(ctx);
317 schedule = schedule_set_options(schedule, options);
318 tree = isl_ast_build_node_from_schedule(build, schedule);
319 isl_ast_build_free(build);
321 print_tree(domain, tree);
323 return 0;
326 /* Read a schedule, a context and (optionally) build options,
327 * generate an AST and print the result in a form that is readable
328 * by pet.
330 static int print_schedule_map(isl_ctx *ctx, struct options *options)
332 isl_set *context;
333 isl_union_set *domain;
334 isl_union_map *schedule;
335 isl_ast_build *build;
336 isl_ast_node *tree;
338 schedule = isl_union_map_read_from_file(ctx, stdin);
339 if (isl_union_map_foreach_map(schedule, &check_name, NULL) < 0) {
340 isl_union_map_free(schedule);
341 return 1;
343 context = isl_set_read_from_file(ctx, stdin);
345 domain = isl_union_map_domain(isl_union_map_copy(schedule));
346 domain = isl_union_set_align_params(domain, isl_set_get_space(context));
348 build = isl_ast_build_from_context(context);
349 build = set_options(build, options, schedule);
350 tree = isl_ast_build_node_from_schedule_map(build, schedule);
351 isl_ast_build_free(build);
353 print_tree(domain, tree);
355 return 0;
358 /* Read either
359 * - a schedule tree or
360 * - a schedule, a context and (optionally) build options,
361 * generate an AST and print the result in a form that is readable
362 * by pet.
364 int main(int argc, char **argv)
366 isl_ctx *ctx;
367 struct options *options;
368 int r;
370 options = options_new_with_defaults();
371 assert(options);
372 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
374 ctx = isl_ctx_alloc_with_options(&options_args, options);
376 if (options->tree)
377 r = print_schedule_tree(ctx, options);
378 else
379 r = print_schedule_map(ctx, options);
381 isl_ctx_free(ctx);
382 return r;