add isl_schedule_node_get_domain
[isl.git] / codegen.c
blob78483f7ea1d7e646b180e8356f2b90ae0d025c70
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>
28 #include <isl/schedule_node.h>
30 struct options {
31 struct isl_options *isl;
32 unsigned atomic;
33 unsigned separate;
36 ISL_ARGS_START(struct options, options_args)
37 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
38 ISL_ARG_BOOL(struct options, atomic, 0, "atomic", 0,
39 "globally set the atomic option")
40 ISL_ARG_BOOL(struct options, separate, 0, "separate", 0,
41 "globally set the separate option")
42 ISL_ARGS_END
44 ISL_ARG_DEF(cg_options, struct options, options_args)
45 ISL_ARG_CTX_DEF(cg_options, struct options, options_args)
47 /* Return a universal, 1-dimensional set with the given name.
49 static __isl_give isl_union_set *universe(isl_ctx *ctx, const char *name)
51 isl_space *space;
53 space = isl_space_set_alloc(ctx, 0, 1);
54 space = isl_space_set_tuple_name(space, isl_dim_set, name);
55 return isl_union_set_from_set(isl_set_universe(space));
58 /* Set the "name" option for the entire schedule domain.
60 static __isl_give isl_union_map *set_universe(__isl_take isl_union_map *opt,
61 __isl_keep isl_union_map *schedule, const char *name)
63 isl_ctx *ctx;
64 isl_union_set *domain, *target;
65 isl_union_map *option;
67 ctx = isl_union_map_get_ctx(opt);
69 domain = isl_union_map_range(isl_union_map_copy(schedule));
70 domain = isl_union_set_universe(domain);
71 target = universe(ctx, name);
72 option = isl_union_map_from_domain_and_range(domain, target);
73 opt = isl_union_map_union(opt, option);
75 return opt;
78 /* Update the build options based on the user-specified options.
80 * If the --separate or --atomic options were specified, then
81 * we clear any separate or atomic options that may already exist in "opt".
83 static __isl_give isl_ast_build *set_options(__isl_take isl_ast_build *build,
84 __isl_take isl_union_map *opt, struct options *options,
85 __isl_keep isl_union_map *schedule)
87 if (options->separate || options->atomic) {
88 isl_ctx *ctx;
89 isl_union_set *target;
91 ctx = isl_union_map_get_ctx(schedule);
93 target = universe(ctx, "separate");
94 opt = isl_union_map_subtract_range(opt, target);
95 target = universe(ctx, "atomic");
96 opt = isl_union_map_subtract_range(opt, target);
99 if (options->separate)
100 opt = set_universe(opt, schedule, "separate");
101 if (options->atomic)
102 opt = set_universe(opt, schedule, "atomic");
104 build = isl_ast_build_set_options(build, opt);
106 return build;
109 /* Construct an AST in case the schedule is specified by a union map.
111 * We read the context and the options from "s" and construct the AST.
113 static __isl_give isl_ast_node *construct_ast_from_union_map(
114 __isl_take isl_union_map *schedule, __isl_keep isl_stream *s)
116 isl_set *context;
117 isl_union_map *options_map;
118 isl_ast_build *build;
119 isl_ast_node *tree;
120 struct options *options;
122 options = isl_ctx_peek_cg_options(isl_stream_get_ctx(s));
124 context = isl_stream_read_set(s);
125 options_map = isl_stream_read_union_map(s);
127 build = isl_ast_build_from_context(context);
128 build = set_options(build, options_map, options, schedule);
129 tree = isl_ast_build_node_from_schedule_map(build, schedule);
130 isl_ast_build_free(build);
132 return tree;
135 /* If "node" is a band node, then replace the AST build options
136 * by "options".
138 static __isl_give isl_schedule_node *node_set_options(
139 __isl_take isl_schedule_node *node, void *user)
141 enum isl_ast_loop_type *type = user;
142 int i, n;
144 if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
145 return node;
147 n = isl_schedule_node_band_n_member(node);
148 for (i = 0; i < n; ++i)
149 node = isl_schedule_node_band_member_set_ast_loop_type(node,
150 i, *type);
151 return node;
154 /* Replace the AST build options on all band nodes if requested
155 * by the user.
157 static __isl_give isl_schedule *schedule_set_options(
158 __isl_take isl_schedule *schedule, struct options *options)
160 enum isl_ast_loop_type type;
162 if (!options->separate && !options->atomic)
163 return schedule;
165 type = options->separate ? isl_ast_loop_separate : isl_ast_loop_atomic;
166 schedule = isl_schedule_map_schedule_node(schedule,
167 &node_set_options, &type);
169 return schedule;
172 /* Construct an AST in case the schedule is specified by a schedule tree.
174 static __isl_give isl_ast_node *construct_ast_from_schedule(
175 __isl_take isl_schedule *schedule)
177 isl_ast_build *build;
178 isl_ast_node *tree;
179 struct options *options;
181 options = isl_ctx_peek_cg_options(isl_schedule_get_ctx(schedule));
183 build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
184 schedule = schedule_set_options(schedule, options);
185 tree = isl_ast_build_node_from_schedule(build, schedule);
186 isl_ast_build_free(build);
188 return tree;
191 /* Read an object from stdin.
192 * If it is a (union) map, then assume an input specified by
193 * schedule map, context and options and construct an AST from
194 * those elements
195 * If it is a schedule object, then construct the AST from the schedule.
197 int main(int argc, char **argv)
199 isl_ctx *ctx;
200 isl_stream *s;
201 isl_ast_node *tree = NULL;
202 struct options *options;
203 isl_printer *p;
204 struct isl_obj obj;
205 int r = EXIT_SUCCESS;
207 options = cg_options_new_with_defaults();
208 assert(options);
209 argc = cg_options_parse(options, argc, argv, ISL_ARG_ALL);
211 ctx = isl_ctx_alloc_with_options(&options_args, options);
213 s = isl_stream_new_file(ctx, stdin);
214 obj = isl_stream_read_obj(s);
215 if (obj.v == NULL) {
216 r = EXIT_FAILURE;
217 } else if (obj.type == isl_obj_map) {
218 isl_union_map *umap;
220 umap = isl_union_map_from_map(obj.v);
221 tree = construct_ast_from_union_map(umap, s);
222 } else if (obj.type == isl_obj_union_map) {
223 tree = construct_ast_from_union_map(obj.v, s);
224 } else if (obj.type == isl_obj_schedule) {
225 tree = construct_ast_from_schedule(obj.v);
226 } else {
227 obj.type->free(obj.v);
228 isl_die(ctx, isl_error_invalid, "unknown input",
229 r = EXIT_FAILURE);
231 isl_stream_free(s);
233 p = isl_printer_to_file(ctx, stdout);
234 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
235 p = isl_printer_print_ast_node(p, tree);
236 isl_printer_free(p);
238 isl_ast_node_free(tree);
240 isl_ctx_free(ctx);
241 return r;