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 void 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
);
538 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
540 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
543 /* Return a map that enforces that the domain iteration occurs after
544 * the range iteration at the given level.
545 * If level is odd, then the domain iteration should occur after
546 * the target iteration in their shared level/2 outermost loops.
547 * In this case we simply need to enforce that these outermost
548 * loop iterations are the same.
549 * If level is even, then the loop iterator of the domain should
550 * be greater than the loop iterator of the range at the last
551 * of the level/2 shared loops, i.e., loop level/2 - 1.
553 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
555 struct isl_basic_map
*bmap
;
558 bmap
= isl_basic_map_equal(dim
, level
/2);
560 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
562 return isl_map_from_basic_map(bmap
);
565 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
566 * but first check if the user has set acc->restrict_fn and if so
567 * update either the input or the output of the maximization problem
568 * with respect to the resulting restriction.
570 * Since the user expects a mapping from sink iterations to source iterations,
571 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
572 * to accessed array elements, we first need to project out the accessed
573 * sink array elements by applying acc->domain_map.
574 * Similarly, the sink restriction specified by the user needs to be
575 * converted back to the wrapped map.
577 static __isl_give isl_map
*restricted_partial_lexmax(
578 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
579 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
582 isl_restriction
*restr
;
583 isl_set
*sink_domain
;
587 if (!acc
->restrict_fn
)
588 return isl_map_partial_lexmax(dep
, sink
, empty
);
590 source_map
= isl_map_copy(dep
);
591 source_map
= isl_map_apply_domain(source_map
,
592 isl_map_copy(acc
->domain_map
));
593 sink_domain
= isl_set_copy(sink
);
594 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
595 restr
= acc
->restrict_fn(source_map
, sink_domain
,
596 acc
->source
[source
].data
, acc
->restrict_user
);
597 isl_set_free(sink_domain
);
598 isl_map_free(source_map
);
602 if (restr
->type
== isl_restriction_type_input
) {
603 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
604 sink_restr
= isl_set_copy(restr
->sink
);
605 sink_restr
= isl_set_apply(sink_restr
,
606 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
607 sink
= isl_set_intersect(sink
, sink_restr
);
608 } else if (restr
->type
== isl_restriction_type_empty
) {
609 isl_space
*space
= isl_map_get_space(dep
);
611 dep
= isl_map_empty(space
);
614 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
616 if (restr
->type
== isl_restriction_type_output
)
617 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
619 isl_restriction_free(restr
);
628 /* Compute the last iteration of must source j that precedes the sink
629 * at the given level for sink iterations in set_C.
630 * The subset of set_C for which no such iteration can be found is returned
633 static struct isl_map
*last_source(struct isl_access_info
*acc
,
634 struct isl_set
*set_C
,
635 int j
, int level
, struct isl_set
**empty
)
637 struct isl_map
*read_map
;
638 struct isl_map
*write_map
;
639 struct isl_map
*dep_map
;
640 struct isl_map
*after
;
641 struct isl_map
*result
;
643 read_map
= isl_map_copy(acc
->sink
.map
);
644 write_map
= isl_map_copy(acc
->source
[j
].map
);
645 write_map
= isl_map_reverse(write_map
);
646 dep_map
= isl_map_apply_range(read_map
, write_map
);
647 after
= after_at_level(isl_map_get_space(dep_map
), level
);
648 dep_map
= isl_map_intersect(dep_map
, after
);
649 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
650 result
= isl_map_reverse(result
);
655 /* For a given mapping between iterations of must source j and iterations
656 * of the sink, compute the last iteration of must source k preceding
657 * the sink at level before_level for any of the sink iterations,
658 * but following the corresponding iteration of must source j at level
661 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
662 struct isl_map
*old_map
,
663 int j
, int before_level
,
664 int k
, int after_level
,
665 struct isl_set
**empty
)
668 struct isl_set
*set_C
;
669 struct isl_map
*read_map
;
670 struct isl_map
*write_map
;
671 struct isl_map
*dep_map
;
672 struct isl_map
*after_write
;
673 struct isl_map
*before_read
;
674 struct isl_map
*result
;
676 set_C
= isl_map_range(isl_map_copy(old_map
));
677 read_map
= isl_map_copy(acc
->sink
.map
);
678 write_map
= isl_map_copy(acc
->source
[k
].map
);
680 write_map
= isl_map_reverse(write_map
);
681 dep_map
= isl_map_apply_range(read_map
, write_map
);
682 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
683 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
684 after_write
= after_at_level(dim
, after_level
);
685 after_write
= isl_map_apply_range(after_write
, old_map
);
686 after_write
= isl_map_reverse(after_write
);
687 dep_map
= isl_map_intersect(dep_map
, after_write
);
688 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
689 dep_map
= isl_map_intersect(dep_map
, before_read
);
690 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
691 result
= isl_map_reverse(result
);
696 /* Given a shared_level between two accesses, return 1 if the
697 * the first can precede the second at the requested target_level.
698 * If the target level is odd, i.e., refers to a statement level
699 * dimension, then first needs to precede second at the requested
700 * level, i.e., shared_level must be equal to target_level.
701 * If the target level is odd, then the two loops should share
702 * at least the requested number of outer loops.
704 static int can_precede_at_level(int shared_level
, int target_level
)
706 if (shared_level
< target_level
)
708 if ((target_level
% 2) && shared_level
> target_level
)
713 /* Given a possible flow dependence temp_rel[j] between source j and the sink
714 * at level sink_level, remove those elements for which
715 * there is an iteration of another source k < j that is closer to the sink.
716 * The flow dependences temp_rel[k] are updated with the improved sources.
717 * Any improved source needs to precede the sink at the same level
718 * and needs to follow source j at the same or a deeper level.
719 * The lower this level, the later the execution date of source k.
720 * We therefore consider lower levels first.
722 * If temp_rel[j] is empty, then there can be no improvement and
723 * we return immediately.
725 * This function returns isl_stat_ok in case it was executed successfully and
726 * isl_stat_error in case of errors during the execution of this function.
728 static isl_stat
intermediate_sources(__isl_keep isl_access_info
*acc
,
729 struct isl_map
**temp_rel
, int j
, int sink_level
)
732 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
734 if (isl_map_plain_is_empty(temp_rel
[j
]))
737 for (k
= j
- 1; k
>= 0; --k
) {
739 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
741 return isl_stat_error
;
742 if (!can_precede_at_level(plevel
, sink_level
))
745 plevel2
= acc
->level_before(acc
->source
[j
].data
,
746 acc
->source
[k
].data
);
748 return isl_stat_error
;
750 for (level
= sink_level
; level
<= depth
; ++level
) {
752 struct isl_set
*trest
;
753 struct isl_map
*copy
;
755 if (!can_precede_at_level(plevel2
, level
))
758 copy
= isl_map_copy(temp_rel
[j
]);
759 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
761 if (isl_map_plain_is_empty(T
)) {
766 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
767 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
774 /* Compute all iterations of may source j that precedes the sink at the given
775 * level for sink iterations in set_C.
777 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
778 __isl_take isl_set
*set_C
, int j
, int level
)
785 read_map
= isl_map_copy(acc
->sink
.map
);
786 read_map
= isl_map_intersect_domain(read_map
, set_C
);
787 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
788 write_map
= isl_map_reverse(write_map
);
789 dep_map
= isl_map_apply_range(read_map
, write_map
);
790 after
= after_at_level(isl_map_get_space(dep_map
), level
);
791 dep_map
= isl_map_intersect(dep_map
, after
);
793 return isl_map_reverse(dep_map
);
796 /* For a given mapping between iterations of must source k and iterations
797 * of the sink, compute all iterations of may source j preceding
798 * the sink at level before_level for any of the sink iterations,
799 * but following the corresponding iteration of must source k at level
802 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
803 __isl_take isl_map
*old_map
,
804 int j
, int before_level
, int k
, int after_level
)
811 isl_map
*after_write
;
812 isl_map
*before_read
;
814 set_C
= isl_map_range(isl_map_copy(old_map
));
815 read_map
= isl_map_copy(acc
->sink
.map
);
816 read_map
= isl_map_intersect_domain(read_map
, set_C
);
817 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
819 write_map
= isl_map_reverse(write_map
);
820 dep_map
= isl_map_apply_range(read_map
, write_map
);
821 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
822 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
823 after_write
= after_at_level(dim
, after_level
);
824 after_write
= isl_map_apply_range(after_write
, old_map
);
825 after_write
= isl_map_reverse(after_write
);
826 dep_map
= isl_map_intersect(dep_map
, after_write
);
827 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
828 dep_map
= isl_map_intersect(dep_map
, before_read
);
829 return isl_map_reverse(dep_map
);
832 /* Given the must and may dependence relations for the must accesses
833 * for level sink_level, check if there are any accesses of may access j
834 * that occur in between and return their union.
835 * If some of these accesses are intermediate with respect to
836 * (previously thought to be) must dependences, then these
837 * must dependences are turned into may dependences.
839 static __isl_give isl_map
*all_intermediate_sources(
840 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
841 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
842 int j
, int sink_level
)
845 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
848 for (k
= 0; k
< acc
->n_must
; ++k
) {
851 if (isl_map_plain_is_empty(may_rel
[k
]) &&
852 isl_map_plain_is_empty(must_rel
[k
]))
855 plevel
= acc
->level_before(acc
->source
[k
].data
,
856 acc
->source
[acc
->n_must
+ j
].data
);
858 return isl_map_free(map
);
860 for (level
= sink_level
; level
<= depth
; ++level
) {
865 if (!can_precede_at_level(plevel
, level
))
868 copy
= isl_map_copy(may_rel
[k
]);
869 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
870 map
= isl_map_union(map
, T
);
872 copy
= isl_map_copy(must_rel
[k
]);
873 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
874 ran
= isl_map_range(isl_map_copy(T
));
875 map
= isl_map_union(map
, T
);
876 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
877 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
879 T
= isl_map_from_domain_and_range(
881 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
883 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
890 /* Given a dependence relation "old_map" between a must-source and the sink,
891 * return a subset of the dependences, augmented with instances
892 * of the source at position "pos" in "acc" that are coscheduled
893 * with the must-source and that access the same element.
894 * That is, if the input lives in a space T -> K, then the output
895 * lives in the space [T -> S] -> K, with S the space of source "pos", and
896 * the domain factor of the domain product is a subset of the input.
897 * The sources are considered to be coscheduled if they have the same values
898 * for the initial "depth" coordinates.
900 * First construct a dependence relation S -> K and a mapping
901 * between coscheduled sources T -> S.
902 * The second is combined with the original dependence relation T -> K
903 * to form a relation in T -> [S -> K], which is subsequently
904 * uncurried to [T -> S] -> K.
905 * This result is then intersected with the dependence relation S -> K
906 * to form the output.
908 * In case a negative depth is given, NULL is returned to indicate an error.
910 static __isl_give isl_map
*coscheduled_source(__isl_keep isl_access_info
*acc
,
911 __isl_keep isl_map
*old_map
, int pos
, int depth
)
924 set_C
= isl_map_range(isl_map_copy(old_map
));
925 read_map
= isl_map_copy(acc
->sink
.map
);
926 read_map
= isl_map_intersect_domain(read_map
, set_C
);
927 write_map
= isl_map_copy(acc
->source
[pos
].map
);
928 dep_map
= isl_map_domain_product(write_map
, read_map
);
929 dep_map
= isl_set_unwrap(isl_map_domain(dep_map
));
930 space
= isl_space_join(isl_map_get_space(old_map
),
931 isl_space_reverse(isl_map_get_space(dep_map
)));
932 equal
= isl_map_from_basic_map(isl_basic_map_equal(space
, depth
));
933 map
= isl_map_range_product(equal
, isl_map_copy(old_map
));
934 map
= isl_map_uncurry(map
);
935 map
= isl_map_intersect_domain_factor_range(map
, dep_map
);
940 /* After the dependences derived from a must-source have been computed
941 * at a certain level, check if any of the sources of the must-dependences
942 * may be coscheduled with other sources.
943 * If they are any such sources, then there is no way of determining
944 * which of the sources actually comes last and the must-dependences
945 * need to be turned into may-dependences, while dependences from
946 * the other sources need to be added to the may-dependences as well.
947 * "acc" describes the sources and a callback for checking whether
948 * two sources may be coscheduled. If acc->coscheduled is NULL then
949 * the sources are assumed not to be coscheduled.
950 * "must_rel" and "may_rel" describe the must and may-dependence relations
951 * computed at the current level for the must-sources. Some of the dependences
952 * may be moved from "must_rel" to "may_rel".
953 * "flow" contains all dependences computed so far (apart from those
954 * in "must_rel" and "may_rel") and may be updated with additional
955 * dependences derived from may-sources.
957 * In particular, consider all the must-sources with a non-empty
958 * dependence relation in "must_rel". They are considered in reverse
959 * order because that is the order in which they are considered in the caller.
960 * If any of the must-sources are coscheduled, then the last one
961 * is the one that will have a corresponding dependence relation.
962 * For each must-source i, consider both all the previous must-sources
963 * and all the may-sources. If any of those may be coscheduled with
964 * must-source i, then compute the coscheduled instances that access
965 * the same memory elements. The result is a relation [T -> S] -> K.
966 * The projection onto T -> K is a subset of the must-dependence relation
967 * that needs to be turned into may-dependences.
968 * The projection onto S -> K needs to be added to the may-dependences
970 * Since a given must-source instance may be coscheduled with several
971 * other source instances, the dependences that need to be turned
972 * into may-dependences are first collected and only actually removed
973 * from the must-dependences after all other sources have been considered.
975 static __isl_give isl_flow
*handle_coscheduled(__isl_keep isl_access_info
*acc
,
976 __isl_keep isl_map
**must_rel
, __isl_keep isl_map
**may_rel
,
977 __isl_take isl_flow
*flow
)
981 if (!acc
->coscheduled
)
983 for (i
= acc
->n_must
- 1; i
>= 0; --i
) {
986 if (isl_map_plain_is_empty(must_rel
[i
]))
988 move
= isl_map_empty(isl_map_get_space(must_rel
[i
]));
989 for (j
= i
- 1; j
>= 0; --j
) {
991 isl_map
*map
, *factor
;
993 if (!acc
->coscheduled(acc
->source
[i
].data
,
994 acc
->source
[j
].data
))
996 depth
= acc
->level_before(acc
->source
[i
].data
,
997 acc
->source
[j
].data
) / 2;
998 map
= coscheduled_source(acc
, must_rel
[i
], j
, depth
);
999 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
1000 may_rel
[j
] = isl_map_union(may_rel
[j
], factor
);
1001 map
= isl_map_domain_factor_domain(map
);
1002 move
= isl_map_union(move
, map
);
1004 for (j
= 0; j
< acc
->n_may
; ++j
) {
1006 isl_map
*map
, *factor
;
1008 pos
= acc
->n_must
+ j
;
1009 if (!acc
->coscheduled(acc
->source
[i
].data
,
1010 acc
->source
[pos
].data
))
1012 depth
= acc
->level_before(acc
->source
[i
].data
,
1013 acc
->source
[pos
].data
) / 2;
1014 map
= coscheduled_source(acc
, must_rel
[i
], pos
, depth
);
1015 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
1016 pos
= 2 * acc
->n_must
+ j
;
1017 flow
->dep
[pos
].map
= isl_map_union(flow
->dep
[pos
].map
,
1019 map
= isl_map_domain_factor_domain(map
);
1020 move
= isl_map_union(move
, map
);
1022 must_rel
[i
] = isl_map_subtract(must_rel
[i
], isl_map_copy(move
));
1023 may_rel
[i
] = isl_map_union(may_rel
[i
], move
);
1029 /* Compute dependences for the case where all accesses are "may"
1030 * accesses, which boils down to computing memory based dependences.
1031 * The generic algorithm would also work in this case, but it would
1032 * be overkill to use it.
1034 static __isl_give isl_flow
*compute_mem_based_dependences(
1035 __isl_keep isl_access_info
*acc
)
1042 res
= isl_flow_alloc(acc
);
1046 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1047 maydo
= isl_set_copy(mustdo
);
1049 for (i
= 0; i
< acc
->n_may
; ++i
) {
1056 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
1060 is_before
= plevel
& 1;
1063 dim
= isl_map_get_space(res
->dep
[i
].map
);
1065 before
= isl_map_lex_le_first(dim
, plevel
);
1067 before
= isl_map_lex_lt_first(dim
, plevel
);
1068 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
1069 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
1070 dep
= isl_map_intersect(dep
, before
);
1071 mustdo
= isl_set_subtract(mustdo
,
1072 isl_map_range(isl_map_copy(dep
)));
1073 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
1076 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
1077 res
->must_no_source
= mustdo
;
1081 isl_set_free(mustdo
);
1082 isl_set_free(maydo
);
1087 /* Compute dependences for the case where there is at least one
1090 * The core algorithm considers all levels in which a source may precede
1091 * the sink, where a level may either be a statement level or a loop level.
1092 * The outermost statement level is 1, the first loop level is 2, etc...
1093 * The algorithm basically does the following:
1094 * for all levels l of the read access from innermost to outermost
1095 * for all sources w that may precede the sink access at that level
1096 * compute the last iteration of the source that precedes the sink access
1098 * add result to possible last accesses at level l of source w
1099 * for all sources w2 that we haven't considered yet at this level that may
1100 * also precede the sink access
1101 * for all levels l2 of w from l to innermost
1102 * for all possible last accesses dep of w at l
1103 * compute last iteration of w2 between the source and sink
1105 * add result to possible last accesses at level l of write w2
1106 * and replace possible last accesses dep by the remainder
1109 * The above algorithm is applied to the must access. During the course
1110 * of the algorithm, we keep track of sink iterations that still
1111 * need to be considered. These iterations are split into those that
1112 * haven't been matched to any source access (mustdo) and those that have only
1113 * been matched to may accesses (maydo).
1114 * At the end of each level, must-sources and may-sources that are coscheduled
1115 * with the sources of the must-dependences at that level are considered.
1116 * If any coscheduled instances are found, then corresponding may-dependences
1117 * are added and the original must-dependences are turned into may-dependences.
1118 * Afterwards, the may accesses that occur after must-dependence sources
1120 * In particular, we consider may accesses that precede the remaining
1121 * sink iterations, moving elements from mustdo to maydo when appropriate,
1122 * and may accesses that occur between a must source and a sink of any
1123 * dependences found at the current level, turning must dependences into
1124 * may dependences when appropriate.
1127 static __isl_give isl_flow
*compute_val_based_dependences(
1128 __isl_keep isl_access_info
*acc
)
1132 isl_set
*mustdo
= NULL
;
1133 isl_set
*maydo
= NULL
;
1136 isl_map
**must_rel
= NULL
;
1137 isl_map
**may_rel
= NULL
;
1142 res
= isl_flow_alloc(acc
);
1145 ctx
= isl_map_get_ctx(acc
->sink
.map
);
1147 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
1148 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1149 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
1150 if (!mustdo
|| !maydo
)
1152 if (isl_set_plain_is_empty(mustdo
))
1155 must_rel
= isl_calloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1156 may_rel
= isl_calloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1157 if (!must_rel
|| !may_rel
)
1160 for (level
= depth
; level
>= 1; --level
) {
1161 for (j
= acc
->n_must
-1; j
>=0; --j
) {
1163 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
1164 must_rel
[j
] = isl_map_empty(space
);
1165 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
1168 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1170 struct isl_set
*rest
;
1173 plevel
= acc
->level_before(acc
->source
[j
].data
,
1177 if (!can_precede_at_level(plevel
, level
))
1180 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
1181 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
1184 if (intermediate_sources(acc
, must_rel
, j
, level
) < 0)
1187 T
= last_source(acc
, maydo
, j
, level
, &rest
);
1188 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
1191 if (intermediate_sources(acc
, may_rel
, j
, level
) < 0)
1194 if (isl_set_plain_is_empty(mustdo
) &&
1195 isl_set_plain_is_empty(maydo
))
1198 for (j
= j
- 1; j
>= 0; --j
) {
1201 plevel
= acc
->level_before(acc
->source
[j
].data
,
1205 if (!can_precede_at_level(plevel
, level
))
1208 if (intermediate_sources(acc
, must_rel
, j
, level
) < 0)
1210 if (intermediate_sources(acc
, may_rel
, j
, level
) < 0)
1214 handle_coscheduled(acc
, must_rel
, may_rel
, res
);
1216 for (j
= 0; j
< acc
->n_may
; ++j
) {
1221 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1225 if (!can_precede_at_level(plevel
, level
))
1228 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1229 res
->dep
[2 * acc
->n_must
+ j
].map
=
1230 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1231 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1232 ran
= isl_map_range(isl_map_copy(T
));
1233 res
->dep
[2 * acc
->n_must
+ j
].map
=
1234 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1235 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1236 maydo
= isl_set_union_disjoint(maydo
, ran
);
1238 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1239 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1241 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1244 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1245 res
->dep
[2 * j
].map
=
1246 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1248 res
->dep
[2 * j
+ 1].map
=
1249 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1253 if (isl_set_plain_is_empty(mustdo
) &&
1254 isl_set_plain_is_empty(maydo
))
1261 res
->must_no_source
= mustdo
;
1262 res
->may_no_source
= maydo
;
1266 for (j
= 0; j
< acc
->n_must
; ++j
)
1267 isl_map_free(must_rel
[j
]);
1269 for (j
= 0; j
< acc
->n_must
; ++j
)
1270 isl_map_free(may_rel
[j
]);
1272 isl_set_free(mustdo
);
1273 isl_set_free(maydo
);
1279 /* Given a "sink" access, a list of n "source" accesses,
1280 * compute for each iteration of the sink access
1281 * and for each element accessed by that iteration,
1282 * the source access in the list that last accessed the
1283 * element accessed by the sink access before this sink access.
1284 * Each access is given as a map from the loop iterators
1285 * to the array indices.
1286 * The result is a list of n relations between source and sink
1287 * iterations and a subset of the domain of the sink access,
1288 * corresponding to those iterations that access an element
1289 * not previously accessed.
1291 * To deal with multi-valued sink access relations, the sink iteration
1292 * domain is first extended with dimensions that correspond to the data
1293 * space. However, these extra dimensions are not projected out again.
1294 * It is up to the caller to decide whether these dimensions should be kept.
1296 static __isl_give isl_flow
*access_info_compute_flow_core(
1297 __isl_take isl_access_info
*acc
)
1299 struct isl_flow
*res
= NULL
;
1304 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1308 if (acc
->n_must
== 0)
1309 res
= compute_mem_based_dependences(acc
);
1311 acc
= isl_access_info_sort_sources(acc
);
1312 res
= compute_val_based_dependences(acc
);
1314 acc
= isl_access_info_free(acc
);
1317 if (!res
->must_no_source
|| !res
->may_no_source
)
1321 isl_access_info_free(acc
);
1326 /* Given a "sink" access, a list of n "source" accesses,
1327 * compute for each iteration of the sink access
1328 * and for each element accessed by that iteration,
1329 * the source access in the list that last accessed the
1330 * element accessed by the sink access before this sink access.
1331 * Each access is given as a map from the loop iterators
1332 * to the array indices.
1333 * The result is a list of n relations between source and sink
1334 * iterations and a subset of the domain of the sink access,
1335 * corresponding to those iterations that access an element
1336 * not previously accessed.
1338 * To deal with multi-valued sink access relations,
1339 * access_info_compute_flow_core extends the sink iteration domain
1340 * with dimensions that correspond to the data space. These extra dimensions
1341 * are projected out from the result of access_info_compute_flow_core.
1343 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1346 struct isl_flow
*res
;
1351 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1352 res
= access_info_compute_flow_core(acc
);
1356 for (j
= 0; j
< res
->n_source
; ++j
) {
1357 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1358 if (!res
->dep
[j
].map
)
1369 /* Keep track of some information about a schedule for a given
1370 * access. In particular, keep track of which dimensions
1371 * have a constant value and of the actual constant values.
1373 struct isl_sched_info
{
1378 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1382 isl_vec_free(info
->cst
);
1387 /* Extract information on the constant dimensions of the schedule
1388 * for a given access. The "map" is of the form
1392 * with S the schedule domain, D the iteration domain and A the data domain.
1394 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1395 __isl_keep isl_map
*map
)
1399 struct isl_sched_info
*info
;
1405 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1408 n
= isl_space_dim(dim
, isl_dim_in
);
1409 isl_space_free(dim
);
1411 ctx
= isl_map_get_ctx(map
);
1412 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1415 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1416 info
->cst
= isl_vec_alloc(ctx
, n
);
1417 if (n
&& (!info
->is_cst
|| !info
->cst
))
1420 for (i
= 0; i
< n
; ++i
) {
1423 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1426 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1427 if (info
->is_cst
[i
])
1428 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1435 sched_info_free(info
);
1439 /* The different types of access relations that isl_union_access_info
1442 * "isl_access_sink" represents the sink accesses.
1443 * "isl_access_must_source" represents the definite source accesses.
1444 * "isl_access_may_source" represents the possible source accesses.
1445 * "isl_access_kill" represents the kills.
1447 * isl_access_sink is sometimes treated differently and
1448 * should therefore appear first.
1450 enum isl_access_type
{
1452 isl_access_must_source
,
1453 isl_access_may_source
,
1458 /* This structure represents the input for a dependence analysis computation.
1460 * "access" contains the access relations.
1462 * "schedule" or "schedule_map" represents the execution order.
1463 * Exactly one of these fields should be NULL. The other field
1464 * determines the execution order.
1466 * The domains of these four maps refer to the same iteration spaces(s).
1467 * The ranges of the first three maps also refer to the same data space(s).
1469 * After a call to isl_union_access_info_introduce_schedule,
1470 * the "schedule_map" field no longer contains useful information.
1472 struct isl_union_access_info
{
1473 isl_union_map
*access
[isl_access_end
];
1475 isl_schedule
*schedule
;
1476 isl_union_map
*schedule_map
;
1479 /* Free "access" and return NULL.
1481 __isl_null isl_union_access_info
*isl_union_access_info_free(
1482 __isl_take isl_union_access_info
*access
)
1484 enum isl_access_type i
;
1489 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1490 isl_union_map_free(access
->access
[i
]);
1491 isl_schedule_free(access
->schedule
);
1492 isl_union_map_free(access
->schedule_map
);
1498 /* Return the isl_ctx to which "access" belongs.
1500 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1504 return isl_union_map_get_ctx(access
->access
[isl_access_sink
]);
1507 /* Construct an empty (invalid) isl_union_access_info object.
1508 * The caller is responsible for setting the sink access relation and
1509 * initializing all the other fields, e.g., by calling
1510 * isl_union_access_info_init.
1512 static __isl_give isl_union_access_info
*isl_union_access_info_alloc(
1515 return isl_calloc_type(ctx
, isl_union_access_info
);
1518 /* Initialize all the fields of "info", except the sink access relation,
1519 * which is assumed to have been set by the caller.
1521 * By default, we use the schedule field of the isl_union_access_info,
1522 * but this may be overridden by a call
1523 * to isl_union_access_info_set_schedule_map.
1525 static __isl_give isl_union_access_info
*isl_union_access_info_init(
1526 __isl_take isl_union_access_info
*info
)
1529 isl_union_map
*empty
;
1530 enum isl_access_type i
;
1534 if (!info
->access
[isl_access_sink
])
1535 return isl_union_access_info_free(info
);
1537 space
= isl_union_map_get_space(info
->access
[isl_access_sink
]);
1538 empty
= isl_union_map_empty(isl_space_copy(space
));
1539 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1540 if (!info
->access
[i
])
1541 info
->access
[i
] = isl_union_map_copy(empty
);
1542 isl_union_map_free(empty
);
1543 if (!info
->schedule
&& !info
->schedule_map
)
1544 info
->schedule
= isl_schedule_empty(isl_space_copy(space
));
1545 isl_space_free(space
);
1547 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1548 if (!info
->access
[i
])
1549 return isl_union_access_info_free(info
);
1550 if (!info
->schedule
&& !info
->schedule_map
)
1551 return isl_union_access_info_free(info
);
1556 /* Create a new isl_union_access_info with the given sink accesses and
1557 * and no other accesses or schedule information.
1559 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1560 __isl_take isl_union_map
*sink
)
1563 isl_union_access_info
*access
;
1567 ctx
= isl_union_map_get_ctx(sink
);
1568 access
= isl_union_access_info_alloc(ctx
);
1571 access
->access
[isl_access_sink
] = sink
;
1572 return isl_union_access_info_init(access
);
1574 isl_union_map_free(sink
);
1578 /* Replace the access relation of type "type" of "info" by "access".
1580 static __isl_give isl_union_access_info
*isl_union_access_info_set(
1581 __isl_take isl_union_access_info
*info
,
1582 enum isl_access_type type
, __isl_take isl_union_map
*access
)
1584 if (!info
|| !access
)
1587 isl_union_map_free(info
->access
[type
]);
1588 info
->access
[type
] = access
;
1592 isl_union_access_info_free(info
);
1593 isl_union_map_free(access
);
1597 /* Replace the definite source accesses of "access" by "must_source".
1599 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1600 __isl_take isl_union_access_info
*access
,
1601 __isl_take isl_union_map
*must_source
)
1603 return isl_union_access_info_set(access
, isl_access_must_source
,
1607 /* Replace the possible source accesses of "access" by "may_source".
1609 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1610 __isl_take isl_union_access_info
*access
,
1611 __isl_take isl_union_map
*may_source
)
1613 return isl_union_access_info_set(access
, isl_access_may_source
,
1617 /* Replace the kills of "info" by "kill".
1619 __isl_give isl_union_access_info
*isl_union_access_info_set_kill(
1620 __isl_take isl_union_access_info
*info
, __isl_take isl_union_map
*kill
)
1622 return isl_union_access_info_set(info
, isl_access_kill
, kill
);
1625 /* Return the access relation of type "type" of "info".
1627 static __isl_give isl_union_map
*isl_union_access_info_get(
1628 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1632 return isl_union_map_copy(info
->access
[type
]);
1635 /* Return the definite source accesses of "info".
1637 __isl_give isl_union_map
*isl_union_access_info_get_must_source(
1638 __isl_keep isl_union_access_info
*info
)
1640 return isl_union_access_info_get(info
, isl_access_must_source
);
1643 /* Return the possible source accesses of "info".
1645 __isl_give isl_union_map
*isl_union_access_info_get_may_source(
1646 __isl_keep isl_union_access_info
*info
)
1648 return isl_union_access_info_get(info
, isl_access_may_source
);
1651 /* Return the kills of "info".
1653 __isl_give isl_union_map
*isl_union_access_info_get_kill(
1654 __isl_keep isl_union_access_info
*info
)
1656 return isl_union_access_info_get(info
, isl_access_kill
);
1659 /* Does "info" specify any kills?
1661 static isl_bool
isl_union_access_has_kill(
1662 __isl_keep isl_union_access_info
*info
)
1667 return isl_bool_error
;
1668 empty
= isl_union_map_is_empty(info
->access
[isl_access_kill
]);
1669 return isl_bool_not(empty
);
1672 /* Replace the schedule of "access" by "schedule".
1673 * Also free the schedule_map in case it was set last.
1675 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1676 __isl_take isl_union_access_info
*access
,
1677 __isl_take isl_schedule
*schedule
)
1679 if (!access
|| !schedule
)
1682 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1683 isl_schedule_free(access
->schedule
);
1684 access
->schedule
= schedule
;
1688 isl_union_access_info_free(access
);
1689 isl_schedule_free(schedule
);
1693 /* Replace the schedule map of "access" by "schedule_map".
1694 * Also free the schedule in case it was set last.
1696 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1697 __isl_take isl_union_access_info
*access
,
1698 __isl_take isl_union_map
*schedule_map
)
1700 if (!access
|| !schedule_map
)
1703 isl_union_map_free(access
->schedule_map
);
1704 access
->schedule
= isl_schedule_free(access
->schedule
);
1705 access
->schedule_map
= schedule_map
;
1709 isl_union_access_info_free(access
);
1710 isl_union_map_free(schedule_map
);
1714 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1715 __isl_keep isl_union_access_info
*access
)
1717 isl_union_access_info
*copy
;
1718 enum isl_access_type i
;
1722 copy
= isl_union_access_info_from_sink(
1723 isl_union_map_copy(access
->access
[isl_access_sink
]));
1724 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1725 copy
= isl_union_access_info_set(copy
, i
,
1726 isl_union_map_copy(access
->access
[i
]));
1727 if (access
->schedule
)
1728 copy
= isl_union_access_info_set_schedule(copy
,
1729 isl_schedule_copy(access
->schedule
));
1731 copy
= isl_union_access_info_set_schedule_map(copy
,
1732 isl_union_map_copy(access
->schedule_map
));
1737 /* Print a key-value pair of a YAML mapping to "p",
1738 * with key "name" and value "umap".
1740 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1741 const char *name
, __isl_keep isl_union_map
*umap
)
1743 p
= isl_printer_print_str(p
, name
);
1744 p
= isl_printer_yaml_next(p
);
1745 p
= isl_printer_print_str(p
, "\"");
1746 p
= isl_printer_print_union_map(p
, umap
);
1747 p
= isl_printer_print_str(p
, "\"");
1748 p
= isl_printer_yaml_next(p
);
1753 /* An enumeration of the various keys that may appear in a YAML mapping
1754 * of an isl_union_access_info object.
1755 * The keys for the access relation types are assumed to have the same values
1756 * as the access relation types in isl_access_type.
1759 isl_ai_key_error
= -1,
1760 isl_ai_key_sink
= isl_access_sink
,
1761 isl_ai_key_must_source
= isl_access_must_source
,
1762 isl_ai_key_may_source
= isl_access_may_source
,
1763 isl_ai_key_kill
= isl_access_kill
,
1764 isl_ai_key_schedule_map
,
1765 isl_ai_key_schedule
,
1769 /* Textual representations of the YAML keys for an isl_union_access_info
1772 static char *key_str
[] = {
1773 [isl_ai_key_sink
] = "sink",
1774 [isl_ai_key_must_source
] = "must_source",
1775 [isl_ai_key_may_source
] = "may_source",
1776 [isl_ai_key_kill
] = "kill",
1777 [isl_ai_key_schedule_map
] = "schedule_map",
1778 [isl_ai_key_schedule
] = "schedule",
1781 /* Print a key-value pair corresponding to the access relation of type "type"
1782 * of a YAML mapping of "info" to "p".
1784 * The sink access relation is always printed, but any other access relation
1785 * is only printed if it is non-empty.
1787 static __isl_give isl_printer
*print_access_field(__isl_take isl_printer
*p
,
1788 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1790 if (type
!= isl_access_sink
) {
1793 empty
= isl_union_map_is_empty(info
->access
[type
]);
1795 return isl_printer_free(p
);
1799 return print_union_map_field(p
, key_str
[type
], info
->access
[type
]);
1802 /* Print the information contained in "access" to "p".
1803 * The information is printed as a YAML document.
1805 __isl_give isl_printer
*isl_printer_print_union_access_info(
1806 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1808 enum isl_access_type i
;
1811 return isl_printer_free(p
);
1813 p
= isl_printer_yaml_start_mapping(p
);
1814 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1815 p
= print_access_field(p
, access
, i
);
1816 if (access
->schedule
) {
1817 p
= isl_printer_print_str(p
, key_str
[isl_ai_key_schedule
]);
1818 p
= isl_printer_yaml_next(p
);
1819 p
= isl_printer_print_schedule(p
, access
->schedule
);
1820 p
= isl_printer_yaml_next(p
);
1822 p
= print_union_map_field(p
, key_str
[isl_ai_key_schedule_map
],
1823 access
->schedule_map
);
1825 p
= isl_printer_yaml_end_mapping(p
);
1830 /* Return a string representation of the information in "access".
1831 * The information is printed in flow format.
1833 __isl_give
char *isl_union_access_info_to_str(
1834 __isl_keep isl_union_access_info
*access
)
1842 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1843 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1844 p
= isl_printer_print_union_access_info(p
, access
);
1845 s
= isl_printer_get_str(p
);
1846 isl_printer_free(p
);
1852 #define KEY enum isl_ai_key
1854 #define KEY_ERROR isl_ai_key_error
1856 #define KEY_END isl_ai_key_end
1857 #include "extract_key.c"
1860 #define BASE union_map
1861 #include "read_in_string_templ.c"
1863 /* Read an isl_union_access_info object from "s".
1865 * Start off with an empty (invalid) isl_union_access_info object and
1866 * then fill up the fields based on the input.
1867 * The input needs to contain at least a description of the sink
1868 * access relation as well as some form of schedule.
1869 * The other access relations are set to empty relations
1870 * by isl_union_access_info_init if they are not specified in the input.
1872 __isl_give isl_union_access_info
*isl_stream_read_union_access_info(
1876 isl_union_access_info
*info
;
1879 int schedule_set
= 0;
1881 if (isl_stream_yaml_read_start_mapping(s
))
1884 ctx
= isl_stream_get_ctx(s
);
1885 info
= isl_union_access_info_alloc(ctx
);
1886 while ((more
= isl_stream_yaml_next(s
)) > 0) {
1887 enum isl_ai_key key
;
1888 isl_union_map
*access
, *schedule_map
;
1889 isl_schedule
*schedule
;
1892 if (isl_stream_yaml_next(s
) < 0)
1893 return isl_union_access_info_free(info
);
1895 case isl_ai_key_end
:
1896 case isl_ai_key_error
:
1897 return isl_union_access_info_free(info
);
1898 case isl_ai_key_sink
:
1900 case isl_ai_key_must_source
:
1901 case isl_ai_key_may_source
:
1902 case isl_ai_key_kill
:
1903 access
= read_union_map(s
);
1904 info
= isl_union_access_info_set(info
, key
, access
);
1908 case isl_ai_key_schedule_map
:
1910 schedule_map
= read_union_map(s
);
1911 info
= isl_union_access_info_set_schedule_map(info
,
1916 case isl_ai_key_schedule
:
1918 schedule
= isl_stream_read_schedule(s
);
1919 info
= isl_union_access_info_set_schedule(info
,
1927 return isl_union_access_info_free(info
);
1929 if (isl_stream_yaml_read_end_mapping(s
) < 0) {
1930 isl_stream_error(s
, NULL
, "unexpected extra elements");
1931 return isl_union_access_info_free(info
);
1935 isl_stream_error(s
, NULL
, "no sink specified");
1936 return isl_union_access_info_free(info
);
1939 if (!schedule_set
) {
1940 isl_stream_error(s
, NULL
, "no schedule specified");
1941 return isl_union_access_info_free(info
);
1944 return isl_union_access_info_init(info
);
1947 /* Read an isl_union_access_info object from the file "input".
1949 __isl_give isl_union_access_info
*isl_union_access_info_read_from_file(
1950 isl_ctx
*ctx
, FILE *input
)
1953 isl_union_access_info
*access
;
1955 s
= isl_stream_new_file(ctx
, input
);
1958 access
= isl_stream_read_union_access_info(s
);
1964 /* Update the fields of "access" such that they all have the same parameters,
1965 * keeping in mind that the schedule_map field may be NULL and ignoring
1966 * the schedule field.
1968 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1969 __isl_take isl_union_access_info
*access
)
1972 enum isl_access_type i
;
1977 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
1978 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1979 space
= isl_space_align_params(space
,
1980 isl_union_map_get_space(access
->access
[i
]));
1981 if (access
->schedule_map
)
1982 space
= isl_space_align_params(space
,
1983 isl_union_map_get_space(access
->schedule_map
));
1984 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1986 isl_union_map_align_params(access
->access
[i
],
1987 isl_space_copy(space
));
1988 if (!access
->schedule_map
) {
1989 isl_space_free(space
);
1991 access
->schedule_map
=
1992 isl_union_map_align_params(access
->schedule_map
, space
);
1993 if (!access
->schedule_map
)
1994 return isl_union_access_info_free(access
);
1997 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1998 if (!access
->access
[i
])
1999 return isl_union_access_info_free(access
);
2004 /* Prepend the schedule dimensions to the iteration domains.
2006 * That is, if the schedule is of the form
2010 * while the access relations are of the form
2014 * then the updated access relations are of the form
2018 * The schedule map is also replaced by the map
2022 * that is used during the internal computation.
2023 * Neither the original schedule map nor this updated schedule map
2024 * are used after the call to this function.
2026 static __isl_give isl_union_access_info
*
2027 isl_union_access_info_introduce_schedule(
2028 __isl_take isl_union_access_info
*access
)
2031 enum isl_access_type i
;
2036 sm
= isl_union_map_reverse(access
->schedule_map
);
2037 sm
= isl_union_map_range_map(sm
);
2038 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2040 isl_union_map_apply_range(isl_union_map_copy(sm
),
2042 access
->schedule_map
= sm
;
2044 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
2045 if (!access
->access
[i
])
2046 return isl_union_access_info_free(access
);
2047 if (!access
->schedule_map
)
2048 return isl_union_access_info_free(access
);
2053 /* This structure represents the result of a dependence analysis computation.
2055 * "must_dep" represents the full definite dependences
2056 * "may_dep" represents the full non-definite dependences.
2057 * Both are of the form
2059 * [Source] -> [[Sink -> Data]]
2061 * (after the schedule dimensions have been projected out).
2062 * "must_no_source" represents the subset of the sink accesses for which
2063 * definitely no source was found.
2064 * "may_no_source" represents the subset of the sink accesses for which
2065 * possibly, but not definitely, no source was found.
2067 struct isl_union_flow
{
2068 isl_union_map
*must_dep
;
2069 isl_union_map
*may_dep
;
2070 isl_union_map
*must_no_source
;
2071 isl_union_map
*may_no_source
;
2074 /* Return the isl_ctx to which "flow" belongs.
2076 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
2078 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
2081 /* Free "flow" and return NULL.
2083 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
2087 isl_union_map_free(flow
->must_dep
);
2088 isl_union_map_free(flow
->may_dep
);
2089 isl_union_map_free(flow
->must_no_source
);
2090 isl_union_map_free(flow
->may_no_source
);
2095 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
2100 fprintf(stderr
, "must dependences: ");
2101 isl_union_map_dump(flow
->must_dep
);
2102 fprintf(stderr
, "may dependences: ");
2103 isl_union_map_dump(flow
->may_dep
);
2104 fprintf(stderr
, "must no source: ");
2105 isl_union_map_dump(flow
->must_no_source
);
2106 fprintf(stderr
, "may no source: ");
2107 isl_union_map_dump(flow
->may_no_source
);
2110 /* Return the full definite dependences in "flow", with accessed elements.
2112 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
2113 __isl_keep isl_union_flow
*flow
)
2117 return isl_union_map_copy(flow
->must_dep
);
2120 /* Return the full possible dependences in "flow", including the definite
2121 * dependences, with accessed elements.
2123 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
2124 __isl_keep isl_union_flow
*flow
)
2128 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2129 isl_union_map_copy(flow
->may_dep
));
2132 /* Return the definite dependences in "flow", without the accessed elements.
2134 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
2135 __isl_keep isl_union_flow
*flow
)
2141 dep
= isl_union_map_copy(flow
->must_dep
);
2142 return isl_union_map_range_factor_domain(dep
);
2145 /* Return the possible dependences in "flow", including the definite
2146 * dependences, without the accessed elements.
2148 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
2149 __isl_keep isl_union_flow
*flow
)
2155 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2156 isl_union_map_copy(flow
->may_dep
));
2157 return isl_union_map_range_factor_domain(dep
);
2160 /* Return the non-definite dependences in "flow".
2162 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
2163 __isl_keep isl_union_flow
*flow
)
2167 return isl_union_map_copy(flow
->may_dep
);
2170 /* Return the subset of the sink accesses for which definitely
2171 * no source was found.
2173 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
2174 __isl_keep isl_union_flow
*flow
)
2178 return isl_union_map_copy(flow
->must_no_source
);
2181 /* Return the subset of the sink accesses for which possibly
2182 * no source was found, including those for which definitely
2183 * no source was found.
2185 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
2186 __isl_keep isl_union_flow
*flow
)
2190 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
2191 isl_union_map_copy(flow
->may_no_source
));
2194 /* Return the subset of the sink accesses for which possibly, but not
2195 * definitely, no source was found.
2197 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
2198 __isl_keep isl_union_flow
*flow
)
2202 return isl_union_map_copy(flow
->may_no_source
);
2205 /* Create a new isl_union_flow object, initialized with empty
2206 * dependence relations and sink subsets.
2208 static __isl_give isl_union_flow
*isl_union_flow_alloc(
2209 __isl_take isl_space
*space
)
2212 isl_union_map
*empty
;
2213 isl_union_flow
*flow
;
2217 ctx
= isl_space_get_ctx(space
);
2218 flow
= isl_alloc_type(ctx
, isl_union_flow
);
2222 empty
= isl_union_map_empty(space
);
2223 flow
->must_dep
= isl_union_map_copy(empty
);
2224 flow
->may_dep
= isl_union_map_copy(empty
);
2225 flow
->must_no_source
= isl_union_map_copy(empty
);
2226 flow
->may_no_source
= empty
;
2228 if (!flow
->must_dep
|| !flow
->may_dep
||
2229 !flow
->must_no_source
|| !flow
->may_no_source
)
2230 return isl_union_flow_free(flow
);
2234 isl_space_free(space
);
2238 /* Copy this isl_union_flow object.
2240 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
2242 isl_union_flow
*copy
;
2247 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
2252 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
2253 isl_union_map_copy(flow
->must_dep
));
2254 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
2255 isl_union_map_copy(flow
->may_dep
));
2256 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
2257 isl_union_map_copy(flow
->must_no_source
));
2258 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
2259 isl_union_map_copy(flow
->may_no_source
));
2261 if (!copy
->must_dep
|| !copy
->may_dep
||
2262 !copy
->must_no_source
|| !copy
->may_no_source
)
2263 return isl_union_flow_free(copy
);
2268 /* Drop the schedule dimensions from the iteration domains in "flow".
2269 * In particular, the schedule dimensions have been prepended
2270 * to the iteration domains prior to the dependence analysis by
2271 * replacing the iteration domain D, by the wrapped map [S -> D].
2272 * Replace these wrapped maps by the original D.
2274 * In particular, the dependences computed by access_info_compute_flow_core
2277 * [S -> D] -> [[S' -> D'] -> A]
2279 * The schedule dimensions are projected out by first currying the range,
2282 * [S -> D] -> [S' -> [D' -> A]]
2284 * and then computing the factor range
2288 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
2289 __isl_take isl_union_flow
*flow
)
2294 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
2295 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
2296 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
2297 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
2298 flow
->must_no_source
=
2299 isl_union_map_domain_factor_range(flow
->must_no_source
);
2300 flow
->may_no_source
=
2301 isl_union_map_domain_factor_range(flow
->may_no_source
);
2303 if (!flow
->must_dep
|| !flow
->may_dep
||
2304 !flow
->must_no_source
|| !flow
->may_no_source
)
2305 return isl_union_flow_free(flow
);
2310 struct isl_compute_flow_data
{
2311 isl_union_map
*must_source
;
2312 isl_union_map
*may_source
;
2313 isl_union_flow
*flow
;
2318 struct isl_sched_info
*sink_info
;
2319 struct isl_sched_info
**source_info
;
2320 isl_access_info
*accesses
;
2323 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
2327 struct isl_compute_flow_data
*data
;
2329 data
= (struct isl_compute_flow_data
*)user
;
2331 dim
= isl_space_range(isl_map_get_space(map
));
2333 eq
= isl_space_is_equal(dim
, data
->dim
);
2335 isl_space_free(dim
);
2339 return isl_stat_error
;
2346 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
2350 struct isl_sched_info
*info
;
2351 struct isl_compute_flow_data
*data
;
2353 data
= (struct isl_compute_flow_data
*)user
;
2355 dim
= isl_space_range(isl_map_get_space(map
));
2357 eq
= isl_space_is_equal(dim
, data
->dim
);
2359 isl_space_free(dim
);
2368 info
= sched_info_alloc(map
);
2369 data
->source_info
[data
->count
] = info
;
2371 data
->accesses
= isl_access_info_add_source(data
->accesses
,
2372 map
, data
->must
, info
);
2379 return isl_stat_error
;
2382 /* Determine the shared nesting level and the "textual order" of
2383 * the given accesses.
2385 * We first determine the minimal schedule dimension for both accesses.
2387 * If among those dimensions, we can find one where both have a fixed
2388 * value and if moreover those values are different, then the previous
2389 * dimension is the last shared nesting level and the textual order
2390 * is determined based on the order of the fixed values.
2391 * If no such fixed values can be found, then we set the shared
2392 * nesting level to the minimal schedule dimension, with no textual ordering.
2394 static int before(void *first
, void *second
)
2396 struct isl_sched_info
*info1
= first
;
2397 struct isl_sched_info
*info2
= second
;
2401 n1
= isl_vec_size(info1
->cst
);
2402 n2
= isl_vec_size(info2
->cst
);
2407 for (i
= 0; i
< n1
; ++i
) {
2411 if (!info1
->is_cst
[i
])
2413 if (!info2
->is_cst
[i
])
2415 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2419 r
= 2 * i
+ (cmp
< 0);
2427 /* Check if the given two accesses may be coscheduled.
2428 * If so, return 1. Otherwise return 0.
2430 * Two accesses may only be coscheduled if the fixed schedule
2431 * coordinates have the same values.
2433 static int coscheduled(void *first
, void *second
)
2435 struct isl_sched_info
*info1
= first
;
2436 struct isl_sched_info
*info2
= second
;
2440 n1
= isl_vec_size(info1
->cst
);
2441 n2
= isl_vec_size(info2
->cst
);
2446 for (i
= 0; i
< n1
; ++i
) {
2449 if (!info1
->is_cst
[i
])
2451 if (!info2
->is_cst
[i
])
2453 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2461 /* Given a sink access, look for all the source accesses that access
2462 * the same array and perform dataflow analysis on them using
2463 * isl_access_info_compute_flow_core.
2465 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
2469 struct isl_compute_flow_data
*data
;
2473 data
= (struct isl_compute_flow_data
*)user
;
2476 ctx
= isl_map_get_ctx(map
);
2478 data
->accesses
= NULL
;
2479 data
->sink_info
= NULL
;
2480 data
->source_info
= NULL
;
2482 data
->dim
= isl_space_range(isl_map_get_space(map
));
2484 if (isl_union_map_foreach_map(data
->must_source
,
2485 &count_matching_array
, data
) < 0)
2487 if (isl_union_map_foreach_map(data
->may_source
,
2488 &count_matching_array
, data
) < 0)
2491 data
->sink_info
= sched_info_alloc(map
);
2492 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
2495 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
2496 data
->sink_info
, &before
, data
->count
);
2497 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
2500 data
->accesses
->coscheduled
= &coscheduled
;
2503 if (isl_union_map_foreach_map(data
->must_source
,
2504 &collect_matching_array
, data
) < 0)
2507 if (isl_union_map_foreach_map(data
->may_source
,
2508 &collect_matching_array
, data
) < 0)
2511 flow
= access_info_compute_flow_core(data
->accesses
);
2512 data
->accesses
= NULL
;
2517 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
2518 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
2519 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2520 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2522 for (i
= 0; i
< flow
->n_source
; ++i
) {
2524 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2525 if (flow
->dep
[i
].must
)
2526 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2528 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2531 isl_flow_free(flow
);
2533 sched_info_free(data
->sink_info
);
2534 if (data
->source_info
) {
2535 for (i
= 0; i
< data
->count
; ++i
)
2536 sched_info_free(data
->source_info
[i
]);
2537 free(data
->source_info
);
2539 isl_space_free(data
->dim
);
2544 isl_access_info_free(data
->accesses
);
2545 sched_info_free(data
->sink_info
);
2546 if (data
->source_info
) {
2547 for (i
= 0; i
< data
->count
; ++i
)
2548 sched_info_free(data
->source_info
[i
]);
2549 free(data
->source_info
);
2551 isl_space_free(data
->dim
);
2554 return isl_stat_error
;
2557 /* Add the kills of "info" to the must-sources.
2559 static __isl_give isl_union_access_info
*
2560 isl_union_access_info_add_kill_to_must_source(
2561 __isl_take isl_union_access_info
*info
)
2563 isl_union_map
*must
, *kill
;
2565 must
= isl_union_access_info_get_must_source(info
);
2566 kill
= isl_union_access_info_get_kill(info
);
2567 must
= isl_union_map_union(must
, kill
);
2568 return isl_union_access_info_set_must_source(info
, must
);
2571 /* Drop dependences from "flow" that purely originate from kills.
2572 * That is, only keep those dependences that originate from
2573 * the original must-sources "must" and/or the original may-sources "may".
2574 * In particular, "must" contains the must-sources from before
2575 * the kills were added and "may" contains the may-source from before
2576 * the kills were removed.
2578 * The dependences are of the form
2580 * Source -> [Sink -> Data]
2582 * Only those dependences are kept where the Source -> Data part
2583 * is a subset of the original may-sources or must-sources.
2584 * Of those, only the must-dependences that intersect with the must-sources
2585 * remain must-dependences.
2586 * If there is some overlap between the may-sources and the must-sources,
2587 * then the may-dependences and must-dependences may also overlap.
2588 * This should be fine since the may-dependences are only kept
2589 * disjoint from the must-dependences for the isl_union_map_compute_flow
2590 * interface. This interface does not support kills, so it will
2591 * not end up calling this function.
2593 static __isl_give isl_union_flow
*isl_union_flow_drop_kill_source(
2594 __isl_take isl_union_flow
*flow
, __isl_take isl_union_map
*must
,
2595 __isl_take isl_union_map
*may
)
2597 isl_union_map
*move
;
2601 move
= isl_union_map_copy(flow
->must_dep
);
2602 move
= isl_union_map_intersect_range_factor_range(move
,
2603 isl_union_map_copy(may
));
2604 may
= isl_union_map_union(may
, isl_union_map_copy(must
));
2605 flow
->may_dep
= isl_union_map_intersect_range_factor_range(
2606 flow
->may_dep
, may
);
2607 flow
->must_dep
= isl_union_map_intersect_range_factor_range(
2608 flow
->must_dep
, must
);
2609 flow
->may_dep
= isl_union_map_union(flow
->may_dep
, move
);
2610 if (!flow
->must_dep
|| !flow
->may_dep
)
2611 return isl_union_flow_free(flow
);
2615 isl_union_map_free(must
);
2616 isl_union_map_free(may
);
2620 /* Remove the must accesses from the may accesses.
2622 * A must access always trumps a may access, so there is no need
2623 * for a must access to also be considered as a may access. Doing so
2624 * would only cost extra computations only to find out that
2625 * the duplicated may access does not make any difference.
2627 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2628 __isl_take isl_union_access_info
*access
)
2632 access
->access
[isl_access_may_source
] =
2633 isl_union_map_subtract(access
->access
[isl_access_may_source
],
2634 isl_union_map_copy(access
->access
[isl_access_must_source
]));
2635 if (!access
->access
[isl_access_may_source
])
2636 return isl_union_access_info_free(access
);
2641 /* Given a description of the "sink" accesses, the "source" accesses and
2642 * a schedule, compute for each instance of a sink access
2643 * and for each element accessed by that instance,
2644 * the possible or definite source accesses that last accessed the
2645 * element accessed by the sink access before this sink access
2646 * in the sense that there is no intermediate definite source access.
2648 * The must_no_source and may_no_source elements of the result
2649 * are subsets of access->sink. The elements must_dep and may_dep
2650 * map domain elements of access->{may,must)_source to
2651 * domain elements of access->sink.
2653 * This function is used when only the schedule map representation
2656 * We first prepend the schedule dimensions to the domain
2657 * of the accesses so that we can easily compare their relative order.
2658 * Then we consider each sink access individually in compute_flow.
2660 static __isl_give isl_union_flow
*compute_flow_union_map(
2661 __isl_take isl_union_access_info
*access
)
2663 struct isl_compute_flow_data data
;
2664 isl_union_map
*sink
;
2666 access
= isl_union_access_info_align_params(access
);
2667 access
= isl_union_access_info_introduce_schedule(access
);
2671 data
.must_source
= access
->access
[isl_access_must_source
];
2672 data
.may_source
= access
->access
[isl_access_may_source
];
2674 sink
= access
->access
[isl_access_sink
];
2675 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(sink
));
2677 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
2680 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2682 isl_union_access_info_free(access
);
2685 isl_union_access_info_free(access
);
2686 isl_union_flow_free(data
.flow
);
2690 /* A schedule access relation.
2692 * The access relation "access" is of the form [S -> D] -> A,
2693 * where S corresponds to the prefix schedule at "node".
2694 * "must" is only relevant for source accesses and indicates
2695 * whether the access is a must source or a may source.
2697 struct isl_scheduled_access
{
2700 isl_schedule_node
*node
;
2703 /* Data structure for keeping track of individual scheduled sink and source
2704 * accesses when computing dependence analysis based on a schedule tree.
2706 * "n_sink" is the number of used entries in "sink"
2707 * "n_source" is the number of used entries in "source"
2709 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2710 * to keep track of the current node and
2711 * of what extract_sink_source needs to do.
2713 struct isl_compute_flow_schedule_data
{
2714 isl_union_access_info
*access
;
2719 struct isl_scheduled_access
*sink
;
2720 struct isl_scheduled_access
*source
;
2724 isl_schedule_node
*node
;
2727 /* Align the parameters of all sinks with all sources.
2729 * If there are no sinks or no sources, then no alignment is needed.
2731 static void isl_compute_flow_schedule_data_align_params(
2732 struct isl_compute_flow_schedule_data
*data
)
2737 if (data
->n_sink
== 0 || data
->n_source
== 0)
2740 space
= isl_map_get_space(data
->sink
[0].access
);
2742 for (i
= 1; i
< data
->n_sink
; ++i
)
2743 space
= isl_space_align_params(space
,
2744 isl_map_get_space(data
->sink
[i
].access
));
2745 for (i
= 0; i
< data
->n_source
; ++i
)
2746 space
= isl_space_align_params(space
,
2747 isl_map_get_space(data
->source
[i
].access
));
2749 for (i
= 0; i
< data
->n_sink
; ++i
)
2750 data
->sink
[i
].access
=
2751 isl_map_align_params(data
->sink
[i
].access
,
2752 isl_space_copy(space
));
2753 for (i
= 0; i
< data
->n_source
; ++i
)
2754 data
->source
[i
].access
=
2755 isl_map_align_params(data
->source
[i
].access
,
2756 isl_space_copy(space
));
2758 isl_space_free(space
);
2761 /* Free all the memory referenced from "data".
2762 * Do not free "data" itself as it may be allocated on the stack.
2764 static void isl_compute_flow_schedule_data_clear(
2765 struct isl_compute_flow_schedule_data
*data
)
2772 for (i
= 0; i
< data
->n_sink
; ++i
) {
2773 isl_map_free(data
->sink
[i
].access
);
2774 isl_schedule_node_free(data
->sink
[i
].node
);
2777 for (i
= 0; i
< data
->n_source
; ++i
) {
2778 isl_map_free(data
->source
[i
].access
);
2779 isl_schedule_node_free(data
->source
[i
].node
);
2785 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2786 * (an upper bound on) the number of sinks and sources.
2788 * Sinks and sources are only extracted at leaves of the tree,
2789 * so we skip the node if it is not a leaf.
2790 * Otherwise we increment data->n_sink and data->n_source with
2791 * the number of spaces in the sink and source access domains
2792 * that reach this node.
2794 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2797 struct isl_compute_flow_schedule_data
*data
= user
;
2798 isl_union_set
*domain
;
2799 isl_union_map
*umap
;
2800 isl_bool r
= isl_bool_false
;
2802 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2803 return isl_bool_true
;
2805 domain
= isl_schedule_node_get_universe_domain(node
);
2807 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2808 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2809 data
->n_sink
+= isl_union_map_n_map(umap
);
2810 isl_union_map_free(umap
);
2814 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2815 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2816 data
->n_source
+= isl_union_map_n_map(umap
);
2817 isl_union_map_free(umap
);
2821 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2822 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2823 data
->n_source
+= isl_union_map_n_map(umap
);
2824 isl_union_map_free(umap
);
2828 isl_union_set_free(domain
);
2833 /* Add a single scheduled sink or source (depending on data->set_sink)
2834 * with scheduled access relation "map", must property data->must and
2835 * schedule node data->node to the list of sinks or sources.
2837 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2839 struct isl_compute_flow_schedule_data
*data
= user
;
2840 struct isl_scheduled_access
*access
;
2843 access
= data
->sink
+ data
->n_sink
++;
2845 access
= data
->source
+ data
->n_source
++;
2847 access
->access
= map
;
2848 access
->must
= data
->must
;
2849 access
->node
= isl_schedule_node_copy(data
->node
);
2854 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2855 * individual scheduled source and sink accesses (taking into account
2856 * the domain of the schedule).
2858 * We only collect accesses at the leaves of the schedule tree.
2859 * We prepend the schedule dimensions at the leaf to the iteration
2860 * domains of the source and sink accesses and then extract
2861 * the individual accesses (per space).
2863 * In particular, if the prefix schedule at the node is of the form
2867 * while the access relations are of the form
2871 * then the updated access relations are of the form
2875 * Note that S consists of a single space such that introducing S
2876 * in the access relations does not increase the number of spaces.
2878 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2881 struct isl_compute_flow_schedule_data
*data
= user
;
2882 isl_union_map
*prefix
;
2883 isl_union_map
*umap
;
2884 isl_bool r
= isl_bool_false
;
2886 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2887 return isl_bool_true
;
2891 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2892 prefix
= isl_union_map_reverse(prefix
);
2893 prefix
= isl_union_map_range_map(prefix
);
2896 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2897 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2898 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2900 isl_union_map_free(umap
);
2904 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2905 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2906 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2908 isl_union_map_free(umap
);
2912 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2913 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2914 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2916 isl_union_map_free(umap
);
2918 isl_union_map_free(prefix
);
2923 /* isl_access_info_compute_flow callback for determining whether
2924 * the shared nesting level and the ordering within that level
2925 * for two scheduled accesses for use in compute_single_flow.
2927 * The tokens passed to this function refer to the leaves
2928 * in the schedule tree where the accesses take place.
2930 * If n is the shared number of loops, then we need to return
2931 * "2 * n + 1" if "first" precedes "second" inside the innermost
2932 * shared loop and "2 * n" otherwise.
2934 * The innermost shared ancestor may be the leaves themselves
2935 * if the accesses take place in the same leaf. Otherwise,
2936 * it is either a set node or a sequence node. Only in the case
2937 * of a sequence node do we consider one access to precede the other.
2939 static int before_node(void *first
, void *second
)
2941 isl_schedule_node
*node1
= first
;
2942 isl_schedule_node
*node2
= second
;
2943 isl_schedule_node
*shared
;
2947 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2951 depth
= isl_schedule_node_get_schedule_depth(shared
);
2952 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2955 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2957 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2959 before
= pos1
< pos2
;
2962 isl_schedule_node_free(shared
);
2964 return 2 * depth
+ before
;
2967 /* Check if the given two accesses may be coscheduled.
2968 * If so, return 1. Otherwise return 0.
2970 * Two accesses may only be coscheduled if they appear in the same leaf.
2972 static int coscheduled_node(void *first
, void *second
)
2974 isl_schedule_node
*node1
= first
;
2975 isl_schedule_node
*node2
= second
;
2977 return node1
== node2
;
2980 /* Add the scheduled sources from "data" that access
2981 * the same data space as "sink" to "access".
2983 static __isl_give isl_access_info
*add_matching_sources(
2984 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2985 struct isl_compute_flow_schedule_data
*data
)
2990 space
= isl_space_range(isl_map_get_space(sink
->access
));
2991 for (i
= 0; i
< data
->n_source
; ++i
) {
2992 struct isl_scheduled_access
*source
;
2993 isl_space
*source_space
;
2996 source
= &data
->source
[i
];
2997 source_space
= isl_map_get_space(source
->access
);
2998 source_space
= isl_space_range(source_space
);
2999 eq
= isl_space_is_equal(space
, source_space
);
3000 isl_space_free(source_space
);
3007 access
= isl_access_info_add_source(access
,
3008 isl_map_copy(source
->access
), source
->must
, source
->node
);
3011 isl_space_free(space
);
3014 isl_space_free(space
);
3015 isl_access_info_free(access
);
3019 /* Given a scheduled sink access relation "sink", compute the corresponding
3020 * dependences on the sources in "data" and add the computed dependences
3023 * The dependences computed by access_info_compute_flow_core are of the form
3025 * [S -> I] -> [[S' -> I'] -> A]
3027 * The schedule dimensions are projected out by first currying the range,
3030 * [S -> I] -> [S' -> [I' -> A]]
3032 * and then computing the factor range
3036 static __isl_give isl_union_flow
*compute_single_flow(
3037 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
3038 struct isl_compute_flow_schedule_data
*data
)
3041 isl_access_info
*access
;
3048 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
3049 &before_node
, data
->n_source
);
3051 access
->coscheduled
= &coscheduled_node
;
3052 access
= add_matching_sources(access
, sink
, data
);
3054 flow
= access_info_compute_flow_core(access
);
3056 return isl_union_flow_free(uf
);
3058 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
3059 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
3060 isl_union_map_from_map(map
));
3061 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
3062 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
3063 isl_union_map_from_map(map
));
3065 for (i
= 0; i
< flow
->n_source
; ++i
) {
3068 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
3069 map
= isl_map_factor_range(map
);
3070 dep
= isl_union_map_from_map(map
);
3071 if (flow
->dep
[i
].must
)
3072 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
3074 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
3077 isl_flow_free(flow
);
3082 /* Given a description of the "sink" accesses, the "source" accesses and
3083 * a schedule, compute for each instance of a sink access
3084 * and for each element accessed by that instance,
3085 * the possible or definite source accesses that last accessed the
3086 * element accessed by the sink access before this sink access
3087 * in the sense that there is no intermediate definite source access.
3088 * Only consider dependences between statement instances that belong
3089 * to the domain of the schedule.
3091 * The must_no_source and may_no_source elements of the result
3092 * are subsets of access->sink. The elements must_dep and may_dep
3093 * map domain elements of access->{may,must)_source to
3094 * domain elements of access->sink.
3096 * This function is used when a schedule tree representation
3099 * We extract the individual scheduled source and sink access relations
3100 * (taking into account the domain of the schedule) and
3101 * then compute dependences for each scheduled sink individually.
3103 static __isl_give isl_union_flow
*compute_flow_schedule(
3104 __isl_take isl_union_access_info
*access
)
3106 struct isl_compute_flow_schedule_data data
= { access
};
3110 isl_union_flow
*flow
;
3112 ctx
= isl_union_access_info_get_ctx(access
);
3116 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3117 &count_sink_source
, &data
) < 0)
3120 n
= data
.n_sink
+ data
.n_source
;
3121 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
3122 if (n
&& !data
.sink
)
3124 data
.source
= data
.sink
+ data
.n_sink
;
3128 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3129 &collect_sink_source
, &data
) < 0)
3132 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
3133 flow
= isl_union_flow_alloc(space
);
3135 isl_compute_flow_schedule_data_align_params(&data
);
3137 for (i
= 0; i
< data
.n_sink
; ++i
)
3138 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
3140 isl_compute_flow_schedule_data_clear(&data
);
3142 isl_union_access_info_free(access
);
3145 isl_union_access_info_free(access
);
3146 isl_compute_flow_schedule_data_clear(&data
);
3150 /* Given a description of the "sink" accesses, the "source" accesses and
3151 * a schedule, compute for each instance of a sink access
3152 * and for each element accessed by that instance,
3153 * the possible or definite source accesses that last accessed the
3154 * element accessed by the sink access before this sink access
3155 * in the sense that there is no intermediate definite source access.
3157 * The must_no_source and may_no_source elements of the result
3158 * are subsets of access->sink. The elements must_dep and may_dep
3159 * map domain elements of access->{may,must)_source to
3160 * domain elements of access->sink.
3162 * If any kills have been specified, then they are treated as
3163 * must-sources internally. Any dependence that purely derives
3164 * from an original kill is removed from the output.
3166 * We check whether the schedule is available as a schedule tree
3167 * or a schedule map and call the corresponding function to perform
3170 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
3171 __isl_take isl_union_access_info
*access
)
3174 isl_union_map
*must
= NULL
, *may
= NULL
;
3175 isl_union_flow
*flow
;
3177 has_kill
= isl_union_access_has_kill(access
);
3181 must
= isl_union_access_info_get_must_source(access
);
3182 may
= isl_union_access_info_get_may_source(access
);
3184 access
= isl_union_access_info_add_kill_to_must_source(access
);
3185 access
= isl_union_access_info_normalize(access
);
3188 if (access
->schedule
)
3189 flow
= compute_flow_schedule(access
);
3191 flow
= compute_flow_union_map(access
);
3193 flow
= isl_union_flow_drop_kill_source(flow
, must
, may
);
3196 isl_union_access_info_free(access
);
3197 isl_union_map_free(must
);
3198 isl_union_map_free(may
);
3202 /* Print the information contained in "flow" to "p".
3203 * The information is printed as a YAML document.
3205 __isl_give isl_printer
*isl_printer_print_union_flow(
3206 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
3208 isl_union_map
*umap
;
3211 return isl_printer_free(p
);
3213 p
= isl_printer_yaml_start_mapping(p
);
3214 umap
= isl_union_flow_get_full_must_dependence(flow
);
3215 p
= print_union_map_field(p
, "must_dependence", umap
);
3216 isl_union_map_free(umap
);
3217 umap
= isl_union_flow_get_full_may_dependence(flow
);
3218 p
= print_union_map_field(p
, "may_dependence", umap
);
3219 isl_union_map_free(umap
);
3220 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
3221 umap
= isl_union_flow_get_may_no_source(flow
);
3222 p
= print_union_map_field(p
, "may_no_source", umap
);
3223 isl_union_map_free(umap
);
3224 p
= isl_printer_yaml_end_mapping(p
);
3229 /* Return a string representation of the information in "flow".
3230 * The information is printed in flow format.
3232 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
3240 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
3241 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
3242 p
= isl_printer_print_union_flow(p
, flow
);
3243 s
= isl_printer_get_str(p
);
3244 isl_printer_free(p
);
3249 /* Given a collection of "sink" and "source" accesses,
3250 * compute for each iteration of a sink access
3251 * and for each element accessed by that iteration,
3252 * the source access in the list that last accessed the
3253 * element accessed by the sink access before this sink access.
3254 * Each access is given as a map from the loop iterators
3255 * to the array indices.
3256 * The result is a relations between source and sink
3257 * iterations and a subset of the domain of the sink accesses,
3258 * corresponding to those iterations that access an element
3259 * not previously accessed.
3261 * We collect the inputs in an isl_union_access_info object,
3262 * call isl_union_access_info_compute_flow and extract
3263 * the outputs from the result.
3265 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
3266 __isl_take isl_union_map
*must_source
,
3267 __isl_take isl_union_map
*may_source
,
3268 __isl_take isl_union_map
*schedule
,
3269 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
3270 __isl_give isl_union_map
**must_no_source
,
3271 __isl_give isl_union_map
**may_no_source
)
3273 isl_union_access_info
*access
;
3274 isl_union_flow
*flow
;
3276 access
= isl_union_access_info_from_sink(sink
);
3277 access
= isl_union_access_info_set_must_source(access
, must_source
);
3278 access
= isl_union_access_info_set_may_source(access
, may_source
);
3279 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
3280 flow
= isl_union_access_info_compute_flow(access
);
3283 *must_dep
= isl_union_flow_get_must_dependence(flow
);
3285 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
3287 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
3289 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
3291 isl_union_flow_free(flow
);
3293 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
3294 (must_no_source
&& !*must_no_source
) ||
3295 (may_no_source
&& !*may_no_source
))
3301 *must_dep
= isl_union_map_free(*must_dep
);
3303 *may_dep
= isl_union_map_free(*may_dep
);
3305 *must_no_source
= isl_union_map_free(*must_no_source
);
3307 *may_no_source
= isl_union_map_free(*may_no_source
);