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