2 * Copyright 2012 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 #include <isl_ast_build_private.h>
14 #include <isl_ast_private.h>
16 /* Construct a map that isolates the current dimension.
18 * Essentially, the current dimension of "set" is moved to the single output
19 * dimension in the result, with the current dimension in the domain replaced
20 * by an unconstrained variable.
22 __isl_give isl_map
*isl_ast_build_map_to_iterator(
23 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
27 map
= isl_map_from_domain(set
);
28 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
31 return isl_map_free(map
);
33 map
= isl_map_equate(map
, isl_dim_in
, build
->depth
, isl_dim_out
, 0);
34 map
= isl_map_eliminate(map
, isl_dim_in
, build
->depth
, 1);
39 /* Initialize the information derived during the AST generation to default
40 * values for a schedule domain in "space".
42 * We also check that the remaining fields are not NULL so that
43 * the calling functions don't have to perform this test.
45 static __isl_give isl_ast_build
*isl_ast_build_init_derived(
46 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
51 build
= isl_ast_build_cow(build
);
52 if (!build
|| !build
->domain
)
55 ctx
= isl_ast_build_get_ctx(build
);
56 strides
= isl_vec_alloc(ctx
, isl_space_dim(space
, isl_dim_set
));
57 strides
= isl_vec_set_si(strides
, 1);
59 isl_vec_free(build
->strides
);
60 build
->strides
= strides
;
62 space
= isl_space_map_from_set(space
);
63 isl_multi_aff_free(build
->offsets
);
64 build
->offsets
= isl_multi_aff_zero(isl_space_copy(space
));
65 isl_multi_aff_free(build
->values
);
66 build
->values
= isl_multi_aff_identity(space
);
68 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
69 !build
->pending
|| !build
->values
||
70 !build
->strides
|| !build
->offsets
|| !build
->options
)
71 return isl_ast_build_free(build
);
75 isl_space_free(space
);
76 return isl_ast_build_free(build
);
79 /* Return an isl_id called "c%d", with "%d" set to "i".
80 * If an isl_id with such a name already appears among the parameters
81 * in build->domain, then adjust the name to "c%d_%d".
83 static __isl_give isl_id
*generate_name(isl_ctx
*ctx
, int i
,
84 __isl_keep isl_ast_build
*build
)
88 isl_set
*dom
= build
->domain
;
90 snprintf(name
, sizeof(name
), "c%d", i
);
92 while (isl_set_find_dim_by_name(dom
, isl_dim_param
, name
) >= 0)
93 snprintf(name
, sizeof(name
), "c%d_%d", i
, j
++);
94 return isl_id_alloc(ctx
, name
, NULL
);
97 /* Create an isl_ast_build with "set" as domain.
99 * The input set is usually a parameter domain, but we currently allow it to
100 * be any kind of set. We set the domain of the returned isl_ast_build
101 * to "set" and initialize all the other field to default values.
103 __isl_give isl_ast_build
*isl_ast_build_from_context(__isl_take isl_set
*set
)
108 isl_ast_build
*build
;
110 set
= isl_set_compute_divs(set
);
114 ctx
= isl_set_get_ctx(set
);
116 build
= isl_calloc_type(ctx
, isl_ast_build
);
122 build
->generated
= isl_set_copy(build
->domain
);
123 build
->pending
= isl_set_universe(isl_set_get_space(build
->domain
));
124 build
->options
= isl_union_map_empty(isl_space_params_alloc(ctx
, 0));
125 n
= isl_set_dim(set
, isl_dim_set
);
127 build
->iterators
= isl_id_list_alloc(ctx
, n
);
128 for (i
= 0; i
< n
; ++i
) {
130 if (isl_set_has_dim_id(set
, isl_dim_set
, i
))
131 id
= isl_set_get_dim_id(set
, isl_dim_set
, i
);
133 id
= generate_name(ctx
, i
, build
);
134 build
->iterators
= isl_id_list_add(build
->iterators
, id
);
136 space
= isl_set_get_space(set
);
137 if (isl_space_is_params(space
))
138 space
= isl_space_set_from_params(space
);
140 return isl_ast_build_init_derived(build
, space
);
146 __isl_give isl_ast_build
*isl_ast_build_copy(__isl_keep isl_ast_build
*build
)
155 __isl_give isl_ast_build
*isl_ast_build_dup(__isl_keep isl_ast_build
*build
)
163 ctx
= isl_ast_build_get_ctx(build
);
164 dup
= isl_calloc_type(ctx
, isl_ast_build
);
169 dup
->outer_pos
= build
->outer_pos
;
170 dup
->depth
= build
->depth
;
171 dup
->iterators
= isl_id_list_copy(build
->iterators
);
172 dup
->domain
= isl_set_copy(build
->domain
);
173 dup
->generated
= isl_set_copy(build
->generated
);
174 dup
->pending
= isl_set_copy(build
->pending
);
175 dup
->values
= isl_multi_aff_copy(build
->values
);
176 dup
->value
= isl_pw_aff_copy(build
->value
);
177 dup
->strides
= isl_vec_copy(build
->strides
);
178 dup
->offsets
= isl_multi_aff_copy(build
->offsets
);
179 dup
->executed
= isl_union_map_copy(build
->executed
);
180 dup
->single_valued
= build
->single_valued
;
181 dup
->options
= isl_union_map_copy(build
->options
);
182 dup
->at_each_domain
= build
->at_each_domain
;
183 dup
->at_each_domain_user
= build
->at_each_domain_user
;
184 dup
->before_each_for
= build
->before_each_for
;
185 dup
->before_each_for_user
= build
->before_each_for_user
;
186 dup
->after_each_for
= build
->after_each_for
;
187 dup
->after_each_for_user
= build
->after_each_for_user
;
188 dup
->create_leaf
= build
->create_leaf
;
189 dup
->create_leaf_user
= build
->create_leaf_user
;
191 if (!dup
->iterators
|| !dup
->domain
|| !dup
->generated
||
192 !dup
->pending
|| !dup
->values
||
193 !dup
->strides
|| !dup
->offsets
|| !dup
->options
||
194 (build
->executed
&& !dup
->executed
) ||
195 (build
->value
&& !dup
->value
))
196 return isl_ast_build_free(dup
);
201 /* Align the parameters of "build" to those of "model", introducing
202 * additional parameters if needed.
204 __isl_give isl_ast_build
*isl_ast_build_align_params(
205 __isl_take isl_ast_build
*build
, __isl_take isl_space
*model
)
207 build
= isl_ast_build_cow(build
);
211 build
->domain
= isl_set_align_params(build
->domain
,
212 isl_space_copy(model
));
213 build
->generated
= isl_set_align_params(build
->generated
,
214 isl_space_copy(model
));
215 build
->pending
= isl_set_align_params(build
->pending
,
216 isl_space_copy(model
));
217 build
->values
= isl_multi_aff_align_params(build
->values
,
218 isl_space_copy(model
));
219 build
->offsets
= isl_multi_aff_align_params(build
->offsets
,
220 isl_space_copy(model
));
221 build
->options
= isl_union_map_align_params(build
->options
,
222 isl_space_copy(model
));
223 isl_space_free(model
);
225 if (!build
->domain
|| !build
->values
|| !build
->offsets
||
227 return isl_ast_build_free(build
);
231 isl_space_free(model
);
235 __isl_give isl_ast_build
*isl_ast_build_cow(__isl_take isl_ast_build
*build
)
243 return isl_ast_build_dup(build
);
246 __isl_null isl_ast_build
*isl_ast_build_free(
247 __isl_take isl_ast_build
*build
)
252 if (--build
->ref
> 0)
255 isl_id_list_free(build
->iterators
);
256 isl_set_free(build
->domain
);
257 isl_set_free(build
->generated
);
258 isl_set_free(build
->pending
);
259 isl_multi_aff_free(build
->values
);
260 isl_pw_aff_free(build
->value
);
261 isl_vec_free(build
->strides
);
262 isl_multi_aff_free(build
->offsets
);
263 isl_multi_aff_free(build
->schedule_map
);
264 isl_union_map_free(build
->executed
);
265 isl_union_map_free(build
->options
);
272 isl_ctx
*isl_ast_build_get_ctx(__isl_keep isl_ast_build
*build
)
274 return build
? isl_set_get_ctx(build
->domain
) : NULL
;
277 /* Replace build->options by "options".
279 __isl_give isl_ast_build
*isl_ast_build_set_options(
280 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*options
)
282 build
= isl_ast_build_cow(build
);
284 if (!build
|| !options
)
287 isl_union_map_free(build
->options
);
288 build
->options
= options
;
292 isl_union_map_free(options
);
293 return isl_ast_build_free(build
);
296 /* Set the iterators for the next code generation.
298 * If we still have some iterators left from the previous code generation
299 * (if any) or if iterators have already been set by a previous
300 * call to this function, then we remove them first.
302 __isl_give isl_ast_build
*isl_ast_build_set_iterators(
303 __isl_take isl_ast_build
*build
, __isl_take isl_id_list
*iterators
)
307 build
= isl_ast_build_cow(build
);
311 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
312 n_it
= isl_id_list_n_id(build
->iterators
);
314 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
315 "isl_ast_build in inconsistent state", goto error
);
317 build
->iterators
= isl_id_list_drop(build
->iterators
,
319 build
->iterators
= isl_id_list_concat(build
->iterators
, iterators
);
320 if (!build
->iterators
)
321 return isl_ast_build_free(build
);
325 isl_id_list_free(iterators
);
326 return isl_ast_build_free(build
);
329 /* Set the "at_each_domain" callback of "build" to "fn".
331 __isl_give isl_ast_build
*isl_ast_build_set_at_each_domain(
332 __isl_take isl_ast_build
*build
,
333 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_node
*node
,
334 __isl_keep isl_ast_build
*build
, void *user
), void *user
)
336 build
= isl_ast_build_cow(build
);
341 build
->at_each_domain
= fn
;
342 build
->at_each_domain_user
= user
;
347 /* Set the "before_each_for" callback of "build" to "fn".
349 __isl_give isl_ast_build
*isl_ast_build_set_before_each_for(
350 __isl_take isl_ast_build
*build
,
351 __isl_give isl_id
*(*fn
)(__isl_keep isl_ast_build
*build
,
352 void *user
), void *user
)
354 build
= isl_ast_build_cow(build
);
359 build
->before_each_for
= fn
;
360 build
->before_each_for_user
= user
;
365 /* Set the "after_each_for" callback of "build" to "fn".
367 __isl_give isl_ast_build
*isl_ast_build_set_after_each_for(
368 __isl_take isl_ast_build
*build
,
369 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_node
*node
,
370 __isl_keep isl_ast_build
*build
, void *user
), void *user
)
372 build
= isl_ast_build_cow(build
);
377 build
->after_each_for
= fn
;
378 build
->after_each_for_user
= user
;
383 /* Set the "create_leaf" callback of "build" to "fn".
385 __isl_give isl_ast_build
*isl_ast_build_set_create_leaf(
386 __isl_take isl_ast_build
*build
,
387 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_build
*build
,
388 void *user
), void *user
)
390 build
= isl_ast_build_cow(build
);
395 build
->create_leaf
= fn
;
396 build
->create_leaf_user
= user
;
401 /* Clear all information that is specific to this code generation
402 * and that is (probably) not meaningful to any nested code generation.
404 __isl_give isl_ast_build
*isl_ast_build_clear_local_info(
405 __isl_take isl_ast_build
*build
)
409 build
= isl_ast_build_cow(build
);
413 space
= isl_union_map_get_space(build
->options
);
414 isl_union_map_free(build
->options
);
415 build
->options
= isl_union_map_empty(space
);
417 build
->at_each_domain
= NULL
;
418 build
->at_each_domain_user
= NULL
;
419 build
->before_each_for
= NULL
;
420 build
->before_each_for_user
= NULL
;
421 build
->after_each_for
= NULL
;
422 build
->after_each_for_user
= NULL
;
423 build
->create_leaf
= NULL
;
424 build
->create_leaf_user
= NULL
;
427 return isl_ast_build_free(build
);
432 /* Have any loops been eliminated?
433 * That is, do any of the original schedule dimensions have a fixed
434 * value that has been substituted?
436 static int any_eliminated(isl_ast_build
*build
)
440 for (i
= 0; i
< build
->depth
; ++i
)
441 if (isl_ast_build_has_affine_value(build
, i
))
447 /* Clear build->schedule_map.
448 * This function should be called whenever anything that might affect
449 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
450 * In particular, it should be called when the depth is changed or
451 * when an iterator is determined to have a fixed value.
453 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build
*build
)
457 isl_multi_aff_free(build
->schedule_map
);
458 build
->schedule_map
= NULL
;
461 /* Do we need a (non-trivial) schedule map?
462 * That is, is the internal schedule space different from
463 * the external schedule space?
465 * The internal and external schedule spaces are only the same
466 * if code has been generated for the entire schedule and if none
467 * of the loops have been eliminated.
469 __isl_give
int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build
*build
)
476 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
477 return build
->depth
!= dim
|| any_eliminated(build
);
480 /* Return a mapping from the internal schedule space to the external
481 * schedule space in the form of an isl_multi_aff.
482 * The internal schedule space originally corresponds to that of the
483 * input schedule. This may change during the code generation if
484 * if isl_ast_build_insert_dim is ever called.
485 * The external schedule space corresponds to the
486 * loops that have been generated.
488 * Currently, the only difference between the internal schedule domain
489 * and the external schedule domain is that some dimensions are projected
490 * out in the external schedule domain. In particular, the dimensions
491 * for which no code has been generated yet and the dimensions that correspond
492 * to eliminated loops.
494 * We cache a copy of the schedule_map in build->schedule_map.
495 * The cache is cleared through isl_ast_build_reset_schedule_map
496 * whenever anything changes that might affect the result of this function.
498 __isl_give isl_multi_aff
*isl_ast_build_get_schedule_map_multi_aff(
499 __isl_keep isl_ast_build
*build
)
506 if (build
->schedule_map
)
507 return isl_multi_aff_copy(build
->schedule_map
);
509 space
= isl_ast_build_get_space(build
, 1);
510 space
= isl_space_map_from_set(space
);
511 ma
= isl_multi_aff_identity(space
);
512 if (isl_ast_build_need_schedule_map(build
)) {
514 int dim
= isl_set_dim(build
->domain
, isl_dim_set
);
515 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
,
516 build
->depth
, dim
- build
->depth
);
517 for (i
= build
->depth
- 1; i
>= 0; --i
)
518 if (isl_ast_build_has_affine_value(build
, i
))
519 ma
= isl_multi_aff_drop_dims(ma
,
523 build
->schedule_map
= ma
;
524 return isl_multi_aff_copy(build
->schedule_map
);
527 /* Return a mapping from the internal schedule space to the external
528 * schedule space in the form of an isl_map.
530 __isl_give isl_map
*isl_ast_build_get_schedule_map(
531 __isl_keep isl_ast_build
*build
)
535 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
536 return isl_map_from_multi_aff(ma
);
539 /* Return the position of the dimension in build->domain for which
540 * an AST node is currently being generated.
542 int isl_ast_build_get_depth(__isl_keep isl_ast_build
*build
)
544 return build
? build
->depth
: -1;
547 /* Prepare for generating code for the next level.
548 * In particular, increase the depth and reset any information
549 * that is local to the current depth.
551 __isl_give isl_ast_build
*isl_ast_build_increase_depth(
552 __isl_take isl_ast_build
*build
)
554 build
= isl_ast_build_cow(build
);
558 isl_ast_build_reset_schedule_map(build
);
559 build
->value
= isl_pw_aff_free(build
->value
);
563 void isl_ast_build_dump(__isl_keep isl_ast_build
*build
)
568 fprintf(stderr
, "domain: ");
569 isl_set_dump(build
->domain
);
570 fprintf(stderr
, "generated: ");
571 isl_set_dump(build
->generated
);
572 fprintf(stderr
, "pending: ");
573 isl_set_dump(build
->pending
);
574 fprintf(stderr
, "iterators: ");
575 isl_id_list_dump(build
->iterators
);
576 fprintf(stderr
, "values: ");
577 isl_multi_aff_dump(build
->values
);
579 fprintf(stderr
, "value: ");
580 isl_pw_aff_dump(build
->value
);
582 fprintf(stderr
, "strides: ");
583 isl_vec_dump(build
->strides
);
584 fprintf(stderr
, "offsets: ");
585 isl_multi_aff_dump(build
->offsets
);
588 /* Initialize "build" for AST construction in schedule space "space"
589 * in the case that build->domain is a parameter set.
591 * build->iterators is assumed to have been updated already.
593 static __isl_give isl_ast_build
*isl_ast_build_init(
594 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
598 build
= isl_ast_build_cow(build
);
602 set
= isl_set_universe(isl_space_copy(space
));
603 build
->domain
= isl_set_intersect_params(isl_set_copy(set
),
605 build
->pending
= isl_set_intersect_params(isl_set_copy(set
),
607 build
->generated
= isl_set_intersect_params(set
, build
->generated
);
609 return isl_ast_build_init_derived(build
, space
);
611 isl_ast_build_free(build
);
612 isl_space_free(space
);
616 /* Assign "aff" to *user and return -1, effectively extracting
617 * the first (and presumably only) affine expression in the isl_pw_aff
618 * on which this function is used.
620 static int extract_single_piece(__isl_take isl_set
*set
,
621 __isl_take isl_aff
*aff
, void *user
)
631 /* Check if the given bounds on the current dimension imply that
632 * this current dimension attains only a single value (in terms of
633 * parameters and outer dimensions).
634 * If so, we record it in build->value.
635 * If, moreover, this value can be represented as a single affine expression,
636 * then we also update build->values, effectively marking the current
637 * dimension as "eliminated".
639 * When computing the gist of the fixed value that can be represented
640 * as a single affine expression, it is important to only take into
641 * account the domain constraints in the original AST build and
642 * not the domain of the affine expression itself.
643 * Otherwise, a [i/3] is changed into a i/3 because we know that i
644 * is a multiple of 3, but then we end up not expressing anywhere
645 * in the context that i is a multiple of 3.
647 static __isl_give isl_ast_build
*update_values(
648 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*bounds
)
651 isl_pw_multi_aff
*pma
;
656 set
= isl_set_from_basic_set(bounds
);
657 set
= isl_set_intersect(set
, isl_set_copy(build
->domain
));
658 it_map
= isl_ast_build_map_to_iterator(build
, set
);
660 sv
= isl_map_is_single_valued(it_map
);
662 build
= isl_ast_build_free(build
);
664 isl_map_free(it_map
);
668 pma
= isl_pw_multi_aff_from_map(it_map
);
669 build
->value
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
670 build
->value
= isl_ast_build_compute_gist_pw_aff(build
, build
->value
);
671 build
->value
= isl_pw_aff_coalesce(build
->value
);
672 isl_pw_multi_aff_free(pma
);
675 return isl_ast_build_free(build
);
677 if (isl_pw_aff_n_piece(build
->value
) != 1)
680 isl_pw_aff_foreach_piece(build
->value
, &extract_single_piece
, &aff
);
682 build
->values
= isl_multi_aff_set_aff(build
->values
, build
->depth
, aff
);
684 return isl_ast_build_free(build
);
685 isl_ast_build_reset_schedule_map(build
);
689 /* Update the AST build based on the given loop bounds for
690 * the current dimension.
692 * We first make sure that the bounds do not refer to any iterators
693 * that have already been eliminated.
694 * Then, we check if the bounds imply that the current iterator
696 * If they do and if this fixed value can be expressed as a single
697 * affine expression, we eliminate the iterators from the bounds.
698 * Note that we cannot simply plug in this single value using
699 * isl_basic_set_preimage_multi_aff as the single value may only
700 * be defined on a subset of the domain. Plugging in the value
701 * would restrict the build domain to this subset, while this
702 * restriction may not be reflected in the generated code.
703 * build->domain may, however, already refer to the current dimension
704 * due an earlier call to isl_ast_build_include_stride. If so, we need
705 * to eliminate the dimension so that we do not introduce it in any other sets.
706 * Finally, we intersect build->domain with the updated bounds.
708 * Note that the check for a fixed value in update_values requires
709 * us to intersect the bounds with the current build domain.
710 * When we intersect build->domain with the updated bounds in
711 * the final step, we make sure that these updated bounds have
712 * not been intersected with the old build->domain.
713 * Otherwise, we would indirectly intersect the build domain with itself,
714 * which can lead to inefficiencies, in particular if the build domain
715 * contains any unknown divs.
717 __isl_give isl_ast_build
*isl_ast_build_set_loop_bounds(
718 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*bounds
)
722 build
= isl_ast_build_cow(build
);
726 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
727 isl_multi_aff_copy(build
->values
));
728 build
= update_values(build
, isl_basic_set_copy(bounds
));
731 set
= isl_set_from_basic_set(isl_basic_set_copy(bounds
));
732 if (isl_ast_build_has_affine_value(build
, build
->depth
)) {
733 set
= isl_set_eliminate(set
, isl_dim_set
, build
->depth
, 1);
734 set
= isl_set_compute_divs(set
);
735 build
->pending
= isl_set_intersect(build
->pending
,
737 if (isl_ast_build_has_stride(build
, build
->depth
)) {
738 build
->domain
= isl_set_eliminate(build
->domain
,
739 isl_dim_set
, build
->depth
, 1);
740 build
->domain
= isl_set_compute_divs(build
->domain
);
743 isl_basic_set
*generated
, *pending
;
745 pending
= isl_basic_set_copy(bounds
);
746 pending
= isl_basic_set_drop_constraints_involving_dims(pending
,
747 isl_dim_set
, build
->depth
, 1);
748 build
->pending
= isl_set_intersect(build
->pending
,
749 isl_set_from_basic_set(pending
));
750 generated
= isl_basic_set_copy(bounds
);
751 generated
= isl_basic_set_drop_constraints_not_involving_dims(
752 generated
, isl_dim_set
, build
->depth
, 1);
753 build
->generated
= isl_set_intersect(build
->generated
,
754 isl_set_from_basic_set(generated
));
756 isl_basic_set_free(bounds
);
758 build
->domain
= isl_set_intersect(build
->domain
, set
);
759 if (!build
->domain
|| !build
->pending
|| !build
->generated
)
760 return isl_ast_build_free(build
);
764 isl_ast_build_free(build
);
765 isl_basic_set_free(bounds
);
769 /* Update build->domain based on the constraints enforced by inner loops.
771 * The constraints in build->pending may end up not getting generated
772 * if they are implied by "enforced". We therefore reconstruct
773 * build->domain from build->generated and build->pending, dropping
774 * those constraint in build->pending that may not get generated.
776 __isl_give isl_ast_build
*isl_ast_build_set_enforced(
777 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*enforced
)
781 build
= isl_ast_build_cow(build
);
785 set
= isl_set_from_basic_set(enforced
);
786 set
= isl_set_gist(isl_set_copy(build
->pending
), set
);
787 set
= isl_set_intersect(isl_set_copy(build
->generated
), set
);
789 isl_set_free(build
->domain
);
793 return isl_ast_build_free(build
);
797 isl_basic_set_free(enforced
);
798 return isl_ast_build_free(build
);
801 /* Intersect build->domain with "set", where "set" is specified
802 * in terms of the internal schedule domain.
804 static __isl_give isl_ast_build
*isl_ast_build_restrict_internal(
805 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
807 build
= isl_ast_build_cow(build
);
811 set
= isl_set_compute_divs(set
);
812 build
->domain
= isl_set_intersect(build
->domain
, set
);
813 build
->domain
= isl_set_coalesce(build
->domain
);
816 return isl_ast_build_free(build
);
820 isl_ast_build_free(build
);
825 /* Intersect build->generated and build->domain with "set",
826 * where "set" is specified in terms of the internal schedule domain.
828 __isl_give isl_ast_build
*isl_ast_build_restrict_generated(
829 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
831 set
= isl_set_compute_divs(set
);
832 build
= isl_ast_build_restrict_internal(build
, isl_set_copy(set
));
833 build
= isl_ast_build_cow(build
);
837 build
->generated
= isl_set_intersect(build
->generated
, set
);
838 build
->generated
= isl_set_coalesce(build
->generated
);
840 if (!build
->generated
)
841 return isl_ast_build_free(build
);
845 isl_ast_build_free(build
);
850 /* Intersect build->pending and build->domain with "set",
851 * where "set" is specified in terms of the internal schedule domain.
853 __isl_give isl_ast_build
*isl_ast_build_restrict_pending(
854 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
856 set
= isl_set_compute_divs(set
);
857 build
= isl_ast_build_restrict_internal(build
, isl_set_copy(set
));
858 build
= isl_ast_build_cow(build
);
862 build
->pending
= isl_set_intersect(build
->pending
, set
);
863 build
->pending
= isl_set_coalesce(build
->pending
);
866 return isl_ast_build_free(build
);
870 isl_ast_build_free(build
);
875 /* Intersect build->domain with "set", where "set" is specified
876 * in terms of the external schedule domain.
878 __isl_give isl_ast_build
*isl_ast_build_restrict(
879 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
881 if (isl_set_is_params(set
))
882 return isl_ast_build_restrict_generated(build
, set
);
884 if (isl_ast_build_need_schedule_map(build
)) {
886 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
887 set
= isl_set_preimage_multi_aff(set
, ma
);
889 return isl_ast_build_restrict_generated(build
, set
);
892 /* Replace build->executed by "executed".
894 __isl_give isl_ast_build
*isl_ast_build_set_executed(
895 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*executed
)
897 build
= isl_ast_build_cow(build
);
901 isl_union_map_free(build
->executed
);
902 build
->executed
= executed
;
906 isl_ast_build_free(build
);
907 isl_union_map_free(executed
);
911 /* Return a copy of the current schedule domain.
913 __isl_give isl_set
*isl_ast_build_get_domain(__isl_keep isl_ast_build
*build
)
915 return build
? isl_set_copy(build
->domain
) : NULL
;
918 /* Return the (schedule) space of "build".
920 * If "internal" is set, then this space is the space of the internal
921 * representation of the entire schedule, including those parts for
922 * which no code has been generated yet.
924 * If "internal" is not set, then this space is the external representation
925 * of the loops generated so far.
927 __isl_give isl_space
*isl_ast_build_get_space(__isl_keep isl_ast_build
*build
,
937 space
= isl_set_get_space(build
->domain
);
941 if (!isl_ast_build_need_schedule_map(build
))
944 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
945 space
= isl_space_drop_dims(space
, isl_dim_set
,
946 build
->depth
, dim
- build
->depth
);
947 for (i
= build
->depth
- 1; i
>= 0; --i
)
948 if (isl_ast_build_has_affine_value(build
, i
))
949 space
= isl_space_drop_dims(space
, isl_dim_set
, i
, 1);
954 /* Return the external representation of the schedule space of "build",
955 * i.e., a space with a dimension for each loop generated so far,
956 * with the names of the dimensions set to the loop iterators.
958 __isl_give isl_space
*isl_ast_build_get_schedule_space(
959 __isl_keep isl_ast_build
*build
)
967 space
= isl_ast_build_get_space(build
, 0);
970 for (i
= 0; i
< build
->depth
; ++i
) {
973 if (isl_ast_build_has_affine_value(build
, i
)) {
978 id
= isl_ast_build_get_iterator_id(build
, i
);
979 space
= isl_space_set_dim_id(space
, isl_dim_set
, i
- skip
, id
);
985 /* Return the current schedule, as stored in build->executed, in terms
986 * of the external schedule domain.
988 __isl_give isl_union_map
*isl_ast_build_get_schedule(
989 __isl_keep isl_ast_build
*build
)
991 isl_union_map
*executed
;
992 isl_union_map
*schedule
;
997 executed
= isl_union_map_copy(build
->executed
);
998 if (isl_ast_build_need_schedule_map(build
)) {
999 isl_map
*proj
= isl_ast_build_get_schedule_map(build
);
1000 executed
= isl_union_map_apply_domain(executed
,
1001 isl_union_map_from_map(proj
));
1003 schedule
= isl_union_map_reverse(executed
);
1008 /* Return the iterator attached to the internal schedule dimension "pos".
1010 __isl_give isl_id
*isl_ast_build_get_iterator_id(
1011 __isl_keep isl_ast_build
*build
, int pos
)
1016 return isl_id_list_get_id(build
->iterators
, pos
);
1019 /* Set the stride and offset of the current dimension to the given
1020 * value and expression.
1022 * If we had already found a stride before, then the two strides
1023 * are combined into a single stride.
1025 * In particular, if the new stride information is of the form
1029 * and the old stride information is of the form
1033 * then we compute the extended gcd of s and s2
1037 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
1038 * and the second with t2 = a s1/g.
1041 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
1043 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
1044 * is the combined stride.
1046 static __isl_give isl_ast_build
*set_stride(__isl_take isl_ast_build
*build
,
1047 __isl_take isl_val
*stride
, __isl_take isl_aff
*offset
)
1051 build
= isl_ast_build_cow(build
);
1052 if (!build
|| !stride
|| !offset
)
1057 if (isl_ast_build_has_stride(build
, pos
)) {
1058 isl_val
*stride2
, *a
, *b
, *g
;
1061 stride2
= isl_vec_get_element_val(build
->strides
, pos
);
1062 g
= isl_val_gcdext(isl_val_copy(stride
), isl_val_copy(stride2
),
1064 a
= isl_val_mul(a
, isl_val_copy(stride
));
1065 a
= isl_val_div(a
, isl_val_copy(g
));
1066 stride2
= isl_val_div(stride2
, g
);
1067 b
= isl_val_mul(b
, isl_val_copy(stride2
));
1068 stride
= isl_val_mul(stride
, stride2
);
1070 offset2
= isl_multi_aff_get_aff(build
->offsets
, pos
);
1071 offset2
= isl_aff_scale_val(offset2
, a
);
1072 offset
= isl_aff_scale_val(offset
, b
);
1073 offset
= isl_aff_add(offset
, offset2
);
1076 build
->strides
= isl_vec_set_element_val(build
->strides
, pos
, stride
);
1077 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, pos
, offset
);
1078 if (!build
->strides
|| !build
->offsets
)
1079 return isl_ast_build_free(build
);
1083 isl_val_free(stride
);
1084 isl_aff_free(offset
);
1085 return isl_ast_build_free(build
);
1088 /* Return a set expressing the stride constraint at the current depth.
1090 * In particular, if the current iterator (i) is known to attain values
1094 * where f is the offset and s is the stride, then the returned set
1095 * expresses the constraint
1099 __isl_give isl_set
*isl_ast_build_get_stride_constraint(
1100 __isl_keep isl_ast_build
*build
)
1112 if (!isl_ast_build_has_stride(build
, pos
))
1113 return isl_set_universe(isl_ast_build_get_space(build
, 1));
1115 stride
= isl_ast_build_get_stride(build
, pos
);
1116 aff
= isl_ast_build_get_offset(build
, pos
);
1117 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, -1);
1118 aff
= isl_aff_mod_val(aff
, stride
);
1119 set
= isl_set_from_basic_set(isl_aff_zero_basic_set(aff
));
1124 /* Return the expansion implied by the stride and offset at the current
1127 * That is, return the mapping
1129 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1130 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1132 * where s is the stride at the current depth d and offset(i) is
1133 * the corresponding offset.
1135 __isl_give isl_multi_aff
*isl_ast_build_get_stride_expansion(
1136 __isl_keep isl_ast_build
*build
)
1141 isl_aff
*aff
, *offset
;
1147 pos
= isl_ast_build_get_depth(build
);
1148 space
= isl_ast_build_get_space(build
, 1);
1149 space
= isl_space_map_from_set(space
);
1150 ma
= isl_multi_aff_identity(space
);
1152 if (!isl_ast_build_has_stride(build
, pos
))
1155 offset
= isl_ast_build_get_offset(build
, pos
);
1156 stride
= isl_ast_build_get_stride(build
, pos
);
1157 aff
= isl_multi_aff_get_aff(ma
, pos
);
1158 aff
= isl_aff_scale_val(aff
, stride
);
1159 aff
= isl_aff_add(aff
, offset
);
1160 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1165 /* Add constraints corresponding to any previously detected
1166 * stride on the current dimension to build->domain.
1168 __isl_give isl_ast_build
*isl_ast_build_include_stride(
1169 __isl_take isl_ast_build
*build
)
1175 if (!isl_ast_build_has_stride(build
, build
->depth
))
1177 build
= isl_ast_build_cow(build
);
1181 set
= isl_ast_build_get_stride_constraint(build
);
1183 build
->domain
= isl_set_intersect(build
->domain
, isl_set_copy(set
));
1184 build
->generated
= isl_set_intersect(build
->generated
, set
);
1185 if (!build
->domain
|| !build
->generated
)
1186 return isl_ast_build_free(build
);
1191 /* Information used inside detect_stride.
1193 * "build" may be updated by detect_stride to include stride information.
1194 * "pos" is equal to build->depth.
1196 struct isl_detect_stride_data
{
1197 isl_ast_build
*build
;
1201 /* Check if constraint "c" imposes any stride on dimension data->pos
1202 * and, if so, update the stride information in data->build.
1204 * In order to impose a stride on the dimension, "c" needs to be an equality
1205 * and it needs to involve the dimension. Note that "c" may also be
1206 * a div constraint and thus an inequality that we cannot use.
1208 * Let c be of the form
1210 * h(p) + g * v * i + g * stride * f(alpha) = 0
1212 * with h(p) an expression in terms of the parameters and outer dimensions
1213 * and f(alpha) an expression in terms of the existentially quantified
1214 * variables. Note that the inner dimensions have been eliminated so
1215 * they do not appear in "c".
1217 * If "stride" is not zero and not one, then it represents a non-trivial stride
1218 * on "i". We compute a and b such that
1220 * a v + b stride = 1
1224 * g v i = -h(p) + g stride f(alpha)
1226 * a g v i = -a h(p) + g stride f(alpha)
1228 * a g v i + b g stride i = -a h(p) + g stride * (...)
1230 * g i = -a h(p) + g stride * (...)
1232 * i = -a h(p)/g + stride * (...)
1234 * The expression "-a h(p)/g" can therefore be used as offset.
1236 static int detect_stride(__isl_take isl_constraint
*c
, void *user
)
1238 struct isl_detect_stride_data
*data
= user
;
1241 isl_val
*v
, *stride
, *m
;
1243 if (!isl_constraint_is_equality(c
) ||
1244 !isl_constraint_involves_dims(c
, isl_dim_set
, data
->pos
, 1)) {
1245 isl_constraint_free(c
);
1249 ctx
= isl_constraint_get_ctx(c
);
1250 stride
= isl_val_zero(ctx
);
1251 n_div
= isl_constraint_dim(c
, isl_dim_div
);
1252 for (i
= 0; i
< n_div
; ++i
) {
1253 v
= isl_constraint_get_coefficient_val(c
, isl_dim_div
, i
);
1254 stride
= isl_val_gcd(stride
, v
);
1257 v
= isl_constraint_get_coefficient_val(c
, isl_dim_set
, data
->pos
);
1258 m
= isl_val_gcd(isl_val_copy(stride
), isl_val_copy(v
));
1259 stride
= isl_val_div(stride
, isl_val_copy(m
));
1260 v
= isl_val_div(v
, isl_val_copy(m
));
1262 if (!isl_val_is_zero(stride
) && !isl_val_is_one(stride
)) {
1264 isl_val
*gcd
, *a
, *b
;
1266 gcd
= isl_val_gcdext(v
, isl_val_copy(stride
), &a
, &b
);
1270 aff
= isl_constraint_get_aff(c
);
1271 for (i
= 0; i
< n_div
; ++i
)
1272 aff
= isl_aff_set_coefficient_si(aff
,
1274 aff
= isl_aff_set_coefficient_si(aff
, isl_dim_in
, data
->pos
, 0);
1276 aff
= isl_aff_scale_val(aff
, a
);
1277 aff
= isl_aff_scale_down_val(aff
, m
);
1278 data
->build
= set_stride(data
->build
, stride
, aff
);
1280 isl_val_free(stride
);
1285 isl_constraint_free(c
);
1289 /* Check if the constraints in "set" imply any stride on the current
1290 * dimension and, if so, record the stride information in "build"
1291 * and return the updated "build".
1293 * We compute the affine hull and then check if any of the constraints
1294 * in the hull imposes any stride on the current dimension.
1296 * We assume that inner dimensions have been eliminated from "set"
1297 * by the caller. This is needed because the common stride
1298 * may be imposed by different inner dimensions on different parts of
1301 __isl_give isl_ast_build
*isl_ast_build_detect_strides(
1302 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
1304 isl_basic_set
*hull
;
1305 struct isl_detect_stride_data data
;
1311 data
.pos
= isl_ast_build_get_depth(build
);
1312 hull
= isl_set_affine_hull(set
);
1314 if (isl_basic_set_foreach_constraint(hull
, &detect_stride
, &data
) < 0)
1315 data
.build
= isl_ast_build_free(data
.build
);
1317 isl_basic_set_free(hull
);
1324 struct isl_ast_build_involves_data
{
1329 /* Check if "map" involves the input dimension data->depth.
1331 static int involves_depth(__isl_take isl_map
*map
, void *user
)
1333 struct isl_ast_build_involves_data
*data
= user
;
1335 data
->involves
= isl_map_involves_dims(map
, isl_dim_in
, data
->depth
, 1);
1338 if (data
->involves
< 0 || data
->involves
)
1343 /* Do any options depend on the value of the dimension at the current depth?
1345 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build
*build
)
1347 struct isl_ast_build_involves_data data
;
1352 data
.depth
= build
->depth
;
1355 if (isl_union_map_foreach_map(build
->options
,
1356 &involves_depth
, &data
) < 0) {
1357 if (data
.involves
< 0 || !data
.involves
)
1361 return data
.involves
;
1364 /* Construct the map
1366 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1368 * with "space" the parameter space of the constructed map.
1370 static __isl_give isl_map
*construct_insertion_map(__isl_take isl_space
*space
,
1374 isl_basic_map
*bmap1
, *bmap2
;
1376 space
= isl_space_set_from_params(space
);
1377 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1378 space
= isl_space_map_from_set(space
);
1379 c
= isl_equality_alloc(isl_local_space_from_space(space
));
1380 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, 0, 1);
1381 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, -1);
1382 bmap1
= isl_basic_map_from_constraint(isl_constraint_copy(c
));
1383 c
= isl_constraint_set_constant_si(c
, 1);
1384 bmap2
= isl_basic_map_from_constraint(c
);
1386 bmap1
= isl_basic_map_upper_bound_si(bmap1
, isl_dim_in
, 0, pos
- 1);
1387 bmap2
= isl_basic_map_lower_bound_si(bmap2
, isl_dim_in
, 0, pos
);
1389 return isl_basic_map_union(bmap1
, bmap2
);
1392 static const char *option_str
[] = {
1393 [atomic
] = "atomic",
1394 [unroll
] = "unroll",
1395 [separate
] = "separate"
1398 /* Update the "options" to reflect the insertion of a dimension
1399 * at position "pos" in the schedule domain space.
1400 * "space" is the original domain space before the insertion and
1401 * may be named and/or structured.
1403 * The (relevant) input options all have "space" as domain, which
1404 * has to be mapped to the extended space.
1405 * The values of the ranges also refer to the schedule domain positions
1406 * and they therefore also need to be adjusted. In particular, values
1407 * smaller than pos do not need to change, while values greater than or
1408 * equal to pos need to be incremented.
1409 * That is, we need to apply the following map.
1411 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1412 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1413 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1414 * separation_class[[i] -> [c]]
1415 * -> separation_class[[i] -> [c]] : i < pos;
1416 * separation_class[[i] -> [c]]
1417 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1419 static __isl_give isl_union_map
*options_insert_dim(
1420 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
, int pos
)
1423 isl_union_map
*insertion
;
1424 enum isl_ast_build_domain_type type
;
1425 const char *name
= "separation_class";
1427 space
= isl_space_map_from_set(space
);
1428 map
= isl_map_identity(space
);
1429 map
= isl_map_insert_dims(map
, isl_dim_out
, pos
, 1);
1430 options
= isl_union_map_apply_domain(options
,
1431 isl_union_map_from_map(map
));
1436 map
= construct_insertion_map(isl_union_map_get_space(options
), pos
);
1438 insertion
= isl_union_map_empty(isl_union_map_get_space(options
));
1440 for (type
= atomic
; type
<= separate
; ++type
) {
1441 isl_map
*map_type
= isl_map_copy(map
);
1442 const char *name
= option_str
[type
];
1443 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_in
, name
);
1444 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_out
, name
);
1445 insertion
= isl_union_map_add_map(insertion
, map_type
);
1448 map
= isl_map_product(map
, isl_map_identity(isl_map_get_space(map
)));
1449 map
= isl_map_set_tuple_name(map
, isl_dim_in
, name
);
1450 map
= isl_map_set_tuple_name(map
, isl_dim_out
, name
);
1451 insertion
= isl_union_map_add_map(insertion
, map
);
1453 options
= isl_union_map_apply_range(options
, insertion
);
1458 /* Insert a single dimension in the schedule domain at position "pos".
1459 * The new dimension is given an isl_id with the empty string as name.
1461 * The main difficulty is updating build->options to reflect the
1462 * extra dimension. This is handled in options_insert_dim.
1464 * Note that because of the dimension manipulations, the resulting
1465 * schedule domain space will always be unnamed and unstructured.
1466 * However, the original schedule domain space may be named and/or
1467 * structured, so we have to take this possibility into account
1468 * while performing the transformations.
1470 __isl_give isl_ast_build
*isl_ast_build_insert_dim(
1471 __isl_take isl_ast_build
*build
, int pos
)
1474 isl_space
*space
, *ma_space
;
1478 build
= isl_ast_build_cow(build
);
1482 ctx
= isl_ast_build_get_ctx(build
);
1483 id
= isl_id_alloc(ctx
, "", NULL
);
1484 space
= isl_ast_build_get_space(build
, 1);
1485 build
->iterators
= isl_id_list_insert(build
->iterators
, pos
, id
);
1486 build
->domain
= isl_set_insert_dims(build
->domain
,
1487 isl_dim_set
, pos
, 1);
1488 build
->generated
= isl_set_insert_dims(build
->generated
,
1489 isl_dim_set
, pos
, 1);
1490 build
->pending
= isl_set_insert_dims(build
->pending
,
1491 isl_dim_set
, pos
, 1);
1492 build
->strides
= isl_vec_insert_els(build
->strides
, pos
, 1);
1493 build
->strides
= isl_vec_set_element_si(build
->strides
, pos
, 1);
1494 ma_space
= isl_space_params(isl_multi_aff_get_space(build
->offsets
));
1495 ma_space
= isl_space_set_from_params(ma_space
);
1496 ma_space
= isl_space_add_dims(ma_space
, isl_dim_set
, 1);
1497 ma_space
= isl_space_map_from_set(ma_space
);
1498 ma
= isl_multi_aff_zero(isl_space_copy(ma_space
));
1499 build
->offsets
= isl_multi_aff_splice(build
->offsets
, pos
, pos
, ma
);
1500 ma
= isl_multi_aff_identity(ma_space
);
1501 build
->values
= isl_multi_aff_splice(build
->values
, pos
, pos
, ma
);
1502 build
->options
= options_insert_dim(build
->options
, space
, pos
);
1504 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1505 !build
->pending
|| !build
->values
||
1506 !build
->strides
|| !build
->offsets
|| !build
->options
)
1507 return isl_ast_build_free(build
);
1512 /* Scale down the current dimension by a factor of "m".
1513 * "umap" is an isl_union_map that implements the scaling down.
1514 * That is, it is of the form
1516 * { [.... i ....] -> [.... i' ....] : i = m i' }
1518 * This function is called right after the strides have been
1519 * detected, but before any constraints on the current dimension
1520 * have been included in build->domain.
1521 * We therefore only need to update stride, offset and the options.
1523 __isl_give isl_ast_build
*isl_ast_build_scale_down(
1524 __isl_take isl_ast_build
*build
, __isl_take isl_val
*m
,
1525 __isl_take isl_union_map
*umap
)
1531 build
= isl_ast_build_cow(build
);
1532 if (!build
|| !umap
|| !m
)
1535 depth
= build
->depth
;
1537 v
= isl_vec_get_element_val(build
->strides
, depth
);
1538 v
= isl_val_div(v
, isl_val_copy(m
));
1539 build
->strides
= isl_vec_set_element_val(build
->strides
, depth
, v
);
1541 aff
= isl_multi_aff_get_aff(build
->offsets
, depth
);
1542 aff
= isl_aff_scale_down_val(aff
, m
);
1543 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, depth
, aff
);
1544 build
->options
= isl_union_map_apply_domain(build
->options
, umap
);
1545 if (!build
->strides
|| !build
->offsets
|| !build
->options
)
1546 return isl_ast_build_free(build
);
1551 isl_union_map_free(umap
);
1552 return isl_ast_build_free(build
);
1555 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1556 * If an isl_id with such a name already appears among the parameters
1557 * in build->domain, then adjust the name to "c%d_%d".
1559 static __isl_give isl_id_list
*generate_names(isl_ctx
*ctx
, int n
, int first
,
1560 __isl_keep isl_ast_build
*build
)
1565 names
= isl_id_list_alloc(ctx
, n
);
1566 for (i
= 0; i
< n
; ++i
) {
1569 id
= generate_name(ctx
, first
+ i
, build
);
1570 names
= isl_id_list_add(names
, id
);
1576 /* Embed "options" into the given isl_ast_build space.
1578 * This function is called from within a nested call to
1579 * isl_ast_build_ast_from_schedule.
1580 * "options" refers to the additional schedule,
1581 * while space refers to both the space of the outer isl_ast_build and
1582 * that of the additional schedule.
1583 * Specifically, space is of the form
1587 * while options lives in the space(s)
1595 * and compose this with options, to obtain the new options
1596 * living in the space(s)
1600 static __isl_give isl_union_map
*embed_options(
1601 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
)
1605 map
= isl_map_universe(isl_space_unwrap(space
));
1606 map
= isl_map_range_map(map
);
1608 options
= isl_union_map_apply_range(
1609 isl_union_map_from_map(map
), options
);
1614 /* Update "build" for use in a (possibly nested) code generation. That is,
1615 * extend "build" from an AST build on some domain O to an AST build
1616 * on domain [O -> S], with S corresponding to "space".
1617 * If the original domain is a parameter domain, then the new domain is
1619 * "iterators" is a list of iterators for S, but the number of elements
1620 * may be smaller or greater than the number of set dimensions of S.
1621 * If "keep_iterators" is set, then any extra ids in build->iterators
1622 * are reused for S. Otherwise, these extra ids are dropped.
1624 * We first update build->outer_pos to the current depth.
1625 * This depth is zero in case this is the outermost code generation.
1627 * We then add additional ids such that the number of iterators is at least
1628 * equal to the dimension of the new build domain.
1630 * If the original domain is parametric, then we are constructing
1631 * an isl_ast_build for the outer code generation and we pass control
1632 * to isl_ast_build_init.
1634 * Otherwise, we adjust the fields of "build" to include "space".
1636 __isl_give isl_ast_build
*isl_ast_build_product(
1637 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
1642 isl_multi_aff
*embedding
;
1645 build
= isl_ast_build_cow(build
);
1649 build
->outer_pos
= build
->depth
;
1651 ctx
= isl_ast_build_get_ctx(build
);
1652 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
1653 dim
+= isl_space_dim(space
, isl_dim_set
);
1654 n_it
= isl_id_list_n_id(build
->iterators
);
1657 l
= generate_names(ctx
, dim
- n_it
, n_it
, build
);
1658 build
->iterators
= isl_id_list_concat(build
->iterators
, l
);
1661 if (isl_set_is_params(build
->domain
))
1662 return isl_ast_build_init(build
, space
);
1664 set
= isl_set_universe(isl_space_copy(space
));
1665 build
->domain
= isl_set_product(build
->domain
, isl_set_copy(set
));
1666 build
->pending
= isl_set_product(build
->pending
, isl_set_copy(set
));
1667 build
->generated
= isl_set_product(build
->generated
, set
);
1669 strides
= isl_vec_alloc(ctx
, isl_space_dim(space
, isl_dim_set
));
1670 strides
= isl_vec_set_si(strides
, 1);
1671 build
->strides
= isl_vec_concat(build
->strides
, strides
);
1673 space
= isl_space_map_from_set(space
);
1674 build
->offsets
= isl_multi_aff_align_params(build
->offsets
,
1675 isl_space_copy(space
));
1676 build
->offsets
= isl_multi_aff_product(build
->offsets
,
1677 isl_multi_aff_zero(isl_space_copy(space
)));
1678 build
->values
= isl_multi_aff_align_params(build
->values
,
1679 isl_space_copy(space
));
1680 embedding
= isl_multi_aff_identity(space
);
1681 build
->values
= isl_multi_aff_product(build
->values
, embedding
);
1683 space
= isl_ast_build_get_space(build
, 1);
1684 build
->options
= embed_options(build
->options
, space
);
1686 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1687 !build
->pending
|| !build
->values
||
1688 !build
->strides
|| !build
->offsets
|| !build
->options
)
1689 return isl_ast_build_free(build
);
1693 isl_ast_build_free(build
);
1694 isl_space_free(space
);
1698 /* Does "aff" only attain non-negative values over build->domain?
1699 * That is, does it not attain any negative values?
1701 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build
*build
,
1702 __isl_keep isl_aff
*aff
)
1710 aff
= isl_aff_copy(aff
);
1711 test
= isl_set_from_basic_set(isl_aff_neg_basic_set(aff
));
1712 test
= isl_set_intersect(test
, isl_set_copy(build
->domain
));
1713 empty
= isl_set_is_empty(test
);
1719 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1721 int isl_ast_build_has_stride(__isl_keep isl_ast_build
*build
, int pos
)
1729 v
= isl_vec_get_element_val(build
->strides
, pos
);
1732 has_stride
= !isl_val_is_one(v
);
1738 /* Given that the dimension at position "pos" takes on values
1742 * with a an integer, return s through *stride.
1744 __isl_give isl_val
*isl_ast_build_get_stride(__isl_keep isl_ast_build
*build
,
1750 return isl_vec_get_element_val(build
->strides
, pos
);
1753 /* Given that the dimension at position "pos" takes on values
1757 * with a an integer, return f.
1759 __isl_give isl_aff
*isl_ast_build_get_offset(
1760 __isl_keep isl_ast_build
*build
, int pos
)
1765 return isl_multi_aff_get_aff(build
->offsets
, pos
);
1768 /* Is the dimension at position "pos" known to attain only a single
1769 * value that, moreover, can be described by a single affine expression
1770 * in terms of the outer dimensions and parameters?
1772 * If not, then the correponding affine expression in build->values
1773 * is set to be equal to the same input dimension.
1774 * Otherwise, it is set to the requested expression in terms of
1775 * outer dimensions and parameters.
1777 int isl_ast_build_has_affine_value(__isl_keep isl_ast_build
*build
,
1786 aff
= isl_multi_aff_get_aff(build
->values
, pos
);
1787 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, pos
, 1);
1796 /* Plug in the known values (fixed affine expressions in terms of
1797 * parameters and outer loop iterators) of all loop iterators
1798 * in the domain of "umap".
1800 * We simply precompose "umap" with build->values.
1802 __isl_give isl_union_map
*isl_ast_build_substitute_values_union_map_domain(
1803 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*umap
)
1805 isl_multi_aff
*values
;
1808 return isl_union_map_free(umap
);
1810 values
= isl_multi_aff_copy(build
->values
);
1811 umap
= isl_union_map_preimage_domain_multi_aff(umap
, values
);
1816 /* Is the current dimension known to attain only a single value?
1818 int isl_ast_build_has_value(__isl_keep isl_ast_build
*build
)
1823 return build
->value
!= NULL
;
1826 /* Simplify the basic set "bset" based on what we know about
1827 * the iterators of already generated loops.
1829 * "bset" is assumed to live in the (internal) schedule domain.
1831 __isl_give isl_basic_set
*isl_ast_build_compute_gist_basic_set(
1832 __isl_keep isl_ast_build
*build
, __isl_take isl_basic_set
*bset
)
1837 bset
= isl_basic_set_preimage_multi_aff(bset
,
1838 isl_multi_aff_copy(build
->values
));
1839 bset
= isl_basic_set_gist(bset
,
1840 isl_set_simple_hull(isl_set_copy(build
->domain
)));
1844 isl_basic_set_free(bset
);
1848 /* Simplify the set "set" based on what we know about
1849 * the iterators of already generated loops.
1851 * "set" is assumed to live in the (internal) schedule domain.
1853 __isl_give isl_set
*isl_ast_build_compute_gist(
1854 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
1859 set
= isl_set_preimage_multi_aff(set
,
1860 isl_multi_aff_copy(build
->values
));
1861 set
= isl_set_gist(set
, isl_set_copy(build
->domain
));
1869 /* Simplify the map "map" based on what we know about
1870 * the iterators of already generated loops.
1872 * The domain of "map" is assumed to live in the (internal) schedule domain.
1874 __isl_give isl_map
*isl_ast_build_compute_gist_map_domain(
1875 __isl_keep isl_ast_build
*build
, __isl_take isl_map
*map
)
1880 map
= isl_map_gist_domain(map
, isl_set_copy(build
->domain
));
1888 /* Simplify the affine expression "aff" based on what we know about
1889 * the iterators of already generated loops.
1891 * The domain of "aff" is assumed to live in the (internal) schedule domain.
1893 __isl_give isl_aff
*isl_ast_build_compute_gist_aff(
1894 __isl_keep isl_ast_build
*build
, __isl_take isl_aff
*aff
)
1899 aff
= isl_aff_gist(aff
, isl_set_copy(build
->domain
));
1907 /* Simplify the piecewise affine expression "aff" based on what we know about
1908 * the iterators of already generated loops.
1910 * The domain of "pa" is assumed to live in the (internal) schedule domain.
1912 __isl_give isl_pw_aff
*isl_ast_build_compute_gist_pw_aff(
1913 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_aff
*pa
)
1918 if (!isl_set_is_params(build
->domain
))
1919 pa
= isl_pw_aff_pullback_multi_aff(pa
,
1920 isl_multi_aff_copy(build
->values
));
1921 pa
= isl_pw_aff_gist(pa
, isl_set_copy(build
->domain
));
1925 isl_pw_aff_free(pa
);
1929 /* Simplify the piecewise multi-affine expression "aff" based on what
1930 * we know about the iterators of already generated loops.
1932 * The domain of "pma" is assumed to live in the (internal) schedule domain.
1934 __isl_give isl_pw_multi_aff
*isl_ast_build_compute_gist_pw_multi_aff(
1935 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_multi_aff
*pma
)
1940 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
,
1941 isl_multi_aff_copy(build
->values
));
1942 pma
= isl_pw_multi_aff_gist(pma
, isl_set_copy(build
->domain
));
1946 isl_pw_multi_aff_free(pma
);
1950 /* Extract the schedule domain of the given type from build->options
1951 * at the current depth.
1953 * In particular, find the subset of build->options that is of
1954 * the following form
1956 * schedule_domain -> type[depth]
1958 * and return the corresponding domain, after eliminating inner dimensions
1959 * and divs that depend on the current dimension.
1961 * Note that the domain of build->options has been reformulated
1962 * in terms of the internal build space in embed_options,
1963 * but the position is still that within the current code generation.
1965 __isl_give isl_set
*isl_ast_build_get_option_domain(
1966 __isl_keep isl_ast_build
*build
,
1967 enum isl_ast_build_domain_type type
)
1978 name
= option_str
[type
];
1979 local_pos
= build
->depth
- build
->outer_pos
;
1981 space
= isl_ast_build_get_space(build
, 1);
1982 space
= isl_space_from_domain(space
);
1983 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
1984 space
= isl_space_set_tuple_name(space
, isl_dim_out
, name
);
1986 option
= isl_union_map_extract_map(build
->options
, space
);
1987 option
= isl_map_fix_si(option
, isl_dim_out
, 0, local_pos
);
1989 domain
= isl_map_domain(option
);
1990 domain
= isl_ast_build_eliminate(build
, domain
);
1995 /* Extract the separation class mapping at the current depth.
1997 * In particular, find and return the subset of build->options that is of
1998 * the following form
2000 * schedule_domain -> separation_class[[depth] -> [class]]
2002 * The caller is expected to eliminate inner dimensions from the domain.
2004 * Note that the domain of build->options has been reformulated
2005 * in terms of the internal build space in embed_options,
2006 * but the position is still that within the current code generation.
2008 __isl_give isl_map
*isl_ast_build_get_separation_class(
2009 __isl_keep isl_ast_build
*build
)
2012 isl_space
*space_sep
, *space
;
2019 local_pos
= build
->depth
- build
->outer_pos
;
2020 ctx
= isl_ast_build_get_ctx(build
);
2021 space_sep
= isl_space_alloc(ctx
, 0, 1, 1);
2022 space_sep
= isl_space_wrap(space_sep
);
2023 space_sep
= isl_space_set_tuple_name(space_sep
, isl_dim_set
,
2024 "separation_class");
2025 space
= isl_ast_build_get_space(build
, 1);
2026 space_sep
= isl_space_align_params(space_sep
, isl_space_copy(space
));
2027 space
= isl_space_map_from_domain_and_range(space
, space_sep
);
2029 res
= isl_union_map_extract_map(build
->options
, space
);
2030 res
= isl_map_fix_si(res
, isl_dim_out
, 0, local_pos
);
2031 res
= isl_map_coalesce(res
);
2036 /* Eliminate dimensions inner to the current dimension.
2038 __isl_give isl_set
*isl_ast_build_eliminate_inner(
2039 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2045 return isl_set_free(set
);
2047 dim
= isl_set_dim(set
, isl_dim_set
);
2048 depth
= build
->depth
;
2049 set
= isl_set_detect_equalities(set
);
2050 set
= isl_set_eliminate(set
, isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2055 /* Eliminate unknown divs and divs that depend on the current dimension.
2057 * Note that during the elimination of unknown divs, we may discover
2058 * an explicit representation of some other unknown divs, which may
2059 * depend on the current dimension. We therefore need to eliminate
2060 * unknown divs first.
2062 __isl_give isl_set
*isl_ast_build_eliminate_divs(
2063 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2068 return isl_set_free(set
);
2070 set
= isl_set_remove_unknown_divs(set
);
2071 depth
= build
->depth
;
2072 set
= isl_set_remove_divs_involving_dims(set
, isl_dim_set
, depth
, 1);
2077 /* Eliminate dimensions inner to the current dimension as well as
2078 * unknown divs and divs that depend on the current dimension.
2079 * The result then consists only of constraints that are independent
2080 * of the current dimension and upper and lower bounds on the current
2083 __isl_give isl_set
*isl_ast_build_eliminate(
2084 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*domain
)
2086 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2087 domain
= isl_ast_build_eliminate_divs(build
, domain
);
2091 /* Replace build->single_valued by "sv".
2093 __isl_give isl_ast_build
*isl_ast_build_set_single_valued(
2094 __isl_take isl_ast_build
*build
, int sv
)
2098 if (build
->single_valued
== sv
)
2100 build
= isl_ast_build_cow(build
);
2103 build
->single_valued
= sv
;