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 isl_bool equal_params
;
367 equal_params
= isl_space_has_equal_params(left
, right
);
368 if (equal_params
< 0)
371 return isl_space_join(left
, right
);
373 left
= isl_space_align_params(left
, isl_space_copy(right
));
374 right
= isl_space_align_params(right
, isl_space_copy(left
));
375 return isl_space_join(left
, right
);
377 isl_space_free(left
);
378 isl_space_free(right
);
382 /* Initialize an empty isl_flow structure corresponding to a given
383 * isl_access_info structure.
384 * For each must access, two dependences are created (initialized
385 * to the empty relation), one for the resulting must dependences
386 * and one for the resulting may dependences. May accesses can
387 * only lead to may dependences, so only one dependence is created
389 * This function is private as isl_flow structures are only supposed
390 * to be created by isl_access_info_compute_flow.
392 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
396 struct isl_flow
*dep
;
401 ctx
= isl_map_get_ctx(acc
->sink
.map
);
402 dep
= isl_calloc_type(ctx
, struct isl_flow
);
406 n
= 2 * acc
->n_must
+ acc
->n_may
;
407 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
412 for (i
= 0; i
< acc
->n_must
; ++i
) {
414 dim
= space_align_and_join(
415 isl_map_get_space(acc
->source
[i
].map
),
416 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
417 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
418 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
419 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
420 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
421 dep
->dep
[2 * i
].must
= 1;
422 dep
->dep
[2 * i
+ 1].must
= 0;
423 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
426 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
428 dim
= space_align_and_join(
429 isl_map_get_space(acc
->source
[i
].map
),
430 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
431 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
432 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
433 dep
->dep
[acc
->n_must
+ i
].must
= 0;
434 if (!dep
->dep
[acc
->n_must
+ i
].map
)
444 /* Iterate over all sources and for each resulting flow dependence
445 * that is not empty, call the user specfied function.
446 * The second argument in this function call identifies the source,
447 * while the third argument correspond to the final argument of
448 * the isl_flow_foreach call.
450 isl_stat
isl_flow_foreach(__isl_keep isl_flow
*deps
,
451 isl_stat (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
,
458 return isl_stat_error
;
460 for (i
= 0; i
< deps
->n_source
; ++i
) {
461 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
463 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
464 deps
->dep
[i
].data
, user
) < 0)
465 return isl_stat_error
;
471 /* Return a copy of the subset of the sink for which no source could be found.
473 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
479 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
481 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
484 void isl_flow_free(__isl_take isl_flow
*deps
)
490 isl_set_free(deps
->must_no_source
);
491 isl_set_free(deps
->may_no_source
);
493 for (i
= 0; i
< deps
->n_source
; ++i
)
494 isl_map_free(deps
->dep
[i
].map
);
500 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
502 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
505 /* Return a map that enforces that the domain iteration occurs after
506 * the range iteration at the given level.
507 * If level is odd, then the domain iteration should occur after
508 * the target iteration in their shared level/2 outermost loops.
509 * In this case we simply need to enforce that these outermost
510 * loop iterations are the same.
511 * If level is even, then the loop iterator of the domain should
512 * be greater than the loop iterator of the range at the last
513 * of the level/2 shared loops, i.e., loop level/2 - 1.
515 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
517 struct isl_basic_map
*bmap
;
520 bmap
= isl_basic_map_equal(dim
, level
/2);
522 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
524 return isl_map_from_basic_map(bmap
);
527 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
528 * but first check if the user has set acc->restrict_fn and if so
529 * update either the input or the output of the maximization problem
530 * with respect to the resulting restriction.
532 * Since the user expects a mapping from sink iterations to source iterations,
533 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
534 * to accessed array elements, we first need to project out the accessed
535 * sink array elements by applying acc->domain_map.
536 * Similarly, the sink restriction specified by the user needs to be
537 * converted back to the wrapped map.
539 static __isl_give isl_map
*restricted_partial_lexmax(
540 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
541 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
544 isl_restriction
*restr
;
545 isl_set
*sink_domain
;
549 if (!acc
->restrict_fn
)
550 return isl_map_partial_lexmax(dep
, sink
, empty
);
552 source_map
= isl_map_copy(dep
);
553 source_map
= isl_map_apply_domain(source_map
,
554 isl_map_copy(acc
->domain_map
));
555 sink_domain
= isl_set_copy(sink
);
556 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
557 restr
= acc
->restrict_fn(source_map
, sink_domain
,
558 acc
->source
[source
].data
, acc
->restrict_user
);
559 isl_set_free(sink_domain
);
560 isl_map_free(source_map
);
564 if (restr
->type
== isl_restriction_type_input
) {
565 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
566 sink_restr
= isl_set_copy(restr
->sink
);
567 sink_restr
= isl_set_apply(sink_restr
,
568 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
569 sink
= isl_set_intersect(sink
, sink_restr
);
570 } else if (restr
->type
== isl_restriction_type_empty
) {
571 isl_space
*space
= isl_map_get_space(dep
);
573 dep
= isl_map_empty(space
);
576 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
578 if (restr
->type
== isl_restriction_type_output
)
579 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
581 isl_restriction_free(restr
);
590 /* Compute the last iteration of must source j that precedes the sink
591 * at the given level for sink iterations in set_C.
592 * The subset of set_C for which no such iteration can be found is returned
595 static struct isl_map
*last_source(struct isl_access_info
*acc
,
596 struct isl_set
*set_C
,
597 int j
, int level
, struct isl_set
**empty
)
599 struct isl_map
*read_map
;
600 struct isl_map
*write_map
;
601 struct isl_map
*dep_map
;
602 struct isl_map
*after
;
603 struct isl_map
*result
;
605 read_map
= isl_map_copy(acc
->sink
.map
);
606 write_map
= isl_map_copy(acc
->source
[j
].map
);
607 write_map
= isl_map_reverse(write_map
);
608 dep_map
= isl_map_apply_range(read_map
, write_map
);
609 after
= after_at_level(isl_map_get_space(dep_map
), level
);
610 dep_map
= isl_map_intersect(dep_map
, after
);
611 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
612 result
= isl_map_reverse(result
);
617 /* For a given mapping between iterations of must source j and iterations
618 * of the sink, compute the last iteration of must source k preceding
619 * the sink at level before_level for any of the sink iterations,
620 * but following the corresponding iteration of must source j at level
623 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
624 struct isl_map
*old_map
,
625 int j
, int before_level
,
626 int k
, int after_level
,
627 struct isl_set
**empty
)
630 struct isl_set
*set_C
;
631 struct isl_map
*read_map
;
632 struct isl_map
*write_map
;
633 struct isl_map
*dep_map
;
634 struct isl_map
*after_write
;
635 struct isl_map
*before_read
;
636 struct isl_map
*result
;
638 set_C
= isl_map_range(isl_map_copy(old_map
));
639 read_map
= isl_map_copy(acc
->sink
.map
);
640 write_map
= isl_map_copy(acc
->source
[k
].map
);
642 write_map
= isl_map_reverse(write_map
);
643 dep_map
= isl_map_apply_range(read_map
, write_map
);
644 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
645 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
646 after_write
= after_at_level(dim
, after_level
);
647 after_write
= isl_map_apply_range(after_write
, old_map
);
648 after_write
= isl_map_reverse(after_write
);
649 dep_map
= isl_map_intersect(dep_map
, after_write
);
650 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
651 dep_map
= isl_map_intersect(dep_map
, before_read
);
652 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
653 result
= isl_map_reverse(result
);
658 /* Given a shared_level between two accesses, return 1 if the
659 * the first can precede the second at the requested target_level.
660 * If the target level is odd, i.e., refers to a statement level
661 * dimension, then first needs to precede second at the requested
662 * level, i.e., shared_level must be equal to target_level.
663 * If the target level is odd, then the two loops should share
664 * at least the requested number of outer loops.
666 static int can_precede_at_level(int shared_level
, int target_level
)
668 if (shared_level
< target_level
)
670 if ((target_level
% 2) && shared_level
> target_level
)
675 /* Given a possible flow dependence temp_rel[j] between source j and the sink
676 * at level sink_level, remove those elements for which
677 * there is an iteration of another source k < j that is closer to the sink.
678 * The flow dependences temp_rel[k] are updated with the improved sources.
679 * Any improved source needs to precede the sink at the same level
680 * and needs to follow source j at the same or a deeper level.
681 * The lower this level, the later the execution date of source k.
682 * We therefore consider lower levels first.
684 * If temp_rel[j] is empty, then there can be no improvement and
685 * we return immediately.
687 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
688 struct isl_map
**temp_rel
, int j
, int sink_level
)
691 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
693 if (isl_map_plain_is_empty(temp_rel
[j
]))
696 for (k
= j
- 1; k
>= 0; --k
) {
698 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
699 if (!can_precede_at_level(plevel
, sink_level
))
702 plevel2
= acc
->level_before(acc
->source
[j
].data
,
703 acc
->source
[k
].data
);
705 for (level
= sink_level
; level
<= depth
; ++level
) {
707 struct isl_set
*trest
;
708 struct isl_map
*copy
;
710 if (!can_precede_at_level(plevel2
, level
))
713 copy
= isl_map_copy(temp_rel
[j
]);
714 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
716 if (isl_map_plain_is_empty(T
)) {
721 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
722 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
729 /* Compute all iterations of may source j that precedes the sink at the given
730 * level for sink iterations in set_C.
732 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
733 __isl_take isl_set
*set_C
, int j
, int level
)
740 read_map
= isl_map_copy(acc
->sink
.map
);
741 read_map
= isl_map_intersect_domain(read_map
, set_C
);
742 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
743 write_map
= isl_map_reverse(write_map
);
744 dep_map
= isl_map_apply_range(read_map
, write_map
);
745 after
= after_at_level(isl_map_get_space(dep_map
), level
);
746 dep_map
= isl_map_intersect(dep_map
, after
);
748 return isl_map_reverse(dep_map
);
751 /* For a given mapping between iterations of must source k and iterations
752 * of the sink, compute all iterations of may source j preceding
753 * the sink at level before_level for any of the sink iterations,
754 * but following the corresponding iteration of must source k at level
757 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
758 __isl_take isl_map
*old_map
,
759 int j
, int before_level
, int k
, int after_level
)
766 isl_map
*after_write
;
767 isl_map
*before_read
;
769 set_C
= isl_map_range(isl_map_copy(old_map
));
770 read_map
= isl_map_copy(acc
->sink
.map
);
771 read_map
= isl_map_intersect_domain(read_map
, set_C
);
772 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
774 write_map
= isl_map_reverse(write_map
);
775 dep_map
= isl_map_apply_range(read_map
, write_map
);
776 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
777 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
778 after_write
= after_at_level(dim
, after_level
);
779 after_write
= isl_map_apply_range(after_write
, old_map
);
780 after_write
= isl_map_reverse(after_write
);
781 dep_map
= isl_map_intersect(dep_map
, after_write
);
782 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
783 dep_map
= isl_map_intersect(dep_map
, before_read
);
784 return isl_map_reverse(dep_map
);
787 /* Given the must and may dependence relations for the must accesses
788 * for level sink_level, check if there are any accesses of may access j
789 * that occur in between and return their union.
790 * If some of these accesses are intermediate with respect to
791 * (previously thought to be) must dependences, then these
792 * must dependences are turned into may dependences.
794 static __isl_give isl_map
*all_intermediate_sources(
795 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
796 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
797 int j
, int sink_level
)
800 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
803 for (k
= 0; k
< acc
->n_must
; ++k
) {
806 if (isl_map_plain_is_empty(may_rel
[k
]) &&
807 isl_map_plain_is_empty(must_rel
[k
]))
810 plevel
= acc
->level_before(acc
->source
[k
].data
,
811 acc
->source
[acc
->n_must
+ j
].data
);
813 for (level
= sink_level
; level
<= depth
; ++level
) {
818 if (!can_precede_at_level(plevel
, level
))
821 copy
= isl_map_copy(may_rel
[k
]);
822 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
823 map
= isl_map_union(map
, T
);
825 copy
= isl_map_copy(must_rel
[k
]);
826 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
827 ran
= isl_map_range(isl_map_copy(T
));
828 map
= isl_map_union(map
, T
);
829 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
830 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
832 T
= isl_map_from_domain_and_range(
834 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
836 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
843 /* Compute dependences for the case where all accesses are "may"
844 * accesses, which boils down to computing memory based dependences.
845 * The generic algorithm would also work in this case, but it would
846 * be overkill to use it.
848 static __isl_give isl_flow
*compute_mem_based_dependences(
849 __isl_keep isl_access_info
*acc
)
856 res
= isl_flow_alloc(acc
);
860 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
861 maydo
= isl_set_copy(mustdo
);
863 for (i
= 0; i
< acc
->n_may
; ++i
) {
870 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
871 is_before
= plevel
& 1;
874 dim
= isl_map_get_space(res
->dep
[i
].map
);
876 before
= isl_map_lex_le_first(dim
, plevel
);
878 before
= isl_map_lex_lt_first(dim
, plevel
);
879 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
880 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
881 dep
= isl_map_intersect(dep
, before
);
882 mustdo
= isl_set_subtract(mustdo
,
883 isl_map_range(isl_map_copy(dep
)));
884 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
887 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
888 res
->must_no_source
= mustdo
;
893 /* Compute dependences for the case where there is at least one
896 * The core algorithm considers all levels in which a source may precede
897 * the sink, where a level may either be a statement level or a loop level.
898 * The outermost statement level is 1, the first loop level is 2, etc...
899 * The algorithm basically does the following:
900 * for all levels l of the read access from innermost to outermost
901 * for all sources w that may precede the sink access at that level
902 * compute the last iteration of the source that precedes the sink access
904 * add result to possible last accesses at level l of source w
905 * for all sources w2 that we haven't considered yet at this level that may
906 * also precede the sink access
907 * for all levels l2 of w from l to innermost
908 * for all possible last accesses dep of w at l
909 * compute last iteration of w2 between the source and sink
911 * add result to possible last accesses at level l of write w2
912 * and replace possible last accesses dep by the remainder
915 * The above algorithm is applied to the must access. During the course
916 * of the algorithm, we keep track of sink iterations that still
917 * need to be considered. These iterations are split into those that
918 * haven't been matched to any source access (mustdo) and those that have only
919 * been matched to may accesses (maydo).
920 * At the end of each level, we also consider the may accesses.
921 * In particular, we consider may accesses that precede the remaining
922 * sink iterations, moving elements from mustdo to maydo when appropriate,
923 * and may accesses that occur between a must source and a sink of any
924 * dependences found at the current level, turning must dependences into
925 * may dependences when appropriate.
928 static __isl_give isl_flow
*compute_val_based_dependences(
929 __isl_keep isl_access_info
*acc
)
933 isl_set
*mustdo
= NULL
;
934 isl_set
*maydo
= NULL
;
937 isl_map
**must_rel
= NULL
;
938 isl_map
**may_rel
= NULL
;
943 res
= isl_flow_alloc(acc
);
946 ctx
= isl_map_get_ctx(acc
->sink
.map
);
948 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
949 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
950 maydo
= isl_set_empty(isl_set_get_space(mustdo
));
951 if (!mustdo
|| !maydo
)
953 if (isl_set_plain_is_empty(mustdo
))
956 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
957 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
958 if (!must_rel
|| !may_rel
)
961 for (level
= depth
; level
>= 1; --level
) {
962 for (j
= acc
->n_must
-1; j
>=0; --j
) {
964 space
= isl_map_get_space(res
->dep
[2 * j
].map
);
965 must_rel
[j
] = isl_map_empty(space
);
966 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
969 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
971 struct isl_set
*rest
;
974 plevel
= acc
->level_before(acc
->source
[j
].data
,
976 if (!can_precede_at_level(plevel
, level
))
979 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
980 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
983 intermediate_sources(acc
, must_rel
, j
, level
);
985 T
= last_source(acc
, maydo
, j
, level
, &rest
);
986 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
989 intermediate_sources(acc
, may_rel
, j
, level
);
991 if (isl_set_plain_is_empty(mustdo
) &&
992 isl_set_plain_is_empty(maydo
))
995 for (j
= j
- 1; j
>= 0; --j
) {
998 plevel
= acc
->level_before(acc
->source
[j
].data
,
1000 if (!can_precede_at_level(plevel
, level
))
1003 intermediate_sources(acc
, must_rel
, j
, level
);
1004 intermediate_sources(acc
, may_rel
, j
, level
);
1007 for (j
= 0; j
< acc
->n_may
; ++j
) {
1012 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1014 if (!can_precede_at_level(plevel
, level
))
1017 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1018 res
->dep
[2 * acc
->n_must
+ j
].map
=
1019 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1020 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1021 ran
= isl_map_range(isl_map_copy(T
));
1022 res
->dep
[2 * acc
->n_must
+ j
].map
=
1023 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1024 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1025 maydo
= isl_set_union_disjoint(maydo
, ran
);
1027 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1028 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1030 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1033 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1034 res
->dep
[2 * j
].map
=
1035 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1037 res
->dep
[2 * j
+ 1].map
=
1038 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1042 if (isl_set_plain_is_empty(mustdo
) &&
1043 isl_set_plain_is_empty(maydo
))
1050 res
->must_no_source
= mustdo
;
1051 res
->may_no_source
= maydo
;
1055 isl_set_free(mustdo
);
1056 isl_set_free(maydo
);
1062 /* Given a "sink" access, a list of n "source" accesses,
1063 * compute for each iteration of the sink access
1064 * and for each element accessed by that iteration,
1065 * the source access in the list that last accessed the
1066 * element accessed by the sink access before this sink access.
1067 * Each access is given as a map from the loop iterators
1068 * to the array indices.
1069 * The result is a list of n relations between source and sink
1070 * iterations and a subset of the domain of the sink access,
1071 * corresponding to those iterations that access an element
1072 * not previously accessed.
1074 * To deal with multi-valued sink access relations, the sink iteration
1075 * domain is first extended with dimensions that correspond to the data
1076 * space. However, these extra dimensions are not projected out again.
1077 * It is up to the caller to decide whether these dimensions should be kept.
1079 static __isl_give isl_flow
*access_info_compute_flow_core(
1080 __isl_take isl_access_info
*acc
)
1082 struct isl_flow
*res
= NULL
;
1087 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1091 if (acc
->n_must
== 0)
1092 res
= compute_mem_based_dependences(acc
);
1094 acc
= isl_access_info_sort_sources(acc
);
1095 res
= compute_val_based_dependences(acc
);
1097 acc
= isl_access_info_free(acc
);
1100 if (!res
->must_no_source
|| !res
->may_no_source
)
1104 isl_access_info_free(acc
);
1109 /* Given a "sink" access, a list of n "source" accesses,
1110 * compute for each iteration of the sink access
1111 * and for each element accessed by that iteration,
1112 * the source access in the list that last accessed the
1113 * element accessed by the sink access before this sink access.
1114 * Each access is given as a map from the loop iterators
1115 * to the array indices.
1116 * The result is a list of n relations between source and sink
1117 * iterations and a subset of the domain of the sink access,
1118 * corresponding to those iterations that access an element
1119 * not previously accessed.
1121 * To deal with multi-valued sink access relations,
1122 * access_info_compute_flow_core extends the sink iteration domain
1123 * with dimensions that correspond to the data space. These extra dimensions
1124 * are projected out from the result of access_info_compute_flow_core.
1126 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1129 struct isl_flow
*res
;
1134 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1135 res
= access_info_compute_flow_core(acc
);
1139 for (j
= 0; j
< res
->n_source
; ++j
) {
1140 res
->dep
[j
].map
= isl_map_range_factor_domain(res
->dep
[j
].map
);
1141 if (!res
->dep
[j
].map
)
1152 /* Keep track of some information about a schedule for a given
1153 * access. In particular, keep track of which dimensions
1154 * have a constant value and of the actual constant values.
1156 struct isl_sched_info
{
1161 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1165 isl_vec_free(info
->cst
);
1170 /* Extract information on the constant dimensions of the schedule
1171 * for a given access. The "map" is of the form
1175 * with S the schedule domain, D the iteration domain and A the data domain.
1177 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1178 __isl_keep isl_map
*map
)
1182 struct isl_sched_info
*info
;
1188 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1191 n
= isl_space_dim(dim
, isl_dim_in
);
1192 isl_space_free(dim
);
1194 ctx
= isl_map_get_ctx(map
);
1195 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1198 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1199 info
->cst
= isl_vec_alloc(ctx
, n
);
1200 if (n
&& (!info
->is_cst
|| !info
->cst
))
1203 for (i
= 0; i
< n
; ++i
) {
1206 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1209 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1210 if (info
->is_cst
[i
])
1211 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1218 sched_info_free(info
);
1222 /* The different types of access relations that isl_union_access_info
1225 * "isl_access_sink" represents the sink accesses.
1226 * "isl_access_must_source" represents the definite source accesses.
1227 * "isl_access_may_source" represents the possible source accesses.
1229 * isl_access_sink is sometimes treated differently and
1230 * should therefore appear first.
1232 enum isl_access_type
{
1234 isl_access_must_source
,
1235 isl_access_may_source
,
1239 /* This structure represents the input for a dependence analysis computation.
1241 * "access" contains the access relations.
1243 * "schedule" or "schedule_map" represents the execution order.
1244 * Exactly one of these fields should be NULL. The other field
1245 * determines the execution order.
1247 * The domains of these four maps refer to the same iteration spaces(s).
1248 * The ranges of the first three maps also refer to the same data space(s).
1250 * After a call to isl_union_access_info_introduce_schedule,
1251 * the "schedule_map" field no longer contains useful information.
1253 struct isl_union_access_info
{
1254 isl_union_map
*access
[isl_access_end
];
1256 isl_schedule
*schedule
;
1257 isl_union_map
*schedule_map
;
1260 /* Free "access" and return NULL.
1262 __isl_null isl_union_access_info
*isl_union_access_info_free(
1263 __isl_take isl_union_access_info
*access
)
1265 enum isl_access_type i
;
1270 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1271 isl_union_map_free(access
->access
[i
]);
1272 isl_schedule_free(access
->schedule
);
1273 isl_union_map_free(access
->schedule_map
);
1279 /* Return the isl_ctx to which "access" belongs.
1281 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1285 return isl_union_map_get_ctx(access
->access
[isl_access_sink
]);
1288 /* Create a new isl_union_access_info with the given sink accesses and
1289 * and no other accesses or schedule information.
1291 * By default, we use the schedule field of the isl_union_access_info,
1292 * but this may be overridden by a call
1293 * to isl_union_access_info_set_schedule_map.
1295 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1296 __isl_take isl_union_map
*sink
)
1300 isl_union_map
*empty
;
1301 isl_union_access_info
*access
;
1302 enum isl_access_type i
;
1306 ctx
= isl_union_map_get_ctx(sink
);
1307 access
= isl_alloc_type(ctx
, isl_union_access_info
);
1311 space
= isl_union_map_get_space(sink
);
1312 empty
= isl_union_map_empty(isl_space_copy(space
));
1313 access
->access
[isl_access_sink
] = sink
;
1314 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1315 access
->access
[i
] = isl_union_map_copy(empty
);
1316 isl_union_map_free(empty
);
1317 access
->schedule
= isl_schedule_empty(space
);
1318 access
->schedule_map
= NULL
;
1320 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1321 if (!access
->access
[i
])
1322 return isl_union_access_info_free(access
);
1323 if (!access
->schedule
)
1324 return isl_union_access_info_free(access
);
1328 isl_union_map_free(sink
);
1332 /* Replace the access relation of type "type" of "info" by "access".
1334 static __isl_give isl_union_access_info
*isl_union_access_info_set(
1335 __isl_take isl_union_access_info
*info
,
1336 enum isl_access_type type
, __isl_take isl_union_map
*access
)
1338 if (!info
|| !access
)
1341 isl_union_map_free(info
->access
[type
]);
1342 info
->access
[type
] = access
;
1346 isl_union_access_info_free(info
);
1347 isl_union_map_free(access
);
1351 /* Replace the definite source accesses of "access" by "must_source".
1353 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1354 __isl_take isl_union_access_info
*access
,
1355 __isl_take isl_union_map
*must_source
)
1357 return isl_union_access_info_set(access
, isl_access_must_source
,
1361 /* Replace the possible source accesses of "access" by "may_source".
1363 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1364 __isl_take isl_union_access_info
*access
,
1365 __isl_take isl_union_map
*may_source
)
1367 return isl_union_access_info_set(access
, isl_access_may_source
,
1371 /* Replace the schedule of "access" by "schedule".
1372 * Also free the schedule_map in case it was set last.
1374 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1375 __isl_take isl_union_access_info
*access
,
1376 __isl_take isl_schedule
*schedule
)
1378 if (!access
|| !schedule
)
1381 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1382 isl_schedule_free(access
->schedule
);
1383 access
->schedule
= schedule
;
1387 isl_union_access_info_free(access
);
1388 isl_schedule_free(schedule
);
1392 /* Replace the schedule map of "access" by "schedule_map".
1393 * Also free the schedule in case it was set last.
1395 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1396 __isl_take isl_union_access_info
*access
,
1397 __isl_take isl_union_map
*schedule_map
)
1399 if (!access
|| !schedule_map
)
1402 isl_union_map_free(access
->schedule_map
);
1403 access
->schedule
= isl_schedule_free(access
->schedule
);
1404 access
->schedule_map
= schedule_map
;
1408 isl_union_access_info_free(access
);
1409 isl_union_map_free(schedule_map
);
1413 __isl_give isl_union_access_info
*isl_union_access_info_copy(
1414 __isl_keep isl_union_access_info
*access
)
1416 isl_union_access_info
*copy
;
1417 enum isl_access_type i
;
1421 copy
= isl_union_access_info_from_sink(
1422 isl_union_map_copy(access
->access
[isl_access_sink
]));
1423 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1424 copy
= isl_union_access_info_set(copy
, i
,
1425 isl_union_map_copy(access
->access
[i
]));
1426 if (access
->schedule
)
1427 copy
= isl_union_access_info_set_schedule(copy
,
1428 isl_schedule_copy(access
->schedule
));
1430 copy
= isl_union_access_info_set_schedule_map(copy
,
1431 isl_union_map_copy(access
->schedule_map
));
1436 /* Print a key-value pair of a YAML mapping to "p",
1437 * with key "name" and value "umap".
1439 static __isl_give isl_printer
*print_union_map_field(__isl_take isl_printer
*p
,
1440 const char *name
, __isl_keep isl_union_map
*umap
)
1442 p
= isl_printer_print_str(p
, name
);
1443 p
= isl_printer_yaml_next(p
);
1444 p
= isl_printer_print_str(p
, "\"");
1445 p
= isl_printer_print_union_map(p
, umap
);
1446 p
= isl_printer_print_str(p
, "\"");
1447 p
= isl_printer_yaml_next(p
);
1452 /* An enumeration of the various keys that may appear in a YAML mapping
1453 * of an isl_union_access_info object.
1454 * The keys for the access relation types are assumed to have the same values
1455 * as the access relation types in isl_access_type.
1458 isl_ai_key_sink
= isl_access_sink
,
1459 isl_ai_key_must_source
= isl_access_must_source
,
1460 isl_ai_key_may_source
= isl_access_may_source
,
1461 isl_ai_key_schedule_map
,
1462 isl_ai_key_schedule
,
1465 /* Textual representations of the YAML keys for an isl_union_access_info
1468 static char *key_str
[] = {
1469 [isl_ai_key_sink
] = "sink",
1470 [isl_ai_key_must_source
] = "must_source",
1471 [isl_ai_key_may_source
] = "may_source",
1472 [isl_ai_key_schedule_map
] = "schedule_map",
1473 [isl_ai_key_schedule
] = "schedule",
1476 /* Print a key-value pair corresponding to the access relation of type "type"
1477 * of a YAML mapping of "info" to "p".
1479 * The sink access relation is always printed, but any other access relation
1480 * is only printed if it is non-empty.
1482 static __isl_give isl_printer
*print_access_field(__isl_take isl_printer
*p
,
1483 __isl_keep isl_union_access_info
*info
, enum isl_access_type type
)
1485 if (type
!= isl_access_sink
) {
1488 empty
= isl_union_map_is_empty(info
->access
[type
]);
1490 return isl_printer_free(p
);
1494 return print_union_map_field(p
, key_str
[type
], info
->access
[type
]);
1497 /* Print the information contained in "access" to "p".
1498 * The information is printed as a YAML document.
1500 __isl_give isl_printer
*isl_printer_print_union_access_info(
1501 __isl_take isl_printer
*p
, __isl_keep isl_union_access_info
*access
)
1503 enum isl_access_type i
;
1506 return isl_printer_free(p
);
1508 p
= isl_printer_yaml_start_mapping(p
);
1509 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1510 p
= print_access_field(p
, access
, i
);
1511 if (access
->schedule
) {
1512 p
= isl_printer_print_str(p
, key_str
[isl_ai_key_schedule
]);
1513 p
= isl_printer_yaml_next(p
);
1514 p
= isl_printer_print_schedule(p
, access
->schedule
);
1515 p
= isl_printer_yaml_next(p
);
1517 p
= print_union_map_field(p
, key_str
[isl_ai_key_schedule_map
],
1518 access
->schedule_map
);
1520 p
= isl_printer_yaml_end_mapping(p
);
1525 /* Return a string representation of the information in "access".
1526 * The information is printed in flow format.
1528 __isl_give
char *isl_union_access_info_to_str(
1529 __isl_keep isl_union_access_info
*access
)
1537 p
= isl_printer_to_str(isl_union_access_info_get_ctx(access
));
1538 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
1539 p
= isl_printer_print_union_access_info(p
, access
);
1540 s
= isl_printer_get_str(p
);
1541 isl_printer_free(p
);
1546 /* Update the fields of "access" such that they all have the same parameters,
1547 * keeping in mind that the schedule_map field may be NULL and ignoring
1548 * the schedule field.
1550 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1551 __isl_take isl_union_access_info
*access
)
1554 enum isl_access_type i
;
1559 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
1560 for (i
= isl_access_sink
+ 1; i
< isl_access_end
; ++i
)
1561 space
= isl_space_align_params(space
,
1562 isl_union_map_get_space(access
->access
[i
]));
1563 if (access
->schedule_map
)
1564 space
= isl_space_align_params(space
,
1565 isl_union_map_get_space(access
->schedule_map
));
1566 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1568 isl_union_map_align_params(access
->access
[i
],
1569 isl_space_copy(space
));
1570 if (!access
->schedule_map
) {
1571 isl_space_free(space
);
1573 access
->schedule_map
=
1574 isl_union_map_align_params(access
->schedule_map
, space
);
1575 if (!access
->schedule_map
)
1576 return isl_union_access_info_free(access
);
1579 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1580 if (!access
->access
[i
])
1581 return isl_union_access_info_free(access
);
1586 /* Prepend the schedule dimensions to the iteration domains.
1588 * That is, if the schedule is of the form
1592 * while the access relations are of the form
1596 * then the updated access relations are of the form
1600 * The schedule map is also replaced by the map
1604 * that is used during the internal computation.
1605 * Neither the original schedule map nor this updated schedule map
1606 * are used after the call to this function.
1608 static __isl_give isl_union_access_info
*
1609 isl_union_access_info_introduce_schedule(
1610 __isl_take isl_union_access_info
*access
)
1613 enum isl_access_type i
;
1618 sm
= isl_union_map_reverse(access
->schedule_map
);
1619 sm
= isl_union_map_range_map(sm
);
1620 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1622 isl_union_map_apply_range(isl_union_map_copy(sm
),
1624 access
->schedule_map
= sm
;
1626 for (i
= isl_access_sink
; i
< isl_access_end
; ++i
)
1627 if (!access
->access
[i
])
1628 return isl_union_access_info_free(access
);
1629 if (!access
->schedule_map
)
1630 return isl_union_access_info_free(access
);
1635 /* This structure represents the result of a dependence analysis computation.
1637 * "must_dep" represents the full definite dependences
1638 * "may_dep" represents the full non-definite dependences.
1639 * Both are of the form
1641 * [Source] -> [[Sink -> Data]]
1643 * (after the schedule dimensions have been projected out).
1644 * "must_no_source" represents the subset of the sink accesses for which
1645 * definitely no source was found.
1646 * "may_no_source" represents the subset of the sink accesses for which
1647 * possibly, but not definitely, no source was found.
1649 struct isl_union_flow
{
1650 isl_union_map
*must_dep
;
1651 isl_union_map
*may_dep
;
1652 isl_union_map
*must_no_source
;
1653 isl_union_map
*may_no_source
;
1656 /* Return the isl_ctx to which "flow" belongs.
1658 isl_ctx
*isl_union_flow_get_ctx(__isl_keep isl_union_flow
*flow
)
1660 return flow
? isl_union_map_get_ctx(flow
->must_dep
) : NULL
;
1663 /* Free "flow" and return NULL.
1665 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1669 isl_union_map_free(flow
->must_dep
);
1670 isl_union_map_free(flow
->may_dep
);
1671 isl_union_map_free(flow
->must_no_source
);
1672 isl_union_map_free(flow
->may_no_source
);
1677 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1682 fprintf(stderr
, "must dependences: ");
1683 isl_union_map_dump(flow
->must_dep
);
1684 fprintf(stderr
, "may dependences: ");
1685 isl_union_map_dump(flow
->may_dep
);
1686 fprintf(stderr
, "must no source: ");
1687 isl_union_map_dump(flow
->must_no_source
);
1688 fprintf(stderr
, "may no source: ");
1689 isl_union_map_dump(flow
->may_no_source
);
1692 /* Return the full definite dependences in "flow", with accessed elements.
1694 __isl_give isl_union_map
*isl_union_flow_get_full_must_dependence(
1695 __isl_keep isl_union_flow
*flow
)
1699 return isl_union_map_copy(flow
->must_dep
);
1702 /* Return the full possible dependences in "flow", including the definite
1703 * dependences, with accessed elements.
1705 __isl_give isl_union_map
*isl_union_flow_get_full_may_dependence(
1706 __isl_keep isl_union_flow
*flow
)
1710 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1711 isl_union_map_copy(flow
->may_dep
));
1714 /* Return the definite dependences in "flow", without the accessed elements.
1716 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1717 __isl_keep isl_union_flow
*flow
)
1723 dep
= isl_union_map_copy(flow
->must_dep
);
1724 return isl_union_map_range_factor_domain(dep
);
1727 /* Return the possible dependences in "flow", including the definite
1728 * dependences, without the accessed elements.
1730 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1731 __isl_keep isl_union_flow
*flow
)
1737 dep
= isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1738 isl_union_map_copy(flow
->may_dep
));
1739 return isl_union_map_range_factor_domain(dep
);
1742 /* Return the non-definite dependences in "flow".
1744 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1745 __isl_keep isl_union_flow
*flow
)
1749 return isl_union_map_copy(flow
->may_dep
);
1752 /* Return the subset of the sink accesses for which definitely
1753 * no source was found.
1755 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1756 __isl_keep isl_union_flow
*flow
)
1760 return isl_union_map_copy(flow
->must_no_source
);
1763 /* Return the subset of the sink accesses for which possibly
1764 * no source was found, including those for which definitely
1765 * no source was found.
1767 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1768 __isl_keep isl_union_flow
*flow
)
1772 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1773 isl_union_map_copy(flow
->may_no_source
));
1776 /* Return the subset of the sink accesses for which possibly, but not
1777 * definitely, no source was found.
1779 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1780 __isl_keep isl_union_flow
*flow
)
1784 return isl_union_map_copy(flow
->may_no_source
);
1787 /* Create a new isl_union_flow object, initialized with empty
1788 * dependence relations and sink subsets.
1790 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1791 __isl_take isl_space
*space
)
1794 isl_union_map
*empty
;
1795 isl_union_flow
*flow
;
1799 ctx
= isl_space_get_ctx(space
);
1800 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1804 empty
= isl_union_map_empty(space
);
1805 flow
->must_dep
= isl_union_map_copy(empty
);
1806 flow
->may_dep
= isl_union_map_copy(empty
);
1807 flow
->must_no_source
= isl_union_map_copy(empty
);
1808 flow
->may_no_source
= empty
;
1810 if (!flow
->must_dep
|| !flow
->may_dep
||
1811 !flow
->must_no_source
|| !flow
->may_no_source
)
1812 return isl_union_flow_free(flow
);
1816 isl_space_free(space
);
1820 /* Copy this isl_union_flow object.
1822 __isl_give isl_union_flow
*isl_union_flow_copy(__isl_keep isl_union_flow
*flow
)
1824 isl_union_flow
*copy
;
1829 copy
= isl_union_flow_alloc(isl_union_map_get_space(flow
->must_dep
));
1834 copy
->must_dep
= isl_union_map_union(copy
->must_dep
,
1835 isl_union_map_copy(flow
->must_dep
));
1836 copy
->may_dep
= isl_union_map_union(copy
->may_dep
,
1837 isl_union_map_copy(flow
->may_dep
));
1838 copy
->must_no_source
= isl_union_map_union(copy
->must_no_source
,
1839 isl_union_map_copy(flow
->must_no_source
));
1840 copy
->may_no_source
= isl_union_map_union(copy
->may_no_source
,
1841 isl_union_map_copy(flow
->may_no_source
));
1843 if (!copy
->must_dep
|| !copy
->may_dep
||
1844 !copy
->must_no_source
|| !copy
->may_no_source
)
1845 return isl_union_flow_free(copy
);
1850 /* Drop the schedule dimensions from the iteration domains in "flow".
1851 * In particular, the schedule dimensions have been prepended
1852 * to the iteration domains prior to the dependence analysis by
1853 * replacing the iteration domain D, by the wrapped map [S -> D].
1854 * Replace these wrapped maps by the original D.
1856 * In particular, the dependences computed by access_info_compute_flow_core
1859 * [S -> D] -> [[S' -> D'] -> A]
1861 * The schedule dimensions are projected out by first currying the range,
1864 * [S -> D] -> [S' -> [D' -> A]]
1866 * and then computing the factor range
1870 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1871 __isl_take isl_union_flow
*flow
)
1876 flow
->must_dep
= isl_union_map_range_curry(flow
->must_dep
);
1877 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1878 flow
->may_dep
= isl_union_map_range_curry(flow
->may_dep
);
1879 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1880 flow
->must_no_source
=
1881 isl_union_map_domain_factor_range(flow
->must_no_source
);
1882 flow
->may_no_source
=
1883 isl_union_map_domain_factor_range(flow
->may_no_source
);
1885 if (!flow
->must_dep
|| !flow
->may_dep
||
1886 !flow
->must_no_source
|| !flow
->may_no_source
)
1887 return isl_union_flow_free(flow
);
1892 struct isl_compute_flow_data
{
1893 isl_union_map
*must_source
;
1894 isl_union_map
*may_source
;
1895 isl_union_flow
*flow
;
1900 struct isl_sched_info
*sink_info
;
1901 struct isl_sched_info
**source_info
;
1902 isl_access_info
*accesses
;
1905 static isl_stat
count_matching_array(__isl_take isl_map
*map
, void *user
)
1909 struct isl_compute_flow_data
*data
;
1911 data
= (struct isl_compute_flow_data
*)user
;
1913 dim
= isl_space_range(isl_map_get_space(map
));
1915 eq
= isl_space_is_equal(dim
, data
->dim
);
1917 isl_space_free(dim
);
1921 return isl_stat_error
;
1928 static isl_stat
collect_matching_array(__isl_take isl_map
*map
, void *user
)
1932 struct isl_sched_info
*info
;
1933 struct isl_compute_flow_data
*data
;
1935 data
= (struct isl_compute_flow_data
*)user
;
1937 dim
= isl_space_range(isl_map_get_space(map
));
1939 eq
= isl_space_is_equal(dim
, data
->dim
);
1941 isl_space_free(dim
);
1950 info
= sched_info_alloc(map
);
1951 data
->source_info
[data
->count
] = info
;
1953 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1954 map
, data
->must
, info
);
1961 return isl_stat_error
;
1964 /* Determine the shared nesting level and the "textual order" of
1965 * the given accesses.
1967 * We first determine the minimal schedule dimension for both accesses.
1969 * If among those dimensions, we can find one where both have a fixed
1970 * value and if moreover those values are different, then the previous
1971 * dimension is the last shared nesting level and the textual order
1972 * is determined based on the order of the fixed values.
1973 * If no such fixed values can be found, then we set the shared
1974 * nesting level to the minimal schedule dimension, with no textual ordering.
1976 static int before(void *first
, void *second
)
1978 struct isl_sched_info
*info1
= first
;
1979 struct isl_sched_info
*info2
= second
;
1983 n1
= isl_vec_size(info1
->cst
);
1984 n2
= isl_vec_size(info2
->cst
);
1989 for (i
= 0; i
< n1
; ++i
) {
1993 if (!info1
->is_cst
[i
])
1995 if (!info2
->is_cst
[i
])
1997 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
2001 r
= 2 * i
+ (cmp
< 0);
2009 /* Given a sink access, look for all the source accesses that access
2010 * the same array and perform dataflow analysis on them using
2011 * isl_access_info_compute_flow_core.
2013 static isl_stat
compute_flow(__isl_take isl_map
*map
, void *user
)
2017 struct isl_compute_flow_data
*data
;
2021 data
= (struct isl_compute_flow_data
*)user
;
2024 ctx
= isl_map_get_ctx(map
);
2026 data
->accesses
= NULL
;
2027 data
->sink_info
= NULL
;
2028 data
->source_info
= NULL
;
2030 data
->dim
= isl_space_range(isl_map_get_space(map
));
2032 if (isl_union_map_foreach_map(data
->must_source
,
2033 &count_matching_array
, data
) < 0)
2035 if (isl_union_map_foreach_map(data
->may_source
,
2036 &count_matching_array
, data
) < 0)
2039 data
->sink_info
= sched_info_alloc(map
);
2040 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
2043 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
2044 data
->sink_info
, &before
, data
->count
);
2045 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
2050 if (isl_union_map_foreach_map(data
->must_source
,
2051 &collect_matching_array
, data
) < 0)
2054 if (isl_union_map_foreach_map(data
->may_source
,
2055 &collect_matching_array
, data
) < 0)
2058 flow
= access_info_compute_flow_core(data
->accesses
);
2059 data
->accesses
= NULL
;
2064 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
2065 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
2066 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
2067 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
2069 for (i
= 0; i
< flow
->n_source
; ++i
) {
2071 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
2072 if (flow
->dep
[i
].must
)
2073 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
2075 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
2078 isl_flow_free(flow
);
2080 sched_info_free(data
->sink_info
);
2081 if (data
->source_info
) {
2082 for (i
= 0; i
< data
->count
; ++i
)
2083 sched_info_free(data
->source_info
[i
]);
2084 free(data
->source_info
);
2086 isl_space_free(data
->dim
);
2091 isl_access_info_free(data
->accesses
);
2092 sched_info_free(data
->sink_info
);
2093 if (data
->source_info
) {
2094 for (i
= 0; i
< data
->count
; ++i
)
2095 sched_info_free(data
->source_info
[i
]);
2096 free(data
->source_info
);
2098 isl_space_free(data
->dim
);
2101 return isl_stat_error
;
2104 /* Remove the must accesses from the may accesses.
2106 * A must access always trumps a may access, so there is no need
2107 * for a must access to also be considered as a may access. Doing so
2108 * would only cost extra computations only to find out that
2109 * the duplicated may access does not make any difference.
2111 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
2112 __isl_take isl_union_access_info
*access
)
2116 access
->access
[isl_access_may_source
] =
2117 isl_union_map_subtract(access
->access
[isl_access_may_source
],
2118 isl_union_map_copy(access
->access
[isl_access_must_source
]));
2119 if (!access
->access
[isl_access_may_source
])
2120 return isl_union_access_info_free(access
);
2125 /* Given a description of the "sink" accesses, the "source" accesses and
2126 * a schedule, compute for each instance of a sink access
2127 * and for each element accessed by that instance,
2128 * the possible or definite source accesses that last accessed the
2129 * element accessed by the sink access before this sink access
2130 * in the sense that there is no intermediate definite source access.
2132 * The must_no_source and may_no_source elements of the result
2133 * are subsets of access->sink. The elements must_dep and may_dep
2134 * map domain elements of access->{may,must)_source to
2135 * domain elements of access->sink.
2137 * This function is used when only the schedule map representation
2140 * We first prepend the schedule dimensions to the domain
2141 * of the accesses so that we can easily compare their relative order.
2142 * Then we consider each sink access individually in compute_flow.
2144 static __isl_give isl_union_flow
*compute_flow_union_map(
2145 __isl_take isl_union_access_info
*access
)
2147 struct isl_compute_flow_data data
;
2148 isl_union_map
*sink
;
2150 access
= isl_union_access_info_align_params(access
);
2151 access
= isl_union_access_info_introduce_schedule(access
);
2155 data
.must_source
= access
->access
[isl_access_must_source
];
2156 data
.may_source
= access
->access
[isl_access_may_source
];
2158 sink
= access
->access
[isl_access_sink
];
2159 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(sink
));
2161 if (isl_union_map_foreach_map(sink
, &compute_flow
, &data
) < 0)
2164 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
2166 isl_union_access_info_free(access
);
2169 isl_union_access_info_free(access
);
2170 isl_union_flow_free(data
.flow
);
2174 /* A schedule access relation.
2176 * The access relation "access" is of the form [S -> D] -> A,
2177 * where S corresponds to the prefix schedule at "node".
2178 * "must" is only relevant for source accesses and indicates
2179 * whether the access is a must source or a may source.
2181 struct isl_scheduled_access
{
2184 isl_schedule_node
*node
;
2187 /* Data structure for keeping track of individual scheduled sink and source
2188 * accesses when computing dependence analysis based on a schedule tree.
2190 * "n_sink" is the number of used entries in "sink"
2191 * "n_source" is the number of used entries in "source"
2193 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2194 * to keep track of the current node and
2195 * of what extract_sink_source needs to do.
2197 struct isl_compute_flow_schedule_data
{
2198 isl_union_access_info
*access
;
2203 struct isl_scheduled_access
*sink
;
2204 struct isl_scheduled_access
*source
;
2208 isl_schedule_node
*node
;
2211 /* Align the parameters of all sinks with all sources.
2213 * If there are no sinks or no sources, then no alignment is needed.
2215 static void isl_compute_flow_schedule_data_align_params(
2216 struct isl_compute_flow_schedule_data
*data
)
2221 if (data
->n_sink
== 0 || data
->n_source
== 0)
2224 space
= isl_map_get_space(data
->sink
[0].access
);
2226 for (i
= 1; i
< data
->n_sink
; ++i
)
2227 space
= isl_space_align_params(space
,
2228 isl_map_get_space(data
->sink
[i
].access
));
2229 for (i
= 0; i
< data
->n_source
; ++i
)
2230 space
= isl_space_align_params(space
,
2231 isl_map_get_space(data
->source
[i
].access
));
2233 for (i
= 0; i
< data
->n_sink
; ++i
)
2234 data
->sink
[i
].access
=
2235 isl_map_align_params(data
->sink
[i
].access
,
2236 isl_space_copy(space
));
2237 for (i
= 0; i
< data
->n_source
; ++i
)
2238 data
->source
[i
].access
=
2239 isl_map_align_params(data
->source
[i
].access
,
2240 isl_space_copy(space
));
2242 isl_space_free(space
);
2245 /* Free all the memory referenced from "data".
2246 * Do not free "data" itself as it may be allocated on the stack.
2248 static void isl_compute_flow_schedule_data_clear(
2249 struct isl_compute_flow_schedule_data
*data
)
2256 for (i
= 0; i
< data
->n_sink
; ++i
) {
2257 isl_map_free(data
->sink
[i
].access
);
2258 isl_schedule_node_free(data
->sink
[i
].node
);
2261 for (i
= 0; i
< data
->n_source
; ++i
) {
2262 isl_map_free(data
->source
[i
].access
);
2263 isl_schedule_node_free(data
->source
[i
].node
);
2269 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2270 * (an upper bound on) the number of sinks and sources.
2272 * Sinks and sources are only extracted at leaves of the tree,
2273 * so we skip the node if it is not a leaf.
2274 * Otherwise we increment data->n_sink and data->n_source with
2275 * the number of spaces in the sink and source access domains
2276 * that reach this node.
2278 static isl_bool
count_sink_source(__isl_keep isl_schedule_node
*node
,
2281 struct isl_compute_flow_schedule_data
*data
= user
;
2282 isl_union_set
*domain
;
2283 isl_union_map
*umap
;
2284 isl_bool r
= isl_bool_false
;
2286 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2287 return isl_bool_true
;
2289 domain
= isl_schedule_node_get_universe_domain(node
);
2291 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2292 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2293 data
->n_sink
+= isl_union_map_n_map(umap
);
2294 isl_union_map_free(umap
);
2298 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2299 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2300 data
->n_source
+= isl_union_map_n_map(umap
);
2301 isl_union_map_free(umap
);
2305 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2306 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2307 data
->n_source
+= isl_union_map_n_map(umap
);
2308 isl_union_map_free(umap
);
2312 isl_union_set_free(domain
);
2317 /* Add a single scheduled sink or source (depending on data->set_sink)
2318 * with scheduled access relation "map", must property data->must and
2319 * schedule node data->node to the list of sinks or sources.
2321 static isl_stat
extract_sink_source(__isl_take isl_map
*map
, void *user
)
2323 struct isl_compute_flow_schedule_data
*data
= user
;
2324 struct isl_scheduled_access
*access
;
2327 access
= data
->sink
+ data
->n_sink
++;
2329 access
= data
->source
+ data
->n_source
++;
2331 access
->access
= map
;
2332 access
->must
= data
->must
;
2333 access
->node
= isl_schedule_node_copy(data
->node
);
2338 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2339 * individual scheduled source and sink accesses (taking into account
2340 * the domain of the schedule).
2342 * We only collect accesses at the leaves of the schedule tree.
2343 * We prepend the schedule dimensions at the leaf to the iteration
2344 * domains of the source and sink accesses and then extract
2345 * the individual accesses (per space).
2347 * In particular, if the prefix schedule at the node is of the form
2351 * while the access relations are of the form
2355 * then the updated access relations are of the form
2359 * Note that S consists of a single space such that introducing S
2360 * in the access relations does not increase the number of spaces.
2362 static isl_bool
collect_sink_source(__isl_keep isl_schedule_node
*node
,
2365 struct isl_compute_flow_schedule_data
*data
= user
;
2366 isl_union_map
*prefix
;
2367 isl_union_map
*umap
;
2368 isl_bool r
= isl_bool_false
;
2370 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2371 return isl_bool_true
;
2375 prefix
= isl_schedule_node_get_prefix_schedule_relation(node
);
2376 prefix
= isl_union_map_reverse(prefix
);
2377 prefix
= isl_union_map_range_map(prefix
);
2380 umap
= isl_union_map_copy(data
->access
->access
[isl_access_sink
]);
2381 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2382 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2384 isl_union_map_free(umap
);
2388 umap
= isl_union_map_copy(data
->access
->access
[isl_access_must_source
]);
2389 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2390 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2392 isl_union_map_free(umap
);
2396 umap
= isl_union_map_copy(data
->access
->access
[isl_access_may_source
]);
2397 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2398 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2400 isl_union_map_free(umap
);
2402 isl_union_map_free(prefix
);
2407 /* isl_access_info_compute_flow callback for determining whether
2408 * the shared nesting level and the ordering within that level
2409 * for two scheduled accesses for use in compute_single_flow.
2411 * The tokens passed to this function refer to the leaves
2412 * in the schedule tree where the accesses take place.
2414 * If n is the shared number of loops, then we need to return
2415 * "2 * n + 1" if "first" precedes "second" inside the innermost
2416 * shared loop and "2 * n" otherwise.
2418 * The innermost shared ancestor may be the leaves themselves
2419 * if the accesses take place in the same leaf. Otherwise,
2420 * it is either a set node or a sequence node. Only in the case
2421 * of a sequence node do we consider one access to precede the other.
2423 static int before_node(void *first
, void *second
)
2425 isl_schedule_node
*node1
= first
;
2426 isl_schedule_node
*node2
= second
;
2427 isl_schedule_node
*shared
;
2431 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2435 depth
= isl_schedule_node_get_schedule_depth(shared
);
2436 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2439 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2441 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2443 before
= pos1
< pos2
;
2446 isl_schedule_node_free(shared
);
2448 return 2 * depth
+ before
;
2451 /* Add the scheduled sources from "data" that access
2452 * the same data space as "sink" to "access".
2454 static __isl_give isl_access_info
*add_matching_sources(
2455 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2456 struct isl_compute_flow_schedule_data
*data
)
2461 space
= isl_space_range(isl_map_get_space(sink
->access
));
2462 for (i
= 0; i
< data
->n_source
; ++i
) {
2463 struct isl_scheduled_access
*source
;
2464 isl_space
*source_space
;
2467 source
= &data
->source
[i
];
2468 source_space
= isl_map_get_space(source
->access
);
2469 source_space
= isl_space_range(source_space
);
2470 eq
= isl_space_is_equal(space
, source_space
);
2471 isl_space_free(source_space
);
2478 access
= isl_access_info_add_source(access
,
2479 isl_map_copy(source
->access
), source
->must
, source
->node
);
2482 isl_space_free(space
);
2485 isl_space_free(space
);
2486 isl_access_info_free(access
);
2490 /* Given a scheduled sink access relation "sink", compute the corresponding
2491 * dependences on the sources in "data" and add the computed dependences
2494 * The dependences computed by access_info_compute_flow_core are of the form
2496 * [S -> I] -> [[S' -> I'] -> A]
2498 * The schedule dimensions are projected out by first currying the range,
2501 * [S -> I] -> [S' -> [I' -> A]]
2503 * and then computing the factor range
2507 static __isl_give isl_union_flow
*compute_single_flow(
2508 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
2509 struct isl_compute_flow_schedule_data
*data
)
2512 isl_access_info
*access
;
2519 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
2520 &before_node
, data
->n_source
);
2521 access
= add_matching_sources(access
, sink
, data
);
2523 flow
= access_info_compute_flow_core(access
);
2525 return isl_union_flow_free(uf
);
2527 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
2528 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
2529 isl_union_map_from_map(map
));
2530 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
2531 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
2532 isl_union_map_from_map(map
));
2534 for (i
= 0; i
< flow
->n_source
; ++i
) {
2537 map
= isl_map_range_curry(isl_map_copy(flow
->dep
[i
].map
));
2538 map
= isl_map_factor_range(map
);
2539 dep
= isl_union_map_from_map(map
);
2540 if (flow
->dep
[i
].must
)
2541 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
2543 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
2546 isl_flow_free(flow
);
2551 /* Given a description of the "sink" accesses, the "source" accesses and
2552 * a schedule, compute for each instance of a sink access
2553 * and for each element accessed by that instance,
2554 * the possible or definite source accesses that last accessed the
2555 * element accessed by the sink access before this sink access
2556 * in the sense that there is no intermediate definite source access.
2557 * Only consider dependences between statement instances that belong
2558 * to the domain of the schedule.
2560 * The must_no_source and may_no_source elements of the result
2561 * are subsets of access->sink. The elements must_dep and may_dep
2562 * map domain elements of access->{may,must)_source to
2563 * domain elements of access->sink.
2565 * This function is used when a schedule tree representation
2568 * We extract the individual scheduled source and sink access relations
2569 * (taking into account the domain of the schedule) and
2570 * then compute dependences for each scheduled sink individually.
2572 static __isl_give isl_union_flow
*compute_flow_schedule(
2573 __isl_take isl_union_access_info
*access
)
2575 struct isl_compute_flow_schedule_data data
= { access
};
2579 isl_union_flow
*flow
;
2581 ctx
= isl_union_access_info_get_ctx(access
);
2585 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2586 &count_sink_source
, &data
) < 0)
2589 n
= data
.n_sink
+ data
.n_source
;
2590 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
2591 if (n
&& !data
.sink
)
2593 data
.source
= data
.sink
+ data
.n_sink
;
2597 if (isl_schedule_foreach_schedule_node_top_down(access
->schedule
,
2598 &collect_sink_source
, &data
) < 0)
2601 space
= isl_union_map_get_space(access
->access
[isl_access_sink
]);
2602 flow
= isl_union_flow_alloc(space
);
2604 isl_compute_flow_schedule_data_align_params(&data
);
2606 for (i
= 0; i
< data
.n_sink
; ++i
)
2607 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
2609 isl_compute_flow_schedule_data_clear(&data
);
2611 isl_union_access_info_free(access
);
2614 isl_union_access_info_free(access
);
2615 isl_compute_flow_schedule_data_clear(&data
);
2619 /* Given a description of the "sink" accesses, the "source" accesses and
2620 * a schedule, compute for each instance of a sink access
2621 * and for each element accessed by that instance,
2622 * the possible or definite source accesses that last accessed the
2623 * element accessed by the sink access before this sink access
2624 * in the sense that there is no intermediate definite source access.
2626 * The must_no_source and may_no_source elements of the result
2627 * are subsets of access->sink. The elements must_dep and may_dep
2628 * map domain elements of access->{may,must)_source to
2629 * domain elements of access->sink.
2631 * We check whether the schedule is available as a schedule tree
2632 * or a schedule map and call the corresponding function to perform
2635 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
2636 __isl_take isl_union_access_info
*access
)
2638 access
= isl_union_access_info_normalize(access
);
2641 if (access
->schedule
)
2642 return compute_flow_schedule(access
);
2644 return compute_flow_union_map(access
);
2647 /* Print the information contained in "flow" to "p".
2648 * The information is printed as a YAML document.
2650 __isl_give isl_printer
*isl_printer_print_union_flow(
2651 __isl_take isl_printer
*p
, __isl_keep isl_union_flow
*flow
)
2653 isl_union_map
*umap
;
2656 return isl_printer_free(p
);
2658 p
= isl_printer_yaml_start_mapping(p
);
2659 umap
= isl_union_flow_get_full_must_dependence(flow
);
2660 p
= print_union_map_field(p
, "must_dependence", umap
);
2661 isl_union_map_free(umap
);
2662 umap
= isl_union_flow_get_full_may_dependence(flow
);
2663 p
= print_union_map_field(p
, "may_dependence", umap
);
2664 isl_union_map_free(umap
);
2665 p
= print_union_map_field(p
, "must_no_source", flow
->must_no_source
);
2666 umap
= isl_union_flow_get_may_no_source(flow
);
2667 p
= print_union_map_field(p
, "may_no_source", umap
);
2668 isl_union_map_free(umap
);
2669 p
= isl_printer_yaml_end_mapping(p
);
2674 /* Return a string representation of the information in "flow".
2675 * The information is printed in flow format.
2677 __isl_give
char *isl_union_flow_to_str(__isl_keep isl_union_flow
*flow
)
2685 p
= isl_printer_to_str(isl_union_flow_get_ctx(flow
));
2686 p
= isl_printer_set_yaml_style(p
, ISL_YAML_STYLE_FLOW
);
2687 p
= isl_printer_print_union_flow(p
, flow
);
2688 s
= isl_printer_get_str(p
);
2689 isl_printer_free(p
);
2694 /* Given a collection of "sink" and "source" accesses,
2695 * compute for each iteration of a sink access
2696 * and for each element accessed by that iteration,
2697 * the source access in the list that last accessed the
2698 * element accessed by the sink access before this sink access.
2699 * Each access is given as a map from the loop iterators
2700 * to the array indices.
2701 * The result is a relations between source and sink
2702 * iterations and a subset of the domain of the sink accesses,
2703 * corresponding to those iterations that access an element
2704 * not previously accessed.
2706 * We collect the inputs in an isl_union_access_info object,
2707 * call isl_union_access_info_compute_flow and extract
2708 * the outputs from the result.
2710 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
2711 __isl_take isl_union_map
*must_source
,
2712 __isl_take isl_union_map
*may_source
,
2713 __isl_take isl_union_map
*schedule
,
2714 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
2715 __isl_give isl_union_map
**must_no_source
,
2716 __isl_give isl_union_map
**may_no_source
)
2718 isl_union_access_info
*access
;
2719 isl_union_flow
*flow
;
2721 access
= isl_union_access_info_from_sink(sink
);
2722 access
= isl_union_access_info_set_must_source(access
, must_source
);
2723 access
= isl_union_access_info_set_may_source(access
, may_source
);
2724 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
2725 flow
= isl_union_access_info_compute_flow(access
);
2728 *must_dep
= isl_union_flow_get_must_dependence(flow
);
2730 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
2732 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
2734 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
2736 isl_union_flow_free(flow
);
2738 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
2739 (must_no_source
&& !*must_no_source
) ||
2740 (may_no_source
&& !*may_no_source
))
2746 *must_dep
= isl_union_map_free(*must_dep
);
2748 *may_dep
= isl_union_map_free(*may_dep
);
2750 *must_no_source
= isl_union_map_free(*must_no_source
);
2752 *may_no_source
= isl_union_map_free(*may_no_source
);