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
));
1766 /* Print a key-value pair of a YAML mapping to "p",
1767 * with key "name" and value "umap".
1769 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1770 const char *name
, __isl_keep isl_union_map
*umap
)
1772 p
= isl_printer_print_str(p
, name
);
1773 p
= isl_printer_yaml_next(p
);
1774 p
= isl_printer_print_str(p
, "\"");
1775 p
= isl_printer_print_union_map(p
, umap
);
1776 p
= isl_printer_print_str(p
, "\"");
1777 p
= isl_printer_yaml_next(p
);
1782 /* An enumeration of the various keys that may appear in a YAML mapping
1783 * of an isl_union_access_info object.
1784 * The keys for the access relation types are assumed to have the same values
1785 * as the access relation types in isl_access_type.
1788 isl_ai_key_error
= -1,
1789 isl_ai_key_sink
= isl_access_sink
,
1790 isl_ai_key_must_source
= isl_access_must_source
,
1791 isl_ai_key_may_source
= isl_access_may_source
,
1792 isl_ai_key_kill
= isl_access_kill
,
1793 isl_ai_key_schedule_map
,
1794 isl_ai_key_schedule
,
1798 /* Textual representations of the YAML keys for an isl_union_access_info
1801 static char *key_str
[] = {
1802 [isl_ai_key_sink
] = "sink",
1803 [isl_ai_key_must_source
] = "must_source",
1804 [isl_ai_key_may_source
] = "may_source",
1805 [isl_ai_key_kill
] = "kill",
1806 [isl_ai_key_schedule_map
] = "schedule_map",
1807 [isl_ai_key_schedule
] = "schedule",
1810 /* Print a key-value pair corresponding to the access relation of type "type"
1811 * of a YAML mapping of "info" to "p".
1813 * The sink access relation is always printed, but any other access relation
1814 * is only printed if it is non-empty.
1816 static __isl_give isl_printer
*print_access_field(__isl_take isl_printer
*p
,
1817 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1819 if (type
!= isl_access_sink
) {
1822 empty
= isl_union_map_is_empty(info
->access
[type
]);
1824 return isl_printer_free(p
);
1828 return print_union_map_field(p
, key_str
[type
], info
->access
[type
]);
1831 /* Print the information contained in "access" to "p".
1832 * The information is printed as a YAML document.
1834 __isl_give isl_printer
*isl_printer_print_union_access_info(
1835 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1837 enum isl_access_type i
;
1840 return isl_printer_free(p
);
1842 p
= isl_printer_yaml_start_mapping(p
);
1843 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1844 p
= print_access_field(p
, access
, i
);
1845 if (access
->schedule
) {
1846 p
= isl_printer_print_str(p
, key_str
[isl_ai_key_schedule
]);
1847 p
= isl_printer_yaml_next(p
);
1848 p
= isl_printer_print_schedule(p
, access
->schedule
);
1849 p
= isl_printer_yaml_next(p
);
1851 p
= print_union_map_field(p
, key_str
[isl_ai_key_schedule_map
],
1852 access
->schedule_map
);
1854 p
= isl_printer_yaml_end_mapping(p
);
1859 /* Return a string representation of the information in "access".
1860 * The information is printed in flow format.
1862 __isl_give
char *isl_union_access_info_to_str(
1863 __isl_keep isl_union_access_info
*access
)
1871 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1872 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1873 p
= isl_printer_print_union_access_info(p
, access
);
1874 s
= isl_printer_get_str(p
);
1875 isl_printer_free(p
);
1881 #define KEY enum isl_ai_key
1883 #define KEY_ERROR isl_ai_key_error
1885 #define KEY_END isl_ai_key_end
1886 #include "extract_key.c"
1889 #define BASE union_map
1890 #include "read_in_string_templ.c"
1892 /* Read an isl_union_access_info object from "s".
1894 * Start off with an empty (invalid) isl_union_access_info object and
1895 * then fill up the fields based on the input.
1896 * The input needs to contain at least a description of the sink
1897 * access relation as well as some form of schedule.
1898 * The other access relations are set to empty relations
1899 * by isl_union_access_info_init if they are not specified in the input.
1901 __isl_give isl_union_access_info
*isl_stream_read_union_access_info(
1905 isl_union_access_info
*info
;
1908 int schedule_set
= 0;
1910 if (isl_stream_yaml_read_start_mapping(s
))
1913 ctx
= isl_stream_get_ctx(s
);
1914 info
= isl_union_access_info_alloc(ctx
);
1915 while ((more
= isl_stream_yaml_next(s
)) > 0) {
1916 enum isl_ai_key key
;
1917 isl_union_map
*access
, *schedule_map
;
1918 isl_schedule
*schedule
;
1921 if (isl_stream_yaml_next(s
) < 0)
1922 return isl_union_access_info_free(info
);
1924 case isl_ai_key_end
:
1925 case isl_ai_key_error
:
1926 return isl_union_access_info_free(info
);
1927 case isl_ai_key_sink
:
1929 case isl_ai_key_must_source
:
1930 case isl_ai_key_may_source
:
1931 case isl_ai_key_kill
:
1932 access
= read_union_map(s
);
1933 info
= isl_union_access_info_set(info
, key
, access
);
1937 case isl_ai_key_schedule_map
:
1939 schedule_map
= read_union_map(s
);
1940 info
= isl_union_access_info_set_schedule_map(info
,
1945 case isl_ai_key_schedule
:
1947 schedule
= isl_stream_read_schedule(s
);
1948 info
= isl_union_access_info_set_schedule(info
,
1956 return isl_union_access_info_free(info
);
1958 if (isl_stream_yaml_read_end_mapping(s
) < 0) {
1959 isl_stream_error(s
, NULL
, "unexpected extra elements");
1960 return isl_union_access_info_free(info
);
1964 isl_stream_error(s
, NULL
, "no sink specified");
1965 return isl_union_access_info_free(info
);
1968 if (!schedule_set
) {
1969 isl_stream_error(s
, NULL
, "no schedule specified");
1970 return isl_union_access_info_free(info
);
1973 return isl_union_access_info_init(info
);
1976 /* Read an isl_union_access_info object from the file "input".
1978 __isl_give isl_union_access_info
*isl_union_access_info_read_from_file(
1979 isl_ctx
*ctx
, FILE *input
)
1982 isl_union_access_info
*access
;
1984 s
= isl_stream_new_file(ctx
, input
);
1987 access
= isl_stream_read_union_access_info(s
);
1993 /* Update the fields of "access" such that they all have the same parameters,
1994 * keeping in mind that the schedule_map field may be NULL and ignoring
1995 * the schedule field.
1997 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1998 __isl_take isl_union_access_info
*access
)
2001 enum isl_access_type i
;
2006 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
2007 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
2008 space
= isl_space_align_params(space
,
2009 isl_union_map_get_space(access
->access
[i
]));
2010 if (access
->schedule_map
)
2011 space
= isl_space_align_params(space
,
2012 isl_union_map_get_space(access
->schedule_map
));
2013 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2015 isl_union_map_align_params(access
->access
[i
],
2016 isl_space_copy(space
));
2017 if (!access
->schedule_map
) {
2018 isl_space_free(space
);
2020 access
->schedule_map
=
2021 isl_union_map_align_params(access
->schedule_map
, space
);
2022 if (!access
->schedule_map
)
2023 return isl_union_access_info_free(access
);
2026 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2027 if (!access
->access
[i
])
2028 return isl_union_access_info_free(access
);
2033 /* Prepend the schedule dimensions to the iteration domains.
2035 * That is, if the schedule is of the form
2039 * while the access relations are of the form
2043 * then the updated access relations are of the form
2047 * The schedule map is also replaced by the map
2051 * that is used during the internal computation.
2052 * Neither the original schedule map nor this updated schedule map
2053 * are used after the call to this function.
2055 static __isl_give isl_union_access_info
*
2056 isl_union_access_info_introduce_schedule(
2057 __isl_take isl_union_access_info
*access
)
2060 enum isl_access_type i
;
2065 sm
= isl_union_map_reverse(access
->schedule_map
);
2066 sm
= isl_union_map_range_map(sm
);
2067 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2069 isl_union_map_apply_range(isl_union_map_copy(sm
),
2071 access
->schedule_map
= sm
;
2073 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2074 if (!access
->access
[i
])
2075 return isl_union_access_info_free(access
);
2076 if (!access
->schedule_map
)
2077 return isl_union_access_info_free(access
);
2082 /* This structure represents the result of a dependence analysis computation.
2084 * "must_dep" represents the full definite dependences
2085 * "may_dep" represents the full non-definite dependences.
2086 * Both are of the form
2088 * [Source] -> [[Sink -> Data]]
2090 * (after the schedule dimensions have been projected out).
2091 * "must_no_source" represents the subset of the sink accesses for which
2092 * definitely no source was found.
2093 * "may_no_source" represents the subset of the sink accesses for which
2094 * possibly, but not definitely, no source was found.
2096 struct isl_union_flow
{
2097 isl_union_map
*must_dep
;
2098 isl_union_map
*may_dep
;
2099 isl_union_map
*must_no_source
;
2100 isl_union_map
*may_no_source
;
2103 /* Return the isl_ctx to which "flow" belongs.
2105 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
2107 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
2110 /* Free "flow" and return NULL.
2112 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
2116 isl_union_map_free(flow
->must_dep
);
2117 isl_union_map_free(flow
->may_dep
);
2118 isl_union_map_free(flow
->must_no_source
);
2119 isl_union_map_free(flow
->may_no_source
);
2124 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
2129 fprintf(stderr
, "must dependences: ");
2130 isl_union_map_dump(flow
->must_dep
);
2131 fprintf(stderr
, "may dependences: ");
2132 isl_union_map_dump(flow
->may_dep
);
2133 fprintf(stderr
, "must no source: ");
2134 isl_union_map_dump(flow
->must_no_source
);
2135 fprintf(stderr
, "may no source: ");
2136 isl_union_map_dump(flow
->may_no_source
);
2139 /* Return the full definite dependences in "flow", with accessed elements.
2141 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
2142 __isl_keep isl_union_flow
*flow
)
2146 return isl_union_map_copy(flow
->must_dep
);
2149 /* Return the full possible dependences in "flow", including the definite
2150 * dependences, with accessed elements.
2152 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
2153 __isl_keep isl_union_flow
*flow
)
2157 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2158 isl_union_map_copy(flow
->may_dep
));
2161 /* Return the definite dependences in "flow", without the accessed elements.
2163 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
2164 __isl_keep isl_union_flow
*flow
)
2170 dep
= isl_union_map_copy(flow
->must_dep
);
2171 return isl_union_map_range_factor_domain(dep
);
2174 /* Return the possible dependences in "flow", including the definite
2175 * dependences, without the accessed elements.
2177 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
2178 __isl_keep isl_union_flow
*flow
)
2184 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2185 isl_union_map_copy(flow
->may_dep
));
2186 return isl_union_map_range_factor_domain(dep
);
2189 /* Return the non-definite dependences in "flow".
2191 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
2192 __isl_keep isl_union_flow
*flow
)
2196 return isl_union_map_copy(flow
->may_dep
);
2199 /* Return the subset of the sink accesses for which definitely
2200 * no source was found.
2202 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
2203 __isl_keep isl_union_flow
*flow
)
2207 return isl_union_map_copy(flow
->must_no_source
);
2210 /* Return the subset of the sink accesses for which possibly
2211 * no source was found, including those for which definitely
2212 * no source was found.
2214 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
2215 __isl_keep isl_union_flow
*flow
)
2219 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
2220 isl_union_map_copy(flow
->may_no_source
));
2223 /* Return the subset of the sink accesses for which possibly, but not
2224 * definitely, no source was found.
2226 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
2227 __isl_keep isl_union_flow
*flow
)
2231 return isl_union_map_copy(flow
->may_no_source
);
2234 /* Create a new isl_union_flow object, initialized with empty
2235 * dependence relations and sink subsets.
2237 static __isl_give isl_union_flow
*isl_union_flow_alloc(
2238 __isl_take isl_space
*space
)
2241 isl_union_map
*empty
;
2242 isl_union_flow
*flow
;
2246 ctx
= isl_space_get_ctx(space
);
2247 flow
= isl_alloc_type(ctx
, isl_union_flow
);
2251 empty
= isl_union_map_empty(space
);
2252 flow
->must_dep
= isl_union_map_copy(empty
);
2253 flow
->may_dep
= isl_union_map_copy(empty
);
2254 flow
->must_no_source
= isl_union_map_copy(empty
);
2255 flow
->may_no_source
= empty
;
2257 if (!flow
->must_dep
|| !flow
->may_dep
||
2258 !flow
->must_no_source
|| !flow
->may_no_source
)
2259 return isl_union_flow_free(flow
);
2263 isl_space_free(space
);
2267 /* Copy this isl_union_flow object.
2269 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
2271 isl_union_flow
*copy
;
2276 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
2281 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
2282 isl_union_map_copy(flow
->must_dep
));
2283 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
2284 isl_union_map_copy(flow
->may_dep
));
2285 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
2286 isl_union_map_copy(flow
->must_no_source
));
2287 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
2288 isl_union_map_copy(flow
->may_no_source
));
2290 if (!copy
->must_dep
|| !copy
->may_dep
||
2291 !copy
->must_no_source
|| !copy
->may_no_source
)
2292 return isl_union_flow_free(copy
);
2297 /* Drop the schedule dimensions from the iteration domains in "flow".
2298 * In particular, the schedule dimensions have been prepended
2299 * to the iteration domains prior to the dependence analysis by
2300 * replacing the iteration domain D, by the wrapped map [S -> D].
2301 * Replace these wrapped maps by the original D.
2303 * In particular, the dependences computed by access_info_compute_flow_core
2306 * [S -> D] -> [[S' -> D'] -> A]
2308 * The schedule dimensions are projected out by first currying the range,
2311 * [S -> D] -> [S' -> [D' -> A]]
2313 * and then computing the factor range
2317 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
2318 __isl_take isl_union_flow
*flow
)
2323 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
2324 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
2325 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
2326 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
2327 flow
->must_no_source
=
2328 isl_union_map_domain_factor_range(flow
->must_no_source
);
2329 flow
->may_no_source
=
2330 isl_union_map_domain_factor_range(flow
->may_no_source
);
2332 if (!flow
->must_dep
|| !flow
->may_dep
||
2333 !flow
->must_no_source
|| !flow
->may_no_source
)
2334 return isl_union_flow_free(flow
);
2339 struct isl_compute_flow_data
{
2340 isl_union_map
*must_source
;
2341 isl_union_map
*may_source
;
2342 isl_union_flow
*flow
;
2347 struct isl_sched_info
*sink_info
;
2348 struct isl_sched_info
**source_info
;
2349 isl_access_info
*accesses
;
2352 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
2356 struct isl_compute_flow_data
*data
;
2358 data
= (struct isl_compute_flow_data
*)user
;
2360 dim
= isl_space_range(isl_map_get_space(map
));
2362 eq
= isl_space_is_equal(dim
, data
->dim
);
2364 isl_space_free(dim
);
2368 return isl_stat_error
;
2375 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
2379 struct isl_sched_info
*info
;
2380 struct isl_compute_flow_data
*data
;
2382 data
= (struct isl_compute_flow_data
*)user
;
2384 dim
= isl_space_range(isl_map_get_space(map
));
2386 eq
= isl_space_is_equal(dim
, data
->dim
);
2388 isl_space_free(dim
);
2397 info
= sched_info_alloc(map
);
2398 data
->source_info
[data
->count
] = info
;
2400 data
->accesses
= isl_access_info_add_source(data
->accesses
,
2401 map
, data
->must
, info
);
2408 return isl_stat_error
;
2411 /* Determine the shared nesting level and the "textual order" of
2412 * the given accesses.
2414 * We first determine the minimal schedule dimension for both accesses.
2416 * If among those dimensions, we can find one where both have a fixed
2417 * value and if moreover those values are different, then the previous
2418 * dimension is the last shared nesting level and the textual order
2419 * is determined based on the order of the fixed values.
2420 * If no such fixed values can be found, then we set the shared
2421 * nesting level to the minimal schedule dimension, with no textual ordering.
2423 static int before(void *first
, void *second
)
2425 struct isl_sched_info
*info1
= first
;
2426 struct isl_sched_info
*info2
= second
;
2430 n1
= isl_vec_size(info1
->cst
);
2431 n2
= isl_vec_size(info2
->cst
);
2432 if (n1
< 0 || n2
< 0)
2438 for (i
= 0; i
< n1
; ++i
) {
2442 if (!info1
->is_cst
[i
])
2444 if (!info2
->is_cst
[i
])
2446 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2450 r
= 2 * i
+ (cmp
< 0);
2458 /* Check if the given two accesses may be coscheduled.
2459 * If so, return isl_bool_true. Otherwise return isl_bool_false.
2461 * Two accesses may only be coscheduled if the fixed schedule
2462 * coordinates have the same values.
2464 static isl_bool
coscheduled(void *first
, void *second
)
2466 struct isl_sched_info
*info1
= first
;
2467 struct isl_sched_info
*info2
= second
;
2471 n1
= isl_vec_size(info1
->cst
);
2472 n2
= isl_vec_size(info2
->cst
);
2473 if (n1
< 0 || n2
< 0)
2474 return isl_bool_error
;
2479 for (i
= 0; i
< n1
; ++i
) {
2482 if (!info1
->is_cst
[i
])
2484 if (!info2
->is_cst
[i
])
2486 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2488 return isl_bool_false
;
2491 return isl_bool_true
;
2494 /* Given a sink access, look for all the source accesses that access
2495 * the same array and perform dataflow analysis on them using
2496 * isl_access_info_compute_flow_core.
2498 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
2502 struct isl_compute_flow_data
*data
;
2506 data
= (struct isl_compute_flow_data
*)user
;
2509 ctx
= isl_map_get_ctx(map
);
2511 data
->accesses
= NULL
;
2512 data
->sink_info
= NULL
;
2513 data
->source_info
= NULL
;
2515 data
->dim
= isl_space_range(isl_map_get_space(map
));
2517 if (isl_union_map_foreach_map(data
->must_source
,
2518 &count_matching_array
, data
) < 0)
2520 if (isl_union_map_foreach_map(data
->may_source
,
2521 &count_matching_array
, data
) < 0)
2524 data
->sink_info
= sched_info_alloc(map
);
2525 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
2528 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
2529 data
->sink_info
, &before
, data
->count
);
2530 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
2533 data
->accesses
->coscheduled
= &coscheduled
;
2536 if (isl_union_map_foreach_map(data
->must_source
,
2537 &collect_matching_array
, data
) < 0)
2540 if (isl_union_map_foreach_map(data
->may_source
,
2541 &collect_matching_array
, data
) < 0)
2544 flow
= access_info_compute_flow_core(data
->accesses
);
2545 data
->accesses
= NULL
;
2550 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
2551 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
2552 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2553 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2555 for (i
= 0; i
< flow
->n_source
; ++i
) {
2557 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2558 if (flow
->dep
[i
].must
)
2559 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2561 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2564 isl_flow_free(flow
);
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
);
2577 isl_access_info_free(data
->accesses
);
2578 sched_info_free(data
->sink_info
);
2579 if (data
->source_info
) {
2580 for (i
= 0; i
< data
->count
; ++i
)
2581 sched_info_free(data
->source_info
[i
]);
2582 free(data
->source_info
);
2584 isl_space_free(data
->dim
);
2587 return isl_stat_error
;
2590 /* Add the kills of "info" to the must-sources.
2592 static __isl_give isl_union_access_info
*
2593 isl_union_access_info_add_kill_to_must_source(
2594 __isl_take isl_union_access_info
*info
)
2596 isl_union_map
*must
, *kill
;
2598 must
= isl_union_access_info_get_must_source(info
);
2599 kill
= isl_union_access_info_get_kill(info
);
2600 must
= isl_union_map_union(must
, kill
);
2601 return isl_union_access_info_set_must_source(info
, must
);
2604 /* Drop dependences from "flow" that purely originate from kills.
2605 * That is, only keep those dependences that originate from
2606 * the original must-sources "must" and/or the original may-sources "may".
2607 * In particular, "must" contains the must-sources from before
2608 * the kills were added and "may" contains the may-source from before
2609 * the kills were removed.
2611 * The dependences are of the form
2613 * Source -> [Sink -> Data]
2615 * Only those dependences are kept where the Source -> Data part
2616 * is a subset of the original may-sources or must-sources.
2617 * Of those, only the must-dependences that intersect with the must-sources
2618 * remain must-dependences.
2619 * If there is some overlap between the may-sources and the must-sources,
2620 * then the may-dependences and must-dependences may also overlap.
2621 * This should be fine since the may-dependences are only kept
2622 * disjoint from the must-dependences for the isl_union_map_compute_flow
2623 * interface. This interface does not support kills, so it will
2624 * not end up calling this function.
2626 static __isl_give isl_union_flow
*isl_union_flow_drop_kill_source(
2627 __isl_take isl_union_flow
*flow
, __isl_take isl_union_map
*must
,
2628 __isl_take isl_union_map
*may
)
2630 isl_union_map
*move
;
2634 move
= isl_union_map_copy(flow
->must_dep
);
2635 move
= isl_union_map_intersect_range_factor_range(move
,
2636 isl_union_map_copy(may
));
2637 may
= isl_union_map_union(may
, isl_union_map_copy(must
));
2638 flow
->may_dep
= isl_union_map_intersect_range_factor_range(
2639 flow
->may_dep
, may
);
2640 flow
->must_dep
= isl_union_map_intersect_range_factor_range(
2641 flow
->must_dep
, must
);
2642 flow
->may_dep
= isl_union_map_union(flow
->may_dep
, move
);
2643 if (!flow
->must_dep
|| !flow
->may_dep
)
2644 return isl_union_flow_free(flow
);
2648 isl_union_map_free(must
);
2649 isl_union_map_free(may
);
2653 /* Remove the must accesses from the may accesses.
2655 * A must access always trumps a may access, so there is no need
2656 * for a must access to also be considered as a may access. Doing so
2657 * would only cost extra computations only to find out that
2658 * the duplicated may access does not make any difference.
2660 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2661 __isl_take isl_union_access_info
*access
)
2665 access
->access
[isl_access_may_source
] =
2666 isl_union_map_subtract(access
->access
[isl_access_may_source
],
2667 isl_union_map_copy(access
->access
[isl_access_must_source
]));
2668 if (!access
->access
[isl_access_may_source
])
2669 return isl_union_access_info_free(access
);
2674 /* Given a description of the "sink" accesses, the "source" accesses and
2675 * a schedule, compute for each instance of a sink access
2676 * and for each element accessed by that instance,
2677 * the possible or definite source accesses that last accessed the
2678 * element accessed by the sink access before this sink access
2679 * in the sense that there is no intermediate definite source access.
2681 * The must_no_source and may_no_source elements of the result
2682 * are subsets of access->sink. The elements must_dep and may_dep
2683 * map domain elements of access->{may,must)_source to
2684 * domain elements of access->sink.
2686 * This function is used when only the schedule map representation
2689 * We first prepend the schedule dimensions to the domain
2690 * of the accesses so that we can easily compare their relative order.
2691 * Then we consider each sink access individually in compute_flow.
2693 static __isl_give isl_union_flow
*compute_flow_union_map(
2694 __isl_take isl_union_access_info
*access
)
2696 struct isl_compute_flow_data data
;
2697 isl_union_map
*sink
;
2699 access
= isl_union_access_info_align_params(access
);
2700 access
= isl_union_access_info_introduce_schedule(access
);
2704 data
.must_source
= access
->access
[isl_access_must_source
];
2705 data
.may_source
= access
->access
[isl_access_may_source
];
2707 sink
= access
->access
[isl_access_sink
];
2708 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(sink
));
2710 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
2713 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2715 isl_union_access_info_free(access
);
2718 isl_union_access_info_free(access
);
2719 isl_union_flow_free(data
.flow
);
2723 /* A schedule access relation.
2725 * The access relation "access" is of the form [S -> D] -> A,
2726 * where S corresponds to the prefix schedule at "node".
2727 * "must" is only relevant for source accesses and indicates
2728 * whether the access is a must source or a may source.
2730 struct isl_scheduled_access
{
2733 isl_schedule_node
*node
;
2736 /* Data structure for keeping track of individual scheduled sink and source
2737 * accesses when computing dependence analysis based on a schedule tree.
2739 * "n_sink" is the number of used entries in "sink"
2740 * "n_source" is the number of used entries in "source"
2742 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2743 * to keep track of the current node and
2744 * of what extract_sink_source needs to do.
2746 struct isl_compute_flow_schedule_data
{
2747 isl_union_access_info
*access
;
2752 struct isl_scheduled_access
*sink
;
2753 struct isl_scheduled_access
*source
;
2757 isl_schedule_node
*node
;
2760 /* Align the parameters of all sinks with all sources.
2762 * If there are no sinks or no sources, then no alignment is needed.
2764 static void isl_compute_flow_schedule_data_align_params(
2765 struct isl_compute_flow_schedule_data
*data
)
2770 if (data
->n_sink
== 0 || data
->n_source
== 0)
2773 space
= isl_map_get_space(data
->sink
[0].access
);
2775 for (i
= 1; i
< data
->n_sink
; ++i
)
2776 space
= isl_space_align_params(space
,
2777 isl_map_get_space(data
->sink
[i
].access
));
2778 for (i
= 0; i
< data
->n_source
; ++i
)
2779 space
= isl_space_align_params(space
,
2780 isl_map_get_space(data
->source
[i
].access
));
2782 for (i
= 0; i
< data
->n_sink
; ++i
)
2783 data
->sink
[i
].access
=
2784 isl_map_align_params(data
->sink
[i
].access
,
2785 isl_space_copy(space
));
2786 for (i
= 0; i
< data
->n_source
; ++i
)
2787 data
->source
[i
].access
=
2788 isl_map_align_params(data
->source
[i
].access
,
2789 isl_space_copy(space
));
2791 isl_space_free(space
);
2794 /* Free all the memory referenced from "data".
2795 * Do not free "data" itself as it may be allocated on the stack.
2797 static void isl_compute_flow_schedule_data_clear(
2798 struct isl_compute_flow_schedule_data
*data
)
2805 for (i
= 0; i
< data
->n_sink
; ++i
) {
2806 isl_map_free(data
->sink
[i
].access
);
2807 isl_schedule_node_free(data
->sink
[i
].node
);
2810 for (i
= 0; i
< data
->n_source
; ++i
) {
2811 isl_map_free(data
->source
[i
].access
);
2812 isl_schedule_node_free(data
->source
[i
].node
);
2818 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2819 * (an upper bound on) the number of sinks and sources.
2821 * Sinks and sources are only extracted at leaves of the tree,
2822 * so we skip the node if it is not a leaf.
2823 * Otherwise we increment data->n_sink and data->n_source with
2824 * the number of spaces in the sink and source access domains
2825 * that reach this node.
2827 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2830 struct isl_compute_flow_schedule_data
*data
= user
;
2831 isl_union_set
*domain
;
2832 isl_union_map
*umap
;
2833 isl_bool r
= isl_bool_false
;
2836 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2837 return isl_bool_true
;
2839 domain
= isl_schedule_node_get_universe_domain(node
);
2841 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2842 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2843 data
->n_sink
+= n
= isl_union_map_n_map(umap
);
2844 isl_union_map_free(umap
);
2848 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2849 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2850 data
->n_source
+= n
= isl_union_map_n_map(umap
);
2851 isl_union_map_free(umap
);
2855 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2856 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2857 data
->n_source
+= n
= isl_union_map_n_map(umap
);
2858 isl_union_map_free(umap
);
2862 isl_union_set_free(domain
);
2867 /* Add a single scheduled sink or source (depending on data->set_sink)
2868 * with scheduled access relation "map", must property data->must and
2869 * schedule node data->node to the list of sinks or sources.
2871 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2873 struct isl_compute_flow_schedule_data
*data
= user
;
2874 struct isl_scheduled_access
*access
;
2877 access
= data
->sink
+ data
->n_sink
++;
2879 access
= data
->source
+ data
->n_source
++;
2881 access
->access
= map
;
2882 access
->must
= data
->must
;
2883 access
->node
= isl_schedule_node_copy(data
->node
);
2888 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2889 * individual scheduled source and sink accesses (taking into account
2890 * the domain of the schedule).
2892 * We only collect accesses at the leaves of the schedule tree.
2893 * We prepend the schedule dimensions at the leaf to the iteration
2894 * domains of the source and sink accesses and then extract
2895 * the individual accesses (per space).
2897 * In particular, if the prefix schedule at the node is of the form
2901 * while the access relations are of the form
2905 * then the updated access relations are of the form
2909 * Note that S consists of a single space such that introducing S
2910 * in the access relations does not increase the number of spaces.
2912 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2915 struct isl_compute_flow_schedule_data
*data
= user
;
2916 isl_union_map
*prefix
;
2917 isl_union_map
*umap
;
2918 isl_bool r
= isl_bool_false
;
2920 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2921 return isl_bool_true
;
2925 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2926 prefix
= isl_union_map_reverse(prefix
);
2927 prefix
= isl_union_map_range_map(prefix
);
2930 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2931 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2932 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2934 isl_union_map_free(umap
);
2938 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2939 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2940 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2942 isl_union_map_free(umap
);
2946 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2947 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2948 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2950 isl_union_map_free(umap
);
2952 isl_union_map_free(prefix
);
2957 /* isl_access_info_compute_flow callback for determining whether
2958 * the shared nesting level and the ordering within that level
2959 * for two scheduled accesses for use in compute_single_flow.
2961 * The tokens passed to this function refer to the leaves
2962 * in the schedule tree where the accesses take place.
2964 * If n is the shared number of loops, then we need to return
2965 * "2 * n + 1" if "first" precedes "second" inside the innermost
2966 * shared loop and "2 * n" otherwise.
2968 * The innermost shared ancestor may be the leaves themselves
2969 * if the accesses take place in the same leaf. Otherwise,
2970 * it is either a set node or a sequence node. Only in the case
2971 * of a sequence node do we consider one access to precede the other.
2973 static int before_node(void *first
, void *second
)
2975 isl_schedule_node
*node1
= first
;
2976 isl_schedule_node
*node2
= second
;
2977 isl_schedule_node
*shared
;
2981 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2982 depth
= isl_schedule_node_get_schedule_depth(shared
);
2984 isl_schedule_node_free(shared
);
2988 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2989 isl_size pos1
, pos2
;
2991 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2993 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2995 if (pos1
< 0 || pos2
< 0) {
2996 isl_schedule_node_free(shared
);
2999 before
= pos1
< pos2
;
3002 isl_schedule_node_free(shared
);
3004 return 2 * depth
+ before
;
3007 /* Check if the given two accesses may be coscheduled.
3008 * If so, return isl_bool_true. Otherwise return isl_bool_false.
3010 * Two accesses may only be coscheduled if they appear in the same leaf.
3012 static isl_bool
coscheduled_node(void *first
, void *second
)
3014 isl_schedule_node
*node1
= first
;
3015 isl_schedule_node
*node2
= second
;
3017 return node1
== node2
;
3020 /* Add the scheduled sources from "data" that access
3021 * the same data space as "sink" to "access".
3023 static __isl_give isl_access_info
*add_matching_sources(
3024 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
3025 struct isl_compute_flow_schedule_data
*data
)
3030 space
= isl_space_range(isl_map_get_space(sink
->access
));
3031 for (i
= 0; i
< data
->n_source
; ++i
) {
3032 struct isl_scheduled_access
*source
;
3033 isl_space
*source_space
;
3036 source
= &data
->source
[i
];
3037 source_space
= isl_map_get_space(source
->access
);
3038 source_space
= isl_space_range(source_space
);
3039 eq
= isl_space_is_equal(space
, source_space
);
3040 isl_space_free(source_space
);
3047 access
= isl_access_info_add_source(access
,
3048 isl_map_copy(source
->access
), source
->must
, source
->node
);
3051 isl_space_free(space
);
3054 isl_space_free(space
);
3055 isl_access_info_free(access
);
3059 /* Given a scheduled sink access relation "sink", compute the corresponding
3060 * dependences on the sources in "data" and add the computed dependences
3063 * The dependences computed by access_info_compute_flow_core are of the form
3065 * [S -> I] -> [[S' -> I'] -> A]
3067 * The schedule dimensions are projected out by first currying the range,
3070 * [S -> I] -> [S' -> [I' -> A]]
3072 * and then computing the factor range
3076 static __isl_give isl_union_flow
*compute_single_flow(
3077 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
3078 struct isl_compute_flow_schedule_data
*data
)
3081 isl_access_info
*access
;
3088 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
3089 &before_node
, data
->n_source
);
3091 access
->coscheduled
= &coscheduled_node
;
3092 access
= add_matching_sources(access
, sink
, data
);
3094 flow
= access_info_compute_flow_core(access
);
3096 return isl_union_flow_free(uf
);
3098 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
3099 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
3100 isl_union_map_from_map(map
));
3101 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
3102 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
3103 isl_union_map_from_map(map
));
3105 for (i
= 0; i
< flow
->n_source
; ++i
) {
3108 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
3109 map
= isl_map_factor_range(map
);
3110 dep
= isl_union_map_from_map(map
);
3111 if (flow
->dep
[i
].must
)
3112 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
3114 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
3117 isl_flow_free(flow
);
3122 /* Given a description of the "sink" accesses, the "source" accesses and
3123 * a schedule, compute for each instance of a sink access
3124 * and for each element accessed by that instance,
3125 * the possible or definite source accesses that last accessed the
3126 * element accessed by the sink access before this sink access
3127 * in the sense that there is no intermediate definite source access.
3128 * Only consider dependences between statement instances that belong
3129 * to the domain of the schedule.
3131 * The must_no_source and may_no_source elements of the result
3132 * are subsets of access->sink. The elements must_dep and may_dep
3133 * map domain elements of access->{may,must)_source to
3134 * domain elements of access->sink.
3136 * This function is used when a schedule tree representation
3139 * We extract the individual scheduled source and sink access relations
3140 * (taking into account the domain of the schedule) and
3141 * then compute dependences for each scheduled sink individually.
3143 static __isl_give isl_union_flow
*compute_flow_schedule(
3144 __isl_take isl_union_access_info
*access
)
3146 struct isl_compute_flow_schedule_data data
= { access
};
3150 isl_union_flow
*flow
;
3152 ctx
= isl_union_access_info_get_ctx(access
);
3156 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3157 &count_sink_source
, &data
) < 0)
3160 n
= data
.n_sink
+ data
.n_source
;
3161 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
3162 if (n
&& !data
.sink
)
3164 data
.source
= data
.sink
+ data
.n_sink
;
3168 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3169 &collect_sink_source
, &data
) < 0)
3172 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
3173 flow
= isl_union_flow_alloc(space
);
3175 isl_compute_flow_schedule_data_align_params(&data
);
3177 for (i
= 0; i
< data
.n_sink
; ++i
)
3178 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
3180 isl_compute_flow_schedule_data_clear(&data
);
3182 isl_union_access_info_free(access
);
3185 isl_union_access_info_free(access
);
3186 isl_compute_flow_schedule_data_clear(&data
);
3190 /* Given a description of the "sink" accesses, the "source" accesses and
3191 * a schedule, compute for each instance of a sink access
3192 * and for each element accessed by that instance,
3193 * the possible or definite source accesses that last accessed the
3194 * element accessed by the sink access before this sink access
3195 * in the sense that there is no intermediate definite source access.
3197 * The must_no_source and may_no_source elements of the result
3198 * are subsets of access->sink. The elements must_dep and may_dep
3199 * map domain elements of access->{may,must)_source to
3200 * domain elements of access->sink.
3202 * If any kills have been specified, then they are treated as
3203 * must-sources internally. Any dependence that purely derives
3204 * from an original kill is removed from the output.
3206 * We check whether the schedule is available as a schedule tree
3207 * or a schedule map and call the corresponding function to perform
3210 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
3211 __isl_take isl_union_access_info
*access
)
3214 isl_union_map
*must
= NULL
, *may
= NULL
;
3215 isl_union_flow
*flow
;
3217 has_kill
= isl_union_access_has_kill(access
);
3221 must
= isl_union_access_info_get_must_source(access
);
3222 may
= isl_union_access_info_get_may_source(access
);
3224 access
= isl_union_access_info_add_kill_to_must_source(access
);
3225 access
= isl_union_access_info_normalize(access
);
3228 if (access
->schedule
)
3229 flow
= compute_flow_schedule(access
);
3231 flow
= compute_flow_union_map(access
);
3233 flow
= isl_union_flow_drop_kill_source(flow
, must
, may
);
3236 isl_union_access_info_free(access
);
3237 isl_union_map_free(must
);
3238 isl_union_map_free(may
);
3242 /* Print the information contained in "flow" to "p".
3243 * The information is printed as a YAML document.
3245 __isl_give isl_printer
*isl_printer_print_union_flow(
3246 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
3248 isl_union_map
*umap
;
3251 return isl_printer_free(p
);
3253 p
= isl_printer_yaml_start_mapping(p
);
3254 umap
= isl_union_flow_get_full_must_dependence(flow
);
3255 p
= print_union_map_field(p
, "must_dependence", umap
);
3256 isl_union_map_free(umap
);
3257 umap
= isl_union_flow_get_full_may_dependence(flow
);
3258 p
= print_union_map_field(p
, "may_dependence", umap
);
3259 isl_union_map_free(umap
);
3260 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
3261 umap
= isl_union_flow_get_may_no_source(flow
);
3262 p
= print_union_map_field(p
, "may_no_source", umap
);
3263 isl_union_map_free(umap
);
3264 p
= isl_printer_yaml_end_mapping(p
);
3269 /* Return a string representation of the information in "flow".
3270 * The information is printed in flow format.
3272 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
3280 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
3281 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
3282 p
= isl_printer_print_union_flow(p
, flow
);
3283 s
= isl_printer_get_str(p
);
3284 isl_printer_free(p
);
3289 /* Given a collection of "sink" and "source" accesses,
3290 * compute for each iteration of a sink access
3291 * and for each element accessed by that iteration,
3292 * the source access in the list that last accessed the
3293 * element accessed by the sink access before this sink access.
3294 * Each access is given as a map from the loop iterators
3295 * to the array indices.
3296 * The result is a relations between source and sink
3297 * iterations and a subset of the domain of the sink accesses,
3298 * corresponding to those iterations that access an element
3299 * not previously accessed.
3301 * We collect the inputs in an isl_union_access_info object,
3302 * call isl_union_access_info_compute_flow and extract
3303 * the outputs from the result.
3305 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
3306 __isl_take isl_union_map
*must_source
,
3307 __isl_take isl_union_map
*may_source
,
3308 __isl_take isl_union_map
*schedule
,
3309 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
3310 __isl_give isl_union_map
**must_no_source
,
3311 __isl_give isl_union_map
**may_no_source
)
3313 isl_union_access_info
*access
;
3314 isl_union_flow
*flow
;
3316 access
= isl_union_access_info_from_sink(sink
);
3317 access
= isl_union_access_info_set_must_source(access
, must_source
);
3318 access
= isl_union_access_info_set_may_source(access
, may_source
);
3319 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
3320 flow
= isl_union_access_info_compute_flow(access
);
3323 *must_dep
= isl_union_flow_get_must_dependence(flow
);
3325 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
3327 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
3329 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
3331 isl_union_flow_free(flow
);
3333 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
3334 (must_no_source
&& !*must_no_source
) ||
3335 (may_no_source
&& !*may_no_source
))
3341 *must_dep
= isl_union_map_free(*must_dep
);
3343 *may_dep
= isl_union_map_free(*may_dep
);
3345 *must_no_source
= isl_union_map_free(*must_no_source
);
3347 *may_no_source
= isl_union_map_free(*may_no_source
);