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 int (*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 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
736 if (isl_map_plain_is_empty(temp_rel
[j
]))
739 for (k
= j
- 1; k
>= 0; --k
) {
741 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
743 return isl_stat_error
;
744 if (!can_precede_at_level(plevel
, sink_level
))
747 plevel2
= acc
->level_before(acc
->source
[j
].data
,
748 acc
->source
[k
].data
);
750 return isl_stat_error
;
752 for (level
= sink_level
; level
<= depth
; ++level
) {
754 struct isl_set
*trest
;
755 struct isl_map
*copy
;
757 if (!can_precede_at_level(plevel2
, level
))
760 copy
= isl_map_copy(temp_rel
[j
]);
761 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
763 if (isl_map_plain_is_empty(T
)) {
768 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
769 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
776 /* Compute all iterations of may source j that precedes the sink at the given
777 * level for sink iterations in set_C.
779 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
780 __isl_take isl_set
*set_C
, int j
, int level
)
787 read_map
= isl_map_copy(acc
->sink
.map
);
788 read_map
= isl_map_intersect_domain(read_map
, set_C
);
789 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
790 write_map
= isl_map_reverse(write_map
);
791 dep_map
= isl_map_apply_range(read_map
, write_map
);
792 after
= after_at_level(isl_map_get_space(dep_map
), level
);
793 dep_map
= isl_map_intersect(dep_map
, after
);
795 return isl_map_reverse(dep_map
);
798 /* For a given mapping between iterations of must source k and iterations
799 * of the sink, compute all iterations of may source j preceding
800 * the sink at level before_level for any of the sink iterations,
801 * but following the corresponding iteration of must source k at level
804 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
805 __isl_take isl_map
*old_map
,
806 int j
, int before_level
, int k
, int after_level
)
813 isl_map
*after_write
;
814 isl_map
*before_read
;
816 set_C
= isl_map_range(isl_map_copy(old_map
));
817 read_map
= isl_map_copy(acc
->sink
.map
);
818 read_map
= isl_map_intersect_domain(read_map
, set_C
);
819 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
821 write_map
= isl_map_reverse(write_map
);
822 dep_map
= isl_map_apply_range(read_map
, write_map
);
823 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
824 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
825 after_write
= after_at_level(dim
, after_level
);
826 after_write
= isl_map_apply_range(after_write
, old_map
);
827 after_write
= isl_map_reverse(after_write
);
828 dep_map
= isl_map_intersect(dep_map
, after_write
);
829 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
830 dep_map
= isl_map_intersect(dep_map
, before_read
);
831 return isl_map_reverse(dep_map
);
834 /* Given the must and may dependence relations for the must accesses
835 * for level sink_level, check if there are any accesses of may access j
836 * that occur in between and return their union.
837 * If some of these accesses are intermediate with respect to
838 * (previously thought to be) must dependences, then these
839 * must dependences are turned into may dependences.
841 static __isl_give isl_map
*all_intermediate_sources(
842 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
843 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
844 int j
, int sink_level
)
847 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
850 for (k
= 0; k
< acc
->n_must
; ++k
) {
853 if (isl_map_plain_is_empty(may_rel
[k
]) &&
854 isl_map_plain_is_empty(must_rel
[k
]))
857 plevel
= acc
->level_before(acc
->source
[k
].data
,
858 acc
->source
[acc
->n_must
+ j
].data
);
860 return isl_map_free(map
);
862 for (level
= sink_level
; level
<= depth
; ++level
) {
867 if (!can_precede_at_level(plevel
, level
))
870 copy
= isl_map_copy(may_rel
[k
]);
871 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
872 map
= isl_map_union(map
, T
);
874 copy
= isl_map_copy(must_rel
[k
]);
875 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
876 ran
= isl_map_range(isl_map_copy(T
));
877 map
= isl_map_union(map
, T
);
878 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
879 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
881 T
= isl_map_from_domain_and_range(
883 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
885 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
892 /* Given a dependence relation "old_map" between a must-source and the sink,
893 * return a subset of the dependences, augmented with instances
894 * of the source at position "pos" in "acc" that are coscheduled
895 * with the must-source and that access the same element.
896 * That is, if the input lives in a space T -> K, then the output
897 * lives in the space [T -> S] -> K, with S the space of source "pos", and
898 * the domain factor of the domain product is a subset of the input.
899 * The sources are considered to be coscheduled if they have the same values
900 * for the initial "depth" coordinates.
902 * First construct a dependence relation S -> K and a mapping
903 * between coscheduled sources T -> S.
904 * The second is combined with the original dependence relation T -> K
905 * to form a relation in T -> [S -> K], which is subsequently
906 * uncurried to [T -> S] -> K.
907 * This result is then intersected with the dependence relation S -> K
908 * to form the output.
910 * In case a negative depth is given, NULL is returned to indicate an error.
912 static __isl_give isl_map
*coscheduled_source(__isl_keep isl_access_info
*acc
,
913 __isl_keep isl_map
*old_map
, int pos
, int depth
)
926 set_C
= isl_map_range(isl_map_copy(old_map
));
927 read_map
= isl_map_copy(acc
->sink
.map
);
928 read_map
= isl_map_intersect_domain(read_map
, set_C
);
929 write_map
= isl_map_copy(acc
->source
[pos
].map
);
930 dep_map
= isl_map_domain_product(write_map
, read_map
);
931 dep_map
= isl_set_unwrap(isl_map_domain(dep_map
));
932 space
= isl_space_join(isl_map_get_space(old_map
),
933 isl_space_reverse(isl_map_get_space(dep_map
)));
934 equal
= isl_map_from_basic_map(isl_basic_map_equal(space
, depth
));
935 map
= isl_map_range_product(equal
, isl_map_copy(old_map
));
936 map
= isl_map_uncurry(map
);
937 map
= isl_map_intersect_domain_factor_range(map
, dep_map
);
942 /* After the dependences derived from a must-source have been computed
943 * at a certain level, check if any of the sources of the must-dependences
944 * may be coscheduled with other sources.
945 * If they are any such sources, then there is no way of determining
946 * which of the sources actually comes last and the must-dependences
947 * need to be turned into may-dependences, while dependences from
948 * the other sources need to be added to the may-dependences as well.
949 * "acc" describes the sources and a callback for checking whether
950 * two sources may be coscheduled. If acc->coscheduled is NULL then
951 * the sources are assumed not to be coscheduled.
952 * "must_rel" and "may_rel" describe the must and may-dependence relations
953 * computed at the current level for the must-sources. Some of the dependences
954 * may be moved from "must_rel" to "may_rel".
955 * "flow" contains all dependences computed so far (apart from those
956 * in "must_rel" and "may_rel") and may be updated with additional
957 * dependences derived from may-sources.
959 * In particular, consider all the must-sources with a non-empty
960 * dependence relation in "must_rel". They are considered in reverse
961 * order because that is the order in which they are considered in the caller.
962 * If any of the must-sources are coscheduled, then the last one
963 * is the one that will have a corresponding dependence relation.
964 * For each must-source i, consider both all the previous must-sources
965 * and all the may-sources. If any of those may be coscheduled with
966 * must-source i, then compute the coscheduled instances that access
967 * the same memory elements. The result is a relation [T -> S] -> K.
968 * The projection onto T -> K is a subset of the must-dependence relation
969 * that needs to be turned into may-dependences.
970 * The projection onto S -> K needs to be added to the may-dependences
972 * Since a given must-source instance may be coscheduled with several
973 * other source instances, the dependences that need to be turned
974 * into may-dependences are first collected and only actually removed
975 * from the must-dependences after all other sources have been considered.
977 static __isl_give isl_flow
*handle_coscheduled(__isl_keep isl_access_info
*acc
,
978 __isl_keep isl_map
**must_rel
, __isl_keep isl_map
**may_rel
,
979 __isl_take isl_flow
*flow
)
983 if (!acc
->coscheduled
)
985 for (i
= acc
->n_must
- 1; i
>= 0; --i
) {
988 if (isl_map_plain_is_empty(must_rel
[i
]))
990 move
= isl_map_empty(isl_map_get_space(must_rel
[i
]));
991 for (j
= i
- 1; j
>= 0; --j
) {
993 isl_map
*map
, *factor
;
995 if (!acc
->coscheduled(acc
->source
[i
].data
,
996 acc
->source
[j
].data
))
998 depth
= acc
->level_before(acc
->source
[i
].data
,
999 acc
->source
[j
].data
) / 2;
1000 map
= coscheduled_source(acc
, must_rel
[i
], j
, depth
);
1001 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
1002 may_rel
[j
] = isl_map_union(may_rel
[j
], factor
);
1003 map
= isl_map_domain_factor_domain(map
);
1004 move
= isl_map_union(move
, map
);
1006 for (j
= 0; j
< acc
->n_may
; ++j
) {
1008 isl_map
*map
, *factor
;
1010 pos
= acc
->n_must
+ j
;
1011 if (!acc
->coscheduled(acc
->source
[i
].data
,
1012 acc
->source
[pos
].data
))
1014 depth
= acc
->level_before(acc
->source
[i
].data
,
1015 acc
->source
[pos
].data
) / 2;
1016 map
= coscheduled_source(acc
, must_rel
[i
], pos
, depth
);
1017 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
1018 pos
= 2 * acc
->n_must
+ j
;
1019 flow
->dep
[pos
].map
= isl_map_union(flow
->dep
[pos
].map
,
1021 map
= isl_map_domain_factor_domain(map
);
1022 move
= isl_map_union(move
, map
);
1024 must_rel
[i
] = isl_map_subtract(must_rel
[i
], isl_map_copy(move
));
1025 may_rel
[i
] = isl_map_union(may_rel
[i
], move
);
1031 /* Compute dependences for the case where all accesses are "may"
1032 * accesses, which boils down to computing memory based dependences.
1033 * The generic algorithm would also work in this case, but it would
1034 * be overkill to use it.
1036 static __isl_give isl_flow
*compute_mem_based_dependences(
1037 __isl_keep isl_access_info
*acc
)
1044 res
= isl_flow_alloc(acc
);
1048 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1049 maydo
= isl_set_copy(mustdo
);
1051 for (i
= 0; i
< acc
->n_may
; ++i
) {
1058 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
1062 is_before
= plevel
& 1;
1065 dim
= isl_map_get_space(res
->dep
[i
].map
);
1067 before
= isl_map_lex_le_first(dim
, plevel
);
1069 before
= isl_map_lex_lt_first(dim
, plevel
);
1070 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
1071 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
1072 dep
= isl_map_intersect(dep
, before
);
1073 mustdo
= isl_set_subtract(mustdo
,
1074 isl_map_range(isl_map_copy(dep
)));
1075 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
1078 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
1079 res
->must_no_source
= mustdo
;
1083 isl_set_free(mustdo
);
1084 isl_set_free(maydo
);
1089 /* Compute dependences for the case where there is at least one
1092 * The core algorithm considers all levels in which a source may precede
1093 * the sink, where a level may either be a statement level or a loop level.
1094 * The outermost statement level is 1, the first loop level is 2, etc...
1095 * The algorithm basically does the following:
1096 * for all levels l of the read access from innermost to outermost
1097 * for all sources w that may precede the sink access at that level
1098 * compute the last iteration of the source that precedes the sink access
1100 * add result to possible last accesses at level l of source w
1101 * for all sources w2 that we haven't considered yet at this level that may
1102 * also precede the sink access
1103 * for all levels l2 of w from l to innermost
1104 * for all possible last accesses dep of w at l
1105 * compute last iteration of w2 between the source and sink
1107 * add result to possible last accesses at level l of write w2
1108 * and replace possible last accesses dep by the remainder
1111 * The above algorithm is applied to the must access. During the course
1112 * of the algorithm, we keep track of sink iterations that still
1113 * need to be considered. These iterations are split into those that
1114 * haven't been matched to any source access (mustdo) and those that have only
1115 * been matched to may accesses (maydo).
1116 * At the end of each level, must-sources and may-sources that are coscheduled
1117 * with the sources of the must-dependences at that level are considered.
1118 * If any coscheduled instances are found, then corresponding may-dependences
1119 * are added and the original must-dependences are turned into may-dependences.
1120 * Afterwards, the may accesses that occur after must-dependence sources
1122 * In particular, we consider may accesses that precede the remaining
1123 * sink iterations, moving elements from mustdo to maydo when appropriate,
1124 * and may accesses that occur between a must source and a sink of any
1125 * dependences found at the current level, turning must dependences into
1126 * may dependences when appropriate.
1129 static __isl_give isl_flow
*compute_val_based_dependences(
1130 __isl_keep isl_access_info
*acc
)
1134 isl_set
*mustdo
= NULL
;
1135 isl_set
*maydo
= NULL
;
1138 isl_map
**must_rel
= NULL
;
1139 isl_map
**may_rel
= NULL
;
1144 res
= isl_flow_alloc(acc
);
1147 ctx
= isl_map_get_ctx(acc
->sink
.map
);
1149 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
1150 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1151 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
1152 if (!mustdo
|| !maydo
)
1154 if (isl_set_plain_is_empty(mustdo
))
1157 must_rel
= isl_calloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1158 may_rel
= isl_calloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1159 if (!must_rel
|| !may_rel
)
1162 for (level
= depth
; level
>= 1; --level
) {
1163 for (j
= acc
->n_must
-1; j
>=0; --j
) {
1165 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
1166 must_rel
[j
] = isl_map_empty(space
);
1167 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
1170 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1172 struct isl_set
*rest
;
1175 plevel
= acc
->level_before(acc
->source
[j
].data
,
1179 if (!can_precede_at_level(plevel
, level
))
1182 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
1183 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
1186 if (intermediate_sources(acc
, must_rel
, j
, level
) < 0)
1189 T
= last_source(acc
, maydo
, j
, level
, &rest
);
1190 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
1193 if (intermediate_sources(acc
, may_rel
, j
, level
) < 0)
1196 if (isl_set_plain_is_empty(mustdo
) &&
1197 isl_set_plain_is_empty(maydo
))
1200 for (j
= j
- 1; j
>= 0; --j
) {
1203 plevel
= acc
->level_before(acc
->source
[j
].data
,
1207 if (!can_precede_at_level(plevel
, level
))
1210 if (intermediate_sources(acc
, must_rel
, j
, level
) < 0)
1212 if (intermediate_sources(acc
, may_rel
, j
, level
) < 0)
1216 handle_coscheduled(acc
, must_rel
, may_rel
, res
);
1218 for (j
= 0; j
< acc
->n_may
; ++j
) {
1223 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1227 if (!can_precede_at_level(plevel
, level
))
1230 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1231 res
->dep
[2 * acc
->n_must
+ j
].map
=
1232 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1233 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1234 ran
= isl_map_range(isl_map_copy(T
));
1235 res
->dep
[2 * acc
->n_must
+ j
].map
=
1236 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1237 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1238 maydo
= isl_set_union_disjoint(maydo
, ran
);
1240 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1241 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1243 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1246 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1247 res
->dep
[2 * j
].map
=
1248 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1250 res
->dep
[2 * j
+ 1].map
=
1251 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1255 if (isl_set_plain_is_empty(mustdo
) &&
1256 isl_set_plain_is_empty(maydo
))
1263 res
->must_no_source
= mustdo
;
1264 res
->may_no_source
= maydo
;
1268 for (j
= 0; j
< acc
->n_must
; ++j
)
1269 isl_map_free(must_rel
[j
]);
1271 for (j
= 0; j
< acc
->n_must
; ++j
)
1272 isl_map_free(may_rel
[j
]);
1274 isl_set_free(mustdo
);
1275 isl_set_free(maydo
);
1281 /* Given a "sink" access, a list of n "source" accesses,
1282 * compute for each iteration of the sink access
1283 * and for each element accessed by that iteration,
1284 * the source access in the list that last accessed the
1285 * element accessed by the sink access before this sink access.
1286 * Each access is given as a map from the loop iterators
1287 * to the array indices.
1288 * The result is a list of n relations between source and sink
1289 * iterations and a subset of the domain of the sink access,
1290 * corresponding to those iterations that access an element
1291 * not previously accessed.
1293 * To deal with multi-valued sink access relations, the sink iteration
1294 * domain is first extended with dimensions that correspond to the data
1295 * space. However, these extra dimensions are not projected out again.
1296 * It is up to the caller to decide whether these dimensions should be kept.
1298 static __isl_give isl_flow
*access_info_compute_flow_core(
1299 __isl_take isl_access_info
*acc
)
1301 struct isl_flow
*res
= NULL
;
1306 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1310 if (acc
->n_must
== 0)
1311 res
= compute_mem_based_dependences(acc
);
1313 acc
= isl_access_info_sort_sources(acc
);
1314 res
= compute_val_based_dependences(acc
);
1316 acc
= isl_access_info_free(acc
);
1319 if (!res
->must_no_source
|| !res
->may_no_source
)
1323 isl_access_info_free(acc
);
1328 /* Given a "sink" access, a list of n "source" accesses,
1329 * compute for each iteration of the sink access
1330 * and for each element accessed by that iteration,
1331 * the source access in the list that last accessed the
1332 * element accessed by the sink access before this sink access.
1333 * Each access is given as a map from the loop iterators
1334 * to the array indices.
1335 * The result is a list of n relations between source and sink
1336 * iterations and a subset of the domain of the sink access,
1337 * corresponding to those iterations that access an element
1338 * not previously accessed.
1340 * To deal with multi-valued sink access relations,
1341 * access_info_compute_flow_core extends the sink iteration domain
1342 * with dimensions that correspond to the data space. These extra dimensions
1343 * are projected out from the result of access_info_compute_flow_core.
1345 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1348 struct isl_flow
*res
;
1353 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1354 res
= access_info_compute_flow_core(acc
);
1358 for (j
= 0; j
< res
->n_source
; ++j
) {
1359 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1360 if (!res
->dep
[j
].map
)
1371 /* Keep track of some information about a schedule for a given
1372 * access. In particular, keep track of which dimensions
1373 * have a constant value and of the actual constant values.
1375 struct isl_sched_info
{
1380 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1384 isl_vec_free(info
->cst
);
1389 /* Extract information on the constant dimensions of the schedule
1390 * for a given access. The "map" is of the form
1394 * with S the schedule domain, D the iteration domain and A the data domain.
1396 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1397 __isl_keep isl_map
*map
)
1401 struct isl_sched_info
*info
;
1407 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1410 n
= isl_space_dim(dim
, isl_dim_in
);
1411 isl_space_free(dim
);
1413 ctx
= isl_map_get_ctx(map
);
1414 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1417 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1418 info
->cst
= isl_vec_alloc(ctx
, n
);
1419 if (n
&& (!info
->is_cst
|| !info
->cst
))
1422 for (i
= 0; i
< n
; ++i
) {
1425 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1428 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1429 if (info
->is_cst
[i
])
1430 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1437 sched_info_free(info
);
1441 /* The different types of access relations that isl_union_access_info
1444 * "isl_access_sink" represents the sink accesses.
1445 * "isl_access_must_source" represents the definite source accesses.
1446 * "isl_access_may_source" represents the possible source accesses.
1447 * "isl_access_kill" represents the kills.
1449 * isl_access_sink is sometimes treated differently and
1450 * should therefore appear first.
1452 enum isl_access_type
{
1454 isl_access_must_source
,
1455 isl_access_may_source
,
1460 /* This structure represents the input for a dependence analysis computation.
1462 * "access" contains the access relations.
1464 * "schedule" or "schedule_map" represents the execution order.
1465 * Exactly one of these fields should be NULL. The other field
1466 * determines the execution order.
1468 * The domains of these four maps refer to the same iteration spaces(s).
1469 * The ranges of the first three maps also refer to the same data space(s).
1471 * After a call to isl_union_access_info_introduce_schedule,
1472 * the "schedule_map" field no longer contains useful information.
1474 struct isl_union_access_info
{
1475 isl_union_map
*access
[isl_access_end
];
1477 isl_schedule
*schedule
;
1478 isl_union_map
*schedule_map
;
1481 /* Free "access" and return NULL.
1483 __isl_null isl_union_access_info
*isl_union_access_info_free(
1484 __isl_take isl_union_access_info
*access
)
1486 enum isl_access_type i
;
1491 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1492 isl_union_map_free(access
->access
[i
]);
1493 isl_schedule_free(access
->schedule
);
1494 isl_union_map_free(access
->schedule_map
);
1500 /* Return the isl_ctx to which "access" belongs.
1502 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1506 return isl_union_map_get_ctx(access
->access
[isl_access_sink
]);
1509 /* Construct an empty (invalid) isl_union_access_info object.
1510 * The caller is responsible for setting the sink access relation and
1511 * initializing all the other fields, e.g., by calling
1512 * isl_union_access_info_init.
1514 static __isl_give isl_union_access_info
*isl_union_access_info_alloc(
1517 return isl_calloc_type(ctx
, isl_union_access_info
);
1520 /* Initialize all the fields of "info", except the sink access relation,
1521 * which is assumed to have been set by the caller.
1523 * By default, we use the schedule field of the isl_union_access_info,
1524 * but this may be overridden by a call
1525 * to isl_union_access_info_set_schedule_map.
1527 static __isl_give isl_union_access_info
*isl_union_access_info_init(
1528 __isl_take isl_union_access_info
*info
)
1531 isl_union_map
*empty
;
1532 enum isl_access_type i
;
1536 if (!info
->access
[isl_access_sink
])
1537 return isl_union_access_info_free(info
);
1539 space
= isl_union_map_get_space(info
->access
[isl_access_sink
]);
1540 empty
= isl_union_map_empty(isl_space_copy(space
));
1541 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1542 if (!info
->access
[i
])
1543 info
->access
[i
] = isl_union_map_copy(empty
);
1544 isl_union_map_free(empty
);
1545 if (!info
->schedule
&& !info
->schedule_map
)
1546 info
->schedule
= isl_schedule_empty(isl_space_copy(space
));
1547 isl_space_free(space
);
1549 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1550 if (!info
->access
[i
])
1551 return isl_union_access_info_free(info
);
1552 if (!info
->schedule
&& !info
->schedule_map
)
1553 return isl_union_access_info_free(info
);
1558 /* Create a new isl_union_access_info with the given sink accesses and
1559 * and no other accesses or schedule information.
1561 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1562 __isl_take isl_union_map
*sink
)
1565 isl_union_access_info
*access
;
1569 ctx
= isl_union_map_get_ctx(sink
);
1570 access
= isl_union_access_info_alloc(ctx
);
1573 access
->access
[isl_access_sink
] = sink
;
1574 return isl_union_access_info_init(access
);
1576 isl_union_map_free(sink
);
1580 /* Replace the access relation of type "type" of "info" by "access".
1582 static __isl_give isl_union_access_info
*isl_union_access_info_set(
1583 __isl_take isl_union_access_info
*info
,
1584 enum isl_access_type type
, __isl_take isl_union_map
*access
)
1586 if (!info
|| !access
)
1589 isl_union_map_free(info
->access
[type
]);
1590 info
->access
[type
] = access
;
1594 isl_union_access_info_free(info
);
1595 isl_union_map_free(access
);
1599 /* Replace the definite source accesses of "access" by "must_source".
1601 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1602 __isl_take isl_union_access_info
*access
,
1603 __isl_take isl_union_map
*must_source
)
1605 return isl_union_access_info_set(access
, isl_access_must_source
,
1609 /* Replace the possible source accesses of "access" by "may_source".
1611 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1612 __isl_take isl_union_access_info
*access
,
1613 __isl_take isl_union_map
*may_source
)
1615 return isl_union_access_info_set(access
, isl_access_may_source
,
1619 /* Replace the kills of "info" by "kill".
1621 __isl_give isl_union_access_info
*isl_union_access_info_set_kill(
1622 __isl_take isl_union_access_info
*info
, __isl_take isl_union_map
*kill
)
1624 return isl_union_access_info_set(info
, isl_access_kill
, kill
);
1627 /* Return the access relation of type "type" of "info".
1629 static __isl_give isl_union_map
*isl_union_access_info_get(
1630 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1634 return isl_union_map_copy(info
->access
[type
]);
1637 /* Return the definite source accesses of "info".
1639 __isl_give isl_union_map
*isl_union_access_info_get_must_source(
1640 __isl_keep isl_union_access_info
*info
)
1642 return isl_union_access_info_get(info
, isl_access_must_source
);
1645 /* Return the possible source accesses of "info".
1647 __isl_give isl_union_map
*isl_union_access_info_get_may_source(
1648 __isl_keep isl_union_access_info
*info
)
1650 return isl_union_access_info_get(info
, isl_access_may_source
);
1653 /* Return the kills of "info".
1655 __isl_give isl_union_map
*isl_union_access_info_get_kill(
1656 __isl_keep isl_union_access_info
*info
)
1658 return isl_union_access_info_get(info
, isl_access_kill
);
1661 /* Does "info" specify any kills?
1663 static isl_bool
isl_union_access_has_kill(
1664 __isl_keep isl_union_access_info
*info
)
1669 return isl_bool_error
;
1670 empty
= isl_union_map_is_empty(info
->access
[isl_access_kill
]);
1671 return isl_bool_not(empty
);
1674 /* Replace the schedule of "access" by "schedule".
1675 * Also free the schedule_map in case it was set last.
1677 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1678 __isl_take isl_union_access_info
*access
,
1679 __isl_take isl_schedule
*schedule
)
1681 if (!access
|| !schedule
)
1684 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1685 isl_schedule_free(access
->schedule
);
1686 access
->schedule
= schedule
;
1690 isl_union_access_info_free(access
);
1691 isl_schedule_free(schedule
);
1695 /* Replace the schedule map of "access" by "schedule_map".
1696 * Also free the schedule in case it was set last.
1698 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1699 __isl_take isl_union_access_info
*access
,
1700 __isl_take isl_union_map
*schedule_map
)
1702 if (!access
|| !schedule_map
)
1705 isl_union_map_free(access
->schedule_map
);
1706 access
->schedule
= isl_schedule_free(access
->schedule
);
1707 access
->schedule_map
= schedule_map
;
1711 isl_union_access_info_free(access
);
1712 isl_union_map_free(schedule_map
);
1716 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1717 __isl_keep isl_union_access_info
*access
)
1719 isl_union_access_info
*copy
;
1720 enum isl_access_type i
;
1724 copy
= isl_union_access_info_from_sink(
1725 isl_union_map_copy(access
->access
[isl_access_sink
]));
1726 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1727 copy
= isl_union_access_info_set(copy
, i
,
1728 isl_union_map_copy(access
->access
[i
]));
1729 if (access
->schedule
)
1730 copy
= isl_union_access_info_set_schedule(copy
,
1731 isl_schedule_copy(access
->schedule
));
1733 copy
= isl_union_access_info_set_schedule_map(copy
,
1734 isl_union_map_copy(access
->schedule_map
));
1739 /* Print a key-value pair of a YAML mapping to "p",
1740 * with key "name" and value "umap".
1742 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1743 const char *name
, __isl_keep isl_union_map
*umap
)
1745 p
= isl_printer_print_str(p
, name
);
1746 p
= isl_printer_yaml_next(p
);
1747 p
= isl_printer_print_str(p
, "\"");
1748 p
= isl_printer_print_union_map(p
, umap
);
1749 p
= isl_printer_print_str(p
, "\"");
1750 p
= isl_printer_yaml_next(p
);
1755 /* An enumeration of the various keys that may appear in a YAML mapping
1756 * of an isl_union_access_info object.
1757 * The keys for the access relation types are assumed to have the same values
1758 * as the access relation types in isl_access_type.
1761 isl_ai_key_error
= -1,
1762 isl_ai_key_sink
= isl_access_sink
,
1763 isl_ai_key_must_source
= isl_access_must_source
,
1764 isl_ai_key_may_source
= isl_access_may_source
,
1765 isl_ai_key_kill
= isl_access_kill
,
1766 isl_ai_key_schedule_map
,
1767 isl_ai_key_schedule
,
1771 /* Textual representations of the YAML keys for an isl_union_access_info
1774 static char *key_str
[] = {
1775 [isl_ai_key_sink
] = "sink",
1776 [isl_ai_key_must_source
] = "must_source",
1777 [isl_ai_key_may_source
] = "may_source",
1778 [isl_ai_key_kill
] = "kill",
1779 [isl_ai_key_schedule_map
] = "schedule_map",
1780 [isl_ai_key_schedule
] = "schedule",
1783 /* Print a key-value pair corresponding to the access relation of type "type"
1784 * of a YAML mapping of "info" to "p".
1786 * The sink access relation is always printed, but any other access relation
1787 * is only printed if it is non-empty.
1789 static __isl_give isl_printer
*print_access_field(__isl_take isl_printer
*p
,
1790 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1792 if (type
!= isl_access_sink
) {
1795 empty
= isl_union_map_is_empty(info
->access
[type
]);
1797 return isl_printer_free(p
);
1801 return print_union_map_field(p
, key_str
[type
], info
->access
[type
]);
1804 /* Print the information contained in "access" to "p".
1805 * The information is printed as a YAML document.
1807 __isl_give isl_printer
*isl_printer_print_union_access_info(
1808 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1810 enum isl_access_type i
;
1813 return isl_printer_free(p
);
1815 p
= isl_printer_yaml_start_mapping(p
);
1816 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1817 p
= print_access_field(p
, access
, i
);
1818 if (access
->schedule
) {
1819 p
= isl_printer_print_str(p
, key_str
[isl_ai_key_schedule
]);
1820 p
= isl_printer_yaml_next(p
);
1821 p
= isl_printer_print_schedule(p
, access
->schedule
);
1822 p
= isl_printer_yaml_next(p
);
1824 p
= print_union_map_field(p
, key_str
[isl_ai_key_schedule_map
],
1825 access
->schedule_map
);
1827 p
= isl_printer_yaml_end_mapping(p
);
1832 /* Return a string representation of the information in "access".
1833 * The information is printed in flow format.
1835 __isl_give
char *isl_union_access_info_to_str(
1836 __isl_keep isl_union_access_info
*access
)
1844 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1845 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1846 p
= isl_printer_print_union_access_info(p
, access
);
1847 s
= isl_printer_get_str(p
);
1848 isl_printer_free(p
);
1854 #define KEY enum isl_ai_key
1856 #define KEY_ERROR isl_ai_key_error
1858 #define KEY_END isl_ai_key_end
1859 #include "extract_key.c"
1862 #define BASE union_map
1863 #include "read_in_string_templ.c"
1865 /* Read an isl_union_access_info object from "s".
1867 * Start off with an empty (invalid) isl_union_access_info object and
1868 * then fill up the fields based on the input.
1869 * The input needs to contain at least a description of the sink
1870 * access relation as well as some form of schedule.
1871 * The other access relations are set to empty relations
1872 * by isl_union_access_info_init if they are not specified in the input.
1874 __isl_give isl_union_access_info
*isl_stream_read_union_access_info(
1878 isl_union_access_info
*info
;
1881 int schedule_set
= 0;
1883 if (isl_stream_yaml_read_start_mapping(s
))
1886 ctx
= isl_stream_get_ctx(s
);
1887 info
= isl_union_access_info_alloc(ctx
);
1888 while ((more
= isl_stream_yaml_next(s
)) > 0) {
1889 enum isl_ai_key key
;
1890 isl_union_map
*access
, *schedule_map
;
1891 isl_schedule
*schedule
;
1894 if (isl_stream_yaml_next(s
) < 0)
1895 return isl_union_access_info_free(info
);
1897 case isl_ai_key_end
:
1898 case isl_ai_key_error
:
1899 return isl_union_access_info_free(info
);
1900 case isl_ai_key_sink
:
1902 case isl_ai_key_must_source
:
1903 case isl_ai_key_may_source
:
1904 case isl_ai_key_kill
:
1905 access
= read_union_map(s
);
1906 info
= isl_union_access_info_set(info
, key
, access
);
1910 case isl_ai_key_schedule_map
:
1912 schedule_map
= read_union_map(s
);
1913 info
= isl_union_access_info_set_schedule_map(info
,
1918 case isl_ai_key_schedule
:
1920 schedule
= isl_stream_read_schedule(s
);
1921 info
= isl_union_access_info_set_schedule(info
,
1929 return isl_union_access_info_free(info
);
1931 if (isl_stream_yaml_read_end_mapping(s
) < 0) {
1932 isl_stream_error(s
, NULL
, "unexpected extra elements");
1933 return isl_union_access_info_free(info
);
1937 isl_stream_error(s
, NULL
, "no sink specified");
1938 return isl_union_access_info_free(info
);
1941 if (!schedule_set
) {
1942 isl_stream_error(s
, NULL
, "no schedule specified");
1943 return isl_union_access_info_free(info
);
1946 return isl_union_access_info_init(info
);
1949 /* Read an isl_union_access_info object from the file "input".
1951 __isl_give isl_union_access_info
*isl_union_access_info_read_from_file(
1952 isl_ctx
*ctx
, FILE *input
)
1955 isl_union_access_info
*access
;
1957 s
= isl_stream_new_file(ctx
, input
);
1960 access
= isl_stream_read_union_access_info(s
);
1966 /* Update the fields of "access" such that they all have the same parameters,
1967 * keeping in mind that the schedule_map field may be NULL and ignoring
1968 * the schedule field.
1970 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1971 __isl_take isl_union_access_info
*access
)
1974 enum isl_access_type i
;
1979 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
1980 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1981 space
= isl_space_align_params(space
,
1982 isl_union_map_get_space(access
->access
[i
]));
1983 if (access
->schedule_map
)
1984 space
= isl_space_align_params(space
,
1985 isl_union_map_get_space(access
->schedule_map
));
1986 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1988 isl_union_map_align_params(access
->access
[i
],
1989 isl_space_copy(space
));
1990 if (!access
->schedule_map
) {
1991 isl_space_free(space
);
1993 access
->schedule_map
=
1994 isl_union_map_align_params(access
->schedule_map
, space
);
1995 if (!access
->schedule_map
)
1996 return isl_union_access_info_free(access
);
1999 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2000 if (!access
->access
[i
])
2001 return isl_union_access_info_free(access
);
2006 /* Prepend the schedule dimensions to the iteration domains.
2008 * That is, if the schedule is of the form
2012 * while the access relations are of the form
2016 * then the updated access relations are of the form
2020 * The schedule map is also replaced by the map
2024 * that is used during the internal computation.
2025 * Neither the original schedule map nor this updated schedule map
2026 * are used after the call to this function.
2028 static __isl_give isl_union_access_info
*
2029 isl_union_access_info_introduce_schedule(
2030 __isl_take isl_union_access_info
*access
)
2033 enum isl_access_type i
;
2038 sm
= isl_union_map_reverse(access
->schedule_map
);
2039 sm
= isl_union_map_range_map(sm
);
2040 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2042 isl_union_map_apply_range(isl_union_map_copy(sm
),
2044 access
->schedule_map
= sm
;
2046 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2047 if (!access
->access
[i
])
2048 return isl_union_access_info_free(access
);
2049 if (!access
->schedule_map
)
2050 return isl_union_access_info_free(access
);
2055 /* This structure represents the result of a dependence analysis computation.
2057 * "must_dep" represents the full definite dependences
2058 * "may_dep" represents the full non-definite dependences.
2059 * Both are of the form
2061 * [Source] -> [[Sink -> Data]]
2063 * (after the schedule dimensions have been projected out).
2064 * "must_no_source" represents the subset of the sink accesses for which
2065 * definitely no source was found.
2066 * "may_no_source" represents the subset of the sink accesses for which
2067 * possibly, but not definitely, no source was found.
2069 struct isl_union_flow
{
2070 isl_union_map
*must_dep
;
2071 isl_union_map
*may_dep
;
2072 isl_union_map
*must_no_source
;
2073 isl_union_map
*may_no_source
;
2076 /* Return the isl_ctx to which "flow" belongs.
2078 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
2080 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
2083 /* Free "flow" and return NULL.
2085 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
2089 isl_union_map_free(flow
->must_dep
);
2090 isl_union_map_free(flow
->may_dep
);
2091 isl_union_map_free(flow
->must_no_source
);
2092 isl_union_map_free(flow
->may_no_source
);
2097 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
2102 fprintf(stderr
, "must dependences: ");
2103 isl_union_map_dump(flow
->must_dep
);
2104 fprintf(stderr
, "may dependences: ");
2105 isl_union_map_dump(flow
->may_dep
);
2106 fprintf(stderr
, "must no source: ");
2107 isl_union_map_dump(flow
->must_no_source
);
2108 fprintf(stderr
, "may no source: ");
2109 isl_union_map_dump(flow
->may_no_source
);
2112 /* Return the full definite dependences in "flow", with accessed elements.
2114 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
2115 __isl_keep isl_union_flow
*flow
)
2119 return isl_union_map_copy(flow
->must_dep
);
2122 /* Return the full possible dependences in "flow", including the definite
2123 * dependences, with accessed elements.
2125 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
2126 __isl_keep isl_union_flow
*flow
)
2130 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2131 isl_union_map_copy(flow
->may_dep
));
2134 /* Return the definite dependences in "flow", without the accessed elements.
2136 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
2137 __isl_keep isl_union_flow
*flow
)
2143 dep
= isl_union_map_copy(flow
->must_dep
);
2144 return isl_union_map_range_factor_domain(dep
);
2147 /* Return the possible dependences in "flow", including the definite
2148 * dependences, without the accessed elements.
2150 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
2151 __isl_keep isl_union_flow
*flow
)
2157 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2158 isl_union_map_copy(flow
->may_dep
));
2159 return isl_union_map_range_factor_domain(dep
);
2162 /* Return the non-definite dependences in "flow".
2164 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
2165 __isl_keep isl_union_flow
*flow
)
2169 return isl_union_map_copy(flow
->may_dep
);
2172 /* Return the subset of the sink accesses for which definitely
2173 * no source was found.
2175 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
2176 __isl_keep isl_union_flow
*flow
)
2180 return isl_union_map_copy(flow
->must_no_source
);
2183 /* Return the subset of the sink accesses for which possibly
2184 * no source was found, including those for which definitely
2185 * no source was found.
2187 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
2188 __isl_keep isl_union_flow
*flow
)
2192 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
2193 isl_union_map_copy(flow
->may_no_source
));
2196 /* Return the subset of the sink accesses for which possibly, but not
2197 * definitely, no source was found.
2199 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
2200 __isl_keep isl_union_flow
*flow
)
2204 return isl_union_map_copy(flow
->may_no_source
);
2207 /* Create a new isl_union_flow object, initialized with empty
2208 * dependence relations and sink subsets.
2210 static __isl_give isl_union_flow
*isl_union_flow_alloc(
2211 __isl_take isl_space
*space
)
2214 isl_union_map
*empty
;
2215 isl_union_flow
*flow
;
2219 ctx
= isl_space_get_ctx(space
);
2220 flow
= isl_alloc_type(ctx
, isl_union_flow
);
2224 empty
= isl_union_map_empty(space
);
2225 flow
->must_dep
= isl_union_map_copy(empty
);
2226 flow
->may_dep
= isl_union_map_copy(empty
);
2227 flow
->must_no_source
= isl_union_map_copy(empty
);
2228 flow
->may_no_source
= empty
;
2230 if (!flow
->must_dep
|| !flow
->may_dep
||
2231 !flow
->must_no_source
|| !flow
->may_no_source
)
2232 return isl_union_flow_free(flow
);
2236 isl_space_free(space
);
2240 /* Copy this isl_union_flow object.
2242 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
2244 isl_union_flow
*copy
;
2249 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
2254 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
2255 isl_union_map_copy(flow
->must_dep
));
2256 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
2257 isl_union_map_copy(flow
->may_dep
));
2258 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
2259 isl_union_map_copy(flow
->must_no_source
));
2260 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
2261 isl_union_map_copy(flow
->may_no_source
));
2263 if (!copy
->must_dep
|| !copy
->may_dep
||
2264 !copy
->must_no_source
|| !copy
->may_no_source
)
2265 return isl_union_flow_free(copy
);
2270 /* Drop the schedule dimensions from the iteration domains in "flow".
2271 * In particular, the schedule dimensions have been prepended
2272 * to the iteration domains prior to the dependence analysis by
2273 * replacing the iteration domain D, by the wrapped map [S -> D].
2274 * Replace these wrapped maps by the original D.
2276 * In particular, the dependences computed by access_info_compute_flow_core
2279 * [S -> D] -> [[S' -> D'] -> A]
2281 * The schedule dimensions are projected out by first currying the range,
2284 * [S -> D] -> [S' -> [D' -> A]]
2286 * and then computing the factor range
2290 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
2291 __isl_take isl_union_flow
*flow
)
2296 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
2297 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
2298 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
2299 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
2300 flow
->must_no_source
=
2301 isl_union_map_domain_factor_range(flow
->must_no_source
);
2302 flow
->may_no_source
=
2303 isl_union_map_domain_factor_range(flow
->may_no_source
);
2305 if (!flow
->must_dep
|| !flow
->may_dep
||
2306 !flow
->must_no_source
|| !flow
->may_no_source
)
2307 return isl_union_flow_free(flow
);
2312 struct isl_compute_flow_data
{
2313 isl_union_map
*must_source
;
2314 isl_union_map
*may_source
;
2315 isl_union_flow
*flow
;
2320 struct isl_sched_info
*sink_info
;
2321 struct isl_sched_info
**source_info
;
2322 isl_access_info
*accesses
;
2325 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
2329 struct isl_compute_flow_data
*data
;
2331 data
= (struct isl_compute_flow_data
*)user
;
2333 dim
= isl_space_range(isl_map_get_space(map
));
2335 eq
= isl_space_is_equal(dim
, data
->dim
);
2337 isl_space_free(dim
);
2341 return isl_stat_error
;
2348 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
2352 struct isl_sched_info
*info
;
2353 struct isl_compute_flow_data
*data
;
2355 data
= (struct isl_compute_flow_data
*)user
;
2357 dim
= isl_space_range(isl_map_get_space(map
));
2359 eq
= isl_space_is_equal(dim
, data
->dim
);
2361 isl_space_free(dim
);
2370 info
= sched_info_alloc(map
);
2371 data
->source_info
[data
->count
] = info
;
2373 data
->accesses
= isl_access_info_add_source(data
->accesses
,
2374 map
, data
->must
, info
);
2381 return isl_stat_error
;
2384 /* Determine the shared nesting level and the "textual order" of
2385 * the given accesses.
2387 * We first determine the minimal schedule dimension for both accesses.
2389 * If among those dimensions, we can find one where both have a fixed
2390 * value and if moreover those values are different, then the previous
2391 * dimension is the last shared nesting level and the textual order
2392 * is determined based on the order of the fixed values.
2393 * If no such fixed values can be found, then we set the shared
2394 * nesting level to the minimal schedule dimension, with no textual ordering.
2396 static int before(void *first
, void *second
)
2398 struct isl_sched_info
*info1
= first
;
2399 struct isl_sched_info
*info2
= second
;
2403 n1
= isl_vec_size(info1
->cst
);
2404 n2
= isl_vec_size(info2
->cst
);
2409 for (i
= 0; i
< n1
; ++i
) {
2413 if (!info1
->is_cst
[i
])
2415 if (!info2
->is_cst
[i
])
2417 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2421 r
= 2 * i
+ (cmp
< 0);
2429 /* Check if the given two accesses may be coscheduled.
2430 * If so, return 1. Otherwise return 0.
2432 * Two accesses may only be coscheduled if the fixed schedule
2433 * coordinates have the same values.
2435 static int coscheduled(void *first
, void *second
)
2437 struct isl_sched_info
*info1
= first
;
2438 struct isl_sched_info
*info2
= second
;
2442 n1
= isl_vec_size(info1
->cst
);
2443 n2
= isl_vec_size(info2
->cst
);
2448 for (i
= 0; i
< n1
; ++i
) {
2451 if (!info1
->is_cst
[i
])
2453 if (!info2
->is_cst
[i
])
2455 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2463 /* Given a sink access, look for all the source accesses that access
2464 * the same array and perform dataflow analysis on them using
2465 * isl_access_info_compute_flow_core.
2467 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
2471 struct isl_compute_flow_data
*data
;
2475 data
= (struct isl_compute_flow_data
*)user
;
2478 ctx
= isl_map_get_ctx(map
);
2480 data
->accesses
= NULL
;
2481 data
->sink_info
= NULL
;
2482 data
->source_info
= NULL
;
2484 data
->dim
= isl_space_range(isl_map_get_space(map
));
2486 if (isl_union_map_foreach_map(data
->must_source
,
2487 &count_matching_array
, data
) < 0)
2489 if (isl_union_map_foreach_map(data
->may_source
,
2490 &count_matching_array
, data
) < 0)
2493 data
->sink_info
= sched_info_alloc(map
);
2494 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
2497 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
2498 data
->sink_info
, &before
, data
->count
);
2499 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
2502 data
->accesses
->coscheduled
= &coscheduled
;
2505 if (isl_union_map_foreach_map(data
->must_source
,
2506 &collect_matching_array
, data
) < 0)
2509 if (isl_union_map_foreach_map(data
->may_source
,
2510 &collect_matching_array
, data
) < 0)
2513 flow
= access_info_compute_flow_core(data
->accesses
);
2514 data
->accesses
= NULL
;
2519 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
2520 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
2521 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2522 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2524 for (i
= 0; i
< flow
->n_source
; ++i
) {
2526 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2527 if (flow
->dep
[i
].must
)
2528 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2530 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2533 isl_flow_free(flow
);
2535 sched_info_free(data
->sink_info
);
2536 if (data
->source_info
) {
2537 for (i
= 0; i
< data
->count
; ++i
)
2538 sched_info_free(data
->source_info
[i
]);
2539 free(data
->source_info
);
2541 isl_space_free(data
->dim
);
2546 isl_access_info_free(data
->accesses
);
2547 sched_info_free(data
->sink_info
);
2548 if (data
->source_info
) {
2549 for (i
= 0; i
< data
->count
; ++i
)
2550 sched_info_free(data
->source_info
[i
]);
2551 free(data
->source_info
);
2553 isl_space_free(data
->dim
);
2556 return isl_stat_error
;
2559 /* Add the kills of "info" to the must-sources.
2561 static __isl_give isl_union_access_info
*
2562 isl_union_access_info_add_kill_to_must_source(
2563 __isl_take isl_union_access_info
*info
)
2565 isl_union_map
*must
, *kill
;
2567 must
= isl_union_access_info_get_must_source(info
);
2568 kill
= isl_union_access_info_get_kill(info
);
2569 must
= isl_union_map_union(must
, kill
);
2570 return isl_union_access_info_set_must_source(info
, must
);
2573 /* Drop dependences from "flow" that purely originate from kills.
2574 * That is, only keep those dependences that originate from
2575 * the original must-sources "must" and/or the original may-sources "may".
2576 * In particular, "must" contains the must-sources from before
2577 * the kills were added and "may" contains the may-source from before
2578 * the kills were removed.
2580 * The dependences are of the form
2582 * Source -> [Sink -> Data]
2584 * Only those dependences are kept where the Source -> Data part
2585 * is a subset of the original may-sources or must-sources.
2586 * Of those, only the must-dependences that intersect with the must-sources
2587 * remain must-dependences.
2588 * If there is some overlap between the may-sources and the must-sources,
2589 * then the may-dependences and must-dependences may also overlap.
2590 * This should be fine since the may-dependences are only kept
2591 * disjoint from the must-dependences for the isl_union_map_compute_flow
2592 * interface. This interface does not support kills, so it will
2593 * not end up calling this function.
2595 static __isl_give isl_union_flow
*isl_union_flow_drop_kill_source(
2596 __isl_take isl_union_flow
*flow
, __isl_take isl_union_map
*must
,
2597 __isl_take isl_union_map
*may
)
2599 isl_union_map
*move
;
2603 move
= isl_union_map_copy(flow
->must_dep
);
2604 move
= isl_union_map_intersect_range_factor_range(move
,
2605 isl_union_map_copy(may
));
2606 may
= isl_union_map_union(may
, isl_union_map_copy(must
));
2607 flow
->may_dep
= isl_union_map_intersect_range_factor_range(
2608 flow
->may_dep
, may
);
2609 flow
->must_dep
= isl_union_map_intersect_range_factor_range(
2610 flow
->must_dep
, must
);
2611 flow
->may_dep
= isl_union_map_union(flow
->may_dep
, move
);
2612 if (!flow
->must_dep
|| !flow
->may_dep
)
2613 return isl_union_flow_free(flow
);
2617 isl_union_map_free(must
);
2618 isl_union_map_free(may
);
2622 /* Remove the must accesses from the may accesses.
2624 * A must access always trumps a may access, so there is no need
2625 * for a must access to also be considered as a may access. Doing so
2626 * would only cost extra computations only to find out that
2627 * the duplicated may access does not make any difference.
2629 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2630 __isl_take isl_union_access_info
*access
)
2634 access
->access
[isl_access_may_source
] =
2635 isl_union_map_subtract(access
->access
[isl_access_may_source
],
2636 isl_union_map_copy(access
->access
[isl_access_must_source
]));
2637 if (!access
->access
[isl_access_may_source
])
2638 return isl_union_access_info_free(access
);
2643 /* Given a description of the "sink" accesses, the "source" accesses and
2644 * a schedule, compute for each instance of a sink access
2645 * and for each element accessed by that instance,
2646 * the possible or definite source accesses that last accessed the
2647 * element accessed by the sink access before this sink access
2648 * in the sense that there is no intermediate definite source access.
2650 * The must_no_source and may_no_source elements of the result
2651 * are subsets of access->sink. The elements must_dep and may_dep
2652 * map domain elements of access->{may,must)_source to
2653 * domain elements of access->sink.
2655 * This function is used when only the schedule map representation
2658 * We first prepend the schedule dimensions to the domain
2659 * of the accesses so that we can easily compare their relative order.
2660 * Then we consider each sink access individually in compute_flow.
2662 static __isl_give isl_union_flow
*compute_flow_union_map(
2663 __isl_take isl_union_access_info
*access
)
2665 struct isl_compute_flow_data data
;
2666 isl_union_map
*sink
;
2668 access
= isl_union_access_info_align_params(access
);
2669 access
= isl_union_access_info_introduce_schedule(access
);
2673 data
.must_source
= access
->access
[isl_access_must_source
];
2674 data
.may_source
= access
->access
[isl_access_may_source
];
2676 sink
= access
->access
[isl_access_sink
];
2677 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(sink
));
2679 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
2682 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2684 isl_union_access_info_free(access
);
2687 isl_union_access_info_free(access
);
2688 isl_union_flow_free(data
.flow
);
2692 /* A schedule access relation.
2694 * The access relation "access" is of the form [S -> D] -> A,
2695 * where S corresponds to the prefix schedule at "node".
2696 * "must" is only relevant for source accesses and indicates
2697 * whether the access is a must source or a may source.
2699 struct isl_scheduled_access
{
2702 isl_schedule_node
*node
;
2705 /* Data structure for keeping track of individual scheduled sink and source
2706 * accesses when computing dependence analysis based on a schedule tree.
2708 * "n_sink" is the number of used entries in "sink"
2709 * "n_source" is the number of used entries in "source"
2711 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2712 * to keep track of the current node and
2713 * of what extract_sink_source needs to do.
2715 struct isl_compute_flow_schedule_data
{
2716 isl_union_access_info
*access
;
2721 struct isl_scheduled_access
*sink
;
2722 struct isl_scheduled_access
*source
;
2726 isl_schedule_node
*node
;
2729 /* Align the parameters of all sinks with all sources.
2731 * If there are no sinks or no sources, then no alignment is needed.
2733 static void isl_compute_flow_schedule_data_align_params(
2734 struct isl_compute_flow_schedule_data
*data
)
2739 if (data
->n_sink
== 0 || data
->n_source
== 0)
2742 space
= isl_map_get_space(data
->sink
[0].access
);
2744 for (i
= 1; i
< data
->n_sink
; ++i
)
2745 space
= isl_space_align_params(space
,
2746 isl_map_get_space(data
->sink
[i
].access
));
2747 for (i
= 0; i
< data
->n_source
; ++i
)
2748 space
= isl_space_align_params(space
,
2749 isl_map_get_space(data
->source
[i
].access
));
2751 for (i
= 0; i
< data
->n_sink
; ++i
)
2752 data
->sink
[i
].access
=
2753 isl_map_align_params(data
->sink
[i
].access
,
2754 isl_space_copy(space
));
2755 for (i
= 0; i
< data
->n_source
; ++i
)
2756 data
->source
[i
].access
=
2757 isl_map_align_params(data
->source
[i
].access
,
2758 isl_space_copy(space
));
2760 isl_space_free(space
);
2763 /* Free all the memory referenced from "data".
2764 * Do not free "data" itself as it may be allocated on the stack.
2766 static void isl_compute_flow_schedule_data_clear(
2767 struct isl_compute_flow_schedule_data
*data
)
2774 for (i
= 0; i
< data
->n_sink
; ++i
) {
2775 isl_map_free(data
->sink
[i
].access
);
2776 isl_schedule_node_free(data
->sink
[i
].node
);
2779 for (i
= 0; i
< data
->n_source
; ++i
) {
2780 isl_map_free(data
->source
[i
].access
);
2781 isl_schedule_node_free(data
->source
[i
].node
);
2787 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2788 * (an upper bound on) the number of sinks and sources.
2790 * Sinks and sources are only extracted at leaves of the tree,
2791 * so we skip the node if it is not a leaf.
2792 * Otherwise we increment data->n_sink and data->n_source with
2793 * the number of spaces in the sink and source access domains
2794 * that reach this node.
2796 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2799 struct isl_compute_flow_schedule_data
*data
= user
;
2800 isl_union_set
*domain
;
2801 isl_union_map
*umap
;
2802 isl_bool r
= isl_bool_false
;
2804 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2805 return isl_bool_true
;
2807 domain
= isl_schedule_node_get_universe_domain(node
);
2809 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2810 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2811 data
->n_sink
+= isl_union_map_n_map(umap
);
2812 isl_union_map_free(umap
);
2816 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2817 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2818 data
->n_source
+= isl_union_map_n_map(umap
);
2819 isl_union_map_free(umap
);
2823 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2824 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2825 data
->n_source
+= isl_union_map_n_map(umap
);
2826 isl_union_map_free(umap
);
2830 isl_union_set_free(domain
);
2835 /* Add a single scheduled sink or source (depending on data->set_sink)
2836 * with scheduled access relation "map", must property data->must and
2837 * schedule node data->node to the list of sinks or sources.
2839 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2841 struct isl_compute_flow_schedule_data
*data
= user
;
2842 struct isl_scheduled_access
*access
;
2845 access
= data
->sink
+ data
->n_sink
++;
2847 access
= data
->source
+ data
->n_source
++;
2849 access
->access
= map
;
2850 access
->must
= data
->must
;
2851 access
->node
= isl_schedule_node_copy(data
->node
);
2856 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2857 * individual scheduled source and sink accesses (taking into account
2858 * the domain of the schedule).
2860 * We only collect accesses at the leaves of the schedule tree.
2861 * We prepend the schedule dimensions at the leaf to the iteration
2862 * domains of the source and sink accesses and then extract
2863 * the individual accesses (per space).
2865 * In particular, if the prefix schedule at the node is of the form
2869 * while the access relations are of the form
2873 * then the updated access relations are of the form
2877 * Note that S consists of a single space such that introducing S
2878 * in the access relations does not increase the number of spaces.
2880 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2883 struct isl_compute_flow_schedule_data
*data
= user
;
2884 isl_union_map
*prefix
;
2885 isl_union_map
*umap
;
2886 isl_bool r
= isl_bool_false
;
2888 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2889 return isl_bool_true
;
2893 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2894 prefix
= isl_union_map_reverse(prefix
);
2895 prefix
= isl_union_map_range_map(prefix
);
2898 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2899 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2900 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2902 isl_union_map_free(umap
);
2906 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2907 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2908 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2910 isl_union_map_free(umap
);
2914 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2915 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2916 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2918 isl_union_map_free(umap
);
2920 isl_union_map_free(prefix
);
2925 /* isl_access_info_compute_flow callback for determining whether
2926 * the shared nesting level and the ordering within that level
2927 * for two scheduled accesses for use in compute_single_flow.
2929 * The tokens passed to this function refer to the leaves
2930 * in the schedule tree where the accesses take place.
2932 * If n is the shared number of loops, then we need to return
2933 * "2 * n + 1" if "first" precedes "second" inside the innermost
2934 * shared loop and "2 * n" otherwise.
2936 * The innermost shared ancestor may be the leaves themselves
2937 * if the accesses take place in the same leaf. Otherwise,
2938 * it is either a set node or a sequence node. Only in the case
2939 * of a sequence node do we consider one access to precede the other.
2941 static int before_node(void *first
, void *second
)
2943 isl_schedule_node
*node1
= first
;
2944 isl_schedule_node
*node2
= second
;
2945 isl_schedule_node
*shared
;
2949 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2953 depth
= isl_schedule_node_get_schedule_depth(shared
);
2954 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2957 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2959 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2961 before
= pos1
< pos2
;
2964 isl_schedule_node_free(shared
);
2966 return 2 * depth
+ before
;
2969 /* Check if the given two accesses may be coscheduled.
2970 * If so, return 1. Otherwise return 0.
2972 * Two accesses may only be coscheduled if they appear in the same leaf.
2974 static int coscheduled_node(void *first
, void *second
)
2976 isl_schedule_node
*node1
= first
;
2977 isl_schedule_node
*node2
= second
;
2979 return node1
== node2
;
2982 /* Add the scheduled sources from "data" that access
2983 * the same data space as "sink" to "access".
2985 static __isl_give isl_access_info
*add_matching_sources(
2986 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2987 struct isl_compute_flow_schedule_data
*data
)
2992 space
= isl_space_range(isl_map_get_space(sink
->access
));
2993 for (i
= 0; i
< data
->n_source
; ++i
) {
2994 struct isl_scheduled_access
*source
;
2995 isl_space
*source_space
;
2998 source
= &data
->source
[i
];
2999 source_space
= isl_map_get_space(source
->access
);
3000 source_space
= isl_space_range(source_space
);
3001 eq
= isl_space_is_equal(space
, source_space
);
3002 isl_space_free(source_space
);
3009 access
= isl_access_info_add_source(access
,
3010 isl_map_copy(source
->access
), source
->must
, source
->node
);
3013 isl_space_free(space
);
3016 isl_space_free(space
);
3017 isl_access_info_free(access
);
3021 /* Given a scheduled sink access relation "sink", compute the corresponding
3022 * dependences on the sources in "data" and add the computed dependences
3025 * The dependences computed by access_info_compute_flow_core are of the form
3027 * [S -> I] -> [[S' -> I'] -> A]
3029 * The schedule dimensions are projected out by first currying the range,
3032 * [S -> I] -> [S' -> [I' -> A]]
3034 * and then computing the factor range
3038 static __isl_give isl_union_flow
*compute_single_flow(
3039 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
3040 struct isl_compute_flow_schedule_data
*data
)
3043 isl_access_info
*access
;
3050 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
3051 &before_node
, data
->n_source
);
3053 access
->coscheduled
= &coscheduled_node
;
3054 access
= add_matching_sources(access
, sink
, data
);
3056 flow
= access_info_compute_flow_core(access
);
3058 return isl_union_flow_free(uf
);
3060 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
3061 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
3062 isl_union_map_from_map(map
));
3063 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
3064 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
3065 isl_union_map_from_map(map
));
3067 for (i
= 0; i
< flow
->n_source
; ++i
) {
3070 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
3071 map
= isl_map_factor_range(map
);
3072 dep
= isl_union_map_from_map(map
);
3073 if (flow
->dep
[i
].must
)
3074 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
3076 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
3079 isl_flow_free(flow
);
3084 /* Given a description of the "sink" accesses, the "source" accesses and
3085 * a schedule, compute for each instance of a sink access
3086 * and for each element accessed by that instance,
3087 * the possible or definite source accesses that last accessed the
3088 * element accessed by the sink access before this sink access
3089 * in the sense that there is no intermediate definite source access.
3090 * Only consider dependences between statement instances that belong
3091 * to the domain of the schedule.
3093 * The must_no_source and may_no_source elements of the result
3094 * are subsets of access->sink. The elements must_dep and may_dep
3095 * map domain elements of access->{may,must)_source to
3096 * domain elements of access->sink.
3098 * This function is used when a schedule tree representation
3101 * We extract the individual scheduled source and sink access relations
3102 * (taking into account the domain of the schedule) and
3103 * then compute dependences for each scheduled sink individually.
3105 static __isl_give isl_union_flow
*compute_flow_schedule(
3106 __isl_take isl_union_access_info
*access
)
3108 struct isl_compute_flow_schedule_data data
= { access
};
3112 isl_union_flow
*flow
;
3114 ctx
= isl_union_access_info_get_ctx(access
);
3118 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3119 &count_sink_source
, &data
) < 0)
3122 n
= data
.n_sink
+ data
.n_source
;
3123 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
3124 if (n
&& !data
.sink
)
3126 data
.source
= data
.sink
+ data
.n_sink
;
3130 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3131 &collect_sink_source
, &data
) < 0)
3134 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
3135 flow
= isl_union_flow_alloc(space
);
3137 isl_compute_flow_schedule_data_align_params(&data
);
3139 for (i
= 0; i
< data
.n_sink
; ++i
)
3140 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
3142 isl_compute_flow_schedule_data_clear(&data
);
3144 isl_union_access_info_free(access
);
3147 isl_union_access_info_free(access
);
3148 isl_compute_flow_schedule_data_clear(&data
);
3152 /* Given a description of the "sink" accesses, the "source" accesses and
3153 * a schedule, compute for each instance of a sink access
3154 * and for each element accessed by that instance,
3155 * the possible or definite source accesses that last accessed the
3156 * element accessed by the sink access before this sink access
3157 * in the sense that there is no intermediate definite source access.
3159 * The must_no_source and may_no_source elements of the result
3160 * are subsets of access->sink. The elements must_dep and may_dep
3161 * map domain elements of access->{may,must)_source to
3162 * domain elements of access->sink.
3164 * If any kills have been specified, then they are treated as
3165 * must-sources internally. Any dependence that purely derives
3166 * from an original kill is removed from the output.
3168 * We check whether the schedule is available as a schedule tree
3169 * or a schedule map and call the corresponding function to perform
3172 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
3173 __isl_take isl_union_access_info
*access
)
3176 isl_union_map
*must
= NULL
, *may
= NULL
;
3177 isl_union_flow
*flow
;
3179 has_kill
= isl_union_access_has_kill(access
);
3183 must
= isl_union_access_info_get_must_source(access
);
3184 may
= isl_union_access_info_get_may_source(access
);
3186 access
= isl_union_access_info_add_kill_to_must_source(access
);
3187 access
= isl_union_access_info_normalize(access
);
3190 if (access
->schedule
)
3191 flow
= compute_flow_schedule(access
);
3193 flow
= compute_flow_union_map(access
);
3195 flow
= isl_union_flow_drop_kill_source(flow
, must
, may
);
3198 isl_union_access_info_free(access
);
3199 isl_union_map_free(must
);
3200 isl_union_map_free(may
);
3204 /* Print the information contained in "flow" to "p".
3205 * The information is printed as a YAML document.
3207 __isl_give isl_printer
*isl_printer_print_union_flow(
3208 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
3210 isl_union_map
*umap
;
3213 return isl_printer_free(p
);
3215 p
= isl_printer_yaml_start_mapping(p
);
3216 umap
= isl_union_flow_get_full_must_dependence(flow
);
3217 p
= print_union_map_field(p
, "must_dependence", umap
);
3218 isl_union_map_free(umap
);
3219 umap
= isl_union_flow_get_full_may_dependence(flow
);
3220 p
= print_union_map_field(p
, "may_dependence", umap
);
3221 isl_union_map_free(umap
);
3222 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
3223 umap
= isl_union_flow_get_may_no_source(flow
);
3224 p
= print_union_map_field(p
, "may_no_source", umap
);
3225 isl_union_map_free(umap
);
3226 p
= isl_printer_yaml_end_mapping(p
);
3231 /* Return a string representation of the information in "flow".
3232 * The information is printed in flow format.
3234 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
3242 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
3243 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
3244 p
= isl_printer_print_union_flow(p
, flow
);
3245 s
= isl_printer_get_str(p
);
3246 isl_printer_free(p
);
3251 /* Given a collection of "sink" and "source" accesses,
3252 * compute for each iteration of a sink access
3253 * and for each element accessed by that iteration,
3254 * the source access in the list that last accessed the
3255 * element accessed by the sink access before this sink access.
3256 * Each access is given as a map from the loop iterators
3257 * to the array indices.
3258 * The result is a relations between source and sink
3259 * iterations and a subset of the domain of the sink accesses,
3260 * corresponding to those iterations that access an element
3261 * not previously accessed.
3263 * We collect the inputs in an isl_union_access_info object,
3264 * call isl_union_access_info_compute_flow and extract
3265 * the outputs from the result.
3267 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
3268 __isl_take isl_union_map
*must_source
,
3269 __isl_take isl_union_map
*may_source
,
3270 __isl_take isl_union_map
*schedule
,
3271 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
3272 __isl_give isl_union_map
**must_no_source
,
3273 __isl_give isl_union_map
**may_no_source
)
3275 isl_union_access_info
*access
;
3276 isl_union_flow
*flow
;
3278 access
= isl_union_access_info_from_sink(sink
);
3279 access
= isl_union_access_info_set_must_source(access
, must_source
);
3280 access
= isl_union_access_info_set_may_source(access
, may_source
);
3281 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
3282 flow
= isl_union_access_info_compute_flow(access
);
3285 *must_dep
= isl_union_flow_get_must_dependence(flow
);
3287 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
3289 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
3291 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
3293 isl_union_flow_free(flow
);
3295 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
3296 (must_no_source
&& !*must_no_source
) ||
3297 (may_no_source
&& !*may_no_source
))
3303 *must_dep
= isl_union_map_free(*must_dep
);
3305 *may_dep
= isl_union_map_free(*may_dep
);
3307 *must_no_source
= isl_union_map_free(*must_no_source
);
3309 *may_no_source
= isl_union_map_free(*may_no_source
);