2 * Copyright 2012 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
16 #include <isl_ast_build_private.h>
17 #include <isl_ast_private.h>
19 /* Construct a map that isolates the current dimension.
21 * Essentially, the current dimension of "set" is moved to the single output
22 * dimension in the result, with the current dimension in the domain replaced
23 * by an unconstrained variable.
25 __isl_give isl_map
*isl_ast_build_map_to_iterator(
26 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
30 map
= isl_map_from_domain(set
);
31 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
34 return isl_map_free(map
);
36 map
= isl_map_equate(map
, isl_dim_in
, build
->depth
, isl_dim_out
, 0);
37 map
= isl_map_eliminate(map
, isl_dim_in
, build
->depth
, 1);
42 /* Initialize the information derived during the AST generation to default
43 * values for a schedule domain in "space".
45 * We also check that the remaining fields are not NULL so that
46 * the calling functions don't have to perform this test.
48 static __isl_give isl_ast_build
*isl_ast_build_init_derived(
49 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
54 build
= isl_ast_build_cow(build
);
55 if (!build
|| !build
->domain
)
58 ctx
= isl_ast_build_get_ctx(build
);
59 strides
= isl_vec_alloc(ctx
, isl_space_dim(space
, isl_dim_set
));
60 strides
= isl_vec_set_si(strides
, 1);
62 isl_vec_free(build
->strides
);
63 build
->strides
= strides
;
65 space
= isl_space_map_from_set(space
);
66 isl_multi_aff_free(build
->offsets
);
67 build
->offsets
= isl_multi_aff_zero(isl_space_copy(space
));
68 isl_multi_aff_free(build
->values
);
69 build
->values
= isl_multi_aff_identity(space
);
71 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
72 !build
->pending
|| !build
->values
||
73 !build
->strides
|| !build
->offsets
|| !build
->options
)
74 return isl_ast_build_free(build
);
78 isl_space_free(space
);
79 return isl_ast_build_free(build
);
82 /* Return an isl_id called "c%d", with "%d" set to "i".
83 * If an isl_id with such a name already appears among the parameters
84 * in build->domain, then adjust the name to "c%d_%d".
86 static __isl_give isl_id
*generate_name(isl_ctx
*ctx
, int i
,
87 __isl_keep isl_ast_build
*build
)
91 isl_set
*dom
= build
->domain
;
93 snprintf(name
, sizeof(name
), "c%d", i
);
95 while (isl_set_find_dim_by_name(dom
, isl_dim_param
, name
) >= 0)
96 snprintf(name
, sizeof(name
), "c%d_%d", i
, j
++);
97 return isl_id_alloc(ctx
, name
, NULL
);
100 /* Create an isl_ast_build with "set" as domain.
102 * The input set is usually a parameter domain, but we currently allow it to
103 * be any kind of set. We set the domain of the returned isl_ast_build
104 * to "set" and initialize all the other fields to default values.
106 __isl_give isl_ast_build
*isl_ast_build_from_context(__isl_take isl_set
*set
)
111 isl_ast_build
*build
;
113 set
= isl_set_compute_divs(set
);
117 ctx
= isl_set_get_ctx(set
);
119 build
= isl_calloc_type(ctx
, isl_ast_build
);
125 build
->generated
= isl_set_copy(build
->domain
);
126 build
->pending
= isl_set_universe(isl_set_get_space(build
->domain
));
127 build
->options
= isl_union_map_empty(isl_space_params_alloc(ctx
, 0));
128 n
= isl_set_dim(set
, isl_dim_set
);
130 build
->iterators
= isl_id_list_alloc(ctx
, n
);
131 for (i
= 0; i
< n
; ++i
) {
133 if (isl_set_has_dim_id(set
, isl_dim_set
, i
))
134 id
= isl_set_get_dim_id(set
, isl_dim_set
, i
);
136 id
= generate_name(ctx
, i
, build
);
137 build
->iterators
= isl_id_list_add(build
->iterators
, id
);
139 space
= isl_set_get_space(set
);
140 if (isl_space_is_params(space
))
141 space
= isl_space_set_from_params(space
);
143 return isl_ast_build_init_derived(build
, space
);
149 __isl_give isl_ast_build
*isl_ast_build_copy(__isl_keep isl_ast_build
*build
)
158 __isl_give isl_ast_build
*isl_ast_build_dup(__isl_keep isl_ast_build
*build
)
166 ctx
= isl_ast_build_get_ctx(build
);
167 dup
= isl_calloc_type(ctx
, isl_ast_build
);
172 dup
->outer_pos
= build
->outer_pos
;
173 dup
->depth
= build
->depth
;
174 dup
->iterators
= isl_id_list_copy(build
->iterators
);
175 dup
->domain
= isl_set_copy(build
->domain
);
176 dup
->generated
= isl_set_copy(build
->generated
);
177 dup
->pending
= isl_set_copy(build
->pending
);
178 dup
->values
= isl_multi_aff_copy(build
->values
);
179 dup
->value
= isl_pw_aff_copy(build
->value
);
180 dup
->strides
= isl_vec_copy(build
->strides
);
181 dup
->offsets
= isl_multi_aff_copy(build
->offsets
);
182 dup
->executed
= isl_union_map_copy(build
->executed
);
183 dup
->single_valued
= build
->single_valued
;
184 dup
->options
= isl_union_map_copy(build
->options
);
185 dup
->at_each_domain
= build
->at_each_domain
;
186 dup
->at_each_domain_user
= build
->at_each_domain_user
;
187 dup
->before_each_for
= build
->before_each_for
;
188 dup
->before_each_for_user
= build
->before_each_for_user
;
189 dup
->after_each_for
= build
->after_each_for
;
190 dup
->after_each_for_user
= build
->after_each_for_user
;
191 dup
->create_leaf
= build
->create_leaf
;
192 dup
->create_leaf_user
= build
->create_leaf_user
;
194 if (!dup
->iterators
|| !dup
->domain
|| !dup
->generated
||
195 !dup
->pending
|| !dup
->values
||
196 !dup
->strides
|| !dup
->offsets
|| !dup
->options
||
197 (build
->executed
&& !dup
->executed
) ||
198 (build
->value
&& !dup
->value
))
199 return isl_ast_build_free(dup
);
204 /* Align the parameters of "build" to those of "model", introducing
205 * additional parameters if needed.
207 __isl_give isl_ast_build
*isl_ast_build_align_params(
208 __isl_take isl_ast_build
*build
, __isl_take isl_space
*model
)
210 build
= isl_ast_build_cow(build
);
214 build
->domain
= isl_set_align_params(build
->domain
,
215 isl_space_copy(model
));
216 build
->generated
= isl_set_align_params(build
->generated
,
217 isl_space_copy(model
));
218 build
->pending
= isl_set_align_params(build
->pending
,
219 isl_space_copy(model
));
220 build
->values
= isl_multi_aff_align_params(build
->values
,
221 isl_space_copy(model
));
222 build
->offsets
= isl_multi_aff_align_params(build
->offsets
,
223 isl_space_copy(model
));
224 build
->options
= isl_union_map_align_params(build
->options
,
225 isl_space_copy(model
));
226 isl_space_free(model
);
228 if (!build
->domain
|| !build
->values
|| !build
->offsets
||
230 return isl_ast_build_free(build
);
234 isl_space_free(model
);
238 __isl_give isl_ast_build
*isl_ast_build_cow(__isl_take isl_ast_build
*build
)
246 return isl_ast_build_dup(build
);
249 __isl_null isl_ast_build
*isl_ast_build_free(
250 __isl_take isl_ast_build
*build
)
255 if (--build
->ref
> 0)
258 isl_id_list_free(build
->iterators
);
259 isl_set_free(build
->domain
);
260 isl_set_free(build
->generated
);
261 isl_set_free(build
->pending
);
262 isl_multi_aff_free(build
->values
);
263 isl_pw_aff_free(build
->value
);
264 isl_vec_free(build
->strides
);
265 isl_multi_aff_free(build
->offsets
);
266 isl_multi_aff_free(build
->schedule_map
);
267 isl_union_map_free(build
->executed
);
268 isl_union_map_free(build
->options
);
275 isl_ctx
*isl_ast_build_get_ctx(__isl_keep isl_ast_build
*build
)
277 return build
? isl_set_get_ctx(build
->domain
) : NULL
;
280 /* Replace build->options by "options".
282 __isl_give isl_ast_build
*isl_ast_build_set_options(
283 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*options
)
285 build
= isl_ast_build_cow(build
);
287 if (!build
|| !options
)
290 isl_union_map_free(build
->options
);
291 build
->options
= options
;
295 isl_union_map_free(options
);
296 return isl_ast_build_free(build
);
299 /* Set the iterators for the next code generation.
301 * If we still have some iterators left from the previous code generation
302 * (if any) or if iterators have already been set by a previous
303 * call to this function, then we remove them first.
305 __isl_give isl_ast_build
*isl_ast_build_set_iterators(
306 __isl_take isl_ast_build
*build
, __isl_take isl_id_list
*iterators
)
310 build
= isl_ast_build_cow(build
);
314 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
315 n_it
= isl_id_list_n_id(build
->iterators
);
317 isl_die(isl_ast_build_get_ctx(build
), isl_error_internal
,
318 "isl_ast_build in inconsistent state", goto error
);
320 build
->iterators
= isl_id_list_drop(build
->iterators
,
322 build
->iterators
= isl_id_list_concat(build
->iterators
, iterators
);
323 if (!build
->iterators
)
324 return isl_ast_build_free(build
);
328 isl_id_list_free(iterators
);
329 return isl_ast_build_free(build
);
332 /* Set the "at_each_domain" callback of "build" to "fn".
334 __isl_give isl_ast_build
*isl_ast_build_set_at_each_domain(
335 __isl_take isl_ast_build
*build
,
336 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_node
*node
,
337 __isl_keep isl_ast_build
*build
, void *user
), void *user
)
339 build
= isl_ast_build_cow(build
);
344 build
->at_each_domain
= fn
;
345 build
->at_each_domain_user
= user
;
350 /* Set the "before_each_for" callback of "build" to "fn".
352 __isl_give isl_ast_build
*isl_ast_build_set_before_each_for(
353 __isl_take isl_ast_build
*build
,
354 __isl_give isl_id
*(*fn
)(__isl_keep isl_ast_build
*build
,
355 void *user
), void *user
)
357 build
= isl_ast_build_cow(build
);
362 build
->before_each_for
= fn
;
363 build
->before_each_for_user
= user
;
368 /* Set the "after_each_for" callback of "build" to "fn".
370 __isl_give isl_ast_build
*isl_ast_build_set_after_each_for(
371 __isl_take isl_ast_build
*build
,
372 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_node
*node
,
373 __isl_keep isl_ast_build
*build
, void *user
), void *user
)
375 build
= isl_ast_build_cow(build
);
380 build
->after_each_for
= fn
;
381 build
->after_each_for_user
= user
;
386 /* Set the "create_leaf" callback of "build" to "fn".
388 __isl_give isl_ast_build
*isl_ast_build_set_create_leaf(
389 __isl_take isl_ast_build
*build
,
390 __isl_give isl_ast_node
*(*fn
)(__isl_take isl_ast_build
*build
,
391 void *user
), void *user
)
393 build
= isl_ast_build_cow(build
);
398 build
->create_leaf
= fn
;
399 build
->create_leaf_user
= user
;
404 /* Clear all information that is specific to this code generation
405 * and that is (probably) not meaningful to any nested code generation.
407 __isl_give isl_ast_build
*isl_ast_build_clear_local_info(
408 __isl_take isl_ast_build
*build
)
412 build
= isl_ast_build_cow(build
);
416 space
= isl_union_map_get_space(build
->options
);
417 isl_union_map_free(build
->options
);
418 build
->options
= isl_union_map_empty(space
);
420 build
->at_each_domain
= NULL
;
421 build
->at_each_domain_user
= NULL
;
422 build
->before_each_for
= NULL
;
423 build
->before_each_for_user
= NULL
;
424 build
->after_each_for
= NULL
;
425 build
->after_each_for_user
= NULL
;
426 build
->create_leaf
= NULL
;
427 build
->create_leaf_user
= NULL
;
430 return isl_ast_build_free(build
);
435 /* Have any loops been eliminated?
436 * That is, do any of the original schedule dimensions have a fixed
437 * value that has been substituted?
439 static int any_eliminated(isl_ast_build
*build
)
443 for (i
= 0; i
< build
->depth
; ++i
)
444 if (isl_ast_build_has_affine_value(build
, i
))
450 /* Clear build->schedule_map.
451 * This function should be called whenever anything that might affect
452 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
453 * In particular, it should be called when the depth is changed or
454 * when an iterator is determined to have a fixed value.
456 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build
*build
)
460 isl_multi_aff_free(build
->schedule_map
);
461 build
->schedule_map
= NULL
;
464 /* Do we need a (non-trivial) schedule map?
465 * That is, is the internal schedule space different from
466 * the external schedule space?
468 * The internal and external schedule spaces are only the same
469 * if code has been generated for the entire schedule and if none
470 * of the loops have been eliminated.
472 __isl_give
int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build
*build
)
479 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
480 return build
->depth
!= dim
|| any_eliminated(build
);
483 /* Return a mapping from the internal schedule space to the external
484 * schedule space in the form of an isl_multi_aff.
485 * The internal schedule space originally corresponds to that of the
486 * input schedule. This may change during the code generation if
487 * if isl_ast_build_insert_dim is ever called.
488 * The external schedule space corresponds to the
489 * loops that have been generated.
491 * Currently, the only difference between the internal schedule domain
492 * and the external schedule domain is that some dimensions are projected
493 * out in the external schedule domain. In particular, the dimensions
494 * for which no code has been generated yet and the dimensions that correspond
495 * to eliminated loops.
497 * We cache a copy of the schedule_map in build->schedule_map.
498 * The cache is cleared through isl_ast_build_reset_schedule_map
499 * whenever anything changes that might affect the result of this function.
501 __isl_give isl_multi_aff
*isl_ast_build_get_schedule_map_multi_aff(
502 __isl_keep isl_ast_build
*build
)
509 if (build
->schedule_map
)
510 return isl_multi_aff_copy(build
->schedule_map
);
512 space
= isl_ast_build_get_space(build
, 1);
513 space
= isl_space_map_from_set(space
);
514 ma
= isl_multi_aff_identity(space
);
515 if (isl_ast_build_need_schedule_map(build
)) {
517 int dim
= isl_set_dim(build
->domain
, isl_dim_set
);
518 ma
= isl_multi_aff_drop_dims(ma
, isl_dim_out
,
519 build
->depth
, dim
- build
->depth
);
520 for (i
= build
->depth
- 1; i
>= 0; --i
)
521 if (isl_ast_build_has_affine_value(build
, i
))
522 ma
= isl_multi_aff_drop_dims(ma
,
526 build
->schedule_map
= ma
;
527 return isl_multi_aff_copy(build
->schedule_map
);
530 /* Return a mapping from the internal schedule space to the external
531 * schedule space in the form of an isl_map.
533 __isl_give isl_map
*isl_ast_build_get_schedule_map(
534 __isl_keep isl_ast_build
*build
)
538 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
539 return isl_map_from_multi_aff(ma
);
542 /* Return the position of the dimension in build->domain for which
543 * an AST node is currently being generated.
545 int isl_ast_build_get_depth(__isl_keep isl_ast_build
*build
)
547 return build
? build
->depth
: -1;
550 /* Prepare for generating code for the next level.
551 * In particular, increase the depth and reset any information
552 * that is local to the current depth.
554 __isl_give isl_ast_build
*isl_ast_build_increase_depth(
555 __isl_take isl_ast_build
*build
)
557 build
= isl_ast_build_cow(build
);
561 isl_ast_build_reset_schedule_map(build
);
562 build
->value
= isl_pw_aff_free(build
->value
);
566 void isl_ast_build_dump(__isl_keep isl_ast_build
*build
)
571 fprintf(stderr
, "domain: ");
572 isl_set_dump(build
->domain
);
573 fprintf(stderr
, "generated: ");
574 isl_set_dump(build
->generated
);
575 fprintf(stderr
, "pending: ");
576 isl_set_dump(build
->pending
);
577 fprintf(stderr
, "iterators: ");
578 isl_id_list_dump(build
->iterators
);
579 fprintf(stderr
, "values: ");
580 isl_multi_aff_dump(build
->values
);
582 fprintf(stderr
, "value: ");
583 isl_pw_aff_dump(build
->value
);
585 fprintf(stderr
, "strides: ");
586 isl_vec_dump(build
->strides
);
587 fprintf(stderr
, "offsets: ");
588 isl_multi_aff_dump(build
->offsets
);
591 /* Initialize "build" for AST construction in schedule space "space"
592 * in the case that build->domain is a parameter set.
594 * build->iterators is assumed to have been updated already.
596 static __isl_give isl_ast_build
*isl_ast_build_init(
597 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
601 build
= isl_ast_build_cow(build
);
605 set
= isl_set_universe(isl_space_copy(space
));
606 build
->domain
= isl_set_intersect_params(isl_set_copy(set
),
608 build
->pending
= isl_set_intersect_params(isl_set_copy(set
),
610 build
->generated
= isl_set_intersect_params(set
, build
->generated
);
612 return isl_ast_build_init_derived(build
, space
);
614 isl_ast_build_free(build
);
615 isl_space_free(space
);
619 /* Assign "aff" to *user and return -1, effectively extracting
620 * the first (and presumably only) affine expression in the isl_pw_aff
621 * on which this function is used.
623 static int extract_single_piece(__isl_take isl_set
*set
,
624 __isl_take isl_aff
*aff
, void *user
)
634 /* Intersect "set" with the stride constraint of "build", if any.
636 static __isl_give isl_set
*intersect_stride_constraint(__isl_take isl_set
*set
,
637 __isl_keep isl_ast_build
*build
)
642 return isl_set_free(set
);
643 if (!isl_ast_build_has_stride(build
, build
->depth
))
646 stride
= isl_ast_build_get_stride_constraint(build
);
647 return isl_set_intersect(set
, stride
);
650 /* Check if the given bounds on the current dimension (together with
651 * the stride constraint, if any) imply that
652 * this current dimension attains only a single value (in terms of
653 * parameters and outer dimensions).
654 * If so, we record it in build->value.
655 * If, moreover, this value can be represented as a single affine expression,
656 * then we also update build->values, effectively marking the current
657 * dimension as "eliminated".
659 * When computing the gist of the fixed value that can be represented
660 * as a single affine expression, it is important to only take into
661 * account the domain constraints in the original AST build and
662 * not the domain of the affine expression itself.
663 * Otherwise, a [i/3] is changed into a i/3 because we know that i
664 * is a multiple of 3, but then we end up not expressing anywhere
665 * in the context that i is a multiple of 3.
667 static __isl_give isl_ast_build
*update_values(
668 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*bounds
)
671 isl_pw_multi_aff
*pma
;
676 set
= isl_set_from_basic_set(bounds
);
677 set
= isl_set_intersect(set
, isl_set_copy(build
->domain
));
678 set
= intersect_stride_constraint(set
, build
);
679 it_map
= isl_ast_build_map_to_iterator(build
, set
);
681 sv
= isl_map_is_single_valued(it_map
);
683 build
= isl_ast_build_free(build
);
685 isl_map_free(it_map
);
689 pma
= isl_pw_multi_aff_from_map(it_map
);
690 build
->value
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
691 build
->value
= isl_ast_build_compute_gist_pw_aff(build
, build
->value
);
692 build
->value
= isl_pw_aff_coalesce(build
->value
);
693 isl_pw_multi_aff_free(pma
);
696 return isl_ast_build_free(build
);
698 if (isl_pw_aff_n_piece(build
->value
) != 1)
701 isl_pw_aff_foreach_piece(build
->value
, &extract_single_piece
, &aff
);
703 build
->values
= isl_multi_aff_set_aff(build
->values
, build
->depth
, aff
);
705 return isl_ast_build_free(build
);
706 isl_ast_build_reset_schedule_map(build
);
710 /* Update the AST build based on the given loop bounds for
711 * the current dimension and the stride information available in the build.
713 * We first make sure that the bounds do not refer to any iterators
714 * that have already been eliminated.
715 * Then, we check if the bounds imply that the current iterator
717 * If they do and if this fixed value can be expressed as a single
718 * affine expression, we eliminate the iterators from the bounds.
719 * Note that we cannot simply plug in this single value using
720 * isl_basic_set_preimage_multi_aff as the single value may only
721 * be defined on a subset of the domain. Plugging in the value
722 * would restrict the build domain to this subset, while this
723 * restriction may not be reflected in the generated code.
724 * Finally, we intersect build->domain with the updated bounds.
725 * We also add the stride constraint unless we have been able
726 * to find a fixed value expressed as a single affine expression.
728 * Note that the check for a fixed value in update_values requires
729 * us to intersect the bounds with the current build domain.
730 * When we intersect build->domain with the updated bounds in
731 * the final step, we make sure that these updated bounds have
732 * not been intersected with the old build->domain.
733 * Otherwise, we would indirectly intersect the build domain with itself,
734 * which can lead to inefficiencies, in particular if the build domain
735 * contains any unknown divs.
737 __isl_give isl_ast_build
*isl_ast_build_set_loop_bounds(
738 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*bounds
)
742 build
= isl_ast_build_cow(build
);
746 bounds
= isl_basic_set_preimage_multi_aff(bounds
,
747 isl_multi_aff_copy(build
->values
));
748 build
= update_values(build
, isl_basic_set_copy(bounds
));
751 set
= isl_set_from_basic_set(isl_basic_set_copy(bounds
));
752 if (isl_ast_build_has_affine_value(build
, build
->depth
)) {
753 set
= isl_set_eliminate(set
, isl_dim_set
, build
->depth
, 1);
754 set
= isl_set_compute_divs(set
);
755 build
->pending
= isl_set_intersect(build
->pending
,
757 build
->domain
= isl_set_intersect(build
->domain
, set
);
759 isl_basic_set
*generated
, *pending
;
761 pending
= isl_basic_set_copy(bounds
);
762 pending
= isl_basic_set_drop_constraints_involving_dims(pending
,
763 isl_dim_set
, build
->depth
, 1);
764 build
->pending
= isl_set_intersect(build
->pending
,
765 isl_set_from_basic_set(pending
));
766 generated
= isl_basic_set_copy(bounds
);
767 generated
= isl_basic_set_drop_constraints_not_involving_dims(
768 generated
, isl_dim_set
, build
->depth
, 1);
769 build
->generated
= isl_set_intersect(build
->generated
,
770 isl_set_from_basic_set(generated
));
771 build
->domain
= isl_set_intersect(build
->domain
, set
);
772 build
= isl_ast_build_include_stride(build
);
776 isl_basic_set_free(bounds
);
778 if (!build
->domain
|| !build
->pending
|| !build
->generated
)
779 return isl_ast_build_free(build
);
783 isl_ast_build_free(build
);
784 isl_basic_set_free(bounds
);
788 /* Update build->domain based on the constraints enforced by inner loops.
790 * The constraints in build->pending may end up not getting generated
791 * if they are implied by "enforced". We therefore reconstruct
792 * build->domain from build->generated and build->pending, dropping
793 * those constraint in build->pending that may not get generated.
795 __isl_give isl_ast_build
*isl_ast_build_set_enforced(
796 __isl_take isl_ast_build
*build
, __isl_take isl_basic_set
*enforced
)
800 build
= isl_ast_build_cow(build
);
804 set
= isl_set_from_basic_set(enforced
);
805 set
= isl_set_gist(isl_set_copy(build
->pending
), set
);
806 set
= isl_set_intersect(isl_set_copy(build
->generated
), set
);
808 isl_set_free(build
->domain
);
812 return isl_ast_build_free(build
);
816 isl_basic_set_free(enforced
);
817 return isl_ast_build_free(build
);
820 /* Intersect build->domain with "set", where "set" is specified
821 * in terms of the internal schedule domain.
823 static __isl_give isl_ast_build
*isl_ast_build_restrict_internal(
824 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
826 build
= isl_ast_build_cow(build
);
830 set
= isl_set_compute_divs(set
);
831 build
->domain
= isl_set_intersect(build
->domain
, set
);
832 build
->domain
= isl_set_coalesce(build
->domain
);
835 return isl_ast_build_free(build
);
839 isl_ast_build_free(build
);
844 /* Intersect build->generated and build->domain with "set",
845 * where "set" is specified in terms of the internal schedule domain.
847 __isl_give isl_ast_build
*isl_ast_build_restrict_generated(
848 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
850 set
= isl_set_compute_divs(set
);
851 build
= isl_ast_build_restrict_internal(build
, isl_set_copy(set
));
852 build
= isl_ast_build_cow(build
);
856 build
->generated
= isl_set_intersect(build
->generated
, set
);
857 build
->generated
= isl_set_coalesce(build
->generated
);
859 if (!build
->generated
)
860 return isl_ast_build_free(build
);
864 isl_ast_build_free(build
);
869 /* Intersect build->pending and build->domain with "set",
870 * where "set" is specified in terms of the internal schedule domain.
872 __isl_give isl_ast_build
*isl_ast_build_restrict_pending(
873 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
875 set
= isl_set_compute_divs(set
);
876 build
= isl_ast_build_restrict_internal(build
, isl_set_copy(set
));
877 build
= isl_ast_build_cow(build
);
881 build
->pending
= isl_set_intersect(build
->pending
, set
);
882 build
->pending
= isl_set_coalesce(build
->pending
);
885 return isl_ast_build_free(build
);
889 isl_ast_build_free(build
);
894 /* Intersect build->domain with "set", where "set" is specified
895 * in terms of the external schedule domain.
897 __isl_give isl_ast_build
*isl_ast_build_restrict(
898 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
900 if (isl_set_is_params(set
))
901 return isl_ast_build_restrict_generated(build
, set
);
903 if (isl_ast_build_need_schedule_map(build
)) {
905 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
906 set
= isl_set_preimage_multi_aff(set
, ma
);
908 return isl_ast_build_restrict_generated(build
, set
);
911 /* Replace build->executed by "executed".
913 __isl_give isl_ast_build
*isl_ast_build_set_executed(
914 __isl_take isl_ast_build
*build
, __isl_take isl_union_map
*executed
)
916 build
= isl_ast_build_cow(build
);
920 isl_union_map_free(build
->executed
);
921 build
->executed
= executed
;
925 isl_ast_build_free(build
);
926 isl_union_map_free(executed
);
930 /* Return a copy of the current schedule domain.
932 __isl_give isl_set
*isl_ast_build_get_domain(__isl_keep isl_ast_build
*build
)
934 return build
? isl_set_copy(build
->domain
) : NULL
;
937 /* Return the number of variables of the given type
938 * in the (internal) schedule space.
940 unsigned isl_ast_build_dim(__isl_keep isl_ast_build
*build
,
941 enum isl_dim_type type
)
945 return isl_set_dim(build
->domain
, type
);
948 /* Return the (schedule) space of "build".
950 * If "internal" is set, then this space is the space of the internal
951 * representation of the entire schedule, including those parts for
952 * which no code has been generated yet.
954 * If "internal" is not set, then this space is the external representation
955 * of the loops generated so far.
957 __isl_give isl_space
*isl_ast_build_get_space(__isl_keep isl_ast_build
*build
,
967 space
= isl_set_get_space(build
->domain
);
971 if (!isl_ast_build_need_schedule_map(build
))
974 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
975 space
= isl_space_drop_dims(space
, isl_dim_set
,
976 build
->depth
, dim
- build
->depth
);
977 for (i
= build
->depth
- 1; i
>= 0; --i
)
978 if (isl_ast_build_has_affine_value(build
, i
))
979 space
= isl_space_drop_dims(space
, isl_dim_set
, i
, 1);
984 /* Return the external representation of the schedule space of "build",
985 * i.e., a space with a dimension for each loop generated so far,
986 * with the names of the dimensions set to the loop iterators.
988 __isl_give isl_space
*isl_ast_build_get_schedule_space(
989 __isl_keep isl_ast_build
*build
)
997 space
= isl_ast_build_get_space(build
, 0);
1000 for (i
= 0; i
< build
->depth
; ++i
) {
1003 if (isl_ast_build_has_affine_value(build
, i
)) {
1008 id
= isl_ast_build_get_iterator_id(build
, i
);
1009 space
= isl_space_set_dim_id(space
, isl_dim_set
, i
- skip
, id
);
1015 /* Return the current schedule, as stored in build->executed, in terms
1016 * of the external schedule domain.
1018 __isl_give isl_union_map
*isl_ast_build_get_schedule(
1019 __isl_keep isl_ast_build
*build
)
1021 isl_union_map
*executed
;
1022 isl_union_map
*schedule
;
1027 executed
= isl_union_map_copy(build
->executed
);
1028 if (isl_ast_build_need_schedule_map(build
)) {
1029 isl_map
*proj
= isl_ast_build_get_schedule_map(build
);
1030 executed
= isl_union_map_apply_domain(executed
,
1031 isl_union_map_from_map(proj
));
1033 schedule
= isl_union_map_reverse(executed
);
1038 /* Return the iterator attached to the internal schedule dimension "pos".
1040 __isl_give isl_id
*isl_ast_build_get_iterator_id(
1041 __isl_keep isl_ast_build
*build
, int pos
)
1046 return isl_id_list_get_id(build
->iterators
, pos
);
1049 /* Set the stride and offset of the current dimension to the given
1050 * value and expression.
1052 * If we had already found a stride before, then the two strides
1053 * are combined into a single stride.
1055 * In particular, if the new stride information is of the form
1059 * and the old stride information is of the form
1063 * then we compute the extended gcd of s and s2
1067 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
1068 * and the second with t2 = a s1/g.
1071 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
1073 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
1074 * is the combined stride.
1076 static __isl_give isl_ast_build
*set_stride(__isl_take isl_ast_build
*build
,
1077 __isl_take isl_val
*stride
, __isl_take isl_aff
*offset
)
1081 build
= isl_ast_build_cow(build
);
1082 if (!build
|| !stride
|| !offset
)
1087 if (isl_ast_build_has_stride(build
, pos
)) {
1088 isl_val
*stride2
, *a
, *b
, *g
;
1091 stride2
= isl_vec_get_element_val(build
->strides
, pos
);
1092 g
= isl_val_gcdext(isl_val_copy(stride
), isl_val_copy(stride2
),
1094 a
= isl_val_mul(a
, isl_val_copy(stride
));
1095 a
= isl_val_div(a
, isl_val_copy(g
));
1096 stride2
= isl_val_div(stride2
, g
);
1097 b
= isl_val_mul(b
, isl_val_copy(stride2
));
1098 stride
= isl_val_mul(stride
, stride2
);
1100 offset2
= isl_multi_aff_get_aff(build
->offsets
, pos
);
1101 offset2
= isl_aff_scale_val(offset2
, a
);
1102 offset
= isl_aff_scale_val(offset
, b
);
1103 offset
= isl_aff_add(offset
, offset2
);
1106 build
->strides
= isl_vec_set_element_val(build
->strides
, pos
, stride
);
1107 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, pos
, offset
);
1108 if (!build
->strides
|| !build
->offsets
)
1109 return isl_ast_build_free(build
);
1113 isl_val_free(stride
);
1114 isl_aff_free(offset
);
1115 return isl_ast_build_free(build
);
1118 /* Return a set expressing the stride constraint at the current depth.
1120 * In particular, if the current iterator (i) is known to attain values
1124 * where f is the offset and s is the stride, then the returned set
1125 * expresses the constraint
1129 __isl_give isl_set
*isl_ast_build_get_stride_constraint(
1130 __isl_keep isl_ast_build
*build
)
1142 if (!isl_ast_build_has_stride(build
, pos
))
1143 return isl_set_universe(isl_ast_build_get_space(build
, 1));
1145 stride
= isl_ast_build_get_stride(build
, pos
);
1146 aff
= isl_ast_build_get_offset(build
, pos
);
1147 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, -1);
1148 aff
= isl_aff_mod_val(aff
, stride
);
1149 set
= isl_set_from_basic_set(isl_aff_zero_basic_set(aff
));
1154 /* Return the expansion implied by the stride and offset at the current
1157 * That is, return the mapping
1159 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1160 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1162 * where s is the stride at the current depth d and offset(i) is
1163 * the corresponding offset.
1165 __isl_give isl_multi_aff
*isl_ast_build_get_stride_expansion(
1166 __isl_keep isl_ast_build
*build
)
1171 isl_aff
*aff
, *offset
;
1177 pos
= isl_ast_build_get_depth(build
);
1178 space
= isl_ast_build_get_space(build
, 1);
1179 space
= isl_space_map_from_set(space
);
1180 ma
= isl_multi_aff_identity(space
);
1182 if (!isl_ast_build_has_stride(build
, pos
))
1185 offset
= isl_ast_build_get_offset(build
, pos
);
1186 stride
= isl_ast_build_get_stride(build
, pos
);
1187 aff
= isl_multi_aff_get_aff(ma
, pos
);
1188 aff
= isl_aff_scale_val(aff
, stride
);
1189 aff
= isl_aff_add(aff
, offset
);
1190 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1195 /* Add constraints corresponding to any previously detected
1196 * stride on the current dimension to build->domain.
1198 __isl_give isl_ast_build
*isl_ast_build_include_stride(
1199 __isl_take isl_ast_build
*build
)
1205 if (!isl_ast_build_has_stride(build
, build
->depth
))
1207 build
= isl_ast_build_cow(build
);
1211 set
= isl_ast_build_get_stride_constraint(build
);
1213 build
->domain
= isl_set_intersect(build
->domain
, isl_set_copy(set
));
1214 build
->generated
= isl_set_intersect(build
->generated
, set
);
1215 if (!build
->domain
|| !build
->generated
)
1216 return isl_ast_build_free(build
);
1221 /* Information used inside detect_stride.
1223 * "build" may be updated by detect_stride to include stride information.
1224 * "pos" is equal to build->depth.
1226 struct isl_detect_stride_data
{
1227 isl_ast_build
*build
;
1231 /* Check if constraint "c" imposes any stride on dimension data->pos
1232 * and, if so, update the stride information in data->build.
1234 * In order to impose a stride on the dimension, "c" needs to be an equality
1235 * and it needs to involve the dimension. Note that "c" may also be
1236 * a div constraint and thus an inequality that we cannot use.
1238 * Let c be of the form
1240 * h(p) + g * v * i + g * stride * f(alpha) = 0
1242 * with h(p) an expression in terms of the parameters and outer dimensions
1243 * and f(alpha) an expression in terms of the existentially quantified
1244 * variables. Note that the inner dimensions have been eliminated so
1245 * they do not appear in "c".
1247 * If "stride" is not zero and not one, then it represents a non-trivial stride
1248 * on "i". We compute a and b such that
1250 * a v + b stride = 1
1254 * g v i = -h(p) + g stride f(alpha)
1256 * a g v i = -a h(p) + g stride f(alpha)
1258 * a g v i + b g stride i = -a h(p) + g stride * (...)
1260 * g i = -a h(p) + g stride * (...)
1262 * i = -a h(p)/g + stride * (...)
1264 * The expression "-a h(p)/g" can therefore be used as offset.
1266 static int detect_stride(__isl_take isl_constraint
*c
, void *user
)
1268 struct isl_detect_stride_data
*data
= user
;
1271 isl_val
*v
, *stride
, *m
;
1273 if (!isl_constraint_is_equality(c
) ||
1274 !isl_constraint_involves_dims(c
, isl_dim_set
, data
->pos
, 1)) {
1275 isl_constraint_free(c
);
1279 ctx
= isl_constraint_get_ctx(c
);
1280 stride
= isl_val_zero(ctx
);
1281 n_div
= isl_constraint_dim(c
, isl_dim_div
);
1282 for (i
= 0; i
< n_div
; ++i
) {
1283 v
= isl_constraint_get_coefficient_val(c
, isl_dim_div
, i
);
1284 stride
= isl_val_gcd(stride
, v
);
1287 v
= isl_constraint_get_coefficient_val(c
, isl_dim_set
, data
->pos
);
1288 m
= isl_val_gcd(isl_val_copy(stride
), isl_val_copy(v
));
1289 stride
= isl_val_div(stride
, isl_val_copy(m
));
1290 v
= isl_val_div(v
, isl_val_copy(m
));
1292 if (!isl_val_is_zero(stride
) && !isl_val_is_one(stride
)) {
1294 isl_val
*gcd
, *a
, *b
;
1296 gcd
= isl_val_gcdext(v
, isl_val_copy(stride
), &a
, &b
);
1300 aff
= isl_constraint_get_aff(c
);
1301 for (i
= 0; i
< n_div
; ++i
)
1302 aff
= isl_aff_set_coefficient_si(aff
,
1304 aff
= isl_aff_set_coefficient_si(aff
, isl_dim_in
, data
->pos
, 0);
1306 aff
= isl_aff_scale_val(aff
, a
);
1307 aff
= isl_aff_scale_down_val(aff
, m
);
1308 data
->build
= set_stride(data
->build
, stride
, aff
);
1310 isl_val_free(stride
);
1315 isl_constraint_free(c
);
1319 /* Check if the constraints in "set" imply any stride on the current
1320 * dimension and, if so, record the stride information in "build"
1321 * and return the updated "build".
1323 * We compute the affine hull and then check if any of the constraints
1324 * in the hull imposes any stride on the current dimension.
1326 * We assume that inner dimensions have been eliminated from "set"
1327 * by the caller. This is needed because the common stride
1328 * may be imposed by different inner dimensions on different parts of
1331 __isl_give isl_ast_build
*isl_ast_build_detect_strides(
1332 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
1334 isl_basic_set
*hull
;
1335 struct isl_detect_stride_data data
;
1341 data
.pos
= isl_ast_build_get_depth(build
);
1342 hull
= isl_set_affine_hull(set
);
1344 if (isl_basic_set_foreach_constraint(hull
, &detect_stride
, &data
) < 0)
1345 data
.build
= isl_ast_build_free(data
.build
);
1347 isl_basic_set_free(hull
);
1354 struct isl_ast_build_involves_data
{
1359 /* Check if "map" involves the input dimension data->depth.
1361 static int involves_depth(__isl_take isl_map
*map
, void *user
)
1363 struct isl_ast_build_involves_data
*data
= user
;
1365 data
->involves
= isl_map_involves_dims(map
, isl_dim_in
, data
->depth
, 1);
1368 if (data
->involves
< 0 || data
->involves
)
1373 /* Do any options depend on the value of the dimension at the current depth?
1375 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build
*build
)
1377 struct isl_ast_build_involves_data data
;
1382 data
.depth
= build
->depth
;
1385 if (isl_union_map_foreach_map(build
->options
,
1386 &involves_depth
, &data
) < 0) {
1387 if (data
.involves
< 0 || !data
.involves
)
1391 return data
.involves
;
1394 /* Construct the map
1396 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1398 * with "space" the parameter space of the constructed map.
1400 static __isl_give isl_map
*construct_insertion_map(__isl_take isl_space
*space
,
1404 isl_basic_map
*bmap1
, *bmap2
;
1406 space
= isl_space_set_from_params(space
);
1407 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1408 space
= isl_space_map_from_set(space
);
1409 c
= isl_equality_alloc(isl_local_space_from_space(space
));
1410 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, 0, 1);
1411 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, -1);
1412 bmap1
= isl_basic_map_from_constraint(isl_constraint_copy(c
));
1413 c
= isl_constraint_set_constant_si(c
, 1);
1414 bmap2
= isl_basic_map_from_constraint(c
);
1416 bmap1
= isl_basic_map_upper_bound_si(bmap1
, isl_dim_in
, 0, pos
- 1);
1417 bmap2
= isl_basic_map_lower_bound_si(bmap2
, isl_dim_in
, 0, pos
);
1419 return isl_basic_map_union(bmap1
, bmap2
);
1422 static const char *option_str
[] = {
1423 [atomic
] = "atomic",
1424 [unroll
] = "unroll",
1425 [separate
] = "separate"
1428 /* Update the "options" to reflect the insertion of a dimension
1429 * at position "pos" in the schedule domain space.
1430 * "space" is the original domain space before the insertion and
1431 * may be named and/or structured.
1433 * The (relevant) input options all have "space" as domain, which
1434 * has to be mapped to the extended space.
1435 * The values of the ranges also refer to the schedule domain positions
1436 * and they therefore also need to be adjusted. In particular, values
1437 * smaller than pos do not need to change, while values greater than or
1438 * equal to pos need to be incremented.
1439 * That is, we need to apply the following map.
1441 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1442 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1443 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1444 * separation_class[[i] -> [c]]
1445 * -> separation_class[[i] -> [c]] : i < pos;
1446 * separation_class[[i] -> [c]]
1447 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1449 static __isl_give isl_union_map
*options_insert_dim(
1450 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
, int pos
)
1453 isl_union_map
*insertion
;
1454 enum isl_ast_build_domain_type type
;
1455 const char *name
= "separation_class";
1457 space
= isl_space_map_from_set(space
);
1458 map
= isl_map_identity(space
);
1459 map
= isl_map_insert_dims(map
, isl_dim_out
, pos
, 1);
1460 options
= isl_union_map_apply_domain(options
,
1461 isl_union_map_from_map(map
));
1466 map
= construct_insertion_map(isl_union_map_get_space(options
), pos
);
1468 insertion
= isl_union_map_empty(isl_union_map_get_space(options
));
1470 for (type
= atomic
; type
<= separate
; ++type
) {
1471 isl_map
*map_type
= isl_map_copy(map
);
1472 const char *name
= option_str
[type
];
1473 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_in
, name
);
1474 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_out
, name
);
1475 insertion
= isl_union_map_add_map(insertion
, map_type
);
1478 map
= isl_map_product(map
, isl_map_identity(isl_map_get_space(map
)));
1479 map
= isl_map_set_tuple_name(map
, isl_dim_in
, name
);
1480 map
= isl_map_set_tuple_name(map
, isl_dim_out
, name
);
1481 insertion
= isl_union_map_add_map(insertion
, map
);
1483 options
= isl_union_map_apply_range(options
, insertion
);
1488 /* Insert a single dimension in the schedule domain at position "pos".
1489 * The new dimension is given an isl_id with the empty string as name.
1491 * The main difficulty is updating build->options to reflect the
1492 * extra dimension. This is handled in options_insert_dim.
1494 * Note that because of the dimension manipulations, the resulting
1495 * schedule domain space will always be unnamed and unstructured.
1496 * However, the original schedule domain space may be named and/or
1497 * structured, so we have to take this possibility into account
1498 * while performing the transformations.
1500 __isl_give isl_ast_build
*isl_ast_build_insert_dim(
1501 __isl_take isl_ast_build
*build
, int pos
)
1504 isl_space
*space
, *ma_space
;
1508 build
= isl_ast_build_cow(build
);
1512 ctx
= isl_ast_build_get_ctx(build
);
1513 id
= isl_id_alloc(ctx
, "", NULL
);
1514 space
= isl_ast_build_get_space(build
, 1);
1515 build
->iterators
= isl_id_list_insert(build
->iterators
, pos
, id
);
1516 build
->domain
= isl_set_insert_dims(build
->domain
,
1517 isl_dim_set
, pos
, 1);
1518 build
->generated
= isl_set_insert_dims(build
->generated
,
1519 isl_dim_set
, pos
, 1);
1520 build
->pending
= isl_set_insert_dims(build
->pending
,
1521 isl_dim_set
, pos
, 1);
1522 build
->strides
= isl_vec_insert_els(build
->strides
, pos
, 1);
1523 build
->strides
= isl_vec_set_element_si(build
->strides
, pos
, 1);
1524 ma_space
= isl_space_params(isl_multi_aff_get_space(build
->offsets
));
1525 ma_space
= isl_space_set_from_params(ma_space
);
1526 ma_space
= isl_space_add_dims(ma_space
, isl_dim_set
, 1);
1527 ma_space
= isl_space_map_from_set(ma_space
);
1528 ma
= isl_multi_aff_zero(isl_space_copy(ma_space
));
1529 build
->offsets
= isl_multi_aff_splice(build
->offsets
, pos
, pos
, ma
);
1530 ma
= isl_multi_aff_identity(ma_space
);
1531 build
->values
= isl_multi_aff_splice(build
->values
, pos
, pos
, ma
);
1532 build
->options
= options_insert_dim(build
->options
, space
, pos
);
1534 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1535 !build
->pending
|| !build
->values
||
1536 !build
->strides
|| !build
->offsets
|| !build
->options
)
1537 return isl_ast_build_free(build
);
1542 /* Scale down the current dimension by a factor of "m".
1543 * "umap" is an isl_union_map that implements the scaling down.
1544 * That is, it is of the form
1546 * { [.... i ....] -> [.... i' ....] : i = m i' }
1548 * This function is called right after the strides have been
1549 * detected, but before any constraints on the current dimension
1550 * have been included in build->domain.
1551 * We therefore only need to update stride, offset and the options.
1553 __isl_give isl_ast_build
*isl_ast_build_scale_down(
1554 __isl_take isl_ast_build
*build
, __isl_take isl_val
*m
,
1555 __isl_take isl_union_map
*umap
)
1561 build
= isl_ast_build_cow(build
);
1562 if (!build
|| !umap
|| !m
)
1565 depth
= build
->depth
;
1567 v
= isl_vec_get_element_val(build
->strides
, depth
);
1568 v
= isl_val_div(v
, isl_val_copy(m
));
1569 build
->strides
= isl_vec_set_element_val(build
->strides
, depth
, v
);
1571 aff
= isl_multi_aff_get_aff(build
->offsets
, depth
);
1572 aff
= isl_aff_scale_down_val(aff
, m
);
1573 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, depth
, aff
);
1574 build
->options
= isl_union_map_apply_domain(build
->options
, umap
);
1575 if (!build
->strides
|| !build
->offsets
|| !build
->options
)
1576 return isl_ast_build_free(build
);
1581 isl_union_map_free(umap
);
1582 return isl_ast_build_free(build
);
1585 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1586 * If an isl_id with such a name already appears among the parameters
1587 * in build->domain, then adjust the name to "c%d_%d".
1589 static __isl_give isl_id_list
*generate_names(isl_ctx
*ctx
, int n
, int first
,
1590 __isl_keep isl_ast_build
*build
)
1595 names
= isl_id_list_alloc(ctx
, n
);
1596 for (i
= 0; i
< n
; ++i
) {
1599 id
= generate_name(ctx
, first
+ i
, build
);
1600 names
= isl_id_list_add(names
, id
);
1606 /* Embed "options" into the given isl_ast_build space.
1608 * This function is called from within a nested call to
1609 * isl_ast_build_ast_from_schedule.
1610 * "options" refers to the additional schedule,
1611 * while space refers to both the space of the outer isl_ast_build and
1612 * that of the additional schedule.
1613 * Specifically, space is of the form
1617 * while options lives in the space(s)
1625 * and compose this with options, to obtain the new options
1626 * living in the space(s)
1630 static __isl_give isl_union_map
*embed_options(
1631 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
)
1635 map
= isl_map_universe(isl_space_unwrap(space
));
1636 map
= isl_map_range_map(map
);
1638 options
= isl_union_map_apply_range(
1639 isl_union_map_from_map(map
), options
);
1644 /* Update "build" for use in a (possibly nested) code generation. That is,
1645 * extend "build" from an AST build on some domain O to an AST build
1646 * on domain [O -> S], with S corresponding to "space".
1647 * If the original domain is a parameter domain, then the new domain is
1649 * "iterators" is a list of iterators for S, but the number of elements
1650 * may be smaller or greater than the number of set dimensions of S.
1651 * If "keep_iterators" is set, then any extra ids in build->iterators
1652 * are reused for S. Otherwise, these extra ids are dropped.
1654 * We first update build->outer_pos to the current depth.
1655 * This depth is zero in case this is the outermost code generation.
1657 * We then add additional ids such that the number of iterators is at least
1658 * equal to the dimension of the new build domain.
1660 * If the original domain is parametric, then we are constructing
1661 * an isl_ast_build for the outer code generation and we pass control
1662 * to isl_ast_build_init.
1664 * Otherwise, we adjust the fields of "build" to include "space".
1666 __isl_give isl_ast_build
*isl_ast_build_product(
1667 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
1672 isl_multi_aff
*embedding
;
1675 build
= isl_ast_build_cow(build
);
1679 build
->outer_pos
= build
->depth
;
1681 ctx
= isl_ast_build_get_ctx(build
);
1682 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
1683 dim
+= isl_space_dim(space
, isl_dim_set
);
1684 n_it
= isl_id_list_n_id(build
->iterators
);
1687 l
= generate_names(ctx
, dim
- n_it
, n_it
, build
);
1688 build
->iterators
= isl_id_list_concat(build
->iterators
, l
);
1691 if (isl_set_is_params(build
->domain
))
1692 return isl_ast_build_init(build
, space
);
1694 set
= isl_set_universe(isl_space_copy(space
));
1695 build
->domain
= isl_set_product(build
->domain
, isl_set_copy(set
));
1696 build
->pending
= isl_set_product(build
->pending
, isl_set_copy(set
));
1697 build
->generated
= isl_set_product(build
->generated
, set
);
1699 strides
= isl_vec_alloc(ctx
, isl_space_dim(space
, isl_dim_set
));
1700 strides
= isl_vec_set_si(strides
, 1);
1701 build
->strides
= isl_vec_concat(build
->strides
, strides
);
1703 space
= isl_space_map_from_set(space
);
1704 build
->offsets
= isl_multi_aff_align_params(build
->offsets
,
1705 isl_space_copy(space
));
1706 build
->offsets
= isl_multi_aff_product(build
->offsets
,
1707 isl_multi_aff_zero(isl_space_copy(space
)));
1708 build
->values
= isl_multi_aff_align_params(build
->values
,
1709 isl_space_copy(space
));
1710 embedding
= isl_multi_aff_identity(space
);
1711 build
->values
= isl_multi_aff_product(build
->values
, embedding
);
1713 space
= isl_ast_build_get_space(build
, 1);
1714 build
->options
= embed_options(build
->options
, space
);
1716 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1717 !build
->pending
|| !build
->values
||
1718 !build
->strides
|| !build
->offsets
|| !build
->options
)
1719 return isl_ast_build_free(build
);
1723 isl_ast_build_free(build
);
1724 isl_space_free(space
);
1728 /* Does "aff" only attain non-negative values over build->domain?
1729 * That is, does it not attain any negative values?
1731 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build
*build
,
1732 __isl_keep isl_aff
*aff
)
1740 aff
= isl_aff_copy(aff
);
1741 test
= isl_set_from_basic_set(isl_aff_neg_basic_set(aff
));
1742 test
= isl_set_intersect(test
, isl_set_copy(build
->domain
));
1743 empty
= isl_set_is_empty(test
);
1749 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1751 int isl_ast_build_has_stride(__isl_keep isl_ast_build
*build
, int pos
)
1759 v
= isl_vec_get_element_val(build
->strides
, pos
);
1762 has_stride
= !isl_val_is_one(v
);
1768 /* Given that the dimension at position "pos" takes on values
1772 * with a an integer, return s through *stride.
1774 __isl_give isl_val
*isl_ast_build_get_stride(__isl_keep isl_ast_build
*build
,
1780 return isl_vec_get_element_val(build
->strides
, pos
);
1783 /* Given that the dimension at position "pos" takes on values
1787 * with a an integer, return f.
1789 __isl_give isl_aff
*isl_ast_build_get_offset(
1790 __isl_keep isl_ast_build
*build
, int pos
)
1795 return isl_multi_aff_get_aff(build
->offsets
, pos
);
1798 /* Is the dimension at position "pos" known to attain only a single
1799 * value that, moreover, can be described by a single affine expression
1800 * in terms of the outer dimensions and parameters?
1802 * If not, then the correponding affine expression in build->values
1803 * is set to be equal to the same input dimension.
1804 * Otherwise, it is set to the requested expression in terms of
1805 * outer dimensions and parameters.
1807 int isl_ast_build_has_affine_value(__isl_keep isl_ast_build
*build
,
1816 aff
= isl_multi_aff_get_aff(build
->values
, pos
);
1817 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, pos
, 1);
1826 /* Plug in the known values (fixed affine expressions in terms of
1827 * parameters and outer loop iterators) of all loop iterators
1828 * in the domain of "umap".
1830 * We simply precompose "umap" with build->values.
1832 __isl_give isl_union_map
*isl_ast_build_substitute_values_union_map_domain(
1833 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*umap
)
1835 isl_multi_aff
*values
;
1838 return isl_union_map_free(umap
);
1840 values
= isl_multi_aff_copy(build
->values
);
1841 umap
= isl_union_map_preimage_domain_multi_aff(umap
, values
);
1846 /* Is the current dimension known to attain only a single value?
1848 int isl_ast_build_has_value(__isl_keep isl_ast_build
*build
)
1853 return build
->value
!= NULL
;
1856 /* Simplify the basic set "bset" based on what we know about
1857 * the iterators of already generated loops.
1859 * "bset" is assumed to live in the (internal) schedule domain.
1861 __isl_give isl_basic_set
*isl_ast_build_compute_gist_basic_set(
1862 __isl_keep isl_ast_build
*build
, __isl_take isl_basic_set
*bset
)
1867 bset
= isl_basic_set_preimage_multi_aff(bset
,
1868 isl_multi_aff_copy(build
->values
));
1869 bset
= isl_basic_set_gist(bset
,
1870 isl_set_simple_hull(isl_set_copy(build
->domain
)));
1874 isl_basic_set_free(bset
);
1878 /* Simplify the set "set" based on what we know about
1879 * the iterators of already generated loops.
1881 * "set" is assumed to live in the (internal) schedule domain.
1883 __isl_give isl_set
*isl_ast_build_compute_gist(
1884 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
1889 set
= isl_set_preimage_multi_aff(set
,
1890 isl_multi_aff_copy(build
->values
));
1891 set
= isl_set_gist(set
, isl_set_copy(build
->domain
));
1899 /* Include information about what we know about the iterators of
1900 * already generated loops to "set".
1902 * We currently only plug in the known affine values of outer loop
1904 * In principle we could also introduce equalities or even other
1905 * constraints implied by the intersection of "set" and build->domain.
1907 __isl_give isl_set
*isl_ast_build_specialize(__isl_keep isl_ast_build
*build
,
1908 __isl_take isl_set
*set
)
1911 return isl_set_free(set
);
1913 return isl_set_preimage_multi_aff(set
,
1914 isl_multi_aff_copy(build
->values
));
1917 /* Simplify the map "map" based on what we know about
1918 * the iterators of already generated loops.
1920 * The domain of "map" is assumed to live in the (internal) schedule domain.
1922 __isl_give isl_map
*isl_ast_build_compute_gist_map_domain(
1923 __isl_keep isl_ast_build
*build
, __isl_take isl_map
*map
)
1928 map
= isl_map_gist_domain(map
, isl_set_copy(build
->domain
));
1936 /* Simplify the affine expression "aff" based on what we know about
1937 * the iterators of already generated loops.
1939 * The domain of "aff" is assumed to live in the (internal) schedule domain.
1941 __isl_give isl_aff
*isl_ast_build_compute_gist_aff(
1942 __isl_keep isl_ast_build
*build
, __isl_take isl_aff
*aff
)
1947 aff
= isl_aff_gist(aff
, isl_set_copy(build
->domain
));
1955 /* Simplify the piecewise affine expression "aff" based on what we know about
1956 * the iterators of already generated loops.
1958 * The domain of "pa" is assumed to live in the (internal) schedule domain.
1960 __isl_give isl_pw_aff
*isl_ast_build_compute_gist_pw_aff(
1961 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_aff
*pa
)
1966 if (!isl_set_is_params(build
->domain
))
1967 pa
= isl_pw_aff_pullback_multi_aff(pa
,
1968 isl_multi_aff_copy(build
->values
));
1969 pa
= isl_pw_aff_gist(pa
, isl_set_copy(build
->domain
));
1973 isl_pw_aff_free(pa
);
1977 /* Simplify the piecewise multi-affine expression "aff" based on what
1978 * we know about the iterators of already generated loops.
1980 * The domain of "pma" is assumed to live in the (internal) schedule domain.
1982 __isl_give isl_pw_multi_aff
*isl_ast_build_compute_gist_pw_multi_aff(
1983 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_multi_aff
*pma
)
1988 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
,
1989 isl_multi_aff_copy(build
->values
));
1990 pma
= isl_pw_multi_aff_gist(pma
, isl_set_copy(build
->domain
));
1994 isl_pw_multi_aff_free(pma
);
1998 /* Extract the schedule domain of the given type from build->options
1999 * at the current depth.
2001 * In particular, find the subset of build->options that is of
2002 * the following form
2004 * schedule_domain -> type[depth]
2006 * and return the corresponding domain, after eliminating inner dimensions
2007 * and divs that depend on the current dimension.
2009 * Note that the domain of build->options has been reformulated
2010 * in terms of the internal build space in embed_options,
2011 * but the position is still that within the current code generation.
2013 __isl_give isl_set
*isl_ast_build_get_option_domain(
2014 __isl_keep isl_ast_build
*build
,
2015 enum isl_ast_build_domain_type type
)
2026 name
= option_str
[type
];
2027 local_pos
= build
->depth
- build
->outer_pos
;
2029 space
= isl_ast_build_get_space(build
, 1);
2030 space
= isl_space_from_domain(space
);
2031 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2032 space
= isl_space_set_tuple_name(space
, isl_dim_out
, name
);
2034 option
= isl_union_map_extract_map(build
->options
, space
);
2035 option
= isl_map_fix_si(option
, isl_dim_out
, 0, local_pos
);
2037 domain
= isl_map_domain(option
);
2038 domain
= isl_ast_build_eliminate(build
, domain
);
2043 /* Extract the separation class mapping at the current depth.
2045 * In particular, find and return the subset of build->options that is of
2046 * the following form
2048 * schedule_domain -> separation_class[[depth] -> [class]]
2050 * The caller is expected to eliminate inner dimensions from the domain.
2052 * Note that the domain of build->options has been reformulated
2053 * in terms of the internal build space in embed_options,
2054 * but the position is still that within the current code generation.
2056 __isl_give isl_map
*isl_ast_build_get_separation_class(
2057 __isl_keep isl_ast_build
*build
)
2060 isl_space
*space_sep
, *space
;
2067 local_pos
= build
->depth
- build
->outer_pos
;
2068 ctx
= isl_ast_build_get_ctx(build
);
2069 space_sep
= isl_space_alloc(ctx
, 0, 1, 1);
2070 space_sep
= isl_space_wrap(space_sep
);
2071 space_sep
= isl_space_set_tuple_name(space_sep
, isl_dim_set
,
2072 "separation_class");
2073 space
= isl_ast_build_get_space(build
, 1);
2074 space_sep
= isl_space_align_params(space_sep
, isl_space_copy(space
));
2075 space
= isl_space_map_from_domain_and_range(space
, space_sep
);
2077 res
= isl_union_map_extract_map(build
->options
, space
);
2078 res
= isl_map_fix_si(res
, isl_dim_out
, 0, local_pos
);
2079 res
= isl_map_coalesce(res
);
2084 /* Eliminate dimensions inner to the current dimension.
2086 __isl_give isl_set
*isl_ast_build_eliminate_inner(
2087 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2093 return isl_set_free(set
);
2095 dim
= isl_set_dim(set
, isl_dim_set
);
2096 depth
= build
->depth
;
2097 set
= isl_set_detect_equalities(set
);
2098 set
= isl_set_eliminate(set
, isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2103 /* Eliminate unknown divs and divs that depend on the current dimension.
2105 * Note that during the elimination of unknown divs, we may discover
2106 * an explicit representation of some other unknown divs, which may
2107 * depend on the current dimension. We therefore need to eliminate
2108 * unknown divs first.
2110 __isl_give isl_set
*isl_ast_build_eliminate_divs(
2111 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2116 return isl_set_free(set
);
2118 set
= isl_set_remove_unknown_divs(set
);
2119 depth
= build
->depth
;
2120 set
= isl_set_remove_divs_involving_dims(set
, isl_dim_set
, depth
, 1);
2125 /* Eliminate dimensions inner to the current dimension as well as
2126 * unknown divs and divs that depend on the current dimension.
2127 * The result then consists only of constraints that are independent
2128 * of the current dimension and upper and lower bounds on the current
2131 __isl_give isl_set
*isl_ast_build_eliminate(
2132 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*domain
)
2134 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2135 domain
= isl_ast_build_eliminate_divs(build
, domain
);
2139 /* Replace build->single_valued by "sv".
2141 __isl_give isl_ast_build
*isl_ast_build_set_single_valued(
2142 __isl_take isl_ast_build
*build
, int sv
)
2146 if (build
->single_valued
== sv
)
2148 build
= isl_ast_build_cow(build
);
2151 build
->single_valued
= sv
;