update isl to version 0.23
[pet.git] / pet_codegen.c
blob0cba88825a7e3dada081e9fd936f610d63c7cc93
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/space.h>
36 #include <isl/aff.h>
37 #include <isl/ast_build.h>
38 #include <isl/options.h>
39 #include <isl/set.h>
40 #include <isl/map.h>
41 #include <isl/union_set.h>
42 #include <isl/schedule_node.h>
44 struct options {
45 struct isl_options *isl;
46 unsigned tree;
47 unsigned atomic;
48 unsigned separate;
49 unsigned read_options;
52 ISL_ARGS_START(struct options, options_args)
53 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
54 ISL_ARG_BOOL(struct options, tree, 0, "tree", 0,
55 "input schedule is specified as schedule tree")
56 ISL_ARG_BOOL(struct options, atomic, 0, "atomic", 0,
57 "globally set the atomic option")
58 ISL_ARG_BOOL(struct options, separate, 0, "separate", 0,
59 "globally set the separate option")
60 ISL_ARG_BOOL(struct options, read_options, 0, "read-options", 0,
61 "read options from standard input")
62 ISL_ARGS_END
64 ISL_ARG_DEF(options, struct options, options_args)
66 /* Return a universal, 1-dimensional set with the given name.
68 static __isl_give isl_union_set *universe(isl_ctx *ctx, const char *name)
70 isl_space *space;
72 space = isl_space_set_alloc(ctx, 0, 1);
73 space = isl_space_set_tuple_name(space, isl_dim_set, name);
74 return isl_union_set_from_set(isl_set_universe(space));
77 /* Set the "name" option for the entire schedule domain.
79 static __isl_give isl_union_map *set_universe(__isl_take isl_union_map *opt,
80 __isl_keep isl_union_map *schedule, const char *name)
82 isl_ctx *ctx;
83 isl_union_set *domain, *target;
84 isl_union_map *option;
86 ctx = isl_union_map_get_ctx(opt);
88 domain = isl_union_map_range(isl_union_map_copy(schedule));
89 domain = isl_union_set_universe(domain);
90 target = universe(ctx, name);
91 option = isl_union_map_from_domain_and_range(domain, target);
92 opt = isl_union_map_union(opt, option);
94 return opt;
97 /* Set the build options based on the command line options.
99 * If no command line options are specified, we use default build options.
100 * If --read-options is specified, we read the build options from standard
101 * input.
102 * If --separate or --atomic is specified, we turn on the corresponding
103 * build option over the entire schedule domain.
105 static __isl_give isl_ast_build *set_options(__isl_take isl_ast_build *build,
106 struct options *options, __isl_keep isl_union_map *schedule)
108 isl_ctx *ctx;
109 isl_union_map *opt;
111 if (!options->separate && !options->atomic && !options->read_options)
112 return build;
114 ctx = isl_union_map_get_ctx(schedule);
116 if (options->read_options)
117 opt = isl_union_map_read_from_file(ctx, stdin);
118 else
119 opt = isl_union_map_empty(isl_union_map_get_space(schedule));
121 if (options->separate)
122 opt = set_universe(opt, schedule, "separate");
123 if (options->atomic)
124 opt = set_universe(opt, schedule, "atomic");
126 build = isl_ast_build_set_options(build, opt);
128 return build;
131 /* Print a function declaration for the domain "set".
133 * In particular, print a declaration of the form
135 * void S(int, ..., int);
137 * where S is the name of the domain and the number of arguments
138 * is equal to the dimension of "set".
140 static isl_stat print_declaration(__isl_take isl_set *set, void *user)
142 isl_printer **p = user;
143 int i, n;
145 n = isl_set_dim(set, isl_dim_set);
147 *p = isl_printer_start_line(*p);
148 *p = isl_printer_print_str(*p, "void ");
149 *p = isl_printer_print_str(*p, isl_set_get_tuple_name(set));
150 *p = isl_printer_print_str(*p, "(");
151 for (i = 0; i < n; ++i) {
152 if (i)
153 *p = isl_printer_print_str(*p, ", ");
154 *p = isl_printer_print_str(*p, "int");
156 *p = isl_printer_print_str(*p, ");");
157 *p = isl_printer_end_line(*p);
159 isl_set_free(set);
161 return isl_stat_ok;
164 /* Print a function declaration for each domain in "uset".
166 static __isl_give isl_printer *print_declarations(__isl_take isl_printer *p,
167 __isl_keep isl_union_set *uset)
169 if (isl_union_set_foreach_set(uset, &print_declaration, &p) >= 0)
170 return p;
171 isl_printer_free(p);
172 return NULL;
175 /* Check that the domain of "map" is named.
177 static isl_stat check_name(__isl_take isl_map *map, void *user)
179 int named;
180 isl_ctx *ctx;
182 ctx = isl_map_get_ctx(map);
183 named = isl_map_has_tuple_name(map, isl_dim_in);
184 isl_map_free(map);
186 if (named < 0)
187 return isl_stat_error;
188 if (!named)
189 isl_die(ctx, isl_error_invalid,
190 "all domains should be named", return isl_stat_error);
191 return isl_stat_ok;
194 /* Given an AST "tree", print out the following code
196 * void foo(<parameters>/)
198 * void S1(int,...,int);
199 * #pragma scop
200 * AST
201 * #pragma endscop
204 * where the declarations are derived from the spaces in "domain".
206 static void print_tree(__isl_take isl_union_set *domain,
207 __isl_take isl_ast_node *tree)
209 int i, n;
210 isl_ctx *ctx;
211 isl_space *space;
212 isl_printer *p;
213 isl_ast_print_options *print_options;
215 if (!domain || !tree)
216 goto error;
218 ctx = isl_union_set_get_ctx(domain);
220 p = isl_printer_to_file(ctx, stdout);
221 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
222 p = isl_printer_start_line(p);
223 p = isl_printer_print_str(p, "void foo(");
225 space = isl_union_set_get_space(domain);
226 n = isl_space_dim(space, isl_dim_param);
227 for (i = 0; i < n; ++i) {
228 const char *name;
230 if (i)
231 p = isl_printer_print_str(p, ", ");
232 name = isl_space_get_dim_name(space, isl_dim_param, i);
233 p = isl_printer_print_str(p, "int ");
234 p = isl_printer_print_str(p, name);
236 isl_space_free(space);
238 p = isl_printer_print_str(p, ")");
239 p = isl_printer_end_line(p);
240 p = isl_printer_start_line(p);
241 p = isl_printer_print_str(p, "{");
242 p = isl_printer_end_line(p);
243 p = isl_printer_start_line(p);
244 p = isl_printer_indent(p, 2);
245 p = print_declarations(p, domain);
246 p = isl_printer_indent(p, -2);
247 p = isl_printer_print_str(p, "#pragma scop");
248 p = isl_printer_end_line(p);
250 p = isl_printer_indent(p, 2);
251 print_options = isl_ast_print_options_alloc(ctx);
252 p = isl_ast_node_print(tree, p, print_options);
253 p = isl_printer_indent(p, -2);
254 p = isl_printer_start_line(p);
255 p = isl_printer_print_str(p, "#pragma endscop");
256 p = isl_printer_end_line(p);
257 p = isl_printer_start_line(p);
258 p = isl_printer_print_str(p, "}");
259 p = isl_printer_end_line(p);
260 isl_printer_free(p);
262 error:
263 isl_union_set_free(domain);
264 isl_ast_node_free(tree);
267 /* If "node" is a band node, then replace the AST build options
268 * by "options".
270 static __isl_give isl_schedule_node *node_set_options(
271 __isl_take isl_schedule_node *node, void *user)
273 enum isl_ast_loop_type *type = user;
274 int i, n;
276 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
277 return node;
279 n = isl_schedule_node_band_n_member(node);
280 for (i = 0; i < n; ++i)
281 node = isl_schedule_node_band_member_set_ast_loop_type(node,
282 i, *type);
283 return node;
286 /* Replace the AST build options on all band nodes if requested
287 * by the user.
289 static __isl_give isl_schedule *schedule_set_options(
290 __isl_take isl_schedule *schedule, struct options *options)
292 enum isl_ast_loop_type type;
294 if (!options->separate && !options->atomic)
295 return schedule;
297 type = options->separate ? isl_ast_loop_separate : isl_ast_loop_atomic;
298 schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
299 &node_set_options, &type);
301 return schedule;
304 /* Read a schedule tree, generate an AST and print the result
305 * in a form that is readable by pet.
307 static int print_schedule_tree(isl_ctx *ctx, struct options *options)
309 isl_union_set *domain;
310 isl_schedule *schedule;
311 isl_ast_build *build;
312 isl_ast_node *tree;
314 schedule = isl_schedule_read_from_file(ctx, stdin);
315 domain = isl_schedule_get_domain(schedule);
317 build = isl_ast_build_alloc(ctx);
318 schedule = schedule_set_options(schedule, options);
319 tree = isl_ast_build_node_from_schedule(build, schedule);
320 isl_ast_build_free(build);
322 print_tree(domain, tree);
324 return 0;
327 /* Read a schedule, a context and (optionally) build options,
328 * generate an AST and print the result in a form that is readable
329 * by pet.
331 static int print_schedule_map(isl_ctx *ctx, struct options *options)
333 isl_set *context;
334 isl_union_set *domain;
335 isl_union_map *schedule;
336 isl_ast_build *build;
337 isl_ast_node *tree;
339 schedule = isl_union_map_read_from_file(ctx, stdin);
340 if (isl_union_map_foreach_map(schedule, &check_name, NULL) < 0) {
341 isl_union_map_free(schedule);
342 return 1;
344 context = isl_set_read_from_file(ctx, stdin);
346 domain = isl_union_map_domain(isl_union_map_copy(schedule));
347 domain = isl_union_set_align_params(domain, isl_set_get_space(context));
349 build = isl_ast_build_from_context(context);
350 build = set_options(build, options, schedule);
351 tree = isl_ast_build_node_from_schedule_map(build, schedule);
352 isl_ast_build_free(build);
354 print_tree(domain, tree);
356 return 0;
359 /* Read either
360 * - a schedule tree or
361 * - a schedule, a context and (optionally) build options,
362 * generate an AST and print the result in a form that is readable
363 * by pet.
365 int main(int argc, char **argv)
367 isl_ctx *ctx;
368 struct options *options;
369 int r;
371 options = options_new_with_defaults();
372 assert(options);
373 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
375 ctx = isl_ctx_alloc_with_options(&options_args, options);
377 if (options->tree)
378 r = print_schedule_tree(ctx, options);
379 else
380 r = print_schedule_map(ctx, options);
382 isl_ctx_free(ctx);
383 return r;