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
15 #include <isl/space.h>
18 #include <isl/constraint.h>
20 #include <isl/union_set.h>
21 #include <isl/union_map.h>
22 #include <isl_ast_build_private.h>
23 #include <isl_ast_private.h>
24 #include <isl_config.h>
26 /* Construct a map that isolates the current dimension.
28 * Essentially, the current dimension of "set" is moved to the single output
29 * dimension in the result, with the current dimension in the domain replaced
30 * by an unconstrained variable.
32 __isl_give isl_map
*isl_ast_build_map_to_iterator(
33 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
37 map
= isl_map_from_domain(set
);
38 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
41 return isl_map_free(map
);
43 map
= isl_map_equate(map
, isl_dim_in
, build
->depth
, isl_dim_out
, 0);
44 map
= isl_map_eliminate(map
, isl_dim_in
, build
->depth
, 1);
49 /* Initialize the information derived during the AST generation to default
50 * values for a schedule domain in "space".
52 * We also check that the remaining fields are not NULL so that
53 * the calling functions don't have to perform this test.
55 static __isl_give isl_ast_build
*isl_ast_build_init_derived(
56 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
62 build
= isl_ast_build_cow(build
);
63 if (!build
|| !build
->domain
)
66 ctx
= isl_ast_build_get_ctx(build
);
67 dim
= isl_space_dim(space
, isl_dim_set
);
70 strides
= isl_vec_alloc(ctx
, dim
);
71 strides
= isl_vec_set_si(strides
, 1);
73 isl_vec_free(build
->strides
);
74 build
->strides
= strides
;
76 space
= isl_space_map_from_set(space
);
77 isl_multi_aff_free(build
->offsets
);
78 build
->offsets
= isl_multi_aff_zero(isl_space_copy(space
));
79 isl_multi_aff_free(build
->values
);
80 build
->values
= isl_multi_aff_identity(isl_space_copy(space
));
81 isl_multi_aff_free(build
->internal2input
);
82 build
->internal2input
= isl_multi_aff_identity(space
);
84 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
85 !build
->pending
|| !build
->values
|| !build
->internal2input
||
86 !build
->strides
|| !build
->offsets
|| !build
->options
)
87 return isl_ast_build_free(build
);
91 isl_space_free(space
);
92 return isl_ast_build_free(build
);
95 /* Return an isl_id called "c%d", with "%d" set to "i".
96 * If an isl_id with such a name already appears among the parameters
97 * in build->domain, then adjust the name to "c%d_%d".
99 static __isl_give isl_id
*generate_name(isl_ctx
*ctx
, int i
,
100 __isl_keep isl_ast_build
*build
)
104 isl_set
*dom
= build
->domain
;
106 snprintf(name
, sizeof(name
), "c%d", i
);
108 while (isl_set_find_dim_by_name(dom
, isl_dim_param
, name
) >= 0)
109 snprintf(name
, sizeof(name
), "c%d_%d", i
, j
++);
110 return isl_id_alloc(ctx
, name
, NULL
);
113 /* Create an isl_ast_build with "set" as domain.
115 * The input set is usually a parameter domain, but we currently allow it to
116 * be any kind of set. We set the domain of the returned isl_ast_build
117 * to "set" and initialize all the other fields to default values.
119 __isl_give isl_ast_build
*isl_ast_build_from_context(__isl_take isl_set
*set
)
125 isl_ast_build
*build
;
127 set
= isl_set_compute_divs(set
);
128 n
= isl_set_dim(set
, isl_dim_set
);
132 ctx
= isl_set_get_ctx(set
);
134 build
= isl_calloc_type(ctx
, isl_ast_build
);
140 build
->generated
= isl_set_copy(build
->domain
);
141 build
->pending
= isl_set_universe(isl_set_get_space(build
->domain
));
142 build
->options
= isl_union_map_empty(isl_space_params_alloc(ctx
, 0));
144 build
->iterators
= isl_id_list_alloc(ctx
, n
);
145 for (i
= 0; i
< n
; ++i
) {
147 if (isl_set_has_dim_id(set
, isl_dim_set
, i
))
148 id
= isl_set_get_dim_id(set
, isl_dim_set
, i
);
150 id
= generate_name(ctx
, i
, build
);
151 build
->iterators
= isl_id_list_add(build
->iterators
, id
);
153 space
= isl_set_get_space(set
);
154 if (isl_space_is_params(space
))
155 space
= isl_space_set_from_params(space
);
157 return isl_ast_build_init_derived(build
, space
);
163 /* Create an isl_ast_build with a universe (parametric) context.
165 __isl_give isl_ast_build
*isl_ast_build_alloc(isl_ctx
*ctx
)
170 space
= isl_space_params_alloc(ctx
, 0);
171 context
= isl_set_universe(space
);
173 return isl_ast_build_from_context(context
);
176 __isl_give isl_ast_build
*isl_ast_build_copy(__isl_keep isl_ast_build
*build
)
185 __isl_give isl_ast_build
*isl_ast_build_dup(__isl_keep isl_ast_build
*build
)
193 ctx
= isl_ast_build_get_ctx(build
);
194 dup
= isl_calloc_type(ctx
, isl_ast_build
);
199 dup
->outer_pos
= build
->outer_pos
;
200 dup
->depth
= build
->depth
;
201 dup
->iterators
= isl_id_list_copy(build
->iterators
);
202 dup
->domain
= isl_set_copy(build
->domain
);
203 dup
->generated
= isl_set_copy(build
->generated
);
204 dup
->pending
= isl_set_copy(build
->pending
);
205 dup
->values
= isl_multi_aff_copy(build
->values
);
206 dup
->internal2input
= isl_multi_aff_copy(build
->internal2input
);
207 dup
->value
= isl_pw_aff_copy(build
->value
);
208 dup
->strides
= isl_vec_copy(build
->strides
);
209 dup
->offsets
= isl_multi_aff_copy(build
->offsets
);
210 dup
->executed
= isl_union_map_copy(build
->executed
);
211 dup
->options
= isl_union_map_copy(build
->options
);
212 dup
->at_each_domain
= build
->at_each_domain
;
213 dup
->at_each_domain_user
= build
->at_each_domain_user
;
214 dup
->before_each_for
= build
->before_each_for
;
215 dup
->before_each_for_user
= build
->before_each_for_user
;
216 dup
->after_each_for
= build
->after_each_for
;
217 dup
->after_each_for_user
= build
->after_each_for_user
;
218 dup
->before_each_mark
= build
->before_each_mark
;
219 dup
->before_each_mark_user
= build
->before_each_mark_user
;
220 dup
->after_each_mark
= build
->after_each_mark
;
221 dup
->after_each_mark_user
= build
->after_each_mark_user
;
222 dup
->create_leaf
= build
->create_leaf
;
223 dup
->create_leaf_user
= build
->create_leaf_user
;
224 dup
->node
= isl_schedule_node_copy(build
->node
);
225 if (build
->loop_type
) {
229 dup
->loop_type
= isl_alloc_array(ctx
,
230 enum isl_ast_loop_type
, dup
->n
);
231 if (dup
->n
&& !dup
->loop_type
)
232 return isl_ast_build_free(dup
);
233 for (i
= 0; i
< dup
->n
; ++i
)
234 dup
->loop_type
[i
] = build
->loop_type
[i
];
237 if (!dup
->iterators
|| !dup
->domain
|| !dup
->generated
||
238 !dup
->pending
|| !dup
->values
||
239 !dup
->strides
|| !dup
->offsets
|| !dup
->options
||
240 (build
->internal2input
&& !dup
->internal2input
) ||
241 (build
->executed
&& !dup
->executed
) ||
242 (build
->value
&& !dup
->value
) ||
243 (build
->node
&& !dup
->node
))
244 return isl_ast_build_free(dup
);
249 /* Align the parameters of "build" to those of "model", introducing
250 * additional parameters if needed.
252 __isl_give isl_ast_build
*isl_ast_build_align_params(
253 __isl_take isl_ast_build
*build
, __isl_take isl_space
*model
)
255 build
= isl_ast_build_cow(build
);
259 build
->domain
= isl_set_align_params(build
->domain
,
260 isl_space_copy(model
));
261 build
->generated
= isl_set_align_params(build
->generated
,
262 isl_space_copy(model
));
263 build
->pending
= isl_set_align_params(build
->pending
,
264 isl_space_copy(model
));
265 build
->values
= isl_multi_aff_align_params(build
->values
,
266 isl_space_copy(model
));
267 build
->offsets
= isl_multi_aff_align_params(build
->offsets
,
268 isl_space_copy(model
));
269 build
->options
= isl_union_map_align_params(build
->options
,
270 isl_space_copy(model
));
271 if (build
->internal2input
) {
272 build
->internal2input
=
273 isl_multi_aff_align_params(build
->internal2input
,
275 if (!build
->internal2input
)
276 return isl_ast_build_free(build
);
278 isl_space_free(model
);
281 if (!build
->domain
|| !build
->values
|| !build
->offsets
||
283 return isl_ast_build_free(build
);
287 isl_space_free(model
);
291 __isl_give isl_ast_build
*isl_ast_build_cow(__isl_take isl_ast_build
*build
)
299 return isl_ast_build_dup(build
);
302 __isl_null isl_ast_build
*isl_ast_build_free(
303 __isl_take isl_ast_build
*build
)
308 if (--build
->ref
> 0)
311 isl_id_list_free(build
->iterators
);
312 isl_set_free(build
->domain
);
313 isl_set_free(build
->generated
);
314 isl_set_free(build
->pending
);
315 isl_multi_aff_free(build
->values
);
316 isl_multi_aff_free(build
->internal2input
);
317 isl_pw_aff_free(build
->value
);
318 isl_vec_free(build
->strides
);
319 isl_multi_aff_free(build
->offsets
);
320 isl_multi_aff_free(build
->schedule_map
);
321 isl_union_map_free(build
->executed
);
322 isl_union_map_free(build
->options
);
323 isl_schedule_node_free(build
->node
);
324 free(build
->loop_type
);
325 isl_set_free(build
->isolated
);
332 isl_ctx
*isl_ast_build_get_ctx(__isl_keep isl_ast_build
*build
)
334 return build
? isl_set_get_ctx(build
->domain
) : NULL
;
337 /* Replace build->options by "options".
339 __isl_give isl_ast_build
*isl_ast_build_set_options(
340 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*options
)
342 build
= isl_ast_build_cow(build
);
344 if (!build
|| !options
)
347 isl_union_map_free(build
->options
);
348 build
->options
= options
;
352 isl_union_map_free(options
);
353 return isl_ast_build_free(build
);
356 /* Set the iterators for the next code generation.
358 * If we still have some iterators left from the previous code generation
359 * (if any) or if iterators have already been set by a previous
360 * call to this function, then we remove them first.
362 __isl_give isl_ast_build
*isl_ast_build_set_iterators(
363 __isl_take isl_ast_build
*build
, __isl_take isl_id_list
*iterators
)
367 build
= isl_ast_build_cow(build
);
371 dim
= isl_ast_build_dim(build
, isl_dim_set
);
372 n_it
= isl_id_list_n_id(build
->iterators
);
373 if (dim
< 0 || n_it
< 0)
376 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
377 "isl_ast_build in inconsistent state", goto error
);
379 build
->iterators
= isl_id_list_drop(build
->iterators
,
381 build
->iterators
= isl_id_list_concat(build
->iterators
, iterators
);
382 if (!build
->iterators
)
383 return isl_ast_build_free(build
);
387 isl_id_list_free(iterators
);
388 return isl_ast_build_free(build
);
391 /* Set the "at_each_domain" callback of "build" to "fn".
393 __isl_give isl_ast_build
*isl_ast_build_set_at_each_domain(
394 __isl_take isl_ast_build
*build
,
395 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_node
*node
,
396 __isl_keep isl_ast_build
*build
, void *user
), void *user
)
398 build
= isl_ast_build_cow(build
);
403 build
->at_each_domain
= fn
;
404 build
->at_each_domain_user
= user
;
409 /* Set the "before_each_for" callback of "build" to "fn".
411 __isl_give isl_ast_build
*isl_ast_build_set_before_each_for(
412 __isl_take isl_ast_build
*build
,
413 __isl_give isl_id
*(*fn
)(__isl_keep isl_ast_build
*build
,
414 void *user
), void *user
)
416 build
= isl_ast_build_cow(build
);
421 build
->before_each_for
= fn
;
422 build
->before_each_for_user
= user
;
427 /* Set the "after_each_for" callback of "build" to "fn".
429 __isl_give isl_ast_build
*isl_ast_build_set_after_each_for(
430 __isl_take isl_ast_build
*build
,
431 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_node
*node
,
432 __isl_keep isl_ast_build
*build
, void *user
), void *user
)
434 build
= isl_ast_build_cow(build
);
439 build
->after_each_for
= fn
;
440 build
->after_each_for_user
= user
;
445 /* Set the "before_each_mark" callback of "build" to "fn".
447 __isl_give isl_ast_build
*isl_ast_build_set_before_each_mark(
448 __isl_take isl_ast_build
*build
,
449 isl_stat (*fn
)(__isl_keep isl_id
*mark
, __isl_keep isl_ast_build
*build
,
450 void *user
), void *user
)
452 build
= isl_ast_build_cow(build
);
457 build
->before_each_mark
= fn
;
458 build
->before_each_mark_user
= user
;
463 /* Set the "after_each_mark" callback of "build" to "fn".
465 __isl_give isl_ast_build
*isl_ast_build_set_after_each_mark(
466 __isl_take isl_ast_build
*build
,
467 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_node
*node
,
468 __isl_keep isl_ast_build
*build
, void *user
), void *user
)
470 build
= isl_ast_build_cow(build
);
475 build
->after_each_mark
= fn
;
476 build
->after_each_mark_user
= user
;
481 /* Set the "create_leaf" callback of "build" to "fn".
483 __isl_give isl_ast_build
*isl_ast_build_set_create_leaf(
484 __isl_take isl_ast_build
*build
,
485 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_build
*build
,
486 void *user
), void *user
)
488 build
= isl_ast_build_cow(build
);
493 build
->create_leaf
= fn
;
494 build
->create_leaf_user
= user
;
499 /* Clear all information that is specific to this code generation
500 * and that is (probably) not meaningful to any nested code generation.
502 __isl_give isl_ast_build
*isl_ast_build_clear_local_info(
503 __isl_take isl_ast_build
*build
)
507 build
= isl_ast_build_cow(build
);
511 space
= isl_union_map_get_space(build
->options
);
512 isl_union_map_free(build
->options
);
513 build
->options
= isl_union_map_empty(space
);
515 build
->at_each_domain
= NULL
;
516 build
->at_each_domain_user
= NULL
;
517 build
->before_each_for
= NULL
;
518 build
->before_each_for_user
= NULL
;
519 build
->after_each_for
= NULL
;
520 build
->after_each_for_user
= NULL
;
521 build
->before_each_mark
= NULL
;
522 build
->before_each_mark_user
= NULL
;
523 build
->after_each_mark
= NULL
;
524 build
->after_each_mark_user
= NULL
;
525 build
->create_leaf
= NULL
;
526 build
->create_leaf_user
= NULL
;
529 return isl_ast_build_free(build
);
534 /* Have any loops been eliminated?
535 * That is, do any of the original schedule dimensions have a fixed
536 * value that has been substituted?
538 static int any_eliminated(isl_ast_build
*build
)
542 for (i
= 0; i
< build
->depth
; ++i
)
543 if (isl_ast_build_has_affine_value(build
, i
))
549 /* Clear build->schedule_map.
550 * This function should be called whenever anything that might affect
551 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
552 * In particular, it should be called when the depth is changed or
553 * when an iterator is determined to have a fixed value.
555 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build
*build
)
559 isl_multi_aff_free(build
->schedule_map
);
560 build
->schedule_map
= NULL
;
563 /* Do we need a (non-trivial) schedule map?
564 * That is, is the internal schedule space different from
565 * the external schedule space?
567 * The internal and external schedule spaces are only the same
568 * if code has been generated for the entire schedule and if none
569 * of the loops have been eliminated.
571 isl_bool
isl_ast_build_need_schedule_map(__isl_keep isl_ast_build
*build
)
575 dim
= isl_ast_build_dim(build
, isl_dim_set
);
577 return isl_bool_error
;
578 return isl_bool_ok(build
->depth
!= dim
|| any_eliminated(build
));
581 /* Return a mapping from the internal schedule space to the external
582 * schedule space in the form of an isl_multi_aff.
583 * The internal schedule space originally corresponds to that of the
584 * input schedule. This may change during the code generation if
585 * if isl_ast_build_insert_dim is ever called.
586 * The external schedule space corresponds to the
587 * loops that have been generated.
589 * Currently, the only difference between the internal schedule domain
590 * and the external schedule domain is that some dimensions are projected
591 * out in the external schedule domain. In particular, the dimensions
592 * for which no code has been generated yet and the dimensions that correspond
593 * to eliminated loops.
595 * We cache a copy of the schedule_map in build->schedule_map.
596 * The cache is cleared through isl_ast_build_reset_schedule_map
597 * whenever anything changes that might affect the result of this function.
599 __isl_give isl_multi_aff
*isl_ast_build_get_schedule_map_multi_aff(
600 __isl_keep isl_ast_build
*build
)
608 if (build
->schedule_map
)
609 return isl_multi_aff_copy(build
->schedule_map
);
610 needs_map
= isl_ast_build_need_schedule_map(build
);
614 space
= isl_ast_build_get_space(build
, 1);
615 space
= isl_space_map_from_set(space
);
616 ma
= isl_multi_aff_identity(space
);
619 isl_size dim
= isl_ast_build_dim(build
, isl_dim_set
);
622 ma
= isl_multi_aff_free(ma
);
623 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
,
624 build
->depth
, dim
- build
->depth
);
625 for (i
= build
->depth
- 1; i
>= 0; --i
)
626 if (isl_ast_build_has_affine_value(build
, i
))
627 ma
= isl_multi_aff_drop_dims(ma
,
631 build
->schedule_map
= ma
;
632 return isl_multi_aff_copy(build
->schedule_map
);
635 /* Return a mapping from the internal schedule space to the external
636 * schedule space in the form of an isl_map.
638 __isl_give isl_map
*isl_ast_build_get_schedule_map(
639 __isl_keep isl_ast_build
*build
)
643 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
644 return isl_map_from_multi_aff(ma
);
647 /* Return the position of the dimension in build->domain for which
648 * an AST node is currently being generated.
650 isl_size
isl_ast_build_get_depth(__isl_keep isl_ast_build
*build
)
652 return build
? build
->depth
: isl_size_error
;
655 /* Prepare for generating code for the next level.
656 * In particular, increase the depth and reset any information
657 * that is local to the current depth.
659 __isl_give isl_ast_build
*isl_ast_build_increase_depth(
660 __isl_take isl_ast_build
*build
)
662 build
= isl_ast_build_cow(build
);
666 isl_ast_build_reset_schedule_map(build
);
667 build
->value
= isl_pw_aff_free(build
->value
);
671 void isl_ast_build_dump(__isl_keep isl_ast_build
*build
)
676 fprintf(stderr
, "domain: ");
677 isl_set_dump(build
->domain
);
678 fprintf(stderr
, "generated: ");
679 isl_set_dump(build
->generated
);
680 fprintf(stderr
, "pending: ");
681 isl_set_dump(build
->pending
);
682 fprintf(stderr
, "iterators: ");
683 isl_id_list_dump(build
->iterators
);
684 fprintf(stderr
, "values: ");
685 isl_multi_aff_dump(build
->values
);
687 fprintf(stderr
, "value: ");
688 isl_pw_aff_dump(build
->value
);
690 fprintf(stderr
, "strides: ");
691 isl_vec_dump(build
->strides
);
692 fprintf(stderr
, "offsets: ");
693 isl_multi_aff_dump(build
->offsets
);
694 fprintf(stderr
, "internal2input: ");
695 isl_multi_aff_dump(build
->internal2input
);
698 /* Initialize "build" for AST construction in schedule space "space"
699 * in the case that build->domain is a parameter set.
701 * build->iterators is assumed to have been updated already.
703 static __isl_give isl_ast_build
*isl_ast_build_init(
704 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
708 build
= isl_ast_build_cow(build
);
712 set
= isl_set_universe(isl_space_copy(space
));
713 build
->domain
= isl_set_intersect_params(isl_set_copy(set
),
715 build
->pending
= isl_set_intersect_params(isl_set_copy(set
),
717 build
->generated
= isl_set_intersect_params(set
, build
->generated
);
719 return isl_ast_build_init_derived(build
, space
);
721 isl_ast_build_free(build
);
722 isl_space_free(space
);
726 /* Assign "aff" to *user and return -1, effectively extracting
727 * the first (and presumably only) affine expression in the isl_pw_aff
728 * on which this function is used.
730 static isl_stat
extract_single_piece(__isl_take isl_set
*set
,
731 __isl_take isl_aff
*aff
, void *user
)
738 return isl_stat_error
;
741 /* Intersect "set" with the stride constraint of "build", if any.
743 static __isl_give isl_set
*intersect_stride_constraint(__isl_take isl_set
*set
,
744 __isl_keep isl_ast_build
*build
)
749 return isl_set_free(set
);
750 if (!isl_ast_build_has_stride(build
, build
->depth
))
753 stride
= isl_ast_build_get_stride_constraint(build
);
754 return isl_set_intersect(set
, stride
);
757 /* Check if the given bounds on the current dimension (together with
758 * the stride constraint, if any) imply that
759 * this current dimension attains only a single value (in terms of
760 * parameters and outer dimensions).
761 * If so, we record it in build->value.
762 * If, moreover, this value can be represented as a single affine expression,
763 * then we also update build->values, effectively marking the current
764 * dimension as "eliminated".
766 * When computing the gist of the fixed value that can be represented
767 * as a single affine expression, it is important to only take into
768 * account the domain constraints in the original AST build and
769 * not the domain of the affine expression itself.
770 * Otherwise, a [i/3] is changed into a i/3 because we know that i
771 * is a multiple of 3, but then we end up not expressing anywhere
772 * in the context that i is a multiple of 3.
774 static __isl_give isl_ast_build
*update_values(
775 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*bounds
)
779 isl_pw_multi_aff
*pma
;
784 set
= isl_set_from_basic_set(bounds
);
785 set
= isl_set_intersect(set
, isl_set_copy(build
->domain
));
786 set
= intersect_stride_constraint(set
, build
);
787 it_map
= isl_ast_build_map_to_iterator(build
, set
);
789 sv
= isl_map_is_single_valued(it_map
);
791 build
= isl_ast_build_free(build
);
793 isl_map_free(it_map
);
797 pma
= isl_pw_multi_aff_from_map(it_map
);
798 build
->value
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
799 build
->value
= isl_ast_build_compute_gist_pw_aff(build
, build
->value
);
800 build
->value
= isl_pw_aff_coalesce(build
->value
);
801 isl_pw_multi_aff_free(pma
);
803 n
= isl_pw_aff_n_piece(build
->value
);
805 return isl_ast_build_free(build
);
809 isl_pw_aff_foreach_piece(build
->value
, &extract_single_piece
, &aff
);
811 build
->values
= isl_multi_aff_set_aff(build
->values
, build
->depth
, aff
);
813 return isl_ast_build_free(build
);
814 isl_ast_build_reset_schedule_map(build
);
818 /* Update the AST build based on the given loop bounds for
819 * the current dimension and the stride information available in the build.
821 * We first make sure that the bounds do not refer to any iterators
822 * that have already been eliminated.
823 * Then, we check if the bounds imply that the current iterator
825 * If they do and if this fixed value can be expressed as a single
826 * affine expression, we eliminate the iterators from the bounds.
827 * Note that we cannot simply plug in this single value using
828 * isl_basic_set_preimage_multi_aff as the single value may only
829 * be defined on a subset of the domain. Plugging in the value
830 * would restrict the build domain to this subset, while this
831 * restriction may not be reflected in the generated code.
832 * Finally, we intersect build->domain with the updated bounds.
833 * We also add the stride constraint unless we have been able
834 * to find a fixed value expressed as a single affine expression.
836 * Note that the check for a fixed value in update_values requires
837 * us to intersect the bounds with the current build domain.
838 * When we intersect build->domain with the updated bounds in
839 * the final step, we make sure that these updated bounds have
840 * not been intersected with the old build->domain.
841 * Otherwise, we would indirectly intersect the build domain with itself,
842 * which can lead to inefficiencies, in particular if the build domain
843 * contains any unknown divs.
845 * The pending and generated sets are not updated by this function to
846 * match the updated domain.
847 * The caller still needs to call isl_ast_build_set_pending_generated.
849 __isl_give isl_ast_build
*isl_ast_build_set_loop_bounds(
850 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*bounds
)
854 build
= isl_ast_build_cow(build
);
858 build
= update_values(build
, isl_basic_set_copy(bounds
));
861 set
= isl_set_from_basic_set(isl_basic_set_copy(bounds
));
862 if (isl_ast_build_has_affine_value(build
, build
->depth
)) {
863 set
= isl_set_eliminate(set
, isl_dim_set
, build
->depth
, 1);
864 set
= isl_set_compute_divs(set
);
865 build
->pending
= isl_set_intersect(build
->pending
,
867 build
->domain
= isl_set_intersect(build
->domain
, set
);
869 build
->domain
= isl_set_intersect(build
->domain
, set
);
870 build
= isl_ast_build_include_stride(build
);
874 isl_basic_set_free(bounds
);
876 if (!build
->domain
|| !build
->pending
|| !build
->generated
)
877 return isl_ast_build_free(build
);
881 isl_ast_build_free(build
);
882 isl_basic_set_free(bounds
);
886 /* Update the pending and generated sets of "build" according to "bounds".
887 * If the build has an affine value at the current depth,
888 * then isl_ast_build_set_loop_bounds has already set the pending set.
889 * Otherwise, do it here.
891 __isl_give isl_ast_build
*isl_ast_build_set_pending_generated(
892 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*bounds
)
894 isl_basic_set
*generated
, *pending
;
899 if (isl_ast_build_has_affine_value(build
, build
->depth
)) {
900 isl_basic_set_free(bounds
);
904 build
= isl_ast_build_cow(build
);
908 pending
= isl_basic_set_copy(bounds
);
909 pending
= isl_basic_set_drop_constraints_involving_dims(pending
,
910 isl_dim_set
, build
->depth
, 1);
911 build
->pending
= isl_set_intersect(build
->pending
,
912 isl_set_from_basic_set(pending
));
914 generated
= isl_basic_set_drop_constraints_not_involving_dims(
915 generated
, isl_dim_set
, build
->depth
, 1);
916 build
->generated
= isl_set_intersect(build
->generated
,
917 isl_set_from_basic_set(generated
));
919 if (!build
->pending
|| !build
->generated
)
920 return isl_ast_build_free(build
);
924 isl_ast_build_free(build
);
925 isl_basic_set_free(bounds
);
929 /* Intersect build->domain with "set", where "set" is specified
930 * in terms of the internal schedule domain.
932 static __isl_give isl_ast_build
*isl_ast_build_restrict_internal(
933 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
935 build
= isl_ast_build_cow(build
);
939 set
= isl_set_compute_divs(set
);
940 build
->domain
= isl_set_intersect(build
->domain
, set
);
941 build
->domain
= isl_set_coalesce(build
->domain
);
944 return isl_ast_build_free(build
);
948 isl_ast_build_free(build
);
953 /* Intersect build->generated and build->domain with "set",
954 * where "set" is specified in terms of the internal schedule domain.
956 __isl_give isl_ast_build
*isl_ast_build_restrict_generated(
957 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
959 set
= isl_set_compute_divs(set
);
960 build
= isl_ast_build_restrict_internal(build
, isl_set_copy(set
));
961 build
= isl_ast_build_cow(build
);
965 build
->generated
= isl_set_intersect(build
->generated
, set
);
966 build
->generated
= isl_set_coalesce(build
->generated
);
968 if (!build
->generated
)
969 return isl_ast_build_free(build
);
973 isl_ast_build_free(build
);
978 /* Replace the set of pending constraints by "guard", which is then
979 * no longer considered as pending.
980 * That is, add "guard" to the generated constraints and clear all pending
981 * constraints, making the domain equal to the generated constraints.
983 __isl_give isl_ast_build
*isl_ast_build_replace_pending_by_guard(
984 __isl_take isl_ast_build
*build
, __isl_take isl_set
*guard
)
986 build
= isl_ast_build_restrict_generated(build
, guard
);
987 build
= isl_ast_build_cow(build
);
991 isl_set_free(build
->domain
);
992 build
->domain
= isl_set_copy(build
->generated
);
993 isl_set_free(build
->pending
);
994 build
->pending
= isl_set_universe(isl_set_get_space(build
->domain
));
997 return isl_ast_build_free(build
);
1002 /* Intersect build->domain with "set", where "set" is specified
1003 * in terms of the external schedule domain.
1005 __isl_give isl_ast_build
*isl_ast_build_restrict(
1006 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
1010 if (isl_set_is_params(set
))
1011 return isl_ast_build_restrict_generated(build
, set
);
1013 needs_map
= isl_ast_build_need_schedule_map(build
);
1018 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
1019 set
= isl_set_preimage_multi_aff(set
, ma
);
1021 return isl_ast_build_restrict_generated(build
, set
);
1023 isl_ast_build_free(build
);
1028 /* Replace build->executed by "executed".
1030 __isl_give isl_ast_build
*isl_ast_build_set_executed(
1031 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*executed
)
1033 build
= isl_ast_build_cow(build
);
1037 isl_union_map_free(build
->executed
);
1038 build
->executed
= executed
;
1042 isl_ast_build_free(build
);
1043 isl_union_map_free(executed
);
1047 /* Does "build" point to a band node?
1048 * That is, are we currently handling a band node inside a schedule tree?
1050 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build
*build
)
1054 return build
->node
!= NULL
;
1057 /* Return a copy of the band node that "build" refers to.
1059 __isl_give isl_schedule_node
*isl_ast_build_get_schedule_node(
1060 __isl_keep isl_ast_build
*build
)
1064 return isl_schedule_node_copy(build
->node
);
1067 /* Extract the loop AST generation types for the members of build->node
1068 * and store them in build->loop_type.
1070 static __isl_give isl_ast_build
*extract_loop_types(
1071 __isl_take isl_ast_build
*build
)
1076 isl_schedule_node
*node
;
1080 n
= isl_schedule_node_band_n_member(build
->node
);
1082 return isl_ast_build_free(build
);
1083 ctx
= isl_ast_build_get_ctx(build
);
1085 isl_die(ctx
, isl_error_internal
, "missing AST node",
1086 return isl_ast_build_free(build
));
1088 free(build
->loop_type
);
1090 build
->loop_type
= isl_alloc_array(ctx
,
1091 enum isl_ast_loop_type
, build
->n
);
1092 if (build
->n
&& !build
->loop_type
)
1093 return isl_ast_build_free(build
);
1095 for (i
= 0; i
< build
->n
; ++i
)
1096 build
->loop_type
[i
] =
1097 isl_schedule_node_band_member_get_ast_loop_type(node
, i
);
1102 /* Replace the band node that "build" refers to by "node" and
1103 * extract the corresponding loop AST generation types.
1105 __isl_give isl_ast_build
*isl_ast_build_set_schedule_node(
1106 __isl_take isl_ast_build
*build
,
1107 __isl_take isl_schedule_node
*node
)
1109 build
= isl_ast_build_cow(build
);
1110 if (!build
|| !node
)
1113 isl_schedule_node_free(build
->node
);
1116 build
= extract_loop_types(build
);
1120 isl_ast_build_free(build
);
1121 isl_schedule_node_free(node
);
1125 /* Remove any reference to a band node from "build".
1127 __isl_give isl_ast_build
*isl_ast_build_reset_schedule_node(
1128 __isl_take isl_ast_build
*build
)
1130 build
= isl_ast_build_cow(build
);
1134 isl_schedule_node_free(build
->node
);
1140 /* Return a copy of the current schedule domain.
1142 __isl_give isl_set
*isl_ast_build_get_domain(__isl_keep isl_ast_build
*build
)
1144 return build
? isl_set_copy(build
->domain
) : NULL
;
1147 /* Return a copy of the set of pending constraints.
1149 __isl_give isl_set
*isl_ast_build_get_pending(
1150 __isl_keep isl_ast_build
*build
)
1152 return build
? isl_set_copy(build
->pending
) : NULL
;
1155 /* Return a copy of the set of generated constraints.
1157 __isl_give isl_set
*isl_ast_build_get_generated(
1158 __isl_keep isl_ast_build
*build
)
1160 return build
? isl_set_copy(build
->generated
) : NULL
;
1163 /* Return a copy of the map from the internal schedule domain
1164 * to the original input schedule domain.
1166 __isl_give isl_multi_aff
*isl_ast_build_get_internal2input(
1167 __isl_keep isl_ast_build
*build
)
1169 return build
? isl_multi_aff_copy(build
->internal2input
) : NULL
;
1172 /* Return the number of variables of the given type
1173 * in the (internal) schedule space.
1175 isl_size
isl_ast_build_dim(__isl_keep isl_ast_build
*build
,
1176 enum isl_dim_type type
)
1179 return isl_size_error
;
1180 return isl_set_dim(build
->domain
, type
);
1183 /* Return the (schedule) space of "build".
1185 * If "internal" is set, then this space is the space of the internal
1186 * representation of the entire schedule, including those parts for
1187 * which no code has been generated yet.
1189 * If "internal" is not set, then this space is the external representation
1190 * of the loops generated so far.
1192 __isl_give isl_space
*isl_ast_build_get_space(__isl_keep isl_ast_build
*build
,
1203 space
= isl_set_get_space(build
->domain
);
1207 needs_map
= isl_ast_build_need_schedule_map(build
);
1209 return isl_space_free(space
);
1213 dim
= isl_ast_build_dim(build
, isl_dim_set
);
1215 return isl_space_free(space
);
1216 space
= isl_space_drop_dims(space
, isl_dim_set
,
1217 build
->depth
, dim
- build
->depth
);
1218 for (i
= build
->depth
- 1; i
>= 0; --i
) {
1219 isl_bool affine
= isl_ast_build_has_affine_value(build
, i
);
1222 return isl_space_free(space
);
1224 space
= isl_space_drop_dims(space
, isl_dim_set
, i
, 1);
1230 /* Return the external representation of the schedule space of "build",
1231 * i.e., a space with a dimension for each loop generated so far,
1232 * with the names of the dimensions set to the loop iterators.
1234 __isl_give isl_space
*isl_ast_build_get_schedule_space(
1235 __isl_keep isl_ast_build
*build
)
1243 space
= isl_ast_build_get_space(build
, 0);
1246 for (i
= 0; i
< build
->depth
; ++i
) {
1249 if (isl_ast_build_has_affine_value(build
, i
)) {
1254 id
= isl_ast_build_get_iterator_id(build
, i
);
1255 space
= isl_space_set_dim_id(space
, isl_dim_set
, i
- skip
, id
);
1261 /* Return the current schedule, as stored in build->executed, in terms
1262 * of the external schedule domain.
1264 __isl_give isl_union_map
*isl_ast_build_get_schedule(
1265 __isl_keep isl_ast_build
*build
)
1268 isl_union_map
*executed
;
1269 isl_union_map
*schedule
;
1271 needs_map
= isl_ast_build_need_schedule_map(build
);
1275 executed
= isl_union_map_copy(build
->executed
);
1277 isl_map
*proj
= isl_ast_build_get_schedule_map(build
);
1278 executed
= isl_union_map_apply_domain(executed
,
1279 isl_union_map_from_map(proj
));
1281 schedule
= isl_union_map_reverse(executed
);
1286 /* Return the iterator attached to the internal schedule dimension "pos".
1288 __isl_give isl_id
*isl_ast_build_get_iterator_id(
1289 __isl_keep isl_ast_build
*build
, int pos
)
1294 return isl_id_list_get_id(build
->iterators
, pos
);
1297 /* Set the stride and offset of the current dimension to the given
1298 * value and expression.
1300 static __isl_give isl_ast_build
*set_stride(__isl_take isl_ast_build
*build
,
1301 __isl_take isl_val
*stride
, __isl_take isl_aff
*offset
)
1305 build
= isl_ast_build_cow(build
);
1306 if (!build
|| !stride
|| !offset
)
1311 build
->strides
= isl_vec_set_element_val(build
->strides
, pos
, stride
);
1312 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, pos
, offset
);
1313 if (!build
->strides
|| !build
->offsets
)
1314 return isl_ast_build_free(build
);
1318 isl_val_free(stride
);
1319 isl_aff_free(offset
);
1320 return isl_ast_build_free(build
);
1323 /* Return a set expressing the stride constraint at the current depth.
1325 * In particular, if the current iterator (i) is known to attain values
1329 * where f is the offset and s is the stride, then the returned set
1330 * expresses the constraint
1334 __isl_give isl_set
*isl_ast_build_get_stride_constraint(
1335 __isl_keep isl_ast_build
*build
)
1347 if (!isl_ast_build_has_stride(build
, pos
))
1348 return isl_set_universe(isl_ast_build_get_space(build
, 1));
1350 stride
= isl_ast_build_get_stride(build
, pos
);
1351 aff
= isl_ast_build_get_offset(build
, pos
);
1352 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, -1);
1353 aff
= isl_aff_mod_val(aff
, stride
);
1354 set
= isl_set_from_basic_set(isl_aff_zero_basic_set(aff
));
1359 /* Return the expansion implied by the stride and offset at the current
1362 * That is, return the mapping
1364 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1365 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1367 * where s is the stride at the current depth d and offset(i) is
1368 * the corresponding offset.
1370 __isl_give isl_multi_aff
*isl_ast_build_get_stride_expansion(
1371 __isl_keep isl_ast_build
*build
)
1376 isl_aff
*aff
, *offset
;
1379 pos
= isl_ast_build_get_depth(build
);
1383 space
= isl_ast_build_get_space(build
, 1);
1384 space
= isl_space_map_from_set(space
);
1385 ma
= isl_multi_aff_identity(space
);
1387 if (!isl_ast_build_has_stride(build
, pos
))
1390 offset
= isl_ast_build_get_offset(build
, pos
);
1391 stride
= isl_ast_build_get_stride(build
, pos
);
1392 aff
= isl_multi_aff_get_aff(ma
, pos
);
1393 aff
= isl_aff_scale_val(aff
, stride
);
1394 aff
= isl_aff_add(aff
, offset
);
1395 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1400 /* Add constraints corresponding to any previously detected
1401 * stride on the current dimension to build->domain.
1403 __isl_give isl_ast_build
*isl_ast_build_include_stride(
1404 __isl_take isl_ast_build
*build
)
1410 if (!isl_ast_build_has_stride(build
, build
->depth
))
1412 build
= isl_ast_build_cow(build
);
1416 set
= isl_ast_build_get_stride_constraint(build
);
1418 build
->domain
= isl_set_intersect(build
->domain
, isl_set_copy(set
));
1419 build
->generated
= isl_set_intersect(build
->generated
, set
);
1420 if (!build
->domain
|| !build
->generated
)
1421 return isl_ast_build_free(build
);
1426 /* Check if the constraints in "set" imply any stride on the current
1427 * dimension and, if so, record the stride information in "build"
1428 * and return the updated "build".
1430 * We assume that inner dimensions have been eliminated from "set"
1431 * by the caller. This is needed because the common stride
1432 * may be imposed by different inner dimensions on different parts of
1434 * The assumption ensures that the lower bound does not depend
1435 * on inner dimensions.
1437 __isl_give isl_ast_build
*isl_ast_build_detect_strides(
1438 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
1444 isl_stride_info
*si
;
1446 pos
= isl_ast_build_get_depth(build
);
1450 si
= isl_set_get_stride_info(set
, pos
);
1451 stride
= isl_stride_info_get_stride(si
);
1452 offset
= isl_stride_info_get_offset(si
);
1453 isl_stride_info_free(si
);
1456 no_stride
= isl_val_is_one(stride
);
1457 if (no_stride
>= 0 && !no_stride
)
1458 return set_stride(build
, stride
, offset
);
1459 isl_val_free(stride
);
1460 isl_aff_free(offset
);
1462 return isl_ast_build_free(build
);
1469 /* Does "map" not involve the input dimension data->depth?
1471 static isl_bool
free_of_depth(__isl_keep isl_map
*map
, void *user
)
1475 return isl_bool_not(isl_map_involves_dims(map
, isl_dim_in
, *depth
, 1));
1478 /* Do any options depend on the value of the dimension at the current depth?
1480 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build
*build
)
1487 free
= isl_union_map_every_map(build
->options
, &free_of_depth
,
1489 return isl_bool_not(free
);
1492 /* Construct the map
1494 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1496 * with "space" the parameter space of the constructed map.
1498 static __isl_give isl_map
*construct_insertion_map(__isl_take isl_space
*space
,
1502 isl_basic_map
*bmap1
, *bmap2
;
1504 space
= isl_space_set_from_params(space
);
1505 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1506 space
= isl_space_map_from_set(space
);
1507 c
= isl_constraint_alloc_equality(isl_local_space_from_space(space
));
1508 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, 0, 1);
1509 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, -1);
1510 bmap1
= isl_basic_map_from_constraint(isl_constraint_copy(c
));
1511 c
= isl_constraint_set_constant_si(c
, 1);
1512 bmap2
= isl_basic_map_from_constraint(c
);
1514 bmap1
= isl_basic_map_upper_bound_si(bmap1
, isl_dim_in
, 0, pos
- 1);
1515 bmap2
= isl_basic_map_lower_bound_si(bmap2
, isl_dim_in
, 0, pos
);
1517 return isl_basic_map_union(bmap1
, bmap2
);
1520 static const char *option_str
[] = {
1521 [isl_ast_loop_atomic
] = "atomic",
1522 [isl_ast_loop_unroll
] = "unroll",
1523 [isl_ast_loop_separate
] = "separate"
1526 /* Update the "options" to reflect the insertion of a dimension
1527 * at position "pos" in the schedule domain space.
1528 * "space" is the original domain space before the insertion and
1529 * may be named and/or structured.
1531 * The (relevant) input options all have "space" as domain, which
1532 * has to be mapped to the extended space.
1533 * The values of the ranges also refer to the schedule domain positions
1534 * and they therefore also need to be adjusted. In particular, values
1535 * smaller than pos do not need to change, while values greater than or
1536 * equal to pos need to be incremented.
1537 * That is, we need to apply the following map.
1539 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1540 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1541 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1542 * separation_class[[i] -> [c]]
1543 * -> separation_class[[i] -> [c]] : i < pos;
1544 * separation_class[[i] -> [c]]
1545 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1547 static __isl_give isl_union_map
*options_insert_dim(
1548 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
, int pos
)
1551 isl_union_map
*insertion
;
1552 enum isl_ast_loop_type type
;
1553 const char *name
= "separation_class";
1555 space
= isl_space_map_from_set(space
);
1556 map
= isl_map_identity(space
);
1557 map
= isl_map_insert_dims(map
, isl_dim_out
, pos
, 1);
1558 options
= isl_union_map_apply_domain(options
,
1559 isl_union_map_from_map(map
));
1564 map
= construct_insertion_map(isl_union_map_get_space(options
), pos
);
1566 insertion
= isl_union_map_empty(isl_union_map_get_space(options
));
1568 for (type
= isl_ast_loop_atomic
;
1569 type
<= isl_ast_loop_separate
; ++type
) {
1570 isl_map
*map_type
= isl_map_copy(map
);
1571 const char *name
= option_str
[type
];
1572 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_in
, name
);
1573 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_out
, name
);
1574 insertion
= isl_union_map_add_map(insertion
, map_type
);
1577 map
= isl_map_product(map
, isl_map_identity(isl_map_get_space(map
)));
1578 map
= isl_map_set_tuple_name(map
, isl_dim_in
, name
);
1579 map
= isl_map_set_tuple_name(map
, isl_dim_out
, name
);
1580 insertion
= isl_union_map_add_map(insertion
, map
);
1582 options
= isl_union_map_apply_range(options
, insertion
);
1587 /* If we are generating an AST from a schedule tree (build->node is set),
1588 * then update the loop AST generation types
1589 * to reflect the insertion of a dimension at (global) position "pos"
1590 * in the schedule domain space.
1591 * We do not need to adjust any isolate option since we would not be inserting
1592 * any dimensions if there were any isolate option.
1594 static __isl_give isl_ast_build
*node_insert_dim(
1595 __isl_take isl_ast_build
*build
, int pos
)
1599 enum isl_ast_loop_type
*loop_type
;
1602 build
= isl_ast_build_cow(build
);
1608 ctx
= isl_ast_build_get_ctx(build
);
1609 local_pos
= pos
- build
->outer_pos
;
1610 loop_type
= isl_realloc_array(ctx
, build
->loop_type
,
1611 enum isl_ast_loop_type
, build
->n
+ 1);
1613 return isl_ast_build_free(build
);
1614 build
->loop_type
= loop_type
;
1615 for (i
= build
->n
- 1; i
>= local_pos
; --i
)
1616 loop_type
[i
+ 1] = loop_type
[i
];
1617 loop_type
[local_pos
] = isl_ast_loop_default
;
1623 /* Insert a single dimension in the schedule domain at position "pos".
1624 * The new dimension is given an isl_id with the empty string as name.
1626 * The main difficulty is updating build->options to reflect the
1627 * extra dimension. This is handled in options_insert_dim.
1629 * Note that because of the dimension manipulations, the resulting
1630 * schedule domain space will always be unnamed and unstructured.
1631 * However, the original schedule domain space may be named and/or
1632 * structured, so we have to take this possibility into account
1633 * while performing the transformations.
1635 * Since the inserted schedule dimension is used by the caller
1636 * to differentiate between different domain spaces, there is
1637 * no longer a uniform mapping from the internal schedule space
1638 * to the input schedule space. The internal2input mapping is
1639 * therefore removed.
1641 __isl_give isl_ast_build
*isl_ast_build_insert_dim(
1642 __isl_take isl_ast_build
*build
, int pos
)
1645 isl_space
*space
, *ma_space
;
1649 build
= isl_ast_build_cow(build
);
1653 ctx
= isl_ast_build_get_ctx(build
);
1654 id
= isl_id_alloc(ctx
, "", NULL
);
1656 space
= isl_ast_build_get_space(build
, 1);
1657 build
->iterators
= isl_id_list_insert(build
->iterators
, pos
, id
);
1658 build
->domain
= isl_set_insert_dims(build
->domain
,
1659 isl_dim_set
, pos
, 1);
1660 build
->generated
= isl_set_insert_dims(build
->generated
,
1661 isl_dim_set
, pos
, 1);
1662 build
->pending
= isl_set_insert_dims(build
->pending
,
1663 isl_dim_set
, pos
, 1);
1664 build
->strides
= isl_vec_insert_els(build
->strides
, pos
, 1);
1665 build
->strides
= isl_vec_set_element_si(build
->strides
, pos
, 1);
1666 ma_space
= isl_space_params(isl_multi_aff_get_space(build
->offsets
));
1667 ma_space
= isl_space_set_from_params(ma_space
);
1668 ma_space
= isl_space_add_dims(ma_space
, isl_dim_set
, 1);
1669 ma_space
= isl_space_map_from_set(ma_space
);
1670 ma
= isl_multi_aff_zero(isl_space_copy(ma_space
));
1671 build
->offsets
= isl_multi_aff_splice(build
->offsets
, pos
, pos
, ma
);
1672 ma
= isl_multi_aff_identity(ma_space
);
1673 build
->values
= isl_multi_aff_splice(build
->values
, pos
, pos
, ma
);
1675 build
->options
= options_insert_dim(build
->options
, space
, pos
);
1676 build
->internal2input
= isl_multi_aff_free(build
->internal2input
);
1678 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1679 !build
->pending
|| !build
->values
||
1680 !build
->strides
|| !build
->offsets
|| !build
->options
)
1681 return isl_ast_build_free(build
);
1683 build
= node_insert_dim(build
, pos
);
1688 /* Scale down the current dimension by a factor of "m".
1689 * "umap" is an isl_union_map that implements the scaling down.
1690 * That is, it is of the form
1692 * { [.... i ....] -> [.... i' ....] : i = m i' }
1694 * This function is called right after the strides have been
1695 * detected, but before any constraints on the current dimension
1696 * have been included in build->domain.
1697 * We therefore only need to update stride, offset, the options and
1698 * the mapping from internal schedule space to the original schedule
1699 * space, if we are still keeping track of such a mapping.
1700 * The latter mapping is updated by plugging in
1701 * { [... i ...] -> [... m i ... ] }.
1703 __isl_give isl_ast_build
*isl_ast_build_scale_down(
1704 __isl_take isl_ast_build
*build
, __isl_take isl_val
*m
,
1705 __isl_take isl_union_map
*umap
)
1711 build
= isl_ast_build_cow(build
);
1712 if (!build
|| !umap
|| !m
)
1715 depth
= build
->depth
;
1717 if (build
->internal2input
) {
1722 space
= isl_multi_aff_get_space(build
->internal2input
);
1723 space
= isl_space_map_from_set(isl_space_domain(space
));
1724 ma
= isl_multi_aff_identity(space
);
1725 aff
= isl_multi_aff_get_aff(ma
, depth
);
1726 aff
= isl_aff_scale_val(aff
, isl_val_copy(m
));
1727 ma
= isl_multi_aff_set_aff(ma
, depth
, aff
);
1728 build
->internal2input
=
1729 isl_multi_aff_pullback_multi_aff(build
->internal2input
, ma
);
1730 if (!build
->internal2input
)
1734 v
= isl_vec_get_element_val(build
->strides
, depth
);
1735 v
= isl_val_div(v
, isl_val_copy(m
));
1736 build
->strides
= isl_vec_set_element_val(build
->strides
, depth
, v
);
1738 aff
= isl_multi_aff_get_aff(build
->offsets
, depth
);
1739 aff
= isl_aff_scale_down_val(aff
, m
);
1740 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, depth
, aff
);
1741 build
->options
= isl_union_map_apply_domain(build
->options
, umap
);
1742 if (!build
->strides
|| !build
->offsets
|| !build
->options
)
1743 return isl_ast_build_free(build
);
1748 isl_union_map_free(umap
);
1749 return isl_ast_build_free(build
);
1752 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1753 * If an isl_id with such a name already appears among the parameters
1754 * in build->domain, then adjust the name to "c%d_%d".
1756 static __isl_give isl_id_list
*generate_names(isl_ctx
*ctx
, int n
, int first
,
1757 __isl_keep isl_ast_build
*build
)
1762 names
= isl_id_list_alloc(ctx
, n
);
1763 for (i
= 0; i
< n
; ++i
) {
1766 id
= generate_name(ctx
, first
+ i
, build
);
1767 names
= isl_id_list_add(names
, id
);
1773 /* Embed "options" into the given isl_ast_build space.
1775 * This function is called from within a nested call to
1776 * isl_ast_build_node_from_schedule_map.
1777 * "options" refers to the additional schedule,
1778 * while space refers to both the space of the outer isl_ast_build and
1779 * that of the additional schedule.
1780 * Specifically, space is of the form
1784 * while options lives in the space(s)
1792 * and compose this with options, to obtain the new options
1793 * living in the space(s)
1797 static __isl_give isl_union_map
*embed_options(
1798 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
)
1802 map
= isl_map_universe(isl_space_unwrap(space
));
1803 map
= isl_map_range_map(map
);
1805 options
= isl_union_map_apply_range(
1806 isl_union_map_from_map(map
), options
);
1811 /* Update "build" for use in a (possibly nested) code generation. That is,
1812 * extend "build" from an AST build on some domain O to an AST build
1813 * on domain [O -> S], with S corresponding to "space".
1814 * If the original domain is a parameter domain, then the new domain is
1816 * "iterators" is a list of iterators for S, but the number of elements
1817 * may be smaller or greater than the number of set dimensions of S.
1818 * If "keep_iterators" is set, then any extra ids in build->iterators
1819 * are reused for S. Otherwise, these extra ids are dropped.
1821 * We first update build->outer_pos to the current depth.
1822 * This depth is zero in case this is the outermost code generation.
1824 * We then add additional ids such that the number of iterators is at least
1825 * equal to the dimension of the new build domain.
1827 * If the original domain is parametric, then we are constructing
1828 * an isl_ast_build for the outer code generation and we pass control
1829 * to isl_ast_build_init.
1831 * Otherwise, we adjust the fields of "build" to include "space".
1833 __isl_give isl_ast_build
*isl_ast_build_product(
1834 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
1839 isl_multi_aff
*embedding
;
1840 isl_size dim
, space_dim
, n_it
;
1842 build
= isl_ast_build_cow(build
);
1846 build
->outer_pos
= build
->depth
;
1848 ctx
= isl_ast_build_get_ctx(build
);
1849 dim
= isl_ast_build_dim(build
, isl_dim_set
);
1850 space_dim
= isl_space_dim(space
, isl_dim_set
);
1851 n_it
= isl_id_list_n_id(build
->iterators
);
1852 if (dim
< 0 || space_dim
< 0 || n_it
< 0)
1857 l
= generate_names(ctx
, dim
- n_it
, n_it
, build
);
1858 build
->iterators
= isl_id_list_concat(build
->iterators
, l
);
1861 if (isl_set_is_params(build
->domain
))
1862 return isl_ast_build_init(build
, space
);
1864 set
= isl_set_universe(isl_space_copy(space
));
1865 build
->domain
= isl_set_product(build
->domain
, isl_set_copy(set
));
1866 build
->pending
= isl_set_product(build
->pending
, isl_set_copy(set
));
1867 build
->generated
= isl_set_product(build
->generated
, set
);
1869 strides
= isl_vec_alloc(ctx
, space_dim
);
1870 strides
= isl_vec_set_si(strides
, 1);
1871 build
->strides
= isl_vec_concat(build
->strides
, strides
);
1873 space
= isl_space_map_from_set(space
);
1874 build
->offsets
= isl_multi_aff_align_params(build
->offsets
,
1875 isl_space_copy(space
));
1876 build
->offsets
= isl_multi_aff_product(build
->offsets
,
1877 isl_multi_aff_zero(isl_space_copy(space
)));
1878 build
->values
= isl_multi_aff_align_params(build
->values
,
1879 isl_space_copy(space
));
1880 embedding
= isl_multi_aff_identity(space
);
1881 build
->values
= isl_multi_aff_product(build
->values
,
1882 isl_multi_aff_copy(embedding
));
1883 if (build
->internal2input
) {
1884 build
->internal2input
=
1885 isl_multi_aff_product(build
->internal2input
, embedding
);
1886 build
->internal2input
=
1887 isl_multi_aff_flatten_range(build
->internal2input
);
1888 if (!build
->internal2input
)
1889 return isl_ast_build_free(build
);
1891 isl_multi_aff_free(embedding
);
1894 space
= isl_ast_build_get_space(build
, 1);
1895 build
->options
= embed_options(build
->options
, space
);
1897 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1898 !build
->pending
|| !build
->values
||
1899 !build
->strides
|| !build
->offsets
|| !build
->options
)
1900 return isl_ast_build_free(build
);
1904 isl_ast_build_free(build
);
1905 isl_space_free(space
);
1909 /* Does "aff" only attain non-negative values over build->domain?
1910 * That is, does it not attain any negative values?
1912 isl_bool
isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build
*build
,
1913 __isl_keep isl_aff
*aff
)
1919 return isl_bool_error
;
1921 aff
= isl_aff_copy(aff
);
1922 test
= isl_set_from_basic_set(isl_aff_neg_basic_set(aff
));
1923 test
= isl_set_intersect(test
, isl_set_copy(build
->domain
));
1924 empty
= isl_set_is_empty(test
);
1930 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1932 isl_bool
isl_ast_build_has_stride(__isl_keep isl_ast_build
*build
, int pos
)
1935 isl_bool has_stride
;
1938 return isl_bool_error
;
1940 v
= isl_vec_get_element_val(build
->strides
, pos
);
1941 has_stride
= isl_bool_not(isl_val_is_one(v
));
1947 /* Given that the dimension at position "pos" takes on values
1951 * with a an integer, return s.
1953 __isl_give isl_val
*isl_ast_build_get_stride(__isl_keep isl_ast_build
*build
,
1959 return isl_vec_get_element_val(build
->strides
, pos
);
1962 /* Given that the dimension at position "pos" takes on values
1966 * with a an integer, return f.
1968 __isl_give isl_aff
*isl_ast_build_get_offset(
1969 __isl_keep isl_ast_build
*build
, int pos
)
1974 return isl_multi_aff_get_aff(build
->offsets
, pos
);
1977 /* Is the dimension at position "pos" known to attain only a single
1978 * value that, moreover, can be described by a single affine expression
1979 * in terms of the outer dimensions and parameters?
1981 * If not, then the corresponding affine expression in build->values
1982 * is set to be equal to the same input dimension.
1983 * Otherwise, it is set to the requested expression in terms of
1984 * outer dimensions and parameters.
1986 isl_bool
isl_ast_build_has_affine_value(__isl_keep isl_ast_build
*build
,
1993 return isl_bool_error
;
1995 aff
= isl_multi_aff_get_aff(build
->values
, pos
);
1996 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, pos
, 1);
1999 return isl_bool_not(involves
);
2002 /* Plug in the known values (fixed affine expressions in terms of
2003 * parameters and outer loop iterators) of all loop iterators
2004 * in the domain of "umap".
2006 * We simply precompose "umap" with build->values.
2008 __isl_give isl_union_map
*isl_ast_build_substitute_values_union_map_domain(
2009 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*umap
)
2011 isl_multi_aff
*values
;
2014 return isl_union_map_free(umap
);
2016 values
= isl_multi_aff_copy(build
->values
);
2017 umap
= isl_union_map_preimage_domain_multi_aff(umap
, values
);
2022 /* Is the current dimension known to attain only a single value?
2024 int isl_ast_build_has_value(__isl_keep isl_ast_build
*build
)
2029 return build
->value
!= NULL
;
2032 /* Simplify the basic set "bset" based on what we know about
2033 * the iterators of already generated loops.
2035 * "bset" is assumed to live in the (internal) schedule domain.
2037 __isl_give isl_basic_set
*isl_ast_build_compute_gist_basic_set(
2038 __isl_keep isl_ast_build
*build
, __isl_take isl_basic_set
*bset
)
2043 bset
= isl_basic_set_preimage_multi_aff(bset
,
2044 isl_multi_aff_copy(build
->values
));
2045 bset
= isl_basic_set_gist(bset
,
2046 isl_set_simple_hull(isl_set_copy(build
->domain
)));
2050 isl_basic_set_free(bset
);
2054 /* Simplify the set "set" based on what we know about
2055 * the iterators of already generated loops.
2057 * "set" is assumed to live in the (internal) schedule domain.
2059 __isl_give isl_set
*isl_ast_build_compute_gist(
2060 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2065 if (!isl_set_is_params(set
))
2066 set
= isl_set_preimage_multi_aff(set
,
2067 isl_multi_aff_copy(build
->values
));
2068 set
= isl_set_gist(set
, isl_set_copy(build
->domain
));
2076 /* Include information about what we know about the iterators of
2077 * already generated loops to "set".
2079 * We currently only plug in the known affine values of outer loop
2081 * In principle we could also introduce equalities or even other
2082 * constraints implied by the intersection of "set" and build->domain.
2084 __isl_give isl_set
*isl_ast_build_specialize(__isl_keep isl_ast_build
*build
,
2085 __isl_take isl_set
*set
)
2088 return isl_set_free(set
);
2090 return isl_set_preimage_multi_aff(set
,
2091 isl_multi_aff_copy(build
->values
));
2094 /* Plug in the known affine values of outer loop iterators in "bset".
2096 __isl_give isl_basic_set
*isl_ast_build_specialize_basic_set(
2097 __isl_keep isl_ast_build
*build
, __isl_take isl_basic_set
*bset
)
2100 return isl_basic_set_free(bset
);
2102 return isl_basic_set_preimage_multi_aff(bset
,
2103 isl_multi_aff_copy(build
->values
));
2106 /* Simplify the map "map" based on what we know about
2107 * the iterators of already generated loops.
2109 * The domain of "map" is assumed to live in the (internal) schedule domain.
2111 __isl_give isl_map
*isl_ast_build_compute_gist_map_domain(
2112 __isl_keep isl_ast_build
*build
, __isl_take isl_map
*map
)
2117 map
= isl_map_gist_domain(map
, isl_set_copy(build
->domain
));
2125 /* Simplify the affine expression "aff" based on what we know about
2126 * the iterators of already generated loops.
2128 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2130 __isl_give isl_aff
*isl_ast_build_compute_gist_aff(
2131 __isl_keep isl_ast_build
*build
, __isl_take isl_aff
*aff
)
2136 aff
= isl_aff_gist(aff
, isl_set_copy(build
->domain
));
2144 /* Simplify the piecewise affine expression "aff" based on what we know about
2145 * the iterators of already generated loops.
2147 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2149 __isl_give isl_pw_aff
*isl_ast_build_compute_gist_pw_aff(
2150 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_aff
*pa
)
2155 if (!isl_set_is_params(build
->domain
))
2156 pa
= isl_pw_aff_pullback_multi_aff(pa
,
2157 isl_multi_aff_copy(build
->values
));
2158 pa
= isl_pw_aff_gist(pa
, isl_set_copy(build
->domain
));
2162 isl_pw_aff_free(pa
);
2166 /* Simplify the piecewise multi-affine expression "aff" based on what
2167 * we know about the iterators of already generated loops.
2169 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2171 __isl_give isl_pw_multi_aff
*isl_ast_build_compute_gist_pw_multi_aff(
2172 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_multi_aff
*pma
)
2177 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
,
2178 isl_multi_aff_copy(build
->values
));
2179 pma
= isl_pw_multi_aff_gist(pma
, isl_set_copy(build
->domain
));
2183 isl_pw_multi_aff_free(pma
);
2187 /* Extract the schedule domain of the given type from build->options
2188 * at the current depth.
2190 * In particular, find the subset of build->options that is of
2191 * the following form
2193 * schedule_domain -> type[depth]
2195 * and return the corresponding domain, after eliminating inner dimensions
2196 * and divs that depend on the current dimension.
2198 * Note that the domain of build->options has been reformulated
2199 * in terms of the internal build space in embed_options,
2200 * but the position is still that within the current code generation.
2202 __isl_give isl_set
*isl_ast_build_get_option_domain(
2203 __isl_keep isl_ast_build
*build
, enum isl_ast_loop_type type
)
2214 name
= option_str
[type
];
2215 local_pos
= build
->depth
- build
->outer_pos
;
2217 space
= isl_ast_build_get_space(build
, 1);
2218 space
= isl_space_from_domain(space
);
2219 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2220 space
= isl_space_set_tuple_name(space
, isl_dim_out
, name
);
2222 option
= isl_union_map_extract_map(build
->options
, space
);
2223 option
= isl_map_fix_si(option
, isl_dim_out
, 0, local_pos
);
2225 domain
= isl_map_domain(option
);
2226 domain
= isl_ast_build_eliminate(build
, domain
);
2231 /* How does the user want the current schedule dimension to be generated?
2232 * These choices have been extracted from the schedule node
2233 * in extract_loop_types and stored in build->loop_type.
2234 * They have been updated to reflect any dimension insertion in
2236 * Return isl_ast_domain_error on error.
2238 * If "isolated" is set, then we get the loop AST generation type
2239 * directly from the band node since node_insert_dim cannot have been
2240 * called on a band with the isolate option.
2242 enum isl_ast_loop_type
isl_ast_build_get_loop_type(
2243 __isl_keep isl_ast_build
*build
, int isolated
)
2249 return isl_ast_loop_error
;
2250 ctx
= isl_ast_build_get_ctx(build
);
2252 isl_die(ctx
, isl_error_internal
,
2253 "only works for schedule tree based AST generation",
2254 return isl_ast_loop_error
);
2256 local_pos
= build
->depth
- build
->outer_pos
;
2258 return build
->loop_type
[local_pos
];
2259 return isl_schedule_node_band_member_get_isolate_ast_loop_type(
2260 build
->node
, local_pos
);
2263 /* Extract the isolated set from the isolate option, if any,
2264 * and store in the build.
2265 * If there is no isolate option, then the isolated set is
2266 * set to the empty set.
2268 * The isolate option is of the form
2270 * isolate[[outer bands] -> current_band]
2272 * We flatten this set and then map it back to the internal
2275 * If we have already extracted the isolated set
2276 * or if internal2input is no longer set, then we do not
2277 * need to do anything. In the latter case, we know
2278 * that the current band cannot have any isolate option.
2280 __isl_give isl_ast_build
*isl_ast_build_extract_isolated(
2281 __isl_take isl_ast_build
*build
)
2287 if (!build
->internal2input
)
2289 if (build
->isolated
)
2292 build
= isl_ast_build_cow(build
);
2296 isolated
= isl_schedule_node_band_get_ast_isolate_option(build
->node
);
2297 isolated
= isl_set_flatten(isolated
);
2298 isolated
= isl_set_preimage_multi_aff(isolated
,
2299 isl_multi_aff_copy(build
->internal2input
));
2301 build
->isolated
= isolated
;
2302 if (!build
->isolated
)
2303 return isl_ast_build_free(build
);
2308 /* Does "build" have a non-empty isolated set?
2310 * The caller is assumed to have called isl_ast_build_extract_isolated first.
2312 int isl_ast_build_has_isolated(__isl_keep isl_ast_build
*build
)
2318 if (!build
->internal2input
)
2320 if (!build
->isolated
)
2321 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
2322 "isolated set not extracted yet", return -1);
2324 empty
= isl_set_plain_is_empty(build
->isolated
);
2325 return empty
< 0 ? -1 : !empty
;
2328 /* Return a copy of the isolated set of "build".
2330 * The caller is assume to have called isl_ast_build_has_isolated first,
2331 * with this function returning true.
2332 * In particular, this function should not be called if we are no
2333 * longer keeping track of internal2input (and there therefore could
2334 * not possibly be any isolated set).
2336 __isl_give isl_set
*isl_ast_build_get_isolated(__isl_keep isl_ast_build
*build
)
2340 if (!build
->internal2input
)
2341 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
2342 "build cannot have isolated set", return NULL
);
2344 return isl_set_copy(build
->isolated
);
2347 /* Extract the separation class mapping at the current depth.
2349 * In particular, find and return the subset of build->options that is of
2350 * the following form
2352 * schedule_domain -> separation_class[[depth] -> [class]]
2354 * The caller is expected to eliminate inner dimensions from the domain.
2356 * Note that the domain of build->options has been reformulated
2357 * in terms of the internal build space in embed_options,
2358 * but the position is still that within the current code generation.
2360 __isl_give isl_map
*isl_ast_build_get_separation_class(
2361 __isl_keep isl_ast_build
*build
)
2364 isl_space
*space_sep
, *space
;
2371 local_pos
= build
->depth
- build
->outer_pos
;
2372 ctx
= isl_ast_build_get_ctx(build
);
2373 space_sep
= isl_space_alloc(ctx
, 0, 1, 1);
2374 space_sep
= isl_space_wrap(space_sep
);
2375 space_sep
= isl_space_set_tuple_name(space_sep
, isl_dim_set
,
2376 "separation_class");
2377 space
= isl_ast_build_get_space(build
, 1);
2378 space_sep
= isl_space_align_params(space_sep
, isl_space_copy(space
));
2379 space
= isl_space_map_from_domain_and_range(space
, space_sep
);
2381 res
= isl_union_map_extract_map(build
->options
, space
);
2382 res
= isl_map_fix_si(res
, isl_dim_out
, 0, local_pos
);
2383 res
= isl_map_coalesce(res
);
2388 /* Eliminate dimensions inner to the current dimension.
2390 __isl_give isl_set
*isl_ast_build_eliminate_inner(
2391 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2397 return isl_set_free(set
);
2399 dim
= isl_set_dim(set
, isl_dim_set
);
2400 depth
= build
->depth
;
2401 set
= isl_set_detect_equalities(set
);
2402 set
= isl_set_eliminate(set
, isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2407 /* Eliminate unknown divs and divs that depend on the current dimension.
2409 * Note that during the elimination of unknown divs, we may discover
2410 * an explicit representation of some other unknown divs, which may
2411 * depend on the current dimension. We therefore need to eliminate
2412 * unknown divs first.
2414 __isl_give isl_set
*isl_ast_build_eliminate_divs(
2415 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2420 return isl_set_free(set
);
2422 set
= isl_set_remove_unknown_divs(set
);
2423 depth
= build
->depth
;
2424 set
= isl_set_remove_divs_involving_dims(set
, isl_dim_set
, depth
, 1);
2429 /* Eliminate dimensions inner to the current dimension as well as
2430 * unknown divs and divs that depend on the current dimension.
2431 * The result then consists only of constraints that are independent
2432 * of the current dimension and upper and lower bounds on the current
2435 __isl_give isl_set
*isl_ast_build_eliminate(
2436 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*domain
)
2438 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2439 domain
= isl_ast_build_eliminate_divs(build
, domain
);