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