isl_ast_codegen.c: do_unroll: extract out foreach_iteration
[isl.git] / codegen.c
blob1b16b4dc099b4fac1a853dcbb83c051d4d446250
1 /*
2 * Copyright 2012,2014 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 /* This program prints an AST that scans the domain elements of
11 * the domain of a given schedule in the order specified by
12 * the schedule tree or by their image(s) in the schedule map.
14 * The input consists of either a schedule tree or
15 * a sequence of three sets/relations.
16 * - a schedule map
17 * - a context
18 * - a relation describing AST generation options
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <isl/ast.h>
24 #include <isl/ast_build.h>
25 #include <isl/options.h>
26 #include <isl/set.h>
27 #include <isl/stream.h>
29 struct options {
30 struct isl_options *isl;
31 unsigned atomic;
32 unsigned separate;
35 ISL_ARGS_START(struct options, options_args)
36 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
37 ISL_ARG_BOOL(struct options, atomic, 0, "atomic", 0,
38 "globally set the atomic option")
39 ISL_ARG_BOOL(struct options, separate, 0, "separate", 0,
40 "globally set the separate option")
41 ISL_ARGS_END
43 ISL_ARG_DEF(cg_options, struct options, options_args)
44 ISL_ARG_CTX_DEF(cg_options, struct options, options_args)
46 /* Return a universal, 1-dimensional set with the given name.
48 static __isl_give isl_union_set *universe(isl_ctx *ctx, const char *name)
50 isl_space *space;
52 space = isl_space_set_alloc(ctx, 0, 1);
53 space = isl_space_set_tuple_name(space, isl_dim_set, name);
54 return isl_union_set_from_set(isl_set_universe(space));
57 /* Set the "name" option for the entire schedule domain.
59 static __isl_give isl_union_map *set_universe(__isl_take isl_union_map *opt,
60 __isl_keep isl_union_map *schedule, const char *name)
62 isl_ctx *ctx;
63 isl_union_set *domain, *target;
64 isl_union_map *option;
66 ctx = isl_union_map_get_ctx(opt);
68 domain = isl_union_map_range(isl_union_map_copy(schedule));
69 domain = isl_union_set_universe(domain);
70 target = universe(ctx, name);
71 option = isl_union_map_from_domain_and_range(domain, target);
72 opt = isl_union_map_union(opt, option);
74 return opt;
77 /* Update the build options based on the user-specified options.
79 * If the --separate or --atomic options were specified, then
80 * we clear any separate or atomic options that may already exist in "opt".
82 static __isl_give isl_ast_build *set_options(__isl_take isl_ast_build *build,
83 __isl_take isl_union_map *opt, struct options *options,
84 __isl_keep isl_union_map *schedule)
86 if (options->separate || options->atomic) {
87 isl_ctx *ctx;
88 isl_union_set *target;
90 ctx = isl_union_map_get_ctx(schedule);
92 target = universe(ctx, "separate");
93 opt = isl_union_map_subtract_range(opt, target);
94 target = universe(ctx, "atomic");
95 opt = isl_union_map_subtract_range(opt, target);
98 if (options->separate)
99 opt = set_universe(opt, schedule, "separate");
100 if (options->atomic)
101 opt = set_universe(opt, schedule, "atomic");
103 build = isl_ast_build_set_options(build, opt);
105 return build;
108 /* Construct an AST in case the schedule is specified by a union map.
110 * We read the context and the options from "s" and construct the AST.
112 static __isl_give isl_ast_node *construct_ast_from_union_map(
113 __isl_take isl_union_map *schedule, __isl_keep isl_stream *s)
115 isl_set *context;
116 isl_union_map *options_map;
117 isl_ast_build *build;
118 isl_ast_node *tree;
119 struct options *options;
121 options = isl_ctx_peek_cg_options(isl_stream_get_ctx(s));
123 context = isl_stream_read_set(s);
124 options_map = isl_stream_read_union_map(s);
126 build = isl_ast_build_from_context(context);
127 build = set_options(build, options_map, options, schedule);
128 tree = isl_ast_build_node_from_schedule_map(build, schedule);
129 isl_ast_build_free(build);
131 return tree;
134 /* Construct an AST in case the schedule is specified by a schedule tree.
136 static __isl_give isl_ast_node *construct_ast_from_schedule(
137 __isl_take isl_schedule *schedule)
139 isl_ast_build *build;
140 isl_ast_node *tree;
142 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
143 tree = isl_ast_build_node_from_schedule(build, schedule);
144 isl_ast_build_free(build);
146 return tree;
149 /* Read an object from stdin.
150 * If it is a (union) map, then assume an input specified by
151 * schedule map, context and options and construct an AST from
152 * those elements
153 * If it is a schedule object, then construct the AST from the schedule.
155 int main(int argc, char **argv)
157 isl_ctx *ctx;
158 isl_stream *s;
159 isl_ast_node *tree = NULL;
160 struct options *options;
161 isl_printer *p;
162 struct isl_obj obj;
163 int r = EXIT_SUCCESS;
165 options = cg_options_new_with_defaults();
166 assert(options);
167 argc = cg_options_parse(options, argc, argv, ISL_ARG_ALL);
169 ctx = isl_ctx_alloc_with_options(&options_args, options);
171 s = isl_stream_new_file(ctx, stdin);
172 obj = isl_stream_read_obj(s);
173 if (obj.v == NULL) {
174 r = EXIT_FAILURE;
175 } else if (obj.type == isl_obj_map) {
176 isl_union_map *umap;
178 umap = isl_union_map_from_map(obj.v);
179 tree = construct_ast_from_union_map(umap, s);
180 } else if (obj.type == isl_obj_union_map) {
181 tree = construct_ast_from_union_map(obj.v, s);
182 } else if (obj.type == isl_obj_schedule) {
183 tree = construct_ast_from_schedule(obj.v);
184 } else {
185 obj.type->free(obj.v);
186 isl_die(ctx, isl_error_invalid, "unknown input",
187 r = EXIT_FAILURE);
189 isl_stream_free(s);
191 p = isl_printer_to_file(ctx, stdout);
192 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
193 p = isl_printer_print_ast_node(p, tree);
194 isl_printer_free(p);
196 isl_ast_node_free(tree);
198 isl_ctx_free(ctx);
199 return r;