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 a copy of the set of pending constraints.
939 __isl_give isl_set
*isl_ast_build_get_pending(
940 __isl_keep isl_ast_build
*build
)
942 return build
? isl_set_copy(build
->pending
) : NULL
;
945 /* Return a copy of the set of generated constraints.
947 __isl_give isl_set
*isl_ast_build_get_generated(
948 __isl_keep isl_ast_build
*build
)
950 return build
? isl_set_copy(build
->generated
) : NULL
;
953 /* Return the number of variables of the given type
954 * in the (internal) schedule space.
956 unsigned isl_ast_build_dim(__isl_keep isl_ast_build
*build
,
957 enum isl_dim_type type
)
961 return isl_set_dim(build
->domain
, type
);
964 /* Return the (schedule) space of "build".
966 * If "internal" is set, then this space is the space of the internal
967 * representation of the entire schedule, including those parts for
968 * which no code has been generated yet.
970 * If "internal" is not set, then this space is the external representation
971 * of the loops generated so far.
973 __isl_give isl_space
*isl_ast_build_get_space(__isl_keep isl_ast_build
*build
,
983 space
= isl_set_get_space(build
->domain
);
987 if (!isl_ast_build_need_schedule_map(build
))
990 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
991 space
= isl_space_drop_dims(space
, isl_dim_set
,
992 build
->depth
, dim
- build
->depth
);
993 for (i
= build
->depth
- 1; i
>= 0; --i
)
994 if (isl_ast_build_has_affine_value(build
, i
))
995 space
= isl_space_drop_dims(space
, isl_dim_set
, i
, 1);
1000 /* Return the external representation of the schedule space of "build",
1001 * i.e., a space with a dimension for each loop generated so far,
1002 * with the names of the dimensions set to the loop iterators.
1004 __isl_give isl_space
*isl_ast_build_get_schedule_space(
1005 __isl_keep isl_ast_build
*build
)
1013 space
= isl_ast_build_get_space(build
, 0);
1016 for (i
= 0; i
< build
->depth
; ++i
) {
1019 if (isl_ast_build_has_affine_value(build
, i
)) {
1024 id
= isl_ast_build_get_iterator_id(build
, i
);
1025 space
= isl_space_set_dim_id(space
, isl_dim_set
, i
- skip
, id
);
1031 /* Return the current schedule, as stored in build->executed, in terms
1032 * of the external schedule domain.
1034 __isl_give isl_union_map
*isl_ast_build_get_schedule(
1035 __isl_keep isl_ast_build
*build
)
1037 isl_union_map
*executed
;
1038 isl_union_map
*schedule
;
1043 executed
= isl_union_map_copy(build
->executed
);
1044 if (isl_ast_build_need_schedule_map(build
)) {
1045 isl_map
*proj
= isl_ast_build_get_schedule_map(build
);
1046 executed
= isl_union_map_apply_domain(executed
,
1047 isl_union_map_from_map(proj
));
1049 schedule
= isl_union_map_reverse(executed
);
1054 /* Return the iterator attached to the internal schedule dimension "pos".
1056 __isl_give isl_id
*isl_ast_build_get_iterator_id(
1057 __isl_keep isl_ast_build
*build
, int pos
)
1062 return isl_id_list_get_id(build
->iterators
, pos
);
1065 /* Set the stride and offset of the current dimension to the given
1066 * value and expression.
1068 * If we had already found a stride before, then the two strides
1069 * are combined into a single stride.
1071 * In particular, if the new stride information is of the form
1075 * and the old stride information is of the form
1079 * then we compute the extended gcd of s and s2
1083 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
1084 * and the second with t2 = a s1/g.
1087 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
1089 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
1090 * is the combined stride.
1092 static __isl_give isl_ast_build
*set_stride(__isl_take isl_ast_build
*build
,
1093 __isl_take isl_val
*stride
, __isl_take isl_aff
*offset
)
1097 build
= isl_ast_build_cow(build
);
1098 if (!build
|| !stride
|| !offset
)
1103 if (isl_ast_build_has_stride(build
, pos
)) {
1104 isl_val
*stride2
, *a
, *b
, *g
;
1107 stride2
= isl_vec_get_element_val(build
->strides
, pos
);
1108 g
= isl_val_gcdext(isl_val_copy(stride
), isl_val_copy(stride2
),
1110 a
= isl_val_mul(a
, isl_val_copy(stride
));
1111 a
= isl_val_div(a
, isl_val_copy(g
));
1112 stride2
= isl_val_div(stride2
, g
);
1113 b
= isl_val_mul(b
, isl_val_copy(stride2
));
1114 stride
= isl_val_mul(stride
, stride2
);
1116 offset2
= isl_multi_aff_get_aff(build
->offsets
, pos
);
1117 offset2
= isl_aff_scale_val(offset2
, a
);
1118 offset
= isl_aff_scale_val(offset
, b
);
1119 offset
= isl_aff_add(offset
, offset2
);
1122 build
->strides
= isl_vec_set_element_val(build
->strides
, pos
, stride
);
1123 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, pos
, offset
);
1124 if (!build
->strides
|| !build
->offsets
)
1125 return isl_ast_build_free(build
);
1129 isl_val_free(stride
);
1130 isl_aff_free(offset
);
1131 return isl_ast_build_free(build
);
1134 /* Return a set expressing the stride constraint at the current depth.
1136 * In particular, if the current iterator (i) is known to attain values
1140 * where f is the offset and s is the stride, then the returned set
1141 * expresses the constraint
1145 __isl_give isl_set
*isl_ast_build_get_stride_constraint(
1146 __isl_keep isl_ast_build
*build
)
1158 if (!isl_ast_build_has_stride(build
, pos
))
1159 return isl_set_universe(isl_ast_build_get_space(build
, 1));
1161 stride
= isl_ast_build_get_stride(build
, pos
);
1162 aff
= isl_ast_build_get_offset(build
, pos
);
1163 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, -1);
1164 aff
= isl_aff_mod_val(aff
, stride
);
1165 set
= isl_set_from_basic_set(isl_aff_zero_basic_set(aff
));
1170 /* Return the expansion implied by the stride and offset at the current
1173 * That is, return the mapping
1175 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1176 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1178 * where s is the stride at the current depth d and offset(i) is
1179 * the corresponding offset.
1181 __isl_give isl_multi_aff
*isl_ast_build_get_stride_expansion(
1182 __isl_keep isl_ast_build
*build
)
1187 isl_aff
*aff
, *offset
;
1193 pos
= isl_ast_build_get_depth(build
);
1194 space
= isl_ast_build_get_space(build
, 1);
1195 space
= isl_space_map_from_set(space
);
1196 ma
= isl_multi_aff_identity(space
);
1198 if (!isl_ast_build_has_stride(build
, pos
))
1201 offset
= isl_ast_build_get_offset(build
, pos
);
1202 stride
= isl_ast_build_get_stride(build
, pos
);
1203 aff
= isl_multi_aff_get_aff(ma
, pos
);
1204 aff
= isl_aff_scale_val(aff
, stride
);
1205 aff
= isl_aff_add(aff
, offset
);
1206 ma
= isl_multi_aff_set_aff(ma
, pos
, aff
);
1211 /* Add constraints corresponding to any previously detected
1212 * stride on the current dimension to build->domain.
1214 __isl_give isl_ast_build
*isl_ast_build_include_stride(
1215 __isl_take isl_ast_build
*build
)
1221 if (!isl_ast_build_has_stride(build
, build
->depth
))
1223 build
= isl_ast_build_cow(build
);
1227 set
= isl_ast_build_get_stride_constraint(build
);
1229 build
->domain
= isl_set_intersect(build
->domain
, isl_set_copy(set
));
1230 build
->generated
= isl_set_intersect(build
->generated
, set
);
1231 if (!build
->domain
|| !build
->generated
)
1232 return isl_ast_build_free(build
);
1237 /* Information used inside detect_stride.
1239 * "build" may be updated by detect_stride to include stride information.
1240 * "pos" is equal to build->depth.
1242 struct isl_detect_stride_data
{
1243 isl_ast_build
*build
;
1247 /* Check if constraint "c" imposes any stride on dimension data->pos
1248 * and, if so, update the stride information in data->build.
1250 * In order to impose a stride on the dimension, "c" needs to be an equality
1251 * and it needs to involve the dimension. Note that "c" may also be
1252 * a div constraint and thus an inequality that we cannot use.
1254 * Let c be of the form
1256 * h(p) + g * v * i + g * stride * f(alpha) = 0
1258 * with h(p) an expression in terms of the parameters and outer dimensions
1259 * and f(alpha) an expression in terms of the existentially quantified
1260 * variables. Note that the inner dimensions have been eliminated so
1261 * they do not appear in "c".
1263 * If "stride" is not zero and not one, then it represents a non-trivial stride
1264 * on "i". We compute a and b such that
1266 * a v + b stride = 1
1270 * g v i = -h(p) + g stride f(alpha)
1272 * a g v i = -a h(p) + g stride f(alpha)
1274 * a g v i + b g stride i = -a h(p) + g stride * (...)
1276 * g i = -a h(p) + g stride * (...)
1278 * i = -a h(p)/g + stride * (...)
1280 * The expression "-a h(p)/g" can therefore be used as offset.
1282 static int detect_stride(__isl_take isl_constraint
*c
, void *user
)
1284 struct isl_detect_stride_data
*data
= user
;
1287 isl_val
*v
, *stride
, *m
;
1289 if (!isl_constraint_is_equality(c
) ||
1290 !isl_constraint_involves_dims(c
, isl_dim_set
, data
->pos
, 1)) {
1291 isl_constraint_free(c
);
1295 ctx
= isl_constraint_get_ctx(c
);
1296 stride
= isl_val_zero(ctx
);
1297 n_div
= isl_constraint_dim(c
, isl_dim_div
);
1298 for (i
= 0; i
< n_div
; ++i
) {
1299 v
= isl_constraint_get_coefficient_val(c
, isl_dim_div
, i
);
1300 stride
= isl_val_gcd(stride
, v
);
1303 v
= isl_constraint_get_coefficient_val(c
, isl_dim_set
, data
->pos
);
1304 m
= isl_val_gcd(isl_val_copy(stride
), isl_val_copy(v
));
1305 stride
= isl_val_div(stride
, isl_val_copy(m
));
1306 v
= isl_val_div(v
, isl_val_copy(m
));
1308 if (!isl_val_is_zero(stride
) && !isl_val_is_one(stride
)) {
1310 isl_val
*gcd
, *a
, *b
;
1312 gcd
= isl_val_gcdext(v
, isl_val_copy(stride
), &a
, &b
);
1316 aff
= isl_constraint_get_aff(c
);
1317 for (i
= 0; i
< n_div
; ++i
)
1318 aff
= isl_aff_set_coefficient_si(aff
,
1320 aff
= isl_aff_set_coefficient_si(aff
, isl_dim_in
, data
->pos
, 0);
1322 aff
= isl_aff_scale_val(aff
, a
);
1323 aff
= isl_aff_scale_down_val(aff
, m
);
1324 data
->build
= set_stride(data
->build
, stride
, aff
);
1326 isl_val_free(stride
);
1331 isl_constraint_free(c
);
1335 /* Check if the constraints in "set" imply any stride on the current
1336 * dimension and, if so, record the stride information in "build"
1337 * and return the updated "build".
1339 * We compute the affine hull and then check if any of the constraints
1340 * in the hull imposes any stride on the current dimension.
1342 * We assume that inner dimensions have been eliminated from "set"
1343 * by the caller. This is needed because the common stride
1344 * may be imposed by different inner dimensions on different parts of
1347 __isl_give isl_ast_build
*isl_ast_build_detect_strides(
1348 __isl_take isl_ast_build
*build
, __isl_take isl_set
*set
)
1350 isl_basic_set
*hull
;
1351 struct isl_detect_stride_data data
;
1357 data
.pos
= isl_ast_build_get_depth(build
);
1358 hull
= isl_set_affine_hull(set
);
1360 if (isl_basic_set_foreach_constraint(hull
, &detect_stride
, &data
) < 0)
1361 data
.build
= isl_ast_build_free(data
.build
);
1363 isl_basic_set_free(hull
);
1370 struct isl_ast_build_involves_data
{
1375 /* Check if "map" involves the input dimension data->depth.
1377 static int involves_depth(__isl_take isl_map
*map
, void *user
)
1379 struct isl_ast_build_involves_data
*data
= user
;
1381 data
->involves
= isl_map_involves_dims(map
, isl_dim_in
, data
->depth
, 1);
1384 if (data
->involves
< 0 || data
->involves
)
1389 /* Do any options depend on the value of the dimension at the current depth?
1391 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build
*build
)
1393 struct isl_ast_build_involves_data data
;
1398 data
.depth
= build
->depth
;
1401 if (isl_union_map_foreach_map(build
->options
,
1402 &involves_depth
, &data
) < 0) {
1403 if (data
.involves
< 0 || !data
.involves
)
1407 return data
.involves
;
1410 /* Construct the map
1412 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1414 * with "space" the parameter space of the constructed map.
1416 static __isl_give isl_map
*construct_insertion_map(__isl_take isl_space
*space
,
1420 isl_basic_map
*bmap1
, *bmap2
;
1422 space
= isl_space_set_from_params(space
);
1423 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
1424 space
= isl_space_map_from_set(space
);
1425 c
= isl_equality_alloc(isl_local_space_from_space(space
));
1426 c
= isl_constraint_set_coefficient_si(c
, isl_dim_in
, 0, 1);
1427 c
= isl_constraint_set_coefficient_si(c
, isl_dim_out
, 0, -1);
1428 bmap1
= isl_basic_map_from_constraint(isl_constraint_copy(c
));
1429 c
= isl_constraint_set_constant_si(c
, 1);
1430 bmap2
= isl_basic_map_from_constraint(c
);
1432 bmap1
= isl_basic_map_upper_bound_si(bmap1
, isl_dim_in
, 0, pos
- 1);
1433 bmap2
= isl_basic_map_lower_bound_si(bmap2
, isl_dim_in
, 0, pos
);
1435 return isl_basic_map_union(bmap1
, bmap2
);
1438 static const char *option_str
[] = {
1439 [atomic
] = "atomic",
1440 [unroll
] = "unroll",
1441 [separate
] = "separate"
1444 /* Update the "options" to reflect the insertion of a dimension
1445 * at position "pos" in the schedule domain space.
1446 * "space" is the original domain space before the insertion and
1447 * may be named and/or structured.
1449 * The (relevant) input options all have "space" as domain, which
1450 * has to be mapped to the extended space.
1451 * The values of the ranges also refer to the schedule domain positions
1452 * and they therefore also need to be adjusted. In particular, values
1453 * smaller than pos do not need to change, while values greater than or
1454 * equal to pos need to be incremented.
1455 * That is, we need to apply the following map.
1457 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1458 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1459 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1460 * separation_class[[i] -> [c]]
1461 * -> separation_class[[i] -> [c]] : i < pos;
1462 * separation_class[[i] -> [c]]
1463 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1465 static __isl_give isl_union_map
*options_insert_dim(
1466 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
, int pos
)
1469 isl_union_map
*insertion
;
1470 enum isl_ast_build_domain_type type
;
1471 const char *name
= "separation_class";
1473 space
= isl_space_map_from_set(space
);
1474 map
= isl_map_identity(space
);
1475 map
= isl_map_insert_dims(map
, isl_dim_out
, pos
, 1);
1476 options
= isl_union_map_apply_domain(options
,
1477 isl_union_map_from_map(map
));
1482 map
= construct_insertion_map(isl_union_map_get_space(options
), pos
);
1484 insertion
= isl_union_map_empty(isl_union_map_get_space(options
));
1486 for (type
= atomic
; type
<= separate
; ++type
) {
1487 isl_map
*map_type
= isl_map_copy(map
);
1488 const char *name
= option_str
[type
];
1489 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_in
, name
);
1490 map_type
= isl_map_set_tuple_name(map_type
, isl_dim_out
, name
);
1491 insertion
= isl_union_map_add_map(insertion
, map_type
);
1494 map
= isl_map_product(map
, isl_map_identity(isl_map_get_space(map
)));
1495 map
= isl_map_set_tuple_name(map
, isl_dim_in
, name
);
1496 map
= isl_map_set_tuple_name(map
, isl_dim_out
, name
);
1497 insertion
= isl_union_map_add_map(insertion
, map
);
1499 options
= isl_union_map_apply_range(options
, insertion
);
1504 /* Insert a single dimension in the schedule domain at position "pos".
1505 * The new dimension is given an isl_id with the empty string as name.
1507 * The main difficulty is updating build->options to reflect the
1508 * extra dimension. This is handled in options_insert_dim.
1510 * Note that because of the dimension manipulations, the resulting
1511 * schedule domain space will always be unnamed and unstructured.
1512 * However, the original schedule domain space may be named and/or
1513 * structured, so we have to take this possibility into account
1514 * while performing the transformations.
1516 __isl_give isl_ast_build
*isl_ast_build_insert_dim(
1517 __isl_take isl_ast_build
*build
, int pos
)
1520 isl_space
*space
, *ma_space
;
1524 build
= isl_ast_build_cow(build
);
1528 ctx
= isl_ast_build_get_ctx(build
);
1529 id
= isl_id_alloc(ctx
, "", NULL
);
1530 space
= isl_ast_build_get_space(build
, 1);
1531 build
->iterators
= isl_id_list_insert(build
->iterators
, pos
, id
);
1532 build
->domain
= isl_set_insert_dims(build
->domain
,
1533 isl_dim_set
, pos
, 1);
1534 build
->generated
= isl_set_insert_dims(build
->generated
,
1535 isl_dim_set
, pos
, 1);
1536 build
->pending
= isl_set_insert_dims(build
->pending
,
1537 isl_dim_set
, pos
, 1);
1538 build
->strides
= isl_vec_insert_els(build
->strides
, pos
, 1);
1539 build
->strides
= isl_vec_set_element_si(build
->strides
, pos
, 1);
1540 ma_space
= isl_space_params(isl_multi_aff_get_space(build
->offsets
));
1541 ma_space
= isl_space_set_from_params(ma_space
);
1542 ma_space
= isl_space_add_dims(ma_space
, isl_dim_set
, 1);
1543 ma_space
= isl_space_map_from_set(ma_space
);
1544 ma
= isl_multi_aff_zero(isl_space_copy(ma_space
));
1545 build
->offsets
= isl_multi_aff_splice(build
->offsets
, pos
, pos
, ma
);
1546 ma
= isl_multi_aff_identity(ma_space
);
1547 build
->values
= isl_multi_aff_splice(build
->values
, pos
, pos
, ma
);
1548 build
->options
= options_insert_dim(build
->options
, space
, pos
);
1550 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1551 !build
->pending
|| !build
->values
||
1552 !build
->strides
|| !build
->offsets
|| !build
->options
)
1553 return isl_ast_build_free(build
);
1558 /* Scale down the current dimension by a factor of "m".
1559 * "umap" is an isl_union_map that implements the scaling down.
1560 * That is, it is of the form
1562 * { [.... i ....] -> [.... i' ....] : i = m i' }
1564 * This function is called right after the strides have been
1565 * detected, but before any constraints on the current dimension
1566 * have been included in build->domain.
1567 * We therefore only need to update stride, offset and the options.
1569 __isl_give isl_ast_build
*isl_ast_build_scale_down(
1570 __isl_take isl_ast_build
*build
, __isl_take isl_val
*m
,
1571 __isl_take isl_union_map
*umap
)
1577 build
= isl_ast_build_cow(build
);
1578 if (!build
|| !umap
|| !m
)
1581 depth
= build
->depth
;
1583 v
= isl_vec_get_element_val(build
->strides
, depth
);
1584 v
= isl_val_div(v
, isl_val_copy(m
));
1585 build
->strides
= isl_vec_set_element_val(build
->strides
, depth
, v
);
1587 aff
= isl_multi_aff_get_aff(build
->offsets
, depth
);
1588 aff
= isl_aff_scale_down_val(aff
, m
);
1589 build
->offsets
= isl_multi_aff_set_aff(build
->offsets
, depth
, aff
);
1590 build
->options
= isl_union_map_apply_domain(build
->options
, umap
);
1591 if (!build
->strides
|| !build
->offsets
|| !build
->options
)
1592 return isl_ast_build_free(build
);
1597 isl_union_map_free(umap
);
1598 return isl_ast_build_free(build
);
1601 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1602 * If an isl_id with such a name already appears among the parameters
1603 * in build->domain, then adjust the name to "c%d_%d".
1605 static __isl_give isl_id_list
*generate_names(isl_ctx
*ctx
, int n
, int first
,
1606 __isl_keep isl_ast_build
*build
)
1611 names
= isl_id_list_alloc(ctx
, n
);
1612 for (i
= 0; i
< n
; ++i
) {
1615 id
= generate_name(ctx
, first
+ i
, build
);
1616 names
= isl_id_list_add(names
, id
);
1622 /* Embed "options" into the given isl_ast_build space.
1624 * This function is called from within a nested call to
1625 * isl_ast_build_ast_from_schedule.
1626 * "options" refers to the additional schedule,
1627 * while space refers to both the space of the outer isl_ast_build and
1628 * that of the additional schedule.
1629 * Specifically, space is of the form
1633 * while options lives in the space(s)
1641 * and compose this with options, to obtain the new options
1642 * living in the space(s)
1646 static __isl_give isl_union_map
*embed_options(
1647 __isl_take isl_union_map
*options
, __isl_take isl_space
*space
)
1651 map
= isl_map_universe(isl_space_unwrap(space
));
1652 map
= isl_map_range_map(map
);
1654 options
= isl_union_map_apply_range(
1655 isl_union_map_from_map(map
), options
);
1660 /* Update "build" for use in a (possibly nested) code generation. That is,
1661 * extend "build" from an AST build on some domain O to an AST build
1662 * on domain [O -> S], with S corresponding to "space".
1663 * If the original domain is a parameter domain, then the new domain is
1665 * "iterators" is a list of iterators for S, but the number of elements
1666 * may be smaller or greater than the number of set dimensions of S.
1667 * If "keep_iterators" is set, then any extra ids in build->iterators
1668 * are reused for S. Otherwise, these extra ids are dropped.
1670 * We first update build->outer_pos to the current depth.
1671 * This depth is zero in case this is the outermost code generation.
1673 * We then add additional ids such that the number of iterators is at least
1674 * equal to the dimension of the new build domain.
1676 * If the original domain is parametric, then we are constructing
1677 * an isl_ast_build for the outer code generation and we pass control
1678 * to isl_ast_build_init.
1680 * Otherwise, we adjust the fields of "build" to include "space".
1682 __isl_give isl_ast_build
*isl_ast_build_product(
1683 __isl_take isl_ast_build
*build
, __isl_take isl_space
*space
)
1688 isl_multi_aff
*embedding
;
1691 build
= isl_ast_build_cow(build
);
1695 build
->outer_pos
= build
->depth
;
1697 ctx
= isl_ast_build_get_ctx(build
);
1698 dim
= isl_set_dim(build
->domain
, isl_dim_set
);
1699 dim
+= isl_space_dim(space
, isl_dim_set
);
1700 n_it
= isl_id_list_n_id(build
->iterators
);
1703 l
= generate_names(ctx
, dim
- n_it
, n_it
, build
);
1704 build
->iterators
= isl_id_list_concat(build
->iterators
, l
);
1707 if (isl_set_is_params(build
->domain
))
1708 return isl_ast_build_init(build
, space
);
1710 set
= isl_set_universe(isl_space_copy(space
));
1711 build
->domain
= isl_set_product(build
->domain
, isl_set_copy(set
));
1712 build
->pending
= isl_set_product(build
->pending
, isl_set_copy(set
));
1713 build
->generated
= isl_set_product(build
->generated
, set
);
1715 strides
= isl_vec_alloc(ctx
, isl_space_dim(space
, isl_dim_set
));
1716 strides
= isl_vec_set_si(strides
, 1);
1717 build
->strides
= isl_vec_concat(build
->strides
, strides
);
1719 space
= isl_space_map_from_set(space
);
1720 build
->offsets
= isl_multi_aff_align_params(build
->offsets
,
1721 isl_space_copy(space
));
1722 build
->offsets
= isl_multi_aff_product(build
->offsets
,
1723 isl_multi_aff_zero(isl_space_copy(space
)));
1724 build
->values
= isl_multi_aff_align_params(build
->values
,
1725 isl_space_copy(space
));
1726 embedding
= isl_multi_aff_identity(space
);
1727 build
->values
= isl_multi_aff_product(build
->values
, embedding
);
1729 space
= isl_ast_build_get_space(build
, 1);
1730 build
->options
= embed_options(build
->options
, space
);
1732 if (!build
->iterators
|| !build
->domain
|| !build
->generated
||
1733 !build
->pending
|| !build
->values
||
1734 !build
->strides
|| !build
->offsets
|| !build
->options
)
1735 return isl_ast_build_free(build
);
1739 isl_ast_build_free(build
);
1740 isl_space_free(space
);
1744 /* Does "aff" only attain non-negative values over build->domain?
1745 * That is, does it not attain any negative values?
1747 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build
*build
,
1748 __isl_keep isl_aff
*aff
)
1756 aff
= isl_aff_copy(aff
);
1757 test
= isl_set_from_basic_set(isl_aff_neg_basic_set(aff
));
1758 test
= isl_set_intersect(test
, isl_set_copy(build
->domain
));
1759 empty
= isl_set_is_empty(test
);
1765 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1767 int isl_ast_build_has_stride(__isl_keep isl_ast_build
*build
, int pos
)
1775 v
= isl_vec_get_element_val(build
->strides
, pos
);
1778 has_stride
= !isl_val_is_one(v
);
1784 /* Given that the dimension at position "pos" takes on values
1788 * with a an integer, return s through *stride.
1790 __isl_give isl_val
*isl_ast_build_get_stride(__isl_keep isl_ast_build
*build
,
1796 return isl_vec_get_element_val(build
->strides
, pos
);
1799 /* Given that the dimension at position "pos" takes on values
1803 * with a an integer, return f.
1805 __isl_give isl_aff
*isl_ast_build_get_offset(
1806 __isl_keep isl_ast_build
*build
, int pos
)
1811 return isl_multi_aff_get_aff(build
->offsets
, pos
);
1814 /* Is the dimension at position "pos" known to attain only a single
1815 * value that, moreover, can be described by a single affine expression
1816 * in terms of the outer dimensions and parameters?
1818 * If not, then the correponding affine expression in build->values
1819 * is set to be equal to the same input dimension.
1820 * Otherwise, it is set to the requested expression in terms of
1821 * outer dimensions and parameters.
1823 int isl_ast_build_has_affine_value(__isl_keep isl_ast_build
*build
,
1832 aff
= isl_multi_aff_get_aff(build
->values
, pos
);
1833 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, pos
, 1);
1842 /* Plug in the known values (fixed affine expressions in terms of
1843 * parameters and outer loop iterators) of all loop iterators
1844 * in the domain of "umap".
1846 * We simply precompose "umap" with build->values.
1848 __isl_give isl_union_map
*isl_ast_build_substitute_values_union_map_domain(
1849 __isl_keep isl_ast_build
*build
, __isl_take isl_union_map
*umap
)
1851 isl_multi_aff
*values
;
1854 return isl_union_map_free(umap
);
1856 values
= isl_multi_aff_copy(build
->values
);
1857 umap
= isl_union_map_preimage_domain_multi_aff(umap
, values
);
1862 /* Is the current dimension known to attain only a single value?
1864 int isl_ast_build_has_value(__isl_keep isl_ast_build
*build
)
1869 return build
->value
!= NULL
;
1872 /* Simplify the basic set "bset" based on what we know about
1873 * the iterators of already generated loops.
1875 * "bset" is assumed to live in the (internal) schedule domain.
1877 __isl_give isl_basic_set
*isl_ast_build_compute_gist_basic_set(
1878 __isl_keep isl_ast_build
*build
, __isl_take isl_basic_set
*bset
)
1883 bset
= isl_basic_set_preimage_multi_aff(bset
,
1884 isl_multi_aff_copy(build
->values
));
1885 bset
= isl_basic_set_gist(bset
,
1886 isl_set_simple_hull(isl_set_copy(build
->domain
)));
1890 isl_basic_set_free(bset
);
1894 /* Simplify the set "set" based on what we know about
1895 * the iterators of already generated loops.
1897 * "set" is assumed to live in the (internal) schedule domain.
1899 __isl_give isl_set
*isl_ast_build_compute_gist(
1900 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
1905 set
= isl_set_preimage_multi_aff(set
,
1906 isl_multi_aff_copy(build
->values
));
1907 set
= isl_set_gist(set
, isl_set_copy(build
->domain
));
1915 /* Include information about what we know about the iterators of
1916 * already generated loops to "set".
1918 * We currently only plug in the known affine values of outer loop
1920 * In principle we could also introduce equalities or even other
1921 * constraints implied by the intersection of "set" and build->domain.
1923 __isl_give isl_set
*isl_ast_build_specialize(__isl_keep isl_ast_build
*build
,
1924 __isl_take isl_set
*set
)
1927 return isl_set_free(set
);
1929 return isl_set_preimage_multi_aff(set
,
1930 isl_multi_aff_copy(build
->values
));
1933 /* Simplify the map "map" based on what we know about
1934 * the iterators of already generated loops.
1936 * The domain of "map" is assumed to live in the (internal) schedule domain.
1938 __isl_give isl_map
*isl_ast_build_compute_gist_map_domain(
1939 __isl_keep isl_ast_build
*build
, __isl_take isl_map
*map
)
1944 map
= isl_map_gist_domain(map
, isl_set_copy(build
->domain
));
1952 /* Simplify the affine expression "aff" based on what we know about
1953 * the iterators of already generated loops.
1955 * The domain of "aff" is assumed to live in the (internal) schedule domain.
1957 __isl_give isl_aff
*isl_ast_build_compute_gist_aff(
1958 __isl_keep isl_ast_build
*build
, __isl_take isl_aff
*aff
)
1963 aff
= isl_aff_gist(aff
, isl_set_copy(build
->domain
));
1971 /* Simplify the piecewise affine expression "aff" based on what we know about
1972 * the iterators of already generated loops.
1974 * The domain of "pa" is assumed to live in the (internal) schedule domain.
1976 __isl_give isl_pw_aff
*isl_ast_build_compute_gist_pw_aff(
1977 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_aff
*pa
)
1982 if (!isl_set_is_params(build
->domain
))
1983 pa
= isl_pw_aff_pullback_multi_aff(pa
,
1984 isl_multi_aff_copy(build
->values
));
1985 pa
= isl_pw_aff_gist(pa
, isl_set_copy(build
->domain
));
1989 isl_pw_aff_free(pa
);
1993 /* Simplify the piecewise multi-affine expression "aff" based on what
1994 * we know about the iterators of already generated loops.
1996 * The domain of "pma" is assumed to live in the (internal) schedule domain.
1998 __isl_give isl_pw_multi_aff
*isl_ast_build_compute_gist_pw_multi_aff(
1999 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_multi_aff
*pma
)
2004 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
,
2005 isl_multi_aff_copy(build
->values
));
2006 pma
= isl_pw_multi_aff_gist(pma
, isl_set_copy(build
->domain
));
2010 isl_pw_multi_aff_free(pma
);
2014 /* Extract the schedule domain of the given type from build->options
2015 * at the current depth.
2017 * In particular, find the subset of build->options that is of
2018 * the following form
2020 * schedule_domain -> type[depth]
2022 * and return the corresponding domain, after eliminating inner dimensions
2023 * and divs that depend on the current dimension.
2025 * Note that the domain of build->options has been reformulated
2026 * in terms of the internal build space in embed_options,
2027 * but the position is still that within the current code generation.
2029 __isl_give isl_set
*isl_ast_build_get_option_domain(
2030 __isl_keep isl_ast_build
*build
,
2031 enum isl_ast_build_domain_type type
)
2042 name
= option_str
[type
];
2043 local_pos
= build
->depth
- build
->outer_pos
;
2045 space
= isl_ast_build_get_space(build
, 1);
2046 space
= isl_space_from_domain(space
);
2047 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2048 space
= isl_space_set_tuple_name(space
, isl_dim_out
, name
);
2050 option
= isl_union_map_extract_map(build
->options
, space
);
2051 option
= isl_map_fix_si(option
, isl_dim_out
, 0, local_pos
);
2053 domain
= isl_map_domain(option
);
2054 domain
= isl_ast_build_eliminate(build
, domain
);
2059 /* Extract the separation class mapping at the current depth.
2061 * In particular, find and return the subset of build->options that is of
2062 * the following form
2064 * schedule_domain -> separation_class[[depth] -> [class]]
2066 * The caller is expected to eliminate inner dimensions from the domain.
2068 * Note that the domain of build->options has been reformulated
2069 * in terms of the internal build space in embed_options,
2070 * but the position is still that within the current code generation.
2072 __isl_give isl_map
*isl_ast_build_get_separation_class(
2073 __isl_keep isl_ast_build
*build
)
2076 isl_space
*space_sep
, *space
;
2083 local_pos
= build
->depth
- build
->outer_pos
;
2084 ctx
= isl_ast_build_get_ctx(build
);
2085 space_sep
= isl_space_alloc(ctx
, 0, 1, 1);
2086 space_sep
= isl_space_wrap(space_sep
);
2087 space_sep
= isl_space_set_tuple_name(space_sep
, isl_dim_set
,
2088 "separation_class");
2089 space
= isl_ast_build_get_space(build
, 1);
2090 space_sep
= isl_space_align_params(space_sep
, isl_space_copy(space
));
2091 space
= isl_space_map_from_domain_and_range(space
, space_sep
);
2093 res
= isl_union_map_extract_map(build
->options
, space
);
2094 res
= isl_map_fix_si(res
, isl_dim_out
, 0, local_pos
);
2095 res
= isl_map_coalesce(res
);
2100 /* Eliminate dimensions inner to the current dimension.
2102 __isl_give isl_set
*isl_ast_build_eliminate_inner(
2103 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2109 return isl_set_free(set
);
2111 dim
= isl_set_dim(set
, isl_dim_set
);
2112 depth
= build
->depth
;
2113 set
= isl_set_detect_equalities(set
);
2114 set
= isl_set_eliminate(set
, isl_dim_set
, depth
+ 1, dim
- (depth
+ 1));
2119 /* Eliminate unknown divs and divs that depend on the current dimension.
2121 * Note that during the elimination of unknown divs, we may discover
2122 * an explicit representation of some other unknown divs, which may
2123 * depend on the current dimension. We therefore need to eliminate
2124 * unknown divs first.
2126 __isl_give isl_set
*isl_ast_build_eliminate_divs(
2127 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
2132 return isl_set_free(set
);
2134 set
= isl_set_remove_unknown_divs(set
);
2135 depth
= build
->depth
;
2136 set
= isl_set_remove_divs_involving_dims(set
, isl_dim_set
, depth
, 1);
2141 /* Eliminate dimensions inner to the current dimension as well as
2142 * unknown divs and divs that depend on the current dimension.
2143 * The result then consists only of constraints that are independent
2144 * of the current dimension and upper and lower bounds on the current
2147 __isl_give isl_set
*isl_ast_build_eliminate(
2148 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*domain
)
2150 domain
= isl_ast_build_eliminate_inner(build
, domain
);
2151 domain
= isl_ast_build_eliminate_divs(build
, domain
);
2155 /* Replace build->single_valued by "sv".
2157 __isl_give isl_ast_build
*isl_ast_build_set_single_valued(
2158 __isl_take isl_ast_build
*build
, int sv
)
2162 if (build
->single_valued
== sv
)
2164 build
= isl_ast_build_cow(build
);
2167 build
->single_valued
= sv
;