codegen.c revision 1.1 1 1.1 mrg /*
2 1.1 mrg * Copyright 2012,2014 Ecole Normale Superieure
3 1.1 mrg *
4 1.1 mrg * Use of this software is governed by the MIT license
5 1.1 mrg *
6 1.1 mrg * Written by Sven Verdoolaege,
7 1.1 mrg * Ecole Normale Superieure, 45 rue dUlm, 75230 Paris, France
8 1.1 mrg */
9 1.1 mrg
10 1.1 mrg /* This program prints an AST that scans the domain elements of
11 1.1 mrg * the domain of a given schedule in the order specified by
12 1.1 mrg * the schedule tree or by their image(s) in the schedule map.
13 1.1 mrg *
14 1.1 mrg * The input consists of either a schedule tree or
15 1.1 mrg * a sequence of three sets/relations.
16 1.1 mrg * - a schedule map
17 1.1 mrg * - a context
18 1.1 mrg * - a relation describing AST generation options
19 1.1 mrg */
20 1.1 mrg
21 1.1 mrg #include <assert.h>
22 1.1 mrg #include <stdlib.h>
23 1.1 mrg #include <isl/ast.h>
24 1.1 mrg #include <isl/ast_build.h>
25 1.1 mrg #include <isl/options.h>
26 1.1 mrg #include <isl/space.h>
27 1.1 mrg #include <isl/set.h>
28 1.1 mrg #include <isl/union_set.h>
29 1.1 mrg #include <isl/union_map.h>
30 1.1 mrg #include <isl/stream.h>
31 1.1 mrg #include <isl/schedule_node.h>
32 1.1 mrg
33 1.1 mrg struct options {
34 1.1 mrg struct isl_options *isl;
35 1.1 mrg unsigned atomic;
36 1.1 mrg unsigned separate;
37 1.1 mrg };
38 1.1 mrg
39 1.1 mrg ISL_ARGS_START(struct options, options_args)
40 1.1 mrg ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
41 1.1 mrg ISL_ARG_BOOL(struct options, atomic, 0, "atomic", 0,
42 1.1 mrg "globally set the atomic option")
43 1.1 mrg ISL_ARG_BOOL(struct options, separate, 0, "separate", 0,
44 1.1 mrg "globally set the separate option")
45 1.1 mrg ISL_ARGS_END
46 1.1 mrg
47 1.1 mrg ISL_ARG_DEF(cg_options, struct options, options_args)
48 1.1 mrg ISL_ARG_CTX_DEF(cg_options, struct options, options_args)
49 1.1 mrg
50 1.1 mrg /* Return a universal, 1-dimensional set with the given name.
51 1.1 mrg */
52 1.1 mrg static __isl_give isl_union_set *universe(isl_ctx *ctx, const char *name)
53 1.1 mrg {
54 1.1 mrg isl_space *space;
55 1.1 mrg
56 1.1 mrg space = isl_space_set_alloc(ctx, 0, 1);
57 1.1 mrg space = isl_space_set_tuple_name(space, isl_dim_set, name);
58 1.1 mrg return isl_union_set_from_set(isl_set_universe(space));
59 1.1 mrg }
60 1.1 mrg
61 1.1 mrg /* Set the "name" option for the entire schedule domain.
62 1.1 mrg */
63 1.1 mrg static __isl_give isl_union_map *set_universe(__isl_take isl_union_map *opt,
64 1.1 mrg __isl_keep isl_union_map *schedule, const char *name)
65 1.1 mrg {
66 1.1 mrg isl_ctx *ctx;
67 1.1 mrg isl_union_set *domain, *target;
68 1.1 mrg isl_union_map *option;
69 1.1 mrg
70 1.1 mrg ctx = isl_union_map_get_ctx(opt);
71 1.1 mrg
72 1.1 mrg domain = isl_union_map_range(isl_union_map_copy(schedule));
73 1.1 mrg domain = isl_union_set_universe(domain);
74 1.1 mrg target = universe(ctx, name);
75 1.1 mrg option = isl_union_map_from_domain_and_range(domain, target);
76 1.1 mrg opt = isl_union_map_union(opt, option);
77 1.1 mrg
78 1.1 mrg return opt;
79 1.1 mrg }
80 1.1 mrg
81 1.1 mrg /* Update the build options based on the user-specified options.
82 1.1 mrg *
83 1.1 mrg * If the --separate or --atomic options were specified, then
84 1.1 mrg * we clear any separate or atomic options that may already exist in "opt".
85 1.1 mrg */
86 1.1 mrg static __isl_give isl_ast_build *set_options(__isl_take isl_ast_build *build,
87 1.1 mrg __isl_take isl_union_map *opt, struct options *options,
88 1.1 mrg __isl_keep isl_union_map *schedule)
89 1.1 mrg {
90 1.1 mrg if (options->separate || options->atomic) {
91 1.1 mrg isl_ctx *ctx;
92 1.1 mrg isl_union_set *target;
93 1.1 mrg
94 1.1 mrg ctx = isl_union_map_get_ctx(schedule);
95 1.1 mrg
96 1.1 mrg target = universe(ctx, "separate");
97 1.1 mrg opt = isl_union_map_subtract_range(opt, target);
98 1.1 mrg target = universe(ctx, "atomic");
99 1.1 mrg opt = isl_union_map_subtract_range(opt, target);
100 1.1 mrg }
101 1.1 mrg
102 1.1 mrg if (options->separate)
103 1.1 mrg opt = set_universe(opt, schedule, "separate");
104 1.1 mrg if (options->atomic)
105 1.1 mrg opt = set_universe(opt, schedule, "atomic");
106 1.1 mrg
107 1.1 mrg build = isl_ast_build_set_options(build, opt);
108 1.1 mrg
109 1.1 mrg return build;
110 1.1 mrg }
111 1.1 mrg
112 1.1 mrg /* Construct an AST in case the schedule is specified by a union map.
113 1.1 mrg *
114 1.1 mrg * We read the context and the options from "s" and construct the AST.
115 1.1 mrg */
116 1.1 mrg static __isl_give isl_ast_node *construct_ast_from_union_map(
117 1.1 mrg __isl_take isl_union_map *schedule, __isl_keep isl_stream *s)
118 1.1 mrg {
119 1.1 mrg isl_set *context;
120 1.1 mrg isl_union_map *options_map;
121 1.1 mrg isl_ast_build *build;
122 1.1 mrg isl_ast_node *tree;
123 1.1 mrg struct options *options;
124 1.1 mrg
125 1.1 mrg options = isl_ctx_peek_cg_options(isl_stream_get_ctx(s));
126 1.1 mrg
127 1.1 mrg context = isl_stream_read_set(s);
128 1.1 mrg options_map = isl_stream_read_union_map(s);
129 1.1 mrg
130 1.1 mrg build = isl_ast_build_from_context(context);
131 1.1 mrg build = set_options(build, options_map, options, schedule);
132 1.1 mrg tree = isl_ast_build_node_from_schedule_map(build, schedule);
133 1.1 mrg isl_ast_build_free(build);
134 1.1 mrg
135 1.1 mrg return tree;
136 1.1 mrg }
137 1.1 mrg
138 1.1 mrg /* If "node" is a band node, then replace the AST build options
139 1.1 mrg * by "options".
140 1.1 mrg */
141 1.1 mrg static __isl_give isl_schedule_node *node_set_options(
142 1.1 mrg __isl_take isl_schedule_node *node, void *user)
143 1.1 mrg {
144 1.1 mrg enum isl_ast_loop_type *type = user;
145 1.1 mrg int i;
146 1.1 mrg isl_size n;
147 1.1 mrg
148 1.1 mrg if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
149 1.1 mrg return node;
150 1.1 mrg
151 1.1 mrg n = isl_schedule_node_band_n_member(node);
152 1.1 mrg if (n < 0)
153 1.1 mrg return isl_schedule_node_free(node);
154 1.1 mrg for (i = 0; i < n; ++i)
155 1.1 mrg node = isl_schedule_node_band_member_set_ast_loop_type(node,
156 1.1 mrg i, *type);
157 1.1 mrg return node;
158 1.1 mrg }
159 1.1 mrg
160 1.1 mrg /* Replace the AST build options on all band nodes if requested
161 1.1 mrg * by the user.
162 1.1 mrg */
163 1.1 mrg static __isl_give isl_schedule *schedule_set_options(
164 1.1 mrg __isl_take isl_schedule *schedule, struct options *options)
165 1.1 mrg {
166 1.1 mrg enum isl_ast_loop_type type;
167 1.1 mrg
168 1.1 mrg if (!options->separate && !options->atomic)
169 1.1 mrg return schedule;
170 1.1 mrg
171 1.1 mrg type = options->separate ? isl_ast_loop_separate : isl_ast_loop_atomic;
172 1.1 mrg schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
173 1.1 mrg &node_set_options, &type);
174 1.1 mrg
175 1.1 mrg return schedule;
176 1.1 mrg }
177 1.1 mrg
178 1.1 mrg /* Construct an AST in case the schedule is specified by a schedule tree.
179 1.1 mrg */
180 1.1 mrg static __isl_give isl_ast_node *construct_ast_from_schedule(
181 1.1 mrg __isl_take isl_schedule *schedule)
182 1.1 mrg {
183 1.1 mrg isl_ast_build *build;
184 1.1 mrg isl_ast_node *tree;
185 1.1 mrg struct options *options;
186 1.1 mrg
187 1.1 mrg options = isl_ctx_peek_cg_options(isl_schedule_get_ctx(schedule));
188 1.1 mrg
189 1.1 mrg build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
190 1.1 mrg schedule = schedule_set_options(schedule, options);
191 1.1 mrg tree = isl_ast_build_node_from_schedule(build, schedule);
192 1.1 mrg isl_ast_build_free(build);
193 1.1 mrg
194 1.1 mrg return tree;
195 1.1 mrg }
196 1.1 mrg
197 1.1 mrg /* Read an object from stdin.
198 1.1 mrg * If it is a (union) map, then assume an input specified by
199 1.1 mrg * schedule map, context and options and construct an AST from
200 1.1 mrg * those elements
201 1.1 mrg * If it is a schedule object, then construct the AST from the schedule.
202 1.1 mrg */
203 1.1 mrg int main(int argc, char **argv)
204 1.1 mrg {
205 1.1 mrg isl_ctx *ctx;
206 1.1 mrg isl_stream *s;
207 1.1 mrg isl_ast_node *tree = NULL;
208 1.1 mrg struct options *options;
209 1.1 mrg isl_printer *p;
210 1.1 mrg struct isl_obj obj;
211 1.1 mrg int r = EXIT_SUCCESS;
212 1.1 mrg
213 1.1 mrg options = cg_options_new_with_defaults();
214 1.1 mrg assert(options);
215 1.1 mrg ctx = isl_ctx_alloc_with_options(&options_args, options);
216 1.1 mrg isl_options_set_ast_build_detect_min_max(ctx, 1);
217 1.1 mrg isl_options_set_ast_print_outermost_block(ctx, 0);
218 1.1 mrg argc = cg_options_parse(options, argc, argv, ISL_ARG_ALL);
219 1.1 mrg
220 1.1 mrg s = isl_stream_new_file(ctx, stdin);
221 1.1 mrg obj = isl_stream_read_obj(s);
222 1.1 mrg if (obj.v == NULL) {
223 1.1 mrg r = EXIT_FAILURE;
224 1.1 mrg } else if (obj.type == isl_obj_map) {
225 1.1 mrg isl_union_map *umap;
226 1.1 mrg
227 1.1 mrg umap = isl_union_map_from_map(obj.v);
228 1.1 mrg tree = construct_ast_from_union_map(umap, s);
229 1.1 mrg } else if (obj.type == isl_obj_union_map) {
230 1.1 mrg tree = construct_ast_from_union_map(obj.v, s);
231 1.1 mrg } else if (obj.type == isl_obj_schedule) {
232 1.1 mrg tree = construct_ast_from_schedule(obj.v);
233 1.1 mrg } else {
234 1.1 mrg obj.type->free(obj.v);
235 1.1 mrg isl_die(ctx, isl_error_invalid, "unknown input",
236 1.1 mrg r = EXIT_FAILURE);
237 1.1 mrg }
238 1.1 mrg isl_stream_free(s);
239 1.1 mrg
240 1.1 mrg p = isl_printer_to_file(ctx, stdout);
241 1.1 mrg p = isl_printer_set_output_format(p, ISL_FORMAT_C);
242 1.1 mrg p = isl_printer_print_ast_node(p, tree);
243 1.1 mrg isl_printer_free(p);
244 1.1 mrg
245 1.1 mrg isl_ast_node_free(tree);
246 1.1 mrg
247 1.1 mrg isl_ctx_free(ctx);
248 1.1 mrg return r;
249 1.1 mrg }
250