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
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
24 #include <isl/schedule_node.h>
26 #include <isl/stream.h>
28 enum isl_restriction_type
{
29 isl_restriction_type_empty
,
30 isl_restriction_type_none
,
31 isl_restriction_type_input
,
32 isl_restriction_type_output
35 struct isl_restriction
{
36 enum isl_restriction_type type
;
42 /* Create a restriction of the given type.
44 static __isl_give isl_restriction
*isl_restriction_alloc(
45 __isl_take isl_map
*source_map
, enum isl_restriction_type type
)
48 isl_restriction
*restr
;
53 ctx
= isl_map_get_ctx(source_map
);
54 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
60 isl_map_free(source_map
);
63 isl_map_free(source_map
);
67 /* Create a restriction that doesn't restrict anything.
69 __isl_give isl_restriction
*isl_restriction_none(__isl_take isl_map
*source_map
)
71 return isl_restriction_alloc(source_map
, isl_restriction_type_none
);
74 /* Create a restriction that removes everything.
76 __isl_give isl_restriction
*isl_restriction_empty(
77 __isl_take isl_map
*source_map
)
79 return isl_restriction_alloc(source_map
, isl_restriction_type_empty
);
82 /* Create a restriction on the input of the maximization problem
83 * based on the given source and sink restrictions.
85 __isl_give isl_restriction
*isl_restriction_input(
86 __isl_take isl_set
*source_restr
, __isl_take isl_set
*sink_restr
)
89 isl_restriction
*restr
;
91 if (!source_restr
|| !sink_restr
)
94 ctx
= isl_set_get_ctx(source_restr
);
95 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
99 restr
->type
= isl_restriction_type_input
;
100 restr
->source
= source_restr
;
101 restr
->sink
= sink_restr
;
105 isl_set_free(source_restr
);
106 isl_set_free(sink_restr
);
110 /* Create a restriction on the output of the maximization problem
111 * based on the given source restriction.
113 __isl_give isl_restriction
*isl_restriction_output(
114 __isl_take isl_set
*source_restr
)
117 isl_restriction
*restr
;
122 ctx
= isl_set_get_ctx(source_restr
);
123 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
127 restr
->type
= isl_restriction_type_output
;
128 restr
->source
= source_restr
;
132 isl_set_free(source_restr
);
136 __isl_null isl_restriction
*isl_restriction_free(
137 __isl_take isl_restriction
*restr
)
142 isl_set_free(restr
->source
);
143 isl_set_free(restr
->sink
);
148 isl_ctx
*isl_restriction_get_ctx(__isl_keep isl_restriction
*restr
)
150 return restr
? isl_set_get_ctx(restr
->source
) : NULL
;
153 /* A private structure to keep track of a mapping together with
154 * a user-specified identifier and a boolean indicating whether
155 * the map represents a must or may access/dependence.
157 struct isl_labeled_map
{
163 typedef int (*isl_access_coscheduled
)(void *first
, void *second
);
165 /* A structure containing the input for dependence analysis:
167 * - n_must + n_may (<= max_source) sources
168 * - a function for determining the relative order of sources and sink
169 * - an optional function "coscheduled" for determining whether sources
170 * may be coscheduled. If "coscheduled" is NULL, then the sources
171 * are assumed not to be coscheduled.
172 * The must sources are placed before the may sources.
174 * domain_map is an auxiliary map that maps the sink access relation
175 * to the domain of this access relation.
176 * This field is only needed when restrict_fn is set and
177 * the field itself is set by isl_access_info_compute_flow.
179 * restrict_fn is a callback that (if not NULL) will be called
180 * right before any lexicographical maximization.
182 struct isl_access_info
{
184 struct isl_labeled_map sink
;
185 isl_access_level_before level_before
;
186 isl_access_coscheduled coscheduled
;
188 isl_access_restrict restrict_fn
;
194 struct isl_labeled_map source
[1];
197 /* A structure containing the output of dependence analysis:
198 * - n_source dependences
199 * - a wrapped subset of the sink for which definitely no source could be found
200 * - a wrapped subset of the sink for which possibly no source could be found
203 isl_set
*must_no_source
;
204 isl_set
*may_no_source
;
206 struct isl_labeled_map
*dep
;
209 /* Construct an isl_access_info structure and fill it up with
210 * the given data. The number of sources is set to 0.
212 __isl_give isl_access_info
*isl_access_info_alloc(__isl_take isl_map
*sink
,
213 void *sink_user
, isl_access_level_before fn
, int max_source
)
216 struct isl_access_info
*acc
;
221 ctx
= isl_map_get_ctx(sink
);
222 isl_assert(ctx
, max_source
>= 0, goto error
);
224 acc
= isl_calloc(ctx
, struct isl_access_info
,
225 sizeof(struct isl_access_info
) +
226 (max_source
- 1) * sizeof(struct isl_labeled_map
));
230 acc
->sink
.map
= sink
;
231 acc
->sink
.data
= sink_user
;
232 acc
->level_before
= fn
;
233 acc
->max_source
= max_source
;
243 /* Free the given isl_access_info structure.
245 __isl_null isl_access_info
*isl_access_info_free(
246 __isl_take isl_access_info
*acc
)
252 isl_map_free(acc
->domain_map
);
253 isl_map_free(acc
->sink
.map
);
254 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
255 isl_map_free(acc
->source
[i
].map
);
260 isl_ctx
*isl_access_info_get_ctx(__isl_keep isl_access_info
*acc
)
262 return acc
? isl_map_get_ctx(acc
->sink
.map
) : NULL
;
265 __isl_give isl_access_info
*isl_access_info_set_restrict(
266 __isl_take isl_access_info
*acc
, isl_access_restrict fn
, void *user
)
270 acc
->restrict_fn
= fn
;
271 acc
->restrict_user
= user
;
275 /* Add another source to an isl_access_info structure, making
276 * sure the "must" sources are placed before the "may" sources.
277 * This function may be called at most max_source times on a
278 * given isl_access_info structure, with max_source as specified
279 * in the call to isl_access_info_alloc that constructed the structure.
281 __isl_give isl_access_info
*isl_access_info_add_source(
282 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
283 int must
, void *source_user
)
289 ctx
= isl_map_get_ctx(acc
->sink
.map
);
290 isl_assert(ctx
, acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
294 acc
->source
[acc
->n_must
+ acc
->n_may
] =
295 acc
->source
[acc
->n_must
];
296 acc
->source
[acc
->n_must
].map
= source
;
297 acc
->source
[acc
->n_must
].data
= source_user
;
298 acc
->source
[acc
->n_must
].must
= 1;
301 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
302 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
303 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
309 isl_map_free(source
);
310 isl_access_info_free(acc
);
314 /* Return -n, 0 or n (with n a positive value), depending on whether
315 * the source access identified by p1 should be sorted before, together
316 * or after that identified by p2.
318 * If p1 appears before p2, then it should be sorted first.
319 * For more generic initial schedules, it is possible that neither
320 * p1 nor p2 appears before the other, or at least not in any obvious way.
321 * We therefore also check if p2 appears before p1, in which case p2
322 * should be sorted first.
323 * If not, we try to order the two statements based on the description
324 * of the iteration domains. This results in an arbitrary, but fairly
327 static int access_sort_cmp(const void *p1
, const void *p2
, void *user
)
329 isl_access_info
*acc
= user
;
330 const struct isl_labeled_map
*i1
, *i2
;
333 i1
= (const struct isl_labeled_map
*) p1
;
334 i2
= (const struct isl_labeled_map
*) p2
;
336 level1
= acc
->level_before(i1
->data
, i2
->data
);
340 level2
= acc
->level_before(i2
->data
, i1
->data
);
344 h1
= isl_map_get_hash(i1
->map
);
345 h2
= isl_map_get_hash(i2
->map
);
346 return h1
> h2
? 1 : h1
< h2
? -1 : 0;
349 /* Sort the must source accesses in their textual order.
351 static __isl_give isl_access_info
*isl_access_info_sort_sources(
352 __isl_take isl_access_info
*acc
)
356 if (acc
->n_must
<= 1)
359 if (isl_sort(acc
->source
, acc
->n_must
, sizeof(struct isl_labeled_map
),
360 access_sort_cmp
, acc
) < 0)
361 return isl_access_info_free(acc
);
366 /* Align the parameters of the two spaces if needed and then call
369 static __isl_give isl_space
*space_align_and_join(__isl_take isl_space
*left
,
370 __isl_take isl_space
*right
)
372 isl_bool equal_params
;
374 equal_params
= isl_space_has_equal_params(left
, right
);
375 if (equal_params
< 0)
378 return isl_space_join(left
, right
);
380 left
= isl_space_align_params(left
, isl_space_copy(right
));
381 right
= isl_space_align_params(right
, isl_space_copy(left
));
382 return isl_space_join(left
, right
);
384 isl_space_free(left
);
385 isl_space_free(right
);
389 /* Initialize an empty isl_flow structure corresponding to a given
390 * isl_access_info structure.
391 * For each must access, two dependences are created (initialized
392 * to the empty relation), one for the resulting must dependences
393 * and one for the resulting may dependences. May accesses can
394 * only lead to may dependences, so only one dependence is created
396 * This function is private as isl_flow structures are only supposed
397 * to be created by isl_access_info_compute_flow.
399 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
403 struct isl_flow
*dep
;
408 ctx
= isl_map_get_ctx(acc
->sink
.map
);
409 dep
= isl_calloc_type(ctx
, struct isl_flow
);
413 n
= 2 * acc
->n_must
+ acc
->n_may
;
414 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
419 for (i
= 0; i
< acc
->n_must
; ++i
) {
421 dim
= space_align_and_join(
422 isl_map_get_space(acc
->source
[i
].map
),
423 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
424 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
425 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
426 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
427 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
428 dep
->dep
[2 * i
].must
= 1;
429 dep
->dep
[2 * i
+ 1].must
= 0;
430 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
433 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
435 dim
= space_align_and_join(
436 isl_map_get_space(acc
->source
[i
].map
),
437 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
438 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
439 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
440 dep
->dep
[acc
->n_must
+ i
].must
= 0;
441 if (!dep
->dep
[acc
->n_must
+ i
].map
)
451 /* Iterate over all sources and for each resulting flow dependence
452 * that is not empty, call the user specfied function.
453 * The second argument in this function call identifies the source,
454 * while the third argument correspond to the final argument of
455 * the isl_flow_foreach call.
457 isl_stat
isl_flow_foreach(__isl_keep isl_flow
*deps
,
458 isl_stat (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
,
465 return isl_stat_error
;
467 for (i
= 0; i
< deps
->n_source
; ++i
) {
468 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
470 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
471 deps
->dep
[i
].data
, user
) < 0)
472 return isl_stat_error
;
478 /* Return a copy of the subset of the sink for which no source could be found.
480 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
486 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
488 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
491 void isl_flow_free(__isl_take isl_flow
*deps
)
497 isl_set_free(deps
->must_no_source
);
498 isl_set_free(deps
->may_no_source
);
500 for (i
= 0; i
< deps
->n_source
; ++i
)
501 isl_map_free(deps
->dep
[i
].map
);
507 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
509 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
512 /* Return a map that enforces that the domain iteration occurs after
513 * the range iteration at the given level.
514 * If level is odd, then the domain iteration should occur after
515 * the target iteration in their shared level/2 outermost loops.
516 * In this case we simply need to enforce that these outermost
517 * loop iterations are the same.
518 * If level is even, then the loop iterator of the domain should
519 * be greater than the loop iterator of the range at the last
520 * of the level/2 shared loops, i.e., loop level/2 - 1.
522 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
524 struct isl_basic_map
*bmap
;
527 bmap
= isl_basic_map_equal(dim
, level
/2);
529 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
531 return isl_map_from_basic_map(bmap
);
534 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
535 * but first check if the user has set acc->restrict_fn and if so
536 * update either the input or the output of the maximization problem
537 * with respect to the resulting restriction.
539 * Since the user expects a mapping from sink iterations to source iterations,
540 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
541 * to accessed array elements, we first need to project out the accessed
542 * sink array elements by applying acc->domain_map.
543 * Similarly, the sink restriction specified by the user needs to be
544 * converted back to the wrapped map.
546 static __isl_give isl_map
*restricted_partial_lexmax(
547 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
548 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
551 isl_restriction
*restr
;
552 isl_set
*sink_domain
;
556 if (!acc
->restrict_fn
)
557 return isl_map_partial_lexmax(dep
, sink
, empty
);
559 source_map
= isl_map_copy(dep
);
560 source_map
= isl_map_apply_domain(source_map
,
561 isl_map_copy(acc
->domain_map
));
562 sink_domain
= isl_set_copy(sink
);
563 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
564 restr
= acc
->restrict_fn(source_map
, sink_domain
,
565 acc
->source
[source
].data
, acc
->restrict_user
);
566 isl_set_free(sink_domain
);
567 isl_map_free(source_map
);
571 if (restr
->type
== isl_restriction_type_input
) {
572 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
573 sink_restr
= isl_set_copy(restr
->sink
);
574 sink_restr
= isl_set_apply(sink_restr
,
575 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
576 sink
= isl_set_intersect(sink
, sink_restr
);
577 } else if (restr
->type
== isl_restriction_type_empty
) {
578 isl_space
*space
= isl_map_get_space(dep
);
580 dep
= isl_map_empty(space
);
583 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
585 if (restr
->type
== isl_restriction_type_output
)
586 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
588 isl_restriction_free(restr
);
597 /* Compute the last iteration of must source j that precedes the sink
598 * at the given level for sink iterations in set_C.
599 * The subset of set_C for which no such iteration can be found is returned
602 static struct isl_map
*last_source(struct isl_access_info
*acc
,
603 struct isl_set
*set_C
,
604 int j
, int level
, struct isl_set
**empty
)
606 struct isl_map
*read_map
;
607 struct isl_map
*write_map
;
608 struct isl_map
*dep_map
;
609 struct isl_map
*after
;
610 struct isl_map
*result
;
612 read_map
= isl_map_copy(acc
->sink
.map
);
613 write_map
= isl_map_copy(acc
->source
[j
].map
);
614 write_map
= isl_map_reverse(write_map
);
615 dep_map
= isl_map_apply_range(read_map
, write_map
);
616 after
= after_at_level(isl_map_get_space(dep_map
), level
);
617 dep_map
= isl_map_intersect(dep_map
, after
);
618 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
619 result
= isl_map_reverse(result
);
624 /* For a given mapping between iterations of must source j and iterations
625 * of the sink, compute the last iteration of must source k preceding
626 * the sink at level before_level for any of the sink iterations,
627 * but following the corresponding iteration of must source j at level
630 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
631 struct isl_map
*old_map
,
632 int j
, int before_level
,
633 int k
, int after_level
,
634 struct isl_set
**empty
)
637 struct isl_set
*set_C
;
638 struct isl_map
*read_map
;
639 struct isl_map
*write_map
;
640 struct isl_map
*dep_map
;
641 struct isl_map
*after_write
;
642 struct isl_map
*before_read
;
643 struct isl_map
*result
;
645 set_C
= isl_map_range(isl_map_copy(old_map
));
646 read_map
= isl_map_copy(acc
->sink
.map
);
647 write_map
= isl_map_copy(acc
->source
[k
].map
);
649 write_map
= isl_map_reverse(write_map
);
650 dep_map
= isl_map_apply_range(read_map
, write_map
);
651 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
652 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
653 after_write
= after_at_level(dim
, after_level
);
654 after_write
= isl_map_apply_range(after_write
, old_map
);
655 after_write
= isl_map_reverse(after_write
);
656 dep_map
= isl_map_intersect(dep_map
, after_write
);
657 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
658 dep_map
= isl_map_intersect(dep_map
, before_read
);
659 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
660 result
= isl_map_reverse(result
);
665 /* Given a shared_level between two accesses, return 1 if the
666 * the first can precede the second at the requested target_level.
667 * If the target level is odd, i.e., refers to a statement level
668 * dimension, then first needs to precede second at the requested
669 * level, i.e., shared_level must be equal to target_level.
670 * If the target level is odd, then the two loops should share
671 * at least the requested number of outer loops.
673 static int can_precede_at_level(int shared_level
, int target_level
)
675 if (shared_level
< target_level
)
677 if ((target_level
% 2) && shared_level
> target_level
)
682 /* Given a possible flow dependence temp_rel[j] between source j and the sink
683 * at level sink_level, remove those elements for which
684 * there is an iteration of another source k < j that is closer to the sink.
685 * The flow dependences temp_rel[k] are updated with the improved sources.
686 * Any improved source needs to precede the sink at the same level
687 * and needs to follow source j at the same or a deeper level.
688 * The lower this level, the later the execution date of source k.
689 * We therefore consider lower levels first.
691 * If temp_rel[j] is empty, then there can be no improvement and
692 * we return immediately.
694 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
695 struct isl_map
**temp_rel
, int j
, int sink_level
)
698 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
700 if (isl_map_plain_is_empty(temp_rel
[j
]))
703 for (k
= j
- 1; k
>= 0; --k
) {
705 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
706 if (!can_precede_at_level(plevel
, sink_level
))
709 plevel2
= acc
->level_before(acc
->source
[j
].data
,
710 acc
->source
[k
].data
);
712 for (level
= sink_level
; level
<= depth
; ++level
) {
714 struct isl_set
*trest
;
715 struct isl_map
*copy
;
717 if (!can_precede_at_level(plevel2
, level
))
720 copy
= isl_map_copy(temp_rel
[j
]);
721 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
723 if (isl_map_plain_is_empty(T
)) {
728 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
729 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
736 /* Compute all iterations of may source j that precedes the sink at the given
737 * level for sink iterations in set_C.
739 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
740 __isl_take isl_set
*set_C
, int j
, int level
)
747 read_map
= isl_map_copy(acc
->sink
.map
);
748 read_map
= isl_map_intersect_domain(read_map
, set_C
);
749 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
750 write_map
= isl_map_reverse(write_map
);
751 dep_map
= isl_map_apply_range(read_map
, write_map
);
752 after
= after_at_level(isl_map_get_space(dep_map
), level
);
753 dep_map
= isl_map_intersect(dep_map
, after
);
755 return isl_map_reverse(dep_map
);
758 /* For a given mapping between iterations of must source k and iterations
759 * of the sink, compute all iterations of may source j preceding
760 * the sink at level before_level for any of the sink iterations,
761 * but following the corresponding iteration of must source k at level
764 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
765 __isl_take isl_map
*old_map
,
766 int j
, int before_level
, int k
, int after_level
)
773 isl_map
*after_write
;
774 isl_map
*before_read
;
776 set_C
= isl_map_range(isl_map_copy(old_map
));
777 read_map
= isl_map_copy(acc
->sink
.map
);
778 read_map
= isl_map_intersect_domain(read_map
, set_C
);
779 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
781 write_map
= isl_map_reverse(write_map
);
782 dep_map
= isl_map_apply_range(read_map
, write_map
);
783 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
784 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
785 after_write
= after_at_level(dim
, after_level
);
786 after_write
= isl_map_apply_range(after_write
, old_map
);
787 after_write
= isl_map_reverse(after_write
);
788 dep_map
= isl_map_intersect(dep_map
, after_write
);
789 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
790 dep_map
= isl_map_intersect(dep_map
, before_read
);
791 return isl_map_reverse(dep_map
);
794 /* Given the must and may dependence relations for the must accesses
795 * for level sink_level, check if there are any accesses of may access j
796 * that occur in between and return their union.
797 * If some of these accesses are intermediate with respect to
798 * (previously thought to be) must dependences, then these
799 * must dependences are turned into may dependences.
801 static __isl_give isl_map
*all_intermediate_sources(
802 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
803 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
804 int j
, int sink_level
)
807 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
810 for (k
= 0; k
< acc
->n_must
; ++k
) {
813 if (isl_map_plain_is_empty(may_rel
[k
]) &&
814 isl_map_plain_is_empty(must_rel
[k
]))
817 plevel
= acc
->level_before(acc
->source
[k
].data
,
818 acc
->source
[acc
->n_must
+ j
].data
);
820 for (level
= sink_level
; level
<= depth
; ++level
) {
825 if (!can_precede_at_level(plevel
, level
))
828 copy
= isl_map_copy(may_rel
[k
]);
829 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
830 map
= isl_map_union(map
, T
);
832 copy
= isl_map_copy(must_rel
[k
]);
833 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
834 ran
= isl_map_range(isl_map_copy(T
));
835 map
= isl_map_union(map
, T
);
836 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
837 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
839 T
= isl_map_from_domain_and_range(
841 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
843 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
850 /* Given a dependence relation "old_map" between a must-source and the sink,
851 * return a subset of the dependences, augmented with instances
852 * of the source at position "pos" in "acc" that are coscheduled
853 * with the must-source and that access the same element.
854 * That is, if the input lives in a space T -> K, then the output
855 * lives in the space [T -> S] -> K, with S the space of source "pos", and
856 * the domain factor of the domain product is a subset of the input.
857 * The sources are considered to be coscheduled if they have the same values
858 * for the initial "depth" coordinates.
860 * First construct a dependence relation S -> K and a mapping
861 * between coscheduled sources T -> S.
862 * The second is combined with the original dependence relation T -> K
863 * to form a relation in T -> [S -> K], which is subsequently
864 * uncurried to [T -> S] -> K.
865 * This result is then intersected with the dependence relation S -> K
866 * to form the output.
868 static __isl_give isl_map
*coscheduled_source(__isl_keep isl_access_info
*acc
,
869 __isl_keep isl_map
*old_map
, int pos
, int depth
)
879 set_C
= isl_map_range(isl_map_copy(old_map
));
880 read_map
= isl_map_copy(acc
->sink
.map
);
881 read_map
= isl_map_intersect_domain(read_map
, set_C
);
882 write_map
= isl_map_copy(acc
->source
[pos
].map
);
883 dep_map
= isl_map_domain_product(write_map
, read_map
);
884 dep_map
= isl_set_unwrap(isl_map_domain(dep_map
));
885 space
= isl_space_join(isl_map_get_space(old_map
),
886 isl_space_reverse(isl_map_get_space(dep_map
)));
887 equal
= isl_map_from_basic_map(isl_basic_map_equal(space
, depth
));
888 map
= isl_map_range_product(equal
, isl_map_copy(old_map
));
889 map
= isl_map_uncurry(map
);
890 map
= isl_map_intersect_domain_factor_range(map
, dep_map
);
895 /* After the dependences derived from a must-source have been computed
896 * at a certain level, check if any of the sources of the must-dependences
897 * may be coscheduled with other sources.
898 * If they are any such sources, then there is no way of determining
899 * which of the sources actually comes last and the must-dependences
900 * need to be turned into may-dependences, while dependences from
901 * the other sources need to be added to the may-dependences as well.
902 * "acc" describes the sources and a callback for checking whether
903 * two sources may be coscheduled. If acc->coscheduled is NULL then
904 * the sources are assumed not to be coscheduled.
905 * "must_rel" and "may_rel" describe the must and may-dependence relations
906 * computed at the current level for the must-sources. Some of the dependences
907 * may be moved from "must_rel" to "may_rel".
908 * "flow" contains all dependences computed so far (apart from those
909 * in "must_rel" and "may_rel") and may be updated with additional
910 * dependences derived from may-sources.
912 * In particular, consider all the must-sources with a non-empty
913 * dependence relation in "must_rel". They are considered in reverse
914 * order because that is the order in which they are considered in the caller.
915 * If any of the must-sources are coscheduled, then the last one
916 * is the one that will have a corresponding dependence relation.
917 * For each must-source i, consider both all the previous must-sources
918 * and all the may-sources. If any of those may be coscheduled with
919 * must-source i, then compute the coscheduled instances that access
920 * the same memory elements. The result is a relation [T -> S] -> K.
921 * The projection onto T -> K is a subset of the must-dependence relation
922 * that needs to be turned into may-dependences.
923 * The projection onto S -> K needs to be added to the may-dependences
925 * Since a given must-source instance may be coscheduled with several
926 * other source instances, the dependences that need to be turned
927 * into may-dependences are first collected and only actually removed
928 * from the must-dependences after all other sources have been considered.
930 static __isl_give isl_flow
*handle_coscheduled(__isl_keep isl_access_info
*acc
,
931 __isl_keep isl_map
**must_rel
, __isl_keep isl_map
**may_rel
,
932 __isl_take isl_flow
*flow
)
936 if (!acc
->coscheduled
)
938 for (i
= acc
->n_must
- 1; i
>= 0; --i
) {
941 if (isl_map_plain_is_empty(must_rel
[i
]))
943 move
= isl_map_empty(isl_map_get_space(must_rel
[i
]));
944 for (j
= i
- 1; j
>= 0; --j
) {
946 isl_map
*map
, *factor
;
948 if (!acc
->coscheduled(acc
->source
[i
].data
,
949 acc
->source
[j
].data
))
951 depth
= acc
->level_before(acc
->source
[i
].data
,
952 acc
->source
[j
].data
) / 2;
953 map
= coscheduled_source(acc
, must_rel
[i
], j
, depth
);
954 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
955 may_rel
[j
] = isl_map_union(may_rel
[j
], factor
);
956 map
= isl_map_domain_factor_domain(map
);
957 move
= isl_map_union(move
, map
);
959 for (j
= 0; j
< acc
->n_may
; ++j
) {
961 isl_map
*map
, *factor
;
963 pos
= acc
->n_must
+ j
;
964 if (!acc
->coscheduled(acc
->source
[i
].data
,
965 acc
->source
[pos
].data
))
967 depth
= acc
->level_before(acc
->source
[i
].data
,
968 acc
->source
[pos
].data
) / 2;
969 map
= coscheduled_source(acc
, must_rel
[i
], pos
, depth
);
970 factor
= isl_map_domain_factor_range(isl_map_copy(map
));
971 pos
= 2 * acc
->n_must
+ j
;
972 flow
->dep
[pos
].map
= isl_map_union(flow
->dep
[pos
].map
,
974 map
= isl_map_domain_factor_domain(map
);
975 move
= isl_map_union(move
, map
);
977 must_rel
[i
] = isl_map_subtract(must_rel
[i
], isl_map_copy(move
));
978 may_rel
[i
] = isl_map_union(may_rel
[i
], move
);
984 /* Compute dependences for the case where all accesses are "may"
985 * accesses, which boils down to computing memory based dependences.
986 * The generic algorithm would also work in this case, but it would
987 * be overkill to use it.
989 static __isl_give isl_flow
*compute_mem_based_dependences(
990 __isl_keep isl_access_info
*acc
)
997 res
= isl_flow_alloc(acc
);
1001 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1002 maydo
= isl_set_copy(mustdo
);
1004 for (i
= 0; i
< acc
->n_may
; ++i
) {
1011 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
1012 is_before
= plevel
& 1;
1015 dim
= isl_map_get_space(res
->dep
[i
].map
);
1017 before
= isl_map_lex_le_first(dim
, plevel
);
1019 before
= isl_map_lex_lt_first(dim
, plevel
);
1020 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
1021 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
1022 dep
= isl_map_intersect(dep
, before
);
1023 mustdo
= isl_set_subtract(mustdo
,
1024 isl_map_range(isl_map_copy(dep
)));
1025 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
1028 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
1029 res
->must_no_source
= mustdo
;
1034 /* Compute dependences for the case where there is at least one
1037 * The core algorithm considers all levels in which a source may precede
1038 * the sink, where a level may either be a statement level or a loop level.
1039 * The outermost statement level is 1, the first loop level is 2, etc...
1040 * The algorithm basically does the following:
1041 * for all levels l of the read access from innermost to outermost
1042 * for all sources w that may precede the sink access at that level
1043 * compute the last iteration of the source that precedes the sink access
1045 * add result to possible last accesses at level l of source w
1046 * for all sources w2 that we haven't considered yet at this level that may
1047 * also precede the sink access
1048 * for all levels l2 of w from l to innermost
1049 * for all possible last accesses dep of w at l
1050 * compute last iteration of w2 between the source and sink
1052 * add result to possible last accesses at level l of write w2
1053 * and replace possible last accesses dep by the remainder
1056 * The above algorithm is applied to the must access. During the course
1057 * of the algorithm, we keep track of sink iterations that still
1058 * need to be considered. These iterations are split into those that
1059 * haven't been matched to any source access (mustdo) and those that have only
1060 * been matched to may accesses (maydo).
1061 * At the end of each level, must-sources and may-sources that are coscheduled
1062 * with the sources of the must-dependences at that level are considered.
1063 * If any coscheduled instances are found, then corresponding may-dependences
1064 * are added and the original must-dependences are turned into may-dependences.
1065 * Afterwards, the may accesses that occur after must-dependence sources
1067 * In particular, we consider may accesses that precede the remaining
1068 * sink iterations, moving elements from mustdo to maydo when appropriate,
1069 * and may accesses that occur between a must source and a sink of any
1070 * dependences found at the current level, turning must dependences into
1071 * may dependences when appropriate.
1074 static __isl_give isl_flow
*compute_val_based_dependences(
1075 __isl_keep isl_access_info
*acc
)
1079 isl_set
*mustdo
= NULL
;
1080 isl_set
*maydo
= NULL
;
1083 isl_map
**must_rel
= NULL
;
1084 isl_map
**may_rel
= NULL
;
1089 res
= isl_flow_alloc(acc
);
1092 ctx
= isl_map_get_ctx(acc
->sink
.map
);
1094 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
1095 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
1096 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
1097 if (!mustdo
|| !maydo
)
1099 if (isl_set_plain_is_empty(mustdo
))
1102 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1103 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
1104 if (!must_rel
|| !may_rel
)
1107 for (level
= depth
; level
>= 1; --level
) {
1108 for (j
= acc
->n_must
-1; j
>=0; --j
) {
1110 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
1111 must_rel
[j
] = isl_map_empty(space
);
1112 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
1115 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1117 struct isl_set
*rest
;
1120 plevel
= acc
->level_before(acc
->source
[j
].data
,
1122 if (!can_precede_at_level(plevel
, level
))
1125 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
1126 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
1129 intermediate_sources(acc
, must_rel
, j
, level
);
1131 T
= last_source(acc
, maydo
, j
, level
, &rest
);
1132 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
1135 intermediate_sources(acc
, may_rel
, j
, level
);
1137 if (isl_set_plain_is_empty(mustdo
) &&
1138 isl_set_plain_is_empty(maydo
))
1141 for (j
= j
- 1; j
>= 0; --j
) {
1144 plevel
= acc
->level_before(acc
->source
[j
].data
,
1146 if (!can_precede_at_level(plevel
, level
))
1149 intermediate_sources(acc
, must_rel
, j
, level
);
1150 intermediate_sources(acc
, may_rel
, j
, level
);
1153 handle_coscheduled(acc
, must_rel
, may_rel
, res
);
1155 for (j
= 0; j
< acc
->n_may
; ++j
) {
1160 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1162 if (!can_precede_at_level(plevel
, level
))
1165 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1166 res
->dep
[2 * acc
->n_must
+ j
].map
=
1167 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1168 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1169 ran
= isl_map_range(isl_map_copy(T
));
1170 res
->dep
[2 * acc
->n_must
+ j
].map
=
1171 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1172 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1173 maydo
= isl_set_union_disjoint(maydo
, ran
);
1175 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1176 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1178 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1181 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1182 res
->dep
[2 * j
].map
=
1183 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1185 res
->dep
[2 * j
+ 1].map
=
1186 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1190 if (isl_set_plain_is_empty(mustdo
) &&
1191 isl_set_plain_is_empty(maydo
))
1198 res
->must_no_source
= mustdo
;
1199 res
->may_no_source
= maydo
;
1203 isl_set_free(mustdo
);
1204 isl_set_free(maydo
);
1210 /* Given a "sink" access, a list of n "source" accesses,
1211 * compute for each iteration of the sink access
1212 * and for each element accessed by that iteration,
1213 * the source access in the list that last accessed the
1214 * element accessed by the sink access before this sink access.
1215 * Each access is given as a map from the loop iterators
1216 * to the array indices.
1217 * The result is a list of n relations between source and sink
1218 * iterations and a subset of the domain of the sink access,
1219 * corresponding to those iterations that access an element
1220 * not previously accessed.
1222 * To deal with multi-valued sink access relations, the sink iteration
1223 * domain is first extended with dimensions that correspond to the data
1224 * space. However, these extra dimensions are not projected out again.
1225 * It is up to the caller to decide whether these dimensions should be kept.
1227 static __isl_give isl_flow
*access_info_compute_flow_core(
1228 __isl_take isl_access_info
*acc
)
1230 struct isl_flow
*res
= NULL
;
1235 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1239 if (acc
->n_must
== 0)
1240 res
= compute_mem_based_dependences(acc
);
1242 acc
= isl_access_info_sort_sources(acc
);
1243 res
= compute_val_based_dependences(acc
);
1245 acc
= isl_access_info_free(acc
);
1248 if (!res
->must_no_source
|| !res
->may_no_source
)
1252 isl_access_info_free(acc
);
1257 /* Given a "sink" access, a list of n "source" accesses,
1258 * compute for each iteration of the sink access
1259 * and for each element accessed by that iteration,
1260 * the source access in the list that last accessed the
1261 * element accessed by the sink access before this sink access.
1262 * Each access is given as a map from the loop iterators
1263 * to the array indices.
1264 * The result is a list of n relations between source and sink
1265 * iterations and a subset of the domain of the sink access,
1266 * corresponding to those iterations that access an element
1267 * not previously accessed.
1269 * To deal with multi-valued sink access relations,
1270 * access_info_compute_flow_core extends the sink iteration domain
1271 * with dimensions that correspond to the data space. These extra dimensions
1272 * are projected out from the result of access_info_compute_flow_core.
1274 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1277 struct isl_flow
*res
;
1282 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1283 res
= access_info_compute_flow_core(acc
);
1287 for (j
= 0; j
< res
->n_source
; ++j
) {
1288 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1289 if (!res
->dep
[j
].map
)
1300 /* Keep track of some information about a schedule for a given
1301 * access. In particular, keep track of which dimensions
1302 * have a constant value and of the actual constant values.
1304 struct isl_sched_info
{
1309 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1313 isl_vec_free(info
->cst
);
1318 /* Extract information on the constant dimensions of the schedule
1319 * for a given access. The "map" is of the form
1323 * with S the schedule domain, D the iteration domain and A the data domain.
1325 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1326 __isl_keep isl_map
*map
)
1330 struct isl_sched_info
*info
;
1336 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1339 n
= isl_space_dim(dim
, isl_dim_in
);
1340 isl_space_free(dim
);
1342 ctx
= isl_map_get_ctx(map
);
1343 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1346 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1347 info
->cst
= isl_vec_alloc(ctx
, n
);
1348 if (n
&& (!info
->is_cst
|| !info
->cst
))
1351 for (i
= 0; i
< n
; ++i
) {
1354 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1357 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1358 if (info
->is_cst
[i
])
1359 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1366 sched_info_free(info
);
1370 /* The different types of access relations that isl_union_access_info
1373 * "isl_access_sink" represents the sink accesses.
1374 * "isl_access_must_source" represents the definite source accesses.
1375 * "isl_access_may_source" represents the possible source accesses.
1376 * "isl_access_kill" represents the kills.
1378 * isl_access_sink is sometimes treated differently and
1379 * should therefore appear first.
1381 enum isl_access_type
{
1383 isl_access_must_source
,
1384 isl_access_may_source
,
1389 /* This structure represents the input for a dependence analysis computation.
1391 * "access" contains the access relations.
1393 * "schedule" or "schedule_map" represents the execution order.
1394 * Exactly one of these fields should be NULL. The other field
1395 * determines the execution order.
1397 * The domains of these four maps refer to the same iteration spaces(s).
1398 * The ranges of the first three maps also refer to the same data space(s).
1400 * After a call to isl_union_access_info_introduce_schedule,
1401 * the "schedule_map" field no longer contains useful information.
1403 struct isl_union_access_info
{
1404 isl_union_map
*access
[isl_access_end
];
1406 isl_schedule
*schedule
;
1407 isl_union_map
*schedule_map
;
1410 /* Free "access" and return NULL.
1412 __isl_null isl_union_access_info
*isl_union_access_info_free(
1413 __isl_take isl_union_access_info
*access
)
1415 enum isl_access_type i
;
1420 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1421 isl_union_map_free(access
->access
[i
]);
1422 isl_schedule_free(access
->schedule
);
1423 isl_union_map_free(access
->schedule_map
);
1429 /* Return the isl_ctx to which "access" belongs.
1431 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1435 return isl_union_map_get_ctx(access
->access
[isl_access_sink
]);
1438 /* Construct an empty (invalid) isl_union_access_info object.
1439 * The caller is responsible for setting the sink access relation and
1440 * initializing all the other fields, e.g., by calling
1441 * isl_union_access_info_init.
1443 static __isl_give isl_union_access_info
*isl_union_access_info_alloc(
1446 return isl_calloc_type(ctx
, isl_union_access_info
);
1449 /* Initialize all the fields of "info", except the sink access relation,
1450 * which is assumed to have been set by the caller.
1452 * By default, we use the schedule field of the isl_union_access_info,
1453 * but this may be overridden by a call
1454 * to isl_union_access_info_set_schedule_map.
1456 static __isl_give isl_union_access_info
*isl_union_access_info_init(
1457 __isl_take isl_union_access_info
*info
)
1460 isl_union_map
*empty
;
1461 enum isl_access_type i
;
1465 if (!info
->access
[isl_access_sink
])
1466 return isl_union_access_info_free(info
);
1468 space
= isl_union_map_get_space(info
->access
[isl_access_sink
]);
1469 empty
= isl_union_map_empty(isl_space_copy(space
));
1470 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1471 if (!info
->access
[i
])
1472 info
->access
[i
] = isl_union_map_copy(empty
);
1473 isl_union_map_free(empty
);
1474 if (!info
->schedule
&& !info
->schedule_map
)
1475 info
->schedule
= isl_schedule_empty(isl_space_copy(space
));
1476 isl_space_free(space
);
1478 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1479 if (!info
->access
[i
])
1480 return isl_union_access_info_free(info
);
1481 if (!info
->schedule
&& !info
->schedule_map
)
1482 return isl_union_access_info_free(info
);
1487 /* Create a new isl_union_access_info with the given sink accesses and
1488 * and no other accesses or schedule information.
1490 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1491 __isl_take isl_union_map
*sink
)
1494 isl_union_access_info
*access
;
1498 ctx
= isl_union_map_get_ctx(sink
);
1499 access
= isl_union_access_info_alloc(ctx
);
1502 access
->access
[isl_access_sink
] = sink
;
1503 return isl_union_access_info_init(access
);
1505 isl_union_map_free(sink
);
1509 /* Replace the access relation of type "type" of "info" by "access".
1511 static __isl_give isl_union_access_info
*isl_union_access_info_set(
1512 __isl_take isl_union_access_info
*info
,
1513 enum isl_access_type type
, __isl_take isl_union_map
*access
)
1515 if (!info
|| !access
)
1518 isl_union_map_free(info
->access
[type
]);
1519 info
->access
[type
] = access
;
1523 isl_union_access_info_free(info
);
1524 isl_union_map_free(access
);
1528 /* Replace the definite source accesses of "access" by "must_source".
1530 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1531 __isl_take isl_union_access_info
*access
,
1532 __isl_take isl_union_map
*must_source
)
1534 return isl_union_access_info_set(access
, isl_access_must_source
,
1538 /* Replace the possible source accesses of "access" by "may_source".
1540 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1541 __isl_take isl_union_access_info
*access
,
1542 __isl_take isl_union_map
*may_source
)
1544 return isl_union_access_info_set(access
, isl_access_may_source
,
1548 /* Replace the kills of "info" by "kill".
1550 __isl_give isl_union_access_info
*isl_union_access_info_set_kill(
1551 __isl_take isl_union_access_info
*info
, __isl_take isl_union_map
*kill
)
1553 return isl_union_access_info_set(info
, isl_access_kill
, kill
);
1556 /* Return the access relation of type "type" of "info".
1558 static __isl_give isl_union_map
*isl_union_access_info_get(
1559 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1563 return isl_union_map_copy(info
->access
[type
]);
1566 /* Return the definite source accesses of "info".
1568 __isl_give isl_union_map
*isl_union_access_info_get_must_source(
1569 __isl_keep isl_union_access_info
*info
)
1571 return isl_union_access_info_get(info
, isl_access_must_source
);
1574 /* Return the possible source accesses of "info".
1576 __isl_give isl_union_map
*isl_union_access_info_get_may_source(
1577 __isl_keep isl_union_access_info
*info
)
1579 return isl_union_access_info_get(info
, isl_access_may_source
);
1582 /* Return the kills of "info".
1584 __isl_give isl_union_map
*isl_union_access_info_get_kill(
1585 __isl_keep isl_union_access_info
*info
)
1587 return isl_union_access_info_get(info
, isl_access_kill
);
1590 /* Does "info" specify any kills?
1592 static isl_bool
isl_union_access_has_kill(
1593 __isl_keep isl_union_access_info
*info
)
1598 return isl_bool_error
;
1599 empty
= isl_union_map_is_empty(info
->access
[isl_access_kill
]);
1600 return isl_bool_not(empty
);
1603 /* Replace the schedule of "access" by "schedule".
1604 * Also free the schedule_map in case it was set last.
1606 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1607 __isl_take isl_union_access_info
*access
,
1608 __isl_take isl_schedule
*schedule
)
1610 if (!access
|| !schedule
)
1613 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1614 isl_schedule_free(access
->schedule
);
1615 access
->schedule
= schedule
;
1619 isl_union_access_info_free(access
);
1620 isl_schedule_free(schedule
);
1624 /* Replace the schedule map of "access" by "schedule_map".
1625 * Also free the schedule in case it was set last.
1627 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1628 __isl_take isl_union_access_info
*access
,
1629 __isl_take isl_union_map
*schedule_map
)
1631 if (!access
|| !schedule_map
)
1634 isl_union_map_free(access
->schedule_map
);
1635 access
->schedule
= isl_schedule_free(access
->schedule
);
1636 access
->schedule_map
= schedule_map
;
1640 isl_union_access_info_free(access
);
1641 isl_union_map_free(schedule_map
);
1645 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1646 __isl_keep isl_union_access_info
*access
)
1648 isl_union_access_info
*copy
;
1649 enum isl_access_type i
;
1653 copy
= isl_union_access_info_from_sink(
1654 isl_union_map_copy(access
->access
[isl_access_sink
]));
1655 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1656 copy
= isl_union_access_info_set(copy
, i
,
1657 isl_union_map_copy(access
->access
[i
]));
1658 if (access
->schedule
)
1659 copy
= isl_union_access_info_set_schedule(copy
,
1660 isl_schedule_copy(access
->schedule
));
1662 copy
= isl_union_access_info_set_schedule_map(copy
,
1663 isl_union_map_copy(access
->schedule_map
));
1668 /* Print a key-value pair of a YAML mapping to "p",
1669 * with key "name" and value "umap".
1671 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1672 const char *name
, __isl_keep isl_union_map
*umap
)
1674 p
= isl_printer_print_str(p
, name
);
1675 p
= isl_printer_yaml_next(p
);
1676 p
= isl_printer_print_str(p
, "\"");
1677 p
= isl_printer_print_union_map(p
, umap
);
1678 p
= isl_printer_print_str(p
, "\"");
1679 p
= isl_printer_yaml_next(p
);
1684 /* An enumeration of the various keys that may appear in a YAML mapping
1685 * of an isl_union_access_info object.
1686 * The keys for the access relation types are assumed to have the same values
1687 * as the access relation types in isl_access_type.
1690 isl_ai_key_error
= -1,
1691 isl_ai_key_sink
= isl_access_sink
,
1692 isl_ai_key_must_source
= isl_access_must_source
,
1693 isl_ai_key_may_source
= isl_access_may_source
,
1694 isl_ai_key_kill
= isl_access_kill
,
1695 isl_ai_key_schedule_map
,
1696 isl_ai_key_schedule
,
1700 /* Textual representations of the YAML keys for an isl_union_access_info
1703 static char *key_str
[] = {
1704 [isl_ai_key_sink
] = "sink",
1705 [isl_ai_key_must_source
] = "must_source",
1706 [isl_ai_key_may_source
] = "may_source",
1707 [isl_ai_key_kill
] = "kill",
1708 [isl_ai_key_schedule_map
] = "schedule_map",
1709 [isl_ai_key_schedule
] = "schedule",
1712 /* Print a key-value pair corresponding to the access relation of type "type"
1713 * of a YAML mapping of "info" to "p".
1715 * The sink access relation is always printed, but any other access relation
1716 * is only printed if it is non-empty.
1718 static __isl_give isl_printer
*print_access_field(__isl_take isl_printer
*p
,
1719 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1721 if (type
!= isl_access_sink
) {
1724 empty
= isl_union_map_is_empty(info
->access
[type
]);
1726 return isl_printer_free(p
);
1730 return print_union_map_field(p
, key_str
[type
], info
->access
[type
]);
1733 /* Print the information contained in "access" to "p".
1734 * The information is printed as a YAML document.
1736 __isl_give isl_printer
*isl_printer_print_union_access_info(
1737 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1739 enum isl_access_type i
;
1742 return isl_printer_free(p
);
1744 p
= isl_printer_yaml_start_mapping(p
);
1745 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1746 p
= print_access_field(p
, access
, i
);
1747 if (access
->schedule
) {
1748 p
= isl_printer_print_str(p
, key_str
[isl_ai_key_schedule
]);
1749 p
= isl_printer_yaml_next(p
);
1750 p
= isl_printer_print_schedule(p
, access
->schedule
);
1751 p
= isl_printer_yaml_next(p
);
1753 p
= print_union_map_field(p
, key_str
[isl_ai_key_schedule_map
],
1754 access
->schedule_map
);
1756 p
= isl_printer_yaml_end_mapping(p
);
1761 /* Return a string representation of the information in "access".
1762 * The information is printed in flow format.
1764 __isl_give
char *isl_union_access_info_to_str(
1765 __isl_keep isl_union_access_info
*access
)
1773 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1774 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1775 p
= isl_printer_print_union_access_info(p
, access
);
1776 s
= isl_printer_get_str(p
);
1777 isl_printer_free(p
);
1783 #define KEY enum isl_ai_key
1785 #define KEY_ERROR isl_ai_key_error
1787 #define KEY_END isl_ai_key_end
1788 #include "extract_key.c"
1791 #define BASE union_map
1792 #include "read_in_string_templ.c"
1794 /* Read an isl_union_access_info object from "s".
1796 * Start off with an empty (invalid) isl_union_access_info object and
1797 * then fill up the fields based on the input.
1798 * The input needs to contain at least a description of the sink
1799 * access relation as well as some form of schedule.
1800 * The other access relations are set to empty relations
1801 * by isl_union_access_info_init if they are not specified in the input.
1803 __isl_give isl_union_access_info
*isl_stream_read_union_access_info(
1807 isl_union_access_info
*info
;
1810 int schedule_set
= 0;
1812 if (isl_stream_yaml_read_start_mapping(s
))
1815 ctx
= isl_stream_get_ctx(s
);
1816 info
= isl_union_access_info_alloc(ctx
);
1817 while ((more
= isl_stream_yaml_next(s
)) > 0) {
1818 enum isl_ai_key key
;
1819 isl_union_map
*access
, *schedule_map
;
1820 isl_schedule
*schedule
;
1823 if (isl_stream_yaml_next(s
) < 0)
1824 return isl_union_access_info_free(info
);
1826 case isl_ai_key_end
:
1827 case isl_ai_key_error
:
1828 return isl_union_access_info_free(info
);
1829 case isl_ai_key_sink
:
1831 case isl_ai_key_must_source
:
1832 case isl_ai_key_may_source
:
1833 case isl_ai_key_kill
:
1834 access
= read_union_map(s
);
1835 info
= isl_union_access_info_set(info
, key
, access
);
1839 case isl_ai_key_schedule_map
:
1841 schedule_map
= read_union_map(s
);
1842 info
= isl_union_access_info_set_schedule_map(info
,
1847 case isl_ai_key_schedule
:
1849 schedule
= isl_stream_read_schedule(s
);
1850 info
= isl_union_access_info_set_schedule(info
,
1858 return isl_union_access_info_free(info
);
1860 if (isl_stream_yaml_read_end_mapping(s
) < 0) {
1861 isl_stream_error(s
, NULL
, "unexpected extra elements");
1862 return isl_union_access_info_free(info
);
1866 isl_stream_error(s
, NULL
, "no sink specified");
1867 return isl_union_access_info_free(info
);
1870 if (!schedule_set
) {
1871 isl_stream_error(s
, NULL
, "no schedule specified");
1872 return isl_union_access_info_free(info
);
1875 return isl_union_access_info_init(info
);
1878 /* Read an isl_union_access_info object from the file "input".
1880 __isl_give isl_union_access_info
*isl_union_access_info_read_from_file(
1881 isl_ctx
*ctx
, FILE *input
)
1884 isl_union_access_info
*access
;
1886 s
= isl_stream_new_file(ctx
, input
);
1889 access
= isl_stream_read_union_access_info(s
);
1895 /* Update the fields of "access" such that they all have the same parameters,
1896 * keeping in mind that the schedule_map field may be NULL and ignoring
1897 * the schedule field.
1899 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1900 __isl_take isl_union_access_info
*access
)
1903 enum isl_access_type i
;
1908 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
1909 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1910 space
= isl_space_align_params(space
,
1911 isl_union_map_get_space(access
->access
[i
]));
1912 if (access
->schedule_map
)
1913 space
= isl_space_align_params(space
,
1914 isl_union_map_get_space(access
->schedule_map
));
1915 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1917 isl_union_map_align_params(access
->access
[i
],
1918 isl_space_copy(space
));
1919 if (!access
->schedule_map
) {
1920 isl_space_free(space
);
1922 access
->schedule_map
=
1923 isl_union_map_align_params(access
->schedule_map
, space
);
1924 if (!access
->schedule_map
)
1925 return isl_union_access_info_free(access
);
1928 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1929 if (!access
->access
[i
])
1930 return isl_union_access_info_free(access
);
1935 /* Prepend the schedule dimensions to the iteration domains.
1937 * That is, if the schedule is of the form
1941 * while the access relations are of the form
1945 * then the updated access relations are of the form
1949 * The schedule map is also replaced by the map
1953 * that is used during the internal computation.
1954 * Neither the original schedule map nor this updated schedule map
1955 * are used after the call to this function.
1957 static __isl_give isl_union_access_info
*
1958 isl_union_access_info_introduce_schedule(
1959 __isl_take isl_union_access_info
*access
)
1962 enum isl_access_type i
;
1967 sm
= isl_union_map_reverse(access
->schedule_map
);
1968 sm
= isl_union_map_range_map(sm
);
1969 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1971 isl_union_map_apply_range(isl_union_map_copy(sm
),
1973 access
->schedule_map
= sm
;
1975 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1976 if (!access
->access
[i
])
1977 return isl_union_access_info_free(access
);
1978 if (!access
->schedule_map
)
1979 return isl_union_access_info_free(access
);
1984 /* This structure represents the result of a dependence analysis computation.
1986 * "must_dep" represents the full definite dependences
1987 * "may_dep" represents the full non-definite dependences.
1988 * Both are of the form
1990 * [Source] -> [[Sink -> Data]]
1992 * (after the schedule dimensions have been projected out).
1993 * "must_no_source" represents the subset of the sink accesses for which
1994 * definitely no source was found.
1995 * "may_no_source" represents the subset of the sink accesses for which
1996 * possibly, but not definitely, no source was found.
1998 struct isl_union_flow
{
1999 isl_union_map
*must_dep
;
2000 isl_union_map
*may_dep
;
2001 isl_union_map
*must_no_source
;
2002 isl_union_map
*may_no_source
;
2005 /* Return the isl_ctx to which "flow" belongs.
2007 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
2009 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
2012 /* Free "flow" and return NULL.
2014 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
2018 isl_union_map_free(flow
->must_dep
);
2019 isl_union_map_free(flow
->may_dep
);
2020 isl_union_map_free(flow
->must_no_source
);
2021 isl_union_map_free(flow
->may_no_source
);
2026 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
2031 fprintf(stderr
, "must dependences: ");
2032 isl_union_map_dump(flow
->must_dep
);
2033 fprintf(stderr
, "may dependences: ");
2034 isl_union_map_dump(flow
->may_dep
);
2035 fprintf(stderr
, "must no source: ");
2036 isl_union_map_dump(flow
->must_no_source
);
2037 fprintf(stderr
, "may no source: ");
2038 isl_union_map_dump(flow
->may_no_source
);
2041 /* Return the full definite dependences in "flow", with accessed elements.
2043 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
2044 __isl_keep isl_union_flow
*flow
)
2048 return isl_union_map_copy(flow
->must_dep
);
2051 /* Return the full possible dependences in "flow", including the definite
2052 * dependences, with accessed elements.
2054 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
2055 __isl_keep isl_union_flow
*flow
)
2059 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2060 isl_union_map_copy(flow
->may_dep
));
2063 /* Return the definite dependences in "flow", without the accessed elements.
2065 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
2066 __isl_keep isl_union_flow
*flow
)
2072 dep
= isl_union_map_copy(flow
->must_dep
);
2073 return isl_union_map_range_factor_domain(dep
);
2076 /* Return the possible dependences in "flow", including the definite
2077 * dependences, without the accessed elements.
2079 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
2080 __isl_keep isl_union_flow
*flow
)
2086 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
2087 isl_union_map_copy(flow
->may_dep
));
2088 return isl_union_map_range_factor_domain(dep
);
2091 /* Return the non-definite dependences in "flow".
2093 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
2094 __isl_keep isl_union_flow
*flow
)
2098 return isl_union_map_copy(flow
->may_dep
);
2101 /* Return the subset of the sink accesses for which definitely
2102 * no source was found.
2104 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
2105 __isl_keep isl_union_flow
*flow
)
2109 return isl_union_map_copy(flow
->must_no_source
);
2112 /* Return the subset of the sink accesses for which possibly
2113 * no source was found, including those for which definitely
2114 * no source was found.
2116 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
2117 __isl_keep isl_union_flow
*flow
)
2121 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
2122 isl_union_map_copy(flow
->may_no_source
));
2125 /* Return the subset of the sink accesses for which possibly, but not
2126 * definitely, no source was found.
2128 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
2129 __isl_keep isl_union_flow
*flow
)
2133 return isl_union_map_copy(flow
->may_no_source
);
2136 /* Create a new isl_union_flow object, initialized with empty
2137 * dependence relations and sink subsets.
2139 static __isl_give isl_union_flow
*isl_union_flow_alloc(
2140 __isl_take isl_space
*space
)
2143 isl_union_map
*empty
;
2144 isl_union_flow
*flow
;
2148 ctx
= isl_space_get_ctx(space
);
2149 flow
= isl_alloc_type(ctx
, isl_union_flow
);
2153 empty
= isl_union_map_empty(space
);
2154 flow
->must_dep
= isl_union_map_copy(empty
);
2155 flow
->may_dep
= isl_union_map_copy(empty
);
2156 flow
->must_no_source
= isl_union_map_copy(empty
);
2157 flow
->may_no_source
= empty
;
2159 if (!flow
->must_dep
|| !flow
->may_dep
||
2160 !flow
->must_no_source
|| !flow
->may_no_source
)
2161 return isl_union_flow_free(flow
);
2165 isl_space_free(space
);
2169 /* Copy this isl_union_flow object.
2171 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
2173 isl_union_flow
*copy
;
2178 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
2183 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
2184 isl_union_map_copy(flow
->must_dep
));
2185 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
2186 isl_union_map_copy(flow
->may_dep
));
2187 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
2188 isl_union_map_copy(flow
->must_no_source
));
2189 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
2190 isl_union_map_copy(flow
->may_no_source
));
2192 if (!copy
->must_dep
|| !copy
->may_dep
||
2193 !copy
->must_no_source
|| !copy
->may_no_source
)
2194 return isl_union_flow_free(copy
);
2199 /* Drop the schedule dimensions from the iteration domains in "flow".
2200 * In particular, the schedule dimensions have been prepended
2201 * to the iteration domains prior to the dependence analysis by
2202 * replacing the iteration domain D, by the wrapped map [S -> D].
2203 * Replace these wrapped maps by the original D.
2205 * In particular, the dependences computed by access_info_compute_flow_core
2208 * [S -> D] -> [[S' -> D'] -> A]
2210 * The schedule dimensions are projected out by first currying the range,
2213 * [S -> D] -> [S' -> [D' -> A]]
2215 * and then computing the factor range
2219 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
2220 __isl_take isl_union_flow
*flow
)
2225 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
2226 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
2227 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
2228 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
2229 flow
->must_no_source
=
2230 isl_union_map_domain_factor_range(flow
->must_no_source
);
2231 flow
->may_no_source
=
2232 isl_union_map_domain_factor_range(flow
->may_no_source
);
2234 if (!flow
->must_dep
|| !flow
->may_dep
||
2235 !flow
->must_no_source
|| !flow
->may_no_source
)
2236 return isl_union_flow_free(flow
);
2241 struct isl_compute_flow_data
{
2242 isl_union_map
*must_source
;
2243 isl_union_map
*may_source
;
2244 isl_union_flow
*flow
;
2249 struct isl_sched_info
*sink_info
;
2250 struct isl_sched_info
**source_info
;
2251 isl_access_info
*accesses
;
2254 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
2258 struct isl_compute_flow_data
*data
;
2260 data
= (struct isl_compute_flow_data
*)user
;
2262 dim
= isl_space_range(isl_map_get_space(map
));
2264 eq
= isl_space_is_equal(dim
, data
->dim
);
2266 isl_space_free(dim
);
2270 return isl_stat_error
;
2277 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
2281 struct isl_sched_info
*info
;
2282 struct isl_compute_flow_data
*data
;
2284 data
= (struct isl_compute_flow_data
*)user
;
2286 dim
= isl_space_range(isl_map_get_space(map
));
2288 eq
= isl_space_is_equal(dim
, data
->dim
);
2290 isl_space_free(dim
);
2299 info
= sched_info_alloc(map
);
2300 data
->source_info
[data
->count
] = info
;
2302 data
->accesses
= isl_access_info_add_source(data
->accesses
,
2303 map
, data
->must
, info
);
2310 return isl_stat_error
;
2313 /* Determine the shared nesting level and the "textual order" of
2314 * the given accesses.
2316 * We first determine the minimal schedule dimension for both accesses.
2318 * If among those dimensions, we can find one where both have a fixed
2319 * value and if moreover those values are different, then the previous
2320 * dimension is the last shared nesting level and the textual order
2321 * is determined based on the order of the fixed values.
2322 * If no such fixed values can be found, then we set the shared
2323 * nesting level to the minimal schedule dimension, with no textual ordering.
2325 static int before(void *first
, void *second
)
2327 struct isl_sched_info
*info1
= first
;
2328 struct isl_sched_info
*info2
= second
;
2332 n1
= isl_vec_size(info1
->cst
);
2333 n2
= isl_vec_size(info2
->cst
);
2338 for (i
= 0; i
< n1
; ++i
) {
2342 if (!info1
->is_cst
[i
])
2344 if (!info2
->is_cst
[i
])
2346 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2350 r
= 2 * i
+ (cmp
< 0);
2358 /* Check if the given two accesses may be coscheduled.
2359 * If so, return 1. Otherwise return 0.
2361 * Two accesses may only be coscheduled if the fixed schedule
2362 * coordinates have the same values.
2364 static int coscheduled(void *first
, void *second
)
2366 struct isl_sched_info
*info1
= first
;
2367 struct isl_sched_info
*info2
= second
;
2371 n1
= isl_vec_size(info1
->cst
);
2372 n2
= isl_vec_size(info2
->cst
);
2377 for (i
= 0; i
< n1
; ++i
) {
2380 if (!info1
->is_cst
[i
])
2382 if (!info2
->is_cst
[i
])
2384 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2392 /* Given a sink access, look for all the source accesses that access
2393 * the same array and perform dataflow analysis on them using
2394 * isl_access_info_compute_flow_core.
2396 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
2400 struct isl_compute_flow_data
*data
;
2404 data
= (struct isl_compute_flow_data
*)user
;
2407 ctx
= isl_map_get_ctx(map
);
2409 data
->accesses
= NULL
;
2410 data
->sink_info
= NULL
;
2411 data
->source_info
= NULL
;
2413 data
->dim
= isl_space_range(isl_map_get_space(map
));
2415 if (isl_union_map_foreach_map(data
->must_source
,
2416 &count_matching_array
, data
) < 0)
2418 if (isl_union_map_foreach_map(data
->may_source
,
2419 &count_matching_array
, data
) < 0)
2422 data
->sink_info
= sched_info_alloc(map
);
2423 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
2426 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
2427 data
->sink_info
, &before
, data
->count
);
2428 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
2431 data
->accesses
->coscheduled
= &coscheduled
;
2434 if (isl_union_map_foreach_map(data
->must_source
,
2435 &collect_matching_array
, data
) < 0)
2438 if (isl_union_map_foreach_map(data
->may_source
,
2439 &collect_matching_array
, data
) < 0)
2442 flow
= access_info_compute_flow_core(data
->accesses
);
2443 data
->accesses
= NULL
;
2448 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
2449 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
2450 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2451 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2453 for (i
= 0; i
< flow
->n_source
; ++i
) {
2455 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2456 if (flow
->dep
[i
].must
)
2457 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2459 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2462 isl_flow_free(flow
);
2464 sched_info_free(data
->sink_info
);
2465 if (data
->source_info
) {
2466 for (i
= 0; i
< data
->count
; ++i
)
2467 sched_info_free(data
->source_info
[i
]);
2468 free(data
->source_info
);
2470 isl_space_free(data
->dim
);
2475 isl_access_info_free(data
->accesses
);
2476 sched_info_free(data
->sink_info
);
2477 if (data
->source_info
) {
2478 for (i
= 0; i
< data
->count
; ++i
)
2479 sched_info_free(data
->source_info
[i
]);
2480 free(data
->source_info
);
2482 isl_space_free(data
->dim
);
2485 return isl_stat_error
;
2488 /* Add the kills of "info" to the must-sources.
2490 static __isl_give isl_union_access_info
*
2491 isl_union_access_info_add_kill_to_must_source(
2492 __isl_take isl_union_access_info
*info
)
2494 isl_union_map
*must
, *kill
;
2496 must
= isl_union_access_info_get_must_source(info
);
2497 kill
= isl_union_access_info_get_kill(info
);
2498 must
= isl_union_map_union(must
, kill
);
2499 return isl_union_access_info_set_must_source(info
, must
);
2502 /* Drop dependences from "flow" that purely originate from kills.
2503 * That is, only keep those dependences that originate from
2504 * the original must-sources "must" and/or the original may-sources "may".
2505 * In particular, "must" contains the must-sources from before
2506 * the kills were added and "may" contains the may-source from before
2507 * the kills were removed.
2509 * The dependences are of the form
2511 * Source -> [Sink -> Data]
2513 * Only those dependences are kept where the Source -> Data part
2514 * is a subset of the original may-sources or must-sources.
2515 * Of those, only the must-dependences that intersect with the must-sources
2516 * remain must-dependences.
2517 * If there is some overlap between the may-sources and the must-sources,
2518 * then the may-dependences and must-dependences may also overlap.
2519 * This should be fine since the may-dependences are only kept
2520 * disjoint from the must-dependences for the isl_union_map_compute_flow
2521 * interface. This interface does not support kills, so it will
2522 * not end up calling this function.
2524 static __isl_give isl_union_flow
*isl_union_flow_drop_kill_source(
2525 __isl_take isl_union_flow
*flow
, __isl_take isl_union_map
*must
,
2526 __isl_take isl_union_map
*may
)
2528 isl_union_map
*move
;
2532 move
= isl_union_map_copy(flow
->must_dep
);
2533 move
= isl_union_map_intersect_range_factor_range(move
,
2534 isl_union_map_copy(may
));
2535 may
= isl_union_map_union(may
, isl_union_map_copy(must
));
2536 flow
->may_dep
= isl_union_map_intersect_range_factor_range(
2537 flow
->may_dep
, may
);
2538 flow
->must_dep
= isl_union_map_intersect_range_factor_range(
2539 flow
->must_dep
, must
);
2540 flow
->may_dep
= isl_union_map_union(flow
->may_dep
, move
);
2541 if (!flow
->must_dep
|| !flow
->may_dep
)
2542 return isl_union_flow_free(flow
);
2546 isl_union_map_free(must
);
2547 isl_union_map_free(may
);
2551 /* Remove the must accesses from the may accesses.
2553 * A must access always trumps a may access, so there is no need
2554 * for a must access to also be considered as a may access. Doing so
2555 * would only cost extra computations only to find out that
2556 * the duplicated may access does not make any difference.
2558 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2559 __isl_take isl_union_access_info
*access
)
2563 access
->access
[isl_access_may_source
] =
2564 isl_union_map_subtract(access
->access
[isl_access_may_source
],
2565 isl_union_map_copy(access
->access
[isl_access_must_source
]));
2566 if (!access
->access
[isl_access_may_source
])
2567 return isl_union_access_info_free(access
);
2572 /* Given a description of the "sink" accesses, the "source" accesses and
2573 * a schedule, compute for each instance of a sink access
2574 * and for each element accessed by that instance,
2575 * the possible or definite source accesses that last accessed the
2576 * element accessed by the sink access before this sink access
2577 * in the sense that there is no intermediate definite source access.
2579 * The must_no_source and may_no_source elements of the result
2580 * are subsets of access->sink. The elements must_dep and may_dep
2581 * map domain elements of access->{may,must)_source to
2582 * domain elements of access->sink.
2584 * This function is used when only the schedule map representation
2587 * We first prepend the schedule dimensions to the domain
2588 * of the accesses so that we can easily compare their relative order.
2589 * Then we consider each sink access individually in compute_flow.
2591 static __isl_give isl_union_flow
*compute_flow_union_map(
2592 __isl_take isl_union_access_info
*access
)
2594 struct isl_compute_flow_data data
;
2595 isl_union_map
*sink
;
2597 access
= isl_union_access_info_align_params(access
);
2598 access
= isl_union_access_info_introduce_schedule(access
);
2602 data
.must_source
= access
->access
[isl_access_must_source
];
2603 data
.may_source
= access
->access
[isl_access_may_source
];
2605 sink
= access
->access
[isl_access_sink
];
2606 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(sink
));
2608 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
2611 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2613 isl_union_access_info_free(access
);
2616 isl_union_access_info_free(access
);
2617 isl_union_flow_free(data
.flow
);
2621 /* A schedule access relation.
2623 * The access relation "access" is of the form [S -> D] -> A,
2624 * where S corresponds to the prefix schedule at "node".
2625 * "must" is only relevant for source accesses and indicates
2626 * whether the access is a must source or a may source.
2628 struct isl_scheduled_access
{
2631 isl_schedule_node
*node
;
2634 /* Data structure for keeping track of individual scheduled sink and source
2635 * accesses when computing dependence analysis based on a schedule tree.
2637 * "n_sink" is the number of used entries in "sink"
2638 * "n_source" is the number of used entries in "source"
2640 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2641 * to keep track of the current node and
2642 * of what extract_sink_source needs to do.
2644 struct isl_compute_flow_schedule_data
{
2645 isl_union_access_info
*access
;
2650 struct isl_scheduled_access
*sink
;
2651 struct isl_scheduled_access
*source
;
2655 isl_schedule_node
*node
;
2658 /* Align the parameters of all sinks with all sources.
2660 * If there are no sinks or no sources, then no alignment is needed.
2662 static void isl_compute_flow_schedule_data_align_params(
2663 struct isl_compute_flow_schedule_data
*data
)
2668 if (data
->n_sink
== 0 || data
->n_source
== 0)
2671 space
= isl_map_get_space(data
->sink
[0].access
);
2673 for (i
= 1; i
< data
->n_sink
; ++i
)
2674 space
= isl_space_align_params(space
,
2675 isl_map_get_space(data
->sink
[i
].access
));
2676 for (i
= 0; i
< data
->n_source
; ++i
)
2677 space
= isl_space_align_params(space
,
2678 isl_map_get_space(data
->source
[i
].access
));
2680 for (i
= 0; i
< data
->n_sink
; ++i
)
2681 data
->sink
[i
].access
=
2682 isl_map_align_params(data
->sink
[i
].access
,
2683 isl_space_copy(space
));
2684 for (i
= 0; i
< data
->n_source
; ++i
)
2685 data
->source
[i
].access
=
2686 isl_map_align_params(data
->source
[i
].access
,
2687 isl_space_copy(space
));
2689 isl_space_free(space
);
2692 /* Free all the memory referenced from "data".
2693 * Do not free "data" itself as it may be allocated on the stack.
2695 static void isl_compute_flow_schedule_data_clear(
2696 struct isl_compute_flow_schedule_data
*data
)
2703 for (i
= 0; i
< data
->n_sink
; ++i
) {
2704 isl_map_free(data
->sink
[i
].access
);
2705 isl_schedule_node_free(data
->sink
[i
].node
);
2708 for (i
= 0; i
< data
->n_source
; ++i
) {
2709 isl_map_free(data
->source
[i
].access
);
2710 isl_schedule_node_free(data
->source
[i
].node
);
2716 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2717 * (an upper bound on) the number of sinks and sources.
2719 * Sinks and sources are only extracted at leaves of the tree,
2720 * so we skip the node if it is not a leaf.
2721 * Otherwise we increment data->n_sink and data->n_source with
2722 * the number of spaces in the sink and source access domains
2723 * that reach this node.
2725 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2728 struct isl_compute_flow_schedule_data
*data
= user
;
2729 isl_union_set
*domain
;
2730 isl_union_map
*umap
;
2731 isl_bool r
= isl_bool_false
;
2733 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2734 return isl_bool_true
;
2736 domain
= isl_schedule_node_get_universe_domain(node
);
2738 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2739 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2740 data
->n_sink
+= isl_union_map_n_map(umap
);
2741 isl_union_map_free(umap
);
2745 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2746 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2747 data
->n_source
+= isl_union_map_n_map(umap
);
2748 isl_union_map_free(umap
);
2752 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2753 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2754 data
->n_source
+= isl_union_map_n_map(umap
);
2755 isl_union_map_free(umap
);
2759 isl_union_set_free(domain
);
2764 /* Add a single scheduled sink or source (depending on data->set_sink)
2765 * with scheduled access relation "map", must property data->must and
2766 * schedule node data->node to the list of sinks or sources.
2768 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2770 struct isl_compute_flow_schedule_data
*data
= user
;
2771 struct isl_scheduled_access
*access
;
2774 access
= data
->sink
+ data
->n_sink
++;
2776 access
= data
->source
+ data
->n_source
++;
2778 access
->access
= map
;
2779 access
->must
= data
->must
;
2780 access
->node
= isl_schedule_node_copy(data
->node
);
2785 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2786 * individual scheduled source and sink accesses (taking into account
2787 * the domain of the schedule).
2789 * We only collect accesses at the leaves of the schedule tree.
2790 * We prepend the schedule dimensions at the leaf to the iteration
2791 * domains of the source and sink accesses and then extract
2792 * the individual accesses (per space).
2794 * In particular, if the prefix schedule at the node is of the form
2798 * while the access relations are of the form
2802 * then the updated access relations are of the form
2806 * Note that S consists of a single space such that introducing S
2807 * in the access relations does not increase the number of spaces.
2809 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2812 struct isl_compute_flow_schedule_data
*data
= user
;
2813 isl_union_map
*prefix
;
2814 isl_union_map
*umap
;
2815 isl_bool r
= isl_bool_false
;
2817 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2818 return isl_bool_true
;
2822 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2823 prefix
= isl_union_map_reverse(prefix
);
2824 prefix
= isl_union_map_range_map(prefix
);
2827 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2828 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2829 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2831 isl_union_map_free(umap
);
2835 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2836 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2837 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2839 isl_union_map_free(umap
);
2843 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2844 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2845 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2847 isl_union_map_free(umap
);
2849 isl_union_map_free(prefix
);
2854 /* isl_access_info_compute_flow callback for determining whether
2855 * the shared nesting level and the ordering within that level
2856 * for two scheduled accesses for use in compute_single_flow.
2858 * The tokens passed to this function refer to the leaves
2859 * in the schedule tree where the accesses take place.
2861 * If n is the shared number of loops, then we need to return
2862 * "2 * n + 1" if "first" precedes "second" inside the innermost
2863 * shared loop and "2 * n" otherwise.
2865 * The innermost shared ancestor may be the leaves themselves
2866 * if the accesses take place in the same leaf. Otherwise,
2867 * it is either a set node or a sequence node. Only in the case
2868 * of a sequence node do we consider one access to precede the other.
2870 static int before_node(void *first
, void *second
)
2872 isl_schedule_node
*node1
= first
;
2873 isl_schedule_node
*node2
= second
;
2874 isl_schedule_node
*shared
;
2878 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2882 depth
= isl_schedule_node_get_schedule_depth(shared
);
2883 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2886 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2888 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2890 before
= pos1
< pos2
;
2893 isl_schedule_node_free(shared
);
2895 return 2 * depth
+ before
;
2898 /* Check if the given two accesses may be coscheduled.
2899 * If so, return 1. Otherwise return 0.
2901 * Two accesses may only be coscheduled if they appear in the same leaf.
2903 static int coscheduled_node(void *first
, void *second
)
2905 isl_schedule_node
*node1
= first
;
2906 isl_schedule_node
*node2
= second
;
2908 return node1
== node2
;
2911 /* Add the scheduled sources from "data" that access
2912 * the same data space as "sink" to "access".
2914 static __isl_give isl_access_info
*add_matching_sources(
2915 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2916 struct isl_compute_flow_schedule_data
*data
)
2921 space
= isl_space_range(isl_map_get_space(sink
->access
));
2922 for (i
= 0; i
< data
->n_source
; ++i
) {
2923 struct isl_scheduled_access
*source
;
2924 isl_space
*source_space
;
2927 source
= &data
->source
[i
];
2928 source_space
= isl_map_get_space(source
->access
);
2929 source_space
= isl_space_range(source_space
);
2930 eq
= isl_space_is_equal(space
, source_space
);
2931 isl_space_free(source_space
);
2938 access
= isl_access_info_add_source(access
,
2939 isl_map_copy(source
->access
), source
->must
, source
->node
);
2942 isl_space_free(space
);
2945 isl_space_free(space
);
2946 isl_access_info_free(access
);
2950 /* Given a scheduled sink access relation "sink", compute the corresponding
2951 * dependences on the sources in "data" and add the computed dependences
2954 * The dependences computed by access_info_compute_flow_core are of the form
2956 * [S -> I] -> [[S' -> I'] -> A]
2958 * The schedule dimensions are projected out by first currying the range,
2961 * [S -> I] -> [S' -> [I' -> A]]
2963 * and then computing the factor range
2967 static __isl_give isl_union_flow
*compute_single_flow(
2968 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
2969 struct isl_compute_flow_schedule_data
*data
)
2972 isl_access_info
*access
;
2979 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
2980 &before_node
, data
->n_source
);
2982 access
->coscheduled
= &coscheduled_node
;
2983 access
= add_matching_sources(access
, sink
, data
);
2985 flow
= access_info_compute_flow_core(access
);
2987 return isl_union_flow_free(uf
);
2989 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
2990 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
2991 isl_union_map_from_map(map
));
2992 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
2993 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
2994 isl_union_map_from_map(map
));
2996 for (i
= 0; i
< flow
->n_source
; ++i
) {
2999 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
3000 map
= isl_map_factor_range(map
);
3001 dep
= isl_union_map_from_map(map
);
3002 if (flow
->dep
[i
].must
)
3003 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
3005 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
3008 isl_flow_free(flow
);
3013 /* Given a description of the "sink" accesses, the "source" accesses and
3014 * a schedule, compute for each instance of a sink access
3015 * and for each element accessed by that instance,
3016 * the possible or definite source accesses that last accessed the
3017 * element accessed by the sink access before this sink access
3018 * in the sense that there is no intermediate definite source access.
3019 * Only consider dependences between statement instances that belong
3020 * to the domain of the schedule.
3022 * The must_no_source and may_no_source elements of the result
3023 * are subsets of access->sink. The elements must_dep and may_dep
3024 * map domain elements of access->{may,must)_source to
3025 * domain elements of access->sink.
3027 * This function is used when a schedule tree representation
3030 * We extract the individual scheduled source and sink access relations
3031 * (taking into account the domain of the schedule) and
3032 * then compute dependences for each scheduled sink individually.
3034 static __isl_give isl_union_flow
*compute_flow_schedule(
3035 __isl_take isl_union_access_info
*access
)
3037 struct isl_compute_flow_schedule_data data
= { access
};
3041 isl_union_flow
*flow
;
3043 ctx
= isl_union_access_info_get_ctx(access
);
3047 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3048 &count_sink_source
, &data
) < 0)
3051 n
= data
.n_sink
+ data
.n_source
;
3052 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
3053 if (n
&& !data
.sink
)
3055 data
.source
= data
.sink
+ data
.n_sink
;
3059 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
3060 &collect_sink_source
, &data
) < 0)
3063 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
3064 flow
= isl_union_flow_alloc(space
);
3066 isl_compute_flow_schedule_data_align_params(&data
);
3068 for (i
= 0; i
< data
.n_sink
; ++i
)
3069 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
3071 isl_compute_flow_schedule_data_clear(&data
);
3073 isl_union_access_info_free(access
);
3076 isl_union_access_info_free(access
);
3077 isl_compute_flow_schedule_data_clear(&data
);
3081 /* Given a description of the "sink" accesses, the "source" accesses and
3082 * a schedule, compute for each instance of a sink access
3083 * and for each element accessed by that instance,
3084 * the possible or definite source accesses that last accessed the
3085 * element accessed by the sink access before this sink access
3086 * in the sense that there is no intermediate definite source access.
3088 * The must_no_source and may_no_source elements of the result
3089 * are subsets of access->sink. The elements must_dep and may_dep
3090 * map domain elements of access->{may,must)_source to
3091 * domain elements of access->sink.
3093 * If any kills have been specified, then they are treated as
3094 * must-sources internally. Any dependence that purely derives
3095 * from an original kill is removed from the output.
3097 * We check whether the schedule is available as a schedule tree
3098 * or a schedule map and call the corresponding function to perform
3101 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
3102 __isl_take isl_union_access_info
*access
)
3105 isl_union_map
*must
= NULL
, *may
= NULL
;
3106 isl_union_flow
*flow
;
3108 has_kill
= isl_union_access_has_kill(access
);
3112 must
= isl_union_access_info_get_must_source(access
);
3113 may
= isl_union_access_info_get_may_source(access
);
3115 access
= isl_union_access_info_add_kill_to_must_source(access
);
3116 access
= isl_union_access_info_normalize(access
);
3119 if (access
->schedule
)
3120 flow
= compute_flow_schedule(access
);
3122 flow
= compute_flow_union_map(access
);
3124 flow
= isl_union_flow_drop_kill_source(flow
, must
, may
);
3127 isl_union_access_info_free(access
);
3128 isl_union_map_free(must
);
3129 isl_union_map_free(may
);
3133 /* Print the information contained in "flow" to "p".
3134 * The information is printed as a YAML document.
3136 __isl_give isl_printer
*isl_printer_print_union_flow(
3137 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
3139 isl_union_map
*umap
;
3142 return isl_printer_free(p
);
3144 p
= isl_printer_yaml_start_mapping(p
);
3145 umap
= isl_union_flow_get_full_must_dependence(flow
);
3146 p
= print_union_map_field(p
, "must_dependence", umap
);
3147 isl_union_map_free(umap
);
3148 umap
= isl_union_flow_get_full_may_dependence(flow
);
3149 p
= print_union_map_field(p
, "may_dependence", umap
);
3150 isl_union_map_free(umap
);
3151 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
3152 umap
= isl_union_flow_get_may_no_source(flow
);
3153 p
= print_union_map_field(p
, "may_no_source", umap
);
3154 isl_union_map_free(umap
);
3155 p
= isl_printer_yaml_end_mapping(p
);
3160 /* Return a string representation of the information in "flow".
3161 * The information is printed in flow format.
3163 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
3171 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
3172 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
3173 p
= isl_printer_print_union_flow(p
, flow
);
3174 s
= isl_printer_get_str(p
);
3175 isl_printer_free(p
);
3180 /* Given a collection of "sink" and "source" accesses,
3181 * compute for each iteration of a sink access
3182 * and for each element accessed by that iteration,
3183 * the source access in the list that last accessed the
3184 * element accessed by the sink access before this sink access.
3185 * Each access is given as a map from the loop iterators
3186 * to the array indices.
3187 * The result is a relations between source and sink
3188 * iterations and a subset of the domain of the sink accesses,
3189 * corresponding to those iterations that access an element
3190 * not previously accessed.
3192 * We collect the inputs in an isl_union_access_info object,
3193 * call isl_union_access_info_compute_flow and extract
3194 * the outputs from the result.
3196 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
3197 __isl_take isl_union_map
*must_source
,
3198 __isl_take isl_union_map
*may_source
,
3199 __isl_take isl_union_map
*schedule
,
3200 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
3201 __isl_give isl_union_map
**must_no_source
,
3202 __isl_give isl_union_map
**may_no_source
)
3204 isl_union_access_info
*access
;
3205 isl_union_flow
*flow
;
3207 access
= isl_union_access_info_from_sink(sink
);
3208 access
= isl_union_access_info_set_must_source(access
, must_source
);
3209 access
= isl_union_access_info_set_may_source(access
, may_source
);
3210 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
3211 flow
= isl_union_access_info_compute_flow(access
);
3214 *must_dep
= isl_union_flow_get_must_dependence(flow
);
3216 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
3218 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
3220 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
3222 isl_union_flow_free(flow
);
3224 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
3225 (must_no_source
&& !*must_no_source
) ||
3226 (may_no_source
&& !*may_no_source
))
3232 *must_dep
= isl_union_map_free(*must_dep
);
3234 *may_dep
= isl_union_map_free(*may_dep
);
3236 *must_no_source
= isl_union_map_free(*must_no_source
);
3238 *may_no_source
= isl_union_map_free(*may_no_source
);