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>
27 enum isl_restriction_type
{
28 isl_restriction_type_empty
,
29 isl_restriction_type_none
,
30 isl_restriction_type_input
,
31 isl_restriction_type_output
34 struct isl_restriction
{
35 enum isl_restriction_type type
;
41 /* Create a restriction of the given type.
43 static __isl_give isl_restriction
*isl_restriction_alloc(
44 __isl_take isl_map
*source_map
, enum isl_restriction_type type
)
47 isl_restriction
*restr
;
52 ctx
= isl_map_get_ctx(source_map
);
53 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
59 isl_map_free(source_map
);
62 isl_map_free(source_map
);
66 /* Create a restriction that doesn't restrict anything.
68 __isl_give isl_restriction
*isl_restriction_none(__isl_take isl_map
*source_map
)
70 return isl_restriction_alloc(source_map
, isl_restriction_type_none
);
73 /* Create a restriction that removes everything.
75 __isl_give isl_restriction
*isl_restriction_empty(
76 __isl_take isl_map
*source_map
)
78 return isl_restriction_alloc(source_map
, isl_restriction_type_empty
);
81 /* Create a restriction on the input of the maximization problem
82 * based on the given source and sink restrictions.
84 __isl_give isl_restriction
*isl_restriction_input(
85 __isl_take isl_set
*source_restr
, __isl_take isl_set
*sink_restr
)
88 isl_restriction
*restr
;
90 if (!source_restr
|| !sink_restr
)
93 ctx
= isl_set_get_ctx(source_restr
);
94 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
98 restr
->type
= isl_restriction_type_input
;
99 restr
->source
= source_restr
;
100 restr
->sink
= sink_restr
;
104 isl_set_free(source_restr
);
105 isl_set_free(sink_restr
);
109 /* Create a restriction on the output of the maximization problem
110 * based on the given source restriction.
112 __isl_give isl_restriction
*isl_restriction_output(
113 __isl_take isl_set
*source_restr
)
116 isl_restriction
*restr
;
121 ctx
= isl_set_get_ctx(source_restr
);
122 restr
= isl_calloc_type(ctx
, struct isl_restriction
);
126 restr
->type
= isl_restriction_type_output
;
127 restr
->source
= source_restr
;
131 isl_set_free(source_restr
);
135 __isl_null isl_restriction
*isl_restriction_free(
136 __isl_take isl_restriction
*restr
)
141 isl_set_free(restr
->source
);
142 isl_set_free(restr
->sink
);
147 isl_ctx
*isl_restriction_get_ctx(__isl_keep isl_restriction
*restr
)
149 return restr
? isl_set_get_ctx(restr
->source
) : NULL
;
152 /* A private structure to keep track of a mapping together with
153 * a user-specified identifier and a boolean indicating whether
154 * the map represents a must or may access/dependence.
156 struct isl_labeled_map
{
162 /* A structure containing the input for dependence analysis:
164 * - n_must + n_may (<= max_source) sources
165 * - a function for determining the relative order of sources and sink
166 * The must sources are placed before the may sources.
168 * domain_map is an auxiliary map that maps the sink access relation
169 * to the domain of this access relation.
170 * This field is only needed when restrict_fn is set and
171 * the field itself is set by isl_access_info_compute_flow.
173 * restrict_fn is a callback that (if not NULL) will be called
174 * right before any lexicographical maximization.
176 struct isl_access_info
{
178 struct isl_labeled_map sink
;
179 isl_access_level_before level_before
;
181 isl_access_restrict restrict_fn
;
187 struct isl_labeled_map source
[1];
190 /* A structure containing the output of dependence analysis:
191 * - n_source dependences
192 * - a wrapped subset of the sink for which definitely no source could be found
193 * - a wrapped subset of the sink for which possibly no source could be found
196 isl_set
*must_no_source
;
197 isl_set
*may_no_source
;
199 struct isl_labeled_map
*dep
;
202 /* Construct an isl_access_info structure and fill it up with
203 * the given data. The number of sources is set to 0.
205 __isl_give isl_access_info
*isl_access_info_alloc(__isl_take isl_map
*sink
,
206 void *sink_user
, isl_access_level_before fn
, int max_source
)
209 struct isl_access_info
*acc
;
214 ctx
= isl_map_get_ctx(sink
);
215 isl_assert(ctx
, max_source
>= 0, goto error
);
217 acc
= isl_calloc(ctx
, struct isl_access_info
,
218 sizeof(struct isl_access_info
) +
219 (max_source
- 1) * sizeof(struct isl_labeled_map
));
223 acc
->sink
.map
= sink
;
224 acc
->sink
.data
= sink_user
;
225 acc
->level_before
= fn
;
226 acc
->max_source
= max_source
;
236 /* Free the given isl_access_info structure.
238 __isl_null isl_access_info
*isl_access_info_free(
239 __isl_take isl_access_info
*acc
)
245 isl_map_free(acc
->domain_map
);
246 isl_map_free(acc
->sink
.map
);
247 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
248 isl_map_free(acc
->source
[i
].map
);
253 isl_ctx
*isl_access_info_get_ctx(__isl_keep isl_access_info
*acc
)
255 return acc
? isl_map_get_ctx(acc
->sink
.map
) : NULL
;
258 __isl_give isl_access_info
*isl_access_info_set_restrict(
259 __isl_take isl_access_info
*acc
, isl_access_restrict fn
, void *user
)
263 acc
->restrict_fn
= fn
;
264 acc
->restrict_user
= user
;
268 /* Add another source to an isl_access_info structure, making
269 * sure the "must" sources are placed before the "may" sources.
270 * This function may be called at most max_source times on a
271 * given isl_access_info structure, with max_source as specified
272 * in the call to isl_access_info_alloc that constructed the structure.
274 __isl_give isl_access_info
*isl_access_info_add_source(
275 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
276 int must
, void *source_user
)
282 ctx
= isl_map_get_ctx(acc
->sink
.map
);
283 isl_assert(ctx
, acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
287 acc
->source
[acc
->n_must
+ acc
->n_may
] =
288 acc
->source
[acc
->n_must
];
289 acc
->source
[acc
->n_must
].map
= source
;
290 acc
->source
[acc
->n_must
].data
= source_user
;
291 acc
->source
[acc
->n_must
].must
= 1;
294 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
295 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
296 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
302 isl_map_free(source
);
303 isl_access_info_free(acc
);
307 /* Return -n, 0 or n (with n a positive value), depending on whether
308 * the source access identified by p1 should be sorted before, together
309 * or after that identified by p2.
311 * If p1 appears before p2, then it should be sorted first.
312 * For more generic initial schedules, it is possible that neither
313 * p1 nor p2 appears before the other, or at least not in any obvious way.
314 * We therefore also check if p2 appears before p1, in which case p2
315 * should be sorted first.
316 * If not, we try to order the two statements based on the description
317 * of the iteration domains. This results in an arbitrary, but fairly
320 static int access_sort_cmp(const void *p1
, const void *p2
, void *user
)
322 isl_access_info
*acc
= user
;
323 const struct isl_labeled_map
*i1
, *i2
;
326 i1
= (const struct isl_labeled_map
*) p1
;
327 i2
= (const struct isl_labeled_map
*) p2
;
329 level1
= acc
->level_before(i1
->data
, i2
->data
);
333 level2
= acc
->level_before(i2
->data
, i1
->data
);
337 h1
= isl_map_get_hash(i1
->map
);
338 h2
= isl_map_get_hash(i2
->map
);
339 return h1
> h2
? 1 : h1
< h2
? -1 : 0;
342 /* Sort the must source accesses in their textual order.
344 static __isl_give isl_access_info
*isl_access_info_sort_sources(
345 __isl_take isl_access_info
*acc
)
349 if (acc
->n_must
<= 1)
352 if (isl_sort(acc
->source
, acc
->n_must
, sizeof(struct isl_labeled_map
),
353 access_sort_cmp
, acc
) < 0)
354 return isl_access_info_free(acc
);
359 /* Align the parameters of the two spaces if needed and then call
362 static __isl_give isl_space
*space_align_and_join(__isl_take isl_space
*left
,
363 __isl_take isl_space
*right
)
365 if (isl_space_match(left
, isl_dim_param
, right
, isl_dim_param
))
366 return isl_space_join(left
, right
);
368 left
= isl_space_align_params(left
, isl_space_copy(right
));
369 right
= isl_space_align_params(right
, isl_space_copy(left
));
370 return isl_space_join(left
, right
);
373 /* Initialize an empty isl_flow structure corresponding to a given
374 * isl_access_info structure.
375 * For each must access, two dependences are created (initialized
376 * to the empty relation), one for the resulting must dependences
377 * and one for the resulting may dependences. May accesses can
378 * only lead to may dependences, so only one dependence is created
380 * This function is private as isl_flow structures are only supposed
381 * to be created by isl_access_info_compute_flow.
383 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
387 struct isl_flow
*dep
;
392 ctx
= isl_map_get_ctx(acc
->sink
.map
);
393 dep
= isl_calloc_type(ctx
, struct isl_flow
);
397 n
= 2 * acc
->n_must
+ acc
->n_may
;
398 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
403 for (i
= 0; i
< acc
->n_must
; ++i
) {
405 dim
= space_align_and_join(
406 isl_map_get_space(acc
->source
[i
].map
),
407 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
408 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
409 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
410 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
411 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
412 dep
->dep
[2 * i
].must
= 1;
413 dep
->dep
[2 * i
+ 1].must
= 0;
414 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
417 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
419 dim
= space_align_and_join(
420 isl_map_get_space(acc
->source
[i
].map
),
421 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
422 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
423 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
424 dep
->dep
[acc
->n_must
+ i
].must
= 0;
425 if (!dep
->dep
[acc
->n_must
+ i
].map
)
435 /* Iterate over all sources and for each resulting flow dependence
436 * that is not empty, call the user specfied function.
437 * The second argument in this function call identifies the source,
438 * while the third argument correspond to the final argument of
439 * the isl_flow_foreach call.
441 isl_stat
isl_flow_foreach(__isl_keep isl_flow
*deps
,
442 isl_stat (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
,
449 return isl_stat_error
;
451 for (i
= 0; i
< deps
->n_source
; ++i
) {
452 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
454 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
455 deps
->dep
[i
].data
, user
) < 0)
456 return isl_stat_error
;
462 /* Return a copy of the subset of the sink for which no source could be found.
464 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
470 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
472 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
475 void isl_flow_free(__isl_take isl_flow
*deps
)
481 isl_set_free(deps
->must_no_source
);
482 isl_set_free(deps
->may_no_source
);
484 for (i
= 0; i
< deps
->n_source
; ++i
)
485 isl_map_free(deps
->dep
[i
].map
);
491 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
493 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
496 /* Return a map that enforces that the domain iteration occurs after
497 * the range iteration at the given level.
498 * If level is odd, then the domain iteration should occur after
499 * the target iteration in their shared level/2 outermost loops.
500 * In this case we simply need to enforce that these outermost
501 * loop iterations are the same.
502 * If level is even, then the loop iterator of the domain should
503 * be greater than the loop iterator of the range at the last
504 * of the level/2 shared loops, i.e., loop level/2 - 1.
506 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
508 struct isl_basic_map
*bmap
;
511 bmap
= isl_basic_map_equal(dim
, level
/2);
513 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
515 return isl_map_from_basic_map(bmap
);
518 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
519 * but first check if the user has set acc->restrict_fn and if so
520 * update either the input or the output of the maximization problem
521 * with respect to the resulting restriction.
523 * Since the user expects a mapping from sink iterations to source iterations,
524 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
525 * to accessed array elements, we first need to project out the accessed
526 * sink array elements by applying acc->domain_map.
527 * Similarly, the sink restriction specified by the user needs to be
528 * converted back to the wrapped map.
530 static __isl_give isl_map
*restricted_partial_lexmax(
531 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
532 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
535 isl_restriction
*restr
;
536 isl_set
*sink_domain
;
540 if (!acc
->restrict_fn
)
541 return isl_map_partial_lexmax(dep
, sink
, empty
);
543 source_map
= isl_map_copy(dep
);
544 source_map
= isl_map_apply_domain(source_map
,
545 isl_map_copy(acc
->domain_map
));
546 sink_domain
= isl_set_copy(sink
);
547 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
548 restr
= acc
->restrict_fn(source_map
, sink_domain
,
549 acc
->source
[source
].data
, acc
->restrict_user
);
550 isl_set_free(sink_domain
);
551 isl_map_free(source_map
);
555 if (restr
->type
== isl_restriction_type_input
) {
556 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
557 sink_restr
= isl_set_copy(restr
->sink
);
558 sink_restr
= isl_set_apply(sink_restr
,
559 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
560 sink
= isl_set_intersect(sink
, sink_restr
);
561 } else if (restr
->type
== isl_restriction_type_empty
) {
562 isl_space
*space
= isl_map_get_space(dep
);
564 dep
= isl_map_empty(space
);
567 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
569 if (restr
->type
== isl_restriction_type_output
)
570 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
572 isl_restriction_free(restr
);
581 /* Compute the last iteration of must source j that precedes the sink
582 * at the given level for sink iterations in set_C.
583 * The subset of set_C for which no such iteration can be found is returned
586 static struct isl_map
*last_source(struct isl_access_info
*acc
,
587 struct isl_set
*set_C
,
588 int j
, int level
, struct isl_set
**empty
)
590 struct isl_map
*read_map
;
591 struct isl_map
*write_map
;
592 struct isl_map
*dep_map
;
593 struct isl_map
*after
;
594 struct isl_map
*result
;
596 read_map
= isl_map_copy(acc
->sink
.map
);
597 write_map
= isl_map_copy(acc
->source
[j
].map
);
598 write_map
= isl_map_reverse(write_map
);
599 dep_map
= isl_map_apply_range(read_map
, write_map
);
600 after
= after_at_level(isl_map_get_space(dep_map
), level
);
601 dep_map
= isl_map_intersect(dep_map
, after
);
602 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
603 result
= isl_map_reverse(result
);
608 /* For a given mapping between iterations of must source j and iterations
609 * of the sink, compute the last iteration of must source k preceding
610 * the sink at level before_level for any of the sink iterations,
611 * but following the corresponding iteration of must source j at level
614 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
615 struct isl_map
*old_map
,
616 int j
, int before_level
,
617 int k
, int after_level
,
618 struct isl_set
**empty
)
621 struct isl_set
*set_C
;
622 struct isl_map
*read_map
;
623 struct isl_map
*write_map
;
624 struct isl_map
*dep_map
;
625 struct isl_map
*after_write
;
626 struct isl_map
*before_read
;
627 struct isl_map
*result
;
629 set_C
= isl_map_range(isl_map_copy(old_map
));
630 read_map
= isl_map_copy(acc
->sink
.map
);
631 write_map
= isl_map_copy(acc
->source
[k
].map
);
633 write_map
= isl_map_reverse(write_map
);
634 dep_map
= isl_map_apply_range(read_map
, write_map
);
635 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
636 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
637 after_write
= after_at_level(dim
, after_level
);
638 after_write
= isl_map_apply_range(after_write
, old_map
);
639 after_write
= isl_map_reverse(after_write
);
640 dep_map
= isl_map_intersect(dep_map
, after_write
);
641 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
642 dep_map
= isl_map_intersect(dep_map
, before_read
);
643 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
644 result
= isl_map_reverse(result
);
649 /* Given a shared_level between two accesses, return 1 if the
650 * the first can precede the second at the requested target_level.
651 * If the target level is odd, i.e., refers to a statement level
652 * dimension, then first needs to precede second at the requested
653 * level, i.e., shared_level must be equal to target_level.
654 * If the target level is odd, then the two loops should share
655 * at least the requested number of outer loops.
657 static int can_precede_at_level(int shared_level
, int target_level
)
659 if (shared_level
< target_level
)
661 if ((target_level
% 2) && shared_level
> target_level
)
666 /* Given a possible flow dependence temp_rel[j] between source j and the sink
667 * at level sink_level, remove those elements for which
668 * there is an iteration of another source k < j that is closer to the sink.
669 * The flow dependences temp_rel[k] are updated with the improved sources.
670 * Any improved source needs to precede the sink at the same level
671 * and needs to follow source j at the same or a deeper level.
672 * The lower this level, the later the execution date of source k.
673 * We therefore consider lower levels first.
675 * If temp_rel[j] is empty, then there can be no improvement and
676 * we return immediately.
678 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
679 struct isl_map
**temp_rel
, int j
, int sink_level
)
682 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
684 if (isl_map_plain_is_empty(temp_rel
[j
]))
687 for (k
= j
- 1; k
>= 0; --k
) {
689 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
690 if (!can_precede_at_level(plevel
, sink_level
))
693 plevel2
= acc
->level_before(acc
->source
[j
].data
,
694 acc
->source
[k
].data
);
696 for (level
= sink_level
; level
<= depth
; ++level
) {
698 struct isl_set
*trest
;
699 struct isl_map
*copy
;
701 if (!can_precede_at_level(plevel2
, level
))
704 copy
= isl_map_copy(temp_rel
[j
]);
705 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
707 if (isl_map_plain_is_empty(T
)) {
712 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
713 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
720 /* Compute all iterations of may source j that precedes the sink at the given
721 * level for sink iterations in set_C.
723 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
724 __isl_take isl_set
*set_C
, int j
, int level
)
731 read_map
= isl_map_copy(acc
->sink
.map
);
732 read_map
= isl_map_intersect_domain(read_map
, set_C
);
733 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
734 write_map
= isl_map_reverse(write_map
);
735 dep_map
= isl_map_apply_range(read_map
, write_map
);
736 after
= after_at_level(isl_map_get_space(dep_map
), level
);
737 dep_map
= isl_map_intersect(dep_map
, after
);
739 return isl_map_reverse(dep_map
);
742 /* For a given mapping between iterations of must source k and iterations
743 * of the sink, compute the all iteration of may source j preceding
744 * the sink at level before_level for any of the sink iterations,
745 * but following the corresponding iteration of must source k at level
748 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
749 __isl_take isl_map
*old_map
,
750 int j
, int before_level
, int k
, int after_level
)
757 isl_map
*after_write
;
758 isl_map
*before_read
;
760 set_C
= isl_map_range(isl_map_copy(old_map
));
761 read_map
= isl_map_copy(acc
->sink
.map
);
762 read_map
= isl_map_intersect_domain(read_map
, set_C
);
763 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
765 write_map
= isl_map_reverse(write_map
);
766 dep_map
= isl_map_apply_range(read_map
, write_map
);
767 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
768 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
769 after_write
= after_at_level(dim
, after_level
);
770 after_write
= isl_map_apply_range(after_write
, old_map
);
771 after_write
= isl_map_reverse(after_write
);
772 dep_map
= isl_map_intersect(dep_map
, after_write
);
773 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
774 dep_map
= isl_map_intersect(dep_map
, before_read
);
775 return isl_map_reverse(dep_map
);
778 /* Given the must and may dependence relations for the must accesses
779 * for level sink_level, check if there are any accesses of may access j
780 * that occur in between and return their union.
781 * If some of these accesses are intermediate with respect to
782 * (previously thought to be) must dependences, then these
783 * must dependences are turned into may dependences.
785 static __isl_give isl_map
*all_intermediate_sources(
786 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
787 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
788 int j
, int sink_level
)
791 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
794 for (k
= 0; k
< acc
->n_must
; ++k
) {
797 if (isl_map_plain_is_empty(may_rel
[k
]) &&
798 isl_map_plain_is_empty(must_rel
[k
]))
801 plevel
= acc
->level_before(acc
->source
[k
].data
,
802 acc
->source
[acc
->n_must
+ j
].data
);
804 for (level
= sink_level
; level
<= depth
; ++level
) {
809 if (!can_precede_at_level(plevel
, level
))
812 copy
= isl_map_copy(may_rel
[k
]);
813 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
814 map
= isl_map_union(map
, T
);
816 copy
= isl_map_copy(must_rel
[k
]);
817 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
818 ran
= isl_map_range(isl_map_copy(T
));
819 map
= isl_map_union(map
, T
);
820 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
821 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
823 T
= isl_map_from_domain_and_range(
825 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
827 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
834 /* Compute dependences for the case where all accesses are "may"
835 * accesses, which boils down to computing memory based dependences.
836 * The generic algorithm would also work in this case, but it would
837 * be overkill to use it.
839 static __isl_give isl_flow
*compute_mem_based_dependences(
840 __isl_keep isl_access_info
*acc
)
847 res
= isl_flow_alloc(acc
);
851 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
852 maydo
= isl_set_copy(mustdo
);
854 for (i
= 0; i
< acc
->n_may
; ++i
) {
861 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
862 is_before
= plevel
& 1;
865 dim
= isl_map_get_space(res
->dep
[i
].map
);
867 before
= isl_map_lex_le_first(dim
, plevel
);
869 before
= isl_map_lex_lt_first(dim
, plevel
);
870 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
871 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
872 dep
= isl_map_intersect(dep
, before
);
873 mustdo
= isl_set_subtract(mustdo
,
874 isl_map_range(isl_map_copy(dep
)));
875 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
878 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
879 res
->must_no_source
= mustdo
;
884 /* Compute dependences for the case where there is at least one
887 * The core algorithm considers all levels in which a source may precede
888 * the sink, where a level may either be a statement level or a loop level.
889 * The outermost statement level is 1, the first loop level is 2, etc...
890 * The algorithm basically does the following:
891 * for all levels l of the read access from innermost to outermost
892 * for all sources w that may precede the sink access at that level
893 * compute the last iteration of the source that precedes the sink access
895 * add result to possible last accesses at level l of source w
896 * for all sources w2 that we haven't considered yet at this level that may
897 * also precede the sink access
898 * for all levels l2 of w from l to innermost
899 * for all possible last accesses dep of w at l
900 * compute last iteration of w2 between the source and sink
902 * add result to possible last accesses at level l of write w2
903 * and replace possible last accesses dep by the remainder
906 * The above algorithm is applied to the must access. During the course
907 * of the algorithm, we keep track of sink iterations that still
908 * need to be considered. These iterations are split into those that
909 * haven't been matched to any source access (mustdo) and those that have only
910 * been matched to may accesses (maydo).
911 * At the end of each level, we also consider the may accesses.
912 * In particular, we consider may accesses that precede the remaining
913 * sink iterations, moving elements from mustdo to maydo when appropriate,
914 * and may accesses that occur between a must source and a sink of any
915 * dependences found at the current level, turning must dependences into
916 * may dependences when appropriate.
919 static __isl_give isl_flow
*compute_val_based_dependences(
920 __isl_keep isl_access_info
*acc
)
924 isl_set
*mustdo
= NULL
;
925 isl_set
*maydo
= NULL
;
928 isl_map
**must_rel
= NULL
;
929 isl_map
**may_rel
= NULL
;
934 res
= isl_flow_alloc(acc
);
937 ctx
= isl_map_get_ctx(acc
->sink
.map
);
939 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
940 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
941 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
942 if (!mustdo
|| !maydo
)
944 if (isl_set_plain_is_empty(mustdo
))
947 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
948 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
949 if (!must_rel
|| !may_rel
)
952 for (level
= depth
; level
>= 1; --level
) {
953 for (j
= acc
->n_must
-1; j
>=0; --j
) {
955 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
956 must_rel
[j
] = isl_map_empty(space
);
957 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
960 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
962 struct isl_set
*rest
;
965 plevel
= acc
->level_before(acc
->source
[j
].data
,
967 if (!can_precede_at_level(plevel
, level
))
970 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
971 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
974 intermediate_sources(acc
, must_rel
, j
, level
);
976 T
= last_source(acc
, maydo
, j
, level
, &rest
);
977 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
980 intermediate_sources(acc
, may_rel
, j
, level
);
982 if (isl_set_plain_is_empty(mustdo
) &&
983 isl_set_plain_is_empty(maydo
))
986 for (j
= j
- 1; j
>= 0; --j
) {
989 plevel
= acc
->level_before(acc
->source
[j
].data
,
991 if (!can_precede_at_level(plevel
, level
))
994 intermediate_sources(acc
, must_rel
, j
, level
);
995 intermediate_sources(acc
, may_rel
, j
, level
);
998 for (j
= 0; j
< acc
->n_may
; ++j
) {
1003 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1005 if (!can_precede_at_level(plevel
, level
))
1008 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1009 res
->dep
[2 * acc
->n_must
+ j
].map
=
1010 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1011 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1012 ran
= isl_map_range(isl_map_copy(T
));
1013 res
->dep
[2 * acc
->n_must
+ j
].map
=
1014 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1015 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1016 maydo
= isl_set_union_disjoint(maydo
, ran
);
1018 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1019 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1021 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1024 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1025 res
->dep
[2 * j
].map
=
1026 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1028 res
->dep
[2 * j
+ 1].map
=
1029 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1033 if (isl_set_plain_is_empty(mustdo
) &&
1034 isl_set_plain_is_empty(maydo
))
1041 res
->must_no_source
= mustdo
;
1042 res
->may_no_source
= maydo
;
1046 isl_set_free(mustdo
);
1047 isl_set_free(maydo
);
1053 /* Given a "sink" access, a list of n "source" accesses,
1054 * compute for each iteration of the sink access
1055 * and for each element accessed by that iteration,
1056 * the source access in the list that last accessed the
1057 * element accessed by the sink access before this sink access.
1058 * Each access is given as a map from the loop iterators
1059 * to the array indices.
1060 * The result is a list of n relations between source and sink
1061 * iterations and a subset of the domain of the sink access,
1062 * corresponding to those iterations that access an element
1063 * not previously accessed.
1065 * To deal with multi-valued sink access relations, the sink iteration
1066 * domain is first extended with dimensions that correspond to the data
1067 * space. However, these extra dimensions are not projected out again.
1068 * It is up to the caller to decide whether these dimensions should be kept.
1070 static __isl_give isl_flow
*access_info_compute_flow_core(
1071 __isl_take isl_access_info
*acc
)
1073 struct isl_flow
*res
= NULL
;
1078 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1082 if (acc
->n_must
== 0)
1083 res
= compute_mem_based_dependences(acc
);
1085 acc
= isl_access_info_sort_sources(acc
);
1086 res
= compute_val_based_dependences(acc
);
1088 acc
= isl_access_info_free(acc
);
1091 if (!res
->must_no_source
|| !res
->may_no_source
)
1095 isl_access_info_free(acc
);
1100 /* Given a "sink" access, a list of n "source" accesses,
1101 * compute for each iteration of the sink access
1102 * and for each element accessed by that iteration,
1103 * the source access in the list that last accessed the
1104 * element accessed by the sink access before this sink access.
1105 * Each access is given as a map from the loop iterators
1106 * to the array indices.
1107 * The result is a list of n relations between source and sink
1108 * iterations and a subset of the domain of the sink access,
1109 * corresponding to those iterations that access an element
1110 * not previously accessed.
1112 * To deal with multi-valued sink access relations,
1113 * access_info_compute_flow_core extends the sink iteration domain
1114 * with dimensions that correspond to the data space. These extra dimensions
1115 * are projected out from the result of access_info_compute_flow_core.
1117 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1120 struct isl_flow
*res
;
1125 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1126 res
= access_info_compute_flow_core(acc
);
1130 for (j
= 0; j
< res
->n_source
; ++j
) {
1131 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1132 if (!res
->dep
[j
].map
)
1143 /* Keep track of some information about a schedule for a given
1144 * access. In particular, keep track of which dimensions
1145 * have a constant value and of the actual constant values.
1147 struct isl_sched_info
{
1152 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1156 isl_vec_free(info
->cst
);
1161 /* Extract information on the constant dimensions of the schedule
1162 * for a given access. The "map" is of the form
1166 * with S the schedule domain, D the iteration domain and A the data domain.
1168 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1169 __isl_keep isl_map
*map
)
1173 struct isl_sched_info
*info
;
1179 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1182 n
= isl_space_dim(dim
, isl_dim_in
);
1183 isl_space_free(dim
);
1185 ctx
= isl_map_get_ctx(map
);
1186 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1189 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1190 info
->cst
= isl_vec_alloc(ctx
, n
);
1191 if (n
&& (!info
->is_cst
|| !info
->cst
))
1194 for (i
= 0; i
< n
; ++i
) {
1197 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1200 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1201 if (info
->is_cst
[i
])
1202 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1209 sched_info_free(info
);
1213 /* This structure represents the input for a dependence analysis computation.
1215 * "sink" represents the sink accesses.
1216 * "must_source" represents the definite source accesses.
1217 * "may_source" represents the possible source accesses.
1219 * "schedule" or "schedule_map" represents the execution order.
1220 * Exactly one of these fields should be NULL. The other field
1221 * determines the execution order.
1223 * The domains of these four maps refer to the same iteration spaces(s).
1224 * The ranges of the first three maps also refer to the same data space(s).
1226 * After a call to isl_union_access_info_introduce_schedule,
1227 * the "schedule_map" field no longer contains useful information.
1229 struct isl_union_access_info
{
1230 isl_union_map
*sink
;
1231 isl_union_map
*must_source
;
1232 isl_union_map
*may_source
;
1234 isl_schedule
*schedule
;
1235 isl_union_map
*schedule_map
;
1238 /* Free "access" and return NULL.
1240 __isl_null isl_union_access_info
*isl_union_access_info_free(
1241 __isl_take isl_union_access_info
*access
)
1246 isl_union_map_free(access
->sink
);
1247 isl_union_map_free(access
->must_source
);
1248 isl_union_map_free(access
->may_source
);
1249 isl_schedule_free(access
->schedule
);
1250 isl_union_map_free(access
->schedule_map
);
1256 /* Return the isl_ctx to which "access" belongs.
1258 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1260 return access
? isl_union_map_get_ctx(access
->sink
) : NULL
;
1263 /* Create a new isl_union_access_info with the given sink accesses and
1264 * and no source accesses or schedule information.
1266 * By default, we use the schedule field of the isl_union_access_info,
1267 * but this may be overridden by a call
1268 * to isl_union_access_info_set_schedule_map.
1270 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1271 __isl_take isl_union_map
*sink
)
1275 isl_union_map
*empty
;
1276 isl_union_access_info
*access
;
1280 ctx
= isl_union_map_get_ctx(sink
);
1281 access
= isl_alloc_type(ctx
, isl_union_access_info
);
1285 space
= isl_union_map_get_space(sink
);
1286 empty
= isl_union_map_empty(isl_space_copy(space
));
1287 access
->sink
= sink
;
1288 access
->must_source
= isl_union_map_copy(empty
);
1289 access
->may_source
= empty
;
1290 access
->schedule
= isl_schedule_empty(space
);
1291 access
->schedule_map
= NULL
;
1293 if (!access
->sink
|| !access
->must_source
||
1294 !access
->may_source
|| !access
->schedule
)
1295 return isl_union_access_info_free(access
);
1299 isl_union_map_free(sink
);
1303 /* Replace the definite source accesses of "access" by "must_source".
1305 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1306 __isl_take isl_union_access_info
*access
,
1307 __isl_take isl_union_map
*must_source
)
1309 if (!access
|| !must_source
)
1312 isl_union_map_free(access
->must_source
);
1313 access
->must_source
= must_source
;
1317 isl_union_access_info_free(access
);
1318 isl_union_map_free(must_source
);
1322 /* Replace the possible source accesses of "access" by "may_source".
1324 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1325 __isl_take isl_union_access_info
*access
,
1326 __isl_take isl_union_map
*may_source
)
1328 if (!access
|| !may_source
)
1331 isl_union_map_free(access
->may_source
);
1332 access
->may_source
= may_source
;
1336 isl_union_access_info_free(access
);
1337 isl_union_map_free(may_source
);
1341 /* Replace the schedule of "access" by "schedule".
1342 * Also free the schedule_map in case it was set last.
1344 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1345 __isl_take isl_union_access_info
*access
,
1346 __isl_take isl_schedule
*schedule
)
1348 if (!access
|| !schedule
)
1351 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1352 isl_schedule_free(access
->schedule
);
1353 access
->schedule
= schedule
;
1357 isl_union_access_info_free(access
);
1358 isl_schedule_free(schedule
);
1362 /* Replace the schedule map of "access" by "schedule_map".
1363 * Also free the schedule in case it was set last.
1365 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1366 __isl_take isl_union_access_info
*access
,
1367 __isl_take isl_union_map
*schedule_map
)
1369 if (!access
|| !schedule_map
)
1372 isl_union_map_free(access
->schedule_map
);
1373 access
->schedule
= isl_schedule_free(access
->schedule
);
1374 access
->schedule_map
= schedule_map
;
1378 isl_union_access_info_free(access
);
1379 isl_union_map_free(schedule_map
);
1383 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1384 __isl_keep isl_union_access_info
*access
)
1386 isl_union_access_info
*copy
;
1390 copy
= isl_union_access_info_from_sink(
1391 isl_union_map_copy(access
->sink
));
1392 copy
= isl_union_access_info_set_must_source(copy
,
1393 isl_union_map_copy(access
->must_source
));
1394 copy
= isl_union_access_info_set_may_source(copy
,
1395 isl_union_map_copy(access
->may_source
));
1396 if (access
->schedule
)
1397 copy
= isl_union_access_info_set_schedule(copy
,
1398 isl_schedule_copy(access
->schedule
));
1400 copy
= isl_union_access_info_set_schedule_map(copy
,
1401 isl_union_map_copy(access
->schedule_map
));
1406 /* Print a key-value pair of a YAML mapping to "p",
1407 * with key "name" and value "umap".
1409 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1410 const char *name
, __isl_keep isl_union_map
*umap
)
1412 p
= isl_printer_print_str(p
, name
);
1413 p
= isl_printer_yaml_next(p
);
1414 p
= isl_printer_print_str(p
, "\"");
1415 p
= isl_printer_print_union_map(p
, umap
);
1416 p
= isl_printer_print_str(p
, "\"");
1417 p
= isl_printer_yaml_next(p
);
1422 /* Print the information contained in "access" to "p".
1423 * The information is printed as a YAML document.
1425 __isl_give isl_printer
*isl_printer_print_union_access_info(
1426 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1429 return isl_printer_free(p
);
1431 p
= isl_printer_yaml_start_mapping(p
);
1432 p
= print_union_map_field(p
, "sink", access
->sink
);
1433 p
= print_union_map_field(p
, "must_source", access
->must_source
);
1434 p
= print_union_map_field(p
, "may_source", access
->may_source
);
1435 if (access
->schedule
) {
1436 p
= isl_printer_print_str(p
, "schedule");
1437 p
= isl_printer_yaml_next(p
);
1438 p
= isl_printer_print_schedule(p
, access
->schedule
);
1439 p
= isl_printer_yaml_next(p
);
1441 p
= print_union_map_field(p
, "schedule_map",
1442 access
->schedule_map
);
1444 p
= isl_printer_yaml_end_mapping(p
);
1449 /* Return a string representation of the information in "access".
1450 * The information is printed in flow format.
1452 __isl_give
char *isl_union_access_info_to_str(
1453 __isl_keep isl_union_access_info
*access
)
1461 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1462 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1463 p
= isl_printer_print_union_access_info(p
, access
);
1464 s
= isl_printer_get_str(p
);
1465 isl_printer_free(p
);
1470 /* Update the fields of "access" such that they all have the same parameters,
1471 * keeping in mind that the schedule_map field may be NULL and ignoring
1472 * the schedule field.
1474 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1475 __isl_take isl_union_access_info
*access
)
1482 space
= isl_union_map_get_space(access
->sink
);
1483 space
= isl_space_align_params(space
,
1484 isl_union_map_get_space(access
->must_source
));
1485 space
= isl_space_align_params(space
,
1486 isl_union_map_get_space(access
->may_source
));
1487 if (access
->schedule_map
)
1488 space
= isl_space_align_params(space
,
1489 isl_union_map_get_space(access
->schedule_map
));
1490 access
->sink
= isl_union_map_align_params(access
->sink
,
1491 isl_space_copy(space
));
1492 access
->must_source
= isl_union_map_align_params(access
->must_source
,
1493 isl_space_copy(space
));
1494 access
->may_source
= isl_union_map_align_params(access
->may_source
,
1495 isl_space_copy(space
));
1496 if (!access
->schedule_map
) {
1497 isl_space_free(space
);
1499 access
->schedule_map
=
1500 isl_union_map_align_params(access
->schedule_map
, space
);
1501 if (!access
->schedule_map
)
1502 return isl_union_access_info_free(access
);
1505 if (!access
->sink
|| !access
->must_source
|| !access
->may_source
)
1506 return isl_union_access_info_free(access
);
1511 /* Prepend the schedule dimensions to the iteration domains.
1513 * That is, if the schedule is of the form
1517 * while the access relations are of the form
1521 * then the updated access relations are of the form
1525 * The schedule map is also replaced by the map
1529 * that is used during the internal computation.
1530 * Neither the original schedule map nor this updated schedule map
1531 * are used after the call to this function.
1533 static __isl_give isl_union_access_info
*
1534 isl_union_access_info_introduce_schedule(
1535 __isl_take isl_union_access_info
*access
)
1542 sm
= isl_union_map_reverse(access
->schedule_map
);
1543 sm
= isl_union_map_range_map(sm
);
1544 access
->sink
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1546 access
->may_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1547 access
->may_source
);
1548 access
->must_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1549 access
->must_source
);
1550 access
->schedule_map
= sm
;
1552 if (!access
->sink
|| !access
->must_source
||
1553 !access
->may_source
|| !access
->schedule_map
)
1554 return isl_union_access_info_free(access
);
1559 /* This structure represents the result of a dependence analysis computation.
1561 * "must_dep" represents the full definite dependences
1562 * "may_dep" represents the full non-definite dependences.
1563 * Both are of the form
1565 * [Source] -> [[Sink -> Data]]
1567 * (after the schedule dimensions have been projected out).
1568 * "must_no_source" represents the subset of the sink accesses for which
1569 * definitely no source was found.
1570 * "may_no_source" represents the subset of the sink accesses for which
1571 * possibly, but not definitely, no source was found.
1573 struct isl_union_flow
{
1574 isl_union_map
*must_dep
;
1575 isl_union_map
*may_dep
;
1576 isl_union_map
*must_no_source
;
1577 isl_union_map
*may_no_source
;
1580 /* Return the isl_ctx to which "flow" belongs.
1582 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
1584 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
1587 /* Free "flow" and return NULL.
1589 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1593 isl_union_map_free(flow
->must_dep
);
1594 isl_union_map_free(flow
->may_dep
);
1595 isl_union_map_free(flow
->must_no_source
);
1596 isl_union_map_free(flow
->may_no_source
);
1601 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1606 fprintf(stderr
, "must dependences: ");
1607 isl_union_map_dump(flow
->must_dep
);
1608 fprintf(stderr
, "may dependences: ");
1609 isl_union_map_dump(flow
->may_dep
);
1610 fprintf(stderr
, "must no source: ");
1611 isl_union_map_dump(flow
->must_no_source
);
1612 fprintf(stderr
, "may no source: ");
1613 isl_union_map_dump(flow
->may_no_source
);
1616 /* Return the full definite dependences in "flow", with accessed elements.
1618 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
1619 __isl_keep isl_union_flow
*flow
)
1623 return isl_union_map_copy(flow
->must_dep
);
1626 /* Return the full possible dependences in "flow", including the definite
1627 * dependences, with accessed elements.
1629 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
1630 __isl_keep isl_union_flow
*flow
)
1634 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1635 isl_union_map_copy(flow
->may_dep
));
1638 /* Return the definite dependences in "flow", without the accessed elements.
1640 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1641 __isl_keep isl_union_flow
*flow
)
1647 dep
= isl_union_map_copy(flow
->must_dep
);
1648 return isl_union_map_range_factor_domain(dep
);
1651 /* Return the possible dependences in "flow", including the definite
1652 * dependences, without the accessed elements.
1654 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1655 __isl_keep isl_union_flow
*flow
)
1661 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1662 isl_union_map_copy(flow
->may_dep
));
1663 return isl_union_map_range_factor_domain(dep
);
1666 /* Return the non-definite dependences in "flow".
1668 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1669 __isl_keep isl_union_flow
*flow
)
1673 return isl_union_map_copy(flow
->may_dep
);
1676 /* Return the subset of the sink accesses for which definitely
1677 * no source was found.
1679 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1680 __isl_keep isl_union_flow
*flow
)
1684 return isl_union_map_copy(flow
->must_no_source
);
1687 /* Return the subset of the sink accesses for which possibly
1688 * no source was found, including those for which definitely
1689 * no source was found.
1691 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1692 __isl_keep isl_union_flow
*flow
)
1696 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1697 isl_union_map_copy(flow
->may_no_source
));
1700 /* Return the subset of the sink accesses for which possibly, but not
1701 * definitely, no source was found.
1703 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1704 __isl_keep isl_union_flow
*flow
)
1708 return isl_union_map_copy(flow
->may_no_source
);
1711 /* Create a new isl_union_flow object, initialized with empty
1712 * dependence relations and sink subsets.
1714 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1715 __isl_take isl_space
*space
)
1718 isl_union_map
*empty
;
1719 isl_union_flow
*flow
;
1723 ctx
= isl_space_get_ctx(space
);
1724 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1728 empty
= isl_union_map_empty(space
);
1729 flow
->must_dep
= isl_union_map_copy(empty
);
1730 flow
->may_dep
= isl_union_map_copy(empty
);
1731 flow
->must_no_source
= isl_union_map_copy(empty
);
1732 flow
->may_no_source
= empty
;
1734 if (!flow
->must_dep
|| !flow
->may_dep
||
1735 !flow
->must_no_source
|| !flow
->may_no_source
)
1736 return isl_union_flow_free(flow
);
1740 isl_space_free(space
);
1744 /* Drop the schedule dimensions from the iteration domains in "flow".
1745 * In particular, the schedule dimensions have been prepended
1746 * to the iteration domains prior to the dependence analysis by
1747 * replacing the iteration domain D, by the wrapped map [S -> D].
1748 * Replace these wrapped maps by the original D.
1750 * In particular, the dependences computed by access_info_compute_flow_core
1753 * [S -> D] -> [[S' -> D'] -> A]
1755 * The schedule dimensions are projected out by first currying the range,
1758 * [S -> D] -> [S' -> [D' -> A]]
1760 * and then computing the factor range
1764 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1765 __isl_take isl_union_flow
*flow
)
1770 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
1771 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1772 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
1773 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1774 flow
->must_no_source
=
1775 isl_union_map_domain_factor_range(flow
->must_no_source
);
1776 flow
->may_no_source
=
1777 isl_union_map_domain_factor_range(flow
->may_no_source
);
1779 if (!flow
->must_dep
|| !flow
->may_dep
||
1780 !flow
->must_no_source
|| !flow
->may_no_source
)
1781 return isl_union_flow_free(flow
);
1786 struct isl_compute_flow_data
{
1787 isl_union_map
*must_source
;
1788 isl_union_map
*may_source
;
1789 isl_union_flow
*flow
;
1794 struct isl_sched_info
*sink_info
;
1795 struct isl_sched_info
**source_info
;
1796 isl_access_info
*accesses
;
1799 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
1803 struct isl_compute_flow_data
*data
;
1805 data
= (struct isl_compute_flow_data
*)user
;
1807 dim
= isl_space_range(isl_map_get_space(map
));
1809 eq
= isl_space_is_equal(dim
, data
->dim
);
1811 isl_space_free(dim
);
1815 return isl_stat_error
;
1822 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
1826 struct isl_sched_info
*info
;
1827 struct isl_compute_flow_data
*data
;
1829 data
= (struct isl_compute_flow_data
*)user
;
1831 dim
= isl_space_range(isl_map_get_space(map
));
1833 eq
= isl_space_is_equal(dim
, data
->dim
);
1835 isl_space_free(dim
);
1844 info
= sched_info_alloc(map
);
1845 data
->source_info
[data
->count
] = info
;
1847 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1848 map
, data
->must
, info
);
1855 return isl_stat_error
;
1858 /* Determine the shared nesting level and the "textual order" of
1859 * the given accesses.
1861 * We first determine the minimal schedule dimension for both accesses.
1863 * If among those dimensions, we can find one where both have a fixed
1864 * value and if moreover those values are different, then the previous
1865 * dimension is the last shared nesting level and the textual order
1866 * is determined based on the order of the fixed values.
1867 * If no such fixed values can be found, then we set the shared
1868 * nesting level to the minimal schedule dimension, with no textual ordering.
1870 static int before(void *first
, void *second
)
1872 struct isl_sched_info
*info1
= first
;
1873 struct isl_sched_info
*info2
= second
;
1877 n1
= isl_vec_size(info1
->cst
);
1878 n2
= isl_vec_size(info2
->cst
);
1883 for (i
= 0; i
< n1
; ++i
) {
1887 if (!info1
->is_cst
[i
])
1889 if (!info2
->is_cst
[i
])
1891 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
1895 r
= 2 * i
+ (cmp
< 0);
1903 /* Given a sink access, look for all the source accesses that access
1904 * the same array and perform dataflow analysis on them using
1905 * isl_access_info_compute_flow_core.
1907 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
1911 struct isl_compute_flow_data
*data
;
1915 data
= (struct isl_compute_flow_data
*)user
;
1918 ctx
= isl_map_get_ctx(map
);
1920 data
->accesses
= NULL
;
1921 data
->sink_info
= NULL
;
1922 data
->source_info
= NULL
;
1924 data
->dim
= isl_space_range(isl_map_get_space(map
));
1926 if (isl_union_map_foreach_map(data
->must_source
,
1927 &count_matching_array
, data
) < 0)
1929 if (isl_union_map_foreach_map(data
->may_source
,
1930 &count_matching_array
, data
) < 0)
1933 data
->sink_info
= sched_info_alloc(map
);
1934 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
1937 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
1938 data
->sink_info
, &before
, data
->count
);
1939 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
1944 if (isl_union_map_foreach_map(data
->must_source
,
1945 &collect_matching_array
, data
) < 0)
1948 if (isl_union_map_foreach_map(data
->may_source
,
1949 &collect_matching_array
, data
) < 0)
1952 flow
= access_info_compute_flow_core(data
->accesses
);
1953 data
->accesses
= NULL
;
1958 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
1959 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
1960 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
1961 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
1963 for (i
= 0; i
< flow
->n_source
; ++i
) {
1965 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
1966 if (flow
->dep
[i
].must
)
1967 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
1969 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
1972 isl_flow_free(flow
);
1974 sched_info_free(data
->sink_info
);
1975 if (data
->source_info
) {
1976 for (i
= 0; i
< data
->count
; ++i
)
1977 sched_info_free(data
->source_info
[i
]);
1978 free(data
->source_info
);
1980 isl_space_free(data
->dim
);
1985 isl_access_info_free(data
->accesses
);
1986 sched_info_free(data
->sink_info
);
1987 if (data
->source_info
) {
1988 for (i
= 0; i
< data
->count
; ++i
)
1989 sched_info_free(data
->source_info
[i
]);
1990 free(data
->source_info
);
1992 isl_space_free(data
->dim
);
1995 return isl_stat_error
;
1998 /* Remove the must accesses from the may accesses.
2000 * A must access always trumps a may access, so there is no need
2001 * for a must access to also be considered as a may access. Doing so
2002 * would only cost extra computations only to find out that
2003 * the duplicated may access does not make any difference.
2005 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2006 __isl_take isl_union_access_info
*access
)
2010 access
->may_source
= isl_union_map_subtract(access
->may_source
,
2011 isl_union_map_copy(access
->must_source
));
2012 if (!access
->may_source
)
2013 return isl_union_access_info_free(access
);
2018 /* Given a description of the "sink" accesses, the "source" accesses and
2019 * a schedule, compute for each instance of a sink access
2020 * and for each element accessed by that instance,
2021 * the possible or definite source accesses that last accessed the
2022 * element accessed by the sink access before this sink access
2023 * in the sense that there is no intermediate definite source access.
2025 * The must_no_source and may_no_source elements of the result
2026 * are subsets of access->sink. The elements must_dep and may_dep
2027 * map domain elements of access->{may,must)_source to
2028 * domain elements of access->sink.
2030 * This function is used when only the schedule map representation
2033 * We first prepend the schedule dimensions to the domain
2034 * of the accesses so that we can easily compare their relative order.
2035 * Then we consider each sink access individually in compute_flow.
2037 static __isl_give isl_union_flow
*compute_flow_union_map(
2038 __isl_take isl_union_access_info
*access
)
2040 struct isl_compute_flow_data data
;
2042 access
= isl_union_access_info_align_params(access
);
2043 access
= isl_union_access_info_introduce_schedule(access
);
2047 data
.must_source
= access
->must_source
;
2048 data
.may_source
= access
->may_source
;
2050 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
2052 if (isl_union_map_foreach_map(access
->sink
, &compute_flow
, &data
) < 0)
2055 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2057 isl_union_access_info_free(access
);
2060 isl_union_access_info_free(access
);
2061 isl_union_flow_free(data
.flow
);
2065 /* A schedule access relation.
2067 * The access relation "access" is of the form [S -> D] -> A,
2068 * where S corresponds to the prefix schedule at "node".
2069 * "must" is only relevant for source accesses and indicates
2070 * whether the access is a must source or a may source.
2072 struct isl_scheduled_access
{
2075 isl_schedule_node
*node
;
2078 /* Data structure for keeping track of individual scheduled sink and source
2079 * accesses when computing dependence analysis based on a schedule tree.
2081 * "n_sink" is the number of used entries in "sink"
2082 * "n_source" is the number of used entries in "source"
2084 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2085 * to keep track of the current node and
2086 * of what extract_sink_source needs to do.
2088 struct isl_compute_flow_schedule_data
{
2089 isl_union_access_info
*access
;
2094 struct isl_scheduled_access
*sink
;
2095 struct isl_scheduled_access
*source
;
2099 isl_schedule_node
*node
;
2102 /* Align the parameters of all sinks with all sources.
2104 * If there are no sinks or no sources, then no alignment is needed.
2106 static void isl_compute_flow_schedule_data_align_params(
2107 struct isl_compute_flow_schedule_data
*data
)
2112 if (data
->n_sink
== 0 || data
->n_source
== 0)
2115 space
= isl_map_get_space(data
->sink
[0].access
);
2117 for (i
= 1; i
< data
->n_sink
; ++i
)
2118 space
= isl_space_align_params(space
,
2119 isl_map_get_space(data
->sink
[i
].access
));
2120 for (i
= 0; i
< data
->n_source
; ++i
)
2121 space
= isl_space_align_params(space
,
2122 isl_map_get_space(data
->source
[i
].access
));
2124 for (i
= 0; i
< data
->n_sink
; ++i
)
2125 data
->sink
[i
].access
=
2126 isl_map_align_params(data
->sink
[i
].access
,
2127 isl_space_copy(space
));
2128 for (i
= 0; i
< data
->n_source
; ++i
)
2129 data
->source
[i
].access
=
2130 isl_map_align_params(data
->source
[i
].access
,
2131 isl_space_copy(space
));
2133 isl_space_free(space
);
2136 /* Free all the memory referenced from "data".
2137 * Do not free "data" itself as it may be allocated on the stack.
2139 static void isl_compute_flow_schedule_data_clear(
2140 struct isl_compute_flow_schedule_data
*data
)
2147 for (i
= 0; i
< data
->n_sink
; ++i
) {
2148 isl_map_free(data
->sink
[i
].access
);
2149 isl_schedule_node_free(data
->sink
[i
].node
);
2152 for (i
= 0; i
< data
->n_source
; ++i
) {
2153 isl_map_free(data
->source
[i
].access
);
2154 isl_schedule_node_free(data
->source
[i
].node
);
2160 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2161 * (an upper bound on) the number of sinks and sources.
2163 * Sinks and sources are only extracted at leaves of the tree,
2164 * so we skip the node if it is not a leaf.
2165 * Otherwise we increment data->n_sink and data->n_source with
2166 * the number of spaces in the sink and source access domains
2167 * that reach this node.
2169 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2172 struct isl_compute_flow_schedule_data
*data
= user
;
2173 isl_union_set
*domain
;
2174 isl_union_map
*umap
;
2175 isl_bool r
= isl_bool_false
;
2177 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2178 return isl_bool_true
;
2180 domain
= isl_schedule_node_get_universe_domain(node
);
2182 umap
= isl_union_map_copy(data
->access
->sink
);
2183 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2184 data
->n_sink
+= isl_union_map_n_map(umap
);
2185 isl_union_map_free(umap
);
2189 umap
= isl_union_map_copy(data
->access
->must_source
);
2190 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2191 data
->n_source
+= isl_union_map_n_map(umap
);
2192 isl_union_map_free(umap
);
2196 umap
= isl_union_map_copy(data
->access
->may_source
);
2197 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2198 data
->n_source
+= isl_union_map_n_map(umap
);
2199 isl_union_map_free(umap
);
2203 isl_union_set_free(domain
);
2208 /* Add a single scheduled sink or source (depending on data->set_sink)
2209 * with scheduled access relation "map", must property data->must and
2210 * schedule node data->node to the list of sinks or sources.
2212 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2214 struct isl_compute_flow_schedule_data
*data
= user
;
2215 struct isl_scheduled_access
*access
;
2218 access
= data
->sink
+ data
->n_sink
++;
2220 access
= data
->source
+ data
->n_source
++;
2222 access
->access
= map
;
2223 access
->must
= data
->must
;
2224 access
->node
= isl_schedule_node_copy(data
->node
);
2229 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2230 * individual scheduled source and sink accesses (taking into account
2231 * the domain of the schedule).
2233 * We only collect accesses at the leaves of the schedule tree.
2234 * We prepend the schedule dimensions at the leaf to the iteration
2235 * domains of the source and sink accesses and then extract
2236 * the individual accesses (per space).
2238 * In particular, if the prefix schedule at the node is of the form
2242 * while the access relations are of the form
2246 * then the updated access relations are of the form
2250 * Note that S consists of a single space such that introducing S
2251 * in the access relations does not increase the number of spaces.
2253 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2256 struct isl_compute_flow_schedule_data
*data
= user
;
2257 isl_union_map
*prefix
;
2258 isl_union_map
*umap
;
2259 isl_bool r
= isl_bool_false
;
2261 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2262 return isl_bool_true
;
2266 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2267 prefix
= isl_union_map_reverse(prefix
);
2268 prefix
= isl_union_map_range_map(prefix
);
2271 umap
= isl_union_map_copy(data
->access
->sink
);
2272 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2273 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2275 isl_union_map_free(umap
);
2279 umap
= isl_union_map_copy(data
->access
->must_source
);
2280 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2281 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2283 isl_union_map_free(umap
);
2287 umap
= isl_union_map_copy(data
->access
->may_source
);
2288 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2289 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2291 isl_union_map_free(umap
);
2293 isl_union_map_free(prefix
);
2298 /* isl_access_info_compute_flow callback for determining whether
2299 * the shared nesting level and the ordering within that level
2300 * for two scheduled accesses for use in compute_single_flow.
2302 * The tokens passed to this function refer to the leaves
2303 * in the schedule tree where the accesses take place.
2305 * If n is the shared number of loops, then we need to return
2306 * "2 * n + 1" if "first" precedes "second" inside the innermost
2307 * shared loop and "2 * n" otherwise.
2309 * The innermost shared ancestor may be the leaves themselves
2310 * if the accesses take place in the same leaf. Otherwise,
2311 * it is either a set node or a sequence node. Only in the case
2312 * of a sequence node do we consider one access to precede the other.
2314 static int before_node(void *first
, void *second
)
2316 isl_schedule_node
*node1
= first
;
2317 isl_schedule_node
*node2
= second
;
2318 isl_schedule_node
*shared
;
2322 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2326 depth
= isl_schedule_node_get_schedule_depth(shared
);
2327 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2330 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2332 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2334 before
= pos1
< pos2
;
2337 isl_schedule_node_free(shared
);
2339 return 2 * depth
+ before
;
2342 /* Add the scheduled sources from "data" that access
2343 * the same data space as "sink" to "access".
2345 static __isl_give isl_access_info
*add_matching_sources(
2346 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2347 struct isl_compute_flow_schedule_data
*data
)
2352 space
= isl_space_range(isl_map_get_space(sink
->access
));
2353 for (i
= 0; i
< data
->n_source
; ++i
) {
2354 struct isl_scheduled_access
*source
;
2355 isl_space
*source_space
;
2358 source
= &data
->source
[i
];
2359 source_space
= isl_map_get_space(source
->access
);
2360 source_space
= isl_space_range(source_space
);
2361 eq
= isl_space_is_equal(space
, source_space
);
2362 isl_space_free(source_space
);
2369 access
= isl_access_info_add_source(access
,
2370 isl_map_copy(source
->access
), source
->must
, source
->node
);
2373 isl_space_free(space
);
2376 isl_space_free(space
);
2377 isl_access_info_free(access
);
2381 /* Given a scheduled sink access relation "sink", compute the corresponding
2382 * dependences on the sources in "data" and add the computed dependences
2385 * The dependences computed by access_info_compute_flow_core are of the form
2387 * [S -> I] -> [[S' -> I'] -> A]
2389 * The schedule dimensions are projected out by first currying the range,
2392 * [S -> I] -> [S' -> [I' -> A]]
2394 * and then computing the factor range
2398 static __isl_give isl_union_flow
*compute_single_flow(
2399 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
2400 struct isl_compute_flow_schedule_data
*data
)
2403 isl_access_info
*access
;
2410 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
2411 &before_node
, data
->n_source
);
2412 access
= add_matching_sources(access
, sink
, data
);
2414 flow
= access_info_compute_flow_core(access
);
2416 return isl_union_flow_free(uf
);
2418 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
2419 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
2420 isl_union_map_from_map(map
));
2421 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
2422 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
2423 isl_union_map_from_map(map
));
2425 for (i
= 0; i
< flow
->n_source
; ++i
) {
2428 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
2429 map
= isl_map_factor_range(map
);
2430 dep
= isl_union_map_from_map(map
);
2431 if (flow
->dep
[i
].must
)
2432 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
2434 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
2437 isl_flow_free(flow
);
2442 /* Given a description of the "sink" accesses, the "source" accesses and
2443 * a schedule, compute for each instance of a sink access
2444 * and for each element accessed by that instance,
2445 * the possible or definite source accesses that last accessed the
2446 * element accessed by the sink access before this sink access
2447 * in the sense that there is no intermediate definite source access.
2448 * Only consider dependences between statement instances that belong
2449 * to the domain of the schedule.
2451 * The must_no_source and may_no_source elements of the result
2452 * are subsets of access->sink. The elements must_dep and may_dep
2453 * map domain elements of access->{may,must)_source to
2454 * domain elements of access->sink.
2456 * This function is used when a schedule tree representation
2459 * We extract the individual scheduled source and sink access relations
2460 * (taking into account the domain of the schedule) and
2461 * then compute dependences for each scheduled sink individually.
2463 static __isl_give isl_union_flow
*compute_flow_schedule(
2464 __isl_take isl_union_access_info
*access
)
2466 struct isl_compute_flow_schedule_data data
= { access
};
2469 isl_union_flow
*flow
;
2471 ctx
= isl_union_access_info_get_ctx(access
);
2475 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2476 &count_sink_source
, &data
) < 0)
2479 n
= data
.n_sink
+ data
.n_source
;
2480 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
2481 if (n
&& !data
.sink
)
2483 data
.source
= data
.sink
+ data
.n_sink
;
2487 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2488 &collect_sink_source
, &data
) < 0)
2491 flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
2493 isl_compute_flow_schedule_data_align_params(&data
);
2495 for (i
= 0; i
< data
.n_sink
; ++i
)
2496 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
2498 isl_compute_flow_schedule_data_clear(&data
);
2500 isl_union_access_info_free(access
);
2503 isl_union_access_info_free(access
);
2504 isl_compute_flow_schedule_data_clear(&data
);
2508 /* Given a description of the "sink" accesses, the "source" accesses and
2509 * a schedule, compute for each instance of a sink access
2510 * and for each element accessed by that instance,
2511 * the possible or definite source accesses that last accessed the
2512 * element accessed by the sink access before this sink access
2513 * in the sense that there is no intermediate definite source access.
2515 * The must_no_source and may_no_source elements of the result
2516 * are subsets of access->sink. The elements must_dep and may_dep
2517 * map domain elements of access->{may,must)_source to
2518 * domain elements of access->sink.
2520 * We check whether the schedule is available as a schedule tree
2521 * or a schedule map and call the correpsonding function to perform
2524 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
2525 __isl_take isl_union_access_info
*access
)
2527 access
= isl_union_access_info_normalize(access
);
2530 if (access
->schedule
)
2531 return compute_flow_schedule(access
);
2533 return compute_flow_union_map(access
);
2536 /* Print the information contained in "flow" to "p".
2537 * The information is printed as a YAML document.
2539 __isl_give isl_printer
*isl_printer_print_union_flow(
2540 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
2542 isl_union_map
*umap
;
2545 return isl_printer_free(p
);
2547 p
= isl_printer_yaml_start_mapping(p
);
2548 p
= print_union_map_field(p
, "must_dependence", flow
->must_dep
);
2549 umap
= isl_union_flow_get_may_dependence(flow
);
2550 p
= print_union_map_field(p
, "may_dependence", umap
);
2551 isl_union_map_free(umap
);
2552 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
2553 umap
= isl_union_flow_get_may_no_source(flow
);
2554 p
= print_union_map_field(p
, "may_no_source", umap
);
2555 isl_union_map_free(umap
);
2556 p
= isl_printer_yaml_end_mapping(p
);
2561 /* Return a string representation of the information in "flow".
2562 * The information is printed in flow format.
2564 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
2572 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
2573 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2574 p
= isl_printer_print_union_flow(p
, flow
);
2575 s
= isl_printer_get_str(p
);
2576 isl_printer_free(p
);
2581 /* Given a collection of "sink" and "source" accesses,
2582 * compute for each iteration of a sink access
2583 * and for each element accessed by that iteration,
2584 * the source access in the list that last accessed the
2585 * element accessed by the sink access before this sink access.
2586 * Each access is given as a map from the loop iterators
2587 * to the array indices.
2588 * The result is a relations between source and sink
2589 * iterations and a subset of the domain of the sink accesses,
2590 * corresponding to those iterations that access an element
2591 * not previously accessed.
2593 * We collect the inputs in an isl_union_access_info object,
2594 * call isl_union_access_info_compute_flow and extract
2595 * the outputs from the result.
2597 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
2598 __isl_take isl_union_map
*must_source
,
2599 __isl_take isl_union_map
*may_source
,
2600 __isl_take isl_union_map
*schedule
,
2601 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
2602 __isl_give isl_union_map
**must_no_source
,
2603 __isl_give isl_union_map
**may_no_source
)
2605 isl_union_access_info
*access
;
2606 isl_union_flow
*flow
;
2608 access
= isl_union_access_info_from_sink(sink
);
2609 access
= isl_union_access_info_set_must_source(access
, must_source
);
2610 access
= isl_union_access_info_set_may_source(access
, may_source
);
2611 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
2612 flow
= isl_union_access_info_compute_flow(access
);
2615 *must_dep
= isl_union_flow_get_must_dependence(flow
);
2617 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
2619 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
2621 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
2623 isl_union_flow_free(flow
);
2625 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
2626 (must_no_source
&& !*must_no_source
) ||
2627 (may_no_source
&& !*may_no_source
))
2633 *must_dep
= isl_union_map_free(*must_dep
);
2635 *may_dep
= isl_union_map_free(*may_dep
);
2637 *must_no_source
= isl_union_map_free(*must_no_source
);
2639 *may_no_source
= isl_union_map_free(*may_no_source
);