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
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.
18 * - a relation describing AST generation options
24 #include <isl/ast_build.h>
25 #include <isl/options.h>
27 #include <isl/stream.h>
28 #include <isl/schedule_node.h>
31 struct isl_options
*isl
;
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")
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
)
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
)
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
);
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
) {
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");
102 opt
= set_universe(opt
, schedule
, "atomic");
104 build
= isl_ast_build_set_options(build
, opt
);
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
)
117 isl_union_map
*options_map
;
118 isl_ast_build
*build
;
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
);
135 /* If "node" is a band node, then replace the AST build 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
;
144 if (isl_schedule_node_get_type(node
) != isl_schedule_node_band
)
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
,
154 /* Replace the AST build options on all band nodes if requested
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
)
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
);
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
;
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
);
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
195 * If it is a schedule object, then construct the AST from the schedule.
197 int main(int argc
, char **argv
)
201 isl_ast_node
*tree
= NULL
;
202 struct options
*options
;
205 int r
= EXIT_SUCCESS
;
207 options
= cg_options_new_with_defaults();
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
);
217 } else if (obj
.type
== isl_obj_map
) {
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
);
227 obj
.type
->free(obj
.v
);
228 isl_die(ctx
, isl_error_invalid
, "unknown input",
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
);
238 isl_ast_node_free(tree
);