2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
5 * Copyright 2012 Universiteit Leiden
6 * Copyright 2014 Ecole Normale Superieure
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
13 * B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
20 #include <isl/space.h>
23 #include <isl/union_set.h>
24 #include <isl/union_map.h>
26 #include <isl/schedule_node.h>
28 #include <isl/stream.h>
30 enum isl_restriction_type
{
31 isl_restriction_type_empty
,
32 isl_restriction_type_none
,
33 isl_restriction_type_input
,
34 isl_restriction_type_output
37 struct isl_restriction
{
38 enum isl_restriction_type type
;
44 /* Create a restriction of the given type.
46 static __isl_give isl_restriction
*isl_restriction_alloc(
47 __isl_take isl_map
*source_map
, enum isl_restriction_type type
)
50 isl_restriction
*restr
;
55 ctx
= isl_map_get_ctx(source_map
);
56 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
62 isl_map_free(source_map
);
65 isl_map_free(source_map
);
69 /* Create a restriction that doesn't restrict anything.
71 __isl_give isl_restriction
*isl_restriction_none(__isl_take isl_map
*source_map
)
73 return isl_restriction_alloc(source_map
, isl_restriction_type_none
);
76 /* Create a restriction that removes everything.
78 __isl_give isl_restriction
*isl_restriction_empty(
79 __isl_take isl_map
*source_map
)
81 return isl_restriction_alloc(source_map
, isl_restriction_type_empty
);
84 /* Create a restriction on the input of the maximization problem
85 * based on the given source and sink restrictions.
87 __isl_give isl_restriction
*isl_restriction_input(
88 __isl_take isl_set
*source_restr
, __isl_take isl_set
*sink_restr
)
91 isl_restriction
*restr
;
93 if (!source_restr
|| !sink_restr
)
96 ctx
= isl_set_get_ctx(source_restr
);
97 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
101 restr
->type
= isl_restriction_type_input
;
102 restr
->source
= source_restr
;
103 restr
->sink
= sink_restr
;
107 isl_set_free(source_restr
);
108 isl_set_free(sink_restr
);
112 /* Create a restriction on the output of the maximization problem
113 * based on the given source restriction.
115 __isl_give isl_restriction
*isl_restriction_output(
116 __isl_take isl_set
*source_restr
)
119 isl_restriction
*restr
;
124 ctx
= isl_set_get_ctx(source_restr
);
125 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
129 restr
->type
= isl_restriction_type_output
;
130 restr
->source
= source_restr
;
134 isl_set_free(source_restr
);
138 __isl_null isl_restriction
*isl_restriction_free(
139 __isl_take isl_restriction
*restr
)
144 isl_set_free(restr
->source
);
145 isl_set_free(restr
->sink
);
150 isl_ctx
*isl_restriction_get_ctx(__isl_keep isl_restriction
*restr
)
152 return restr
? isl_set_get_ctx(restr
->source
) : NULL
;
155 /* A private structure to keep track of a mapping together with
156 * a user-specified identifier and a boolean indicating whether
157 * the map represents a must or may access/dependence.
159 struct isl_labeled_map
{
165 typedef isl_bool (*isl_access_coscheduled
)(void *first
, void *second
);
167 /* A structure containing the input for dependence analysis:
169 * - n_must + n_may (<= max_source) sources
170 * - a function for determining the relative order of sources and sink
171 * - an optional function "coscheduled" for determining whether sources
172 * may be coscheduled. If "coscheduled" is NULL, then the sources
173 * are assumed not to be coscheduled.
174 * The must sources are placed before the may sources.
176 * domain_map is an auxiliary map that maps the sink access relation
177 * to the domain of this access relation.
178 * This field is only needed when restrict_fn is set and
179 * the field itself is set by isl_access_info_compute_flow.
181 * restrict_fn is a callback that (if not NULL) will be called
182 * right before any lexicographical maximization.
184 struct isl_access_info
{
186 struct isl_labeled_map sink
;
187 isl_access_level_before level_before
;
188 isl_access_coscheduled coscheduled
;
190 isl_access_restrict restrict_fn
;
196 struct isl_labeled_map source
[1];
199 /* A structure containing the output of dependence analysis:
200 * - n_source dependences
201 * - a wrapped subset of the sink for which definitely no source could be found
202 * - a wrapped subset of the sink for which possibly no source could be found
205 isl_set
*must_no_source
;
206 isl_set
*may_no_source
;
208 struct isl_labeled_map
*dep
;
211 /* Construct an isl_access_info structure and fill it up with
212 * the given data. The number of sources is set to 0.
214 __isl_give isl_access_info
*isl_access_info_alloc(__isl_take isl_map
*sink
,
215 void *sink_user
, isl_access_level_before fn
, int max_source
)
218 struct isl_access_info
*acc
;
223 ctx
= isl_map_get_ctx(sink
);
224 isl_assert(ctx
, max_source
>= 0, goto error
);
226 acc
= isl_calloc(ctx
, struct isl_access_info
,
227 sizeof(struct isl_access_info
) +
228 (max_source
- 1) * sizeof(struct isl_labeled_map
));
232 acc
->sink
.map
= sink
;
233 acc
->sink
.data
= sink_user
;
234 acc
->level_before
= fn
;
235 acc
->max_source
= max_source
;
245 /* Free the given isl_access_info structure.
247 __isl_null isl_access_info
*isl_access_info_free(
248 __isl_take isl_access_info
*acc
)
254 isl_map_free(acc
->domain_map
);
255 isl_map_free(acc
->sink
.map
);
256 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
257 isl_map_free(acc
->source
[i
].map
);
262 isl_ctx
*isl_access_info_get_ctx(__isl_keep isl_access_info
*acc
)
264 return acc
? isl_map_get_ctx(acc
->sink
.map
) : NULL
;
267 __isl_give isl_access_info
*isl_access_info_set_restrict(
268 __isl_take isl_access_info
*acc
, isl_access_restrict fn
, void *user
)
272 acc
->restrict_fn
= fn
;
273 acc
->restrict_user
= user
;
277 /* Add another source to an isl_access_info structure, making
278 * sure the "must" sources are placed before the "may" sources.
279 * This function may be called at most max_source times on a
280 * given isl_access_info structure, with max_source as specified
281 * in the call to isl_access_info_alloc that constructed the structure.
283 __isl_give isl_access_info
*isl_access_info_add_source(
284 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
285 int must
, void *source_user
)
291 ctx
= isl_map_get_ctx(acc
->sink
.map
);
292 isl_assert(ctx
, acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
296 acc
->source
[acc
->n_must
+ acc
->n_may
] =
297 acc
->source
[acc
->n_must
];
298 acc
->source
[acc
->n_must
].map
= source
;
299 acc
->source
[acc
->n_must
].data
= source_user
;
300 acc
->source
[acc
->n_must
].must
= 1;
303 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
304 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
305 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
311 isl_map_free(source
);
312 isl_access_info_free(acc
);
316 /* A helper struct carrying the isl_access_info and an error condition.
318 struct access_sort_info
{
319 isl_access_info
*access_info
;
323 /* Return -n, 0 or n (with n a positive value), depending on whether
324 * the source access identified by p1 should be sorted before, together
325 * or after that identified by p2.
327 * If p1 appears before p2, then it should be sorted first.
328 * For more generic initial schedules, it is possible that neither
329 * p1 nor p2 appears before the other, or at least not in any obvious way.
330 * We therefore also check if p2 appears before p1, in which case p2
331 * should be sorted first.
332 * If not, we try to order the two statements based on the description
333 * of the iteration domains. This results in an arbitrary, but fairly
336 * In case of an error, sort_info.error is set to true and all elements are
337 * reported to be equal.
339 static int access_sort_cmp(const void *p1
, const void *p2
, void *user
)
341 struct access_sort_info
*sort_info
= user
;
342 isl_access_info
*acc
= sort_info
->access_info
;
344 if (sort_info
->error
)
347 const struct isl_labeled_map
*i1
, *i2
;
350 i1
= (const struct isl_labeled_map
*) p1
;
351 i2
= (const struct isl_labeled_map
*) p2
;
353 level1
= acc
->level_before(i1
->data
, i2
->data
);
359 level2
= acc
->level_before(i2
->data
, i1
->data
);
365 h1
= isl_map_get_hash(i1
->map
);
366 h2
= isl_map_get_hash(i2
->map
);
367 return h1
> h2
? 1 : h1
< h2
? -1 : 0;
369 sort_info
->error
= 1;
373 /* Sort the must source accesses in their textual order.
375 static __isl_give isl_access_info
*isl_access_info_sort_sources(
376 __isl_take isl_access_info
*acc
)
378 struct access_sort_info sort_info
;
380 sort_info
.access_info
= acc
;
385 if (acc
->n_must
<= 1)
388 if (isl_sort(acc
->source
, acc
->n_must
, sizeof(struct isl_labeled_map
),
389 access_sort_cmp
, &sort_info
) < 0)
390 return isl_access_info_free(acc
);
392 return isl_access_info_free(acc
);
397 /* Align the parameters of the two spaces if needed and then call
400 static __isl_give isl_space
*space_align_and_join(__isl_take isl_space
*left
,
401 __isl_take isl_space
*right
)
403 isl_bool equal_params
;
405 equal_params
= isl_space_has_equal_params(left
, right
);
406 if (equal_params
< 0)
409 return isl_space_join(left
, right
);
411 left
= isl_space_align_params(left
, isl_space_copy(right
));
412 right
= isl_space_align_params(right
, isl_space_copy(left
));
413 return isl_space_join(left
, right
);
415 isl_space_free(left
);
416 isl_space_free(right
);
420 /* Initialize an empty isl_flow structure corresponding to a given
421 * isl_access_info structure.
422 * For each must access, two dependences are created (initialized
423 * to the empty relation), one for the resulting must dependences
424 * and one for the resulting may dependences. May accesses can
425 * only lead to may dependences, so only one dependence is created
427 * This function is private as isl_flow structures are only supposed
428 * to be created by isl_access_info_compute_flow.
430 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
434 struct isl_flow
*dep
;
439 ctx
= isl_map_get_ctx(acc
->sink
.map
);
440 dep
= isl_calloc_type(ctx
, struct isl_flow
);
444 n
= 2 * acc
->n_must
+ acc
->n_may
;
445 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
450 for (i
= 0; i
< acc
->n_must
; ++i
) {
452 dim
= space_align_and_join(
453 isl_map_get_space(acc
->source
[i
].map
),
454 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
455 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
456 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
457 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
458 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
459 dep
->dep
[2 * i
].must
= 1;
460 dep
->dep
[2 * i
+ 1].must
= 0;
461 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
464 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
466 dim
= space_align_and_join(
467 isl_map_get_space(acc
->source
[i
].map
),
468 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
469 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
470 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
471 dep
->dep
[acc
->n_must
+ i
].must
= 0;
472 if (!dep
->dep
[acc
->n_must
+ i
].map
)
482 /* Iterate over all sources and for each resulting flow dependence
483 * that is not empty, call the user specfied function.
484 * The second argument in this function call identifies the source,
485 * while the third argument correspond to the final argument of
486 * the isl_flow_foreach call.
488 isl_stat
isl_flow_foreach(__isl_keep isl_flow
*deps
,
489 isl_stat (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
,
496 return isl_stat_error
;
498 for (i
= 0; i
< deps
->n_source
; ++i
) {
499 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
501 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
502 deps
->dep
[i
].data
, user
) < 0)
503 return isl_stat_error
;
509 /* Return a copy of the subset of the sink for which no source could be found.
511 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
517 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
519 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
522 __isl_null isl_flow
*isl_flow_free(__isl_take isl_flow
*deps
)
528 isl_set_free(deps
->must_no_source
);
529 isl_set_free(deps
->may_no_source
);
531 for (i
= 0; i
< deps
->n_source
; ++i
)
532 isl_map_free(deps
->dep
[i
].map
);
540 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
542 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
545 /* Return a map that enforces that the domain iteration occurs after
546 * the range iteration at the given level.
547 * If level is odd, then the domain iteration should occur after
548 * the target iteration in their shared level/2 outermost loops.
549 * In this case we simply need to enforce that these outermost
550 * loop iterations are the same.
551 * If level is even, then the loop iterator of the domain should
552 * be greater than the loop iterator of the range at the last
553 * of the level/2 shared loops, i.e., loop level/2 - 1.
555 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
557 struct isl_basic_map
*bmap
;
560 bmap
= isl_basic_map_equal(dim
, level
/2);
562 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
564 return isl_map_from_basic_map(bmap
);
567 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
568 * but first check if the user has set acc->restrict_fn and if so
569 * update either the input or the output of the maximization problem
570 * with respect to the resulting restriction.
572 * Since the user expects a mapping from sink iterations to source iterations,
573 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
574 * to accessed array elements, we first need to project out the accessed
575 * sink array elements by applying acc->domain_map.
576 * Similarly, the sink restriction specified by the user needs to be
577 * converted back to the wrapped map.
579 static __isl_give isl_map
*restricted_partial_lexmax(
580 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
581 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
584 isl_restriction
*restr
;
585 isl_set
*sink_domain
;
589 if (!acc
->restrict_fn
)
590 return isl_map_partial_lexmax(dep
, sink
, empty
);
592 source_map
= isl_map_copy(dep
);
593 source_map
= isl_map_apply_domain(source_map
,
594 isl_map_copy(acc
->domain_map
));
595 sink_domain
= isl_set_copy(sink
);
596 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
597 restr
= acc
->restrict_fn(source_map
, sink_domain
,
598 acc
->source
[source
].data
, acc
->restrict_user
);
599 isl_set_free(sink_domain
);
600 isl_map_free(source_map
);
604 if (restr
->type
== isl_restriction_type_input
) {
605 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
606 sink_restr
= isl_set_copy(restr
->sink
);
607 sink_restr
= isl_set_apply(sink_restr
,
608 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
609 sink
= isl_set_intersect(sink
, sink_restr
);
610 } else if (restr
->type
== isl_restriction_type_empty
) {
611 isl_space
*space
= isl_map_get_space(dep
);
613 dep
= isl_map_empty(space
);
616 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
618 if (restr
->type
== isl_restriction_type_output
)
619 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
621 isl_restriction_free(restr
);
630 /* Compute the last iteration of must source j that precedes the sink
631 * at the given level for sink iterations in set_C.
632 * The subset of set_C for which no such iteration can be found is returned
635 static struct isl_map
*last_source(struct isl_access_info
*acc
,
636 struct isl_set
*set_C
,
637 int j
, int level
, struct isl_set
**empty
)
639 struct isl_map
*read_map
;
640 struct isl_map
*write_map
;
641 struct isl_map
*dep_map
;
642 struct isl_map
*after
;
643 struct isl_map
*result
;
645 read_map
= isl_map_copy(acc
->sink
.map
);
646 write_map
= isl_map_copy(acc
->source
[j
].map
);
647 write_map
= isl_map_reverse(write_map
);
648 dep_map
= isl_map_apply_range(read_map
, write_map
);
649 after
= after_at_level(isl_map_get_space(dep_map
), level
);
650 dep_map
= isl_map_intersect(dep_map
, after
);
651 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
652 result
= isl_map_reverse(result
);
657 /* For a given mapping between iterations of must source j and iterations
658 * of the sink, compute the last iteration of must source k preceding
659 * the sink at level before_level for any of the sink iterations,
660 * but following the corresponding iteration of must source j at level
663 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
664 struct isl_map
*old_map
,
665 int j
, int before_level
,
666 int k
, int after_level
,
667 struct isl_set
**empty
)
670 struct isl_set
*set_C
;
671 struct isl_map
*read_map
;
672 struct isl_map
*write_map
;
673 struct isl_map
*dep_map
;
674 struct isl_map
*after_write
;
675 struct isl_map
*before_read
;
676 struct isl_map
*result
;
678 set_C
= isl_map_range(isl_map_copy(old_map
));
679 read_map
= isl_map_copy(acc
->sink
.map
);
680 write_map
= isl_map_copy(acc
->source
[k
].map
);
682 write_map
= isl_map_reverse(write_map
);
683 dep_map
= isl_map_apply_range(read_map
, write_map
);
684 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
685 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
686 after_write
= after_at_level(dim
, after_level
);
687 after_write
= isl_map_apply_range(after_write
, old_map
);
688 after_write
= isl_map_reverse(after_write
);
689 dep_map
= isl_map_intersect(dep_map
, after_write
);
690 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
691 dep_map
= isl_map_intersect(dep_map
, before_read
);
692 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
693 result
= isl_map_reverse(result
);
698 /* Given a shared_level between two accesses, return 1 if the
699 * the first can precede the second at the requested target_level.
700 * If the target level is odd, i.e., refers to a statement level
701 * dimension, then first needs to precede second at the requested
702 * level, i.e., shared_level must be equal to target_level.
703 * If the target level is odd, then the two loops should share
704 * at least the requested number of outer loops.
706 static int can_precede_at_level(int shared_level
, int target_level
)
708 if (shared_level
< target_level
)
710 if ((target_level
% 2) && shared_level
> target_level
)
715 /* Given a possible flow dependence temp_rel[j] between source j and the sink
716 * at level sink_level, remove those elements for which
717 * there is an iteration of another source k < j that is closer to the sink.
718 * The flow dependences temp_rel[k] are updated with the improved sources.
719 * Any improved source needs to precede the sink at the same level
720 * and needs to follow source j at the same or a deeper level.
721 * The lower this level, the later the execution date of source k.
722 * We therefore consider lower levels first.
724 * If temp_rel[j] is empty, then there can be no improvement and
725 * we return immediately.
727 * This function returns isl_stat_ok in case it was executed successfully and
728 * isl_stat_error in case of errors during the execution of this function.
730 static isl_stat
intermediate_sources(__isl_keep isl_access_info
*acc
,
731 struct isl_map
**temp_rel
, int j
, int sink_level
)
734 isl_size n_in
= isl_map_dim(acc
->source
[j
].map
, isl_dim_in
);
735 int depth
= 2 * n_in
+ 1;
738 return isl_stat_error
;
739 if (isl_map_plain_is_empty(temp_rel
[j
]))
742 for (k
= j
- 1; k
>= 0; --k
) {
744 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
746 return isl_stat_error
;
747 if (!can_precede_at_level(plevel
, sink_level
))
750 plevel2
= acc
->level_before(acc
->source
[j
].data
,
751 acc
->source
[k
].data
);
753 return isl_stat_error
;
755 for (level
= sink_level
; level
<= depth
; ++level
) {
757 struct isl_set
*trest
;
758 struct isl_map
*copy
;
760 if (!can_precede_at_level(plevel2
, level
))
763 copy
= isl_map_copy(temp_rel
[j
]);
764 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
766 if (isl_map_plain_is_empty(T
)) {
771 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
772 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
779 /* Compute all iterations of may source j that precedes the sink at the given
780 * level for sink iterations in set_C.
782 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
783 __isl_take isl_set
*set_C
, int j
, int level
)
790 read_map
= isl_map_copy(acc
->sink
.map
);
791 read_map
= isl_map_intersect_domain(read_map
, set_C
);
792 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
793 write_map
= isl_map_reverse(write_map
);
794 dep_map
= isl_map_apply_range(read_map
, write_map
);
795 after
= after_at_level(isl_map_get_space(dep_map
), level
);
796 dep_map
= isl_map_intersect(dep_map
, after
);
798 return isl_map_reverse(dep_map
);
801 /* For a given mapping between iterations of must source k and iterations
802 * of the sink, compute all iterations of may source j preceding
803 * the sink at level before_level for any of the sink iterations,
804 * but following the corresponding iteration of must source k at level
807 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
808 __isl_take isl_map
*old_map
,
809 int j
, int before_level
, int k
, int after_level
)
816 isl_map
*after_write
;
817 isl_map
*before_read
;
819 set_C
= isl_map_range(isl_map_copy(old_map
));
820 read_map
= isl_map_copy(acc
->sink
.map
);
821 read_map
= isl_map_intersect_domain(read_map
, set_C
);
822 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
824 write_map
= isl_map_reverse(write_map
);
825 dep_map
= isl_map_apply_range(read_map
, write_map
);
826 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
827 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
828 after_write
= after_at_level(dim
, after_level
);
829 after_write
= isl_map_apply_range(after_write
, old_map
);
830 after_write
= isl_map_reverse(after_write
);
831 dep_map
= isl_map_intersect(dep_map
, after_write
);
832 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
833 dep_map
= isl_map_intersect(dep_map
, before_read
);
834 return isl_map_reverse(dep_map
);
837 /* Given the must and may dependence relations for the must accesses
838 * for level sink_level, check if there are any accesses of may access j
839 * that occur in between and return their union.
840 * If some of these accesses are intermediate with respect to
841 * (previously thought to be) must dependences, then these
842 * must dependences are turned into may dependences.
844 static __isl_give isl_map
*all_intermediate_sources(
845 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
846 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
847 int j
, int sink_level
)
850 isl_size n_in
= isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
852 int depth
= 2 * n_in
+ 1;
855 return isl_map_free(map
);
856 for (k
= 0; k
< acc
->n_must
; ++k
) {
859 if (isl_map_plain_is_empty(may_rel
[k
]) &&
860 isl_map_plain_is_empty(must_rel
[k
]))
863 plevel
= acc
->level_before(acc
->source
[k
].data
,
864 acc
->source
[acc
->n_must
+ j
].data
);
866 return isl_map_free(map
);
868 for (level
= sink_level
; level
<= depth
; ++level
) {
873 if (!can_precede_at_level(plevel
, level
))
876 copy
= isl_map_copy(may_rel
[k
]);
877 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
878 map
= isl_map_union(map
, T
);
880 copy
= isl_map_copy(must_rel
[k
]);
881 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
882 ran
= isl_map_range(isl_map_copy(T
));
883 map
= isl_map_union(map
, T
);
884 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
885 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
887 T
= isl_map_from_domain_and_range(
889 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
891 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
898 /* Given a dependence relation "old_map" between a must-source and the sink,
899 * return a subset of the dependences, augmented with instances
900 * of the source at position "pos" in "acc" that are coscheduled
901 * with the must-source and that access the same element.
902 * That is, if the input lives in a space T -> K, then the output
903 * lives in the space [T -> S] -> K, with S the space of source "pos", and
904 * the domain factor of the domain product is a subset of the input.
905 * The sources are considered to be coscheduled if they have the same values
906 * for the initial "depth" coordinates.
908 * First construct a dependence relation S -> K and a mapping
909 * between coscheduled sources T -> S.
910 * The second is combined with the original dependence relation T -> K
911 * to form a relation in T -> [S -> K], which is subsequently
912 * uncurried to [T -> S] -> K.
913 * This result is then intersected with the dependence relation S -> K
914 * to form the output.
916 * In case a negative depth is given, NULL is returned to indicate an error.
918 static __isl_give isl_map
*coscheduled_source(__isl_keep isl_access_info
*acc
,
919 __isl_keep isl_map
*old_map
, int pos
, int depth
)
932 set_C
= isl_map_range(isl_map_copy(old_map
));
933 read_map
= isl_map_copy(acc
->sink
.map
);
934 read_map
= isl_map_intersect_domain(read_map
, set_C
);
935 write_map
= isl_map_copy(acc
->source
[pos
].map
);
936 dep_map
= isl_map_domain_product(write_map
, read_map
);
937 dep_map
= isl_set_unwrap(isl_map_domain(dep_map
));
938 space
= isl_space_join(isl_map_get_space(old_map
),
939 isl_space_reverse(isl_map_get_space(dep_map
)));
940 equal
= isl_map_from_basic_map(isl_basic_map_equal(space
, depth
));
941 map
= isl_map_range_product(equal
, isl_map_copy(old_map
));
942 map
= isl_map_uncurry(map
);
943 map
= isl_map_intersect_domain_factor_range(map
, dep_map
);
948 /* After the dependences derived from a must-source have been computed
949 * at a certain level, check if any of the sources of the must-dependences
950 * may be coscheduled with other sources.
951 * If they are any such sources, then there is no way of determining
952 * which of the sources actually comes last and the must-dependences
953 * need to be turned into may-dependences, while dependences from
954 * the other sources need to be added to the may-dependences as well.
955 * "acc" describes the sources and a callback for checking whether
956 * two sources may be coscheduled. If acc->coscheduled is NULL then
957 * the sources are assumed not to be coscheduled.
958 * "must_rel" and "may_rel" describe the must and may-dependence relations
959 * computed at the current level for the must-sources. Some of the dependences
960 * may be moved from "must_rel" to "may_rel".
961 * "flow" contains all dependences computed so far (apart from those
962 * in "must_rel" and "may_rel") and may be updated with additional
963 * dependences derived from may-sources.
965 * In particular, consider all the must-sources with a non-empty
966 * dependence relation in "must_rel". They are considered in reverse
967 * order because that is the order in which they are considered in the caller.
968 * If any of the must-sources are coscheduled, then the last one
969 * is the one that will have a corresponding dependence relation.
970 * For each must-source i, consider both all the previous must-sources
971 * and all the may-sources. If any of those may be coscheduled with
972 * must-source i, then compute the coscheduled instances that access
973 * the same memory elements. The result is a relation [T -> S] -> K.
974 * The projection onto T -> K is a subset of the must-dependence relation
975 * that needs to be turned into may-dependences.
976 * The projection onto S -> K needs to be added to the may-dependences
978 * Since a given must-source instance may be coscheduled with several
979 * other source instances, the dependences that need to be turned
980 * into may-dependences are first collected and only actually removed
981 * from the must-dependences after all other sources have been considered.
983 static __isl_give isl_flow
*handle_coscheduled(__isl_keep isl_access_info
*acc
,
984 __isl_keep isl_map
**must_rel
, __isl_keep isl_map
**may_rel
,
985 __isl_take isl_flow
*flow
)
989 if (!acc
->coscheduled
)
991 for (i
= acc
->n_must
- 1; i
>= 0; --i
) {
994 if (isl_map_plain_is_empty(must_rel
[i
]))
996 move
= isl_map_empty(isl_map_get_space(must_rel
[i
]));
997 for (j
= i
- 1; j
>= 0; --j
) {
999 isl_bool coscheduled
;
1000 isl_map
*map
, *factor
;
1002 coscheduled
= acc
->coscheduled(acc
->source
[i
].data
,
1003 acc
->source
[j
].data
);
1004 if (coscheduled
< 0) {
1006 return isl_flow_free(flow
);
1010 depth
= acc
->level_before(acc
->source
[i
].data
,
1011 acc
->source
[j
].data
) / 2;
1012 map
= coscheduled_source(acc
, must_rel
[i
], j
, depth
);
1013 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
1014 may_rel
[j
] = isl_map_union(may_rel
[j
], factor
);
1015 map
= isl_map_domain_factor_domain(map
);
1016 move
= isl_map_union(move
, map
);
1018 for (j
= 0; j
< acc
->n_may
; ++j
) {
1020 isl_bool coscheduled
;
1021 isl_map
*map
, *factor
;
1023 pos
= acc
->n_must
+ j
;
1024 coscheduled
= acc
->coscheduled(acc
->source
[i
].data
,
1025 acc
->source
[pos
].data
);
1026 if (coscheduled
< 0) {
1028 return isl_flow_free(flow
);
1032 depth
= acc
->level_before(acc
->source
[i
].data
,
1033 acc
->source
[pos
].data
) / 2;
1034 map
= coscheduled_source(acc
, must_rel
[i
], pos
, depth
);
1035 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
1036 pos
= 2 * acc
->n_must
+ j
;
1037 flow
->dep
[pos
].map
= isl_map_union(flow
->dep
[pos
].map
,
1039 map
= isl_map_domain_factor_domain(map
);
1040 move
= isl_map_union(move
, map
);
1042 must_rel
[i
] = isl_map_subtract(must_rel
[i
], isl_map_copy(move
));
1043 may_rel
[i
] = isl_map_union(may_rel
[i
], move
);
1049 /* Compute dependences for the case where all accesses are "may"
1050 * accesses, which boils down to computing memory based dependences.
1051 * The generic algorithm would also work in this case, but it would
1052 * be overkill to use it.
1054 static __isl_give isl_flow
*compute_mem_based_dependences(
1055 __isl_keep isl_access_info
*acc
)
1062 res
= isl_flow_alloc(acc
);
1066 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1067 maydo
= isl_set_copy(mustdo
);
1069 for (i
= 0; i
< acc
->n_may
; ++i
) {
1076 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
1080 is_before
= plevel
& 1;
1083 dim
= isl_map_get_space(res
->dep
[i
].map
);
1085 before
= isl_map_lex_le_first(dim
, plevel
);
1087 before
= isl_map_lex_lt_first(dim
, plevel
);
1088 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
1089 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
1090 dep
= isl_map_intersect(dep
, before
);
1091 mustdo
= isl_set_subtract(mustdo
,
1092 isl_map_range(isl_map_copy(dep
)));
1093 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
1096 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
1097 res
->must_no_source
= mustdo
;
1101 isl_set_free(mustdo
);
1102 isl_set_free(maydo
);
1107 /* Compute dependences for the case where there is at least one
1110 * The core algorithm considers all levels in which a source may precede
1111 * the sink, where a level may either be a statement level or a loop level.
1112 * The outermost statement level is 1, the first loop level is 2, etc...
1113 * The algorithm basically does the following:
1114 * for all levels l of the read access from innermost to outermost
1115 * for all sources w that may precede the sink access at that level
1116 * compute the last iteration of the source that precedes the sink access
1118 * add result to possible last accesses at level l of source w
1119 * for all sources w2 that we haven't considered yet at this level that may
1120 * also precede the sink access
1121 * for all levels l2 of w from l to innermost
1122 * for all possible last accesses dep of w at l
1123 * compute last iteration of w2 between the source and sink
1125 * add result to possible last accesses at level l of write w2
1126 * and replace possible last accesses dep by the remainder
1129 * The above algorithm is applied to the must access. During the course
1130 * of the algorithm, we keep track of sink iterations that still
1131 * need to be considered. These iterations are split into those that
1132 * haven't been matched to any source access (mustdo) and those that have only
1133 * been matched to may accesses (maydo).
1134 * At the end of each level, must-sources and may-sources that are coscheduled
1135 * with the sources of the must-dependences at that level are considered.
1136 * If any coscheduled instances are found, then corresponding may-dependences
1137 * are added and the original must-dependences are turned into may-dependences.
1138 * Afterwards, the may accesses that occur after must-dependence sources
1140 * In particular, we consider may accesses that precede the remaining
1141 * sink iterations, moving elements from mustdo to maydo when appropriate,
1142 * and may accesses that occur between a must source and a sink of any
1143 * dependences found at the current level, turning must dependences into
1144 * may dependences when appropriate.
1147 static __isl_give isl_flow
*compute_val_based_dependences(
1148 __isl_keep isl_access_info
*acc
)
1152 isl_set
*mustdo
= NULL
;
1153 isl_set
*maydo
= NULL
;
1157 isl_map
**must_rel
= NULL
;
1158 isl_map
**may_rel
= NULL
;
1163 res
= isl_flow_alloc(acc
);
1166 ctx
= isl_map_get_ctx(acc
->sink
.map
);
1168 n_in
= isl_map_dim(acc
->sink
.map
, isl_dim_in
);
1171 depth
= 2 * n_in
+ 1;
1172 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1173 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
1174 if (!mustdo
|| !maydo
)
1176 if (isl_set_plain_is_empty(mustdo
))
1179 must_rel
= isl_calloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1180 may_rel
= isl_calloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1181 if (!must_rel
|| !may_rel
)
1184 for (level
= depth
; level
>= 1; --level
) {
1185 for (j
= acc
->n_must
-1; j
>=0; --j
) {
1187 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
1188 must_rel
[j
] = isl_map_empty(space
);
1189 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
1192 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1194 struct isl_set
*rest
;
1197 plevel
= acc
->level_before(acc
->source
[j
].data
,
1201 if (!can_precede_at_level(plevel
, level
))
1204 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
1205 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
1208 if (intermediate_sources(acc
, must_rel
, j
, level
) < 0)
1211 T
= last_source(acc
, maydo
, j
, level
, &rest
);
1212 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
1215 if (intermediate_sources(acc
, may_rel
, j
, level
) < 0)
1218 if (isl_set_plain_is_empty(mustdo
) &&
1219 isl_set_plain_is_empty(maydo
))
1222 for (j
= j
- 1; j
>= 0; --j
) {
1225 plevel
= acc
->level_before(acc
->source
[j
].data
,
1229 if (!can_precede_at_level(plevel
, level
))
1232 if (intermediate_sources(acc
, must_rel
, j
, level
) < 0)
1234 if (intermediate_sources(acc
, may_rel
, j
, level
) < 0)
1238 res
= handle_coscheduled(acc
, must_rel
, may_rel
, res
);
1242 for (j
= 0; j
< acc
->n_may
; ++j
) {
1247 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1251 if (!can_precede_at_level(plevel
, level
))
1254 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1255 res
->dep
[2 * acc
->n_must
+ j
].map
=
1256 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1257 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1258 ran
= isl_map_range(isl_map_copy(T
));
1259 res
->dep
[2 * acc
->n_must
+ j
].map
=
1260 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1261 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1262 maydo
= isl_set_union_disjoint(maydo
, ran
);
1264 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1265 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1267 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1270 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1271 res
->dep
[2 * j
].map
=
1272 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1274 res
->dep
[2 * j
+ 1].map
=
1275 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1279 if (isl_set_plain_is_empty(mustdo
) &&
1280 isl_set_plain_is_empty(maydo
))
1287 res
->must_no_source
= mustdo
;
1288 res
->may_no_source
= maydo
;
1292 for (j
= 0; j
< acc
->n_must
; ++j
)
1293 isl_map_free(must_rel
[j
]);
1295 for (j
= 0; j
< acc
->n_must
; ++j
)
1296 isl_map_free(may_rel
[j
]);
1298 isl_set_free(mustdo
);
1299 isl_set_free(maydo
);
1305 /* Given a "sink" access, a list of n "source" accesses,
1306 * compute for each iteration of the sink access
1307 * and for each element accessed by that iteration,
1308 * the source access in the list that last accessed the
1309 * element accessed by the sink access before this sink access.
1310 * Each access is given as a map from the loop iterators
1311 * to the array indices.
1312 * The result is a list of n relations between source and sink
1313 * iterations and a subset of the domain of the sink access,
1314 * corresponding to those iterations that access an element
1315 * not previously accessed.
1317 * To deal with multi-valued sink access relations, the sink iteration
1318 * domain is first extended with dimensions that correspond to the data
1319 * space. However, these extra dimensions are not projected out again.
1320 * It is up to the caller to decide whether these dimensions should be kept.
1322 static __isl_give isl_flow
*access_info_compute_flow_core(
1323 __isl_take isl_access_info
*acc
)
1325 struct isl_flow
*res
= NULL
;
1330 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1334 if (acc
->n_must
== 0)
1335 res
= compute_mem_based_dependences(acc
);
1337 acc
= isl_access_info_sort_sources(acc
);
1338 res
= compute_val_based_dependences(acc
);
1340 acc
= isl_access_info_free(acc
);
1343 if (!res
->must_no_source
|| !res
->may_no_source
)
1347 isl_access_info_free(acc
);
1352 /* Given a "sink" access, a list of n "source" accesses,
1353 * compute for each iteration of the sink access
1354 * and for each element accessed by that iteration,
1355 * the source access in the list that last accessed the
1356 * element accessed by the sink access before this sink access.
1357 * Each access is given as a map from the loop iterators
1358 * to the array indices.
1359 * The result is a list of n relations between source and sink
1360 * iterations and a subset of the domain of the sink access,
1361 * corresponding to those iterations that access an element
1362 * not previously accessed.
1364 * To deal with multi-valued sink access relations,
1365 * access_info_compute_flow_core extends the sink iteration domain
1366 * with dimensions that correspond to the data space. These extra dimensions
1367 * are projected out from the result of access_info_compute_flow_core.
1369 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1372 struct isl_flow
*res
;
1377 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1378 res
= access_info_compute_flow_core(acc
);
1382 for (j
= 0; j
< res
->n_source
; ++j
) {
1383 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1384 if (!res
->dep
[j
].map
)
1395 /* Keep track of some information about a schedule for a given
1396 * access. In particular, keep track of which dimensions
1397 * have a constant value and of the actual constant values.
1399 struct isl_sched_info
{
1404 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1408 isl_vec_free(info
->cst
);
1413 /* Extract information on the constant dimensions of the schedule
1414 * for a given access. The "map" is of the form
1418 * with S the schedule domain, D the iteration domain and A the data domain.
1420 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1421 __isl_keep isl_map
*map
)
1425 struct isl_sched_info
*info
;
1432 space
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1435 n
= isl_space_dim(space
, isl_dim_in
);
1436 isl_space_free(space
);
1440 ctx
= isl_map_get_ctx(map
);
1441 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1444 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1445 info
->cst
= isl_vec_alloc(ctx
, n
);
1446 if (n
&& (!info
->is_cst
|| !info
->cst
))
1449 for (i
= 0; i
< n
; ++i
) {
1452 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1455 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1456 if (info
->is_cst
[i
])
1457 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1464 sched_info_free(info
);
1468 /* The different types of access relations that isl_union_access_info
1471 * "isl_access_sink" represents the sink accesses.
1472 * "isl_access_must_source" represents the definite source accesses.
1473 * "isl_access_may_source" represents the possible source accesses.
1474 * "isl_access_kill" represents the kills.
1476 * isl_access_sink is sometimes treated differently and
1477 * should therefore appear first.
1479 enum isl_access_type
{
1481 isl_access_must_source
,
1482 isl_access_may_source
,
1487 /* This structure represents the input for a dependence analysis computation.
1489 * "access" contains the access relations.
1491 * "schedule" or "schedule_map" represents the execution order.
1492 * Exactly one of these fields should be NULL. The other field
1493 * determines the execution order.
1495 * The domains of these four maps refer to the same iteration spaces(s).
1496 * The ranges of the first three maps also refer to the same data space(s).
1498 * After a call to isl_union_access_info_introduce_schedule,
1499 * the "schedule_map" field no longer contains useful information.
1501 struct isl_union_access_info
{
1502 isl_union_map
*access
[isl_access_end
];
1504 isl_schedule
*schedule
;
1505 isl_union_map
*schedule_map
;
1508 /* Free "access" and return NULL.
1510 __isl_null isl_union_access_info
*isl_union_access_info_free(
1511 __isl_take isl_union_access_info
*access
)
1513 enum isl_access_type i
;
1518 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1519 isl_union_map_free(access
->access
[i
]);
1520 isl_schedule_free(access
->schedule
);
1521 isl_union_map_free(access
->schedule_map
);
1527 /* Return the isl_ctx to which "access" belongs.
1529 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1533 return isl_union_map_get_ctx(access
->access
[isl_access_sink
]);
1536 /* Construct an empty (invalid) isl_union_access_info object.
1537 * The caller is responsible for setting the sink access relation and
1538 * initializing all the other fields, e.g., by calling
1539 * isl_union_access_info_init.
1541 static __isl_give isl_union_access_info
*isl_union_access_info_alloc(
1544 return isl_calloc_type(ctx
, isl_union_access_info
);
1547 /* Initialize all the fields of "info", except the sink access relation,
1548 * which is assumed to have been set by the caller.
1550 * By default, we use the schedule field of the isl_union_access_info,
1551 * but this may be overridden by a call
1552 * to isl_union_access_info_set_schedule_map.
1554 static __isl_give isl_union_access_info
*isl_union_access_info_init(
1555 __isl_take isl_union_access_info
*info
)
1558 isl_union_map
*empty
;
1559 enum isl_access_type i
;
1563 if (!info
->access
[isl_access_sink
])
1564 return isl_union_access_info_free(info
);
1566 space
= isl_union_map_get_space(info
->access
[isl_access_sink
]);
1567 empty
= isl_union_map_empty(isl_space_copy(space
));
1568 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1569 if (!info
->access
[i
])
1570 info
->access
[i
] = isl_union_map_copy(empty
);
1571 isl_union_map_free(empty
);
1572 if (!info
->schedule
&& !info
->schedule_map
)
1573 info
->schedule
= isl_schedule_empty(isl_space_copy(space
));
1574 isl_space_free(space
);
1576 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1577 if (!info
->access
[i
])
1578 return isl_union_access_info_free(info
);
1579 if (!info
->schedule
&& !info
->schedule_map
)
1580 return isl_union_access_info_free(info
);
1585 /* Create a new isl_union_access_info with the given sink accesses and
1586 * and no other accesses or schedule information.
1588 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1589 __isl_take isl_union_map
*sink
)
1592 isl_union_access_info
*access
;
1596 ctx
= isl_union_map_get_ctx(sink
);
1597 access
= isl_union_access_info_alloc(ctx
);
1600 access
->access
[isl_access_sink
] = sink
;
1601 return isl_union_access_info_init(access
);
1603 isl_union_map_free(sink
);
1607 /* Replace the access relation of type "type" of "info" by "access".
1609 static __isl_give isl_union_access_info
*isl_union_access_info_set(
1610 __isl_take isl_union_access_info
*info
,
1611 enum isl_access_type type
, __isl_take isl_union_map
*access
)
1613 if (!info
|| !access
)
1616 isl_union_map_free(info
->access
[type
]);
1617 info
->access
[type
] = access
;
1621 isl_union_access_info_free(info
);
1622 isl_union_map_free(access
);
1626 /* Replace the definite source accesses of "access" by "must_source".
1628 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1629 __isl_take isl_union_access_info
*access
,
1630 __isl_take isl_union_map
*must_source
)
1632 return isl_union_access_info_set(access
, isl_access_must_source
,
1636 /* Replace the possible source accesses of "access" by "may_source".
1638 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1639 __isl_take isl_union_access_info
*access
,
1640 __isl_take isl_union_map
*may_source
)
1642 return isl_union_access_info_set(access
, isl_access_may_source
,
1646 /* Replace the kills of "info" by "kill".
1648 __isl_give isl_union_access_info
*isl_union_access_info_set_kill(
1649 __isl_take isl_union_access_info
*info
, __isl_take isl_union_map
*kill
)
1651 return isl_union_access_info_set(info
, isl_access_kill
, kill
);
1654 /* Return the access relation of type "type" of "info".
1656 static __isl_give isl_union_map
*isl_union_access_info_get(
1657 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1661 return isl_union_map_copy(info
->access
[type
]);
1664 /* Return the definite source accesses of "info".
1666 __isl_give isl_union_map
*isl_union_access_info_get_must_source(
1667 __isl_keep isl_union_access_info
*info
)
1669 return isl_union_access_info_get(info
, isl_access_must_source
);
1672 /* Return the possible source accesses of "info".
1674 __isl_give isl_union_map
*isl_union_access_info_get_may_source(
1675 __isl_keep isl_union_access_info
*info
)
1677 return isl_union_access_info_get(info
, isl_access_may_source
);
1680 /* Return the kills of "info".
1682 __isl_give isl_union_map
*isl_union_access_info_get_kill(
1683 __isl_keep isl_union_access_info
*info
)
1685 return isl_union_access_info_get(info
, isl_access_kill
);
1688 /* Does "info" specify any kills?
1690 static isl_bool
isl_union_access_has_kill(
1691 __isl_keep isl_union_access_info
*info
)
1696 return isl_bool_error
;
1697 empty
= isl_union_map_is_empty(info
->access
[isl_access_kill
]);
1698 return isl_bool_not(empty
);
1701 /* Replace the schedule of "access" by "schedule".
1702 * Also free the schedule_map in case it was set last.
1704 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1705 __isl_take isl_union_access_info
*access
,
1706 __isl_take isl_schedule
*schedule
)
1708 if (!access
|| !schedule
)
1711 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1712 isl_schedule_free(access
->schedule
);
1713 access
->schedule
= schedule
;
1717 isl_union_access_info_free(access
);
1718 isl_schedule_free(schedule
);
1722 /* Replace the schedule map of "access" by "schedule_map".
1723 * Also free the schedule in case it was set last.
1725 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1726 __isl_take isl_union_access_info
*access
,
1727 __isl_take isl_union_map
*schedule_map
)
1729 if (!access
|| !schedule_map
)
1732 isl_union_map_free(access
->schedule_map
);
1733 access
->schedule
= isl_schedule_free(access
->schedule
);
1734 access
->schedule_map
= schedule_map
;
1738 isl_union_access_info_free(access
);
1739 isl_union_map_free(schedule_map
);
1743 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1744 __isl_keep isl_union_access_info
*access
)
1746 isl_union_access_info
*copy
;
1747 enum isl_access_type i
;
1751 copy
= isl_union_access_info_from_sink(
1752 isl_union_map_copy(access
->access
[isl_access_sink
]));
1753 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1754 copy
= isl_union_access_info_set(copy
, i
,
1755 isl_union_map_copy(access
->access
[i
]));
1756 if (access
->schedule
)
1757 copy
= isl_union_access_info_set_schedule(copy
,
1758 isl_schedule_copy(access
->schedule
));
1760 copy
= isl_union_access_info_set_schedule_map(copy
,
1761 isl_union_map_copy(access
->schedule_map
));
1767 #define BASE union_map
1768 #include "print_yaml_field_templ.c"
1770 /* An enumeration of the various keys that may appear in a YAML mapping
1771 * of an isl_union_access_info object.
1772 * The keys for the access relation types are assumed to have the same values
1773 * as the access relation types in isl_access_type.
1776 isl_ai_key_error
= -1,
1777 isl_ai_key_sink
= isl_access_sink
,
1778 isl_ai_key_must_source
= isl_access_must_source
,
1779 isl_ai_key_may_source
= isl_access_may_source
,
1780 isl_ai_key_kill
= isl_access_kill
,
1781 isl_ai_key_schedule_map
,
1782 isl_ai_key_schedule
,
1786 /* Textual representations of the YAML keys for an isl_union_access_info
1789 static char *key_str
[] = {
1790 [isl_ai_key_sink
] = "sink",
1791 [isl_ai_key_must_source
] = "must_source",
1792 [isl_ai_key_may_source
] = "may_source",
1793 [isl_ai_key_kill
] = "kill",
1794 [isl_ai_key_schedule_map
] = "schedule_map",
1795 [isl_ai_key_schedule
] = "schedule",
1798 /* Print a key-value pair corresponding to the access relation of type "type"
1799 * of a YAML mapping of "info" to "p".
1801 * The sink access relation is always printed, but any other access relation
1802 * is only printed if it is non-empty.
1804 static __isl_give isl_printer
*print_access_field(__isl_take isl_printer
*p
,
1805 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1807 if (type
!= isl_access_sink
) {
1810 empty
= isl_union_map_is_empty(info
->access
[type
]);
1812 return isl_printer_free(p
);
1816 return print_yaml_field_union_map(p
, key_str
[type
], info
->access
[type
]);
1819 /* Print the information contained in "access" to "p".
1820 * The information is printed as a YAML document.
1822 __isl_give isl_printer
*isl_printer_print_union_access_info(
1823 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1825 enum isl_access_type i
;
1828 return isl_printer_free(p
);
1830 p
= isl_printer_yaml_start_mapping(p
);
1831 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1832 p
= print_access_field(p
, access
, i
);
1833 if (access
->schedule
) {
1834 p
= isl_printer_print_str(p
, key_str
[isl_ai_key_schedule
]);
1835 p
= isl_printer_yaml_next(p
);
1836 p
= isl_printer_print_schedule(p
, access
->schedule
);
1837 p
= isl_printer_yaml_next(p
);
1839 p
= print_yaml_field_union_map(p
,
1840 key_str
[isl_ai_key_schedule_map
], access
->schedule_map
);
1842 p
= isl_printer_yaml_end_mapping(p
);
1847 /* Return a string representation of the information in "access".
1848 * The information is printed in flow format.
1850 __isl_give
char *isl_union_access_info_to_str(
1851 __isl_keep isl_union_access_info
*access
)
1859 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1860 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1861 p
= isl_printer_print_union_access_info(p
, access
);
1862 s
= isl_printer_get_str(p
);
1863 isl_printer_free(p
);
1869 #define KEY enum isl_ai_key
1871 #define KEY_ERROR isl_ai_key_error
1873 #define KEY_END isl_ai_key_end
1874 #include "extract_key.c"
1877 #define BASE union_map
1878 #include "read_in_string_templ.c"
1880 /* Read an isl_union_access_info object from "s".
1882 * Start off with an empty (invalid) isl_union_access_info object and
1883 * then fill up the fields based on the input.
1884 * The input needs to contain at least a description of the sink
1885 * access relation as well as some form of schedule.
1886 * The other access relations are set to empty relations
1887 * by isl_union_access_info_init if they are not specified in the input.
1889 __isl_give isl_union_access_info
*isl_stream_read_union_access_info(
1893 isl_union_access_info
*info
;
1896 int schedule_set
= 0;
1898 if (isl_stream_yaml_read_start_mapping(s
))
1901 ctx
= isl_stream_get_ctx(s
);
1902 info
= isl_union_access_info_alloc(ctx
);
1903 while ((more
= isl_stream_yaml_next(s
)) > 0) {
1904 enum isl_ai_key key
;
1905 isl_union_map
*access
, *schedule_map
;
1906 isl_schedule
*schedule
;
1909 if (isl_stream_yaml_next(s
) < 0)
1910 return isl_union_access_info_free(info
);
1912 case isl_ai_key_end
:
1913 case isl_ai_key_error
:
1914 return isl_union_access_info_free(info
);
1915 case isl_ai_key_sink
:
1917 case isl_ai_key_must_source
:
1918 case isl_ai_key_may_source
:
1919 case isl_ai_key_kill
:
1920 access
= read_union_map(s
);
1921 info
= isl_union_access_info_set(info
, key
, access
);
1925 case isl_ai_key_schedule_map
:
1927 schedule_map
= read_union_map(s
);
1928 info
= isl_union_access_info_set_schedule_map(info
,
1933 case isl_ai_key_schedule
:
1935 schedule
= isl_stream_read_schedule(s
);
1936 info
= isl_union_access_info_set_schedule(info
,
1944 return isl_union_access_info_free(info
);
1946 if (isl_stream_yaml_read_end_mapping(s
) < 0) {
1947 isl_stream_error(s
, NULL
, "unexpected extra elements");
1948 return isl_union_access_info_free(info
);
1952 isl_stream_error(s
, NULL
, "no sink specified");
1953 return isl_union_access_info_free(info
);
1956 if (!schedule_set
) {
1957 isl_stream_error(s
, NULL
, "no schedule specified");
1958 return isl_union_access_info_free(info
);
1961 return isl_union_access_info_init(info
);
1964 /* Read an isl_union_access_info object from the file "input".
1966 __isl_give isl_union_access_info
*isl_union_access_info_read_from_file(
1967 isl_ctx
*ctx
, FILE *input
)
1970 isl_union_access_info
*access
;
1972 s
= isl_stream_new_file(ctx
, input
);
1975 access
= isl_stream_read_union_access_info(s
);
1981 /* Update the fields of "access" such that they all have the same parameters,
1982 * keeping in mind that the schedule_map field may be NULL and ignoring
1983 * the schedule field.
1985 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1986 __isl_take isl_union_access_info
*access
)
1989 enum isl_access_type i
;
1994 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
1995 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1996 space
= isl_space_align_params(space
,
1997 isl_union_map_get_space(access
->access
[i
]));
1998 if (access
->schedule_map
)
1999 space
= isl_space_align_params(space
,
2000 isl_union_map_get_space(access
->schedule_map
));
2001 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2003 isl_union_map_align_params(access
->access
[i
],
2004 isl_space_copy(space
));
2005 if (!access
->schedule_map
) {
2006 isl_space_free(space
);
2008 access
->schedule_map
=
2009 isl_union_map_align_params(access
->schedule_map
, space
);
2010 if (!access
->schedule_map
)
2011 return isl_union_access_info_free(access
);
2014 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2015 if (!access
->access
[i
])
2016 return isl_union_access_info_free(access
);
2021 /* Prepend the schedule dimensions to the iteration domains.
2023 * That is, if the schedule is of the form
2027 * while the access relations are of the form
2031 * then the updated access relations are of the form
2035 * The schedule map is also replaced by the map
2039 * that is used during the internal computation.
2040 * Neither the original schedule map nor this updated schedule map
2041 * are used after the call to this function.
2043 static __isl_give isl_union_access_info
*
2044 isl_union_access_info_introduce_schedule(
2045 __isl_take isl_union_access_info
*access
)
2048 enum isl_access_type i
;
2053 sm
= isl_union_map_reverse(access
->schedule_map
);
2054 sm
= isl_union_map_range_map(sm
);
2055 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2057 isl_union_map_apply_range(isl_union_map_copy(sm
),
2059 access
->schedule_map
= sm
;
2061 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2062 if (!access
->access
[i
])
2063 return isl_union_access_info_free(access
);
2064 if (!access
->schedule_map
)
2065 return isl_union_access_info_free(access
);
2070 /* This structure represents the result of a dependence analysis computation.
2072 * "must_dep" represents the full definite dependences
2073 * "may_dep" represents the full non-definite dependences.
2074 * Both are of the form
2076 * [Source] -> [[Sink -> Data]]
2078 * (after the schedule dimensions have been projected out).
2079 * "must_no_source" represents the subset of the sink accesses for which
2080 * definitely no source was found.
2081 * "may_no_source" represents the subset of the sink accesses for which
2082 * possibly, but not definitely, no source was found.
2084 struct isl_union_flow
{
2085 isl_union_map
*must_dep
;
2086 isl_union_map
*may_dep
;
2087 isl_union_map
*must_no_source
;
2088 isl_union_map
*may_no_source
;
2091 /* Return the isl_ctx to which "flow" belongs.
2093 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
2095 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
2098 /* Free "flow" and return NULL.
2100 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
2104 isl_union_map_free(flow
->must_dep
);
2105 isl_union_map_free(flow
->may_dep
);
2106 isl_union_map_free(flow
->must_no_source
);
2107 isl_union_map_free(flow
->may_no_source
);
2112 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
2117 fprintf(stderr
, "must dependences: ");
2118 isl_union_map_dump(flow
->must_dep
);
2119 fprintf(stderr
, "may dependences: ");
2120 isl_union_map_dump(flow
->may_dep
);
2121 fprintf(stderr
, "must no source: ");
2122 isl_union_map_dump(flow
->must_no_source
);
2123 fprintf(stderr
, "may no source: ");
2124 isl_union_map_dump(flow
->may_no_source
);
2127 /* Return the full definite dependences in "flow", with accessed elements.
2129 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
2130 __isl_keep isl_union_flow
*flow
)
2134 return isl_union_map_copy(flow
->must_dep
);
2137 /* Return the full possible dependences in "flow", including the definite
2138 * dependences, with accessed elements.
2140 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
2141 __isl_keep isl_union_flow
*flow
)
2145 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2146 isl_union_map_copy(flow
->may_dep
));
2149 /* Return the definite dependences in "flow", without the accessed elements.
2151 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
2152 __isl_keep isl_union_flow
*flow
)
2158 dep
= isl_union_map_copy(flow
->must_dep
);
2159 return isl_union_map_range_factor_domain(dep
);
2162 /* Return the possible dependences in "flow", including the definite
2163 * dependences, without the accessed elements.
2165 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
2166 __isl_keep isl_union_flow
*flow
)
2172 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2173 isl_union_map_copy(flow
->may_dep
));
2174 return isl_union_map_range_factor_domain(dep
);
2177 /* Return the non-definite dependences in "flow".
2179 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
2180 __isl_keep isl_union_flow
*flow
)
2184 return isl_union_map_copy(flow
->may_dep
);
2187 /* Return the subset of the sink accesses for which definitely
2188 * no source was found.
2190 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
2191 __isl_keep isl_union_flow
*flow
)
2195 return isl_union_map_copy(flow
->must_no_source
);
2198 /* Return the subset of the sink accesses for which possibly
2199 * no source was found, including those for which definitely
2200 * no source was found.
2202 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
2203 __isl_keep isl_union_flow
*flow
)
2207 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
2208 isl_union_map_copy(flow
->may_no_source
));
2211 /* Return the subset of the sink accesses for which possibly, but not
2212 * definitely, no source was found.
2214 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
2215 __isl_keep isl_union_flow
*flow
)
2219 return isl_union_map_copy(flow
->may_no_source
);
2222 /* Create a new isl_union_flow object, initialized with empty
2223 * dependence relations and sink subsets.
2225 static __isl_give isl_union_flow
*isl_union_flow_alloc(
2226 __isl_take isl_space
*space
)
2229 isl_union_map
*empty
;
2230 isl_union_flow
*flow
;
2234 ctx
= isl_space_get_ctx(space
);
2235 flow
= isl_alloc_type(ctx
, isl_union_flow
);
2239 empty
= isl_union_map_empty(space
);
2240 flow
->must_dep
= isl_union_map_copy(empty
);
2241 flow
->may_dep
= isl_union_map_copy(empty
);
2242 flow
->must_no_source
= isl_union_map_copy(empty
);
2243 flow
->may_no_source
= empty
;
2245 if (!flow
->must_dep
|| !flow
->may_dep
||
2246 !flow
->must_no_source
|| !flow
->may_no_source
)
2247 return isl_union_flow_free(flow
);
2251 isl_space_free(space
);
2255 /* Copy this isl_union_flow object.
2257 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
2259 isl_union_flow
*copy
;
2264 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
2269 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
2270 isl_union_map_copy(flow
->must_dep
));
2271 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
2272 isl_union_map_copy(flow
->may_dep
));
2273 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
2274 isl_union_map_copy(flow
->must_no_source
));
2275 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
2276 isl_union_map_copy(flow
->may_no_source
));
2278 if (!copy
->must_dep
|| !copy
->may_dep
||
2279 !copy
->must_no_source
|| !copy
->may_no_source
)
2280 return isl_union_flow_free(copy
);
2285 /* Drop the schedule dimensions from the iteration domains in "flow".
2286 * In particular, the schedule dimensions have been prepended
2287 * to the iteration domains prior to the dependence analysis by
2288 * replacing the iteration domain D, by the wrapped map [S -> D].
2289 * Replace these wrapped maps by the original D.
2291 * In particular, the dependences computed by access_info_compute_flow_core
2294 * [S -> D] -> [[S' -> D'] -> A]
2296 * The schedule dimensions are projected out by first currying the range,
2299 * [S -> D] -> [S' -> [D' -> A]]
2301 * and then computing the factor range
2305 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
2306 __isl_take isl_union_flow
*flow
)
2311 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
2312 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
2313 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
2314 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
2315 flow
->must_no_source
=
2316 isl_union_map_domain_factor_range(flow
->must_no_source
);
2317 flow
->may_no_source
=
2318 isl_union_map_domain_factor_range(flow
->may_no_source
);
2320 if (!flow
->must_dep
|| !flow
->may_dep
||
2321 !flow
->must_no_source
|| !flow
->may_no_source
)
2322 return isl_union_flow_free(flow
);
2327 struct isl_compute_flow_data
{
2328 isl_union_map
*must_source
;
2329 isl_union_map
*may_source
;
2330 isl_union_flow
*flow
;
2335 struct isl_sched_info
*sink_info
;
2336 struct isl_sched_info
**source_info
;
2337 isl_access_info
*accesses
;
2340 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
2344 struct isl_compute_flow_data
*data
;
2346 data
= (struct isl_compute_flow_data
*)user
;
2348 dim
= isl_space_range(isl_map_get_space(map
));
2350 eq
= isl_space_is_equal(dim
, data
->dim
);
2352 isl_space_free(dim
);
2356 return isl_stat_error
;
2363 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
2367 struct isl_sched_info
*info
;
2368 struct isl_compute_flow_data
*data
;
2370 data
= (struct isl_compute_flow_data
*)user
;
2372 dim
= isl_space_range(isl_map_get_space(map
));
2374 eq
= isl_space_is_equal(dim
, data
->dim
);
2376 isl_space_free(dim
);
2385 info
= sched_info_alloc(map
);
2386 data
->source_info
[data
->count
] = info
;
2388 data
->accesses
= isl_access_info_add_source(data
->accesses
,
2389 map
, data
->must
, info
);
2396 return isl_stat_error
;
2399 /* Determine the shared nesting level and the "textual order" of
2400 * the given accesses.
2402 * We first determine the minimal schedule dimension for both accesses.
2404 * If among those dimensions, we can find one where both have a fixed
2405 * value and if moreover those values are different, then the previous
2406 * dimension is the last shared nesting level and the textual order
2407 * is determined based on the order of the fixed values.
2408 * If no such fixed values can be found, then we set the shared
2409 * nesting level to the minimal schedule dimension, with no textual ordering.
2411 static int before(void *first
, void *second
)
2413 struct isl_sched_info
*info1
= first
;
2414 struct isl_sched_info
*info2
= second
;
2418 n1
= isl_vec_size(info1
->cst
);
2419 n2
= isl_vec_size(info2
->cst
);
2420 if (n1
< 0 || n2
< 0)
2426 for (i
= 0; i
< n1
; ++i
) {
2430 if (!info1
->is_cst
[i
])
2432 if (!info2
->is_cst
[i
])
2434 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2438 r
= 2 * i
+ (cmp
< 0);
2446 /* Check if the given two accesses may be coscheduled.
2447 * If so, return isl_bool_true. Otherwise return isl_bool_false.
2449 * Two accesses may only be coscheduled if the fixed schedule
2450 * coordinates have the same values.
2452 static isl_bool
coscheduled(void *first
, void *second
)
2454 struct isl_sched_info
*info1
= first
;
2455 struct isl_sched_info
*info2
= second
;
2459 n1
= isl_vec_size(info1
->cst
);
2460 n2
= isl_vec_size(info2
->cst
);
2461 if (n1
< 0 || n2
< 0)
2462 return isl_bool_error
;
2467 for (i
= 0; i
< n1
; ++i
) {
2470 if (!info1
->is_cst
[i
])
2472 if (!info2
->is_cst
[i
])
2474 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2476 return isl_bool_false
;
2479 return isl_bool_true
;
2482 /* Given a sink access, look for all the source accesses that access
2483 * the same array and perform dataflow analysis on them using
2484 * isl_access_info_compute_flow_core.
2486 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
2490 struct isl_compute_flow_data
*data
;
2494 data
= (struct isl_compute_flow_data
*)user
;
2497 ctx
= isl_map_get_ctx(map
);
2499 data
->accesses
= NULL
;
2500 data
->sink_info
= NULL
;
2501 data
->source_info
= NULL
;
2503 data
->dim
= isl_space_range(isl_map_get_space(map
));
2505 if (isl_union_map_foreach_map(data
->must_source
,
2506 &count_matching_array
, data
) < 0)
2508 if (isl_union_map_foreach_map(data
->may_source
,
2509 &count_matching_array
, data
) < 0)
2512 data
->sink_info
= sched_info_alloc(map
);
2513 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
2516 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
2517 data
->sink_info
, &before
, data
->count
);
2518 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
2521 data
->accesses
->coscheduled
= &coscheduled
;
2524 if (isl_union_map_foreach_map(data
->must_source
,
2525 &collect_matching_array
, data
) < 0)
2528 if (isl_union_map_foreach_map(data
->may_source
,
2529 &collect_matching_array
, data
) < 0)
2532 flow
= access_info_compute_flow_core(data
->accesses
);
2533 data
->accesses
= NULL
;
2538 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
2539 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
2540 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2541 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2543 for (i
= 0; i
< flow
->n_source
; ++i
) {
2545 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2546 if (flow
->dep
[i
].must
)
2547 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2549 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2552 isl_flow_free(flow
);
2554 sched_info_free(data
->sink_info
);
2555 if (data
->source_info
) {
2556 for (i
= 0; i
< data
->count
; ++i
)
2557 sched_info_free(data
->source_info
[i
]);
2558 free(data
->source_info
);
2560 isl_space_free(data
->dim
);
2565 isl_access_info_free(data
->accesses
);
2566 sched_info_free(data
->sink_info
);
2567 if (data
->source_info
) {
2568 for (i
= 0; i
< data
->count
; ++i
)
2569 sched_info_free(data
->source_info
[i
]);
2570 free(data
->source_info
);
2572 isl_space_free(data
->dim
);
2575 return isl_stat_error
;
2578 /* Add the kills of "info" to the must-sources.
2580 static __isl_give isl_union_access_info
*
2581 isl_union_access_info_add_kill_to_must_source(
2582 __isl_take isl_union_access_info
*info
)
2584 isl_union_map
*must
, *kill
;
2586 must
= isl_union_access_info_get_must_source(info
);
2587 kill
= isl_union_access_info_get_kill(info
);
2588 must
= isl_union_map_union(must
, kill
);
2589 return isl_union_access_info_set_must_source(info
, must
);
2592 /* Drop dependences from "flow" that purely originate from kills.
2593 * That is, only keep those dependences that originate from
2594 * the original must-sources "must" and/or the original may-sources "may".
2595 * In particular, "must" contains the must-sources from before
2596 * the kills were added and "may" contains the may-source from before
2597 * the kills were removed.
2599 * The dependences are of the form
2601 * Source -> [Sink -> Data]
2603 * Only those dependences are kept where the Source -> Data part
2604 * is a subset of the original may-sources or must-sources.
2605 * Of those, only the must-dependences that intersect with the must-sources
2606 * remain must-dependences.
2607 * If there is some overlap between the may-sources and the must-sources,
2608 * then the may-dependences and must-dependences may also overlap.
2609 * This should be fine since the may-dependences are only kept
2610 * disjoint from the must-dependences for the isl_union_map_compute_flow
2611 * interface. This interface does not support kills, so it will
2612 * not end up calling this function.
2614 static __isl_give isl_union_flow
*isl_union_flow_drop_kill_source(
2615 __isl_take isl_union_flow
*flow
, __isl_take isl_union_map
*must
,
2616 __isl_take isl_union_map
*may
)
2618 isl_union_map
*move
;
2622 move
= isl_union_map_copy(flow
->must_dep
);
2623 move
= isl_union_map_intersect_range_factor_range(move
,
2624 isl_union_map_copy(may
));
2625 may
= isl_union_map_union(may
, isl_union_map_copy(must
));
2626 flow
->may_dep
= isl_union_map_intersect_range_factor_range(
2627 flow
->may_dep
, may
);
2628 flow
->must_dep
= isl_union_map_intersect_range_factor_range(
2629 flow
->must_dep
, must
);
2630 flow
->may_dep
= isl_union_map_union(flow
->may_dep
, move
);
2631 if (!flow
->must_dep
|| !flow
->may_dep
)
2632 return isl_union_flow_free(flow
);
2636 isl_union_map_free(must
);
2637 isl_union_map_free(may
);
2641 /* Remove the must accesses from the may accesses.
2643 * A must access always trumps a may access, so there is no need
2644 * for a must access to also be considered as a may access. Doing so
2645 * would only cost extra computations only to find out that
2646 * the duplicated may access does not make any difference.
2648 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2649 __isl_take isl_union_access_info
*access
)
2653 access
->access
[isl_access_may_source
] =
2654 isl_union_map_subtract(access
->access
[isl_access_may_source
],
2655 isl_union_map_copy(access
->access
[isl_access_must_source
]));
2656 if (!access
->access
[isl_access_may_source
])
2657 return isl_union_access_info_free(access
);
2662 /* Given a description of the "sink" accesses, the "source" accesses and
2663 * a schedule, compute for each instance of a sink access
2664 * and for each element accessed by that instance,
2665 * the possible or definite source accesses that last accessed the
2666 * element accessed by the sink access before this sink access
2667 * in the sense that there is no intermediate definite source access.
2669 * The must_no_source and may_no_source elements of the result
2670 * are subsets of access->sink. The elements must_dep and may_dep
2671 * map domain elements of access->{may,must)_source to
2672 * domain elements of access->sink.
2674 * This function is used when only the schedule map representation
2677 * We first prepend the schedule dimensions to the domain
2678 * of the accesses so that we can easily compare their relative order.
2679 * Then we consider each sink access individually in compute_flow.
2681 static __isl_give isl_union_flow
*compute_flow_union_map(
2682 __isl_take isl_union_access_info
*access
)
2684 struct isl_compute_flow_data data
;
2685 isl_union_map
*sink
;
2687 access
= isl_union_access_info_align_params(access
);
2688 access
= isl_union_access_info_introduce_schedule(access
);
2692 data
.must_source
= access
->access
[isl_access_must_source
];
2693 data
.may_source
= access
->access
[isl_access_may_source
];
2695 sink
= access
->access
[isl_access_sink
];
2696 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(sink
));
2698 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
2701 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2703 isl_union_access_info_free(access
);
2706 isl_union_access_info_free(access
);
2707 isl_union_flow_free(data
.flow
);
2711 /* A schedule access relation.
2713 * The access relation "access" is of the form [S -> D] -> A,
2714 * where S corresponds to the prefix schedule at "node".
2715 * "must" is only relevant for source accesses and indicates
2716 * whether the access is a must source or a may source.
2718 struct isl_scheduled_access
{
2721 isl_schedule_node
*node
;
2724 /* Data structure for keeping track of individual scheduled sink and source
2725 * accesses when computing dependence analysis based on a schedule tree.
2727 * "n_sink" is the number of used entries in "sink"
2728 * "n_source" is the number of used entries in "source"
2730 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2731 * to keep track of the current node and
2732 * of what extract_sink_source needs to do.
2734 struct isl_compute_flow_schedule_data
{
2735 isl_union_access_info
*access
;
2740 struct isl_scheduled_access
*sink
;
2741 struct isl_scheduled_access
*source
;
2745 isl_schedule_node
*node
;
2748 /* Align the parameters of all sinks with all sources.
2750 * If there are no sinks or no sources, then no alignment is needed.
2752 static void isl_compute_flow_schedule_data_align_params(
2753 struct isl_compute_flow_schedule_data
*data
)
2758 if (data
->n_sink
== 0 || data
->n_source
== 0)
2761 space
= isl_map_get_space(data
->sink
[0].access
);
2763 for (i
= 1; i
< data
->n_sink
; ++i
)
2764 space
= isl_space_align_params(space
,
2765 isl_map_get_space(data
->sink
[i
].access
));
2766 for (i
= 0; i
< data
->n_source
; ++i
)
2767 space
= isl_space_align_params(space
,
2768 isl_map_get_space(data
->source
[i
].access
));
2770 for (i
= 0; i
< data
->n_sink
; ++i
)
2771 data
->sink
[i
].access
=
2772 isl_map_align_params(data
->sink
[i
].access
,
2773 isl_space_copy(space
));
2774 for (i
= 0; i
< data
->n_source
; ++i
)
2775 data
->source
[i
].access
=
2776 isl_map_align_params(data
->source
[i
].access
,
2777 isl_space_copy(space
));
2779 isl_space_free(space
);
2782 /* Free all the memory referenced from "data".
2783 * Do not free "data" itself as it may be allocated on the stack.
2785 static void isl_compute_flow_schedule_data_clear(
2786 struct isl_compute_flow_schedule_data
*data
)
2793 for (i
= 0; i
< data
->n_sink
; ++i
) {
2794 isl_map_free(data
->sink
[i
].access
);
2795 isl_schedule_node_free(data
->sink
[i
].node
);
2798 for (i
= 0; i
< data
->n_source
; ++i
) {
2799 isl_map_free(data
->source
[i
].access
);
2800 isl_schedule_node_free(data
->source
[i
].node
);
2806 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2807 * (an upper bound on) the number of sinks and sources.
2809 * Sinks and sources are only extracted at leaves of the tree,
2810 * so we skip the node if it is not a leaf.
2811 * Otherwise we increment data->n_sink and data->n_source with
2812 * the number of spaces in the sink and source access domains
2813 * that reach this node.
2815 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2818 struct isl_compute_flow_schedule_data
*data
= user
;
2819 isl_union_set
*domain
;
2820 isl_union_map
*umap
;
2821 isl_bool r
= isl_bool_false
;
2824 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2825 return isl_bool_true
;
2827 domain
= isl_schedule_node_get_universe_domain(node
);
2829 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2830 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2831 data
->n_sink
+= n
= isl_union_map_n_map(umap
);
2832 isl_union_map_free(umap
);
2836 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2837 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2838 data
->n_source
+= n
= isl_union_map_n_map(umap
);
2839 isl_union_map_free(umap
);
2843 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2844 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2845 data
->n_source
+= n
= isl_union_map_n_map(umap
);
2846 isl_union_map_free(umap
);
2850 isl_union_set_free(domain
);
2855 /* Add a single scheduled sink or source (depending on data->set_sink)
2856 * with scheduled access relation "map", must property data->must and
2857 * schedule node data->node to the list of sinks or sources.
2859 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2861 struct isl_compute_flow_schedule_data
*data
= user
;
2862 struct isl_scheduled_access
*access
;
2865 access
= data
->sink
+ data
->n_sink
++;
2867 access
= data
->source
+ data
->n_source
++;
2869 access
->access
= map
;
2870 access
->must
= data
->must
;
2871 access
->node
= isl_schedule_node_copy(data
->node
);
2876 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2877 * individual scheduled source and sink accesses (taking into account
2878 * the domain of the schedule).
2880 * We only collect accesses at the leaves of the schedule tree.
2881 * We prepend the schedule dimensions at the leaf to the iteration
2882 * domains of the source and sink accesses and then extract
2883 * the individual accesses (per space).
2885 * In particular, if the prefix schedule at the node is of the form
2889 * while the access relations are of the form
2893 * then the updated access relations are of the form
2897 * Note that S consists of a single space such that introducing S
2898 * in the access relations does not increase the number of spaces.
2900 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2903 struct isl_compute_flow_schedule_data
*data
= user
;
2904 isl_union_map
*prefix
;
2905 isl_union_map
*umap
;
2906 isl_bool r
= isl_bool_false
;
2908 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2909 return isl_bool_true
;
2913 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2914 prefix
= isl_union_map_reverse(prefix
);
2915 prefix
= isl_union_map_range_map(prefix
);
2918 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2919 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2920 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2922 isl_union_map_free(umap
);
2926 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2927 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2928 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2930 isl_union_map_free(umap
);
2934 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2935 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2936 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2938 isl_union_map_free(umap
);
2940 isl_union_map_free(prefix
);
2945 /* isl_access_info_compute_flow callback for determining whether
2946 * the shared nesting level and the ordering within that level
2947 * for two scheduled accesses for use in compute_single_flow.
2949 * The tokens passed to this function refer to the leaves
2950 * in the schedule tree where the accesses take place.
2952 * If n is the shared number of loops, then we need to return
2953 * "2 * n + 1" if "first" precedes "second" inside the innermost
2954 * shared loop and "2 * n" otherwise.
2956 * The innermost shared ancestor may be the leaves themselves
2957 * if the accesses take place in the same leaf. Otherwise,
2958 * it is either a set node or a sequence node. Only in the case
2959 * of a sequence node do we consider one access to precede the other.
2961 static int before_node(void *first
, void *second
)
2963 isl_schedule_node
*node1
= first
;
2964 isl_schedule_node
*node2
= second
;
2965 isl_schedule_node
*shared
;
2969 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2970 depth
= isl_schedule_node_get_schedule_depth(shared
);
2972 isl_schedule_node_free(shared
);
2976 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2977 isl_size pos1
, pos2
;
2979 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2981 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2983 if (pos1
< 0 || pos2
< 0) {
2984 isl_schedule_node_free(shared
);
2987 before
= pos1
< pos2
;
2990 isl_schedule_node_free(shared
);
2992 return 2 * depth
+ before
;
2995 /* Check if the given two accesses may be coscheduled.
2996 * If so, return isl_bool_true. Otherwise return isl_bool_false.
2998 * Two accesses may only be coscheduled if they appear in the same leaf.
3000 static isl_bool
coscheduled_node(void *first
, void *second
)
3002 isl_schedule_node
*node1
= first
;
3003 isl_schedule_node
*node2
= second
;
3005 return isl_bool_ok(node1
== node2
);
3008 /* Add the scheduled sources from "data" that access
3009 * the same data space as "sink" to "access".
3011 static __isl_give isl_access_info
*add_matching_sources(
3012 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
3013 struct isl_compute_flow_schedule_data
*data
)
3018 space
= isl_space_range(isl_map_get_space(sink
->access
));
3019 for (i
= 0; i
< data
->n_source
; ++i
) {
3020 struct isl_scheduled_access
*source
;
3021 isl_space
*source_space
;
3024 source
= &data
->source
[i
];
3025 source_space
= isl_map_get_space(source
->access
);
3026 source_space
= isl_space_range(source_space
);
3027 eq
= isl_space_is_equal(space
, source_space
);
3028 isl_space_free(source_space
);
3035 access
= isl_access_info_add_source(access
,
3036 isl_map_copy(source
->access
), source
->must
, source
->node
);
3039 isl_space_free(space
);
3042 isl_space_free(space
);
3043 isl_access_info_free(access
);
3047 /* Given a scheduled sink access relation "sink", compute the corresponding
3048 * dependences on the sources in "data" and add the computed dependences
3051 * The dependences computed by access_info_compute_flow_core are of the form
3053 * [S -> I] -> [[S' -> I'] -> A]
3055 * The schedule dimensions are projected out by first currying the range,
3058 * [S -> I] -> [S' -> [I' -> A]]
3060 * and then computing the factor range
3064 static __isl_give isl_union_flow
*compute_single_flow(
3065 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
3066 struct isl_compute_flow_schedule_data
*data
)
3069 isl_access_info
*access
;
3076 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
3077 &before_node
, data
->n_source
);
3079 access
->coscheduled
= &coscheduled_node
;
3080 access
= add_matching_sources(access
, sink
, data
);
3082 flow
= access_info_compute_flow_core(access
);
3084 return isl_union_flow_free(uf
);
3086 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
3087 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
3088 isl_union_map_from_map(map
));
3089 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
3090 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
3091 isl_union_map_from_map(map
));
3093 for (i
= 0; i
< flow
->n_source
; ++i
) {
3096 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
3097 map
= isl_map_factor_range(map
);
3098 dep
= isl_union_map_from_map(map
);
3099 if (flow
->dep
[i
].must
)
3100 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
3102 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
3105 isl_flow_free(flow
);
3110 /* Given a description of the "sink" accesses, the "source" accesses and
3111 * a schedule, compute for each instance of a sink access
3112 * and for each element accessed by that instance,
3113 * the possible or definite source accesses that last accessed the
3114 * element accessed by the sink access before this sink access
3115 * in the sense that there is no intermediate definite source access.
3116 * Only consider dependences between statement instances that belong
3117 * to the domain of the schedule.
3119 * The must_no_source and may_no_source elements of the result
3120 * are subsets of access->sink. The elements must_dep and may_dep
3121 * map domain elements of access->{may,must)_source to
3122 * domain elements of access->sink.
3124 * This function is used when a schedule tree representation
3127 * We extract the individual scheduled source and sink access relations
3128 * (taking into account the domain of the schedule) and
3129 * then compute dependences for each scheduled sink individually.
3131 static __isl_give isl_union_flow
*compute_flow_schedule(
3132 __isl_take isl_union_access_info
*access
)
3134 struct isl_compute_flow_schedule_data data
= { access
};
3138 isl_union_flow
*flow
;
3140 ctx
= isl_union_access_info_get_ctx(access
);
3144 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3145 &count_sink_source
, &data
) < 0)
3148 n
= data
.n_sink
+ data
.n_source
;
3149 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
3150 if (n
&& !data
.sink
)
3152 data
.source
= data
.sink
+ data
.n_sink
;
3156 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3157 &collect_sink_source
, &data
) < 0)
3160 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
3161 flow
= isl_union_flow_alloc(space
);
3163 isl_compute_flow_schedule_data_align_params(&data
);
3165 for (i
= 0; i
< data
.n_sink
; ++i
)
3166 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
3168 isl_compute_flow_schedule_data_clear(&data
);
3170 isl_union_access_info_free(access
);
3173 isl_union_access_info_free(access
);
3174 isl_compute_flow_schedule_data_clear(&data
);
3178 /* Given a description of the "sink" accesses, the "source" accesses and
3179 * a schedule, compute for each instance of a sink access
3180 * and for each element accessed by that instance,
3181 * the possible or definite source accesses that last accessed the
3182 * element accessed by the sink access before this sink access
3183 * in the sense that there is no intermediate definite source access.
3185 * The must_no_source and may_no_source elements of the result
3186 * are subsets of access->sink. The elements must_dep and may_dep
3187 * map domain elements of access->{may,must)_source to
3188 * domain elements of access->sink.
3190 * If any kills have been specified, then they are treated as
3191 * must-sources internally. Any dependence that purely derives
3192 * from an original kill is removed from the output.
3194 * We check whether the schedule is available as a schedule tree
3195 * or a schedule map and call the corresponding function to perform
3198 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
3199 __isl_take isl_union_access_info
*access
)
3202 isl_union_map
*must
= NULL
, *may
= NULL
;
3203 isl_union_flow
*flow
;
3205 has_kill
= isl_union_access_has_kill(access
);
3209 must
= isl_union_access_info_get_must_source(access
);
3210 may
= isl_union_access_info_get_may_source(access
);
3212 access
= isl_union_access_info_add_kill_to_must_source(access
);
3213 access
= isl_union_access_info_normalize(access
);
3216 if (access
->schedule
)
3217 flow
= compute_flow_schedule(access
);
3219 flow
= compute_flow_union_map(access
);
3221 flow
= isl_union_flow_drop_kill_source(flow
, must
, may
);
3224 isl_union_access_info_free(access
);
3225 isl_union_map_free(must
);
3226 isl_union_map_free(may
);
3230 /* Print the information contained in "flow" to "p".
3231 * The information is printed as a YAML document.
3233 __isl_give isl_printer
*isl_printer_print_union_flow(
3234 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
3236 isl_union_map
*umap
;
3239 return isl_printer_free(p
);
3241 p
= isl_printer_yaml_start_mapping(p
);
3242 umap
= isl_union_flow_get_full_must_dependence(flow
);
3243 p
= print_yaml_field_union_map(p
, "must_dependence", umap
);
3244 isl_union_map_free(umap
);
3245 umap
= isl_union_flow_get_full_may_dependence(flow
);
3246 p
= print_yaml_field_union_map(p
, "may_dependence", umap
);
3247 isl_union_map_free(umap
);
3248 p
= print_yaml_field_union_map(p
, "must_no_source",
3249 flow
->must_no_source
);
3250 umap
= isl_union_flow_get_may_no_source(flow
);
3251 p
= print_yaml_field_union_map(p
, "may_no_source", umap
);
3252 isl_union_map_free(umap
);
3253 p
= isl_printer_yaml_end_mapping(p
);
3258 /* Return a string representation of the information in "flow".
3259 * The information is printed in flow format.
3261 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
3269 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
3270 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
3271 p
= isl_printer_print_union_flow(p
, flow
);
3272 s
= isl_printer_get_str(p
);
3273 isl_printer_free(p
);
3278 /* Given a collection of "sink" and "source" accesses,
3279 * compute for each iteration of a sink access
3280 * and for each element accessed by that iteration,
3281 * the source access in the list that last accessed the
3282 * element accessed by the sink access before this sink access.
3283 * Each access is given as a map from the loop iterators
3284 * to the array indices.
3285 * The result is a relations between source and sink
3286 * iterations and a subset of the domain of the sink accesses,
3287 * corresponding to those iterations that access an element
3288 * not previously accessed.
3290 * We collect the inputs in an isl_union_access_info object,
3291 * call isl_union_access_info_compute_flow and extract
3292 * the outputs from the result.
3294 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
3295 __isl_take isl_union_map
*must_source
,
3296 __isl_take isl_union_map
*may_source
,
3297 __isl_take isl_union_map
*schedule
,
3298 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
3299 __isl_give isl_union_map
**must_no_source
,
3300 __isl_give isl_union_map
**may_no_source
)
3302 isl_union_access_info
*access
;
3303 isl_union_flow
*flow
;
3305 access
= isl_union_access_info_from_sink(sink
);
3306 access
= isl_union_access_info_set_must_source(access
, must_source
);
3307 access
= isl_union_access_info_set_may_source(access
, may_source
);
3308 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
3309 flow
= isl_union_access_info_compute_flow(access
);
3312 *must_dep
= isl_union_flow_get_must_dependence(flow
);
3314 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
3316 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
3318 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
3320 isl_union_flow_free(flow
);
3322 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
3323 (must_no_source
&& !*must_no_source
) ||
3324 (may_no_source
&& !*may_no_source
))
3330 *must_dep
= isl_union_map_free(*must_dep
);
3332 *may_dep
= isl_union_map_free(*may_dep
);
3334 *must_no_source
= isl_union_map_free(*must_no_source
);
3336 *may_no_source
= isl_union_map_free(*may_no_source
);