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>
30 struct isl_options
*isl
;
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")
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
)
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
)
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
);
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
) {
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");
101 opt
= set_universe(opt
, schedule
, "atomic");
103 build
= isl_ast_build_set_options(build
, opt
);
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
)
116 isl_union_map
*options_map
;
117 isl_ast_build
*build
;
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
);
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
;
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
);
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
153 * If it is a schedule object, then construct the AST from the schedule.
155 int main(int argc
, char **argv
)
159 isl_ast_node
*tree
= NULL
;
160 struct options
*options
;
163 int r
= EXIT_SUCCESS
;
165 options
= cg_options_new_with_defaults();
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
);
175 } else if (obj
.type
== isl_obj_map
) {
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
);
185 obj
.type
->free(obj
.v
);
186 isl_die(ctx
, isl_error_invalid
, "unknown input",
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
);
196 isl_ast_node_free(tree
);