isl_ast_build: keep track of mapping from internal to input schedule space
[isl.git] / isl_ast_build.c
blob8d5e87c8acfd3e438c3f0b718a6ad572125bb99f
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->create_leaf = build->create_leaf;
208 dup->create_leaf_user = build->create_leaf_user;
209 dup->node = isl_schedule_node_copy(build->node);
210 if (build->loop_type) {
211 int i;
213 dup->n = build->n;
214 dup->loop_type = isl_alloc_array(ctx,
215 enum isl_ast_loop_type, dup->n);
216 if (dup->n && !dup->loop_type)
217 return isl_ast_build_free(dup);
218 for (i = 0; i < dup->n; ++i)
219 dup->loop_type[i] = build->loop_type[i];
222 if (!dup->iterators || !dup->domain || !dup->generated ||
223 !dup->pending || !dup->values ||
224 !dup->strides || !dup->offsets || !dup->options ||
225 (build->internal2input && !dup->internal2input) ||
226 (build->executed && !dup->executed) ||
227 (build->value && !dup->value) ||
228 (build->node && !dup->node))
229 return isl_ast_build_free(dup);
231 return dup;
234 /* Align the parameters of "build" to those of "model", introducing
235 * additional parameters if needed.
237 __isl_give isl_ast_build *isl_ast_build_align_params(
238 __isl_take isl_ast_build *build, __isl_take isl_space *model)
240 build = isl_ast_build_cow(build);
241 if (!build)
242 goto error;
244 build->domain = isl_set_align_params(build->domain,
245 isl_space_copy(model));
246 build->generated = isl_set_align_params(build->generated,
247 isl_space_copy(model));
248 build->pending = isl_set_align_params(build->pending,
249 isl_space_copy(model));
250 build->values = isl_multi_aff_align_params(build->values,
251 isl_space_copy(model));
252 build->offsets = isl_multi_aff_align_params(build->offsets,
253 isl_space_copy(model));
254 build->options = isl_union_map_align_params(build->options,
255 isl_space_copy(model));
256 if (build->internal2input) {
257 build->internal2input =
258 isl_multi_aff_align_params(build->internal2input,
259 model);
260 if (!build->internal2input)
261 return isl_ast_build_free(build);
262 } else {
263 isl_space_free(model);
266 if (!build->domain || !build->values || !build->offsets ||
267 !build->options)
268 return isl_ast_build_free(build);
270 return build;
271 error:
272 isl_space_free(model);
273 return NULL;
276 __isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
278 if (!build)
279 return NULL;
281 if (build->ref == 1)
282 return build;
283 build->ref--;
284 return isl_ast_build_dup(build);
287 __isl_null isl_ast_build *isl_ast_build_free(
288 __isl_take isl_ast_build *build)
290 if (!build)
291 return NULL;
293 if (--build->ref > 0)
294 return NULL;
296 isl_id_list_free(build->iterators);
297 isl_set_free(build->domain);
298 isl_set_free(build->generated);
299 isl_set_free(build->pending);
300 isl_multi_aff_free(build->values);
301 isl_multi_aff_free(build->internal2input);
302 isl_pw_aff_free(build->value);
303 isl_vec_free(build->strides);
304 isl_multi_aff_free(build->offsets);
305 isl_multi_aff_free(build->schedule_map);
306 isl_union_map_free(build->executed);
307 isl_union_map_free(build->options);
308 isl_schedule_node_free(build->node);
309 free(build->loop_type);
311 free(build);
313 return NULL;
316 isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
318 return build ? isl_set_get_ctx(build->domain) : NULL;
321 /* Replace build->options by "options".
323 __isl_give isl_ast_build *isl_ast_build_set_options(
324 __isl_take isl_ast_build *build, __isl_take isl_union_map *options)
326 build = isl_ast_build_cow(build);
328 if (!build || !options)
329 goto error;
331 isl_union_map_free(build->options);
332 build->options = options;
334 return build;
335 error:
336 isl_union_map_free(options);
337 return isl_ast_build_free(build);
340 /* Set the iterators for the next code generation.
342 * If we still have some iterators left from the previous code generation
343 * (if any) or if iterators have already been set by a previous
344 * call to this function, then we remove them first.
346 __isl_give isl_ast_build *isl_ast_build_set_iterators(
347 __isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
349 int dim, n_it;
351 build = isl_ast_build_cow(build);
352 if (!build)
353 goto error;
355 dim = isl_set_dim(build->domain, isl_dim_set);
356 n_it = isl_id_list_n_id(build->iterators);
357 if (n_it < dim)
358 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
359 "isl_ast_build in inconsistent state", goto error);
360 if (n_it > dim)
361 build->iterators = isl_id_list_drop(build->iterators,
362 dim, n_it - dim);
363 build->iterators = isl_id_list_concat(build->iterators, iterators);
364 if (!build->iterators)
365 return isl_ast_build_free(build);
367 return build;
368 error:
369 isl_id_list_free(iterators);
370 return isl_ast_build_free(build);
373 /* Set the "at_each_domain" callback of "build" to "fn".
375 __isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
376 __isl_take isl_ast_build *build,
377 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
378 __isl_keep isl_ast_build *build, void *user), void *user)
380 build = isl_ast_build_cow(build);
382 if (!build)
383 return NULL;
385 build->at_each_domain = fn;
386 build->at_each_domain_user = user;
388 return build;
391 /* Set the "before_each_for" callback of "build" to "fn".
393 __isl_give isl_ast_build *isl_ast_build_set_before_each_for(
394 __isl_take isl_ast_build *build,
395 __isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
396 void *user), void *user)
398 build = isl_ast_build_cow(build);
400 if (!build)
401 return NULL;
403 build->before_each_for = fn;
404 build->before_each_for_user = user;
406 return build;
409 /* Set the "after_each_for" callback of "build" to "fn".
411 __isl_give isl_ast_build *isl_ast_build_set_after_each_for(
412 __isl_take isl_ast_build *build,
413 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
414 __isl_keep isl_ast_build *build, void *user), void *user)
416 build = isl_ast_build_cow(build);
418 if (!build)
419 return NULL;
421 build->after_each_for = fn;
422 build->after_each_for_user = user;
424 return build;
427 /* Set the "create_leaf" callback of "build" to "fn".
429 __isl_give isl_ast_build *isl_ast_build_set_create_leaf(
430 __isl_take isl_ast_build *build,
431 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
432 void *user), void *user)
434 build = isl_ast_build_cow(build);
436 if (!build)
437 return NULL;
439 build->create_leaf = fn;
440 build->create_leaf_user = user;
442 return build;
445 /* Clear all information that is specific to this code generation
446 * and that is (probably) not meaningful to any nested code generation.
448 __isl_give isl_ast_build *isl_ast_build_clear_local_info(
449 __isl_take isl_ast_build *build)
451 isl_space *space;
453 build = isl_ast_build_cow(build);
454 if (!build)
455 return NULL;
457 space = isl_union_map_get_space(build->options);
458 isl_union_map_free(build->options);
459 build->options = isl_union_map_empty(space);
461 build->at_each_domain = NULL;
462 build->at_each_domain_user = NULL;
463 build->before_each_for = NULL;
464 build->before_each_for_user = NULL;
465 build->after_each_for = NULL;
466 build->after_each_for_user = NULL;
467 build->create_leaf = NULL;
468 build->create_leaf_user = NULL;
470 if (!build->options)
471 return isl_ast_build_free(build);
473 return build;
476 /* Have any loops been eliminated?
477 * That is, do any of the original schedule dimensions have a fixed
478 * value that has been substituted?
480 static int any_eliminated(isl_ast_build *build)
482 int i;
484 for (i = 0; i < build->depth; ++i)
485 if (isl_ast_build_has_affine_value(build, i))
486 return 1;
488 return 0;
491 /* Clear build->schedule_map.
492 * This function should be called whenever anything that might affect
493 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
494 * In particular, it should be called when the depth is changed or
495 * when an iterator is determined to have a fixed value.
497 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
499 if (!build)
500 return;
501 isl_multi_aff_free(build->schedule_map);
502 build->schedule_map = NULL;
505 /* Do we need a (non-trivial) schedule map?
506 * That is, is the internal schedule space different from
507 * the external schedule space?
509 * The internal and external schedule spaces are only the same
510 * if code has been generated for the entire schedule and if none
511 * of the loops have been eliminated.
513 __isl_give int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
515 int dim;
517 if (!build)
518 return -1;
520 dim = isl_set_dim(build->domain, isl_dim_set);
521 return build->depth != dim || any_eliminated(build);
524 /* Return a mapping from the internal schedule space to the external
525 * schedule space in the form of an isl_multi_aff.
526 * The internal schedule space originally corresponds to that of the
527 * input schedule. This may change during the code generation if
528 * if isl_ast_build_insert_dim is ever called.
529 * The external schedule space corresponds to the
530 * loops that have been generated.
532 * Currently, the only difference between the internal schedule domain
533 * and the external schedule domain is that some dimensions are projected
534 * out in the external schedule domain. In particular, the dimensions
535 * for which no code has been generated yet and the dimensions that correspond
536 * to eliminated loops.
538 * We cache a copy of the schedule_map in build->schedule_map.
539 * The cache is cleared through isl_ast_build_reset_schedule_map
540 * whenever anything changes that might affect the result of this function.
542 __isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
543 __isl_keep isl_ast_build *build)
545 isl_space *space;
546 isl_multi_aff *ma;
548 if (!build)
549 return NULL;
550 if (build->schedule_map)
551 return isl_multi_aff_copy(build->schedule_map);
553 space = isl_ast_build_get_space(build, 1);
554 space = isl_space_map_from_set(space);
555 ma = isl_multi_aff_identity(space);
556 if (isl_ast_build_need_schedule_map(build)) {
557 int i;
558 int dim = isl_set_dim(build->domain, isl_dim_set);
559 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
560 build->depth, dim - build->depth);
561 for (i = build->depth - 1; i >= 0; --i)
562 if (isl_ast_build_has_affine_value(build, i))
563 ma = isl_multi_aff_drop_dims(ma,
564 isl_dim_out, i, 1);
567 build->schedule_map = ma;
568 return isl_multi_aff_copy(build->schedule_map);
571 /* Return a mapping from the internal schedule space to the external
572 * schedule space in the form of an isl_map.
574 __isl_give isl_map *isl_ast_build_get_schedule_map(
575 __isl_keep isl_ast_build *build)
577 isl_multi_aff *ma;
579 ma = isl_ast_build_get_schedule_map_multi_aff(build);
580 return isl_map_from_multi_aff(ma);
583 /* Return the position of the dimension in build->domain for which
584 * an AST node is currently being generated.
586 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
588 return build ? build->depth : -1;
591 /* Prepare for generating code for the next level.
592 * In particular, increase the depth and reset any information
593 * that is local to the current depth.
595 __isl_give isl_ast_build *isl_ast_build_increase_depth(
596 __isl_take isl_ast_build *build)
598 build = isl_ast_build_cow(build);
599 if (!build)
600 return NULL;
601 build->depth++;
602 isl_ast_build_reset_schedule_map(build);
603 build->value = isl_pw_aff_free(build->value);
604 return build;
607 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
609 if (!build)
610 return;
612 fprintf(stderr, "domain: ");
613 isl_set_dump(build->domain);
614 fprintf(stderr, "generated: ");
615 isl_set_dump(build->generated);
616 fprintf(stderr, "pending: ");
617 isl_set_dump(build->pending);
618 fprintf(stderr, "iterators: ");
619 isl_id_list_dump(build->iterators);
620 fprintf(stderr, "values: ");
621 isl_multi_aff_dump(build->values);
622 if (build->value) {
623 fprintf(stderr, "value: ");
624 isl_pw_aff_dump(build->value);
626 fprintf(stderr, "strides: ");
627 isl_vec_dump(build->strides);
628 fprintf(stderr, "offsets: ");
629 isl_multi_aff_dump(build->offsets);
630 fprintf(stderr, "internal2input: ");
631 isl_multi_aff_dump(build->internal2input);
634 /* Initialize "build" for AST construction in schedule space "space"
635 * in the case that build->domain is a parameter set.
637 * build->iterators is assumed to have been updated already.
639 static __isl_give isl_ast_build *isl_ast_build_init(
640 __isl_take isl_ast_build *build, __isl_take isl_space *space)
642 isl_set *set;
644 build = isl_ast_build_cow(build);
645 if (!build)
646 goto error;
648 set = isl_set_universe(isl_space_copy(space));
649 build->domain = isl_set_intersect_params(isl_set_copy(set),
650 build->domain);
651 build->pending = isl_set_intersect_params(isl_set_copy(set),
652 build->pending);
653 build->generated = isl_set_intersect_params(set, build->generated);
655 return isl_ast_build_init_derived(build, space);
656 error:
657 isl_ast_build_free(build);
658 isl_space_free(space);
659 return NULL;
662 /* Assign "aff" to *user and return -1, effectively extracting
663 * the first (and presumably only) affine expression in the isl_pw_aff
664 * on which this function is used.
666 static int extract_single_piece(__isl_take isl_set *set,
667 __isl_take isl_aff *aff, void *user)
669 isl_aff **p = user;
671 *p = aff;
672 isl_set_free(set);
674 return -1;
677 /* Intersect "set" with the stride constraint of "build", if any.
679 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
680 __isl_keep isl_ast_build *build)
682 isl_set *stride;
684 if (!build)
685 return isl_set_free(set);
686 if (!isl_ast_build_has_stride(build, build->depth))
687 return set;
689 stride = isl_ast_build_get_stride_constraint(build);
690 return isl_set_intersect(set, stride);
693 /* Check if the given bounds on the current dimension (together with
694 * the stride constraint, if any) imply that
695 * this current dimension attains only a single value (in terms of
696 * parameters and outer dimensions).
697 * If so, we record it in build->value.
698 * If, moreover, this value can be represented as a single affine expression,
699 * then we also update build->values, effectively marking the current
700 * dimension as "eliminated".
702 * When computing the gist of the fixed value that can be represented
703 * as a single affine expression, it is important to only take into
704 * account the domain constraints in the original AST build and
705 * not the domain of the affine expression itself.
706 * Otherwise, a [i/3] is changed into a i/3 because we know that i
707 * is a multiple of 3, but then we end up not expressing anywhere
708 * in the context that i is a multiple of 3.
710 static __isl_give isl_ast_build *update_values(
711 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
713 int sv;
714 isl_pw_multi_aff *pma;
715 isl_aff *aff = NULL;
716 isl_map *it_map;
717 isl_set *set;
719 set = isl_set_from_basic_set(bounds);
720 set = isl_set_intersect(set, isl_set_copy(build->domain));
721 set = intersect_stride_constraint(set, build);
722 it_map = isl_ast_build_map_to_iterator(build, set);
724 sv = isl_map_is_single_valued(it_map);
725 if (sv < 0)
726 build = isl_ast_build_free(build);
727 if (!build || !sv) {
728 isl_map_free(it_map);
729 return build;
732 pma = isl_pw_multi_aff_from_map(it_map);
733 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
734 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
735 build->value = isl_pw_aff_coalesce(build->value);
736 isl_pw_multi_aff_free(pma);
738 if (!build->value)
739 return isl_ast_build_free(build);
741 if (isl_pw_aff_n_piece(build->value) != 1)
742 return build;
744 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
746 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
747 if (!build->values)
748 return isl_ast_build_free(build);
749 isl_ast_build_reset_schedule_map(build);
750 return build;
753 /* Update the AST build based on the given loop bounds for
754 * the current dimension and the stride information available in the build.
756 * We first make sure that the bounds do not refer to any iterators
757 * that have already been eliminated.
758 * Then, we check if the bounds imply that the current iterator
759 * has a fixed value.
760 * If they do and if this fixed value can be expressed as a single
761 * affine expression, we eliminate the iterators from the bounds.
762 * Note that we cannot simply plug in this single value using
763 * isl_basic_set_preimage_multi_aff as the single value may only
764 * be defined on a subset of the domain. Plugging in the value
765 * would restrict the build domain to this subset, while this
766 * restriction may not be reflected in the generated code.
767 * Finally, we intersect build->domain with the updated bounds.
768 * We also add the stride constraint unless we have been able
769 * to find a fixed value expressed as a single affine expression.
771 * Note that the check for a fixed value in update_values requires
772 * us to intersect the bounds with the current build domain.
773 * When we intersect build->domain with the updated bounds in
774 * the final step, we make sure that these updated bounds have
775 * not been intersected with the old build->domain.
776 * Otherwise, we would indirectly intersect the build domain with itself,
777 * which can lead to inefficiencies, in particular if the build domain
778 * contains any unknown divs.
780 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
781 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
783 isl_set *set;
785 build = isl_ast_build_cow(build);
786 if (!build)
787 goto error;
789 bounds = isl_basic_set_preimage_multi_aff(bounds,
790 isl_multi_aff_copy(build->values));
791 build = update_values(build, isl_basic_set_copy(bounds));
792 if (!build)
793 goto error;
794 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
795 if (isl_ast_build_has_affine_value(build, build->depth)) {
796 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
797 set = isl_set_compute_divs(set);
798 build->pending = isl_set_intersect(build->pending,
799 isl_set_copy(set));
800 build->domain = isl_set_intersect(build->domain, set);
801 } else {
802 isl_basic_set *generated, *pending;
804 pending = isl_basic_set_copy(bounds);
805 pending = isl_basic_set_drop_constraints_involving_dims(pending,
806 isl_dim_set, build->depth, 1);
807 build->pending = isl_set_intersect(build->pending,
808 isl_set_from_basic_set(pending));
809 generated = isl_basic_set_copy(bounds);
810 generated = isl_basic_set_drop_constraints_not_involving_dims(
811 generated, isl_dim_set, build->depth, 1);
812 build->generated = isl_set_intersect(build->generated,
813 isl_set_from_basic_set(generated));
814 build->domain = isl_set_intersect(build->domain, set);
815 build = isl_ast_build_include_stride(build);
816 if (!build)
817 goto error;
819 isl_basic_set_free(bounds);
821 if (!build->domain || !build->pending || !build->generated)
822 return isl_ast_build_free(build);
824 return build;
825 error:
826 isl_ast_build_free(build);
827 isl_basic_set_free(bounds);
828 return NULL;
831 /* Intersect build->domain with "set", where "set" is specified
832 * in terms of the internal schedule domain.
834 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
835 __isl_take isl_ast_build *build, __isl_take isl_set *set)
837 build = isl_ast_build_cow(build);
838 if (!build)
839 goto error;
841 set = isl_set_compute_divs(set);
842 build->domain = isl_set_intersect(build->domain, set);
843 build->domain = isl_set_coalesce(build->domain);
845 if (!build->domain)
846 return isl_ast_build_free(build);
848 return build;
849 error:
850 isl_ast_build_free(build);
851 isl_set_free(set);
852 return NULL;
855 /* Intersect build->generated and build->domain with "set",
856 * where "set" is specified in terms of the internal schedule domain.
858 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
859 __isl_take isl_ast_build *build, __isl_take isl_set *set)
861 set = isl_set_compute_divs(set);
862 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
863 build = isl_ast_build_cow(build);
864 if (!build)
865 goto error;
867 build->generated = isl_set_intersect(build->generated, set);
868 build->generated = isl_set_coalesce(build->generated);
870 if (!build->generated)
871 return isl_ast_build_free(build);
873 return build;
874 error:
875 isl_ast_build_free(build);
876 isl_set_free(set);
877 return NULL;
880 /* Replace the set of pending constraints by "guard", which is then
881 * no longer considered as pending.
882 * That is, add "guard" to the generated constraints and clear all pending
883 * constraints, making the domain equal to the generated constraints.
885 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
886 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
888 build = isl_ast_build_restrict_generated(build, guard);
889 build = isl_ast_build_cow(build);
890 if (!build)
891 return NULL;
893 isl_set_free(build->domain);
894 build->domain = isl_set_copy(build->generated);
895 isl_set_free(build->pending);
896 build->pending = isl_set_universe(isl_set_get_space(build->domain));
898 if (!build->pending)
899 return isl_ast_build_free(build);
901 return build;
904 /* Intersect build->pending and build->domain with "set",
905 * where "set" is specified in terms of the internal schedule domain.
907 __isl_give isl_ast_build *isl_ast_build_restrict_pending(
908 __isl_take isl_ast_build *build, __isl_take isl_set *set)
910 set = isl_set_compute_divs(set);
911 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
912 build = isl_ast_build_cow(build);
913 if (!build)
914 goto error;
916 build->pending = isl_set_intersect(build->pending, set);
917 build->pending = isl_set_coalesce(build->pending);
919 if (!build->pending)
920 return isl_ast_build_free(build);
922 return build;
923 error:
924 isl_ast_build_free(build);
925 isl_set_free(set);
926 return NULL;
929 /* Intersect build->domain with "set", where "set" is specified
930 * in terms of the external schedule domain.
932 __isl_give isl_ast_build *isl_ast_build_restrict(
933 __isl_take isl_ast_build *build, __isl_take isl_set *set)
935 if (isl_set_is_params(set))
936 return isl_ast_build_restrict_generated(build, set);
938 if (isl_ast_build_need_schedule_map(build)) {
939 isl_multi_aff *ma;
940 ma = isl_ast_build_get_schedule_map_multi_aff(build);
941 set = isl_set_preimage_multi_aff(set, ma);
943 return isl_ast_build_restrict_generated(build, set);
946 /* Replace build->executed by "executed".
948 __isl_give isl_ast_build *isl_ast_build_set_executed(
949 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
951 build = isl_ast_build_cow(build);
952 if (!build)
953 goto error;
955 isl_union_map_free(build->executed);
956 build->executed = executed;
958 return build;
959 error:
960 isl_ast_build_free(build);
961 isl_union_map_free(executed);
962 return NULL;
965 /* Does "build" point to a band node?
966 * That is, are we currently handling a band node inside a schedule tree?
968 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
970 if (!build)
971 return -1;
972 return build->node != NULL;
975 /* Return a copy of the band node that "build" refers to.
977 __isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
978 __isl_keep isl_ast_build *build)
980 if (!build)
981 return NULL;
982 return isl_schedule_node_copy(build->node);
985 /* Extract the loop AST generation types for the members of build->node
986 * and store them in build->loop_type.
988 static __isl_give isl_ast_build *extract_loop_types(
989 __isl_take isl_ast_build *build)
991 int i;
992 isl_ctx *ctx;
993 isl_schedule_node *node;
995 if (!build)
996 return NULL;
997 ctx = isl_ast_build_get_ctx(build);
998 if (!build->node)
999 isl_die(ctx, isl_error_internal, "missing AST node",
1000 return isl_ast_build_free(build));
1002 free(build->loop_type);
1003 build->n = isl_schedule_node_band_n_member(build->node);
1004 build->loop_type = isl_alloc_array(ctx,
1005 enum isl_ast_loop_type, build->n);
1006 if (build->n && !build->loop_type)
1007 return isl_ast_build_free(build);
1008 node = build->node;
1009 for (i = 0; i < build->n; ++i)
1010 build->loop_type[i] =
1011 isl_schedule_node_band_member_get_ast_loop_type(node, i);
1013 return build;
1016 /* Replace the band node that "build" refers to by "node" and
1017 * extract the corresponding loop AST generation types.
1019 __isl_give isl_ast_build *isl_ast_build_set_schedule_node(
1020 __isl_take isl_ast_build *build,
1021 __isl_take isl_schedule_node *node)
1023 build = isl_ast_build_cow(build);
1024 if (!build || !node)
1025 goto error;
1027 isl_schedule_node_free(build->node);
1028 build->node = node;
1030 build = extract_loop_types(build);
1032 return build;
1033 error:
1034 isl_ast_build_free(build);
1035 isl_schedule_node_free(node);
1036 return NULL;
1039 /* Remove any reference to a band node from "build".
1041 __isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
1042 __isl_take isl_ast_build *build)
1044 build = isl_ast_build_cow(build);
1045 if (!build)
1046 return NULL;
1048 isl_schedule_node_free(build->node);
1049 build->node = NULL;
1051 return build;
1054 /* Return a copy of the current schedule domain.
1056 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
1058 return build ? isl_set_copy(build->domain) : NULL;
1061 /* Return a copy of the set of pending constraints.
1063 __isl_give isl_set *isl_ast_build_get_pending(
1064 __isl_keep isl_ast_build *build)
1066 return build ? isl_set_copy(build->pending) : NULL;
1069 /* Return a copy of the set of generated constraints.
1071 __isl_give isl_set *isl_ast_build_get_generated(
1072 __isl_keep isl_ast_build *build)
1074 return build ? isl_set_copy(build->generated) : NULL;
1077 /* Return a copy of the map from the internal schedule domain
1078 * to the original input schedule domain.
1080 __isl_give isl_multi_aff *isl_ast_build_get_internal2input(
1081 __isl_keep isl_ast_build *build)
1083 return build ? isl_multi_aff_copy(build->internal2input) : NULL;
1086 /* Return the number of variables of the given type
1087 * in the (internal) schedule space.
1089 unsigned isl_ast_build_dim(__isl_keep isl_ast_build *build,
1090 enum isl_dim_type type)
1092 if (!build)
1093 return 0;
1094 return isl_set_dim(build->domain, type);
1097 /* Return the (schedule) space of "build".
1099 * If "internal" is set, then this space is the space of the internal
1100 * representation of the entire schedule, including those parts for
1101 * which no code has been generated yet.
1103 * If "internal" is not set, then this space is the external representation
1104 * of the loops generated so far.
1106 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
1107 int internal)
1109 int i;
1110 int dim;
1111 isl_space *space;
1113 if (!build)
1114 return NULL;
1116 space = isl_set_get_space(build->domain);
1117 if (internal)
1118 return space;
1120 if (!isl_ast_build_need_schedule_map(build))
1121 return space;
1123 dim = isl_set_dim(build->domain, isl_dim_set);
1124 space = isl_space_drop_dims(space, isl_dim_set,
1125 build->depth, dim - build->depth);
1126 for (i = build->depth - 1; i >= 0; --i)
1127 if (isl_ast_build_has_affine_value(build, i))
1128 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
1130 return space;
1133 /* Return the external representation of the schedule space of "build",
1134 * i.e., a space with a dimension for each loop generated so far,
1135 * with the names of the dimensions set to the loop iterators.
1137 __isl_give isl_space *isl_ast_build_get_schedule_space(
1138 __isl_keep isl_ast_build *build)
1140 isl_space *space;
1141 int i, skip;
1143 if (!build)
1144 return NULL;
1146 space = isl_ast_build_get_space(build, 0);
1148 skip = 0;
1149 for (i = 0; i < build->depth; ++i) {
1150 isl_id *id;
1152 if (isl_ast_build_has_affine_value(build, i)) {
1153 skip++;
1154 continue;
1157 id = isl_ast_build_get_iterator_id(build, i);
1158 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1161 return space;
1164 /* Return the current schedule, as stored in build->executed, in terms
1165 * of the external schedule domain.
1167 __isl_give isl_union_map *isl_ast_build_get_schedule(
1168 __isl_keep isl_ast_build *build)
1170 isl_union_map *executed;
1171 isl_union_map *schedule;
1173 if (!build)
1174 return NULL;
1176 executed = isl_union_map_copy(build->executed);
1177 if (isl_ast_build_need_schedule_map(build)) {
1178 isl_map *proj = isl_ast_build_get_schedule_map(build);
1179 executed = isl_union_map_apply_domain(executed,
1180 isl_union_map_from_map(proj));
1182 schedule = isl_union_map_reverse(executed);
1184 return schedule;
1187 /* Return the iterator attached to the internal schedule dimension "pos".
1189 __isl_give isl_id *isl_ast_build_get_iterator_id(
1190 __isl_keep isl_ast_build *build, int pos)
1192 if (!build)
1193 return NULL;
1195 return isl_id_list_get_id(build->iterators, pos);
1198 /* Set the stride and offset of the current dimension to the given
1199 * value and expression.
1201 * If we had already found a stride before, then the two strides
1202 * are combined into a single stride.
1204 * In particular, if the new stride information is of the form
1206 * i = f + s (...)
1208 * and the old stride information is of the form
1210 * i = f2 + s2 (...)
1212 * then we compute the extended gcd of s and s2
1214 * a s + b s2 = g,
1216 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
1217 * and the second with t2 = a s1/g.
1218 * This results in
1220 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
1222 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
1223 * is the combined stride.
1225 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1226 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1228 int pos;
1230 build = isl_ast_build_cow(build);
1231 if (!build || !stride || !offset)
1232 goto error;
1234 pos = build->depth;
1236 if (isl_ast_build_has_stride(build, pos)) {
1237 isl_val *stride2, *a, *b, *g;
1238 isl_aff *offset2;
1240 stride2 = isl_vec_get_element_val(build->strides, pos);
1241 g = isl_val_gcdext(isl_val_copy(stride), isl_val_copy(stride2),
1242 &a, &b);
1243 a = isl_val_mul(a, isl_val_copy(stride));
1244 a = isl_val_div(a, isl_val_copy(g));
1245 stride2 = isl_val_div(stride2, g);
1246 b = isl_val_mul(b, isl_val_copy(stride2));
1247 stride = isl_val_mul(stride, stride2);
1249 offset2 = isl_multi_aff_get_aff(build->offsets, pos);
1250 offset2 = isl_aff_scale_val(offset2, a);
1251 offset = isl_aff_scale_val(offset, b);
1252 offset = isl_aff_add(offset, offset2);
1255 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1256 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1257 if (!build->strides || !build->offsets)
1258 return isl_ast_build_free(build);
1260 return build;
1261 error:
1262 isl_val_free(stride);
1263 isl_aff_free(offset);
1264 return isl_ast_build_free(build);
1267 /* Return a set expressing the stride constraint at the current depth.
1269 * In particular, if the current iterator (i) is known to attain values
1271 * f + s a
1273 * where f is the offset and s is the stride, then the returned set
1274 * expresses the constraint
1276 * (f - i) mod s = 0
1278 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1279 __isl_keep isl_ast_build *build)
1281 isl_aff *aff;
1282 isl_set *set;
1283 isl_val *stride;
1284 int pos;
1286 if (!build)
1287 return NULL;
1289 pos = build->depth;
1291 if (!isl_ast_build_has_stride(build, pos))
1292 return isl_set_universe(isl_ast_build_get_space(build, 1));
1294 stride = isl_ast_build_get_stride(build, pos);
1295 aff = isl_ast_build_get_offset(build, pos);
1296 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1297 aff = isl_aff_mod_val(aff, stride);
1298 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1300 return set;
1303 /* Return the expansion implied by the stride and offset at the current
1304 * depth.
1306 * That is, return the mapping
1308 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1309 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1311 * where s is the stride at the current depth d and offset(i) is
1312 * the corresponding offset.
1314 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1315 __isl_keep isl_ast_build *build)
1317 isl_space *space;
1318 isl_multi_aff *ma;
1319 int pos;
1320 isl_aff *aff, *offset;
1321 isl_val *stride;
1323 if (!build)
1324 return NULL;
1326 pos = isl_ast_build_get_depth(build);
1327 space = isl_ast_build_get_space(build, 1);
1328 space = isl_space_map_from_set(space);
1329 ma = isl_multi_aff_identity(space);
1331 if (!isl_ast_build_has_stride(build, pos))
1332 return ma;
1334 offset = isl_ast_build_get_offset(build, pos);
1335 stride = isl_ast_build_get_stride(build, pos);
1336 aff = isl_multi_aff_get_aff(ma, pos);
1337 aff = isl_aff_scale_val(aff, stride);
1338 aff = isl_aff_add(aff, offset);
1339 ma = isl_multi_aff_set_aff(ma, pos, aff);
1341 return ma;
1344 /* Add constraints corresponding to any previously detected
1345 * stride on the current dimension to build->domain.
1347 __isl_give isl_ast_build *isl_ast_build_include_stride(
1348 __isl_take isl_ast_build *build)
1350 isl_set *set;
1352 if (!build)
1353 return NULL;
1354 if (!isl_ast_build_has_stride(build, build->depth))
1355 return build;
1356 build = isl_ast_build_cow(build);
1357 if (!build)
1358 return NULL;
1360 set = isl_ast_build_get_stride_constraint(build);
1362 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1363 build->generated = isl_set_intersect(build->generated, set);
1364 if (!build->domain || !build->generated)
1365 return isl_ast_build_free(build);
1367 return build;
1370 /* Information used inside detect_stride.
1372 * "build" may be updated by detect_stride to include stride information.
1373 * "pos" is equal to build->depth.
1375 struct isl_detect_stride_data {
1376 isl_ast_build *build;
1377 int pos;
1380 /* Check if constraint "c" imposes any stride on dimension data->pos
1381 * and, if so, update the stride information in data->build.
1383 * In order to impose a stride on the dimension, "c" needs to be an equality
1384 * and it needs to involve the dimension. Note that "c" may also be
1385 * a div constraint and thus an inequality that we cannot use.
1387 * Let c be of the form
1389 * h(p) + g * v * i + g * stride * f(alpha) = 0
1391 * with h(p) an expression in terms of the parameters and outer dimensions
1392 * and f(alpha) an expression in terms of the existentially quantified
1393 * variables. Note that the inner dimensions have been eliminated so
1394 * they do not appear in "c".
1396 * If "stride" is not zero and not one, then it represents a non-trivial stride
1397 * on "i". We compute a and b such that
1399 * a v + b stride = 1
1401 * We have
1403 * g v i = -h(p) + g stride f(alpha)
1405 * a g v i = -a h(p) + g stride f(alpha)
1407 * a g v i + b g stride i = -a h(p) + g stride * (...)
1409 * g i = -a h(p) + g stride * (...)
1411 * i = -a h(p)/g + stride * (...)
1413 * The expression "-a h(p)/g" can therefore be used as offset.
1415 static int detect_stride(__isl_take isl_constraint *c, void *user)
1417 struct isl_detect_stride_data *data = user;
1418 int i, n_div;
1419 isl_ctx *ctx;
1420 isl_val *v, *stride, *m;
1422 if (!isl_constraint_is_equality(c) ||
1423 !isl_constraint_involves_dims(c, isl_dim_set, data->pos, 1)) {
1424 isl_constraint_free(c);
1425 return 0;
1428 ctx = isl_constraint_get_ctx(c);
1429 stride = isl_val_zero(ctx);
1430 n_div = isl_constraint_dim(c, isl_dim_div);
1431 for (i = 0; i < n_div; ++i) {
1432 v = isl_constraint_get_coefficient_val(c, isl_dim_div, i);
1433 stride = isl_val_gcd(stride, v);
1436 v = isl_constraint_get_coefficient_val(c, isl_dim_set, data->pos);
1437 m = isl_val_gcd(isl_val_copy(stride), isl_val_copy(v));
1438 stride = isl_val_div(stride, isl_val_copy(m));
1439 v = isl_val_div(v, isl_val_copy(m));
1441 if (!isl_val_is_zero(stride) && !isl_val_is_one(stride)) {
1442 isl_aff *aff;
1443 isl_val *gcd, *a, *b;
1445 gcd = isl_val_gcdext(v, isl_val_copy(stride), &a, &b);
1446 isl_val_free(gcd);
1447 isl_val_free(b);
1449 aff = isl_constraint_get_aff(c);
1450 for (i = 0; i < n_div; ++i)
1451 aff = isl_aff_set_coefficient_si(aff,
1452 isl_dim_div, i, 0);
1453 aff = isl_aff_set_coefficient_si(aff, isl_dim_in, data->pos, 0);
1454 a = isl_val_neg(a);
1455 aff = isl_aff_scale_val(aff, a);
1456 aff = isl_aff_scale_down_val(aff, m);
1457 data->build = set_stride(data->build, stride, aff);
1458 } else {
1459 isl_val_free(stride);
1460 isl_val_free(m);
1461 isl_val_free(v);
1464 isl_constraint_free(c);
1465 return 0;
1468 /* Check if the constraints in "set" imply any stride on the current
1469 * dimension and, if so, record the stride information in "build"
1470 * and return the updated "build".
1472 * We compute the affine hull and then check if any of the constraints
1473 * in the hull imposes any stride on the current dimension.
1475 * We assume that inner dimensions have been eliminated from "set"
1476 * by the caller. This is needed because the common stride
1477 * may be imposed by different inner dimensions on different parts of
1478 * the domain.
1480 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1481 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1483 isl_basic_set *hull;
1484 struct isl_detect_stride_data data;
1486 if (!build)
1487 goto error;
1489 data.build = build;
1490 data.pos = isl_ast_build_get_depth(build);
1491 hull = isl_set_affine_hull(set);
1493 if (isl_basic_set_foreach_constraint(hull, &detect_stride, &data) < 0)
1494 data.build = isl_ast_build_free(data.build);
1496 isl_basic_set_free(hull);
1497 return data.build;
1498 error:
1499 isl_set_free(set);
1500 return NULL;
1503 struct isl_ast_build_involves_data {
1504 int depth;
1505 int involves;
1508 /* Check if "map" involves the input dimension data->depth.
1510 static int involves_depth(__isl_take isl_map *map, void *user)
1512 struct isl_ast_build_involves_data *data = user;
1514 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1515 isl_map_free(map);
1517 if (data->involves < 0 || data->involves)
1518 return -1;
1519 return 0;
1522 /* Do any options depend on the value of the dimension at the current depth?
1524 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1526 struct isl_ast_build_involves_data data;
1528 if (!build)
1529 return -1;
1531 data.depth = build->depth;
1532 data.involves = 0;
1534 if (isl_union_map_foreach_map(build->options,
1535 &involves_depth, &data) < 0) {
1536 if (data.involves < 0 || !data.involves)
1537 return -1;
1540 return data.involves;
1543 /* Construct the map
1545 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1547 * with "space" the parameter space of the constructed map.
1549 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1550 int pos)
1552 isl_constraint *c;
1553 isl_basic_map *bmap1, *bmap2;
1555 space = isl_space_set_from_params(space);
1556 space = isl_space_add_dims(space, isl_dim_set, 1);
1557 space = isl_space_map_from_set(space);
1558 c = isl_equality_alloc(isl_local_space_from_space(space));
1559 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1560 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1561 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1562 c = isl_constraint_set_constant_si(c, 1);
1563 bmap2 = isl_basic_map_from_constraint(c);
1565 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1566 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1568 return isl_basic_map_union(bmap1, bmap2);
1571 static const char *option_str[] = {
1572 [isl_ast_loop_atomic] = "atomic",
1573 [isl_ast_loop_unroll] = "unroll",
1574 [isl_ast_loop_separate] = "separate"
1577 /* Update the "options" to reflect the insertion of a dimension
1578 * at position "pos" in the schedule domain space.
1579 * "space" is the original domain space before the insertion and
1580 * may be named and/or structured.
1582 * The (relevant) input options all have "space" as domain, which
1583 * has to be mapped to the extended space.
1584 * The values of the ranges also refer to the schedule domain positions
1585 * and they therefore also need to be adjusted. In particular, values
1586 * smaller than pos do not need to change, while values greater than or
1587 * equal to pos need to be incremented.
1588 * That is, we need to apply the following map.
1590 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1591 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1592 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1593 * separation_class[[i] -> [c]]
1594 * -> separation_class[[i] -> [c]] : i < pos;
1595 * separation_class[[i] -> [c]]
1596 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1598 static __isl_give isl_union_map *options_insert_dim(
1599 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1601 isl_map *map;
1602 isl_union_map *insertion;
1603 enum isl_ast_loop_type type;
1604 const char *name = "separation_class";
1606 space = isl_space_map_from_set(space);
1607 map = isl_map_identity(space);
1608 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1609 options = isl_union_map_apply_domain(options,
1610 isl_union_map_from_map(map));
1612 if (!options)
1613 return NULL;
1615 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1617 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1619 for (type = isl_ast_loop_atomic;
1620 type <= isl_ast_loop_separate; ++type) {
1621 isl_map *map_type = isl_map_copy(map);
1622 const char *name = option_str[type];
1623 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1624 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1625 insertion = isl_union_map_add_map(insertion, map_type);
1628 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1629 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1630 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1631 insertion = isl_union_map_add_map(insertion, map);
1633 options = isl_union_map_apply_range(options, insertion);
1635 return options;
1638 /* If we are generating an AST from a schedule tree (build->node is set),
1639 * then update the loop AST generation types
1640 * to reflect the insertion of a dimension at (global) position "pos"
1641 * in the schedule domain space.
1643 static __isl_give isl_ast_build *node_insert_dim(
1644 __isl_take isl_ast_build *build, int pos)
1646 int i;
1647 int local_pos;
1648 enum isl_ast_loop_type *loop_type;
1649 isl_ctx *ctx;
1651 build = isl_ast_build_cow(build);
1652 if (!build)
1653 return NULL;
1654 if (!build->node)
1655 return build;
1657 ctx = isl_ast_build_get_ctx(build);
1658 local_pos = pos - build->outer_pos;
1659 loop_type = isl_realloc_array(ctx, build->loop_type,
1660 enum isl_ast_loop_type, build->n + 1);
1661 if (!loop_type)
1662 return isl_ast_build_free(build);
1663 build->loop_type = loop_type;
1664 for (i = build->n - 1; i >= local_pos; --i)
1665 loop_type[i + 1] = loop_type[i];
1666 loop_type[local_pos] = isl_ast_loop_default;
1667 build->n++;
1669 return build;
1672 /* Insert a single dimension in the schedule domain at position "pos".
1673 * The new dimension is given an isl_id with the empty string as name.
1675 * The main difficulty is updating build->options to reflect the
1676 * extra dimension. This is handled in options_insert_dim.
1678 * Note that because of the dimension manipulations, the resulting
1679 * schedule domain space will always be unnamed and unstructured.
1680 * However, the original schedule domain space may be named and/or
1681 * structured, so we have to take this possibility into account
1682 * while performing the transformations.
1684 * Since the inserted schedule dimension is used by the caller
1685 * to differentiate between different domain spaces, there is
1686 * no longer a uniform mapping from the internal schedule space
1687 * to the input schedule space. The internal2input mapping is
1688 * therefore removed.
1690 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1691 __isl_take isl_ast_build *build, int pos)
1693 isl_ctx *ctx;
1694 isl_space *space, *ma_space;
1695 isl_id *id;
1696 isl_multi_aff *ma;
1698 build = isl_ast_build_cow(build);
1699 if (!build)
1700 return NULL;
1702 ctx = isl_ast_build_get_ctx(build);
1703 id = isl_id_alloc(ctx, "", NULL);
1704 if (!build->node)
1705 space = isl_ast_build_get_space(build, 1);
1706 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1707 build->domain = isl_set_insert_dims(build->domain,
1708 isl_dim_set, pos, 1);
1709 build->generated = isl_set_insert_dims(build->generated,
1710 isl_dim_set, pos, 1);
1711 build->pending = isl_set_insert_dims(build->pending,
1712 isl_dim_set, pos, 1);
1713 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1714 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1715 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1716 ma_space = isl_space_set_from_params(ma_space);
1717 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1718 ma_space = isl_space_map_from_set(ma_space);
1719 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1720 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1721 ma = isl_multi_aff_identity(ma_space);
1722 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1723 if (!build->node)
1724 build->options = options_insert_dim(build->options, space, pos);
1725 build->internal2input = isl_multi_aff_free(build->internal2input);
1727 if (!build->iterators || !build->domain || !build->generated ||
1728 !build->pending || !build->values ||
1729 !build->strides || !build->offsets || !build->options)
1730 return isl_ast_build_free(build);
1732 build = node_insert_dim(build, pos);
1734 return build;
1737 /* Scale down the current dimension by a factor of "m".
1738 * "umap" is an isl_union_map that implements the scaling down.
1739 * That is, it is of the form
1741 * { [.... i ....] -> [.... i' ....] : i = m i' }
1743 * This function is called right after the strides have been
1744 * detected, but before any constraints on the current dimension
1745 * have been included in build->domain.
1746 * We therefore only need to update stride, offset, the options and
1747 * the mapping from internal schedule space to the original schedule
1748 * space, if we are still keeping track of such a mapping.
1749 * The latter mapping is updated by plugging in
1750 * { [... i ...] -> [... m i ... ] }.
1752 __isl_give isl_ast_build *isl_ast_build_scale_down(
1753 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1754 __isl_take isl_union_map *umap)
1756 isl_aff *aff;
1757 isl_val *v;
1758 int depth;
1760 build = isl_ast_build_cow(build);
1761 if (!build || !umap || !m)
1762 goto error;
1764 depth = build->depth;
1766 if (build->internal2input) {
1767 isl_space *space;
1768 isl_multi_aff *ma;
1769 isl_aff *aff;
1771 space = isl_multi_aff_get_space(build->internal2input);
1772 space = isl_space_map_from_set(isl_space_domain(space));
1773 ma = isl_multi_aff_identity(space);
1774 aff = isl_multi_aff_get_aff(ma, depth);
1775 aff = isl_aff_scale_val(aff, isl_val_copy(m));
1776 ma = isl_multi_aff_set_aff(ma, depth, aff);
1777 build->internal2input =
1778 isl_multi_aff_pullback_multi_aff(build->internal2input, ma);
1779 if (!build->internal2input)
1780 goto error;
1783 v = isl_vec_get_element_val(build->strides, depth);
1784 v = isl_val_div(v, isl_val_copy(m));
1785 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1787 aff = isl_multi_aff_get_aff(build->offsets, depth);
1788 aff = isl_aff_scale_down_val(aff, m);
1789 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1790 build->options = isl_union_map_apply_domain(build->options, umap);
1791 if (!build->strides || !build->offsets || !build->options)
1792 return isl_ast_build_free(build);
1794 return build;
1795 error:
1796 isl_val_free(m);
1797 isl_union_map_free(umap);
1798 return isl_ast_build_free(build);
1801 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1802 * If an isl_id with such a name already appears among the parameters
1803 * in build->domain, then adjust the name to "c%d_%d".
1805 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1806 __isl_keep isl_ast_build *build)
1808 int i;
1809 isl_id_list *names;
1811 names = isl_id_list_alloc(ctx, n);
1812 for (i = 0; i < n; ++i) {
1813 isl_id *id;
1815 id = generate_name(ctx, first + i, build);
1816 names = isl_id_list_add(names, id);
1819 return names;
1822 /* Embed "options" into the given isl_ast_build space.
1824 * This function is called from within a nested call to
1825 * isl_ast_build_node_from_schedule_map.
1826 * "options" refers to the additional schedule,
1827 * while space refers to both the space of the outer isl_ast_build and
1828 * that of the additional schedule.
1829 * Specifically, space is of the form
1831 * [I -> S]
1833 * while options lives in the space(s)
1835 * S -> *
1837 * We compute
1839 * [I -> S] -> S
1841 * and compose this with options, to obtain the new options
1842 * living in the space(s)
1844 * [I -> S] -> *
1846 static __isl_give isl_union_map *embed_options(
1847 __isl_take isl_union_map *options, __isl_take isl_space *space)
1849 isl_map *map;
1851 map = isl_map_universe(isl_space_unwrap(space));
1852 map = isl_map_range_map(map);
1854 options = isl_union_map_apply_range(
1855 isl_union_map_from_map(map), options);
1857 return options;
1860 /* Update "build" for use in a (possibly nested) code generation. That is,
1861 * extend "build" from an AST build on some domain O to an AST build
1862 * on domain [O -> S], with S corresponding to "space".
1863 * If the original domain is a parameter domain, then the new domain is
1864 * simply S.
1865 * "iterators" is a list of iterators for S, but the number of elements
1866 * may be smaller or greater than the number of set dimensions of S.
1867 * If "keep_iterators" is set, then any extra ids in build->iterators
1868 * are reused for S. Otherwise, these extra ids are dropped.
1870 * We first update build->outer_pos to the current depth.
1871 * This depth is zero in case this is the outermost code generation.
1873 * We then add additional ids such that the number of iterators is at least
1874 * equal to the dimension of the new build domain.
1876 * If the original domain is parametric, then we are constructing
1877 * an isl_ast_build for the outer code generation and we pass control
1878 * to isl_ast_build_init.
1880 * Otherwise, we adjust the fields of "build" to include "space".
1882 __isl_give isl_ast_build *isl_ast_build_product(
1883 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1885 isl_ctx *ctx;
1886 isl_vec *strides;
1887 isl_set *set;
1888 isl_multi_aff *embedding;
1889 int dim, n_it;
1891 build = isl_ast_build_cow(build);
1892 if (!build)
1893 goto error;
1895 build->outer_pos = build->depth;
1897 ctx = isl_ast_build_get_ctx(build);
1898 dim = isl_set_dim(build->domain, isl_dim_set);
1899 dim += isl_space_dim(space, isl_dim_set);
1900 n_it = isl_id_list_n_id(build->iterators);
1901 if (n_it < dim) {
1902 isl_id_list *l;
1903 l = generate_names(ctx, dim - n_it, n_it, build);
1904 build->iterators = isl_id_list_concat(build->iterators, l);
1907 if (isl_set_is_params(build->domain))
1908 return isl_ast_build_init(build, space);
1910 set = isl_set_universe(isl_space_copy(space));
1911 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1912 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1913 build->generated = isl_set_product(build->generated, set);
1915 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
1916 strides = isl_vec_set_si(strides, 1);
1917 build->strides = isl_vec_concat(build->strides, strides);
1919 space = isl_space_map_from_set(space);
1920 build->offsets = isl_multi_aff_align_params(build->offsets,
1921 isl_space_copy(space));
1922 build->offsets = isl_multi_aff_product(build->offsets,
1923 isl_multi_aff_zero(isl_space_copy(space)));
1924 build->values = isl_multi_aff_align_params(build->values,
1925 isl_space_copy(space));
1926 embedding = isl_multi_aff_identity(space);
1927 build->values = isl_multi_aff_product(build->values,
1928 isl_multi_aff_copy(embedding));
1929 if (build->internal2input) {
1930 build->internal2input =
1931 isl_multi_aff_product(build->internal2input, embedding);
1932 build->internal2input =
1933 isl_multi_aff_flatten_range(build->internal2input);
1934 if (!build->internal2input)
1935 return isl_ast_build_free(build);
1936 } else {
1937 isl_multi_aff_free(embedding);
1940 space = isl_ast_build_get_space(build, 1);
1941 build->options = embed_options(build->options, space);
1943 if (!build->iterators || !build->domain || !build->generated ||
1944 !build->pending || !build->values ||
1945 !build->strides || !build->offsets || !build->options)
1946 return isl_ast_build_free(build);
1948 return build;
1949 error:
1950 isl_ast_build_free(build);
1951 isl_space_free(space);
1952 return NULL;
1955 /* Does "aff" only attain non-negative values over build->domain?
1956 * That is, does it not attain any negative values?
1958 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
1959 __isl_keep isl_aff *aff)
1961 isl_set *test;
1962 int empty;
1964 if (!build)
1965 return -1;
1967 aff = isl_aff_copy(aff);
1968 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
1969 test = isl_set_intersect(test, isl_set_copy(build->domain));
1970 empty = isl_set_is_empty(test);
1971 isl_set_free(test);
1973 return empty;
1976 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1978 int isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
1980 isl_val *v;
1981 int has_stride;
1983 if (!build)
1984 return -1;
1986 v = isl_vec_get_element_val(build->strides, pos);
1987 if (!v)
1988 return -1;
1989 has_stride = !isl_val_is_one(v);
1990 isl_val_free(v);
1992 return has_stride;
1995 /* Given that the dimension at position "pos" takes on values
1997 * f + s a
1999 * with a an integer, return s through *stride.
2001 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
2002 int pos)
2004 if (!build)
2005 return NULL;
2007 return isl_vec_get_element_val(build->strides, pos);
2010 /* Given that the dimension at position "pos" takes on values
2012 * f + s a
2014 * with a an integer, return f.
2016 __isl_give isl_aff *isl_ast_build_get_offset(
2017 __isl_keep isl_ast_build *build, int pos)
2019 if (!build)
2020 return NULL;
2022 return isl_multi_aff_get_aff(build->offsets, pos);
2025 /* Is the dimension at position "pos" known to attain only a single
2026 * value that, moreover, can be described by a single affine expression
2027 * in terms of the outer dimensions and parameters?
2029 * If not, then the corresponding affine expression in build->values
2030 * is set to be equal to the same input dimension.
2031 * Otherwise, it is set to the requested expression in terms of
2032 * outer dimensions and parameters.
2034 int isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
2035 int pos)
2037 isl_aff *aff;
2038 int involves;
2040 if (!build)
2041 return -1;
2043 aff = isl_multi_aff_get_aff(build->values, pos);
2044 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
2045 isl_aff_free(aff);
2047 if (involves < 0)
2048 return -1;
2050 return !involves;
2053 /* Plug in the known values (fixed affine expressions in terms of
2054 * parameters and outer loop iterators) of all loop iterators
2055 * in the domain of "umap".
2057 * We simply precompose "umap" with build->values.
2059 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
2060 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
2062 isl_multi_aff *values;
2064 if (!build)
2065 return isl_union_map_free(umap);
2067 values = isl_multi_aff_copy(build->values);
2068 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
2070 return umap;
2073 /* Is the current dimension known to attain only a single value?
2075 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
2077 if (!build)
2078 return -1;
2080 return build->value != NULL;
2083 /* Simplify the basic set "bset" based on what we know about
2084 * the iterators of already generated loops.
2086 * "bset" is assumed to live in the (internal) schedule domain.
2088 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
2089 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2091 if (!build)
2092 goto error;
2094 bset = isl_basic_set_preimage_multi_aff(bset,
2095 isl_multi_aff_copy(build->values));
2096 bset = isl_basic_set_gist(bset,
2097 isl_set_simple_hull(isl_set_copy(build->domain)));
2099 return bset;
2100 error:
2101 isl_basic_set_free(bset);
2102 return NULL;
2105 /* Simplify the set "set" based on what we know about
2106 * the iterators of already generated loops.
2108 * "set" is assumed to live in the (internal) schedule domain.
2110 __isl_give isl_set *isl_ast_build_compute_gist(
2111 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2113 if (!build)
2114 goto error;
2116 if (!isl_set_is_params(set))
2117 set = isl_set_preimage_multi_aff(set,
2118 isl_multi_aff_copy(build->values));
2119 set = isl_set_gist(set, isl_set_copy(build->domain));
2121 return set;
2122 error:
2123 isl_set_free(set);
2124 return NULL;
2127 /* Include information about what we know about the iterators of
2128 * already generated loops to "set".
2130 * We currently only plug in the known affine values of outer loop
2131 * iterators.
2132 * In principle we could also introduce equalities or even other
2133 * constraints implied by the intersection of "set" and build->domain.
2135 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
2136 __isl_take isl_set *set)
2138 if (!build)
2139 return isl_set_free(set);
2141 return isl_set_preimage_multi_aff(set,
2142 isl_multi_aff_copy(build->values));
2145 /* Simplify the map "map" based on what we know about
2146 * the iterators of already generated loops.
2148 * The domain of "map" is assumed to live in the (internal) schedule domain.
2150 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
2151 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
2153 if (!build)
2154 goto error;
2156 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
2158 return map;
2159 error:
2160 isl_map_free(map);
2161 return NULL;
2164 /* Simplify the affine expression "aff" based on what we know about
2165 * the iterators of already generated loops.
2167 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2169 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
2170 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
2172 if (!build)
2173 goto error;
2175 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
2177 return aff;
2178 error:
2179 isl_aff_free(aff);
2180 return NULL;
2183 /* Simplify the piecewise affine expression "aff" based on what we know about
2184 * the iterators of already generated loops.
2186 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2188 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
2189 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2191 if (!build)
2192 goto error;
2194 if (!isl_set_is_params(build->domain))
2195 pa = isl_pw_aff_pullback_multi_aff(pa,
2196 isl_multi_aff_copy(build->values));
2197 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
2199 return pa;
2200 error:
2201 isl_pw_aff_free(pa);
2202 return NULL;
2205 /* Simplify the piecewise multi-affine expression "aff" based on what
2206 * we know about the iterators of already generated loops.
2208 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2210 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
2211 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2213 if (!build)
2214 goto error;
2216 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
2217 isl_multi_aff_copy(build->values));
2218 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2220 return pma;
2221 error:
2222 isl_pw_multi_aff_free(pma);
2223 return NULL;
2226 /* Extract the schedule domain of the given type from build->options
2227 * at the current depth.
2229 * In particular, find the subset of build->options that is of
2230 * the following form
2232 * schedule_domain -> type[depth]
2234 * and return the corresponding domain, after eliminating inner dimensions
2235 * and divs that depend on the current dimension.
2237 * Note that the domain of build->options has been reformulated
2238 * in terms of the internal build space in embed_options,
2239 * but the position is still that within the current code generation.
2241 __isl_give isl_set *isl_ast_build_get_option_domain(
2242 __isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
2244 const char *name;
2245 isl_space *space;
2246 isl_map *option;
2247 isl_set *domain;
2248 int local_pos;
2250 if (!build)
2251 return NULL;
2253 name = option_str[type];
2254 local_pos = build->depth - build->outer_pos;
2256 space = isl_ast_build_get_space(build, 1);
2257 space = isl_space_from_domain(space);
2258 space = isl_space_add_dims(space, isl_dim_out, 1);
2259 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2261 option = isl_union_map_extract_map(build->options, space);
2262 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2264 domain = isl_map_domain(option);
2265 domain = isl_ast_build_eliminate(build, domain);
2267 return domain;
2270 /* How does the user want the current schedule dimension to be generated?
2271 * These choices have been extracted from the schedule node
2272 * in extract_loop_types and stored in build->loop_type.
2273 * They have been updated to reflect any dimension insertion in
2274 * node_insert_dim.
2275 * Return isl_ast_domain_error on error.
2277 enum isl_ast_loop_type isl_ast_build_get_loop_type(
2278 __isl_keep isl_ast_build *build)
2280 int local_pos;
2281 isl_ctx *ctx;
2283 if (!build)
2284 return isl_ast_loop_error;
2285 ctx = isl_ast_build_get_ctx(build);
2286 if (!build->node)
2287 isl_die(ctx, isl_error_internal,
2288 "only works for schedule tree based AST generation",
2289 return isl_ast_loop_error);
2291 local_pos = build->depth - build->outer_pos;
2292 return build->loop_type[local_pos];
2295 /* Extract the separation class mapping at the current depth.
2297 * In particular, find and return the subset of build->options that is of
2298 * the following form
2300 * schedule_domain -> separation_class[[depth] -> [class]]
2302 * The caller is expected to eliminate inner dimensions from the domain.
2304 * Note that the domain of build->options has been reformulated
2305 * in terms of the internal build space in embed_options,
2306 * but the position is still that within the current code generation.
2308 __isl_give isl_map *isl_ast_build_get_separation_class(
2309 __isl_keep isl_ast_build *build)
2311 isl_ctx *ctx;
2312 isl_space *space_sep, *space;
2313 isl_map *res;
2314 int local_pos;
2316 if (!build)
2317 return NULL;
2319 local_pos = build->depth - build->outer_pos;
2320 ctx = isl_ast_build_get_ctx(build);
2321 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2322 space_sep = isl_space_wrap(space_sep);
2323 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2324 "separation_class");
2325 space = isl_ast_build_get_space(build, 1);
2326 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2327 space = isl_space_map_from_domain_and_range(space, space_sep);
2329 res = isl_union_map_extract_map(build->options, space);
2330 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2331 res = isl_map_coalesce(res);
2333 return res;
2336 /* Eliminate dimensions inner to the current dimension.
2338 __isl_give isl_set *isl_ast_build_eliminate_inner(
2339 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2341 int dim;
2342 int depth;
2344 if (!build)
2345 return isl_set_free(set);
2347 dim = isl_set_dim(set, isl_dim_set);
2348 depth = build->depth;
2349 set = isl_set_detect_equalities(set);
2350 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2352 return set;
2355 /* Eliminate unknown divs and divs that depend on the current dimension.
2357 * Note that during the elimination of unknown divs, we may discover
2358 * an explicit representation of some other unknown divs, which may
2359 * depend on the current dimension. We therefore need to eliminate
2360 * unknown divs first.
2362 __isl_give isl_set *isl_ast_build_eliminate_divs(
2363 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2365 int depth;
2367 if (!build)
2368 return isl_set_free(set);
2370 set = isl_set_remove_unknown_divs(set);
2371 depth = build->depth;
2372 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2374 return set;
2377 /* Eliminate dimensions inner to the current dimension as well as
2378 * unknown divs and divs that depend on the current dimension.
2379 * The result then consists only of constraints that are independent
2380 * of the current dimension and upper and lower bounds on the current
2381 * dimension.
2383 __isl_give isl_set *isl_ast_build_eliminate(
2384 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2386 domain = isl_ast_build_eliminate_inner(build, domain);
2387 domain = isl_ast_build_eliminate_divs(build, domain);
2388 return domain;
2391 /* Replace build->single_valued by "sv".
2393 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2394 __isl_take isl_ast_build *build, int sv)
2396 if (!build)
2397 return build;
2398 if (build->single_valued == sv)
2399 return build;
2400 build = isl_ast_build_cow(build);
2401 if (!build)
2402 return build;
2403 build->single_valued = sv;
2405 return build;