Merge branch 'maint'
[isl.git] / isl_ast_build.c
blob80623f07bc85c04298e2cb75271700f778baabe0
1 /*
2 * Copyright 2012-2013 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl/map.h>
14 #include <isl/aff.h>
15 #include <isl/map.h>
16 #include <isl_ast_build_private.h>
17 #include <isl_ast_private.h>
19 /* Construct a map that isolates the current dimension.
21 * Essentially, the current dimension of "set" is moved to the single output
22 * dimension in the result, with the current dimension in the domain replaced
23 * by an unconstrained variable.
25 __isl_give isl_map *isl_ast_build_map_to_iterator(
26 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
28 isl_map *map;
30 map = isl_map_from_domain(set);
31 map = isl_map_add_dims(map, isl_dim_out, 1);
33 if (!build)
34 return isl_map_free(map);
36 map = isl_map_equate(map, isl_dim_in, build->depth, isl_dim_out, 0);
37 map = isl_map_eliminate(map, isl_dim_in, build->depth, 1);
39 return map;
42 /* Initialize the information derived during the AST generation to default
43 * values for a schedule domain in "space".
45 * We also check that the remaining fields are not NULL so that
46 * the calling functions don't have to perform this test.
48 static __isl_give isl_ast_build *isl_ast_build_init_derived(
49 __isl_take isl_ast_build *build, __isl_take isl_space *space)
51 isl_ctx *ctx;
52 isl_vec *strides;
54 build = isl_ast_build_cow(build);
55 if (!build || !build->domain)
56 goto error;
58 ctx = isl_ast_build_get_ctx(build);
59 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
60 strides = isl_vec_set_si(strides, 1);
62 isl_vec_free(build->strides);
63 build->strides = strides;
65 space = isl_space_map_from_set(space);
66 isl_multi_aff_free(build->offsets);
67 build->offsets = isl_multi_aff_zero(isl_space_copy(space));
68 isl_multi_aff_free(build->values);
69 build->values = isl_multi_aff_identity(isl_space_copy(space));
70 isl_multi_aff_free(build->internal2input);
71 build->internal2input = isl_multi_aff_identity(space);
73 if (!build->iterators || !build->domain || !build->generated ||
74 !build->pending || !build->values || !build->internal2input ||
75 !build->strides || !build->offsets || !build->options)
76 return isl_ast_build_free(build);
78 return build;
79 error:
80 isl_space_free(space);
81 return isl_ast_build_free(build);
84 /* Return an isl_id called "c%d", with "%d" set to "i".
85 * If an isl_id with such a name already appears among the parameters
86 * in build->domain, then adjust the name to "c%d_%d".
88 static __isl_give isl_id *generate_name(isl_ctx *ctx, int i,
89 __isl_keep isl_ast_build *build)
91 int j;
92 char name[16];
93 isl_set *dom = build->domain;
95 snprintf(name, sizeof(name), "c%d", i);
96 j = 0;
97 while (isl_set_find_dim_by_name(dom, isl_dim_param, name) >= 0)
98 snprintf(name, sizeof(name), "c%d_%d", i, j++);
99 return isl_id_alloc(ctx, name, NULL);
102 /* Create an isl_ast_build with "set" as domain.
104 * The input set is usually a parameter domain, but we currently allow it to
105 * be any kind of set. We set the domain of the returned isl_ast_build
106 * to "set" and initialize all the other fields to default values.
108 __isl_give isl_ast_build *isl_ast_build_from_context(__isl_take isl_set *set)
110 int i, n;
111 isl_ctx *ctx;
112 isl_space *space;
113 isl_ast_build *build;
115 set = isl_set_compute_divs(set);
116 if (!set)
117 return NULL;
119 ctx = isl_set_get_ctx(set);
121 build = isl_calloc_type(ctx, isl_ast_build);
122 if (!build)
123 goto error;
125 build->ref = 1;
126 build->domain = set;
127 build->generated = isl_set_copy(build->domain);
128 build->pending = isl_set_universe(isl_set_get_space(build->domain));
129 build->options = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
130 n = isl_set_dim(set, isl_dim_set);
131 build->depth = n;
132 build->iterators = isl_id_list_alloc(ctx, n);
133 for (i = 0; i < n; ++i) {
134 isl_id *id;
135 if (isl_set_has_dim_id(set, isl_dim_set, i))
136 id = isl_set_get_dim_id(set, isl_dim_set, i);
137 else
138 id = generate_name(ctx, i, build);
139 build->iterators = isl_id_list_add(build->iterators, id);
141 space = isl_set_get_space(set);
142 if (isl_space_is_params(space))
143 space = isl_space_set_from_params(space);
145 return isl_ast_build_init_derived(build, space);
146 error:
147 isl_set_free(set);
148 return NULL;
151 /* Create an isl_ast_build with a universe (parametric) context.
153 __isl_give isl_ast_build *isl_ast_build_alloc(isl_ctx *ctx)
155 isl_space *space;
156 isl_set *context;
158 space = isl_space_params_alloc(ctx, 0);
159 context = isl_set_universe(space);
161 return isl_ast_build_from_context(context);
164 __isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
166 if (!build)
167 return NULL;
169 build->ref++;
170 return build;
173 __isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
175 isl_ctx *ctx;
176 isl_ast_build *dup;
178 if (!build)
179 return NULL;
181 ctx = isl_ast_build_get_ctx(build);
182 dup = isl_calloc_type(ctx, isl_ast_build);
183 if (!dup)
184 return NULL;
186 dup->ref = 1;
187 dup->outer_pos = build->outer_pos;
188 dup->depth = build->depth;
189 dup->iterators = isl_id_list_copy(build->iterators);
190 dup->domain = isl_set_copy(build->domain);
191 dup->generated = isl_set_copy(build->generated);
192 dup->pending = isl_set_copy(build->pending);
193 dup->values = isl_multi_aff_copy(build->values);
194 dup->internal2input = isl_multi_aff_copy(build->internal2input);
195 dup->value = isl_pw_aff_copy(build->value);
196 dup->strides = isl_vec_copy(build->strides);
197 dup->offsets = isl_multi_aff_copy(build->offsets);
198 dup->executed = isl_union_map_copy(build->executed);
199 dup->single_valued = build->single_valued;
200 dup->options = isl_union_map_copy(build->options);
201 dup->at_each_domain = build->at_each_domain;
202 dup->at_each_domain_user = build->at_each_domain_user;
203 dup->before_each_for = build->before_each_for;
204 dup->before_each_for_user = build->before_each_for_user;
205 dup->after_each_for = build->after_each_for;
206 dup->after_each_for_user = build->after_each_for_user;
207 dup->before_each_mark = build->before_each_mark;
208 dup->before_each_mark_user = build->before_each_mark_user;
209 dup->after_each_mark = build->after_each_mark;
210 dup->after_each_mark_user = build->after_each_mark_user;
211 dup->create_leaf = build->create_leaf;
212 dup->create_leaf_user = build->create_leaf_user;
213 dup->node = isl_schedule_node_copy(build->node);
214 if (build->loop_type) {
215 int i;
217 dup->n = build->n;
218 dup->loop_type = isl_alloc_array(ctx,
219 enum isl_ast_loop_type, dup->n);
220 if (dup->n && !dup->loop_type)
221 return isl_ast_build_free(dup);
222 for (i = 0; i < dup->n; ++i)
223 dup->loop_type[i] = build->loop_type[i];
226 if (!dup->iterators || !dup->domain || !dup->generated ||
227 !dup->pending || !dup->values ||
228 !dup->strides || !dup->offsets || !dup->options ||
229 (build->internal2input && !dup->internal2input) ||
230 (build->executed && !dup->executed) ||
231 (build->value && !dup->value) ||
232 (build->node && !dup->node))
233 return isl_ast_build_free(dup);
235 return dup;
238 /* Align the parameters of "build" to those of "model", introducing
239 * additional parameters if needed.
241 __isl_give isl_ast_build *isl_ast_build_align_params(
242 __isl_take isl_ast_build *build, __isl_take isl_space *model)
244 build = isl_ast_build_cow(build);
245 if (!build)
246 goto error;
248 build->domain = isl_set_align_params(build->domain,
249 isl_space_copy(model));
250 build->generated = isl_set_align_params(build->generated,
251 isl_space_copy(model));
252 build->pending = isl_set_align_params(build->pending,
253 isl_space_copy(model));
254 build->values = isl_multi_aff_align_params(build->values,
255 isl_space_copy(model));
256 build->offsets = isl_multi_aff_align_params(build->offsets,
257 isl_space_copy(model));
258 build->options = isl_union_map_align_params(build->options,
259 isl_space_copy(model));
260 if (build->internal2input) {
261 build->internal2input =
262 isl_multi_aff_align_params(build->internal2input,
263 model);
264 if (!build->internal2input)
265 return isl_ast_build_free(build);
266 } else {
267 isl_space_free(model);
270 if (!build->domain || !build->values || !build->offsets ||
271 !build->options)
272 return isl_ast_build_free(build);
274 return build;
275 error:
276 isl_space_free(model);
277 return NULL;
280 __isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
282 if (!build)
283 return NULL;
285 if (build->ref == 1)
286 return build;
287 build->ref--;
288 return isl_ast_build_dup(build);
291 __isl_null isl_ast_build *isl_ast_build_free(
292 __isl_take isl_ast_build *build)
294 if (!build)
295 return NULL;
297 if (--build->ref > 0)
298 return NULL;
300 isl_id_list_free(build->iterators);
301 isl_set_free(build->domain);
302 isl_set_free(build->generated);
303 isl_set_free(build->pending);
304 isl_multi_aff_free(build->values);
305 isl_multi_aff_free(build->internal2input);
306 isl_pw_aff_free(build->value);
307 isl_vec_free(build->strides);
308 isl_multi_aff_free(build->offsets);
309 isl_multi_aff_free(build->schedule_map);
310 isl_union_map_free(build->executed);
311 isl_union_map_free(build->options);
312 isl_schedule_node_free(build->node);
313 free(build->loop_type);
314 isl_set_free(build->isolated);
316 free(build);
318 return NULL;
321 isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
323 return build ? isl_set_get_ctx(build->domain) : NULL;
326 /* Replace build->options by "options".
328 __isl_give isl_ast_build *isl_ast_build_set_options(
329 __isl_take isl_ast_build *build, __isl_take isl_union_map *options)
331 build = isl_ast_build_cow(build);
333 if (!build || !options)
334 goto error;
336 isl_union_map_free(build->options);
337 build->options = options;
339 return build;
340 error:
341 isl_union_map_free(options);
342 return isl_ast_build_free(build);
345 /* Set the iterators for the next code generation.
347 * If we still have some iterators left from the previous code generation
348 * (if any) or if iterators have already been set by a previous
349 * call to this function, then we remove them first.
351 __isl_give isl_ast_build *isl_ast_build_set_iterators(
352 __isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
354 int dim, n_it;
356 build = isl_ast_build_cow(build);
357 if (!build)
358 goto error;
360 dim = isl_set_dim(build->domain, isl_dim_set);
361 n_it = isl_id_list_n_id(build->iterators);
362 if (n_it < dim)
363 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
364 "isl_ast_build in inconsistent state", goto error);
365 if (n_it > dim)
366 build->iterators = isl_id_list_drop(build->iterators,
367 dim, n_it - dim);
368 build->iterators = isl_id_list_concat(build->iterators, iterators);
369 if (!build->iterators)
370 return isl_ast_build_free(build);
372 return build;
373 error:
374 isl_id_list_free(iterators);
375 return isl_ast_build_free(build);
378 /* Set the "at_each_domain" callback of "build" to "fn".
380 __isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
381 __isl_take isl_ast_build *build,
382 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
383 __isl_keep isl_ast_build *build, void *user), void *user)
385 build = isl_ast_build_cow(build);
387 if (!build)
388 return NULL;
390 build->at_each_domain = fn;
391 build->at_each_domain_user = user;
393 return build;
396 /* Set the "before_each_for" callback of "build" to "fn".
398 __isl_give isl_ast_build *isl_ast_build_set_before_each_for(
399 __isl_take isl_ast_build *build,
400 __isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
401 void *user), void *user)
403 build = isl_ast_build_cow(build);
405 if (!build)
406 return NULL;
408 build->before_each_for = fn;
409 build->before_each_for_user = user;
411 return build;
414 /* Set the "after_each_for" callback of "build" to "fn".
416 __isl_give isl_ast_build *isl_ast_build_set_after_each_for(
417 __isl_take isl_ast_build *build,
418 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
419 __isl_keep isl_ast_build *build, void *user), void *user)
421 build = isl_ast_build_cow(build);
423 if (!build)
424 return NULL;
426 build->after_each_for = fn;
427 build->after_each_for_user = user;
429 return build;
432 /* Set the "before_each_mark" callback of "build" to "fn".
434 __isl_give isl_ast_build *isl_ast_build_set_before_each_mark(
435 __isl_take isl_ast_build *build,
436 int (*fn)(__isl_keep isl_id *mark, __isl_keep isl_ast_build *build,
437 void *user), void *user)
439 build = isl_ast_build_cow(build);
441 if (!build)
442 return NULL;
444 build->before_each_mark = fn;
445 build->before_each_mark_user = user;
447 return build;
450 /* Set the "after_each_mark" callback of "build" to "fn".
452 __isl_give isl_ast_build *isl_ast_build_set_after_each_mark(
453 __isl_take isl_ast_build *build,
454 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
455 __isl_keep isl_ast_build *build, void *user), void *user)
457 build = isl_ast_build_cow(build);
459 if (!build)
460 return NULL;
462 build->after_each_mark = fn;
463 build->after_each_mark_user = user;
465 return build;
468 /* Set the "create_leaf" callback of "build" to "fn".
470 __isl_give isl_ast_build *isl_ast_build_set_create_leaf(
471 __isl_take isl_ast_build *build,
472 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
473 void *user), void *user)
475 build = isl_ast_build_cow(build);
477 if (!build)
478 return NULL;
480 build->create_leaf = fn;
481 build->create_leaf_user = user;
483 return build;
486 /* Clear all information that is specific to this code generation
487 * and that is (probably) not meaningful to any nested code generation.
489 __isl_give isl_ast_build *isl_ast_build_clear_local_info(
490 __isl_take isl_ast_build *build)
492 isl_space *space;
494 build = isl_ast_build_cow(build);
495 if (!build)
496 return NULL;
498 space = isl_union_map_get_space(build->options);
499 isl_union_map_free(build->options);
500 build->options = isl_union_map_empty(space);
502 build->at_each_domain = NULL;
503 build->at_each_domain_user = NULL;
504 build->before_each_for = NULL;
505 build->before_each_for_user = NULL;
506 build->after_each_for = NULL;
507 build->after_each_for_user = NULL;
508 build->before_each_mark = NULL;
509 build->before_each_mark_user = NULL;
510 build->after_each_mark = NULL;
511 build->after_each_mark_user = NULL;
512 build->create_leaf = NULL;
513 build->create_leaf_user = NULL;
515 if (!build->options)
516 return isl_ast_build_free(build);
518 return build;
521 /* Have any loops been eliminated?
522 * That is, do any of the original schedule dimensions have a fixed
523 * value that has been substituted?
525 static int any_eliminated(isl_ast_build *build)
527 int i;
529 for (i = 0; i < build->depth; ++i)
530 if (isl_ast_build_has_affine_value(build, i))
531 return 1;
533 return 0;
536 /* Clear build->schedule_map.
537 * This function should be called whenever anything that might affect
538 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
539 * In particular, it should be called when the depth is changed or
540 * when an iterator is determined to have a fixed value.
542 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
544 if (!build)
545 return;
546 isl_multi_aff_free(build->schedule_map);
547 build->schedule_map = NULL;
550 /* Do we need a (non-trivial) schedule map?
551 * That is, is the internal schedule space different from
552 * the external schedule space?
554 * The internal and external schedule spaces are only the same
555 * if code has been generated for the entire schedule and if none
556 * of the loops have been eliminated.
558 __isl_give int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
560 int dim;
562 if (!build)
563 return -1;
565 dim = isl_set_dim(build->domain, isl_dim_set);
566 return build->depth != dim || any_eliminated(build);
569 /* Return a mapping from the internal schedule space to the external
570 * schedule space in the form of an isl_multi_aff.
571 * The internal schedule space originally corresponds to that of the
572 * input schedule. This may change during the code generation if
573 * if isl_ast_build_insert_dim is ever called.
574 * The external schedule space corresponds to the
575 * loops that have been generated.
577 * Currently, the only difference between the internal schedule domain
578 * and the external schedule domain is that some dimensions are projected
579 * out in the external schedule domain. In particular, the dimensions
580 * for which no code has been generated yet and the dimensions that correspond
581 * to eliminated loops.
583 * We cache a copy of the schedule_map in build->schedule_map.
584 * The cache is cleared through isl_ast_build_reset_schedule_map
585 * whenever anything changes that might affect the result of this function.
587 __isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
588 __isl_keep isl_ast_build *build)
590 isl_space *space;
591 isl_multi_aff *ma;
593 if (!build)
594 return NULL;
595 if (build->schedule_map)
596 return isl_multi_aff_copy(build->schedule_map);
598 space = isl_ast_build_get_space(build, 1);
599 space = isl_space_map_from_set(space);
600 ma = isl_multi_aff_identity(space);
601 if (isl_ast_build_need_schedule_map(build)) {
602 int i;
603 int dim = isl_set_dim(build->domain, isl_dim_set);
604 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
605 build->depth, dim - build->depth);
606 for (i = build->depth - 1; i >= 0; --i)
607 if (isl_ast_build_has_affine_value(build, i))
608 ma = isl_multi_aff_drop_dims(ma,
609 isl_dim_out, i, 1);
612 build->schedule_map = ma;
613 return isl_multi_aff_copy(build->schedule_map);
616 /* Return a mapping from the internal schedule space to the external
617 * schedule space in the form of an isl_map.
619 __isl_give isl_map *isl_ast_build_get_schedule_map(
620 __isl_keep isl_ast_build *build)
622 isl_multi_aff *ma;
624 ma = isl_ast_build_get_schedule_map_multi_aff(build);
625 return isl_map_from_multi_aff(ma);
628 /* Return the position of the dimension in build->domain for which
629 * an AST node is currently being generated.
631 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
633 return build ? build->depth : -1;
636 /* Prepare for generating code for the next level.
637 * In particular, increase the depth and reset any information
638 * that is local to the current depth.
640 __isl_give isl_ast_build *isl_ast_build_increase_depth(
641 __isl_take isl_ast_build *build)
643 build = isl_ast_build_cow(build);
644 if (!build)
645 return NULL;
646 build->depth++;
647 isl_ast_build_reset_schedule_map(build);
648 build->value = isl_pw_aff_free(build->value);
649 return build;
652 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
654 if (!build)
655 return;
657 fprintf(stderr, "domain: ");
658 isl_set_dump(build->domain);
659 fprintf(stderr, "generated: ");
660 isl_set_dump(build->generated);
661 fprintf(stderr, "pending: ");
662 isl_set_dump(build->pending);
663 fprintf(stderr, "iterators: ");
664 isl_id_list_dump(build->iterators);
665 fprintf(stderr, "values: ");
666 isl_multi_aff_dump(build->values);
667 if (build->value) {
668 fprintf(stderr, "value: ");
669 isl_pw_aff_dump(build->value);
671 fprintf(stderr, "strides: ");
672 isl_vec_dump(build->strides);
673 fprintf(stderr, "offsets: ");
674 isl_multi_aff_dump(build->offsets);
675 fprintf(stderr, "internal2input: ");
676 isl_multi_aff_dump(build->internal2input);
679 /* Initialize "build" for AST construction in schedule space "space"
680 * in the case that build->domain is a parameter set.
682 * build->iterators is assumed to have been updated already.
684 static __isl_give isl_ast_build *isl_ast_build_init(
685 __isl_take isl_ast_build *build, __isl_take isl_space *space)
687 isl_set *set;
689 build = isl_ast_build_cow(build);
690 if (!build)
691 goto error;
693 set = isl_set_universe(isl_space_copy(space));
694 build->domain = isl_set_intersect_params(isl_set_copy(set),
695 build->domain);
696 build->pending = isl_set_intersect_params(isl_set_copy(set),
697 build->pending);
698 build->generated = isl_set_intersect_params(set, build->generated);
700 return isl_ast_build_init_derived(build, space);
701 error:
702 isl_ast_build_free(build);
703 isl_space_free(space);
704 return NULL;
707 /* Assign "aff" to *user and return -1, effectively extracting
708 * the first (and presumably only) affine expression in the isl_pw_aff
709 * on which this function is used.
711 static int extract_single_piece(__isl_take isl_set *set,
712 __isl_take isl_aff *aff, void *user)
714 isl_aff **p = user;
716 *p = aff;
717 isl_set_free(set);
719 return -1;
722 /* Intersect "set" with the stride constraint of "build", if any.
724 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
725 __isl_keep isl_ast_build *build)
727 isl_set *stride;
729 if (!build)
730 return isl_set_free(set);
731 if (!isl_ast_build_has_stride(build, build->depth))
732 return set;
734 stride = isl_ast_build_get_stride_constraint(build);
735 return isl_set_intersect(set, stride);
738 /* Check if the given bounds on the current dimension (together with
739 * the stride constraint, if any) imply that
740 * this current dimension attains only a single value (in terms of
741 * parameters and outer dimensions).
742 * If so, we record it in build->value.
743 * If, moreover, this value can be represented as a single affine expression,
744 * then we also update build->values, effectively marking the current
745 * dimension as "eliminated".
747 * When computing the gist of the fixed value that can be represented
748 * as a single affine expression, it is important to only take into
749 * account the domain constraints in the original AST build and
750 * not the domain of the affine expression itself.
751 * Otherwise, a [i/3] is changed into a i/3 because we know that i
752 * is a multiple of 3, but then we end up not expressing anywhere
753 * in the context that i is a multiple of 3.
755 static __isl_give isl_ast_build *update_values(
756 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
758 int sv;
759 isl_pw_multi_aff *pma;
760 isl_aff *aff = NULL;
761 isl_map *it_map;
762 isl_set *set;
764 set = isl_set_from_basic_set(bounds);
765 set = isl_set_intersect(set, isl_set_copy(build->domain));
766 set = intersect_stride_constraint(set, build);
767 it_map = isl_ast_build_map_to_iterator(build, set);
769 sv = isl_map_is_single_valued(it_map);
770 if (sv < 0)
771 build = isl_ast_build_free(build);
772 if (!build || !sv) {
773 isl_map_free(it_map);
774 return build;
777 pma = isl_pw_multi_aff_from_map(it_map);
778 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
779 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
780 build->value = isl_pw_aff_coalesce(build->value);
781 isl_pw_multi_aff_free(pma);
783 if (!build->value)
784 return isl_ast_build_free(build);
786 if (isl_pw_aff_n_piece(build->value) != 1)
787 return build;
789 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
791 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
792 if (!build->values)
793 return isl_ast_build_free(build);
794 isl_ast_build_reset_schedule_map(build);
795 return build;
798 /* Update the AST build based on the given loop bounds for
799 * the current dimension and the stride information available in the build.
801 * We first make sure that the bounds do not refer to any iterators
802 * that have already been eliminated.
803 * Then, we check if the bounds imply that the current iterator
804 * has a fixed value.
805 * If they do and if this fixed value can be expressed as a single
806 * affine expression, we eliminate the iterators from the bounds.
807 * Note that we cannot simply plug in this single value using
808 * isl_basic_set_preimage_multi_aff as the single value may only
809 * be defined on a subset of the domain. Plugging in the value
810 * would restrict the build domain to this subset, while this
811 * restriction may not be reflected in the generated code.
812 * Finally, we intersect build->domain with the updated bounds.
813 * We also add the stride constraint unless we have been able
814 * to find a fixed value expressed as a single affine expression.
816 * Note that the check for a fixed value in update_values requires
817 * us to intersect the bounds with the current build domain.
818 * When we intersect build->domain with the updated bounds in
819 * the final step, we make sure that these updated bounds have
820 * not been intersected with the old build->domain.
821 * Otherwise, we would indirectly intersect the build domain with itself,
822 * which can lead to inefficiencies, in particular if the build domain
823 * contains any unknown divs.
825 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
826 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
828 isl_set *set;
830 build = isl_ast_build_cow(build);
831 if (!build)
832 goto error;
834 bounds = isl_basic_set_preimage_multi_aff(bounds,
835 isl_multi_aff_copy(build->values));
836 build = update_values(build, isl_basic_set_copy(bounds));
837 if (!build)
838 goto error;
839 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
840 if (isl_ast_build_has_affine_value(build, build->depth)) {
841 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
842 set = isl_set_compute_divs(set);
843 build->pending = isl_set_intersect(build->pending,
844 isl_set_copy(set));
845 build->domain = isl_set_intersect(build->domain, set);
846 } else {
847 isl_basic_set *generated, *pending;
849 pending = isl_basic_set_copy(bounds);
850 pending = isl_basic_set_drop_constraints_involving_dims(pending,
851 isl_dim_set, build->depth, 1);
852 build->pending = isl_set_intersect(build->pending,
853 isl_set_from_basic_set(pending));
854 generated = isl_basic_set_copy(bounds);
855 generated = isl_basic_set_drop_constraints_not_involving_dims(
856 generated, isl_dim_set, build->depth, 1);
857 build->generated = isl_set_intersect(build->generated,
858 isl_set_from_basic_set(generated));
859 build->domain = isl_set_intersect(build->domain, set);
860 build = isl_ast_build_include_stride(build);
861 if (!build)
862 goto error;
864 isl_basic_set_free(bounds);
866 if (!build->domain || !build->pending || !build->generated)
867 return isl_ast_build_free(build);
869 return build;
870 error:
871 isl_ast_build_free(build);
872 isl_basic_set_free(bounds);
873 return NULL;
876 /* Intersect build->domain with "set", where "set" is specified
877 * in terms of the internal schedule domain.
879 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
880 __isl_take isl_ast_build *build, __isl_take isl_set *set)
882 build = isl_ast_build_cow(build);
883 if (!build)
884 goto error;
886 set = isl_set_compute_divs(set);
887 build->domain = isl_set_intersect(build->domain, set);
888 build->domain = isl_set_coalesce(build->domain);
890 if (!build->domain)
891 return isl_ast_build_free(build);
893 return build;
894 error:
895 isl_ast_build_free(build);
896 isl_set_free(set);
897 return NULL;
900 /* Intersect build->generated and build->domain with "set",
901 * where "set" is specified in terms of the internal schedule domain.
903 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
904 __isl_take isl_ast_build *build, __isl_take isl_set *set)
906 set = isl_set_compute_divs(set);
907 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
908 build = isl_ast_build_cow(build);
909 if (!build)
910 goto error;
912 build->generated = isl_set_intersect(build->generated, set);
913 build->generated = isl_set_coalesce(build->generated);
915 if (!build->generated)
916 return isl_ast_build_free(build);
918 return build;
919 error:
920 isl_ast_build_free(build);
921 isl_set_free(set);
922 return NULL;
925 /* Replace the set of pending constraints by "guard", which is then
926 * no longer considered as pending.
927 * That is, add "guard" to the generated constraints and clear all pending
928 * constraints, making the domain equal to the generated constraints.
930 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
931 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
933 build = isl_ast_build_restrict_generated(build, guard);
934 build = isl_ast_build_cow(build);
935 if (!build)
936 return NULL;
938 isl_set_free(build->domain);
939 build->domain = isl_set_copy(build->generated);
940 isl_set_free(build->pending);
941 build->pending = isl_set_universe(isl_set_get_space(build->domain));
943 if (!build->pending)
944 return isl_ast_build_free(build);
946 return build;
949 /* Intersect build->pending and build->domain with "set",
950 * where "set" is specified in terms of the internal schedule domain.
952 __isl_give isl_ast_build *isl_ast_build_restrict_pending(
953 __isl_take isl_ast_build *build, __isl_take isl_set *set)
955 set = isl_set_compute_divs(set);
956 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
957 build = isl_ast_build_cow(build);
958 if (!build)
959 goto error;
961 build->pending = isl_set_intersect(build->pending, set);
962 build->pending = isl_set_coalesce(build->pending);
964 if (!build->pending)
965 return isl_ast_build_free(build);
967 return build;
968 error:
969 isl_ast_build_free(build);
970 isl_set_free(set);
971 return NULL;
974 /* Intersect build->domain with "set", where "set" is specified
975 * in terms of the external schedule domain.
977 __isl_give isl_ast_build *isl_ast_build_restrict(
978 __isl_take isl_ast_build *build, __isl_take isl_set *set)
980 if (isl_set_is_params(set))
981 return isl_ast_build_restrict_generated(build, set);
983 if (isl_ast_build_need_schedule_map(build)) {
984 isl_multi_aff *ma;
985 ma = isl_ast_build_get_schedule_map_multi_aff(build);
986 set = isl_set_preimage_multi_aff(set, ma);
988 return isl_ast_build_restrict_generated(build, set);
991 /* Replace build->executed by "executed".
993 __isl_give isl_ast_build *isl_ast_build_set_executed(
994 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
996 build = isl_ast_build_cow(build);
997 if (!build)
998 goto error;
1000 isl_union_map_free(build->executed);
1001 build->executed = executed;
1003 return build;
1004 error:
1005 isl_ast_build_free(build);
1006 isl_union_map_free(executed);
1007 return NULL;
1010 /* Does "build" point to a band node?
1011 * That is, are we currently handling a band node inside a schedule tree?
1013 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
1015 if (!build)
1016 return -1;
1017 return build->node != NULL;
1020 /* Return a copy of the band node that "build" refers to.
1022 __isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
1023 __isl_keep isl_ast_build *build)
1025 if (!build)
1026 return NULL;
1027 return isl_schedule_node_copy(build->node);
1030 /* Extract the loop AST generation types for the members of build->node
1031 * and store them in build->loop_type.
1033 static __isl_give isl_ast_build *extract_loop_types(
1034 __isl_take isl_ast_build *build)
1036 int i;
1037 isl_ctx *ctx;
1038 isl_schedule_node *node;
1040 if (!build)
1041 return NULL;
1042 ctx = isl_ast_build_get_ctx(build);
1043 if (!build->node)
1044 isl_die(ctx, isl_error_internal, "missing AST node",
1045 return isl_ast_build_free(build));
1047 free(build->loop_type);
1048 build->n = isl_schedule_node_band_n_member(build->node);
1049 build->loop_type = isl_alloc_array(ctx,
1050 enum isl_ast_loop_type, build->n);
1051 if (build->n && !build->loop_type)
1052 return isl_ast_build_free(build);
1053 node = build->node;
1054 for (i = 0; i < build->n; ++i)
1055 build->loop_type[i] =
1056 isl_schedule_node_band_member_get_ast_loop_type(node, i);
1058 return build;
1061 /* Replace the band node that "build" refers to by "node" and
1062 * extract the corresponding loop AST generation types.
1064 __isl_give isl_ast_build *isl_ast_build_set_schedule_node(
1065 __isl_take isl_ast_build *build,
1066 __isl_take isl_schedule_node *node)
1068 build = isl_ast_build_cow(build);
1069 if (!build || !node)
1070 goto error;
1072 isl_schedule_node_free(build->node);
1073 build->node = node;
1075 build = extract_loop_types(build);
1077 return build;
1078 error:
1079 isl_ast_build_free(build);
1080 isl_schedule_node_free(node);
1081 return NULL;
1084 /* Remove any reference to a band node from "build".
1086 __isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
1087 __isl_take isl_ast_build *build)
1089 build = isl_ast_build_cow(build);
1090 if (!build)
1091 return NULL;
1093 isl_schedule_node_free(build->node);
1094 build->node = NULL;
1096 return build;
1099 /* Return a copy of the current schedule domain.
1101 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
1103 return build ? isl_set_copy(build->domain) : NULL;
1106 /* Return a copy of the set of pending constraints.
1108 __isl_give isl_set *isl_ast_build_get_pending(
1109 __isl_keep isl_ast_build *build)
1111 return build ? isl_set_copy(build->pending) : NULL;
1114 /* Return a copy of the set of generated constraints.
1116 __isl_give isl_set *isl_ast_build_get_generated(
1117 __isl_keep isl_ast_build *build)
1119 return build ? isl_set_copy(build->generated) : NULL;
1122 /* Return a copy of the map from the internal schedule domain
1123 * to the original input schedule domain.
1125 __isl_give isl_multi_aff *isl_ast_build_get_internal2input(
1126 __isl_keep isl_ast_build *build)
1128 return build ? isl_multi_aff_copy(build->internal2input) : NULL;
1131 /* Return the number of variables of the given type
1132 * in the (internal) schedule space.
1134 unsigned isl_ast_build_dim(__isl_keep isl_ast_build *build,
1135 enum isl_dim_type type)
1137 if (!build)
1138 return 0;
1139 return isl_set_dim(build->domain, type);
1142 /* Return the (schedule) space of "build".
1144 * If "internal" is set, then this space is the space of the internal
1145 * representation of the entire schedule, including those parts for
1146 * which no code has been generated yet.
1148 * If "internal" is not set, then this space is the external representation
1149 * of the loops generated so far.
1151 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
1152 int internal)
1154 int i;
1155 int dim;
1156 isl_space *space;
1158 if (!build)
1159 return NULL;
1161 space = isl_set_get_space(build->domain);
1162 if (internal)
1163 return space;
1165 if (!isl_ast_build_need_schedule_map(build))
1166 return space;
1168 dim = isl_set_dim(build->domain, isl_dim_set);
1169 space = isl_space_drop_dims(space, isl_dim_set,
1170 build->depth, dim - build->depth);
1171 for (i = build->depth - 1; i >= 0; --i)
1172 if (isl_ast_build_has_affine_value(build, i))
1173 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
1175 return space;
1178 /* Return the external representation of the schedule space of "build",
1179 * i.e., a space with a dimension for each loop generated so far,
1180 * with the names of the dimensions set to the loop iterators.
1182 __isl_give isl_space *isl_ast_build_get_schedule_space(
1183 __isl_keep isl_ast_build *build)
1185 isl_space *space;
1186 int i, skip;
1188 if (!build)
1189 return NULL;
1191 space = isl_ast_build_get_space(build, 0);
1193 skip = 0;
1194 for (i = 0; i < build->depth; ++i) {
1195 isl_id *id;
1197 if (isl_ast_build_has_affine_value(build, i)) {
1198 skip++;
1199 continue;
1202 id = isl_ast_build_get_iterator_id(build, i);
1203 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1206 return space;
1209 /* Return the current schedule, as stored in build->executed, in terms
1210 * of the external schedule domain.
1212 __isl_give isl_union_map *isl_ast_build_get_schedule(
1213 __isl_keep isl_ast_build *build)
1215 isl_union_map *executed;
1216 isl_union_map *schedule;
1218 if (!build)
1219 return NULL;
1221 executed = isl_union_map_copy(build->executed);
1222 if (isl_ast_build_need_schedule_map(build)) {
1223 isl_map *proj = isl_ast_build_get_schedule_map(build);
1224 executed = isl_union_map_apply_domain(executed,
1225 isl_union_map_from_map(proj));
1227 schedule = isl_union_map_reverse(executed);
1229 return schedule;
1232 /* Return the iterator attached to the internal schedule dimension "pos".
1234 __isl_give isl_id *isl_ast_build_get_iterator_id(
1235 __isl_keep isl_ast_build *build, int pos)
1237 if (!build)
1238 return NULL;
1240 return isl_id_list_get_id(build->iterators, pos);
1243 /* Set the stride and offset of the current dimension to the given
1244 * value and expression.
1246 * If we had already found a stride before, then the two strides
1247 * are combined into a single stride.
1249 * In particular, if the new stride information is of the form
1251 * i = f + s (...)
1253 * and the old stride information is of the form
1255 * i = f2 + s2 (...)
1257 * then we compute the extended gcd of s and s2
1259 * a s + b s2 = g,
1261 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
1262 * and the second with t2 = a s1/g.
1263 * This results in
1265 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
1267 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
1268 * is the combined stride.
1270 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1271 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1273 int pos;
1275 build = isl_ast_build_cow(build);
1276 if (!build || !stride || !offset)
1277 goto error;
1279 pos = build->depth;
1281 if (isl_ast_build_has_stride(build, pos)) {
1282 isl_val *stride2, *a, *b, *g;
1283 isl_aff *offset2;
1285 stride2 = isl_vec_get_element_val(build->strides, pos);
1286 g = isl_val_gcdext(isl_val_copy(stride), isl_val_copy(stride2),
1287 &a, &b);
1288 a = isl_val_mul(a, isl_val_copy(stride));
1289 a = isl_val_div(a, isl_val_copy(g));
1290 stride2 = isl_val_div(stride2, g);
1291 b = isl_val_mul(b, isl_val_copy(stride2));
1292 stride = isl_val_mul(stride, stride2);
1294 offset2 = isl_multi_aff_get_aff(build->offsets, pos);
1295 offset2 = isl_aff_scale_val(offset2, a);
1296 offset = isl_aff_scale_val(offset, b);
1297 offset = isl_aff_add(offset, offset2);
1300 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1301 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1302 if (!build->strides || !build->offsets)
1303 return isl_ast_build_free(build);
1305 return build;
1306 error:
1307 isl_val_free(stride);
1308 isl_aff_free(offset);
1309 return isl_ast_build_free(build);
1312 /* Return a set expressing the stride constraint at the current depth.
1314 * In particular, if the current iterator (i) is known to attain values
1316 * f + s a
1318 * where f is the offset and s is the stride, then the returned set
1319 * expresses the constraint
1321 * (f - i) mod s = 0
1323 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1324 __isl_keep isl_ast_build *build)
1326 isl_aff *aff;
1327 isl_set *set;
1328 isl_val *stride;
1329 int pos;
1331 if (!build)
1332 return NULL;
1334 pos = build->depth;
1336 if (!isl_ast_build_has_stride(build, pos))
1337 return isl_set_universe(isl_ast_build_get_space(build, 1));
1339 stride = isl_ast_build_get_stride(build, pos);
1340 aff = isl_ast_build_get_offset(build, pos);
1341 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1342 aff = isl_aff_mod_val(aff, stride);
1343 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1345 return set;
1348 /* Return the expansion implied by the stride and offset at the current
1349 * depth.
1351 * That is, return the mapping
1353 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1354 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1356 * where s is the stride at the current depth d and offset(i) is
1357 * the corresponding offset.
1359 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1360 __isl_keep isl_ast_build *build)
1362 isl_space *space;
1363 isl_multi_aff *ma;
1364 int pos;
1365 isl_aff *aff, *offset;
1366 isl_val *stride;
1368 if (!build)
1369 return NULL;
1371 pos = isl_ast_build_get_depth(build);
1372 space = isl_ast_build_get_space(build, 1);
1373 space = isl_space_map_from_set(space);
1374 ma = isl_multi_aff_identity(space);
1376 if (!isl_ast_build_has_stride(build, pos))
1377 return ma;
1379 offset = isl_ast_build_get_offset(build, pos);
1380 stride = isl_ast_build_get_stride(build, pos);
1381 aff = isl_multi_aff_get_aff(ma, pos);
1382 aff = isl_aff_scale_val(aff, stride);
1383 aff = isl_aff_add(aff, offset);
1384 ma = isl_multi_aff_set_aff(ma, pos, aff);
1386 return ma;
1389 /* Add constraints corresponding to any previously detected
1390 * stride on the current dimension to build->domain.
1392 __isl_give isl_ast_build *isl_ast_build_include_stride(
1393 __isl_take isl_ast_build *build)
1395 isl_set *set;
1397 if (!build)
1398 return NULL;
1399 if (!isl_ast_build_has_stride(build, build->depth))
1400 return build;
1401 build = isl_ast_build_cow(build);
1402 if (!build)
1403 return NULL;
1405 set = isl_ast_build_get_stride_constraint(build);
1407 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1408 build->generated = isl_set_intersect(build->generated, set);
1409 if (!build->domain || !build->generated)
1410 return isl_ast_build_free(build);
1412 return build;
1415 /* Information used inside detect_stride.
1417 * "build" may be updated by detect_stride to include stride information.
1418 * "pos" is equal to build->depth.
1420 struct isl_detect_stride_data {
1421 isl_ast_build *build;
1422 int pos;
1425 /* Check if constraint "c" imposes any stride on dimension data->pos
1426 * and, if so, update the stride information in data->build.
1428 * In order to impose a stride on the dimension, "c" needs to be an equality
1429 * and it needs to involve the dimension. Note that "c" may also be
1430 * a div constraint and thus an inequality that we cannot use.
1432 * Let c be of the form
1434 * h(p) + g * v * i + g * stride * f(alpha) = 0
1436 * with h(p) an expression in terms of the parameters and outer dimensions
1437 * and f(alpha) an expression in terms of the existentially quantified
1438 * variables. Note that the inner dimensions have been eliminated so
1439 * they do not appear in "c".
1441 * If "stride" is not zero and not one, then it represents a non-trivial stride
1442 * on "i". We compute a and b such that
1444 * a v + b stride = 1
1446 * We have
1448 * g v i = -h(p) + g stride f(alpha)
1450 * a g v i = -a h(p) + g stride f(alpha)
1452 * a g v i + b g stride i = -a h(p) + g stride * (...)
1454 * g i = -a h(p) + g stride * (...)
1456 * i = -a h(p)/g + stride * (...)
1458 * The expression "-a h(p)/g" can therefore be used as offset.
1460 static int detect_stride(__isl_take isl_constraint *c, void *user)
1462 struct isl_detect_stride_data *data = user;
1463 int i, n_div;
1464 isl_ctx *ctx;
1465 isl_val *v, *stride, *m;
1467 if (!isl_constraint_is_equality(c) ||
1468 !isl_constraint_involves_dims(c, isl_dim_set, data->pos, 1)) {
1469 isl_constraint_free(c);
1470 return 0;
1473 ctx = isl_constraint_get_ctx(c);
1474 stride = isl_val_zero(ctx);
1475 n_div = isl_constraint_dim(c, isl_dim_div);
1476 for (i = 0; i < n_div; ++i) {
1477 v = isl_constraint_get_coefficient_val(c, isl_dim_div, i);
1478 stride = isl_val_gcd(stride, v);
1481 v = isl_constraint_get_coefficient_val(c, isl_dim_set, data->pos);
1482 m = isl_val_gcd(isl_val_copy(stride), isl_val_copy(v));
1483 stride = isl_val_div(stride, isl_val_copy(m));
1484 v = isl_val_div(v, isl_val_copy(m));
1486 if (!isl_val_is_zero(stride) && !isl_val_is_one(stride)) {
1487 isl_aff *aff;
1488 isl_val *gcd, *a, *b;
1490 gcd = isl_val_gcdext(v, isl_val_copy(stride), &a, &b);
1491 isl_val_free(gcd);
1492 isl_val_free(b);
1494 aff = isl_constraint_get_aff(c);
1495 for (i = 0; i < n_div; ++i)
1496 aff = isl_aff_set_coefficient_si(aff,
1497 isl_dim_div, i, 0);
1498 aff = isl_aff_set_coefficient_si(aff, isl_dim_in, data->pos, 0);
1499 a = isl_val_neg(a);
1500 aff = isl_aff_scale_val(aff, a);
1501 aff = isl_aff_scale_down_val(aff, m);
1502 data->build = set_stride(data->build, stride, aff);
1503 } else {
1504 isl_val_free(stride);
1505 isl_val_free(m);
1506 isl_val_free(v);
1509 isl_constraint_free(c);
1510 return 0;
1513 /* Check if the constraints in "set" imply any stride on the current
1514 * dimension and, if so, record the stride information in "build"
1515 * and return the updated "build".
1517 * We compute the affine hull and then check if any of the constraints
1518 * in the hull imposes any stride on the current dimension.
1520 * We assume that inner dimensions have been eliminated from "set"
1521 * by the caller. This is needed because the common stride
1522 * may be imposed by different inner dimensions on different parts of
1523 * the domain.
1525 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1526 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1528 isl_basic_set *hull;
1529 struct isl_detect_stride_data data;
1531 if (!build)
1532 goto error;
1534 data.build = build;
1535 data.pos = isl_ast_build_get_depth(build);
1536 hull = isl_set_affine_hull(set);
1538 if (isl_basic_set_foreach_constraint(hull, &detect_stride, &data) < 0)
1539 data.build = isl_ast_build_free(data.build);
1541 isl_basic_set_free(hull);
1542 return data.build;
1543 error:
1544 isl_set_free(set);
1545 return NULL;
1548 struct isl_ast_build_involves_data {
1549 int depth;
1550 int involves;
1553 /* Check if "map" involves the input dimension data->depth.
1555 static int involves_depth(__isl_take isl_map *map, void *user)
1557 struct isl_ast_build_involves_data *data = user;
1559 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1560 isl_map_free(map);
1562 if (data->involves < 0 || data->involves)
1563 return -1;
1564 return 0;
1567 /* Do any options depend on the value of the dimension at the current depth?
1569 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1571 struct isl_ast_build_involves_data data;
1573 if (!build)
1574 return -1;
1576 data.depth = build->depth;
1577 data.involves = 0;
1579 if (isl_union_map_foreach_map(build->options,
1580 &involves_depth, &data) < 0) {
1581 if (data.involves < 0 || !data.involves)
1582 return -1;
1585 return data.involves;
1588 /* Construct the map
1590 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1592 * with "space" the parameter space of the constructed map.
1594 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1595 int pos)
1597 isl_constraint *c;
1598 isl_basic_map *bmap1, *bmap2;
1600 space = isl_space_set_from_params(space);
1601 space = isl_space_add_dims(space, isl_dim_set, 1);
1602 space = isl_space_map_from_set(space);
1603 c = isl_equality_alloc(isl_local_space_from_space(space));
1604 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1605 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1606 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1607 c = isl_constraint_set_constant_si(c, 1);
1608 bmap2 = isl_basic_map_from_constraint(c);
1610 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1611 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1613 return isl_basic_map_union(bmap1, bmap2);
1616 static const char *option_str[] = {
1617 [isl_ast_loop_atomic] = "atomic",
1618 [isl_ast_loop_unroll] = "unroll",
1619 [isl_ast_loop_separate] = "separate"
1622 /* Update the "options" to reflect the insertion of a dimension
1623 * at position "pos" in the schedule domain space.
1624 * "space" is the original domain space before the insertion and
1625 * may be named and/or structured.
1627 * The (relevant) input options all have "space" as domain, which
1628 * has to be mapped to the extended space.
1629 * The values of the ranges also refer to the schedule domain positions
1630 * and they therefore also need to be adjusted. In particular, values
1631 * smaller than pos do not need to change, while values greater than or
1632 * equal to pos need to be incremented.
1633 * That is, we need to apply the following map.
1635 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1636 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1637 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1638 * separation_class[[i] -> [c]]
1639 * -> separation_class[[i] -> [c]] : i < pos;
1640 * separation_class[[i] -> [c]]
1641 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1643 static __isl_give isl_union_map *options_insert_dim(
1644 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1646 isl_map *map;
1647 isl_union_map *insertion;
1648 enum isl_ast_loop_type type;
1649 const char *name = "separation_class";
1651 space = isl_space_map_from_set(space);
1652 map = isl_map_identity(space);
1653 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1654 options = isl_union_map_apply_domain(options,
1655 isl_union_map_from_map(map));
1657 if (!options)
1658 return NULL;
1660 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1662 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1664 for (type = isl_ast_loop_atomic;
1665 type <= isl_ast_loop_separate; ++type) {
1666 isl_map *map_type = isl_map_copy(map);
1667 const char *name = option_str[type];
1668 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1669 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1670 insertion = isl_union_map_add_map(insertion, map_type);
1673 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1674 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1675 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1676 insertion = isl_union_map_add_map(insertion, map);
1678 options = isl_union_map_apply_range(options, insertion);
1680 return options;
1683 /* If we are generating an AST from a schedule tree (build->node is set),
1684 * then update the loop AST generation types
1685 * to reflect the insertion of a dimension at (global) position "pos"
1686 * in the schedule domain space.
1687 * We do not need to adjust any isolate option since we would not be inserting
1688 * any dimensions if there were any isolate option.
1690 static __isl_give isl_ast_build *node_insert_dim(
1691 __isl_take isl_ast_build *build, int pos)
1693 int i;
1694 int local_pos;
1695 enum isl_ast_loop_type *loop_type;
1696 isl_ctx *ctx;
1698 build = isl_ast_build_cow(build);
1699 if (!build)
1700 return NULL;
1701 if (!build->node)
1702 return build;
1704 ctx = isl_ast_build_get_ctx(build);
1705 local_pos = pos - build->outer_pos;
1706 loop_type = isl_realloc_array(ctx, build->loop_type,
1707 enum isl_ast_loop_type, build->n + 1);
1708 if (!loop_type)
1709 return isl_ast_build_free(build);
1710 build->loop_type = loop_type;
1711 for (i = build->n - 1; i >= local_pos; --i)
1712 loop_type[i + 1] = loop_type[i];
1713 loop_type[local_pos] = isl_ast_loop_default;
1714 build->n++;
1716 return build;
1719 /* Insert a single dimension in the schedule domain at position "pos".
1720 * The new dimension is given an isl_id with the empty string as name.
1722 * The main difficulty is updating build->options to reflect the
1723 * extra dimension. This is handled in options_insert_dim.
1725 * Note that because of the dimension manipulations, the resulting
1726 * schedule domain space will always be unnamed and unstructured.
1727 * However, the original schedule domain space may be named and/or
1728 * structured, so we have to take this possibility into account
1729 * while performing the transformations.
1731 * Since the inserted schedule dimension is used by the caller
1732 * to differentiate between different domain spaces, there is
1733 * no longer a uniform mapping from the internal schedule space
1734 * to the input schedule space. The internal2input mapping is
1735 * therefore removed.
1737 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1738 __isl_take isl_ast_build *build, int pos)
1740 isl_ctx *ctx;
1741 isl_space *space, *ma_space;
1742 isl_id *id;
1743 isl_multi_aff *ma;
1745 build = isl_ast_build_cow(build);
1746 if (!build)
1747 return NULL;
1749 ctx = isl_ast_build_get_ctx(build);
1750 id = isl_id_alloc(ctx, "", NULL);
1751 if (!build->node)
1752 space = isl_ast_build_get_space(build, 1);
1753 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1754 build->domain = isl_set_insert_dims(build->domain,
1755 isl_dim_set, pos, 1);
1756 build->generated = isl_set_insert_dims(build->generated,
1757 isl_dim_set, pos, 1);
1758 build->pending = isl_set_insert_dims(build->pending,
1759 isl_dim_set, pos, 1);
1760 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1761 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1762 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1763 ma_space = isl_space_set_from_params(ma_space);
1764 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1765 ma_space = isl_space_map_from_set(ma_space);
1766 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1767 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1768 ma = isl_multi_aff_identity(ma_space);
1769 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1770 if (!build->node)
1771 build->options = options_insert_dim(build->options, space, pos);
1772 build->internal2input = isl_multi_aff_free(build->internal2input);
1774 if (!build->iterators || !build->domain || !build->generated ||
1775 !build->pending || !build->values ||
1776 !build->strides || !build->offsets || !build->options)
1777 return isl_ast_build_free(build);
1779 build = node_insert_dim(build, pos);
1781 return build;
1784 /* Scale down the current dimension by a factor of "m".
1785 * "umap" is an isl_union_map that implements the scaling down.
1786 * That is, it is of the form
1788 * { [.... i ....] -> [.... i' ....] : i = m i' }
1790 * This function is called right after the strides have been
1791 * detected, but before any constraints on the current dimension
1792 * have been included in build->domain.
1793 * We therefore only need to update stride, offset, the options and
1794 * the mapping from internal schedule space to the original schedule
1795 * space, if we are still keeping track of such a mapping.
1796 * The latter mapping is updated by plugging in
1797 * { [... i ...] -> [... m i ... ] }.
1799 __isl_give isl_ast_build *isl_ast_build_scale_down(
1800 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1801 __isl_take isl_union_map *umap)
1803 isl_aff *aff;
1804 isl_val *v;
1805 int depth;
1807 build = isl_ast_build_cow(build);
1808 if (!build || !umap || !m)
1809 goto error;
1811 depth = build->depth;
1813 if (build->internal2input) {
1814 isl_space *space;
1815 isl_multi_aff *ma;
1816 isl_aff *aff;
1818 space = isl_multi_aff_get_space(build->internal2input);
1819 space = isl_space_map_from_set(isl_space_domain(space));
1820 ma = isl_multi_aff_identity(space);
1821 aff = isl_multi_aff_get_aff(ma, depth);
1822 aff = isl_aff_scale_val(aff, isl_val_copy(m));
1823 ma = isl_multi_aff_set_aff(ma, depth, aff);
1824 build->internal2input =
1825 isl_multi_aff_pullback_multi_aff(build->internal2input, ma);
1826 if (!build->internal2input)
1827 goto error;
1830 v = isl_vec_get_element_val(build->strides, depth);
1831 v = isl_val_div(v, isl_val_copy(m));
1832 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1834 aff = isl_multi_aff_get_aff(build->offsets, depth);
1835 aff = isl_aff_scale_down_val(aff, m);
1836 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1837 build->options = isl_union_map_apply_domain(build->options, umap);
1838 if (!build->strides || !build->offsets || !build->options)
1839 return isl_ast_build_free(build);
1841 return build;
1842 error:
1843 isl_val_free(m);
1844 isl_union_map_free(umap);
1845 return isl_ast_build_free(build);
1848 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1849 * If an isl_id with such a name already appears among the parameters
1850 * in build->domain, then adjust the name to "c%d_%d".
1852 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1853 __isl_keep isl_ast_build *build)
1855 int i;
1856 isl_id_list *names;
1858 names = isl_id_list_alloc(ctx, n);
1859 for (i = 0; i < n; ++i) {
1860 isl_id *id;
1862 id = generate_name(ctx, first + i, build);
1863 names = isl_id_list_add(names, id);
1866 return names;
1869 /* Embed "options" into the given isl_ast_build space.
1871 * This function is called from within a nested call to
1872 * isl_ast_build_node_from_schedule_map.
1873 * "options" refers to the additional schedule,
1874 * while space refers to both the space of the outer isl_ast_build and
1875 * that of the additional schedule.
1876 * Specifically, space is of the form
1878 * [I -> S]
1880 * while options lives in the space(s)
1882 * S -> *
1884 * We compute
1886 * [I -> S] -> S
1888 * and compose this with options, to obtain the new options
1889 * living in the space(s)
1891 * [I -> S] -> *
1893 static __isl_give isl_union_map *embed_options(
1894 __isl_take isl_union_map *options, __isl_take isl_space *space)
1896 isl_map *map;
1898 map = isl_map_universe(isl_space_unwrap(space));
1899 map = isl_map_range_map(map);
1901 options = isl_union_map_apply_range(
1902 isl_union_map_from_map(map), options);
1904 return options;
1907 /* Update "build" for use in a (possibly nested) code generation. That is,
1908 * extend "build" from an AST build on some domain O to an AST build
1909 * on domain [O -> S], with S corresponding to "space".
1910 * If the original domain is a parameter domain, then the new domain is
1911 * simply S.
1912 * "iterators" is a list of iterators for S, but the number of elements
1913 * may be smaller or greater than the number of set dimensions of S.
1914 * If "keep_iterators" is set, then any extra ids in build->iterators
1915 * are reused for S. Otherwise, these extra ids are dropped.
1917 * We first update build->outer_pos to the current depth.
1918 * This depth is zero in case this is the outermost code generation.
1920 * We then add additional ids such that the number of iterators is at least
1921 * equal to the dimension of the new build domain.
1923 * If the original domain is parametric, then we are constructing
1924 * an isl_ast_build for the outer code generation and we pass control
1925 * to isl_ast_build_init.
1927 * Otherwise, we adjust the fields of "build" to include "space".
1929 __isl_give isl_ast_build *isl_ast_build_product(
1930 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1932 isl_ctx *ctx;
1933 isl_vec *strides;
1934 isl_set *set;
1935 isl_multi_aff *embedding;
1936 int dim, n_it;
1938 build = isl_ast_build_cow(build);
1939 if (!build)
1940 goto error;
1942 build->outer_pos = build->depth;
1944 ctx = isl_ast_build_get_ctx(build);
1945 dim = isl_set_dim(build->domain, isl_dim_set);
1946 dim += isl_space_dim(space, isl_dim_set);
1947 n_it = isl_id_list_n_id(build->iterators);
1948 if (n_it < dim) {
1949 isl_id_list *l;
1950 l = generate_names(ctx, dim - n_it, n_it, build);
1951 build->iterators = isl_id_list_concat(build->iterators, l);
1954 if (isl_set_is_params(build->domain))
1955 return isl_ast_build_init(build, space);
1957 set = isl_set_universe(isl_space_copy(space));
1958 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1959 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1960 build->generated = isl_set_product(build->generated, set);
1962 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
1963 strides = isl_vec_set_si(strides, 1);
1964 build->strides = isl_vec_concat(build->strides, strides);
1966 space = isl_space_map_from_set(space);
1967 build->offsets = isl_multi_aff_align_params(build->offsets,
1968 isl_space_copy(space));
1969 build->offsets = isl_multi_aff_product(build->offsets,
1970 isl_multi_aff_zero(isl_space_copy(space)));
1971 build->values = isl_multi_aff_align_params(build->values,
1972 isl_space_copy(space));
1973 embedding = isl_multi_aff_identity(space);
1974 build->values = isl_multi_aff_product(build->values,
1975 isl_multi_aff_copy(embedding));
1976 if (build->internal2input) {
1977 build->internal2input =
1978 isl_multi_aff_product(build->internal2input, embedding);
1979 build->internal2input =
1980 isl_multi_aff_flatten_range(build->internal2input);
1981 if (!build->internal2input)
1982 return isl_ast_build_free(build);
1983 } else {
1984 isl_multi_aff_free(embedding);
1987 space = isl_ast_build_get_space(build, 1);
1988 build->options = embed_options(build->options, space);
1990 if (!build->iterators || !build->domain || !build->generated ||
1991 !build->pending || !build->values ||
1992 !build->strides || !build->offsets || !build->options)
1993 return isl_ast_build_free(build);
1995 return build;
1996 error:
1997 isl_ast_build_free(build);
1998 isl_space_free(space);
1999 return NULL;
2002 /* Does "aff" only attain non-negative values over build->domain?
2003 * That is, does it not attain any negative values?
2005 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
2006 __isl_keep isl_aff *aff)
2008 isl_set *test;
2009 int empty;
2011 if (!build)
2012 return -1;
2014 aff = isl_aff_copy(aff);
2015 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
2016 test = isl_set_intersect(test, isl_set_copy(build->domain));
2017 empty = isl_set_is_empty(test);
2018 isl_set_free(test);
2020 return empty;
2023 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
2025 int isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
2027 isl_val *v;
2028 int has_stride;
2030 if (!build)
2031 return -1;
2033 v = isl_vec_get_element_val(build->strides, pos);
2034 if (!v)
2035 return -1;
2036 has_stride = !isl_val_is_one(v);
2037 isl_val_free(v);
2039 return has_stride;
2042 /* Given that the dimension at position "pos" takes on values
2044 * f + s a
2046 * with a an integer, return s through *stride.
2048 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
2049 int pos)
2051 if (!build)
2052 return NULL;
2054 return isl_vec_get_element_val(build->strides, pos);
2057 /* Given that the dimension at position "pos" takes on values
2059 * f + s a
2061 * with a an integer, return f.
2063 __isl_give isl_aff *isl_ast_build_get_offset(
2064 __isl_keep isl_ast_build *build, int pos)
2066 if (!build)
2067 return NULL;
2069 return isl_multi_aff_get_aff(build->offsets, pos);
2072 /* Is the dimension at position "pos" known to attain only a single
2073 * value that, moreover, can be described by a single affine expression
2074 * in terms of the outer dimensions and parameters?
2076 * If not, then the corresponding affine expression in build->values
2077 * is set to be equal to the same input dimension.
2078 * Otherwise, it is set to the requested expression in terms of
2079 * outer dimensions and parameters.
2081 int isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
2082 int pos)
2084 isl_aff *aff;
2085 int involves;
2087 if (!build)
2088 return -1;
2090 aff = isl_multi_aff_get_aff(build->values, pos);
2091 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
2092 isl_aff_free(aff);
2094 if (involves < 0)
2095 return -1;
2097 return !involves;
2100 /* Plug in the known values (fixed affine expressions in terms of
2101 * parameters and outer loop iterators) of all loop iterators
2102 * in the domain of "umap".
2104 * We simply precompose "umap" with build->values.
2106 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
2107 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
2109 isl_multi_aff *values;
2111 if (!build)
2112 return isl_union_map_free(umap);
2114 values = isl_multi_aff_copy(build->values);
2115 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
2117 return umap;
2120 /* Is the current dimension known to attain only a single value?
2122 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
2124 if (!build)
2125 return -1;
2127 return build->value != NULL;
2130 /* Simplify the basic set "bset" based on what we know about
2131 * the iterators of already generated loops.
2133 * "bset" is assumed to live in the (internal) schedule domain.
2135 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
2136 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2138 if (!build)
2139 goto error;
2141 bset = isl_basic_set_preimage_multi_aff(bset,
2142 isl_multi_aff_copy(build->values));
2143 bset = isl_basic_set_gist(bset,
2144 isl_set_simple_hull(isl_set_copy(build->domain)));
2146 return bset;
2147 error:
2148 isl_basic_set_free(bset);
2149 return NULL;
2152 /* Simplify the set "set" based on what we know about
2153 * the iterators of already generated loops.
2155 * "set" is assumed to live in the (internal) schedule domain.
2157 __isl_give isl_set *isl_ast_build_compute_gist(
2158 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2160 if (!build)
2161 goto error;
2163 if (!isl_set_is_params(set))
2164 set = isl_set_preimage_multi_aff(set,
2165 isl_multi_aff_copy(build->values));
2166 set = isl_set_gist(set, isl_set_copy(build->domain));
2168 return set;
2169 error:
2170 isl_set_free(set);
2171 return NULL;
2174 /* Include information about what we know about the iterators of
2175 * already generated loops to "set".
2177 * We currently only plug in the known affine values of outer loop
2178 * iterators.
2179 * In principle we could also introduce equalities or even other
2180 * constraints implied by the intersection of "set" and build->domain.
2182 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
2183 __isl_take isl_set *set)
2185 if (!build)
2186 return isl_set_free(set);
2188 return isl_set_preimage_multi_aff(set,
2189 isl_multi_aff_copy(build->values));
2192 /* Simplify the map "map" based on what we know about
2193 * the iterators of already generated loops.
2195 * The domain of "map" is assumed to live in the (internal) schedule domain.
2197 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
2198 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
2200 if (!build)
2201 goto error;
2203 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
2205 return map;
2206 error:
2207 isl_map_free(map);
2208 return NULL;
2211 /* Simplify the affine expression "aff" based on what we know about
2212 * the iterators of already generated loops.
2214 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2216 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
2217 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
2219 if (!build)
2220 goto error;
2222 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
2224 return aff;
2225 error:
2226 isl_aff_free(aff);
2227 return NULL;
2230 /* Simplify the piecewise affine expression "aff" based on what we know about
2231 * the iterators of already generated loops.
2233 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2235 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
2236 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2238 if (!build)
2239 goto error;
2241 if (!isl_set_is_params(build->domain))
2242 pa = isl_pw_aff_pullback_multi_aff(pa,
2243 isl_multi_aff_copy(build->values));
2244 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
2246 return pa;
2247 error:
2248 isl_pw_aff_free(pa);
2249 return NULL;
2252 /* Simplify the piecewise multi-affine expression "aff" based on what
2253 * we know about the iterators of already generated loops.
2255 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2257 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
2258 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2260 if (!build)
2261 goto error;
2263 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
2264 isl_multi_aff_copy(build->values));
2265 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2267 return pma;
2268 error:
2269 isl_pw_multi_aff_free(pma);
2270 return NULL;
2273 /* Extract the schedule domain of the given type from build->options
2274 * at the current depth.
2276 * In particular, find the subset of build->options that is of
2277 * the following form
2279 * schedule_domain -> type[depth]
2281 * and return the corresponding domain, after eliminating inner dimensions
2282 * and divs that depend on the current dimension.
2284 * Note that the domain of build->options has been reformulated
2285 * in terms of the internal build space in embed_options,
2286 * but the position is still that within the current code generation.
2288 __isl_give isl_set *isl_ast_build_get_option_domain(
2289 __isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
2291 const char *name;
2292 isl_space *space;
2293 isl_map *option;
2294 isl_set *domain;
2295 int local_pos;
2297 if (!build)
2298 return NULL;
2300 name = option_str[type];
2301 local_pos = build->depth - build->outer_pos;
2303 space = isl_ast_build_get_space(build, 1);
2304 space = isl_space_from_domain(space);
2305 space = isl_space_add_dims(space, isl_dim_out, 1);
2306 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2308 option = isl_union_map_extract_map(build->options, space);
2309 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2311 domain = isl_map_domain(option);
2312 domain = isl_ast_build_eliminate(build, domain);
2314 return domain;
2317 /* How does the user want the current schedule dimension to be generated?
2318 * These choices have been extracted from the schedule node
2319 * in extract_loop_types and stored in build->loop_type.
2320 * They have been updated to reflect any dimension insertion in
2321 * node_insert_dim.
2322 * Return isl_ast_domain_error on error.
2324 * If "isolated" is set, then we get the loop AST generation type
2325 * directly from the band node since node_insert_dim cannot have been
2326 * called on a band with the isolate option.
2328 enum isl_ast_loop_type isl_ast_build_get_loop_type(
2329 __isl_keep isl_ast_build *build, int isolated)
2331 int local_pos;
2332 isl_ctx *ctx;
2334 if (!build)
2335 return isl_ast_loop_error;
2336 ctx = isl_ast_build_get_ctx(build);
2337 if (!build->node)
2338 isl_die(ctx, isl_error_internal,
2339 "only works for schedule tree based AST generation",
2340 return isl_ast_loop_error);
2342 local_pos = build->depth - build->outer_pos;
2343 if (!isolated)
2344 return build->loop_type[local_pos];
2345 return isl_schedule_node_band_member_get_isolate_ast_loop_type(
2346 build->node, local_pos);
2349 /* Extract the isolated set from the isolate option, if any,
2350 * and store in the build.
2351 * If there is no isolate option, then the isolated set is
2352 * set to the empty set.
2354 * The isolate option is of the form
2356 * isolate[[outer bands] -> current_band]
2358 * We flatten this set and then map it back to the internal
2359 * schedule space.
2361 * If we have already extracted the isolated set
2362 * or if internal2input is no longer set, then we do not
2363 * need to do anything. In the latter case, we know
2364 * that the current band cannot have any isolate option.
2366 __isl_give isl_ast_build *isl_ast_build_extract_isolated(
2367 __isl_take isl_ast_build *build)
2369 isl_space *space, *space2;
2370 isl_union_set *options;
2371 int n, n2;
2372 isl_set *isolated;
2374 if (!build)
2375 return NULL;
2376 if (!build->internal2input)
2377 return build;
2378 if (build->isolated)
2379 return build;
2381 build = isl_ast_build_cow(build);
2382 if (!build)
2383 return NULL;
2385 options = isl_schedule_node_band_get_ast_build_options(build->node);
2387 space = isl_multi_aff_get_space(build->internal2input);
2388 space = isl_space_range(space);
2389 space2 = isl_set_get_space(build->domain);
2390 if (isl_space_is_wrapping(space2))
2391 space2 = isl_space_range(isl_space_unwrap(space2));
2392 n2 = isl_space_dim(space2, isl_dim_set);
2393 n = isl_space_dim(space, isl_dim_set);
2394 if (n < n2)
2395 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2396 "total input space dimension cannot be smaller "
2397 "than dimension of innermost band",
2398 space = isl_space_free(space));
2399 space = isl_space_drop_dims(space, isl_dim_set, n - n2, n2);
2400 space = isl_space_map_from_domain_and_range(space, space2);
2401 space = isl_space_wrap(space);
2402 space = isl_space_set_tuple_name(space, isl_dim_set, "isolate");
2403 isolated = isl_union_set_extract_set(options, space);
2404 isl_union_set_free(options);
2406 isolated = isl_set_flatten(isolated);
2407 isolated = isl_set_preimage_multi_aff(isolated,
2408 isl_multi_aff_copy(build->internal2input));
2410 build->isolated = isolated;
2411 if (!build->isolated)
2412 return isl_ast_build_free(build);
2414 return build;
2417 /* Does "build" have a non-empty isolated set?
2419 * The caller is assumed to have called isl_ast_build_extract_isolated first.
2421 int isl_ast_build_has_isolated(__isl_keep isl_ast_build *build)
2423 int empty;
2425 if (!build)
2426 return -1;
2427 if (!build->internal2input)
2428 return 0;
2429 if (!build->isolated)
2430 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2431 "isolated set not extracted yet", return -1);
2433 empty = isl_set_plain_is_empty(build->isolated);
2434 return empty < 0 ? -1 : !empty;
2437 /* Return a copy of the isolated set of "build".
2439 * The caller is assume to have called isl_ast_build_has_isolated first,
2440 * with this function returning true.
2441 * In particular, this function should not be called if we are no
2442 * longer keeping track of internal2input (and there therefore could
2443 * not possibly be any isolated set).
2445 __isl_give isl_set *isl_ast_build_get_isolated(__isl_keep isl_ast_build *build)
2447 if (!build)
2448 return NULL;
2449 if (!build->internal2input)
2450 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2451 "build cannot have isolated set", return NULL);
2453 return isl_set_copy(build->isolated);
2456 /* Extract the separation class mapping at the current depth.
2458 * In particular, find and return the subset of build->options that is of
2459 * the following form
2461 * schedule_domain -> separation_class[[depth] -> [class]]
2463 * The caller is expected to eliminate inner dimensions from the domain.
2465 * Note that the domain of build->options has been reformulated
2466 * in terms of the internal build space in embed_options,
2467 * but the position is still that within the current code generation.
2469 __isl_give isl_map *isl_ast_build_get_separation_class(
2470 __isl_keep isl_ast_build *build)
2472 isl_ctx *ctx;
2473 isl_space *space_sep, *space;
2474 isl_map *res;
2475 int local_pos;
2477 if (!build)
2478 return NULL;
2480 local_pos = build->depth - build->outer_pos;
2481 ctx = isl_ast_build_get_ctx(build);
2482 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2483 space_sep = isl_space_wrap(space_sep);
2484 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2485 "separation_class");
2486 space = isl_ast_build_get_space(build, 1);
2487 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2488 space = isl_space_map_from_domain_and_range(space, space_sep);
2490 res = isl_union_map_extract_map(build->options, space);
2491 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2492 res = isl_map_coalesce(res);
2494 return res;
2497 /* Eliminate dimensions inner to the current dimension.
2499 __isl_give isl_set *isl_ast_build_eliminate_inner(
2500 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2502 int dim;
2503 int depth;
2505 if (!build)
2506 return isl_set_free(set);
2508 dim = isl_set_dim(set, isl_dim_set);
2509 depth = build->depth;
2510 set = isl_set_detect_equalities(set);
2511 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2513 return set;
2516 /* Eliminate unknown divs and divs that depend on the current dimension.
2518 * Note that during the elimination of unknown divs, we may discover
2519 * an explicit representation of some other unknown divs, which may
2520 * depend on the current dimension. We therefore need to eliminate
2521 * unknown divs first.
2523 __isl_give isl_set *isl_ast_build_eliminate_divs(
2524 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2526 int depth;
2528 if (!build)
2529 return isl_set_free(set);
2531 set = isl_set_remove_unknown_divs(set);
2532 depth = build->depth;
2533 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2535 return set;
2538 /* Eliminate dimensions inner to the current dimension as well as
2539 * unknown divs and divs that depend on the current dimension.
2540 * The result then consists only of constraints that are independent
2541 * of the current dimension and upper and lower bounds on the current
2542 * dimension.
2544 __isl_give isl_set *isl_ast_build_eliminate(
2545 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2547 domain = isl_ast_build_eliminate_inner(build, domain);
2548 domain = isl_ast_build_eliminate_divs(build, domain);
2549 return domain;
2552 /* Replace build->single_valued by "sv".
2554 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2555 __isl_take isl_ast_build *build, int sv)
2557 if (!build)
2558 return build;
2559 if (build->single_valued == sv)
2560 return build;
2561 build = isl_ast_build_cow(build);
2562 if (!build)
2563 return build;
2564 build->single_valued = sv;
2566 return build;