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.
171 * restrict_fn is a callback that (if not NULL) will be called
172 * right before any lexicographical maximization.
174 struct isl_access_info
{
176 struct isl_labeled_map sink
;
177 isl_access_level_before level_before
;
179 isl_access_restrict restrict_fn
;
185 struct isl_labeled_map source
[1];
188 /* A structure containing the output of dependence analysis:
189 * - n_source dependences
190 * - a wrapped subset of the sink for which definitely no source could be found
191 * - a wrapped subset of the sink for which possibly no source could be found
194 isl_set
*must_no_source
;
195 isl_set
*may_no_source
;
197 struct isl_labeled_map
*dep
;
200 /* Construct an isl_access_info structure and fill it up with
201 * the given data. The number of sources is set to 0.
203 __isl_give isl_access_info
*isl_access_info_alloc(__isl_take isl_map
*sink
,
204 void *sink_user
, isl_access_level_before fn
, int max_source
)
207 struct isl_access_info
*acc
;
212 ctx
= isl_map_get_ctx(sink
);
213 isl_assert(ctx
, max_source
>= 0, goto error
);
215 acc
= isl_calloc(ctx
, struct isl_access_info
,
216 sizeof(struct isl_access_info
) +
217 (max_source
- 1) * sizeof(struct isl_labeled_map
));
221 acc
->sink
.map
= sink
;
222 acc
->sink
.data
= sink_user
;
223 acc
->level_before
= fn
;
224 acc
->max_source
= max_source
;
234 /* Free the given isl_access_info structure.
236 __isl_null isl_access_info
*isl_access_info_free(
237 __isl_take isl_access_info
*acc
)
243 isl_map_free(acc
->domain_map
);
244 isl_map_free(acc
->sink
.map
);
245 for (i
= 0; i
< acc
->n_must
+ acc
->n_may
; ++i
)
246 isl_map_free(acc
->source
[i
].map
);
251 isl_ctx
*isl_access_info_get_ctx(__isl_keep isl_access_info
*acc
)
253 return acc
? isl_map_get_ctx(acc
->sink
.map
) : NULL
;
256 __isl_give isl_access_info
*isl_access_info_set_restrict(
257 __isl_take isl_access_info
*acc
, isl_access_restrict fn
, void *user
)
261 acc
->restrict_fn
= fn
;
262 acc
->restrict_user
= user
;
266 /* Add another source to an isl_access_info structure, making
267 * sure the "must" sources are placed before the "may" sources.
268 * This function may be called at most max_source times on a
269 * given isl_access_info structure, with max_source as specified
270 * in the call to isl_access_info_alloc that constructed the structure.
272 __isl_give isl_access_info
*isl_access_info_add_source(
273 __isl_take isl_access_info
*acc
, __isl_take isl_map
*source
,
274 int must
, void *source_user
)
280 ctx
= isl_map_get_ctx(acc
->sink
.map
);
281 isl_assert(ctx
, acc
->n_must
+ acc
->n_may
< acc
->max_source
, goto error
);
285 acc
->source
[acc
->n_must
+ acc
->n_may
] =
286 acc
->source
[acc
->n_must
];
287 acc
->source
[acc
->n_must
].map
= source
;
288 acc
->source
[acc
->n_must
].data
= source_user
;
289 acc
->source
[acc
->n_must
].must
= 1;
292 acc
->source
[acc
->n_must
+ acc
->n_may
].map
= source
;
293 acc
->source
[acc
->n_must
+ acc
->n_may
].data
= source_user
;
294 acc
->source
[acc
->n_must
+ acc
->n_may
].must
= 0;
300 isl_map_free(source
);
301 isl_access_info_free(acc
);
305 /* Return -n, 0 or n (with n a positive value), depending on whether
306 * the source access identified by p1 should be sorted before, together
307 * or after that identified by p2.
309 * If p1 appears before p2, then it should be sorted first.
310 * For more generic initial schedules, it is possible that neither
311 * p1 nor p2 appears before the other, or at least not in any obvious way.
312 * We therefore also check if p2 appears before p1, in which case p2
313 * should be sorted first.
314 * If not, we try to order the two statements based on the description
315 * of the iteration domains. This results in an arbitrary, but fairly
318 static int access_sort_cmp(const void *p1
, const void *p2
, void *user
)
320 isl_access_info
*acc
= user
;
321 const struct isl_labeled_map
*i1
, *i2
;
324 i1
= (const struct isl_labeled_map
*) p1
;
325 i2
= (const struct isl_labeled_map
*) p2
;
327 level1
= acc
->level_before(i1
->data
, i2
->data
);
331 level2
= acc
->level_before(i2
->data
, i1
->data
);
335 h1
= isl_map_get_hash(i1
->map
);
336 h2
= isl_map_get_hash(i2
->map
);
337 return h1
> h2
? 1 : h1
< h2
? -1 : 0;
340 /* Sort the must source accesses in their textual order.
342 static __isl_give isl_access_info
*isl_access_info_sort_sources(
343 __isl_take isl_access_info
*acc
)
347 if (acc
->n_must
<= 1)
350 if (isl_sort(acc
->source
, acc
->n_must
, sizeof(struct isl_labeled_map
),
351 access_sort_cmp
, acc
) < 0)
352 return isl_access_info_free(acc
);
357 /* Align the parameters of the two spaces if needed and then call
360 static __isl_give isl_space
*space_align_and_join(__isl_take isl_space
*left
,
361 __isl_take isl_space
*right
)
363 if (isl_space_match(left
, isl_dim_param
, right
, isl_dim_param
))
364 return isl_space_join(left
, right
);
366 left
= isl_space_align_params(left
, isl_space_copy(right
));
367 right
= isl_space_align_params(right
, isl_space_copy(left
));
368 return isl_space_join(left
, right
);
371 /* Initialize an empty isl_flow structure corresponding to a given
372 * isl_access_info structure.
373 * For each must access, two dependences are created (initialized
374 * to the empty relation), one for the resulting must dependences
375 * and one for the resulting may dependences. May accesses can
376 * only lead to may dependences, so only one dependence is created
378 * This function is private as isl_flow structures are only supposed
379 * to be created by isl_access_info_compute_flow.
381 static __isl_give isl_flow
*isl_flow_alloc(__isl_keep isl_access_info
*acc
)
385 struct isl_flow
*dep
;
390 ctx
= isl_map_get_ctx(acc
->sink
.map
);
391 dep
= isl_calloc_type(ctx
, struct isl_flow
);
395 n
= 2 * acc
->n_must
+ acc
->n_may
;
396 dep
->dep
= isl_calloc_array(ctx
, struct isl_labeled_map
, n
);
401 for (i
= 0; i
< acc
->n_must
; ++i
) {
403 dim
= space_align_and_join(
404 isl_map_get_space(acc
->source
[i
].map
),
405 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
406 dep
->dep
[2 * i
].map
= isl_map_empty(dim
);
407 dep
->dep
[2 * i
+ 1].map
= isl_map_copy(dep
->dep
[2 * i
].map
);
408 dep
->dep
[2 * i
].data
= acc
->source
[i
].data
;
409 dep
->dep
[2 * i
+ 1].data
= acc
->source
[i
].data
;
410 dep
->dep
[2 * i
].must
= 1;
411 dep
->dep
[2 * i
+ 1].must
= 0;
412 if (!dep
->dep
[2 * i
].map
|| !dep
->dep
[2 * i
+ 1].map
)
415 for (i
= acc
->n_must
; i
< acc
->n_must
+ acc
->n_may
; ++i
) {
417 dim
= space_align_and_join(
418 isl_map_get_space(acc
->source
[i
].map
),
419 isl_space_reverse(isl_map_get_space(acc
->sink
.map
)));
420 dep
->dep
[acc
->n_must
+ i
].map
= isl_map_empty(dim
);
421 dep
->dep
[acc
->n_must
+ i
].data
= acc
->source
[i
].data
;
422 dep
->dep
[acc
->n_must
+ i
].must
= 0;
423 if (!dep
->dep
[acc
->n_must
+ i
].map
)
433 /* Iterate over all sources and for each resulting flow dependence
434 * that is not empty, call the user specfied function.
435 * The second argument in this function call identifies the source,
436 * while the third argument correspond to the final argument of
437 * the isl_flow_foreach call.
439 int isl_flow_foreach(__isl_keep isl_flow
*deps
,
440 int (*fn
)(__isl_take isl_map
*dep
, int must
, void *dep_user
, void *user
),
448 for (i
= 0; i
< deps
->n_source
; ++i
) {
449 if (isl_map_plain_is_empty(deps
->dep
[i
].map
))
451 if (fn(isl_map_copy(deps
->dep
[i
].map
), deps
->dep
[i
].must
,
452 deps
->dep
[i
].data
, user
) < 0)
459 /* Return a copy of the subset of the sink for which no source could be found.
461 __isl_give isl_map
*isl_flow_get_no_source(__isl_keep isl_flow
*deps
, int must
)
467 return isl_set_unwrap(isl_set_copy(deps
->must_no_source
));
469 return isl_set_unwrap(isl_set_copy(deps
->may_no_source
));
472 void isl_flow_free(__isl_take isl_flow
*deps
)
478 isl_set_free(deps
->must_no_source
);
479 isl_set_free(deps
->may_no_source
);
481 for (i
= 0; i
< deps
->n_source
; ++i
)
482 isl_map_free(deps
->dep
[i
].map
);
488 isl_ctx
*isl_flow_get_ctx(__isl_keep isl_flow
*deps
)
490 return deps
? isl_set_get_ctx(deps
->must_no_source
) : NULL
;
493 /* Return a map that enforces that the domain iteration occurs after
494 * the range iteration at the given level.
495 * If level is odd, then the domain iteration should occur after
496 * the target iteration in their shared level/2 outermost loops.
497 * In this case we simply need to enforce that these outermost
498 * loop iterations are the same.
499 * If level is even, then the loop iterator of the domain should
500 * be greater than the loop iterator of the range at the last
501 * of the level/2 shared loops, i.e., loop level/2 - 1.
503 static __isl_give isl_map
*after_at_level(__isl_take isl_space
*dim
, int level
)
505 struct isl_basic_map
*bmap
;
508 bmap
= isl_basic_map_equal(dim
, level
/2);
510 bmap
= isl_basic_map_more_at(dim
, level
/2 - 1);
512 return isl_map_from_basic_map(bmap
);
515 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
516 * but first check if the user has set acc->restrict_fn and if so
517 * update either the input or the output of the maximization problem
518 * with respect to the resulting restriction.
520 * Since the user expects a mapping from sink iterations to source iterations,
521 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
522 * to accessed array elements, we first need to project out the accessed
523 * sink array elements by applying acc->domain_map.
524 * Similarly, the sink restriction specified by the user needs to be
525 * converted back to the wrapped map.
527 static __isl_give isl_map
*restricted_partial_lexmax(
528 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*dep
,
529 int source
, __isl_take isl_set
*sink
, __isl_give isl_set
**empty
)
532 isl_restriction
*restr
;
533 isl_set
*sink_domain
;
537 if (!acc
->restrict_fn
)
538 return isl_map_partial_lexmax(dep
, sink
, empty
);
540 source_map
= isl_map_copy(dep
);
541 source_map
= isl_map_apply_domain(source_map
,
542 isl_map_copy(acc
->domain_map
));
543 sink_domain
= isl_set_copy(sink
);
544 sink_domain
= isl_set_apply(sink_domain
, isl_map_copy(acc
->domain_map
));
545 restr
= acc
->restrict_fn(source_map
, sink_domain
,
546 acc
->source
[source
].data
, acc
->restrict_user
);
547 isl_set_free(sink_domain
);
548 isl_map_free(source_map
);
552 if (restr
->type
== isl_restriction_type_input
) {
553 dep
= isl_map_intersect_range(dep
, isl_set_copy(restr
->source
));
554 sink_restr
= isl_set_copy(restr
->sink
);
555 sink_restr
= isl_set_apply(sink_restr
,
556 isl_map_reverse(isl_map_copy(acc
->domain_map
)));
557 sink
= isl_set_intersect(sink
, sink_restr
);
558 } else if (restr
->type
== isl_restriction_type_empty
) {
559 isl_space
*space
= isl_map_get_space(dep
);
561 dep
= isl_map_empty(space
);
564 res
= isl_map_partial_lexmax(dep
, sink
, empty
);
566 if (restr
->type
== isl_restriction_type_output
)
567 res
= isl_map_intersect_range(res
, isl_set_copy(restr
->source
));
569 isl_restriction_free(restr
);
578 /* Compute the last iteration of must source j that precedes the sink
579 * at the given level for sink iterations in set_C.
580 * The subset of set_C for which no such iteration can be found is returned
583 static struct isl_map
*last_source(struct isl_access_info
*acc
,
584 struct isl_set
*set_C
,
585 int j
, int level
, struct isl_set
**empty
)
587 struct isl_map
*read_map
;
588 struct isl_map
*write_map
;
589 struct isl_map
*dep_map
;
590 struct isl_map
*after
;
591 struct isl_map
*result
;
593 read_map
= isl_map_copy(acc
->sink
.map
);
594 write_map
= isl_map_copy(acc
->source
[j
].map
);
595 write_map
= isl_map_reverse(write_map
);
596 dep_map
= isl_map_apply_range(read_map
, write_map
);
597 after
= after_at_level(isl_map_get_space(dep_map
), level
);
598 dep_map
= isl_map_intersect(dep_map
, after
);
599 result
= restricted_partial_lexmax(acc
, dep_map
, j
, set_C
, empty
);
600 result
= isl_map_reverse(result
);
605 /* For a given mapping between iterations of must source j and iterations
606 * of the sink, compute the last iteration of must source k preceding
607 * the sink at level before_level for any of the sink iterations,
608 * but following the corresponding iteration of must source j at level
611 static struct isl_map
*last_later_source(struct isl_access_info
*acc
,
612 struct isl_map
*old_map
,
613 int j
, int before_level
,
614 int k
, int after_level
,
615 struct isl_set
**empty
)
618 struct isl_set
*set_C
;
619 struct isl_map
*read_map
;
620 struct isl_map
*write_map
;
621 struct isl_map
*dep_map
;
622 struct isl_map
*after_write
;
623 struct isl_map
*before_read
;
624 struct isl_map
*result
;
626 set_C
= isl_map_range(isl_map_copy(old_map
));
627 read_map
= isl_map_copy(acc
->sink
.map
);
628 write_map
= isl_map_copy(acc
->source
[k
].map
);
630 write_map
= isl_map_reverse(write_map
);
631 dep_map
= isl_map_apply_range(read_map
, write_map
);
632 dim
= space_align_and_join(isl_map_get_space(acc
->source
[k
].map
),
633 isl_space_reverse(isl_map_get_space(acc
->source
[j
].map
)));
634 after_write
= after_at_level(dim
, after_level
);
635 after_write
= isl_map_apply_range(after_write
, old_map
);
636 after_write
= isl_map_reverse(after_write
);
637 dep_map
= isl_map_intersect(dep_map
, after_write
);
638 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
639 dep_map
= isl_map_intersect(dep_map
, before_read
);
640 result
= restricted_partial_lexmax(acc
, dep_map
, k
, set_C
, empty
);
641 result
= isl_map_reverse(result
);
646 /* Given a shared_level between two accesses, return 1 if the
647 * the first can precede the second at the requested target_level.
648 * If the target level is odd, i.e., refers to a statement level
649 * dimension, then first needs to precede second at the requested
650 * level, i.e., shared_level must be equal to target_level.
651 * If the target level is odd, then the two loops should share
652 * at least the requested number of outer loops.
654 static int can_precede_at_level(int shared_level
, int target_level
)
656 if (shared_level
< target_level
)
658 if ((target_level
% 2) && shared_level
> target_level
)
663 /* Given a possible flow dependence temp_rel[j] between source j and the sink
664 * at level sink_level, remove those elements for which
665 * there is an iteration of another source k < j that is closer to the sink.
666 * The flow dependences temp_rel[k] are updated with the improved sources.
667 * Any improved source needs to precede the sink at the same level
668 * and needs to follow source j at the same or a deeper level.
669 * The lower this level, the later the execution date of source k.
670 * We therefore consider lower levels first.
672 * If temp_rel[j] is empty, then there can be no improvement and
673 * we return immediately.
675 static int intermediate_sources(__isl_keep isl_access_info
*acc
,
676 struct isl_map
**temp_rel
, int j
, int sink_level
)
679 int depth
= 2 * isl_map_dim(acc
->source
[j
].map
, isl_dim_in
) + 1;
681 if (isl_map_plain_is_empty(temp_rel
[j
]))
684 for (k
= j
- 1; k
>= 0; --k
) {
686 plevel
= acc
->level_before(acc
->source
[k
].data
, acc
->sink
.data
);
687 if (!can_precede_at_level(plevel
, sink_level
))
690 plevel2
= acc
->level_before(acc
->source
[j
].data
,
691 acc
->source
[k
].data
);
693 for (level
= sink_level
; level
<= depth
; ++level
) {
695 struct isl_set
*trest
;
696 struct isl_map
*copy
;
698 if (!can_precede_at_level(plevel2
, level
))
701 copy
= isl_map_copy(temp_rel
[j
]);
702 T
= last_later_source(acc
, copy
, j
, sink_level
, k
,
704 if (isl_map_plain_is_empty(T
)) {
709 temp_rel
[j
] = isl_map_intersect_range(temp_rel
[j
], trest
);
710 temp_rel
[k
] = isl_map_union_disjoint(temp_rel
[k
], T
);
717 /* Compute all iterations of may source j that precedes the sink at the given
718 * level for sink iterations in set_C.
720 static __isl_give isl_map
*all_sources(__isl_keep isl_access_info
*acc
,
721 __isl_take isl_set
*set_C
, int j
, int level
)
728 read_map
= isl_map_copy(acc
->sink
.map
);
729 read_map
= isl_map_intersect_domain(read_map
, set_C
);
730 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
731 write_map
= isl_map_reverse(write_map
);
732 dep_map
= isl_map_apply_range(read_map
, write_map
);
733 after
= after_at_level(isl_map_get_space(dep_map
), level
);
734 dep_map
= isl_map_intersect(dep_map
, after
);
736 return isl_map_reverse(dep_map
);
739 /* For a given mapping between iterations of must source k and iterations
740 * of the sink, compute the all iteration of may source j preceding
741 * the sink at level before_level for any of the sink iterations,
742 * but following the corresponding iteration of must source k at level
745 static __isl_give isl_map
*all_later_sources(__isl_keep isl_access_info
*acc
,
746 __isl_take isl_map
*old_map
,
747 int j
, int before_level
, int k
, int after_level
)
754 isl_map
*after_write
;
755 isl_map
*before_read
;
757 set_C
= isl_map_range(isl_map_copy(old_map
));
758 read_map
= isl_map_copy(acc
->sink
.map
);
759 read_map
= isl_map_intersect_domain(read_map
, set_C
);
760 write_map
= isl_map_copy(acc
->source
[acc
->n_must
+ j
].map
);
762 write_map
= isl_map_reverse(write_map
);
763 dep_map
= isl_map_apply_range(read_map
, write_map
);
764 dim
= isl_space_join(isl_map_get_space(acc
->source
[acc
->n_must
+ j
].map
),
765 isl_space_reverse(isl_map_get_space(acc
->source
[k
].map
)));
766 after_write
= after_at_level(dim
, after_level
);
767 after_write
= isl_map_apply_range(after_write
, old_map
);
768 after_write
= isl_map_reverse(after_write
);
769 dep_map
= isl_map_intersect(dep_map
, after_write
);
770 before_read
= after_at_level(isl_map_get_space(dep_map
), before_level
);
771 dep_map
= isl_map_intersect(dep_map
, before_read
);
772 return isl_map_reverse(dep_map
);
775 /* Given the must and may dependence relations for the must accesses
776 * for level sink_level, check if there are any accesses of may access j
777 * that occur in between and return their union.
778 * If some of these accesses are intermediate with respect to
779 * (previously thought to be) must dependences, then these
780 * must dependences are turned into may dependences.
782 static __isl_give isl_map
*all_intermediate_sources(
783 __isl_keep isl_access_info
*acc
, __isl_take isl_map
*map
,
784 struct isl_map
**must_rel
, struct isl_map
**may_rel
,
785 int j
, int sink_level
)
788 int depth
= 2 * isl_map_dim(acc
->source
[acc
->n_must
+ j
].map
,
791 for (k
= 0; k
< acc
->n_must
; ++k
) {
794 if (isl_map_plain_is_empty(may_rel
[k
]) &&
795 isl_map_plain_is_empty(must_rel
[k
]))
798 plevel
= acc
->level_before(acc
->source
[k
].data
,
799 acc
->source
[acc
->n_must
+ j
].data
);
801 for (level
= sink_level
; level
<= depth
; ++level
) {
806 if (!can_precede_at_level(plevel
, level
))
809 copy
= isl_map_copy(may_rel
[k
]);
810 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
811 map
= isl_map_union(map
, T
);
813 copy
= isl_map_copy(must_rel
[k
]);
814 T
= all_later_sources(acc
, copy
, j
, sink_level
, k
, level
);
815 ran
= isl_map_range(isl_map_copy(T
));
816 map
= isl_map_union(map
, T
);
817 may_rel
[k
] = isl_map_union_disjoint(may_rel
[k
],
818 isl_map_intersect_range(isl_map_copy(must_rel
[k
]),
820 T
= isl_map_from_domain_and_range(
822 isl_space_domain(isl_map_get_space(must_rel
[k
]))),
824 must_rel
[k
] = isl_map_subtract(must_rel
[k
], T
);
831 /* Compute dependences for the case where all accesses are "may"
832 * accesses, which boils down to computing memory based dependences.
833 * The generic algorithm would also work in this case, but it would
834 * be overkill to use it.
836 static __isl_give isl_flow
*compute_mem_based_dependences(
837 __isl_keep isl_access_info
*acc
)
844 res
= isl_flow_alloc(acc
);
848 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
849 maydo
= isl_set_copy(mustdo
);
851 for (i
= 0; i
< acc
->n_may
; ++i
) {
858 plevel
= acc
->level_before(acc
->source
[i
].data
, acc
->sink
.data
);
859 is_before
= plevel
& 1;
862 dim
= isl_map_get_space(res
->dep
[i
].map
);
864 before
= isl_map_lex_le_first(dim
, plevel
);
866 before
= isl_map_lex_lt_first(dim
, plevel
);
867 dep
= isl_map_apply_range(isl_map_copy(acc
->source
[i
].map
),
868 isl_map_reverse(isl_map_copy(acc
->sink
.map
)));
869 dep
= isl_map_intersect(dep
, before
);
870 mustdo
= isl_set_subtract(mustdo
,
871 isl_map_range(isl_map_copy(dep
)));
872 res
->dep
[i
].map
= isl_map_union(res
->dep
[i
].map
, dep
);
875 res
->may_no_source
= isl_set_subtract(maydo
, isl_set_copy(mustdo
));
876 res
->must_no_source
= mustdo
;
881 /* Compute dependences for the case where there is at least one
884 * The core algorithm considers all levels in which a source may precede
885 * the sink, where a level may either be a statement level or a loop level.
886 * The outermost statement level is 1, the first loop level is 2, etc...
887 * The algorithm basically does the following:
888 * for all levels l of the read access from innermost to outermost
889 * for all sources w that may precede the sink access at that level
890 * compute the last iteration of the source that precedes the sink access
892 * add result to possible last accesses at level l of source w
893 * for all sources w2 that we haven't considered yet at this level that may
894 * also precede the sink access
895 * for all levels l2 of w from l to innermost
896 * for all possible last accesses dep of w at l
897 * compute last iteration of w2 between the source and sink
899 * add result to possible last accesses at level l of write w2
900 * and replace possible last accesses dep by the remainder
903 * The above algorithm is applied to the must access. During the course
904 * of the algorithm, we keep track of sink iterations that still
905 * need to be considered. These iterations are split into those that
906 * haven't been matched to any source access (mustdo) and those that have only
907 * been matched to may accesses (maydo).
908 * At the end of each level, we also consider the may accesses.
909 * In particular, we consider may accesses that precede the remaining
910 * sink iterations, moving elements from mustdo to maydo when appropriate,
911 * and may accesses that occur between a must source and a sink of any
912 * dependences found at the current level, turning must dependences into
913 * may dependences when appropriate.
916 static __isl_give isl_flow
*compute_val_based_dependences(
917 __isl_keep isl_access_info
*acc
)
921 isl_set
*mustdo
= NULL
;
922 isl_set
*maydo
= NULL
;
925 isl_map
**must_rel
= NULL
;
926 isl_map
**may_rel
= NULL
;
931 res
= isl_flow_alloc(acc
);
934 ctx
= isl_map_get_ctx(acc
->sink
.map
);
936 depth
= 2 * isl_map_dim(acc
->sink
.map
, isl_dim_in
) + 1;
937 mustdo
= isl_map_domain(isl_map_copy(acc
->sink
.map
));
938 maydo
= isl_set_empty_like(mustdo
);
939 if (!mustdo
|| !maydo
)
941 if (isl_set_plain_is_empty(mustdo
))
944 must_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
945 may_rel
= isl_alloc_array(ctx
, struct isl_map
*, acc
->n_must
);
946 if (!must_rel
|| !may_rel
)
949 for (level
= depth
; level
>= 1; --level
) {
950 for (j
= acc
->n_must
-1; j
>=0; --j
) {
951 must_rel
[j
] = isl_map_empty_like(res
->dep
[2 * j
].map
);
952 may_rel
[j
] = isl_map_copy(must_rel
[j
]);
955 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
957 struct isl_set
*rest
;
960 plevel
= acc
->level_before(acc
->source
[j
].data
,
962 if (!can_precede_at_level(plevel
, level
))
965 T
= last_source(acc
, mustdo
, j
, level
, &rest
);
966 must_rel
[j
] = isl_map_union_disjoint(must_rel
[j
], T
);
969 intermediate_sources(acc
, must_rel
, j
, level
);
971 T
= last_source(acc
, maydo
, j
, level
, &rest
);
972 may_rel
[j
] = isl_map_union_disjoint(may_rel
[j
], T
);
975 intermediate_sources(acc
, may_rel
, j
, level
);
977 if (isl_set_plain_is_empty(mustdo
) &&
978 isl_set_plain_is_empty(maydo
))
981 for (j
= j
- 1; j
>= 0; --j
) {
984 plevel
= acc
->level_before(acc
->source
[j
].data
,
986 if (!can_precede_at_level(plevel
, level
))
989 intermediate_sources(acc
, must_rel
, j
, level
);
990 intermediate_sources(acc
, may_rel
, j
, level
);
993 for (j
= 0; j
< acc
->n_may
; ++j
) {
998 plevel
= acc
->level_before(acc
->source
[acc
->n_must
+ j
].data
,
1000 if (!can_precede_at_level(plevel
, level
))
1003 T
= all_sources(acc
, isl_set_copy(maydo
), j
, level
);
1004 res
->dep
[2 * acc
->n_must
+ j
].map
=
1005 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1006 T
= all_sources(acc
, isl_set_copy(mustdo
), j
, level
);
1007 ran
= isl_map_range(isl_map_copy(T
));
1008 res
->dep
[2 * acc
->n_must
+ j
].map
=
1009 isl_map_union(res
->dep
[2 * acc
->n_must
+ j
].map
, T
);
1010 mustdo
= isl_set_subtract(mustdo
, isl_set_copy(ran
));
1011 maydo
= isl_set_union_disjoint(maydo
, ran
);
1013 T
= res
->dep
[2 * acc
->n_must
+ j
].map
;
1014 T
= all_intermediate_sources(acc
, T
, must_rel
, may_rel
,
1016 res
->dep
[2 * acc
->n_must
+ j
].map
= T
;
1019 for (j
= acc
->n_must
- 1; j
>= 0; --j
) {
1020 res
->dep
[2 * j
].map
=
1021 isl_map_union_disjoint(res
->dep
[2 * j
].map
,
1023 res
->dep
[2 * j
+ 1].map
=
1024 isl_map_union_disjoint(res
->dep
[2 * j
+ 1].map
,
1028 if (isl_set_plain_is_empty(mustdo
) &&
1029 isl_set_plain_is_empty(maydo
))
1036 res
->must_no_source
= mustdo
;
1037 res
->may_no_source
= maydo
;
1041 isl_set_free(mustdo
);
1042 isl_set_free(maydo
);
1048 /* Given a "sink" access, a list of n "source" accesses,
1049 * compute for each iteration of the sink access
1050 * and for each element accessed by that iteration,
1051 * the source access in the list that last accessed the
1052 * element accessed by the sink access before this sink access.
1053 * Each access is given as a map from the loop iterators
1054 * to the array indices.
1055 * The result is a list of n relations between source and sink
1056 * iterations and a subset of the domain of the sink access,
1057 * corresponding to those iterations that access an element
1058 * not previously accessed.
1060 * To deal with multi-valued sink access relations, the sink iteration
1061 * domain is first extended with dimensions that correspond to the data
1062 * space. After the computation is finished, these extra dimensions are
1063 * projected out again.
1065 __isl_give isl_flow
*isl_access_info_compute_flow(__isl_take isl_access_info
*acc
)
1068 struct isl_flow
*res
= NULL
;
1073 acc
->domain_map
= isl_map_domain_map(isl_map_copy(acc
->sink
.map
));
1074 acc
->sink
.map
= isl_map_range_map(acc
->sink
.map
);
1078 if (acc
->n_must
== 0)
1079 res
= compute_mem_based_dependences(acc
);
1081 acc
= isl_access_info_sort_sources(acc
);
1082 res
= compute_val_based_dependences(acc
);
1087 for (j
= 0; j
< res
->n_source
; ++j
) {
1088 res
->dep
[j
].map
= isl_map_apply_range(res
->dep
[j
].map
,
1089 isl_map_copy(acc
->domain_map
));
1090 if (!res
->dep
[j
].map
)
1093 if (!res
->must_no_source
|| !res
->may_no_source
)
1096 isl_access_info_free(acc
);
1099 isl_access_info_free(acc
);
1105 /* Keep track of some information about a schedule for a given
1106 * access. In particular, keep track of which dimensions
1107 * have a constant value and of the actual constant values.
1109 struct isl_sched_info
{
1114 static void sched_info_free(__isl_take
struct isl_sched_info
*info
)
1118 isl_vec_free(info
->cst
);
1123 /* Extract information on the constant dimensions of the schedule
1124 * for a given access. The "map" is of the form
1128 * with S the schedule domain, D the iteration domain and A the data domain.
1130 static __isl_give
struct isl_sched_info
*sched_info_alloc(
1131 __isl_keep isl_map
*map
)
1135 struct isl_sched_info
*info
;
1141 dim
= isl_space_unwrap(isl_space_domain(isl_map_get_space(map
)));
1144 n
= isl_space_dim(dim
, isl_dim_in
);
1145 isl_space_free(dim
);
1147 ctx
= isl_map_get_ctx(map
);
1148 info
= isl_alloc_type(ctx
, struct isl_sched_info
);
1151 info
->is_cst
= isl_alloc_array(ctx
, int, n
);
1152 info
->cst
= isl_vec_alloc(ctx
, n
);
1153 if (n
&& (!info
->is_cst
|| !info
->cst
))
1156 for (i
= 0; i
< n
; ++i
) {
1159 v
= isl_map_plain_get_val_if_fixed(map
, isl_dim_in
, i
);
1162 info
->is_cst
[i
] = !isl_val_is_nan(v
);
1163 if (info
->is_cst
[i
])
1164 info
->cst
= isl_vec_set_element_val(info
->cst
, i
, v
);
1171 sched_info_free(info
);
1175 /* This structure represents the input for a dependence analysis computation.
1177 * "sink" represents the sink accesses.
1178 * "must_source" represents the definite source accesses.
1179 * "may_source" represents the possible source accesses.
1181 * "schedule" or "schedule_map" represents the execution order.
1182 * Exactly one of these fields should be NULL. The other field
1183 * determines the execution order.
1185 * The domains of these four maps refer to the same iteration spaces(s).
1186 * The ranges of the first three maps also refer to the same data space(s).
1188 * After a call to isl_union_access_info_introduce_schedule,
1189 * the "schedule_map" field no longer contains useful information.
1191 struct isl_union_access_info
{
1192 isl_union_map
*sink
;
1193 isl_union_map
*must_source
;
1194 isl_union_map
*may_source
;
1196 isl_schedule
*schedule
;
1197 isl_union_map
*schedule_map
;
1200 /* Free "access" and return NULL.
1202 __isl_null isl_union_access_info
*isl_union_access_info_free(
1203 __isl_take isl_union_access_info
*access
)
1208 isl_union_map_free(access
->sink
);
1209 isl_union_map_free(access
->must_source
);
1210 isl_union_map_free(access
->may_source
);
1211 isl_schedule_free(access
->schedule
);
1212 isl_union_map_free(access
->schedule_map
);
1218 /* Return the isl_ctx to which "access" belongs.
1220 isl_ctx
*isl_union_access_info_get_ctx(__isl_keep isl_union_access_info
*access
)
1222 return access
? isl_union_map_get_ctx(access
->sink
) : NULL
;
1225 /* Create a new isl_union_access_info with the given sink accesses and
1226 * and no source accesses or schedule information.
1228 * By default, we use the schedule field of the isl_union_access_info,
1229 * but this may be overridden by a call
1230 * to isl_union_access_info_set_schedule_map.
1232 __isl_give isl_union_access_info
*isl_union_access_info_from_sink(
1233 __isl_take isl_union_map
*sink
)
1237 isl_union_map
*empty
;
1238 isl_union_access_info
*access
;
1242 ctx
= isl_union_map_get_ctx(sink
);
1243 access
= isl_alloc_type(ctx
, isl_union_access_info
);
1247 space
= isl_union_map_get_space(sink
);
1248 empty
= isl_union_map_empty(isl_space_copy(space
));
1249 access
->sink
= sink
;
1250 access
->must_source
= isl_union_map_copy(empty
);
1251 access
->may_source
= empty
;
1252 access
->schedule
= isl_schedule_empty(space
);
1253 access
->schedule_map
= NULL
;
1255 if (!access
->sink
|| !access
->must_source
||
1256 !access
->may_source
|| !access
->schedule
)
1257 return isl_union_access_info_free(access
);
1261 isl_union_map_free(sink
);
1265 /* Replace the definite source accesses of "access" by "must_source".
1267 __isl_give isl_union_access_info
*isl_union_access_info_set_must_source(
1268 __isl_take isl_union_access_info
*access
,
1269 __isl_take isl_union_map
*must_source
)
1271 if (!access
|| !must_source
)
1274 isl_union_map_free(access
->must_source
);
1275 access
->must_source
= must_source
;
1279 isl_union_access_info_free(access
);
1280 isl_union_map_free(must_source
);
1284 /* Replace the possible source accesses of "access" by "may_source".
1286 __isl_give isl_union_access_info
*isl_union_access_info_set_may_source(
1287 __isl_take isl_union_access_info
*access
,
1288 __isl_take isl_union_map
*may_source
)
1290 if (!access
|| !may_source
)
1293 isl_union_map_free(access
->may_source
);
1294 access
->may_source
= may_source
;
1298 isl_union_access_info_free(access
);
1299 isl_union_map_free(may_source
);
1303 /* Replace the schedule of "access" by "schedule".
1304 * Also free the schedule_map in case it was set last.
1306 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule(
1307 __isl_take isl_union_access_info
*access
,
1308 __isl_take isl_schedule
*schedule
)
1310 if (!access
|| !schedule
)
1313 access
->schedule_map
= isl_union_map_free(access
->schedule_map
);
1314 isl_schedule_free(access
->schedule
);
1315 access
->schedule
= schedule
;
1319 isl_union_access_info_free(access
);
1320 isl_schedule_free(schedule
);
1324 /* Replace the schedule map of "access" by "schedule_map".
1325 * Also free the schedule in case it was set last.
1327 __isl_give isl_union_access_info
*isl_union_access_info_set_schedule_map(
1328 __isl_take isl_union_access_info
*access
,
1329 __isl_take isl_union_map
*schedule_map
)
1331 if (!access
|| !schedule_map
)
1334 isl_union_map_free(access
->schedule_map
);
1335 access
->schedule
= isl_schedule_free(access
->schedule
);
1336 access
->schedule_map
= schedule_map
;
1340 isl_union_access_info_free(access
);
1341 isl_union_map_free(schedule_map
);
1345 /* Update the fields of "access" such that they all have the same parameters,
1346 * keeping in mind that the schedule_map field may be NULL and ignoring
1347 * the schedule field.
1349 static __isl_give isl_union_access_info
*isl_union_access_info_align_params(
1350 __isl_take isl_union_access_info
*access
)
1357 space
= isl_union_map_get_space(access
->sink
);
1358 space
= isl_space_align_params(space
,
1359 isl_union_map_get_space(access
->must_source
));
1360 space
= isl_space_align_params(space
,
1361 isl_union_map_get_space(access
->may_source
));
1362 if (access
->schedule_map
)
1363 space
= isl_space_align_params(space
,
1364 isl_union_map_get_space(access
->schedule_map
));
1365 access
->sink
= isl_union_map_align_params(access
->sink
,
1366 isl_space_copy(space
));
1367 access
->must_source
= isl_union_map_align_params(access
->must_source
,
1368 isl_space_copy(space
));
1369 access
->may_source
= isl_union_map_align_params(access
->may_source
,
1370 isl_space_copy(space
));
1371 if (!access
->schedule_map
) {
1372 isl_space_free(space
);
1374 access
->schedule_map
=
1375 isl_union_map_align_params(access
->schedule_map
, space
);
1376 if (!access
->schedule_map
)
1377 return isl_union_access_info_free(access
);
1380 if (!access
->sink
|| !access
->must_source
|| !access
->may_source
)
1381 return isl_union_access_info_free(access
);
1386 /* Prepend the schedule dimensions to the iteration domains.
1388 * That is, if the schedule is of the form
1392 * while the access relations are of the form
1396 * then the updated access relations are of the form
1400 * The schedule map is also replaced by the map
1404 * that is used during the internal computation.
1405 * Neither the original schedule map nor this updated schedule map
1406 * are used after the call to this function.
1408 static __isl_give isl_union_access_info
*
1409 isl_union_access_info_introduce_schedule(
1410 __isl_take isl_union_access_info
*access
)
1417 sm
= isl_union_map_reverse(access
->schedule_map
);
1418 sm
= isl_union_map_range_map(sm
);
1419 access
->sink
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1421 access
->may_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1422 access
->may_source
);
1423 access
->must_source
= isl_union_map_apply_range(isl_union_map_copy(sm
),
1424 access
->must_source
);
1425 access
->schedule_map
= sm
;
1427 if (!access
->sink
|| !access
->must_source
||
1428 !access
->may_source
|| !access
->schedule_map
)
1429 return isl_union_access_info_free(access
);
1434 /* This structure epresents the result of a dependence analysis computation.
1436 * "must_dep" represents the definite dependences.
1437 * "may_dep" represents the non-definite dependences.
1438 * "must_no_source" represents the subset of the sink accesses for which
1439 * definitely no source was found.
1440 * "may_no_source" represents the subset of the sink accesses for which
1441 * possibly, but not definitely, no source was found.
1443 struct isl_union_flow
{
1444 isl_union_map
*must_dep
;
1445 isl_union_map
*may_dep
;
1446 isl_union_map
*must_no_source
;
1447 isl_union_map
*may_no_source
;
1450 /* Free "flow" and return NULL.
1452 __isl_null isl_union_flow
*isl_union_flow_free(__isl_take isl_union_flow
*flow
)
1456 isl_union_map_free(flow
->must_dep
);
1457 isl_union_map_free(flow
->may_dep
);
1458 isl_union_map_free(flow
->must_no_source
);
1459 isl_union_map_free(flow
->may_no_source
);
1464 void isl_union_flow_dump(__isl_keep isl_union_flow
*flow
)
1469 fprintf(stderr
, "must dependences: ");
1470 isl_union_map_dump(flow
->must_dep
);
1471 fprintf(stderr
, "may dependences: ");
1472 isl_union_map_dump(flow
->may_dep
);
1473 fprintf(stderr
, "must no source: ");
1474 isl_union_map_dump(flow
->must_no_source
);
1475 fprintf(stderr
, "may no source: ");
1476 isl_union_map_dump(flow
->may_no_source
);
1479 /* Return the definite dependences in "flow".
1481 __isl_give isl_union_map
*isl_union_flow_get_must_dependence(
1482 __isl_keep isl_union_flow
*flow
)
1486 return isl_union_map_copy(flow
->must_dep
);
1489 /* Return the possible dependences in "flow", including the definite
1492 __isl_give isl_union_map
*isl_union_flow_get_may_dependence(
1493 __isl_keep isl_union_flow
*flow
)
1497 return isl_union_map_union(isl_union_map_copy(flow
->must_dep
),
1498 isl_union_map_copy(flow
->may_dep
));
1501 /* Return the non-definite dependences in "flow".
1503 static __isl_give isl_union_map
*isl_union_flow_get_non_must_dependence(
1504 __isl_keep isl_union_flow
*flow
)
1508 return isl_union_map_copy(flow
->may_dep
);
1511 /* Return the subset of the sink accesses for which definitely
1512 * no source was found.
1514 __isl_give isl_union_map
*isl_union_flow_get_must_no_source(
1515 __isl_keep isl_union_flow
*flow
)
1519 return isl_union_map_copy(flow
->must_no_source
);
1522 /* Return the subset of the sink accesses for which possibly
1523 * no source was found, including those for which definitely
1524 * no source was found.
1526 __isl_give isl_union_map
*isl_union_flow_get_may_no_source(
1527 __isl_keep isl_union_flow
*flow
)
1531 return isl_union_map_union(isl_union_map_copy(flow
->must_no_source
),
1532 isl_union_map_copy(flow
->may_no_source
));
1535 /* Return the subset of the sink accesses for which possibly, but not
1536 * definitely, no source was found.
1538 static __isl_give isl_union_map
*isl_union_flow_get_non_must_no_source(
1539 __isl_keep isl_union_flow
*flow
)
1543 return isl_union_map_copy(flow
->may_no_source
);
1546 /* Create a new isl_union_flow object, initialized with empty
1547 * dependence relations and sink subsets.
1549 static __isl_give isl_union_flow
*isl_union_flow_alloc(
1550 __isl_take isl_space
*space
)
1553 isl_union_map
*empty
;
1554 isl_union_flow
*flow
;
1558 ctx
= isl_space_get_ctx(space
);
1559 flow
= isl_alloc_type(ctx
, isl_union_flow
);
1563 empty
= isl_union_map_empty(space
);
1564 flow
->must_dep
= isl_union_map_copy(empty
);
1565 flow
->may_dep
= isl_union_map_copy(empty
);
1566 flow
->must_no_source
= isl_union_map_copy(empty
);
1567 flow
->may_no_source
= empty
;
1569 if (!flow
->must_dep
|| !flow
->may_dep
||
1570 !flow
->must_no_source
|| !flow
->may_no_source
)
1571 return isl_union_flow_free(flow
);
1575 isl_space_free(space
);
1579 /* Drop the schedule dimensions from the iteration domains in "flow".
1580 * In particular, the schedule dimensions have been prepended
1581 * to the iteration domains prior to the dependence analysis by
1582 * replacing the iteration domain D, by the wrapped map [S -> D].
1583 * Replace these wrapped maps by the original D.
1585 static __isl_give isl_union_flow
*isl_union_flow_drop_schedule(
1586 __isl_take isl_union_flow
*flow
)
1591 flow
->must_dep
= isl_union_map_factor_range(flow
->must_dep
);
1592 flow
->may_dep
= isl_union_map_factor_range(flow
->may_dep
);
1593 flow
->must_no_source
=
1594 isl_union_map_domain_factor_range(flow
->must_no_source
);
1595 flow
->may_no_source
=
1596 isl_union_map_domain_factor_range(flow
->may_no_source
);
1598 if (!flow
->must_dep
|| !flow
->may_dep
||
1599 !flow
->must_no_source
|| !flow
->may_no_source
)
1600 return isl_union_flow_free(flow
);
1605 struct isl_compute_flow_data
{
1606 isl_union_map
*must_source
;
1607 isl_union_map
*may_source
;
1608 isl_union_flow
*flow
;
1613 struct isl_sched_info
*sink_info
;
1614 struct isl_sched_info
**source_info
;
1615 isl_access_info
*accesses
;
1618 static int count_matching_array(__isl_take isl_map
*map
, void *user
)
1622 struct isl_compute_flow_data
*data
;
1624 data
= (struct isl_compute_flow_data
*)user
;
1626 dim
= isl_space_range(isl_map_get_space(map
));
1628 eq
= isl_space_is_equal(dim
, data
->dim
);
1630 isl_space_free(dim
);
1641 static int collect_matching_array(__isl_take isl_map
*map
, void *user
)
1645 struct isl_sched_info
*info
;
1646 struct isl_compute_flow_data
*data
;
1648 data
= (struct isl_compute_flow_data
*)user
;
1650 dim
= isl_space_range(isl_map_get_space(map
));
1652 eq
= isl_space_is_equal(dim
, data
->dim
);
1654 isl_space_free(dim
);
1663 info
= sched_info_alloc(map
);
1664 data
->source_info
[data
->count
] = info
;
1666 data
->accesses
= isl_access_info_add_source(data
->accesses
,
1667 map
, data
->must
, info
);
1677 /* Determine the shared nesting level and the "textual order" of
1678 * the given accesses.
1680 * We first determine the minimal schedule dimension for both accesses.
1682 * If among those dimensions, we can find one where both have a fixed
1683 * value and if moreover those values are different, then the previous
1684 * dimension is the last shared nesting level and the textual order
1685 * is determined based on the order of the fixed values.
1686 * If no such fixed values can be found, then we set the shared
1687 * nesting level to the minimal schedule dimension, with no textual ordering.
1689 static int before(void *first
, void *second
)
1691 struct isl_sched_info
*info1
= first
;
1692 struct isl_sched_info
*info2
= second
;
1696 n1
= isl_vec_size(info1
->cst
);
1697 n2
= isl_vec_size(info2
->cst
);
1702 for (i
= 0; i
< n1
; ++i
) {
1706 if (!info1
->is_cst
[i
])
1708 if (!info2
->is_cst
[i
])
1710 cmp
= isl_vec_cmp_element(info1
->cst
, info2
->cst
, i
);
1714 r
= 2 * i
+ (cmp
< 0);
1722 /* Given a sink access, look for all the source accesses that access
1723 * the same array and perform dataflow analysis on them using
1724 * isl_access_info_compute_flow.
1726 static int compute_flow(__isl_take isl_map
*map
, void *user
)
1730 struct isl_compute_flow_data
*data
;
1734 data
= (struct isl_compute_flow_data
*)user
;
1737 ctx
= isl_map_get_ctx(map
);
1739 data
->accesses
= NULL
;
1740 data
->sink_info
= NULL
;
1741 data
->source_info
= NULL
;
1743 data
->dim
= isl_space_range(isl_map_get_space(map
));
1745 if (isl_union_map_foreach_map(data
->must_source
,
1746 &count_matching_array
, data
) < 0)
1748 if (isl_union_map_foreach_map(data
->may_source
,
1749 &count_matching_array
, data
) < 0)
1752 data
->sink_info
= sched_info_alloc(map
);
1753 data
->source_info
= isl_calloc_array(ctx
, struct isl_sched_info
*,
1756 data
->accesses
= isl_access_info_alloc(isl_map_copy(map
),
1757 data
->sink_info
, &before
, data
->count
);
1758 if (!data
->sink_info
|| (data
->count
&& !data
->source_info
) ||
1763 if (isl_union_map_foreach_map(data
->must_source
,
1764 &collect_matching_array
, data
) < 0)
1767 if (isl_union_map_foreach_map(data
->may_source
,
1768 &collect_matching_array
, data
) < 0)
1771 flow
= isl_access_info_compute_flow(data
->accesses
);
1772 data
->accesses
= NULL
;
1777 df
->must_no_source
= isl_union_map_union(df
->must_no_source
,
1778 isl_union_map_from_map(isl_flow_get_no_source(flow
, 1)));
1779 df
->may_no_source
= isl_union_map_union(df
->may_no_source
,
1780 isl_union_map_from_map(isl_flow_get_no_source(flow
, 0)));
1782 for (i
= 0; i
< flow
->n_source
; ++i
) {
1784 dep
= isl_union_map_from_map(isl_map_copy(flow
->dep
[i
].map
));
1785 if (flow
->dep
[i
].must
)
1786 df
->must_dep
= isl_union_map_union(df
->must_dep
, dep
);
1788 df
->may_dep
= isl_union_map_union(df
->may_dep
, dep
);
1791 isl_flow_free(flow
);
1793 sched_info_free(data
->sink_info
);
1794 if (data
->source_info
) {
1795 for (i
= 0; i
< data
->count
; ++i
)
1796 sched_info_free(data
->source_info
[i
]);
1797 free(data
->source_info
);
1799 isl_space_free(data
->dim
);
1804 isl_access_info_free(data
->accesses
);
1805 sched_info_free(data
->sink_info
);
1806 if (data
->source_info
) {
1807 for (i
= 0; i
< data
->count
; ++i
)
1808 sched_info_free(data
->source_info
[i
]);
1809 free(data
->source_info
);
1811 isl_space_free(data
->dim
);
1817 /* Remove the must accesses from the may accesses.
1819 * A must access always trumps a may access, so there is no need
1820 * for a must access to also be considered as a may access. Doing so
1821 * would only cost extra computations only to find out that
1822 * the duplicated may access does not make any difference.
1824 static __isl_give isl_union_access_info
*isl_union_access_info_normalize(
1825 __isl_take isl_union_access_info
*access
)
1829 access
->may_source
= isl_union_map_subtract(access
->may_source
,
1830 isl_union_map_copy(access
->must_source
));
1831 if (!access
->may_source
)
1832 return isl_union_access_info_free(access
);
1837 /* Given a description of the "sink" accesses, the "source" accesses and
1838 * a schedule, compute for each instance of a sink access
1839 * and for each element accessed by that instance,
1840 * the possible or definite source accesses that last accessed the
1841 * element accessed by the sink access before this sink access
1842 * in the sense that there is no intermediate definite source access.
1844 * The must_no_source and may_no_source elements of the result
1845 * are subsets of access->sink. The elements must_dep and may_dep
1846 * map domain elements of access->{may,must)_source to
1847 * domain elements of access->sink.
1849 * This function is used when only the schedule map representation
1852 * We first prepend the schedule dimensions to the domain
1853 * of the accesses so that we can easily compare their relative order.
1854 * Then we consider each sink access individually in compute_flow.
1856 static __isl_give isl_union_flow
*compute_flow_union_map(
1857 __isl_take isl_union_access_info
*access
)
1859 struct isl_compute_flow_data data
;
1861 access
= isl_union_access_info_align_params(access
);
1862 access
= isl_union_access_info_introduce_schedule(access
);
1866 data
.must_source
= access
->must_source
;
1867 data
.may_source
= access
->may_source
;
1869 data
.flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
1871 if (isl_union_map_foreach_map(access
->sink
, &compute_flow
, &data
) < 0)
1874 data
.flow
= isl_union_flow_drop_schedule(data
.flow
);
1876 isl_union_access_info_free(access
);
1879 isl_union_access_info_free(access
);
1880 isl_union_flow_free(data
.flow
);
1884 /* A schedule access relation.
1886 * The access relation "access" is of the form [S -> D] -> A,
1887 * where S corresponds to the prefix schedule at "node".
1888 * "must" is only relevant for source accesses and indicates
1889 * whether the access is a must source or a may source.
1891 struct isl_scheduled_access
{
1894 isl_schedule_node
*node
;
1897 /* Data structure for keeping track of individual scheduled sink and source
1898 * accesses when computing dependence analysis based on a schedule tree.
1900 * "n_sink" is the number of used entries in "sink"
1901 * "n_source" is the number of used entries in "source"
1903 * "set_sink", "must" and "node" are only used inside collect_sink_source,
1904 * to keep track of the current node and
1905 * of what extract_sink_source needs to do.
1907 struct isl_compute_flow_schedule_data
{
1908 isl_union_access_info
*access
;
1913 struct isl_scheduled_access
*sink
;
1914 struct isl_scheduled_access
*source
;
1918 isl_schedule_node
*node
;
1921 /* Align the parameters of all sinks with all sources.
1923 * If there are no sinks or no sources, then no alignment is needed.
1925 static void isl_compute_flow_schedule_data_align_params(
1926 struct isl_compute_flow_schedule_data
*data
)
1931 if (data
->n_sink
== 0 || data
->n_source
== 0)
1934 space
= isl_map_get_space(data
->sink
[0].access
);
1936 for (i
= 1; i
< data
->n_sink
; ++i
)
1937 space
= isl_space_align_params(space
,
1938 isl_map_get_space(data
->sink
[i
].access
));
1939 for (i
= 0; i
< data
->n_source
; ++i
)
1940 space
= isl_space_align_params(space
,
1941 isl_map_get_space(data
->source
[i
].access
));
1943 for (i
= 0; i
< data
->n_sink
; ++i
)
1944 data
->sink
[i
].access
=
1945 isl_map_align_params(data
->sink
[i
].access
,
1946 isl_space_copy(space
));
1947 for (i
= 0; i
< data
->n_source
; ++i
)
1948 data
->source
[i
].access
=
1949 isl_map_align_params(data
->source
[i
].access
,
1950 isl_space_copy(space
));
1952 isl_space_free(space
);
1955 /* Free all the memory referenced from "data".
1956 * Do not free "data" itself as it may be allocated on the stack.
1958 static void isl_compute_flow_schedule_data_clear(
1959 struct isl_compute_flow_schedule_data
*data
)
1966 for (i
= 0; i
< data
->n_sink
; ++i
) {
1967 isl_map_free(data
->sink
[i
].access
);
1968 isl_schedule_node_free(data
->sink
[i
].node
);
1971 for (i
= 0; i
< data
->n_source
; ++i
) {
1972 isl_map_free(data
->source
[i
].access
);
1973 isl_schedule_node_free(data
->source
[i
].node
);
1979 /* isl_schedule_foreach_schedule_node callback for counting
1980 * (an upper bound on) the number of sinks and sources.
1982 * Sinks and sources are only extracted at leaves of the tree,
1983 * so we skip the node if it is not a leaf.
1984 * Otherwise we increment data->n_sink and data->n_source with
1985 * the number of spaces in the sink and source access domains
1986 * that reach this node.
1988 static int count_sink_source(__isl_keep isl_schedule_node
*node
, void *user
)
1990 struct isl_compute_flow_schedule_data
*data
= user
;
1991 isl_union_set
*domain
;
1992 isl_union_map
*umap
;
1995 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
1998 domain
= isl_schedule_node_get_universe_domain(node
);
2000 umap
= isl_union_map_copy(data
->access
->sink
);
2001 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2002 data
->n_sink
+= isl_union_map_n_map(umap
);
2003 isl_union_map_free(umap
);
2007 umap
= isl_union_map_copy(data
->access
->must_source
);
2008 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2009 data
->n_source
+= isl_union_map_n_map(umap
);
2010 isl_union_map_free(umap
);
2014 umap
= isl_union_map_copy(data
->access
->may_source
);
2015 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(domain
));
2016 data
->n_source
+= isl_union_map_n_map(umap
);
2017 isl_union_map_free(umap
);
2021 isl_union_set_free(domain
);
2026 /* Add a single scheduled sink or source (depending on data->set_sink)
2027 * with scheduled access relation "map", must property data->must and
2028 * schedule node data->node to the list of sinks or sources.
2030 static int extract_sink_source(__isl_take isl_map
*map
, void *user
)
2032 struct isl_compute_flow_schedule_data
*data
= user
;
2033 struct isl_scheduled_access
*access
;
2036 access
= data
->sink
+ data
->n_sink
++;
2038 access
= data
->source
+ data
->n_source
++;
2040 access
->access
= map
;
2041 access
->must
= data
->must
;
2042 access
->node
= isl_schedule_node_copy(data
->node
);
2047 /* isl_schedule_foreach_schedule_node callback for collecting
2048 * individual scheduled source and sink accesses.
2050 * We only collect accesses at the leaves of the schedule tree.
2051 * We prepend the schedule dimensions at the leaf to the iteration
2052 * domains of the source and sink accesses and then extract
2053 * the individual accesses (per space).
2055 * In particular, if the prefix schedule at the node is of the form
2059 * while the access relations are of the form
2063 * then the updated access relations are of the form
2067 * Note that S consists of a single space such that introducing S
2068 * in the access relations does not increase the number of spaces.
2070 static int collect_sink_source(__isl_keep isl_schedule_node
*node
, void *user
)
2072 struct isl_compute_flow_schedule_data
*data
= user
;
2073 isl_union_map
*prefix
;
2074 isl_union_map
*umap
;
2077 if (isl_schedule_node_get_type(node
) != isl_schedule_node_leaf
)
2082 prefix
= isl_schedule_node_get_prefix_schedule_union_map(node
);
2083 prefix
= isl_union_map_reverse(prefix
);
2084 prefix
= isl_union_map_range_map(prefix
);
2087 umap
= isl_union_map_copy(data
->access
->sink
);
2088 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2089 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2091 isl_union_map_free(umap
);
2095 umap
= isl_union_map_copy(data
->access
->must_source
);
2096 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2097 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2099 isl_union_map_free(umap
);
2103 umap
= isl_union_map_copy(data
->access
->may_source
);
2104 umap
= isl_union_map_apply_range(isl_union_map_copy(prefix
), umap
);
2105 if (isl_union_map_foreach_map(umap
, &extract_sink_source
, data
) < 0)
2107 isl_union_map_free(umap
);
2109 isl_union_map_free(prefix
);
2114 /* isl_access_info_compute_flow callback for determining whether
2115 * the shared nesting level and the ordering within that level
2116 * for two scheduled accesses for use in compute_single_flow.
2118 * The tokens passed to this function refer to the leaves
2119 * in the schedule tree where the accesses take place.
2121 * If n is the shared number of loops, then we need to return
2122 * "2 * n + 1" if "first" precedes "second" inside the innermost
2123 * shared loop and "2 * n" otherwise.
2125 * The innermost shared ancestor may be the leaves themselves
2126 * if the accesses take place in the same leaf. Otherwise,
2127 * it is either a set node or a sequence node. Only in the case
2128 * of a sequence node do we consider one access to precede the other.
2130 static int before_node(void *first
, void *second
)
2132 isl_schedule_node
*node1
= first
;
2133 isl_schedule_node
*node2
= second
;
2134 isl_schedule_node
*shared
;
2138 shared
= isl_schedule_node_get_shared_ancestor(node1
, node2
);
2142 depth
= isl_schedule_node_get_schedule_depth(shared
);
2143 if (isl_schedule_node_get_type(shared
) == isl_schedule_node_sequence
) {
2146 pos1
= isl_schedule_node_get_ancestor_child_position(node1
,
2148 pos2
= isl_schedule_node_get_ancestor_child_position(node2
,
2150 before
= pos1
< pos2
;
2153 isl_schedule_node_free(shared
);
2155 return 2 * depth
+ before
;
2158 /* Add the scheduled sources from "data" that access
2159 * the same data space as "sink" to "access".
2161 static __isl_give isl_access_info
*add_matching_sources(
2162 __isl_take isl_access_info
*access
, struct isl_scheduled_access
*sink
,
2163 struct isl_compute_flow_schedule_data
*data
)
2168 space
= isl_space_range(isl_map_get_space(sink
->access
));
2169 for (i
= 0; i
< data
->n_source
; ++i
) {
2170 struct isl_scheduled_access
*source
;
2171 isl_space
*source_space
;
2174 source
= &data
->source
[i
];
2175 source_space
= isl_map_get_space(source
->access
);
2176 source_space
= isl_space_range(source_space
);
2177 eq
= isl_space_is_equal(space
, source_space
);
2178 isl_space_free(source_space
);
2185 access
= isl_access_info_add_source(access
,
2186 isl_map_copy(source
->access
), source
->must
, source
->node
);
2189 isl_space_free(space
);
2192 isl_space_free(space
);
2193 isl_access_info_free(access
);
2197 /* Given a scheduled sink access relation "sink", compute the corresponding
2198 * dependences on the sources in "data" and add the computed dependences
2201 static __isl_give isl_union_flow
*compute_single_flow(
2202 __isl_take isl_union_flow
*uf
, struct isl_scheduled_access
*sink
,
2203 struct isl_compute_flow_schedule_data
*data
)
2206 isl_access_info
*access
;
2213 access
= isl_access_info_alloc(isl_map_copy(sink
->access
), sink
->node
,
2214 &before_node
, data
->n_source
);
2215 access
= add_matching_sources(access
, sink
, data
);
2217 flow
= isl_access_info_compute_flow(access
);
2219 return isl_union_flow_free(uf
);
2221 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 1));
2222 uf
->must_no_source
= isl_union_map_union(uf
->must_no_source
,
2223 isl_union_map_from_map(map
));
2224 map
= isl_map_domain_factor_range(isl_flow_get_no_source(flow
, 0));
2225 uf
->may_no_source
= isl_union_map_union(uf
->may_no_source
,
2226 isl_union_map_from_map(map
));
2228 for (i
= 0; i
< flow
->n_source
; ++i
) {
2231 map
= isl_map_factor_range(isl_map_copy(flow
->dep
[i
].map
));
2232 dep
= isl_union_map_from_map(map
);
2233 if (flow
->dep
[i
].must
)
2234 uf
->must_dep
= isl_union_map_union(uf
->must_dep
, dep
);
2236 uf
->may_dep
= isl_union_map_union(uf
->may_dep
, dep
);
2239 isl_flow_free(flow
);
2244 /* Given a description of the "sink" accesses, the "source" accesses and
2245 * a schedule, compute for each instance of a sink access
2246 * and for each element accessed by that instance,
2247 * the possible or definite source accesses that last accessed the
2248 * element accessed by the sink access before this sink access
2249 * in the sense that there is no intermediate definite source access.
2251 * The must_no_source and may_no_source elements of the result
2252 * are subsets of access->sink. The elements must_dep and may_dep
2253 * map domain elements of access->{may,must)_source to
2254 * domain elements of access->sink.
2256 * This function is used when a schedule tree representation
2259 * We extract the individual scheduled source and sink access relations and
2260 * then compute dependences for each scheduled sink individually.
2262 static __isl_give isl_union_flow
*compute_flow_schedule(
2263 __isl_take isl_union_access_info
*access
)
2265 struct isl_compute_flow_schedule_data data
= { access
};
2268 isl_union_flow
*flow
;
2270 ctx
= isl_union_access_info_get_ctx(access
);
2274 if (isl_schedule_foreach_schedule_node(access
->schedule
,
2275 &count_sink_source
, &data
) < 0)
2278 n
= data
.n_sink
+ data
.n_source
;
2279 data
.sink
= isl_calloc_array(ctx
, struct isl_scheduled_access
, n
);
2280 if (n
&& !data
.sink
)
2282 data
.source
= data
.sink
+ data
.n_sink
;
2286 if (isl_schedule_foreach_schedule_node(access
->schedule
,
2287 &collect_sink_source
, &data
) < 0)
2290 flow
= isl_union_flow_alloc(isl_union_map_get_space(access
->sink
));
2292 isl_compute_flow_schedule_data_align_params(&data
);
2294 for (i
= 0; i
< data
.n_sink
; ++i
)
2295 flow
= compute_single_flow(flow
, &data
.sink
[i
], &data
);
2297 isl_compute_flow_schedule_data_clear(&data
);
2299 isl_union_access_info_free(access
);
2302 isl_union_access_info_free(access
);
2303 isl_compute_flow_schedule_data_clear(&data
);
2307 /* Given a description of the "sink" accesses, the "source" accesses and
2308 * a schedule, compute for each instance of a sink access
2309 * and for each element accessed by that instance,
2310 * the possible or definite source accesses that last accessed the
2311 * element accessed by the sink access before this sink access
2312 * in the sense that there is no intermediate definite source access.
2314 * The must_no_source and may_no_source elements of the result
2315 * are subsets of access->sink. The elements must_dep and may_dep
2316 * map domain elements of access->{may,must)_source to
2317 * domain elements of access->sink.
2319 * We check whether the schedule is available as a schedule tree
2320 * or a schedule map and call the correpsonding function to perform
2323 __isl_give isl_union_flow
*isl_union_access_info_compute_flow(
2324 __isl_take isl_union_access_info
*access
)
2326 access
= isl_union_access_info_normalize(access
);
2329 if (access
->schedule
)
2330 return compute_flow_schedule(access
);
2332 return compute_flow_union_map(access
);
2335 /* Given a collection of "sink" and "source" accesses,
2336 * compute for each iteration of a sink access
2337 * and for each element accessed by that iteration,
2338 * the source access in the list that last accessed the
2339 * element accessed by the sink access before this sink access.
2340 * Each access is given as a map from the loop iterators
2341 * to the array indices.
2342 * The result is a relations between source and sink
2343 * iterations and a subset of the domain of the sink accesses,
2344 * corresponding to those iterations that access an element
2345 * not previously accessed.
2347 * We collect the inputs in an isl_union_access_info object,
2348 * call isl_union_access_info_compute_flow and extract
2349 * the outputs from the result.
2351 int isl_union_map_compute_flow(__isl_take isl_union_map
*sink
,
2352 __isl_take isl_union_map
*must_source
,
2353 __isl_take isl_union_map
*may_source
,
2354 __isl_take isl_union_map
*schedule
,
2355 __isl_give isl_union_map
**must_dep
, __isl_give isl_union_map
**may_dep
,
2356 __isl_give isl_union_map
**must_no_source
,
2357 __isl_give isl_union_map
**may_no_source
)
2359 isl_union_access_info
*access
;
2360 isl_union_flow
*flow
;
2362 access
= isl_union_access_info_from_sink(sink
);
2363 access
= isl_union_access_info_set_must_source(access
, must_source
);
2364 access
= isl_union_access_info_set_may_source(access
, may_source
);
2365 access
= isl_union_access_info_set_schedule_map(access
, schedule
);
2366 flow
= isl_union_access_info_compute_flow(access
);
2369 *must_dep
= isl_union_flow_get_must_dependence(flow
);
2371 *may_dep
= isl_union_flow_get_non_must_dependence(flow
);
2373 *must_no_source
= isl_union_flow_get_must_no_source(flow
);
2375 *may_no_source
= isl_union_flow_get_non_must_no_source(flow
);
2377 isl_union_flow_free(flow
);
2379 if ((must_dep
&& !*must_dep
) || (may_dep
&& !*may_dep
) ||
2380 (must_no_source
&& !*must_no_source
) ||
2381 (may_no_source
&& !*may_no_source
))
2387 *must_dep
= isl_union_map_free(*must_dep
);
2389 *may_dep
= isl_union_map_free(*may_dep
);
2391 *must_no_source
= isl_union_map_free(*must_no_source
);
2393 *may_no_source
= isl_union_map_free(*may_no_source
);